Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not use Fn::if within CustomStatements: #1218

Closed
eerb-kyruus opened this issue Oct 23, 2019 · 3 comments
Closed

Can not use Fn::if within CustomStatements: #1218

eerb-kyruus opened this issue Oct 23, 2019 · 3 comments
Labels
area/intrinsics Ref, If, Sub, GetAtt, ... type/bug

Comments

@eerb-kyruus
Copy link

eerb-kyruus commented Oct 23, 2019

Description:

I am attempting to write a custom policy for my Serverless api that depends on deployment type. The serverless transform expects a list inside the CustomStatements: label, preventing use of fn:If to conditionally set policies. The obvious work-around is to specify the entire swagger document myself, but this creates lots of duplication and a huge template.

Steps to reproduce the issue:
NOTE: Confidential parts of the template have been redacted

  1. Create a serverless API template with this in the conditional section:
    Conditions:
    IsProd:
  2. Create a conditional resource policy like the following in the AWS:Serverless:Api Auth: section:
        ResourcePolicy:
           CustomStatements:
            Fn::If:
            - IsProd
            - - Principal: '*'
                Effect: Allow
                Action: <redacted>
                Resource: <redacted>
            - - Principal: '*'
                Effect: Allow
                Action: <redacted>
                Resource: <redacted>
                Condition:
                  <redacted>

Observed result:
The macro creates the following within the swagger doc, reformatted for readability. Note carefully the extra set of square brackets around the policy statement:

"x-amazon-apigateway-policy":
  {
  "Version":"2012-10-17","Statement":
    [
      [
        {
        Condition":
          {
             <redacted>
         },
        "Action": <redacted>
        "Resource": <redacted>
        "Effect":"Allow",
        "Principal":"*"
        }
      ]
   ]
  },

The deploy fails due to that extra set of square brackets in the policy statement

Expected result:
The swagger gateway policy is created with a single set of square brackets and the deploy succeeds.

@jlhood
Copy link
Contributor

jlhood commented Oct 23, 2019

@eerb-kyruus Thanks for reporting this. Looks like this code isn't taking intrinsic functions into account:

https://github.com/awslabs/serverless-application-model/blob/440790a8c2278fc16acbe34c83183668c5f40c38/samtranslator/swagger/swagger.py#L987-L988

For now, a possible workaround would be to embed the Fn::If inside each statement, e.g.,:

        ResourcePolicy:
           CustomStatements:
            - !If
              - IsProd
              - Principal: '*'
                Effect: Allow
                Action: <redacted>
                Resource: <redacted>
              - !Ref AWS::NoValue
            - !If
              - IsProd
              - Principal: '*'
                Effect: Allow
                Action: <redacted>
                Resource: <redacted>
                Condition:
                  <redacted>
              - !Ref AWS::NoValue

I'm not sure what CloudFormation will do in the case that all custom statements have this condition and the condition is false. Hopefully it will treat this as an empty list. However, it may throw an error due to all child values resolving to AWS::NoValue.

@jlhood jlhood added contributors/good-first-issue Good first issue for a contributor contributors/welcome Contributors are welcome to work on this type/bug labels Oct 23, 2019
@praneetap praneetap removed contributors/good-first-issue Good first issue for a contributor contributors/welcome Contributors are welcome to work on this good first issue labels Jan 22, 2020
@wchengru wchengru added the area/intrinsics Ref, If, Sub, GetAtt, ... label Mar 10, 2021
@hoffa
Copy link
Contributor

hoffa commented Oct 17, 2022

You might be able to get this to work by adding AWS::LanguageExtensions to Transform as such:

Transform:
  - AWS::LanguageExtensions
  - AWS::Serverless-2016-10-31

AWS::LanguageExtensions resolves intrinsic functions if the value is known when Transforms are run.

See #2533 for more information.

@hoffa
Copy link
Contributor

hoffa commented Nov 3, 2022

Closing in favor of #2533.

@hoffa hoffa closed this as completed Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/intrinsics Ref, If, Sub, GetAtt, ... type/bug
Projects
None yet
Development

No branches or pull requests

6 participants