@@ -24,29 +24,51 @@ const runtime: string = process.env.RUNTIME || 'nodejs18x';
24
24
if ( ! isValidRuntimeKey ( runtime ) ) {
25
25
throw new Error ( `Invalid runtime key: ${ runtime } ` ) ;
26
26
}
27
-
28
- const uuid = v4 ( ) ;
29
- const stackName = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'secretsProvider' ) ;
30
- const functionName = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'secretsProvider' ) ;
31
- const lambdaFunctionCodeFile = 'secretsProvider.class.test.functionCode.ts' ;
32
-
33
- const secretNamePlain = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretPlain' ) ;
34
- const secretNamePlainCached = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretPlainCached' ) ;
35
- const secretNameObject = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretObject' ) ;
36
- const secretNameBinary = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretBinary' ) ;
37
- const secretNameObjectWithSuffix = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretObject.json' ) ;
38
- const secretNameBinaryWithSuffix = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretObject.binary' ) ;
39
-
40
- const invocationCount = 1 ;
41
-
42
- const integTestApp = new App ( ) ;
43
- let stack : Stack ;
44
-
27
+ /**
28
+ * Collection of e2e tests for SecretsProvider utility.
29
+ *
30
+ * Test 1: create a secret with plain text value, fetch it with no additional options
31
+ * Test 2: create a secret with json value, fetch it using `transform: 'json'` option
32
+ * Test 3: create a secret with base64 encoded value (technicaly string), fetch it using `transform: 'binary'` option
33
+ * Test 4: create a secret with json value and secret name ends with .json, fetch it using `transform: 'auto'` option
34
+ * Test 5: create a secret with base64 encoded value (technicaly string) and secert name ends with .binary, fetch it using `transform: 'auto'` option
35
+ * Test 6: create a secret with plain text value, fetch it twice, check that value was cached, the number of SDK calls should be 1
36
+ * Test 7: create a secret with plain text value, fetch it twice, second time with `forceFetch: true` option, check that value was not cached, the number of SDK calls should be 2
37
+ *
38
+ * For tests 6 and 7 we use our own AWS SDK custom middleware plugin `sdkMiddlewareRequestCounter.ts`
39
+ *
40
+ * Adding new test:
41
+ * Please keep the state clean, and create dedicated resource for your test, don't reuse resources from other tests.
42
+ * Pass the necessary information to lambda function by using enviroment variables
43
+ * Make sure to add the right permissions to the lambda function to access the resources. We use our `ResourceAccessGranter` to add permissions.
44
+ *
45
+ */
45
46
describe ( `parameters E2E tests (SecretsProvider) for runtime: ${ runtime } ` , ( ) => {
46
47
48
+ const uuid = v4 ( ) ;
47
49
let invocationLogs : InvocationLogs [ ] ;
50
+ const stackName = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'secretsProvider' ) ;
51
+ const functionName = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'secretsProvider' ) ;
52
+ const lambdaFunctionCodeFile = 'secretsProvider.class.test.functionCode.ts' ;
53
+
54
+ const invocationCount = 1 ;
55
+
56
+ const integTestApp = new App ( ) ;
57
+ let stack : Stack ;
48
58
49
59
beforeAll ( async ( ) => {
60
+
61
+ // use unique names for each test to keep a clean state
62
+ const secretNamePlain = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretPlain' ) ;
63
+ const secretNameObject = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretObject' ) ;
64
+ const secretNameBinary = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretBinary' ) ;
65
+ const secretNameObjectWithSuffix = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretObject.json' ) ;
66
+ const secretNameBinaryWithSuffix = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretObject.binary' ) ;
67
+ const secretNamePlainCached = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretPlainCached' ) ;
68
+ const secretNamePlainForceFetch = generateUniqueName ( RESOURCE_NAME_PREFIX , uuid , runtime , 'testSecretPlainForceFetch' ) ;
69
+
70
+ // creates the test fuction that uses powertools secret provider we want to test
71
+ // pass env vars with secret names we want to fetch
50
72
stack = createStackWithLambdaFunction ( {
51
73
app : integTestApp ,
52
74
stackName : stackName ,
@@ -61,6 +83,7 @@ describe(`parameters E2E tests (SecretsProvider) for runtime: ${runtime}`, () =>
61
83
SECRET_NAME_OBJECT_WITH_SUFFIX : secretNameObjectWithSuffix ,
62
84
SECRET_NAME_BINARY_WITH_SUFFIX : secretNameBinaryWithSuffix ,
63
85
SECRET_NAME_PLAIN_CACHED : secretNamePlainCached ,
86
+ SECRET_NAME_PLAIN_FORCE_FETCH : secretNamePlainForceFetch ,
64
87
} ,
65
88
runtime : runtime
66
89
} ) ;
@@ -99,7 +122,14 @@ describe(`parameters E2E tests (SecretsProvider) for runtime: ${runtime}`, () =>
99
122
secretStringValue : SecretValue . unsafePlainText ( 'foo' )
100
123
} ) ;
101
124
102
- Aspects . of ( stack ) . add ( new ResourceAccessGranter ( [ secretString , secretObject , secretBinary , secretObjectWithSuffix , secretBinaryWithSuffix , secretStringCached ] ) ) ;
125
+ const secretStringForceFetch = new Secret ( stack , 'testSecretStringForceFetch' , {
126
+ secretName : secretNamePlainForceFetch ,
127
+ secretStringValue : SecretValue . unsafePlainText ( 'foo' )
128
+ } ) ;
129
+
130
+ // add secrets here to grant lambda permisisons to access secrets
131
+ Aspects . of ( stack ) . add ( new ResourceAccessGranter ( [
132
+ secretString , secretObject , secretBinary , secretObjectWithSuffix , secretBinaryWithSuffix , secretStringCached , secretStringForceFetch ] ) ) ;
103
133
104
134
await deployStack ( integTestApp , stack ) ;
105
135
@@ -108,7 +138,8 @@ describe(`parameters E2E tests (SecretsProvider) for runtime: ${runtime}`, () =>
108
138
} , SETUP_TIMEOUT ) ;
109
139
110
140
describe ( 'SecretsProvider usage' , ( ) => {
111
- it ( 'should retrieve a single parameter' , async ( ) => {
141
+
142
+ it ( 'should retrieve a secret as plain string' , async ( ) => {
112
143
113
144
const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
114
145
const testLog = InvocationLogs . parseFunctionLog ( logs [ 0 ] ) ;
@@ -119,7 +150,7 @@ describe(`parameters E2E tests (SecretsProvider) for runtime: ${runtime}`, () =>
119
150
} ) ;
120
151
} , TEST_CASE_TIMEOUT ) ;
121
152
122
- it ( 'should retrieve a single parameter with transform json' , async ( ) => {
153
+ it ( 'should retrieve a secret using transform json option ' , async ( ) => {
123
154
const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
124
155
const testLog = InvocationLogs . parseFunctionLog ( logs [ 1 ] ) ;
125
156
@@ -129,7 +160,7 @@ describe(`parameters E2E tests (SecretsProvider) for runtime: ${runtime}`, () =>
129
160
} ) ;
130
161
} , TEST_CASE_TIMEOUT ) ;
131
162
132
- it ( 'should retrieve single param with transform binary' , async ( ) => {
163
+ it ( 'should retrieve a secret using transform binary option ' , async ( ) => {
133
164
const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
134
165
const testLog = InvocationLogs . parseFunctionLog ( logs [ 2 ] ) ;
135
166
@@ -140,17 +171,18 @@ describe(`parameters E2E tests (SecretsProvider) for runtime: ${runtime}`, () =>
140
171
} , TEST_CASE_TIMEOUT ) ;
141
172
} ) ;
142
173
143
- it ( 'should retrieve single param with transform auto json' , async ( ) => {
174
+ it ( 'should retrieve a secret using transform auto option with implicit json' , async ( ) => {
144
175
const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
145
176
const testLog = InvocationLogs . parseFunctionLog ( logs [ 3 ] ) ;
146
177
178
+ // result should be a json object
147
179
expect ( testLog ) . toStrictEqual ( {
148
180
test : 'get-transform-auto-json' ,
149
181
value : { foo : 'bar' }
150
182
} ) ;
151
183
} , TEST_CASE_TIMEOUT ) ;
152
184
153
- it ( 'should retrieve single param wit transform auto binary' , async ( ) => {
185
+ it ( 'should retrieve a secret using transform auto option with implicit binary' , async ( ) => {
154
186
const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
155
187
const testLog = InvocationLogs . parseFunctionLog ( logs [ 4 ] ) ;
156
188
@@ -160,23 +192,25 @@ describe(`parameters E2E tests (SecretsProvider) for runtime: ${runtime}`, () =>
160
192
} ) ;
161
193
} ) ;
162
194
163
- it ( 'should retrieve single parameter cached' , async ( ) => {
195
+ it ( 'should retrieve a secret twice with cached value ' , async ( ) => {
164
196
const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
165
197
const testLogFirst = InvocationLogs . parseFunctionLog ( logs [ 5 ] ) ;
166
198
199
+ // we fetch twice, but we expect to make an API call only once
167
200
expect ( testLogFirst ) . toStrictEqual ( {
168
201
test : 'get-plain-cached' ,
169
202
value : 1
170
203
} ) ;
171
204
} ) ;
172
205
173
- it ( 'should retrieve single parameter twice without caching ' , async ( ) => {
206
+ it ( 'should retrieve a secret twice with forceFetch second time ' , async ( ) => {
174
207
const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
175
208
const testLogFirst = InvocationLogs . parseFunctionLog ( logs [ 6 ] ) ;
176
209
210
+ // we fetch twice, 2nd time with forceFetch: true flag, we expect two api calls
177
211
expect ( testLogFirst ) . toStrictEqual ( {
178
212
test : 'get-plain-force' ,
179
- value : 1
213
+ value : 2
180
214
} ) ;
181
215
} ) ;
182
216
0 commit comments