-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_helpers.py
429 lines (364 loc) · 11.9 KB
/
aws_helpers.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
419
420
421
422
423
424
425
426
427
428
429
"""Helper class for AWS API access"""
#!/usr/bin/python
#***************************************************************************
# Authors/Maintainers: Mike O'Brien (miobrien@amazon.com)
#
# Description: Helper class for retrieving data from AWS API
#---------------------------------------------------------------------------
# Notes
#---------------------------------------------------------------------------
# To Do List:
# -----------
# - no known issues
#***************************************************************************
# *******************************************************************
# Required Modules:
# *******************************************************************
import boto3
from botocore.exceptions import ClientError
import json
import logging
import os
CLIENT = {}
session = ''
class AWSClient:
profile = ''
region = ''
session = None
def __init__(self, profile=None):
if profile:
session = boto3.session.Session(profile_name=profile)
boto3.setup_default_session(profile_name=profile)
def connect(self, service, region):
"""
Connect to AWS api with connection caching
Returns: connection handle
"""
if service in CLIENT and region in CLIENT[service]:
return CLIENT[service][region]
if service not in CLIENT:
CLIENT[service] = {}
try:
CLIENT[service][region] = boto3.client(service, region_name=region)
except Exception as exc:
print(exc)
print(f'Could not connect to {service} in region {region}')
raise
else:
return CLIENT[service][region]
def whoami(self, region='us-east-1'):
"""
get local account info
"""
conn = self.connect('sts', region)
retstuff = conn.get_caller_identity()
return retstuff
def get_tokenized(self, region, service, apimethod, responsekey, tokenparm='NextToken', maxname='MaxResults', tokenname='NextToken'):
"""
Need to figure out how to interpolate the method (api name)
Intent is to place the numerous times this code appears
"""
conn = self.connect(service, region)
token = 'start'
result = []
while token:
if token == 'start': # needed a value to start the loop. Now reset it
token = ''
kwargs = {}
if token:
kwargs = { tokenparm: token, maxname: 100 }
else:
kwargs = { maxname: 100 }
response = getattr(conn,apimethod)(
**kwargs
)
if tokenname in response:
token = response[tokenname]
else:
token = ''
result = result + response[responsekey]
return result
def get_simple(self, region, service, apimethod, responsekey, kwargs={}):
"""
Simple query-capture-return
"""
conn = self.connect(service, region)
response = getattr(conn,apimethod)(
**kwargs
)
return response[responsekey]
#======================================================================
# get_vpcs
#======================================================================
def get_vpcs (self, region):
"""
Input: str(region)
Returns: list of vpcs
"""
return self.get_tokenized(
region,
'ec2',
'describe_vpcs',
'Vpcs'
)
#======================================================================
# get_sgs
#======================================================================
def get_sgs (self, region):
"""
Input: str(region)
Returns: list of SecurityGroups
get_aws_securitygroups: get the data for all sgs in the account in this region
"""
return self.get_tokenized(
region,
'ec2',
'describe_security_groups',
'SecurityGroups'
)
#======================================================================
# sg_exists
#======================================================================
def sg_exists (self, region, sg):
"""
Input: str(region), str(sg)
Returns: boolen
"""
conn = self.connect('ec2', region)
response = conn.describe_security_groups(
Filters=[
{
'Name' : 'group-name',
'Values' : [sg]
},
],
)['SecurityGroups']
return bool(len(response))
def flow_logs_enabled (self, region, vpc):
"""
Input: region, vpc id
Returns: bool
"""
return bool(len(self.get_tokenized(
region,
'ec2',
'describe_flow_logs',
'FlowLogs'
)))
def get_vpc_endpoints (self, region, vpc):
"""
Input: region, vpc id
Returns: list of endpoints
"""
return self.get_tokenized(
region,
'ec2',
'describe_vpc_endpoints',
'VpcEndpoints'
)
def get_vpc_peerings (self, region):
"""
Input: region
Returns: list of active peering connections
"""
conn = self.connect('ec2', region)
response = conn.describe_vpc_peering_connections(
Filters=[
{
'Name': 'status-code',
'Values' : ['active']
}
]
)['VpcPeeringConnections']
return response
def get_dhcp_options(self, region, dhcpoptionsid):
"""
Input: region, dhcpoptionsid
Returns: json response
"""
conn = self.connect('ec2', region)
response = conn.describe_dhcp_options(
DhcpOptionsIds=[
dhcpoptionsid,
]
)['DhcpOptions']
return response
def get_route_tables(self, region, vpc):
"""
Get route tables for a vpc
Returns json
"""
conn = self.connect('ec2', region)
response = conn.describe_route_tables(
Filters=[
{
'Name': 'vpc-id',
'Values': [vpc]
}
]
)['RouteTables']
return response
def get_subnets (self, region, vpc=False):
"""
Get all subnets
Returns json
"""
conn = self.connect('ec2', region)
apiargs = {}
if vpc:
apiargs['Filters'] = [ { 'Name': 'vpc-id', 'Values': [ vpc ] } ]
response = conn.describe_subnets(
**apiargs
)['Subnets']
return response
#======================================================================
# get_interfaces
#======================================================================
def get_interfaces(self, myregion):
"""
get_interfaces: get the data for all interfaces in the account in this region
"""
interfaces = []
conn = self.connect('ec2', myregion)
response = conn.describe_network_interfaces(
MaxResults=100,
Filters=[
{
'Name': 'description',
'Values': [
'ELB *'
]
}
]
)
interfaces = interfaces + response['NetworkInterfaces']
while 'NextToken' in response:
response = conn.describe_network_interfaces(
NextToken=response['NextToken'],
MaxResults=100,
Filters=[
{
'Name': 'description',
'Values': [
'ELB *'
]
}
]
)
interfaces = interfaces + response['NetworkInterfaces']
return interfaces
#======================================================================
# get_rds
#======================================================================
def get_rds(self, region):
"""get the data for all RDS instances in the account in this region"""
return self.get_tokenized(
region,
'rds',
'describe_db_instances',
'DBInstances',
'Marker',
'MaxRecords',
'Marker'
)
def role_exists(self, rolename):
"""
Given a role(str)
Return bool
"""
conn = self.connect('iam', 'us-east-1')
try:
conn.get_role(RoleName=rolename)
return True
except ClientError as e:
if e.response['Error']['Code'] == 'NoSuchEntity':
return False
raise e
except Exception as e:
raise e
#------------------------------------------------------------------
# lambda_exists
#------------------------------------------------------------------
def lambda_exists(self, region, lambdaname):
"""
Given a lambda(str)
Return bool
"""
conn = self.connect('lambda', region)
try:
conn.get_function(FunctionName=lambdaname)
return True
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
return False
raise e
except Exception as e:
raise e
def support_level (self):
"""
Return string 'non-paid', 'business', or 'enterprise'
"""
conn = self.connect('support', 'us-east-1')
try:
sevs = conn.describe_severity_levels()['severityLevels']
if len(sevs) == 5:
return 'enterprise'
elif len(sevs) == 4:
return 'business'
else:
return 'non-paid'
except ClientError as e:
if e.response['Error']['Code'] == 'SubscriptionRequiredException':
return 'non-paid'
raise e
except Exception as e:
raise e
return 'non-paid'
def getLambdaEnv(parmname, defaultval=None):
"""
Cleanly get the value of a Lambda environmental. Return if found or default
"""
try:
myval = os.environ[parmname]
except Exception as exc:
if str(exc) == f'\'{parmname}\'':
myval = defaultval
print(
'Environmental variable \'' + parmname +
'\' not found. Using default [' + str(defaultval) + ']')
else:
print(exc)
print(
'ERROR: Environmental variable \'' + parmname +
'\' not found. Exiting')
raise
# recast if int or bool
if isinstance(defaultval, int):
myval = int(myval)
elif myval == 'True':
myval = True
elif myval == 'False':
myval = False
return myval
def configLogging(logger, logLevel):
# global logger # If already set up don't set it up again
if logger:
return logger
else:
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter('%(asctime)s :: %(lineno)s ::%(levelname)s :: %(funcName)10s :: %(message)s'))
logger = logging.getLogger('ConfigDiscoveryLogger')
logger.addHandler(stream_handler)
if logLevel=='DEBUG':
logger.setLevel(logging.DEBUG)
elif logLevel=='INFO':
logger.setLevel(logging.INFO)
elif logLevel=='WARNING':
logger.setLevel(logging.WARNING)
elif logLevel=='ERROR':
logger.setLevel(logging.ERROR)
elif logLevel=='CRITICAL':
logger.setLevel(logging.CRITICAL)
else:
logger.setLevel(logging.NOTSET)
return logger