-
Notifications
You must be signed in to change notification settings - Fork 269
/
fire.py
executable file
·418 lines (378 loc) · 14.6 KB
/
fire.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
415
416
417
418
#!/usr/bin/env python3
from multiprocessing import Pool
from pathlib import Path
import shutil
import tldextract
import boto3
import os
import sys
import datetime
import tzlocal
import argparse
import json
import configparser
from typing import Tuple, Callable
class FireProx(object):
def __init__(self, arguments: argparse.Namespace, help_text: str):
self.profile_name = arguments.profile_name
self.access_key = arguments.access_key
self.secret_access_key = arguments.secret_access_key
self.session_token = arguments.session_token
self.region = arguments.region
self.command = arguments.command
self.api_id = arguments.api_id
self.url = arguments.url
self.api_list = []
self.client = None
self.help = help_text
if self.access_key and self.secret_access_key:
if not self.region:
self.error('Please provide a region with AWS credentials')
if not self.load_creds():
self.error('Unable to load AWS credentials')
if not self.command:
self.error('Please provide a valid command')
def __str__(self):
return 'FireProx()'
def _try_instance_profile(self) -> bool:
"""Try instance profile credentials
:return:
"""
try:
if not self.region:
self.client = boto3.client('apigateway')
else:
self.client = boto3.client(
'apigateway',
region_name=self.region
)
self.client.get_account()
self.region = self.client._client_config.region_name
return True
except:
return False
def load_creds(self) -> bool:
"""Load credentials from AWS config and credentials files if present.
:return:
"""
# If no access_key, secret_key, or profile name provided, try instance credentials
if not any([self.access_key, self.secret_access_key, self.profile_name]):
return self._try_instance_profile()
# Read in AWS config/credentials files if they exist
credentials = configparser.ConfigParser()
credentials.read(os.path.expanduser('~/.aws/credentials'))
config = configparser.ConfigParser()
config.read(os.path.expanduser('~/.aws/config'))
# If profile in files, try it, but flow through if it does not work
config_profile_section = f'profile {self.profile_name}'
if self.profile_name in credentials:
if config_profile_section not in config:
print(f'Please create a section for {self.profile_name} in your ~/.aws/config file')
return False
self.region = config[config_profile_section].get('region', 'us-east-1')
try:
self.client = boto3.session.Session(profile_name=self.profile_name,
region_name=self.region).client('apigateway')
self.client.get_account()
return True
except:
pass
# Maybe had profile, maybe didn't
if self.access_key and self.secret_access_key:
try:
self.client = boto3.client(
'apigateway',
aws_access_key_id=self.access_key,
aws_secret_access_key=self.secret_access_key,
aws_session_token=self.session_token,
region_name=self.region
)
self.client.get_account()
self.region = self.client._client_config.region_name
# Save/overwrite config if profile specified
if self.profile_name:
if config_profile_section not in config:
config.add_section(config_profile_section)
config[config_profile_section]['region'] = self.region
with open(os.path.expanduser('~/.aws/config'), 'w') as file:
config.write(file)
if self.profile_name not in credentials:
credentials.add_section(self.profile_name)
credentials[self.profile_name]['aws_access_key_id'] = self.access_key
credentials[self.profile_name]['aws_secret_access_key'] = self.secret_access_key
if self.session_token:
credentials[self.profile_name]['aws_session_token'] = self.session_token
else:
credentials.remove_option(self.profile_name, 'aws_session_token')
with open(os.path.expanduser('~/.aws/credentials'), 'w') as file:
credentials.write(file)
return True
except:
return False
else:
return False
def error(self, error):
print(self.help)
sys.exit(error)
def get_template(self):
url = self.url
if url[-1] == '/':
url = url[:-1]
title = 'fireprox_{}'.format(
tldextract.extract(url).domain
)
version_date = f'{datetime.datetime.now():%Y-%m-%dT%XZ}'
template = '''
{
"swagger": "2.0",
"info": {
"version": "{{version_date}}",
"title": "{{title}}"
},
"basePath": "/",
"schemes": [
"https"
],
"paths": {
"/": {
"get": {
"parameters": [
{
"name": "proxy",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "X-My-X-Forwarded-For",
"in": "header",
"required": false,
"type": "string"
}
],
"responses": {},
"x-amazon-apigateway-integration": {
"uri": "{{url}}/",
"responses": {
"default": {
"statusCode": "200"
}
},
"requestParameters": {
"integration.request.path.proxy": "method.request.path.proxy",
"integration.request.header.X-Forwarded-For": "method.request.header.X-My-X-Forwarded-For"
},
"passthroughBehavior": "when_no_match",
"httpMethod": "ANY",
"cacheNamespace": "irx7tm",
"cacheKeyParameters": [
"method.request.path.proxy"
],
"type": "http_proxy"
}
}
},
"/{proxy+}": {
"x-amazon-apigateway-any-method": {
"produces": [
"application/json"
],
"parameters": [
{
"name": "proxy",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "X-My-X-Forwarded-For",
"in": "header",
"required": false,
"type": "string"
}
],
"responses": {},
"x-amazon-apigateway-integration": {
"uri": "{{url}}/{proxy}",
"responses": {
"default": {
"statusCode": "200"
}
},
"requestParameters": {
"integration.request.path.proxy": "method.request.path.proxy",
"integration.request.header.X-Forwarded-For": "method.request.header.X-My-X-Forwarded-For"
},
"passthroughBehavior": "when_no_match",
"httpMethod": "ANY",
"cacheNamespace": "irx7tm",
"cacheKeyParameters": [
"method.request.path.proxy"
],
"type": "http_proxy"
}
}
}
}
}
'''
template = template.replace('{{url}}', url)
template = template.replace('{{title}}', title)
template = template.replace('{{version_date}}', version_date)
return str.encode(template)
def create_api(self, url):
if not url:
self.error('Please provide a valid URL end-point')
print(f'Creating => {url}...')
template = self.get_template()
response = self.client.import_rest_api(
parameters={
'endpointConfigurationTypes': 'REGIONAL'
},
body=template
)
resource_id, proxy_url = self.create_deployment(response['id'])
self.store_api(
response['id'],
response['name'],
response['createdDate'],
response['version'],
url,
resource_id,
proxy_url
)
def update_api(self, api_id, url):
if not any([api_id, url]):
self.error('Please provide a valid API ID and URL end-point')
if url[-1] == '/':
url = url[:-1]
resource_id = self.get_resource(api_id)
if resource_id:
print(f'Found resource {resource_id} for {api_id}!')
response = self.client.update_integration(
restApiId=api_id,
resourceId=resource_id,
httpMethod='ANY',
patchOperations=[
{
'op': 'replace',
'path': '/uri',
'value': '{}/{}'.format(url, r'{proxy}'),
},
]
)
return response['uri'].replace('/{proxy}', '') == url
else:
self.error(f'Unable to update, no valid resource for {api_id}')
def delete_api(self, api_id):
if not api_id:
self.error('Please provide a valid API ID')
items = self.list_api(api_id)
for item in items:
item_api_id = item['id']
if item_api_id == api_id:
response = self.client.delete_rest_api(
restApiId=api_id
)
return True
return False
def list_api(self, deleted_api_id=None):
response = self.client.get_rest_apis()
for item in response['items']:
try:
created_dt = item['createdDate']
api_id = item['id']
name = item['name']
proxy_url = self.get_integration(api_id).replace('{proxy}', '')
url = f'https://{api_id}.execute-api.{self.region}.amazonaws.com/fireprox/'
if not api_id == deleted_api_id:
print(f'[{created_dt}] ({api_id}) {name}: {url} => {proxy_url}')
except:
pass
return response['items']
def store_api(self, api_id, name, created_dt, version_dt, url,
resource_id, proxy_url):
print(
f'[{created_dt}] ({api_id}) {name} => {proxy_url} ({url})'
)
def create_deployment(self, api_id):
if not api_id:
self.error('Please provide a valid API ID')
response = self.client.create_deployment(
restApiId=api_id,
stageName='fireprox',
stageDescription='FireProx Prod',
description='FireProx Production Deployment'
)
resource_id = response['id']
return (resource_id,
f'https://{api_id}.execute-api.{self.region}.amazonaws.com/fireprox/')
def get_resource(self, api_id):
if not api_id:
self.error('Please provide a valid API ID')
response = self.client.get_resources(restApiId=api_id)
items = response['items']
for item in items:
item_id = item['id']
item_path = item['path']
if item_path == '/{proxy+}':
return item_id
return None
def get_integration(self, api_id):
if not api_id:
self.error('Please provide a valid API ID')
resource_id = self.get_resource(api_id)
response = self.client.get_integration(
restApiId=api_id,
resourceId=resource_id,
httpMethod='ANY'
)
return response['uri']
def parse_arguments() -> Tuple[argparse.Namespace, str]:
"""Parse command line arguments and return namespace
:return: Namespace for arguments and help text as a tuple
"""
parser = argparse.ArgumentParser(description='FireProx API Gateway Manager')
parser.add_argument('--profile_name',
help='AWS Profile Name to store/retrieve credentials', type=str, default=None)
parser.add_argument('--access_key',
help='AWS Access Key', type=str, default=None)
parser.add_argument('--secret_access_key',
help='AWS Secret Access Key', type=str, default=None)
parser.add_argument('--session_token',
help='AWS Session Token', type=str, default=None)
parser.add_argument('--region',
help='AWS Region', type=str, default=None)
parser.add_argument('--command',
help='Commands: list, create, delete, update', type=str, default=None)
parser.add_argument('--api_id',
help='API ID', type=str, required=False)
parser.add_argument('--url',
help='URL end-point', type=str, required=False)
return parser.parse_args(), parser.format_help()
def main():
"""Run the main program
:return:
"""
args, help_text = parse_arguments()
fp = FireProx(args, help_text)
if args.command == 'list':
print(f'Listing API\'s...')
result = fp.list_api()
elif args.command == 'create':
result = fp.create_api(fp.url)
elif args.command == 'delete':
result = fp.delete_api(fp.api_id)
success = 'Success!' if result else 'Failed!'
print(f'Deleting {fp.api_id} => {success}')
elif args.command == 'update':
print(f'Updating {fp.api_id} => {fp.url}...')
result = fp.update_api(fp.api_id, fp.url)
success = 'Success!' if result else 'Failed!'
print(f'API Update Complete: {success}')
else:
print(f'[ERROR] Unsupported command: {args.command}\n')
print(help_text)
sys.exit(1)
if __name__ == '__main__':
main()