1
+ /**
2
+ * Test SecretsPorovider class
3
+ *
4
+ * @group e2e/parameters/secrets/class
5
+ */
6
+ import {
7
+ createStackWithLambdaFunction ,
8
+ generateUniqueName ,
9
+ invokeFunction ,
10
+ isValidRuntimeKey
11
+ } from '../../../commons/tests/utils/e2eUtils' ;
12
+ import { RESOURCE_NAME_PREFIX , SETUP_TIMEOUT , TEARDOWN_TIMEOUT , TEST_CASE_TIMEOUT } from './constants' ;
13
+ import { v4 } from 'uuid' ;
14
+ import { Tracing } from 'aws-cdk-lib/aws-lambda' ;
15
+ import { deployStack , destroyStack } from '../../../commons/tests/utils/cdk-cli' ;
16
+ import { App , Aspects , SecretValue , Stack } from 'aws-cdk-lib' ;
17
+ import path from 'path' ;
18
+ import { Secret } from 'aws-cdk-lib/aws-secretsmanager' ;
19
+ import { InvocationLogs } from '../../../commons/tests/utils/InvocationLogs' ;
20
+ import { ResourceAccessGranter } from '../helpers/cdkAspectGrantAccess' ;
21
+
22
+ const runtime : string = process . env . RUNTIME || 'nodejs18x' ;
23
+
24
+ if ( ! isValidRuntimeKey ( runtime ) ) {
25
+ throw new Error ( `Invalid runtime key: ${ runtime } ` ) ;
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
+
45
+ describe ( `parameters E2E tests (SecretsProvider) for runtime: ${ runtime } ` , ( ) => {
46
+
47
+ let invocationLogs : InvocationLogs [ ] ;
48
+
49
+ beforeAll ( async ( ) => {
50
+ stack = createStackWithLambdaFunction ( {
51
+ app : integTestApp ,
52
+ stackName : stackName ,
53
+ functionName : functionName ,
54
+ functionEntry : path . join ( __dirname , lambdaFunctionCodeFile ) ,
55
+ tracing : Tracing . ACTIVE ,
56
+ environment : {
57
+ UUID : uuid ,
58
+ SECRET_NAME_PLAIN : secretNamePlain ,
59
+ SECRET_NAME_OBJECT : secretNameObject ,
60
+ SECRET_NAME_BINARY : secretNameBinary ,
61
+ SECRET_NAME_OBJECT_WITH_SUFFIX : secretNameObjectWithSuffix ,
62
+ SECRET_NAME_BINARY_WITH_SUFFIX : secretNameBinaryWithSuffix ,
63
+ SECRET_NAME_PLAIN_CACHED : secretNamePlainCached ,
64
+ } ,
65
+ runtime : runtime
66
+ } ) ;
67
+
68
+ const secretString = new Secret ( stack , 'testSecretPlain' , {
69
+ secretName : secretNamePlain ,
70
+ secretStringValue : SecretValue . unsafePlainText ( 'foo' )
71
+ } ) ;
72
+
73
+ const secretObject = new Secret ( stack , 'testSecretObject' , {
74
+ secretName : secretNameObject ,
75
+ secretObjectValue : {
76
+ foo : SecretValue . unsafePlainText ( 'bar' ) ,
77
+ }
78
+ } ) ;
79
+
80
+ const secretBinary = new Secret ( stack , 'testSecretBinary' , {
81
+ secretName : secretNameBinary ,
82
+ secretStringValue : SecretValue . unsafePlainText ( 'Zm9v' ) // 'foo' encoded in base64
83
+ } ) ;
84
+
85
+ const secretObjectWithSuffix = new Secret ( stack , 'testSecretObjectWithSuffix' , {
86
+ secretName : secretNameObjectWithSuffix ,
87
+ secretObjectValue : {
88
+ foo : SecretValue . unsafePlainText ( 'bar' )
89
+ }
90
+ } ) ;
91
+
92
+ const secretBinaryWithSuffix = new Secret ( stack , 'testSecretBinaryWithSuffix' , {
93
+ secretName : secretNameBinaryWithSuffix ,
94
+ secretStringValue : SecretValue . unsafePlainText ( 'Zm9v' ) // 'foo' encoded in base64
95
+ } ) ;
96
+
97
+ const secretStringCached = new Secret ( stack , 'testSecretStringCached' , {
98
+ secretName : secretNamePlainCached ,
99
+ secretStringValue : SecretValue . unsafePlainText ( 'foo' )
100
+ } ) ;
101
+
102
+ Aspects . of ( stack ) . add ( new ResourceAccessGranter ( [ secretString , secretObject , secretBinary , secretObjectWithSuffix , secretBinaryWithSuffix , secretStringCached ] ) ) ;
103
+
104
+ await deployStack ( integTestApp , stack ) ;
105
+
106
+ invocationLogs = await invokeFunction ( functionName , invocationCount , 'SEQUENTIAL' ) ;
107
+
108
+ } , SETUP_TIMEOUT ) ;
109
+
110
+ describe ( 'SecretsProvider usage' , ( ) => {
111
+ it ( 'should retrieve a single parameter' , async ( ) => {
112
+
113
+ const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
114
+ const testLog = InvocationLogs . parseFunctionLog ( logs [ 0 ] ) ;
115
+
116
+ expect ( testLog ) . toStrictEqual ( {
117
+ test : 'get-plain' ,
118
+ value : 'foo'
119
+ } ) ;
120
+ } , TEST_CASE_TIMEOUT ) ;
121
+
122
+ it ( 'should retrieve a single parameter with transform json' , async ( ) => {
123
+ const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
124
+ const testLog = InvocationLogs . parseFunctionLog ( logs [ 1 ] ) ;
125
+
126
+ expect ( testLog ) . toStrictEqual ( {
127
+ test : 'get-transform-json' ,
128
+ value : { foo : 'bar' }
129
+ } ) ;
130
+ } , TEST_CASE_TIMEOUT ) ;
131
+
132
+ it ( 'should retrieve single param with transform binary' , async ( ) => {
133
+ const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
134
+ const testLog = InvocationLogs . parseFunctionLog ( logs [ 2 ] ) ;
135
+
136
+ expect ( testLog ) . toStrictEqual ( {
137
+ test : 'get-transform-binary' ,
138
+ value : 'foo'
139
+ } ) ;
140
+ } , TEST_CASE_TIMEOUT ) ;
141
+ } ) ;
142
+
143
+ it ( 'should retrieve single param with transform auto json' , async ( ) => {
144
+ const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
145
+ const testLog = InvocationLogs . parseFunctionLog ( logs [ 3 ] ) ;
146
+
147
+ expect ( testLog ) . toStrictEqual ( {
148
+ test : 'get-transform-auto-json' ,
149
+ value : { foo : 'bar' }
150
+ } ) ;
151
+ } , TEST_CASE_TIMEOUT ) ;
152
+
153
+ it ( 'should retrieve single param wit transform auto binary' , async ( ) => {
154
+ const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
155
+ const testLog = InvocationLogs . parseFunctionLog ( logs [ 4 ] ) ;
156
+
157
+ expect ( testLog ) . toStrictEqual ( {
158
+ test : 'get-transform-auto-binary' ,
159
+ value : 'foo'
160
+ } ) ;
161
+ } ) ;
162
+
163
+ it ( 'should retrieve single parameter cached' , async ( ) => {
164
+ const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
165
+ const testLogFirst = InvocationLogs . parseFunctionLog ( logs [ 5 ] ) ;
166
+
167
+ expect ( testLogFirst ) . toStrictEqual ( {
168
+ test : 'get-plain-cached' ,
169
+ value : 1
170
+ } ) ;
171
+ } ) ;
172
+
173
+ it ( 'should retrieve single parameter twice without caching' , async ( ) => {
174
+ const logs = invocationLogs [ 0 ] . getFunctionLogs ( ) ;
175
+ const testLogFirst = InvocationLogs . parseFunctionLog ( logs [ 6 ] ) ;
176
+
177
+ expect ( testLogFirst ) . toStrictEqual ( {
178
+ test : 'get-plain-force' ,
179
+ value : 1
180
+ } ) ;
181
+ } ) ;
182
+
183
+ afterAll ( async ( ) => {
184
+ if ( ! process . env . DISABLE_TEARDOWN ) {
185
+ await destroyStack ( integTestApp , stack ) ;
186
+ }
187
+ } , TEARDOWN_TIMEOUT ) ;
188
+ } ) ;
0 commit comments