-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathTestCase.ts
215 lines (187 loc) · 5.67 KB
/
TestCase.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
208
209
210
211
212
213
214
215
import type { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import type { Table } from 'aws-cdk-lib/aws-dynamodb';
import type { IStringParameter, StringParameter } from 'aws-cdk-lib/aws-ssm';
import { PolicyStatement, Effect } from 'aws-cdk-lib/aws-iam';
class NodeJsFunction {
public functionName: string;
public ref: NodejsFunction;
public constructor(name: string, ref: NodejsFunction) {
this.functionName = name;
this.ref = ref;
}
}
class DynamoDBTable {
public ref: Table;
public tableName: string;
readonly #envVariableName?: string;
public constructor(name: string, ref: Table, envVariableName?: string) {
this.tableName = name;
this.ref = ref;
this.#envVariableName = envVariableName;
}
public get envVariableName(): string {
return this.#envVariableName ?? 'TABLE_NAME';
}
}
abstract class SSMResource {
public parameterName: string;
public ref: StringParameter | IStringParameter;
protected readonly envVarName?: string;
public constructor(
name: string,
ref: StringParameter | IStringParameter,
envVariableName?: string
) {
this.parameterName = name;
this.ref = ref;
this.envVarName = envVariableName;
}
}
class SsmSecureString extends SSMResource {
public constructor(
name: string,
ref: IStringParameter,
envVariableName?: string
) {
super(name, ref, envVariableName);
}
public get envVariableName(): string {
return this.envVarName ?? 'SECURE_STRING_NAME';
}
}
class SsmString extends SSMResource {
public constructor(
name: string,
ref: StringParameter,
envVariableName?: string
) {
super(name, ref, envVariableName);
}
public get envVariableName(): string {
return this.envVarName ?? 'SSM_STRING_NAME';
}
}
/**
* A test case that can be added to a test stack.
*/
class TestCase {
public testName: string;
#dynamodb?: DynamoDBTable;
#function?: NodeJsFunction;
#ssmSecureString?: SsmSecureString;
#ssmString?: SsmString;
public constructor(testName: string) {
this.testName = testName;
}
/**
* The NodejsFunction that is associated with this test case.
*/
public set function(fn: NodeJsFunction) {
if (this.#dynamodb) {
this.#grantAccessToDynamoDBTableAndSetEnv(fn, this.#dynamodb);
}
if (this.#ssmSecureString) {
this.#grantAccessToSsmeStringAndSetEnv(fn, this.#ssmSecureString);
}
if (this.#ssmString) {
this.#grantAccessToSsmeStringAndSetEnv(fn, this.#ssmString);
}
this.#function = fn;
}
/**
* Get the NodejsFunction that is associated with this test case.
*/
public get function(): NodeJsFunction {
if (!this.#function) throw new Error('This test case has no function.');
return this.#function;
}
/**
* The DynamoDB table that is associated with this test case.
*/
public set dynamodb(table: DynamoDBTable) {
if (this.#function) {
this.#grantAccessToDynamoDBTableAndSetEnv(this.#function, table);
}
this.#dynamodb = table;
}
/**
* Get the DynamoDB table that is associated with this test case.
*/
public get dynamodb(): DynamoDBTable {
if (!this.#dynamodb)
throw new Error('This test case has no DynamoDB table.');
return this.#dynamodb;
}
/**
* Grant access to the DynamoDB table and set the environment variable to
* the table name.
*
* @param fn - The function to grant access to the table and set the environment variable.
* @param table - The table to grant access to and identified by the environment variable.
*/
#grantAccessToDynamoDBTableAndSetEnv = (
fn: NodeJsFunction,
table: DynamoDBTable
): void => {
table.ref.grantReadWriteData(fn.ref);
fn.ref.addEnvironment(table.envVariableName, table.ref.tableName);
};
/**
* The SSM SecureString that is associated with this test case.
*/
public set ssmSecureString(parameter: SsmSecureString) {
if (this.#function) {
this.#grantAccessToSsmeStringAndSetEnv(this.#function, parameter);
}
this.#ssmSecureString = parameter;
}
/**
* Get the SSM SecureString that is associated with this test case.
*/
public get ssmSecureString(): SsmSecureString {
if (!this.#ssmSecureString)
throw new Error('This test case has no SSM SecureString.');
return this.#ssmSecureString;
}
/**
* The SSM String that is associated with this test case.
*/
public set ssmString(parameter: SsmString) {
if (this.#function) {
this.#grantAccessToSsmeStringAndSetEnv(this.#function, parameter);
}
this.#ssmString = parameter;
}
/**
* Get the SSM String that is associated with this test case.
*/
public get ssmString(): SsmString {
if (!this.#ssmString) throw new Error('This test case has no SSM String.');
return this.#ssmString;
}
/**
* Grant access to the SSM String and set the environment variable to
* the parameter name.
*
* @param fn - The function to grant access to the parameter and set the environment variable.
* @param parameter - The parameter to grant access to and identified by the environment variable.
*/
#grantAccessToSsmeStringAndSetEnv = (
fn: NodeJsFunction,
parameter: SsmSecureString | SsmString
): void => {
fn.ref.addEnvironment(parameter.envVariableName, parameter.parameterName);
// Grant access also to the path of the parameter
fn.ref.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['ssm:GetParametersByPath'],
resources: [
parameter.ref.parameterArn.split(':').slice(0, -1).join(':'),
],
})
);
parameter.ref.grantRead(fn.ref);
};
}
export { TestCase, NodeJsFunction, DynamoDBTable, SsmSecureString, SsmString };