-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsecret-value.test.ts
207 lines (163 loc) · 5.64 KB
/
secret-value.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
import { CfnDynamicReference, CfnDynamicReferenceService, CfnParameter, SecretValue, Stack, Token, App, CfnResource } from '../lib';
describe('secret value', () => {
testDeprecated('plainText', () => {
// GIVEN
const stack = new Stack();
// WHEN
const v = SecretValue.plainText('this just resolves to a string');
// THEN
expect(stack.resolve(v)).toEqual('this just resolves to a string');
});
test('unsafePlainText', () => {
// GIVEN
const stack = new Stack();
// WHEN
const v = SecretValue.unsafePlainText('this just resolves to a string');
// THEN
expect(stack.resolve(v)).toEqual('this just resolves to a string');
});
test('isSecretValue returns true', () => {
const v = SecretValue.unsafePlainText('this just resolves to a string');
// THEN
expect(SecretValue.isSecretValue(v)).toEqual(true);
});
test('secret resolution fails if feature flag is switched on, secret can be unwrapped', () => {
const app = new App({
context: { '@aws-cdk/core:checkSecretUsage': true },
});
const stack = new Stack(app, 'Stack');
// WHEN
const v = SecretValue.unsafePlainText('s3cr3t');
// THEN
expect(() => stack.resolve(v)).toThrow(/Using a SecretValue here risks exposing your secret/);
// THEN
expect(stack.resolve(v.unsafeUnwrap())).toEqual('s3cr3t');
});
test('secretsManager', () => {
// GIVEN
const stack = new Stack();
// WHEN
const v = SecretValue.secretsManager('secret-id', {
jsonField: 'json-key',
versionStage: 'version-stage',
});
// THEN
expect(stack.resolve(v)).toEqual('{{resolve:secretsmanager:secret-id:SecretString:json-key:version-stage:}}');
});
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();
// WHEN
const v = SecretValue.secretsManager('secret-id');
// THEN
expect(stack.resolve(v)).toEqual('{{resolve:secretsmanager:secret-id:SecretString:::}}');
});
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/);
});
test('secretsManager with versionStage and versionId', () => {
expect(() => {
SecretValue.secretsManager('secret-id',
{
versionStage: 'version-stage',
versionId: 'version-id',
});
}).toThrow(/were both provided but only one is allowed/);
});
test('secretsManager with a non-ARN ID that has colon', () => {
expect(() => SecretValue.secretsManager('not:an:arn')).toThrow(/is not an ARN but contains ":"/);
});
test('ssmSecure', () => {
// GIVEN
const stack = new Stack();
// WHEN
const v = SecretValue.ssmSecure('param-name', 'param-version');
// THEN
expect(stack.resolve(v)).toEqual('{{resolve:ssm-secure:param-name:param-version}}');
});
test('ssmSecure without version', () => {
// GIVEN
const stack = new Stack();
// WHEN
const v = SecretValue.ssmSecure('param-name');
// THEN
expect(stack.resolve(v)).toEqual('{{resolve:ssm-secure:param-name}}');
});
test('cfnDynamicReference', () => {
// GIVEN
const stack = new Stack();
// WHEN
const v = SecretValue.cfnDynamicReference(new CfnDynamicReference(CfnDynamicReferenceService.SSM, 'foo:bar'));
// THEN
expect(stack.resolve(v)).toEqual('{{resolve:ssm:foo:bar}}');
});
test('cfnParameter (with NoEcho)', () => {
// GIVEN
const stack = new Stack();
const p = new CfnParameter(stack, 'MyParam', { type: 'String', noEcho: true });
// WHEN
const v = SecretValue.cfnParameter(p);
// THEN
expect(stack.resolve(v)).toEqual({ Ref: 'MyParam' });
});
test('fails if cfnParameter does not have NoEcho', () => {
// GIVEN
const stack = new Stack();
const p = new CfnParameter(stack, 'MyParam', { type: 'String' });
// THEN
expect(() => SecretValue.cfnParameter(p)).toThrow(/CloudFormation parameter must be configured with "NoEcho"/);
});
test('resourceAttribute does not work on literal', () => {
expect(() => SecretValue.resourceAttribute('xyz')).toThrow(/must be used with/);
});
test('resourceAttribute does not work on plain ref', () => {
const stack = new Stack();
const param = new CfnParameter(stack, 'Param');
expect(() => SecretValue.resourceAttribute(param.valueAsString)).toThrow(/must be used with/);
});
test('resourceAttribute works on actual resource attribute', () => {
const stack = new Stack();
const res = new CfnResource(stack, 'Resource', {
type: 'AWS::My::Resource',
});
expect(stack.resolve(SecretValue.resourceAttribute(res.ref))).toEqual({ Ref: 'Resource' });
});
});