This repository was archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcft_sidecar_macros.yaml
373 lines (301 loc) · 13.7 KB
/
cft_sidecar_macros.yaml
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
AWSTemplateFormatVersion: 2010-09-09
Parameters:
MacrosTemplateVersionDash:
Description:
"Version of the Cyral Sidecar Macros CloudFormation template, with dashes
in the place of dots for the version, due to stack name constraints."
Type: String
PermissionsBoundary:
Type: String
Description: ARN of the permissions boundary to apply to all the IAM roles. Set to an empty string if no permission boundaries should be used.
Default: ""
Conditions:
usePermissionsBoundary: !Not [!Equals [!Ref PermissionsBoundary, '']]
Resources:
ListMapMacro:
Type: AWS::CloudFormation::Macro
Properties:
Name: !Sub 'CyralListMap-${MacrosTemplateVersionDash}'
Description: Maps all elements of a given list to a specified format
FunctionName: !GetAtt ListMapLambda.Arn
ListMapLambda:
Type: AWS::Lambda::Function
Properties:
Description: Maps all elements of a given list to a specified format
Handler: index.handler
Runtime: "python3.9"
Timeout: 30
Role: !GetAtt LambdaRole.Arn
Code:
ZipFile: |
import copy
import json
# Will be populated by the handler. Have it as a global variable
# because it will be used in a recursive function.
load_balancer_sticky_ports = []
def printInvalidFormatError():
print('[ERROR] ListMap value must be of format "!Ref <ListParameterName>" or "[list, of, values]"')
def mapResource(resource, val):
if isinstance(resource, str):
##################################################################
# Made it possible to use either %l or %ll to trigger replace;
# This is helpful if you need to differentiate between 2 resources
# that have the same name without the replacement, e.g:
# 'MyResource%l' and 'MyResource%ll', otherwise the last one will
# be lost when the template is turned to a python dictionary.
if '%l' in resource:
return resource.replace('%ll', '%l').replace('%l', str(val))
elif '%stickinessEnabled' in resource:
return resource.replace('%stickinessEnabled',
str(val in load_balancer_sticky_ports).lower())
if isinstance(resource, dict):
new_resource = copy.copy(resource)
for key in resource:
if '%l' in key:
content = new_resource.pop(key)
new_resource[mapResource(key, val)] = mapResource(content, val)
else:
content = new_resource[key]
new_resource[key] = mapResource(content, val)
return new_resource
if isinstance(resource, list):
return list(map(lambda elem: mapResource(elem, val), resource))
return resource
def processTemplate(template, parameters):
print(f'[INFO] template object received: {json.dumps(template, default=str)}')
print(f'[INFO] parameters object received: {json.dumps(parameters, default=str)}')
new_template = copy.deepcopy(template)
for name, resource in template['Resources'].items():
if 'ListMap' in resource:
map_list = new_template['Resources'][name].pop('ListMap')
# List may be raw or reference:
if isinstance(map_list, dict):
try:
list_ref = map_list['Ref']
except AttributeError:
printInvalidFormatError()
return 'failed', template
try:
map_list = parameters[list_ref]
except AttributeError:
print(f'[ERROR] no Template Parameter named {list_ref} was found')
return 'failed', template
elif not isinstance(map_list, list):
printInvalidFormatError()
return 'failed', template
resource_to_map = new_template['Resources'].pop(name)
print(f'[Mapping] Mapping resource: {json.dumps(resource_to_map, default=str)}')
print(f'[Mapping] Using list: {json.dumps(map_list, default=str)}')
for val in map_list:
if val == '' or val == None:
continue
resource = copy.deepcopy(resource_to_map)
new_resource = mapResource(resource, val)
new_name = mapResource(name, val)
new_template['Resources'][new_name] = new_resource
return 'success', new_template
def handler(event, context):
template = event['fragment']
parameters = event['templateParameterValues']
# Change this at global scope, since it is used by a recursive
# function.
global load_balancer_sticky_ports
load_balancer_sticky_ports = parameters['LoadBalancerStickyPorts']
status, new_template = processTemplate(template, parameters)
print(f'[INFO] processed template: {json.dumps(new_template, default=str)}')
resp = {
'requestId': event['requestId'],
'status': status,
'fragment': new_template
}
return resp
ExpandMacro:
Type: AWS::CloudFormation::Macro
Properties:
Name: !Sub 'CyralExpand-${MacrosTemplateVersionDash}'
Description: Similar to list map, but expands list values within the same resource instead of creating multiple resources
FunctionName: !GetAtt ExpandLambda.Arn
ExpandLambda:
Type: AWS::Lambda::Function
Properties:
Description: Similar to list map, but expands list values within the same resource instead of creating multiple resources
Handler: index.handler
Runtime: "python3.9"
Timeout: 30
Role: !GetAtt LambdaRole.Arn
Code:
ZipFile: |
import json
import copy
def printInvalidArg0FormatError():
print('[ERROR] Expand arg[0] must be of format "!Ref <ListParameterName>" or "[list, of, values]"')
def expandResource(resource, expanded_list):
if isinstance(resource, dict):
for key in resource:
content = resource[key]
resource[key] = expandResource(content, expanded_list)
return resource
if isinstance(resource, list):
new_resource = copy.deepcopy(resource)
if '%e' in resource:
new_resource.remove('%e')
new_resource += expanded_list
return new_resource
return resource
def replaceFormat(format, val):
if isinstance(format, str):
return format.replace('%v', str(val))
if isinstance(format, dict):
new_format = copy.deepcopy(format)
for key in format:
if '%v' in key:
content = new_format.pop(key)
new_key = replaceFormat(key, val)
new_format[new_key] = replaceFormat(content, val)
else:
new_format[key] = replaceFormat(new_format[key], val)
return new_format
def processTemplate(template, parameters):
print(f'[INFO] template object received: {json.dumps(template, default=str)}')
print(f'[INFO] parameters object received: {json.dumps(parameters, default=str)}')
new_template = copy.deepcopy(template)
for name, resource in template['Resources'].items():
if 'Expand' in resource:
expand_args = new_template['Resources'][name].pop('Expand')
expand_list = expand_args[0]
expand_format = expand_args[1]
# List may be raw or reference:
if isinstance(expand_list, dict):
try:
list_ref = expand_list['Ref']
except AttributeError:
printInvalidArg0FormatError()
return 'failed', template
try:
expand_list = parameters[list_ref]
except AttributeError:
print(f'[ERROR] no Template Parameter named {list_ref} was found')
return 'failed', template
elif not isinstance(expand_list, list):
printInvalidArg0FormatError()
return 'failed', template
expanded_list = list(map(lambda item: replaceFormat(expand_format, item), expand_list))
resource_to_expand = new_template['Resources'].pop(name)
print(f'[Expanding] Expanding resource: {json.dumps(resource_to_expand, default=str)}')
print(f'[Expanding] Using list: {json.dumps(expand_list, default=str)}')
new_resource = expandResource(resource_to_expand, expanded_list)
new_template['Resources'][name] = new_resource
return 'success', new_template
def handler(event, context):
template = event['fragment']
parameters = event['templateParameterValues']
status, new_template = processTemplate(template, parameters)
print(f'[INFO] processed template: {json.dumps(new_template, default=str)}')
resp = {
'requestId': event['requestId'],
'status': status,
'fragment': new_template
}
return resp
ListDiffMacro:
Type: AWS::CloudFormation::Macro
Properties:
Name: !Sub 'CyralListDiff-${MacrosTemplateVersionDash}'
Description: Becomes the elements of the first list minus the elements of the second list
FunctionName: !GetAtt ListDiffLambda.Arn
ListDiffLambda:
Type: AWS::Lambda::Function
Properties:
Description: Becomes the elements of the first list minus the elements of the second list
Handler: index.handler
Runtime: "python3.9"
Timeout: 30
Role: !GetAtt LambdaRole.Arn
Code:
ZipFile: |
import json
import copy
def printInvalidArgsFormatError():
print('[ERROR] DiffList arguments must be of format "!Ref <ListParameterName>" or "[list, of, values]"')
def dereferenceList(expand_list, parameters):
if isinstance(expand_list, dict):
try:
list_ref = expand_list['Ref']
except AttributeError:
printInvalidArgsFormatError()
return 'failed'
try:
expand_list = parameters[list_ref]
except AttributeError:
print(f'[ERROR] no Template Parameter named {list_ref} was found')
return 'failed'
return expand_list
# expand_list must be a list or Ref to a list;
elif not isinstance(expand_list, list):
printInvalidArgsFormatError()
return 'failed'
return expand_list
def diffResource(resource, diffed_list):
if isinstance(resource, str):
if resource == '%d':
return diffed_list
if isinstance(resource, dict):
for key in resource:
resource[key] = diffResource(resource[key], diffed_list)
return resource
def processTemplate(template, parameters):
print(f'[INFO] template object received: {json.dumps(template, default=str)}')
print(f'[INFO] parameters object received: {json.dumps(parameters, default=str)}')
new_template = copy.deepcopy(template)
for name, resource in template['Resources'].items():
if 'ListDiff' in resource:
diff_args = new_template['Resources'][name].pop('ListDiff')
diff_listA = dereferenceList(diff_args[0], parameters)
diff_listB = dereferenceList(diff_args[1], parameters)
resource_to_replace = new_template['Resources'].pop(name)
print(f'[Replacing] Replacing resource: {json.dumps(resource_to_replace, default=str)}')
diffed_list = list(filter(lambda item: item not in diff_listB, diff_listA))
new_resource = diffResource(resource_to_replace, diffed_list)
new_template['Resources'][name] = new_resource
return 'success', new_template
def handler(event, context):
template = event['fragment']
parameters = event['templateParameterValues']
status, new_template = processTemplate(template, parameters)
print(f'[INFO] processed template: {json.dumps(new_template, default=str)}')
resp = {
'requestId': event['requestId'],
'status': status,
'fragment': new_template
}
return resp
LambdaRole:
Type: AWS::IAM::Role
Properties:
PermissionsBoundary:
Fn::If:
- usePermissionsBoundary
- !Ref PermissionsBoundary
- !Ref AWS::NoValue
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: root
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:*'