-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
68 lines (61 loc) · 2.09 KB
/
Copy pathhandler.py
File metadata and controls
68 lines (61 loc) · 2.09 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
import hashlib
import json
import copy
import urllib3
import boto3
import socket
def update_security_group(hostname,secgroup):
try:
new_ip_address = socket.gethostbyname(hostname)
except:
return {
"statusCode": 400,
"body": json.dumps('DNS %s does not exist' % (hostname) )
}
else:
#print (new_ip_address)
try:
client= boto3.client('ec2')
response= client.describe_security_groups(GroupIds=[secgroup])
group= response['SecurityGroups'][0]
except:
return {
"statusCode": 400,
"body": json.dumps('Security group %s does not contain %s' % (secgroup,hostname) )
}
for permission in group['IpPermissions']:
new_permission = copy.deepcopy(permission)
ip_ranges = new_permission['IpRanges']
for ip_range in ip_ranges:
try:
if ip_range['Description'] == hostname:
if ip_range['CidrIp'] != "%s/32" % new_ip_address:
ip_range['CidrIp'] = "%s/32" % new_ip_address
client.revoke_security_group_ingress(GroupId=group['GroupId'], IpPermissions=[permission])
client.authorize_security_group_ingress(GroupId=group['GroupId'], IpPermissions=[new_permission])
except KeyError:
pass
return {
"statusCode": 200,
"body": json.dumps('%s Updated to %s' % (hostname,new_ip_address) )
}
def lambda_handler(event, context):
#print (type(event))
try:
postdata = json.loads(event['body'])
except:
return {
"statusCode": 400,
"body": json.dumps({
"message" : "missing information"}
)}
else:
hostname = postdata['hostname']
secgroup = postdata['secgroup']
result = update_security_group(hostname,secgroup)
return {
"statusCode": 200,
"body": json.dumps({
"message" : result}
)
}