Skip to content

Commit

Permalink
feat(secretsmanager): deletionPolicy for secretsmanager (aws#8188)
Browse files Browse the repository at this point in the history
We often store important values on secretsmanager.Secret.
But, without DeletionPolicy(Retain), it can be deleted by human error.
So, add DeletionPolicy to secretsmanager.Secret's initialization Props.

closes: aws#6527

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
winky authored Jun 8, 2020
1 parent 02ddab8 commit f6fe36a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-secretsmanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const secret = secretsmanager.Secret.fromSecretAttributes(scope, 'ImportedSecret
SecretsManager secret values can only be used in select set of properties. For the
list of properties, see [the CloudFormation Dynamic References documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html).

A secret can set `RemovalPolicy`. If it set to `RETAIN`, that removing a secret will fail.

### Grant permission to use the secret to a role

You must grant permission to a resource for that resource to be allowed to
Expand Down
13 changes: 12 additions & 1 deletion packages/@aws-cdk/aws-secretsmanager/lib/secret.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import { Construct, IResource, Resource, SecretValue, Stack } from '@aws-cdk/core';
import { Construct, IResource, RemovalPolicy, Resource, SecretValue, Stack } from '@aws-cdk/core';
import { ResourcePolicy } from './policy';
import { RotationSchedule, RotationScheduleOptions } from './rotation-schedule';
import * as secretsmanager from './secretsmanager.generated';
Expand Down Expand Up @@ -102,6 +102,13 @@ export interface SecretProps {
* @default - A name is generated by CloudFormation.
*/
readonly secretName?: string;

/**
* Policy to apply when the secret is removed from this stack.
*
* @default - Not set.
*/
readonly removalPolicy?: RemovalPolicy;
}

/**
Expand Down Expand Up @@ -260,6 +267,10 @@ export class Secret extends SecretBase {
name: this.physicalName,
});

if (props.removalPolicy) {
resource.applyRemovalPolicy(props.removalPolicy);
}

this.secretArn = this.getResourceArnAttribute(resource.ref, {
service: 'secretsmanager',
resource: 'secret',
Expand Down
21 changes: 20 additions & 1 deletion packages/@aws-cdk/aws-secretsmanager/test/test.secret.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert';
import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/assert';
import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import * as lambda from '@aws-cdk/aws-lambda';
Expand All @@ -22,6 +22,25 @@ export = {
test.done();
},

'set removalPolicy to secret'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new secretsmanager.Secret(stack, 'Secret', {
removalPolicy: cdk.RemovalPolicy.RETAIN,
});

// THEN
expect(stack).to(haveResourceLike('AWS::SecretsManager::Secret',
{
DeletionPolicy: 'Retain',
}, ResourcePart.CompleteDefinition,
));

test.done();
},

'secret with kms'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit f6fe36a

Please sign in to comment.