Skip to content

Commit b93350f

Browse files
authored
fix(secretsmanager/ssm): verify presence of parameter name (#2066)
Throw an error if Secrets or SSM Parameter are referenced with an empty name. This adds clear messaging around an otherwise obscure CloudFormation error.
1 parent abacc66 commit b93350f

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

packages/@aws-cdk/aws-secretsmanager/lib/secret-string.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export class SecretString extends cdk.DynamicReference {
3939
service: cdk.DynamicReferenceService.SecretsManager,
4040
referenceKey: '',
4141
});
42+
43+
// If we don't validate this here it will lead to a very unclear
44+
// error message in CloudFormation, so better do it.
45+
if (!props.secretId) {
46+
throw new Error('SecretString: secretId cannot be empty');
47+
}
4248
}
4349

4450
/**

packages/@aws-cdk/aws-secretsmanager/test/test.secret-string.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,18 @@ export = {
3232

3333
test.done();
3434
},
35+
36+
'empty secretId will throw'(test: Test) {
37+
// GIVEN
38+
const stack = new cdk.Stack();
39+
40+
// WHEN
41+
test.throws(() => {
42+
new secretsmanager.SecretString(stack, 'Ref', {
43+
secretId: '',
44+
});
45+
}, /secretId cannot be empty/);
46+
47+
test.done();
48+
},
3549
};

packages/@aws-cdk/aws-ssm/lib/parameter-store-string.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export class ParameterStoreString extends cdk.Construct {
2828
constructor(scope: cdk.Construct, id: string, props: ParameterStoreStringProps) {
2929
super(scope, id);
3030

31+
// If we don't validate this here it will lead to a very unclear
32+
// error message in CloudFormation, so better do it.
33+
if (!props.parameterName) {
34+
throw new Error('ParameterStoreString: parameterName cannot be empty');
35+
}
36+
3137
// We use a different inner construct depend on whether we want the latest
3238
// or a specific version.
3339
//
@@ -80,5 +86,11 @@ export class ParameterStoreSecureString extends cdk.DynamicReference {
8086
service: cdk.DynamicReferenceService.SsmSecure,
8187
referenceKey: `${props.parameterName}:${props.version}`,
8288
});
89+
90+
// If we don't validate this here it will lead to a very unclear
91+
// error message in CloudFormation, so better do it.
92+
if (!props.parameterName) {
93+
throw new Error('ParameterStoreSecureString: parameterName cannot be empty');
94+
}
8395
}
8496
}

packages/@aws-cdk/aws-ssm/test/test.parameter-store-string.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,18 @@ export = {
5959

6060
test.done();
6161
},
62+
63+
'empty parameterName will throw'(test: Test) {
64+
// GIVEN
65+
const stack = new cdk.Stack();
66+
67+
// WHEN
68+
test.throws(() => {
69+
new ssm.ParameterStoreString(stack, 'Ref', {
70+
parameterName: '',
71+
});
72+
}, /parameterName cannot be empty/);
73+
74+
test.done();
75+
},
6276
};

0 commit comments

Comments
 (0)