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(core): SecretValue.secretsManager fails for tokenized secret-id #16230

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion packages/@aws-cdk/core/lib/secret-value.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CfnDynamicReference, CfnDynamicReferenceService } from './cfn-dynamic-reference';
import { CfnParameter } from './cfn-parameter';
import { Intrinsic } from './private/intrinsic';
import { Token } from './token';

/**
* Work with secret values in the CDK
Expand Down Expand Up @@ -39,7 +40,7 @@ export class SecretValue extends Intrinsic {
throw new Error('secretId cannot be empty');
}

if (!secretId.startsWith('arn:') && secretId.includes(':')) {
if (!Token.isUnresolved(secretId) && !secretId.startsWith('arn:') && secretId.includes(':')) {
throw new Error(`secret id "${secretId}" is not an ARN but contains ":"`);
}

Expand Down
47 changes: 46 additions & 1 deletion packages/@aws-cdk/core/test/secret-value.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CfnDynamicReference, CfnDynamicReferenceService, CfnParameter, SecretValue, Stack } from '../lib';
import { CfnDynamicReference, CfnDynamicReferenceService, CfnParameter, SecretValue, Stack, Token } from '../lib';

describe('secret value', () => {
test('plainText', () => {
Expand Down Expand Up @@ -28,6 +28,30 @@ describe('secret value', () => {

});

test('secretsManager with secret-id from token', () => {
// GIVEN
const stack = new Stack();

// WHEN
const v = SecretValue.secretsManager(Token.asString({ Ref: 'secret-id' }), {
jsonField: 'json-key',
versionStage: 'version-stage',
});

// THEN
expect(stack.resolve(v)).toEqual({
'Fn::Join': [
'',
[
'{{resolve:secretsmanager:',
{ Ref: 'secret-id' },
':SecretString:json-key:version-stage:}}',
],
],
});

});

test('secretsManager with defaults', () => {
// GIVEN
const stack = new Stack();
Expand All @@ -40,6 +64,27 @@ describe('secret value', () => {

});

test('secretsManager with defaults, secret-id from token', () => {
// GIVEN
const stack = new Stack();

// WHEN
const v = SecretValue.secretsManager(Token.asString({ Ref: 'secret-id' }));

// THEN
expect(stack.resolve(v)).toEqual({
'Fn::Join': [
'',
[
'{{resolve:secretsmanager:',
{ Ref: 'secret-id' },
':SecretString:::}}',
],
],
});

});

test('secretsManager with an empty ID', () => {
expect(() => SecretValue.secretsManager('')).toThrow(/secretId cannot be empty/);

Expand Down