Skip to content

Commit

Permalink
feat(secretsmanager): deletionPolicy for secretsmanager
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.

closes: aws#6527
  • Loading branch information
winky committed May 25, 2020
1 parent 3fed84b commit cee70dc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
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 cee70dc

Please sign in to comment.