cloudformation-coverage-roadmap icon indicating copy to clipboard operation
cloudformation-coverage-roadmap copied to clipboard

Force resolve dynamic secret references in change set creation

Open mschweitzer-sd opened this issue 5 years ago • 10 comments

1. Title

Force resolve dynamic secret references in change set creation

2. Scope of request

Currently, if you have a dynamic reference to a secret, and don't specify the version, change set creation fails with the error message stating that there are no changes to the template, even if the secret value changed.

It would be great if we could pass either a flag or a capability to change set creation to force CloudFormation to detect and re-resolve dynamic references to secrets when you want the latest version of the secret.

We use a lot of CodePipeline, and for us, it is especially necessary for this to be supported by the CHANGE_SET_REPLACE CloudFormation action.

3. Expected behavior

When I leave off the version of a secret, it means I want to use the latest version, and that means when I change the secret value, I expect a change set to be created and for the change set to use the updated value.

4. Suggest specific test cases

Say you have a reference in a template Foo like so: {{resolve:secretsmanager:MySecret:SecretString:password}}

  1. Go to SecretsManager
  2. Retrieve and edit the password for MySecret
  3. Create a change set for Foo (management console or CLI) --> Change set should be created and executed with the new password

Currently, change set creation fails.

5. Helpful Links to speed up research and evaluation

  • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html#dynamic-references-secretsmanager
  • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-changesets-create.html
  • https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CloudFormation.html
  • https://docs.aws.amazon.com/secretsmanager/latest/userguide/integrating_cloudformation.html

6. Category (required) - Will help with tagging and be easier to find by other users to +1

Security, Developer Tools, Enhancement

mschweitzer-sd avatar Feb 10 '20 17:02 mschweitzer-sd

This issue is quite a blocker for me (I am using complex cloudformation setup) - same as referencing latest version of ssm parameter. This two improvements would really help hundreds of people working with complex cf setups.

@mschweitzer-sd I was thinking of a workaround - create a macro or custom resource which would update a cf output with time info in it - every create changeset will trigger the update as the time info won't be the same as previously set.

mliner avatar Mar 24 '20 15:03 mliner

@mliner lol yeah I could see that working, neat idea!

mschweitzer-sd avatar Mar 31 '20 17:03 mschweitzer-sd

Please please please add this. CloudFormation doesn't have a great way to roll out changes which affect resources in other templates, and we would really appreciate having that functionality. We already had to give up native imports since errors are thrown when the changes cause new resources to be created, changing the values of the exported services.

dentonmwood avatar May 30 '21 03:05 dentonmwood

+1 for this. For now, I am ok with adding an extra input param (date), but a native solution would be much preferred.

dudeitssm avatar Aug 31 '21 16:08 dudeitssm

Yes, please please please add this. We have some SAM stacks that use 'resolve' from secretsmanager in template.yaml, and without this functionality we cannot rotate the secret.

mcIovin avatar Oct 24 '22 22:10 mcIovin

+1. If this is working with SSM parameters, it would be consistent to implement with SecretsManager entries, too.

fabiatz avatar Feb 28 '23 17:02 fabiatz

Also blocked by this.

yongzhang avatar Jun 21 '23 02:06 yongzhang

+1. our team ran into this problem too (e.g. if you want the API Gateway authorizer token to be copied up from the latest version of a secret)

  MyAuthorizer:
    Properties:
      IdentityValidationExpression: '{{resolve:secretsmanager:my-authorizer:SecretString:token}}'

InQuirer avatar Dec 21 '23 09:12 InQuirer

Has anyone tried this? https://www.amazonaws.cn/en/new/2024/amazon-cloudformation-improves-changesets-to-enable-safer-deployment-practices/ https://aws.amazon.com/about-aws/whats-new/2024/04/aws-cloudformation-changesets-enhanced-change-visibility-deployments/

image

I did a test today but no luck, but I do see cfn created changeset successfully by updating secret values, unfortunately cfn didn't update my resource (ECS task definition) with updated secret value even though I can see "Replacement: true" in changeset.

yongzhang avatar Apr 26 '24 12:04 yongzhang

for now, I did the following workaround @mliner proposed:

AWSTemplateFormatVersion: 2010-09-09
Resources:

    SecretExpanderMacro:
        Type: AWS::CloudFormation::Macro
        Properties:
            Name: SecretExpanderMacro # use this macro name is Transform section
            Description: Replaces resolve:secretsmanager with the exact latest version
            FunctionName: !Ref SecretExpanderLambda

    SecretExpanderLambda:
        Type: AWS::Serverless::Function
        Properties:
            CodeUri: ./expand_secrets_macro.py # see lambda code gist link below
            MemorySize: 128
            Timeout: 60 # seconds
            Handler: expand_secrets_macro.lambda_handler
            Runtime: python3.11
            Architectures:
                - x86_64
            EventInvokeConfig:
                MaximumEventAgeInSeconds: 60
                MaximumRetryAttempts: 2
            Policies:
                - Statement:
                      - Effect: Allow
                        Action:
                            - logs:CreateLogGroup
                            - logs:CreateLogStream
                            - logs:PutLogEvents
                        Resource:
                            - !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
                      - Effect: Allow
                        Action:
                            - secretsmanager:GetSecretValue
                        Resource:
                            - !Sub arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:*

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

usage:

AWSTemplateFormatVersion: 2010-09-09
# ...

Transform:
  - SecretExpanderMacro

lambda code: https://gist.github.com/InQuirer/a7c5be8004ab4b78744a992ef58facc1

InQuirer avatar Apr 26 '24 15:04 InQuirer