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

fix(secretsmanager): cannot import secrets if ARN is a token #10568

Merged
merged 2 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions 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, IConstruct, IResource, RemovalPolicy, Resource, SecretValue, Stack } from '@aws-cdk/core';
import { Construct, IConstruct, IResource, RemovalPolicy, Resource, SecretValue, Stack, Token } from '@aws-cdk/core';
import { ResourcePolicy } from './policy';
import { RotationSchedule, RotationScheduleOptions } from './rotation-schedule';
import * as secretsmanager from './secretsmanager.generated';
Expand Down Expand Up @@ -596,8 +596,13 @@ export interface SecretStringGenerator {

/** Parses the secret name from the ARN. */
function parseSecretName(construct: IConstruct, secretArn: string) {
const resourceName = Stack.of(construct).parseArn(secretArn).resourceName;
const resourceName = Stack.of(construct).parseArn(secretArn, ':').resourceName;
if (resourceName) {
// Can't operate on the token to remove the SecretsManager suffix, so just return the full secret name
if (Token.isUnresolved(resourceName)) {
return resourceName;
}

// Secret resource names are in the format `${secretName}-${SecretsManager suffix}`
const secretNameFromArn = resourceName.substr(0, resourceName.lastIndexOf('-'));
if (secretNameFromArn) { return secretNameFromArn; }
Expand Down
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-secretsmanager/test/test.secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,30 @@ export = {
test.done();
},

'import by secretArn supports tokens for ARNs'(test: Test) {
// GIVEN
const app = new cdk.App();
const stackA = new cdk.Stack(app, 'StackA');
const stackB = new cdk.Stack(app, 'StackB');
const secretA = new secretsmanager.Secret(stackA, 'SecretA');

// WHEN
const secretB = secretsmanager.Secret.fromSecretArn(stackB, 'SecretB', secretA.secretArn);
new cdk.CfnOutput(stackB, 'secretBSecretName', { value: secretB.secretName });

// THEN
test.equals(secretB.secretArn, secretA.secretArn);
expect(stackB).toMatch({
Outputs: {
secretBSecretName: {
Value: { 'Fn::Select': [6, { 'Fn::Split': [':', { 'Fn::ImportValue': 'StackA:ExportsOutputRefSecretA188F281703FC8A52' }] }] },
},
},
});

test.done();
},

'import by attributes'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down