-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoperation_obj.py
414 lines (331 loc) · 14 KB
/
operation_obj.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
406
407
408
409
410
411
412
413
414
from xeger import Xeger
import api_signer
import protocol_formatter
import aws_api_shapeshifter
""" The operation_obj will be the primary location where parameters are altered and configured.
Every user generated modification will pass throug here. As a result this code is ugly and I'm
only partially sorry. """
class Operation:
def __init__(self, metadata, endpoints, shapes, operation):
self.name = operation['name']
self.method = operation['http']['method']
self.request_uri = operation['http']['requestUri']
self.operation = operation
self.endpoints = endpoints['endpoints']
self.metadata = metadata
self.shapes = shapes
self.endpoint_prefix = metadata['endpointPrefix']
self.target_prefix = self._resolve_target_prefix(self.metadata)
self.shape_input = self._resolve_shape_input(operation)
# make_request will take the requested modifications (if any) and make the request to the AWS API
def make_request(self, **kwargs):
name = self.name
self.input_format = self._parse_input_shape(self.metadata['endpointPrefix'], self.shapes, self.operation, kwargs)
version = self.metadata['apiVersion']
credentials = _resolve_credentials(kwargs)
endpoint_prefix = self._resolve_endpoint_prefix(kwargs)
method = self._resolve_method(kwargs)
region = self._resolve_region(kwargs)
host = self._resolve_host(region, endpoint_prefix, kwargs)
endpoint = self._resolve_endpoint(host, kwargs)
request_uri = self._resolve_request_uri(kwargs)
# TODO: Clean this up
if 'noparam' in kwargs.keys() or 'noparams' in kwargs.keys():
# TODO: Should be {}
self.input_format = {}
if 'protocol' in kwargs.keys():
protocol = kwargs['protocol']
else:
protocol = self.metadata['protocol']
# Depending on the protocol we need to format inputs differently
if protocol == "query":
formatted_request = protocol_formatter.query_protocol_formatter(
host,
credentials.token,
name,
version,
kwargs,
self.input_format
)
response = api_signer.query_signer(
credentials,
method,
endpoint_prefix,
host,
region,
endpoint,
request_uri,
formatted_request
)
return response
if protocol == "json":
json_version = self._resolve_json_version(self.metadata)
amz_target = self._resolve_target_prefix(self.metadata)
amz_target += "." + self.name
signing_name = self._resolve_signing_name(self.metadata, kwargs)
formatted_request = protocol_formatter.json_protocol_formatter(
host,
credentials.token,
json_version,
amz_target,
kwargs,
self.input_format
)
response = api_signer.json_signer(
credentials,
method,
endpoint_prefix,
host,
region,
endpoint,
signing_name,
request_uri,
formatted_request
)
return response
if protocol == "rest-json":
json_version = self._resolve_json_version(self.metadata)
signing_name = self._resolve_signing_name(self.metadata, kwargs)
formatted_request = protocol_formatter.rest_json_protocol_formatter(
host,
credentials.token,
json_version,
request_uri,
kwargs,
self.input_format
)
response = api_signer.rest_json_signer(
credentials,
method,
endpoint_prefix,
host,
region,
endpoint,
signing_name,
formatted_request['request_uri'],
formatted_request
)
return response
return None
def _has_safe_regions(self, regions):
# return true if it has safe regions
# false if not
for region in regions:
if region in SAFE_REGIONS:
return True
return False
def _get_safe_region(self, regions):
for region in regions:
if region in SAFE_REGIONS:
return region
def _test_hostname(self, hostname):
try:
socket.gethostbyname(hostname)
return True
except socket.error:
return False
def _resolve_endpoint_prefix(self, kwargs):
if 'endpoint_prefix' in kwargs.keys():
return kwargs['endpoint_prefix']
return self.endpoint_prefix
def _resolve_method(self, kwargs):
if 'method' in kwargs.keys():
return kwargs['method']
return self.method
def _resolve_signing_name(self, metadata, kwargs):
if 'signing_name' in kwargs.keys():
return kwargs['signing_name']
# if we have a signing name
if 'signingName' in metadata.keys():
return metadata['signingName']
# Give up and go with endpointPrefix
return metadata['endpointPrefix']
def _resolve_region(self, kwargs):
if 'region' in kwargs.keys():
return kwargs['region']
# ngl, gonna prefer us-east-1
if 'us-east-1' in self.endpoints.keys():
return 'us-east-1'
# Otherwise, pick a random region that they support
# Need to check for a credential scope region
potential = list(self.endpoints.keys())[0]
if "credentialScope" in self.endpoints[potential].keys():
return self.endpoints[potential]['credentialScope']['region']
return potential
def _resolve_host(self, region, endpoint_prefix, kwargs):
if 'host' in kwargs.keys():
return kwargs['host']
# iam is an example of this - Need to check for a hostname for a region
# not in the keys (aws-global)
potential = list(self.endpoints.keys())[0]
if 'credentialScope' in self.endpoints[potential].keys() and region == self.endpoints[potential]['credentialScope']['region']:
return self.endpoints[potential]['hostname']
if 'hostname' in self.endpoints[region].keys():
return self.endpoints[region]['hostname']
# TODO: Check ,but I don't think we ever get here.
# I know this is broken but will wait to fix until I have
# more examples
return endpoint_prefix + "." + region + ".amazonaws.com"
def _resolve_endpoint(self, host, kwargs):
if 'endpoint' in kwargs.keys():
return kwargs['endpoint']
return "https://" + host
def _resolve_request_uri(self, kwargs):
if 'request_uri' in kwargs.keys():
return kwargs['request_uri']
return self.request_uri
def _resolve_target_prefix(self, metadata):
if 'targetPrefix' in metadata.keys():
return metadata['targetPrefix']
else:
return metadata['endpointPrefix']
def _resolve_json_version(self, metadata):
if 'jsonVersion' in metadata.keys():
return metadata['jsonVersion']
# Otherwise you likely know what you want to do
# In fact it's likely what you're testing
# I'll give you a 1.0 so you don't complain
return "1.0"
def _resolve_shape_input(self, operation):
if 'input' in operation.keys():
return operation['input']['shape']
return ""
# GIVE THIS THE SHAPE, NOT THE NAME
def _resolve_unknown_shape(self, shapes, unknown_shape):
unknown_shape_type = unknown_shape['type']
if unknown_shape_type == 'string':
return self._gen_string_shape(unknown_shape)
if unknown_shape_type == 'integer':
return 1
if unknown_shape_type == 'boolean':
return "false"
if unknown_shape_type == 'structure':
return self._resolve_structure(shapes, unknown_shape)
if unknown_shape_type == 'list':
return self._resolve_list(shapes, unknown_shape)
if unknown_shape_type == 'timestamp':
return self._resolve_timestamp(shapes, unknown_shape)
if unknown_shape_type == 'blob':
return self._resolve_blob(shapes, unknown_shape)
if unknown_shape_type == 'long':
return 1
if unknown_shape_type == 'map':
return "map"
if unknown_shape_type == 'double' or unknown_shape_type == 'float':
return 2.0
# Map not implemented -Xray
print(unknown_shape_type)
def _resolve_structure(self, shapes, structure):
to_return = {}
for member in structure['members']:
if 'required' in structure.keys() and member in structure['required']:
shape_name = structure['members'][member]['shape']
to_return[member] = self._resolve_unknown_shape(shapes, shapes[shape_name])
return to_return
def _resolve_list(self, shapes, list_shape):
# This is an interesting problem. We should return this in a list
# The reason being that NORMAL operations may have multiple items.
# In our current form, we only give one.
member_shape = list_shape['member']['shape']
#if 'locationName' in list_shape['member'].keys():
# location_name = list_shape['member']['locationName']
#else:
# # Learned from elasticache RemoveTagsFromResource
# location_name = 'member'
result = self._resolve_unknown_shape(shapes, shapes[member_shape])
return [result]
def _resolve_timestamp(self, shapes, timestamp):
return "1615593755.796672"
def _resolve_blob(self, shapes, blob):
return "bbbbbbbbebfbebebbebebb"
def _parse_input_shape(self, name, shapes, operation, kwargs):
to_return = {}
# We may have params in our kwargs and we need to process them
# We are going to support a "Append" format
# Not sure what that means right now. Going to work on it
# Not every operation has an input
if 'input' in operation.keys():
input_shape_name = operation['input']['shape']
shape = shapes[input_shape_name]
if "required" in shape.keys():
# This is actual torture
# TODO: Refactor
for required in shape['required']:
shape_name = shape['members'][required]['shape']
result = self._resolve_unknown_shape(shapes, shapes[shape_name])
to_return[required] = result
# Parsing params for kwargs
if 'params' in kwargs.keys():
passed_params = kwargs['params']
for key in passed_params.keys():
to_return[key] = passed_params[key]
return to_return
# Leaving this code here for now. Going to need to
# improve this. Query protocol is an edge case
# because it uses a weird naming format for
# lists. Maybe move this into the query protocol
# formatter?
i = 2
for item in values:
if "." in item[0]:
if "Key" in item[0]:
new_name = item[0] + "." + str(i//2)
new_name = new_name.replace(".Key.",".") + ".Key"
elif "Value" in item[0]:
new_name = item[0] + "." + str(i//2)
new_name = item[0].replace(".Value.", ".") + ".Value"
else:
new_name = item[0] + "." + str(i//2)
i += 1
to_return[new_name] = item[1]
else:
to_return[item[0]] = item[1]
return to_return
return {}
def _flatten_list(self, list_in):
if isinstance(list_in, list):
for l in list_in:
for y in self._flatten_list(l):
yield y
else:
yield list_in
def _gen_string_shape(self, member_shape):
# min, max, pattern, enum
if "pattern" in member_shape.keys():
return self._gen_regex_pattern(member_shape['pattern'])
if "enum" in member_shape.keys():
return member_shape['enum'][0]
if "min" in member_shape.keys():
return 'a'*member_shape['min']
return 'aareturngen'
def _gen_regex_pattern(self, pattern):
# Some patterns break
x = Xeger()
try:
result = x.xeger(pattern)
return result
except:
return "aregex"
def _resolve_credentials(kwargs):
""" A user can send a few types of creds at us, and we
have to be able to resolve them. Output should
always be a credential object with .access_key,
.secret_key, and .token accessible """
kwargs_keys = kwargs.keys()
# Assume that access key modification is intentional and
# is a higher priority
if 'access_key' in kwargs_keys:
return aws_api_shapeshifter.Credentials(
kwargs['access_key'],
kwargs['secret_key'],
kwargs['token']
)
elif 'creds' in kwargs_keys:
return kwargs['creds']
elif 'credentials' in kwargs_keys:
return kwargs['credentials']
return aws_api_shapeshifter.Credentials("", "", "")
def _resolve_region_hostname(endpoints, preferred=''):
# If there is a hostname return in
# otherwise just return the region
None