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

secretsmanager: Removal Policy set on the secret is not applied to the Resource Policy of the secret #30408

Closed
grbinho opened this issue May 31, 2024 · 3 comments
Assignees
Labels
@aws-cdk/aws-secretsmanager Related to AWS Secrets Manager bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@grbinho
Copy link

grbinho commented May 31, 2024

Describe the bug

If removalPolicy of a secret is set to retain, that is not propagated to ResourcePolicy created/updated for that secret by calling addToResourcePolicy.

Expected Behavior

When removalPolicy is set to RETAIN and secret resource removed from CDK code, I expected that ResourcePolicy of that secret is also retained.

Current Behavior

When removalPolicy is set to RETAIN, and secret resource is removed from CDK code, ResourcePolicy of the secret is deleted.

Reproduction Steps

To replicate this, first create a Secret with a custom resource policy.

  const awsSecret = new Secret(this,'my-secret', {
        secretName: 'my-secret',
        encryptionKey: kmsKey    
        removalPolicy: RemovalPolicy.RETAIN
      })

   awsSecret.addToResourcePolicy(
        new iam.PolicyStatement({
          effect: iam.Effect.ALLOW,
          principals: [myRole],
          actions: ['secretsmanager:DescribeSecret', 'secretsmanager:GetSecretValue'],
          resources: ['*']
        })
      )

Change CDK code and remove the above snipped.
Expectation is that both secret and policy remain in AWS account after deploying.

Possible Solution

removalPolicy is not applied to the ResourcePolicy construct in addToResourcePolicy function.
https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-secretsmanager/lib/secret.ts#L438

applyRemovalPolicy does not consider secrets resource policy
https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-secretsmanager/lib/secret.ts#L655

Additional Information/Context

No response

CDK CLI Version

2.118.0 (build a40f2ec)

Framework Version

No response

Node.js Version

v18.20.2

OS

MacOS Sonoma 14.5 (23F79)

Language

TypeScript

Language Version

5.4.5

Other information

No response

@grbinho grbinho added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 31, 2024
@github-actions github-actions bot added the @aws-cdk/aws-secretsmanager Related to AWS Secrets Manager label May 31, 2024
@khushail khushail added needs-reproduction This issue needs reproduction. and removed needs-triage This issue or PR still needs to be triaged. labels Jun 3, 2024
@khushail khushail self-assigned this Jun 3, 2024
@khushail khushail added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-reproduction This issue needs reproduction. labels Jul 10, 2024
@khushail
Copy link
Contributor

@grbinho , thanks for reaching out.

RemovalPolicy is not set on Resource policy because of the following -

  1. Both Secret and SecretResourcePolicy are treated as separate resource as per CDK. It can be verified looking at this cdk diff -
Resources
[+] AWS::KMS::Key KeyTest KeyTestDB4145CA 
[+] AWS::SecretsManager::Secret my-secret-01 mysecret01CE6DE629 
[+] AWS::SecretsManager::ResourcePolicy my-secret-01/Policy mysecret01PolicyB5B07F62 
  1. When addToResourcePolicy() is called, it creates a child resource of SecretManager and add the policy statement to the corresponding Secret. See this -

    public addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult {

  2. when CDK Stack is deleted, the corresponding resources are also deleted which means ResourcePolicy resource (my-secret-01/Policy mysecret01PolicyB5B07F62) is also deleted. but since Secret has retain removalPolicy, it remains existent without CDK Control but the resource policy gets removed from it. Hence there is no resource policy for the secret.

  3. If Resource policy is to be maintained at the Secret, one needs to add the RemovalPolicy to the resource policy too and pass it down to L1 resource ,something like this -

    // find the child L2
    const resourcePolicy = awsSecret.node.tryFindChild('Policy') as secrets.ResourcePolicy
    // find the L1
    const cfnResourcePoilcy = resourcePolicy.node.defaultChild as secrets.CfnResourcePolicy
    // now apply the removalPolicy from the L1
    cfnResourcePoilcy.applyRemovalPolicy(RemovalPolicy.RETAIN);

    Hope that helps ! Let me know if there are any questions.

    cc: @pahud (Thanks for guidance here)!

@khushail khushail added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Jul 11, 2024
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Jul 13, 2024
@aws-cdk-automation
Copy link
Collaborator

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.

@aws aws locked as resolved and limited conversation to collaborators Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-secretsmanager Related to AWS Secrets Manager bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

3 participants