/
resource_provider.py
405 lines (353 loc) · 13 KB
/
resource_provider.py
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import json
import logging
import sys
import traceback
import jsonschema
import requests
from cfn_resource_provider import default_injecting_validator
log = logging.getLogger()
def is_int(s):
"""
returns true, if the string is a proper decimal integer value
"""
if len(s) > 0 and s[0] in ('-', '+'):
return s[1:].isdigit()
return s.isdigit()
class ResourceProvider(object):
"""
Custom CloudFormation Resource Provider.
"""
def __init__(self):
"""
constructor
"""
self.request = None
self.response = None
self.context = None
self.asynchronous = False
"""
default json schema for request['ResourceProperties']. Override in your subclass.
"""
self.request_schema = {'type': 'object'}
@property
def custom_cfn_resource_name(self):
return 'Custom::%s' % self.__class__.__name__.replace('Provider', '')
def is_supported_resource_type(self):
return self.resource_type == self.custom_cfn_resource_name
def set_request(self, request, context):
"""
sets the lambda request to process.
"""
self.request = request
self.context = context
self.asynchronous = False
self.response = {
'Status': 'SUCCESS',
'Reason': '',
'StackId': request['StackId'],
'RequestId': request['RequestId'],
'LogicalResourceId': request['LogicalResourceId'],
'Data': {}
}
if 'PhysicalResourceId' in request:
self.response['PhysicalResourceId'] = request['PhysicalResourceId']
def get(self, name, default=None):
"""
returns the custom resource property `name` if it exists, otherwise `default`
"""
return self.properties[name] if name in self.properties else default
def get_old(self, name, default=None):
"""
returns the old resource property `name` if it exists, otherwise `default`
"""
return self.old_properties[name] if name in self.old_properties else default
@property
def properties(self):
"""
returns the custom resource properties from the request.
"""
return self.request['ResourceProperties']
@property
def old_properties(self):
"""
returns the old custom resource properties from the request, if available.
"""
return self.request['OldResourceProperties'] if 'OldResourceProperties' in self.request else {}
@property
def logical_resource_id(self):
"""
returns the LogicaLResourceId from the request.
"""
return self.request['LogicalResourceId']
@property
def stack_id(self):
"""
returns the StackId from the request.
"""
return self.request['StackId']
@property
def request_id(self):
"""
returns the RequestId from the request.
"""
return self.request['RequestId']
@property
def response_url(self):
"""
returns the ResponseURL from the request.
"""
return self.request['ResponseURL']
@property
def physical_resource_id(self):
"""
returns the PhysicalResourceId from the response. Initialized from request.
"""
return self.response['PhysicalResourceId'] if 'PhysicalResourceId' in self.response else None
@physical_resource_id.setter
def physical_resource_id(self, new_resource_id):
self.response['PhysicalResourceId'] = new_resource_id
@property
def request_type(self):
"""
returns the CloudFormation request type.
"""
return self.request['RequestType']
@property
def reason(self):
"""
returns the CloudFormation reason for the status.
"""
return self.response['Reason']
@reason.setter
def reason(self, value):
self.response['Reason'] = value
@property
def status(self):
"""
returns the response status, 'FAILED' or 'SUCCESS'
"""
return self.response['Status']
@status.setter
def set_status(self, value):
assert (value == 'FAILED' or value == 'SUCCESS')
self.response['Status'] = value
@property
def resource_type(self):
"""
returns the CloudFormation resource type on which to perform the request.
"""
return self.request['ResourceType']
@property
def no_echo(self):
"""
returns the current value of NoEcho, or None if not set.
"""
return self.response.get('NoEcho', None)
@no_echo.setter
def no_echo(self, value):
"""
sets the NoEcho in the response to `value`.
"""
assert isinstance(value, bool)
self.response['NoEcho'] = value
def is_valid_cfn_request(self):
"""
returns true when self.request is a valid CloudFormation custom resource request, otherwise false.
if false, sets self.status and self.reason.
"""
try:
jsonschema.validate(self.request, self.cfn_request_schema)
return True
except jsonschema.ValidationError as e:
self.fail('invalid CloudFormation Request received: %s' % str(e.context))
return False
def is_valid_cfn_response(self):
"""
returns true when self.response is a valid CloudFormation custom resource response, otherwise false.
if false, it logs the reason.
"""
try:
jsonschema.validate(
self.response, ResourceProvider.cfn_response_schema)
return True
except jsonschema.ValidationError as e:
log.warning('invalid CloudFormation response created: %s', str(e))
return False
def convert_property_types(self):
"""
allows you to coerce the values in properties to be the type expected. Stupid CFN sends all values as Strings..
it is called before the json schema validation takes place.
one day we will make it a generic method, not now...
"""
pass
def heuristic_convert_property_types(self, properties):
"""
heuristic type conversion of string values in `properties`.
"""
if isinstance(properties, dict):
for name in properties:
properties[name] = self.heuristic_convert_property_types(properties[name])
elif isinstance(properties, list):
for i,v in enumerate(properties):
properties[i] = self.heuristic_convert_property_types(v)
elif isinstance(properties, str):
v = str(properties)
if v == 'true':
return True
elif v == 'false':
return False
elif is_int(v):
return int(v)
else:
pass
return properties
def is_valid_request(self):
"""
returns true if `self.properties` is a valid request as specified by the JSON schema self.request_schema, otherwise False.
Optional properties with a default value in the schema will be added to self.porperties.
If false, self.reason and self.status are set.
"""
try:
self.convert_property_types()
default_injecting_validator.validate(self.properties, self.request_schema)
return True
except jsonschema.ValidationError as e:
message = e.message.replace(str(e.instance), "<instance>") if isinstance(e.instance, dict) else e.message
self.fail('invalid resource properties: %s' % message)
return False
def is_supported_request(self):
"""
returns true if request is `is_supported_resource_type`.
If false, self.reason and self.status are set.
"""
supported = self.is_supported_resource_type()
if not supported:
self.fail('ResourceType %s not supported by provider %s' %
(self.resource_type, self.custom_cfn_resource_name))
return supported
def set_attribute(self, name, value):
"""
sets the attribute `name` to `value`. This value can be retrieved using "Fn::GetAtt".
"""
self.response['Data'][name] = value
def get_attribute(self, name):
"""
returns the value of the attribute `name`.
"""
return self.response['Data'][name] if name in self.response['Data'] else None
def success(self, reason=None):
"""
sets response status to SUCCESS, with an optional reason.
"""
self.response['Status'] = 'SUCCESS'
if reason is not None:
self.response['Reason'] = reason
def fail(self, reason):
"""
sets response status to FAILED
"""
self.response['Status'] = 'FAILED'
self.response['Reason'] = reason
def create(self):
"""
create the custom resource
"""
self.fail('create not implemented by %s' % self)
def update(self):
"""
update the custom resource
"""
self.fail('update not implemented by %s' % self)
def delete(self):
"""
delete the custom resource
"""
self.success('delete not implemented by %s' % self)
def execute(self):
"""
execute the request.
"""
try:
if self.is_supported_request() and self.is_valid_cfn_request() and self.is_valid_request():
if self.request_type == 'Create':
self.create()
elif self.request_type == 'Update':
self.update()
else:
assert self.request_type == 'Delete'
self.delete()
self.is_valid_cfn_response()
elif 'RequestType' in self.request and self.request_type == 'Delete':
# failure to delete an invalid request hangs your cfn...
self.success()
except Exception:
etype, value, tb = sys.exc_info()
s = ''.join(traceback.format_exception_only(etype, value)).rstrip()
if self.status == 'SUCCESS':
self.fail(s)
log.error('%s', traceback.format_exception(etype, value, tb))
finally:
if not self.physical_resource_id and self.status == 'FAILED':
# CFN will complain if the physical_resource_id is not set on
# failure to create the physical resource. :-(
if self.request_type == 'Create':
self.physical_resource_id = 'could-not-create'
def handle(self, request, context):
"""
handles the CloudFormation request.
"""
log.debug('received request %s', json.dumps(request))
self.set_request(request, context)
self.execute()
if not self.asynchronous:
self.send_response()
return self.response
def _truncate_reason(self):
if len(self.reason) > 200:
log.error('truncating Reason to 200 characters to avoid exceeding the, %s', self.reason)
self.reason = '%.200s...' % self.reason
def send_response(self):
"""
sends the response to `ResponseURL`
"""
self._truncate_reason()
url = self.request['ResponseURL']
log.debug('sending response to %s -> %s',
url, json.dumps(self.response))
r = requests.put(url, json=self.response, headers={'content-type': ''})
if r.status_code != 200:
raise Exception('failed to put the response to %s status code %d, %s' %
(url, r.status_code, r.text))
"""
A JSON Schema which defines a proper CloudFormation response message
"""
cfn_response_schema = {
"required": ["Status", "Reason", "RequestId", "StackId",
"LogicalResourceId", "Data"],
"properties": {
"Status": {"type": "string", "enum": ["SUCCESS", "FAILED"]},
"StackId": {"type": "string"},
"RequestId": {"type": "string"},
"LogicalResourceId": {"type": "string"},
"PhysicalResourceId": {"type": "string"},
"Data": {"type": "object"}
}
}
"""
A JSON Schema which defines a proper CloudFormation request message
"""
cfn_request_schema = {
"type": "object",
"required": ["RequestType", "ResponseURL", "StackId", "RequestId", "ResourceType",
"LogicalResourceId", "ResourceProperties"],
"properties": {
"RequestType": {"type": "string", "enum": ["Create", "Update", "Delete"]},
"ResponseURL": {"type": "string", "format": "uri", "pattern": "^https?://"},
"StackId": {"type": "string"},
"RequestId": {"type": "string"},
"ResourceType": {"type": "string"},
"LogicalResourceId": {"type": "string"},
"PhysicalResourceId": {"type": "string"},
"ResourceProperties": {"type": "object"}
}
}