-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
executable file
·369 lines (285 loc) · 13.8 KB
/
lambda_function.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
import boto3
from botocore.exceptions import ClientError
from jira import JIRA
from jira import JIRAError
import json
import os
jira_user = os.environ["jira_user"]
jira_token = os.environ["jira_token"]
# set using the get_aws_account() function
api_key = None
api_secret = None
# infra key set using the get_aws_account() function
infra_key = None
infra_secret = None
# api key id (used to retrieve api key name from agi gateway)
apigateway_api_key_id = None
def lambda_handler(event, context):
# print("Received event: " + json.dumps(event, indent=2))
print("Log stream name:", context.log_stream_name)
print("Log group name:", context.log_group_name)
print("Request ID:", context.aws_request_id)
print("Mem. limits(MB):", context.memory_limit_in_mb)
# Code will execute quickly, so we add a 1 second intentional delay so you can see that in time remaining value.
print("Time remaining (MS):", context.get_remaining_time_in_millis())
# get account's api key and secret using aws_account passed in as argument
try:
# api key id for api gateway user key
global apigateway_api_key_id
apigateway_api_key_id = event['requestContext']['identity']['apiKeyId']
body_values = json.loads(event['body'])
# response is { api_key : api_secret } dict
get_aws_account_response = get_aws_account_creds(body_values['aws_account'])
# modify globals
global api_key
api_key = next(iter(get_aws_account_response.keys()))
global api_secret
api_secret = next(iter(get_aws_account_response.values()))
except ClientError as e:
print("ClientError: ", e.response)
return {
'statusCode': 500,
'body': json.dumps("ClientError: {}".format(str(e.response))),
'headers': {'Content-Type': 'application/json'}
}
except Exception as e:
print("Exception: ", e)
return {
'statusCode': 500,
'body': json.dumps("Exception: {}".format(str(e))),
'headers': {'Content-Type': 'application/json'}
}
try:
if event['httpMethod'] == 'GET':
print("GET method")
# body values passed in
body_values = json.loads(event['body'])
secgroup_id = body_values['secgroup_id']
region = body_values["region"]
# get sec group rules
sec_group_rules_response = get_rules(region=region, secgroup_id=secgroup_id)
return {
'statusCode': 200,
'body': json.dumps("{}".format(sec_group_rules_response)),
'headers': {'Content-Type': 'application/json'}
}
if event['httpMethod'] == 'DELETE':
print("DELETE method")
# body values passed in
body_values = json.loads(event['body'])
user = body_values["user"]
region = body_values["region"]
secgroup_id = body_values["secgroup_id"]
protocol = body_values["protocol"]
cidr_ip = body_values["cidr_ip"]
port = int(body_values["port"])
sor_ticket = body_values["sor_ticket"]
remove_rule_response = remove_rule(user=user, region=region, secgroup_id=secgroup_id, protocol=protocol, cidr_ip=cidr_ip, port=port, sor_ticket=sor_ticket)
return {
'statusCode': 200,
'body': json.dumps("{}".format(str(remove_rule_response))),
'headers': {'Content-Type': 'application/json'}
}
if event['httpMethod'] == 'POST':
print("POST method")
# body values passed in
body_values = json.loads(event['body'])
user = body_values["user"]
region = body_values["region"]
secgroup_id = body_values["secgroup_id"]
protocol = body_values["protocol"]
cidr_ip = body_values["cidr_ip"]
port = int(body_values["port"])
sor_ticket = body_values["sor_ticket"]
add_rule_response = add_rule(user=user, region=region, secgroup_id=secgroup_id, protocol=protocol, cidr_ip=cidr_ip, port=port, sor_ticket=sor_ticket)
return {
'statusCode': 200,
'body': json.dumps("{}".format(str(add_rule_response))),
'headers': {'Content-Type': 'application/json'}
}
except KeyError as e:
return {
'statusCode': 500,
'body': json.dumps("KeyError: check that this request has the correct parameter and argument for: {}".format(str(e))),
'headers': {'Content-Type': 'application/json'}
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps("Exception in Lambda Handler: {}".format(str(e))),
'headers': {'Content-Type': 'application/json'}
}
# takes in aws account argument from user
def get_aws_account_creds(aws_account):
accounts = {}
# customer1 creds
customer1_key = os.environ["customer1_key"]
customer1_secret = os.environ["customer1_secret"]
accounts['symphony-aws-customer1'] = {customer1_key: customer1_secret}
# customer 2 creds
customer2_key = os.environ["customer2_key"]
customer2_secret = os.environ["customer2_secret"]
accounts['symphony-aws-customer2'] = {customer2_key: customer2_secret}
# infra creds
global infra_key
infra_key = os.environ["infra_key"]
global infra_secret
infra_secret = os.environ["infra_secret"]
accounts['symphony-aws-infra'] = {infra_key: infra_secret}
return accounts[aws_account]
def jira_ticket_add_comment(user, action, secgroup_id, protocol, cidr_ip, port, sor_ticket):
jira_api_url = "https://perzoinc.atlassian.net"
jira = JIRA(jira_api_url, basic_auth=(jira_user, jira_token))
issue = jira.issue(sor_ticket)
issue_comment = "{} {} rule {} \nPort: {}\nProtocol: {}\n Security Group ID: {}".format(user, action, cidr_ip, port, protocol, secgroup_id)
jira_response = jira.add_comment(issue, issue_comment)
print("Jira response: {} {} rule {} Port: {}, Protocol: {}, Security Group ID: {}".format(user, action, cidr_ip, port, protocol, secgroup_id))
return "Jira response: {} {} rule {} Port: {}, Protocol: {}, Security Group ID: {}".format(user, action, cidr_ip, port, protocol, secgroup_id)
def get_boto_resource(region, service, secgroup_id):
ec2 = boto3.resource(
service,
region_name=region,
aws_access_key_id=api_key,
aws_secret_access_key=api_secret,
)
security_group = ec2.SecurityGroup(secgroup_id)
return security_group
def get_boto_client(region, service):
boto_client = boto3.client(
service,
region_name=region,
aws_access_key_id=api_key,
aws_secret_access_key=api_secret,
)
return boto_client
# get api key name for user key from api gateway
def get_apigateway_api_key_name(apiKeyId):
try:
boto_client = boto3.client(
'apigateway',
region_name='us-east-1',
aws_access_key_id=str(infra_key),
aws_secret_access_key=str(infra_secret),
)
get_api_key_response = boto_client.get_api_key(
apiKey=str(apiKeyId),
includeValue=False
)
except ClientError as e:
print("Client Error: {}".format(str(e)))
return "Client Error: {}".format(str(e))
return str(get_api_key_response['name'])
# checks if sec group is 443/8444 or 22(sftp) sec group
def check_allowed_secgroup(region, secgroup_id):
sec_group_boto_client = get_boto_client(region=region, service='ec2')
group_name = "empty security group name"
try:
response = sec_group_boto_client.describe_security_groups(GroupIds=[secgroup_id])
group_name = str(response['SecurityGroups'][0]['GroupName']).lower()
except ClientError as e:
print("Client Error: {}".format(str(e.response)))
return "Client Error: {}".format(str(e.response))
print("Security group name: {}".format(group_name))
# checks for correct sec group
if (('sftp' in group_name) or ('443' in group_name) or ('8444' in group_name)):
if ('4432407' not in group_name) and ('443_2407' not in group_name) and ('443mds' not in group_name):
# print("Correct sec group")
return True
else:
# print("Incorrect sec group")
return False
else:
# print("Incorrect sec group")
return False
def get_rules(region, secgroup_id):
sec_group_boto_client = get_boto_client(region=region, service='ec2')
try:
response = sec_group_boto_client.describe_security_groups(GroupIds=[secgroup_id])
sec_group_rules = response['SecurityGroups'][0]['IpPermissions']
print(sec_group_rules)
return sec_group_rules
except ClientError as e:
print("Client Error: {}".format(str(e.response)))
return "Client Error: {}".format(str(e.response))
def remove_rule(user, region, secgroup_id, protocol, cidr_ip, port, sor_ticket):
# ensure correct ticket number is added
if str(sor_ticket).startswith('SOR-'):
pass
else:
print("Failed: Description argument needs to start with 'SOR-'")
return "Failed: Description argument needs to start with 'SOR-'"
# True if yes, False if no
allowed_to_modify = check_allowed_secgroup(region=region, secgroup_id=secgroup_id)
print("Allowed to modify sec group: {}".format(allowed_to_modify))
apigateway_key_name = get_apigateway_api_key_name(apigateway_api_key_id)
# can only modify following ports (standard, api or sftp)
if ((port == 443 or port == 8444 or port == 22) and (allowed_to_modify)):
try:
# jira_response = jira_ticket_add_comment(user, "removed", secgroup_id, protocol, cidr_ip, port, sor_ticket)
sec_group_boto_resource = get_boto_resource(region=region, service='ec2', secgroup_id=secgroup_id)
response = sec_group_boto_resource.revoke_ingress(
IpProtocol=protocol,
CidrIp=cidr_ip,
FromPort=port,
ToPort=port,
DryRun=False
)
print("Successfully removed rule; user: {} ({}), cidr: {}, port: {}, protocol: {}, security group: {}, ticket {}".format(user, apigateway_key_name, cidr_ip, port, protocol, secgroup_id, sor_ticket))
return "Successfully removed rule; user: {} ({}), cidr: {}, port: {}, protocol: {}, security group: {}, ticket {}".format(user, apigateway_key_name, cidr_ip, port, protocol, secgroup_id, sor_ticket)
except JIRAError as je:
print("Jira: failed adding comment " + je.text)
return "Jira: failed adding comment " + je.text
except ClientError as e:
print("Client Error: {}".format(str(e.response)))
return "Client Error: {}".format(str(e.response))
except Exception as e:
print("Exception: {}".format(str(e)))
return "Exception: {}".format(str(e))
else:
print("Failed: Not allowed to modify. Check allowed ports and ensure you're allowed to modify this security group")
return "Failed: Not allowed to modify. Check allowed ports and ensure you're allowed to modify this security group"
def add_rule(user, region, secgroup_id, protocol, cidr_ip, port, sor_ticket):
if str(sor_ticket).startswith('SOR-'):
pass
else:
print("Failed: Description argument needs to start with 'SOR-'")
return "Failed: Description argument needs to start with 'SOR-'"
# True if yes, False if no
allowed_to_modify = check_allowed_secgroup(region=region, secgroup_id=secgroup_id)
print("Allowed to modify sec group: {}".format(allowed_to_modify))
apigateway_key_name = get_apigateway_api_key_name(apigateway_api_key_id)
# can only modify following ports (standard, api or sftp)
if ((port == 443 or port == 8444 or port == 22) and (allowed_to_modify)):
try:
# jira_response = jira_ticket_add_comment(user, "added", secgroup_id, protocol, cidr_ip, port, sor_ticket)
sec_group_boto_resource = get_boto_resource(region=region, service='ec2', secgroup_id=secgroup_id)
response = sec_group_boto_resource.authorize_ingress(
IpPermissions=[
{
'FromPort': port,
'IpProtocol': protocol,
'IpRanges': [
{
'CidrIp': cidr_ip,
'Description': sor_ticket
},
],
'ToPort': port,
},
],
DryRun=False
)
print("Successfully added rule; user: {} ({}), cidr: {}, port: {}, protocol: {}, security group: {}, ticket {}".format(user, apigateway_key_name, cidr_ip, port, protocol, secgroup_id, sor_ticket))
return "Successfully added rule; user: {} ({}), cidr: {}, port: {}, protocol: {}, security group: {}, ticket {}".format(user, apigateway_key_name, cidr_ip, port, protocol, secgroup_id, sor_ticket)
except JIRAError as je:
print("Jira: failed adding comment " + je.text)
return "Jira: failed adding comment " + je.text
except ClientError as e:
print("Client Error: {}".format(str(e)))
return "Client Error: {}".format(str(e))
except Exception as e:
print("Exception: {}".format(str(e)))
return "Exception: {}".format(str(e))
else:
print("Failed: Not allowed to modify. Check allowed ports and ensure you're allowed to modify this security group")
return "Failed: Not allowed to modify. Check allowed ports and ensure you're allowed to modify this security group"