Repository navigation
Expand file tree
/
Copy pathaws_stack.py
More file actions
550 lines (450 loc) · 20.6 KB
/
Copy pathaws_stack.py
File metadata and controls
550 lines (450 loc) · 20.6 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import os
import re
import json
import boto3
import base64
import logging
from six import iteritems
from localstack import config
from localstack.constants import (REGION_LOCAL, DEFAULT_REGION,
ENV_DEV, APPLICATION_AMZ_JSON_1_1, APPLICATION_AMZ_JSON_1_0)
from localstack.utils.common import run_safe, to_str, is_string, make_http_request, timestamp
from localstack.utils.aws.aws_models import KinesisStream
# AWS environment variable names
ENV_ACCESS_KEY = 'AWS_ACCESS_KEY_ID'
ENV_SECRET_KEY = 'AWS_SECRET_ACCESS_KEY'
ENV_SESSION_TOKEN = 'AWS_SESSION_TOKEN'
# set up logger
LOGGER = logging.getLogger(__name__)
# Use this field if you want to provide a custom boto3 session.
# This field takes priority over CREATE_NEW_SESSION_PER_BOTO3_CONNECTION
CUSTOM_BOTO3_SESSION = None
# Use this flag to enable creation of a new session for each boto3 connection.
# This flag will be ignored if CUSTOM_BOTO3_SESSION is specified
CREATE_NEW_SESSION_PER_BOTO3_CONNECTION = False
# Used in AWS assume role function
INITIAL_BOTO3_SESSION = None
# Assume role loop seconds
DEFAULT_TIMER_LOOP_SECONDS = 60 * 50
class Environment(object):
def __init__(self, region=None, prefix=None):
# target is the runtime environment to use, e.g.,
# 'local' for local mode
self.region = region or get_local_region()
# prefix can be 'prod', 'stg', 'uat-1', etc.
self.prefix = prefix
def apply_json(self, j):
if isinstance(j, str):
j = json.loads(j)
self.__dict__.update(j)
@staticmethod
def from_string(s):
parts = s.split(':')
if len(parts) == 1:
if s in PREDEFINED_ENVIRONMENTS:
return PREDEFINED_ENVIRONMENTS[s]
parts = [get_local_region(), s]
if len(parts) > 2:
raise Exception('Invalid environment string "%s"' % s)
region = parts[0]
prefix = parts[1]
return Environment(region=region, prefix=prefix)
@staticmethod
def from_json(j):
if not isinstance(j, dict):
j = j.to_dict()
result = Environment()
result.apply_json(j)
return result
def __str__(self):
return '%s:%s' % (self.region, self.prefix)
PREDEFINED_ENVIRONMENTS = {
ENV_DEV: Environment(region=REGION_LOCAL, prefix=ENV_DEV)
}
def get_environment(env=None, region_name=None):
"""
Return an Environment object based on the input arguments.
Parameter `env` can be either of:
* None (or empty), in which case the rules below are applied to (env = os.environ['ENV'] or ENV_DEV)
* an Environment object (then this object is returned)
* a string '<region>:<name>', which corresponds to Environment(region='<region>', prefix='<prefix>')
* the predefined string 'dev' (ENV_DEV), which implies Environment(region='local', prefix='dev')
* a string '<name>', which implies Environment(region=DEFAULT_REGION, prefix='<name>')
Additionally, parameter `region_name` can be used to override DEFAULT_REGION.
"""
if not env:
if 'ENV' in os.environ:
env = os.environ['ENV']
else:
env = ENV_DEV
elif not is_string(env) and not isinstance(env, Environment):
raise Exception('Invalid environment: %s' % env)
if is_string(env):
env = Environment.from_string(env)
if region_name:
env.region = region_name
if not env.region:
raise Exception('Invalid region in environment: "%s"' % env)
return env
def connect_to_resource(service_name, env=None, region_name=None, endpoint_url=None):
"""
Generic method to obtain an AWS service resource using boto3, based on environment, region, or custom endpoint_url.
"""
return connect_to_service(service_name, client=False, env=env, region_name=region_name, endpoint_url=endpoint_url)
def get_boto3_credentials():
if CUSTOM_BOTO3_SESSION:
return CUSTOM_BOTO3_SESSION.get_credentials()
return boto3.session.Session().get_credentials()
def get_boto3_session():
if CUSTOM_BOTO3_SESSION:
return CUSTOM_BOTO3_SESSION
if CREATE_NEW_SESSION_PER_BOTO3_CONNECTION:
return boto3.session.Session()
# return default session
return boto3
def get_local_region():
session = boto3.session.Session()
return session.region_name or DEFAULT_REGION
def get_local_service_url(service_name):
if service_name == 's3api':
service_name = 's3'
return os.environ['TEST_%s_URL' % (service_name.upper().replace('-', '_'))]
def connect_to_service(service_name, client=True, env=None, region_name=None, endpoint_url=None, config=None):
"""
Generic method to obtain an AWS service client using boto3, based on environment, region, or custom endpoint_url.
"""
env = get_environment(env, region_name=region_name)
my_session = get_boto3_session()
method = my_session.client if client else my_session.resource
verify = True
if not endpoint_url:
if env.region == REGION_LOCAL:
endpoint_url = get_local_service_url(service_name)
verify = False
region = env.region if env.region != REGION_LOCAL else get_local_region()
return method(service_name, region_name=region, endpoint_url=endpoint_url, verify=verify, config=config)
class VelocityInput:
"""Simple class to mimick the behavior of variable '$input' in AWS API Gateway integration velocity templates.
See: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html"""
def __init__(self, value):
self.value = value
def path(self, path):
from jsonpath_rw import parse
value = self.value if isinstance(self.value, dict) else json.loads(self.value)
jsonpath_expr = parse(path)
result = [match.value for match in jsonpath_expr.find(value)]
result = result[0] if len(result) == 1 else result
return result
def json(self, path):
return json.dumps(self.path(path))
class VelocityUtil:
"""Simple class to mimick the behavior of variable '$util' in AWS API Gateway integration velocity templates.
See: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html"""
def base64Encode(self, s):
if not isinstance(s, str):
s = json.dumps(s)
encoded_str = s.encode(config.DEFAULT_ENCODING)
encoded_b64_str = base64.b64encode(encoded_str)
return encoded_b64_str.decode(config.DEFAULT_ENCODING)
def base64Decode(self, s):
if not isinstance(s, str):
s = json.dumps(s)
return base64.b64decode(s)
def render_velocity_template(template, context, as_json=False):
import airspeed
t = airspeed.Template(template)
variables = {
'input': VelocityInput(context),
'util': VelocityUtil()
}
replaced = t.merge(variables)
if as_json:
replaced = json.loads(replaced)
return replaced
def check_valid_region(headers):
""" Check whether a valid region is provided, and if not then raise an Exception. """
auth_header = headers.get('Authorization')
if not auth_header:
raise Exception('Unable to find "Authorization" header in request')
replaced = re.sub(r'.*Credential=([^,]+),.*', r'\1', auth_header)
if auth_header == replaced:
raise Exception('Unable to find "Credential" section in "Authorization" header')
# Format is: <your-access-key-id>/<date>/<aws-region>/<aws-service>/aws4_request
# See https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html
parts = replaced.split('/')
region = parts[2]
if region not in config.VALID_REGIONS:
raise Exception('Invalid region specified in "Authorization" header: "%s"' % region)
def get_s3_client():
return boto3.resource('s3',
endpoint_url=config.TEST_S3_URL,
config=boto3.session.Config(
s3={'addressing_style': 'path'}),
verify=False)
def get_account_id(account_id=None, env=None):
if account_id:
return account_id
env = get_environment(env)
if env.region == REGION_LOCAL:
return os.environ['TEST_AWS_ACCOUNT_ID']
raise Exception('Unable to determine AWS account ID')
def role_arn(role_name, account_id=None, env=None):
env = get_environment(env)
account_id = get_account_id(account_id, env=env)
return 'arn:aws:iam::%s:role/%s' % (account_id, role_name)
def iam_resource_arn(resource, role=None, env=None):
env = get_environment(env)
if not role:
role = get_iam_role(resource, env=env)
return role_arn(role_name=role, account_id=get_account_id())
def get_iam_role(resource, env=None):
env = get_environment(env)
return 'role-%s' % resource
def dynamodb_table_arn(table_name, account_id=None):
account_id = get_account_id(account_id)
return 'arn:aws:dynamodb:%s:%s:table/%s' % (get_local_region(), account_id, table_name)
def dynamodb_stream_arn(table_name, account_id=None):
account_id = get_account_id(account_id)
return ('arn:aws:dynamodb:%s:%s:table/%s/stream/%s' %
(get_local_region(), account_id, table_name, timestamp()))
def lambda_function_arn(function_name, account_id=None):
pattern = 'arn:aws:lambda:.*:.*:function:.*'
if re.match(pattern, function_name):
return function_name
if ':' in function_name:
raise Exception('Lambda function name should not contain a colon ":"')
account_id = get_account_id(account_id)
return pattern.replace('.*', '%s') % (get_local_region(), account_id, function_name)
def cognito_user_pool_arn(user_pool_id, account_id=None):
account_id = get_account_id(account_id)
return 'arn:aws:cognito-idp:%s:%s:userpool/%s' % (get_local_region(), account_id, user_pool_id)
def kinesis_stream_arn(stream_name, account_id=None):
account_id = get_account_id(account_id)
return 'arn:aws:kinesis:%s:%s:stream/%s' % (get_local_region(), account_id, stream_name)
def firehose_stream_arn(stream_name, account_id=None):
account_id = get_account_id(account_id)
return ('arn:aws:firehose:%s:%s:deliverystream/%s' % (get_local_region(), account_id, stream_name))
def s3_bucket_arn(bucket_name, account_id=None):
return 'arn:aws:s3:::%s' % (bucket_name)
def sqs_queue_arn(queue_name, account_id=None):
account_id = get_account_id(account_id)
# ElasticMQ sets a static region of "elasticmq"
return ('arn:aws:sqs:elasticmq:%s:%s' % (account_id, queue_name))
def sns_topic_arn(topic_name, account_id=None):
account_id = get_account_id(account_id)
return ('arn:aws:sns:%s:%s:%s' % (get_local_region(), account_id, topic_name))
def get_sqs_queue_url(queue_name):
client = connect_to_service('sqs')
response = client.get_queue_url(QueueName=queue_name)
return response['QueueUrl']
def dynamodb_get_item_raw(request):
headers = mock_aws_request_headers()
headers['X-Amz-Target'] = 'DynamoDB_20120810.GetItem'
new_item = make_http_request(url=config.TEST_DYNAMODB_URL,
method='POST', data=json.dumps(request), headers=headers)
new_item = json.loads(new_item.text)
return new_item
def mock_aws_request_headers(service='dynamodb'):
ctype = APPLICATION_AMZ_JSON_1_0
if service == 'kinesis':
ctype = APPLICATION_AMZ_JSON_1_1
access_key = get_boto3_credentials().access_key
headers = {
'Content-Type': ctype,
'Accept-Encoding': 'identity',
'X-Amz-Date': '20160623T103251Z',
'Authorization': ('AWS4-HMAC-SHA256 ' +
'Credential=%s/20160623/us-east-1/%s/aws4_request, ' +
'SignedHeaders=content-type;host;x-amz-date;x-amz-target, Signature=1234') % (access_key, service)
}
return headers
def get_apigateway_integration(api_id, method, path, env=None):
apigateway = connect_to_service(service_name='apigateway', client=True, env=env)
resources = apigateway.get_resources(restApiId=api_id, limit=100)
resource_id = None
for r in resources['items']:
if r['path'] == path:
resource_id = r['id']
if not resource_id:
raise Exception('Unable to find apigateway integration for path "%s"' % path)
integration = apigateway.get_integration(
restApiId=api_id, resourceId=resource_id, httpMethod=method
)
return integration
def get_apigateway_resource_for_path(api_id, path, parent=None, resources=None):
if resources is None:
apigateway = connect_to_service(service_name='apigateway')
resources = apigateway.get_resources(restApiId=api_id, limit=100)
if not isinstance(path, list):
path = path.split('/')
if not path:
return parent
for resource in resources:
if resource['pathPart'] == path[0] and (not parent or parent['id'] == resource['parentId']):
return get_apigateway_resource_for_path(api_id, path[1:], parent=resource, resources=resources)
return None
def get_apigateway_path_for_resource(api_id, resource_id, path_suffix='', resources=None):
if resources is None:
apigateway = connect_to_service(service_name='apigateway')
resources = apigateway.get_resources(restApiId=api_id, limit=100)['items']
target_resource = list(filter(lambda res: res['id'] == resource_id, resources))[0]
path_part = target_resource.get('pathPart', '')
if path_suffix:
if path_part:
path_suffix = '%s/%s' % (path_part, path_suffix)
else:
path_suffix = path_part
parent_id = target_resource.get('parentId')
if not parent_id:
return '/%s' % path_suffix
return get_apigateway_path_for_resource(api_id, parent_id, path_suffix=path_suffix, resources=resources)
def create_api_gateway(name, description=None, resources=None, stage_name=None,
enabled_api_keys=[], env=None, usage_plan_name=None):
client = connect_to_service('apigateway', env=env)
if not resources:
resources = []
if not stage_name:
stage_name = 'testing'
if not usage_plan_name:
usage_plan_name = 'Basic Usage'
if not description:
description = 'Test description for API "%s"' % name
LOGGER.info('Creating API resources under API Gateway "%s".' % name)
api = client.create_rest_api(name=name, description=description)
# list resources
api_id = api['id']
resources_list = client.get_resources(restApiId=api_id)
root_res_id = resources_list['items'][0]['id']
# add API resources and methods
for path, methods in iteritems(resources):
# create resources recursively
parent_id = root_res_id
for path_part in path.split('/'):
api_resource = client.create_resource(restApiId=api_id, parentId=parent_id, pathPart=path_part)
parent_id = api_resource['id']
# add methods to the API resource
for method in methods:
client.put_method(
restApiId=api_id,
resourceId=api_resource['id'],
httpMethod=method['httpMethod'],
authorizationType=method.get('authorizationType') or 'NONE',
apiKeyRequired=method.get('apiKeyRequired') or False
)
# create integrations for this API resource/method
integrations = method['integrations']
create_api_gateway_integrations(api_id, api_resource['id'], method, integrations, env=env)
# deploy the API gateway
client.create_deployment(restApiId=api_id, stageName=stage_name)
return api
def create_api_gateway_integrations(api_id, resource_id, method, integrations=[], env=None):
client = connect_to_service('apigateway', env=env)
for integration in integrations:
req_templates = integration.get('requestTemplates') or {}
res_templates = integration.get('responseTemplates') or {}
success_code = integration.get('successCode') or '200'
client_error_code = integration.get('clientErrorCode') or '400'
server_error_code = integration.get('serverErrorCode') or '500'
# create integration
client.put_integration(
restApiId=api_id,
resourceId=resource_id,
httpMethod=method['httpMethod'],
integrationHttpMethod=method.get('integrationHttpMethod') or method['httpMethod'],
type=integration['type'],
uri=integration['uri'],
requestTemplates=req_templates
)
response_configs = [
{'pattern': '^2.*', 'code': success_code, 'res_templates': res_templates},
{'pattern': '^4.*', 'code': client_error_code, 'res_templates': {}},
{'pattern': '^5.*', 'code': server_error_code, 'res_templates': {}}
]
# create response configs
for response_config in response_configs:
# create integration response
client.put_integration_response(
restApiId=api_id,
resourceId=resource_id,
httpMethod=method['httpMethod'],
statusCode=response_config['code'],
responseTemplates=response_config['res_templates'],
selectionPattern=response_config['pattern']
)
# create method response
client.put_method_response(
restApiId=api_id,
resourceId=resource_id,
httpMethod=method['httpMethod'],
statusCode=response_config['code']
)
def apigateway_invocations_arn(lambda_uri):
return 'arn:aws:apigateway:%s:lambda:path/2015-03-31/functions/%s/invocations' % (DEFAULT_REGION, lambda_uri)
def get_elasticsearch_endpoint(domain=None, region_name=None):
env = get_environment(region_name=region_name)
if env.region == REGION_LOCAL:
return os.environ['TEST_ELASTICSEARCH_URL']
# get endpoint from API
es_client = connect_to_service(service_name='es', region_name=env.region)
info = es_client.describe_elasticsearch_domain(DomainName=domain)
endpoint = 'https://%s' % info['DomainStatus']['Endpoint']
return endpoint
def connect_elasticsearch(endpoint=None, domain=None, region_name=None, env=None):
from elasticsearch import Elasticsearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
env = get_environment(env, region_name=region_name)
verify_certs = False
use_ssl = False
if not endpoint and env.region == REGION_LOCAL:
endpoint = os.environ['TEST_ELASTICSEARCH_URL']
if not endpoint and env.region != REGION_LOCAL and domain:
endpoint = get_elasticsearch_endpoint(domain=domain, region_name=env.region)
# use ssl?
if 'https://' in endpoint:
use_ssl = True
if env.region != REGION_LOCAL:
verify_certs = True
if CUSTOM_BOTO3_SESSION or (ENV_ACCESS_KEY in os.environ and ENV_SECRET_KEY in os.environ):
access_key = os.environ.get(ENV_ACCESS_KEY)
secret_key = os.environ.get(ENV_SECRET_KEY)
session_token = os.environ.get(ENV_SESSION_TOKEN)
if CUSTOM_BOTO3_SESSION:
credentials = CUSTOM_BOTO3_SESSION.get_credentials()
access_key = credentials.access_key
secret_key = credentials.secret_key
session_token = credentials.token
awsauth = AWS4Auth(access_key, secret_key, env.region, 'es', session_token=session_token)
connection_class = RequestsHttpConnection
return Elasticsearch(hosts=[endpoint], verify_certs=verify_certs, use_ssl=use_ssl,
connection_class=connection_class, http_auth=awsauth)
return Elasticsearch(hosts=[endpoint], verify_certs=verify_certs, use_ssl=use_ssl)
def create_kinesis_stream(stream_name, shards=1, env=None, delete=False):
env = get_environment(env)
# stream
stream = KinesisStream(id=stream_name, num_shards=shards)
conn = connect_to_service('kinesis', env=env)
stream.connect(conn)
if delete:
run_safe(lambda: stream.destroy(), print_error=False)
stream.create()
stream.wait_for()
return stream
def kinesis_get_latest_records(stream_name, shard_id, count=10, env=None):
kinesis = connect_to_service('kinesis', env=env)
result = []
response = kinesis.get_shard_iterator(StreamName=stream_name, ShardId=shard_id,
ShardIteratorType='TRIM_HORIZON')
shard_iterator = response['ShardIterator']
while shard_iterator:
records_response = kinesis.get_records(ShardIterator=shard_iterator)
records = records_response['Records']
for record in records:
try:
record['Data'] = to_str(record['Data'])
except Exception:
pass
result.extend(records)
shard_iterator = records_response['NextShardIterator'] if records else False
while len(result) > count:
result.pop(0)
return result