-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda.py
222 lines (171 loc) · 7.58 KB
/
lambda.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
'''
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
'''
import boto3
import hashlib
import json
import urllib2
# Name of the service, as seen in the ip-groups.json file, to extract information for
SERVICE = "CLOUDFRONT"
# Ports your application uses that need inbound permissions from the service for
INGRESS_PORTS = {'Https': 443}
# Security GroupIds which identify the security groups you want to update with global Ips
GLOBAL_SECURITY_GROUP_IDS = [
'sg-########',
'sg-########'
]
# Security GroupIds which identify the security groups you want to update with regional Ips
REGIONAL_SECURITY_GROUP_IDS = [
'sg-########',
'sg-########'
]
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
message = json.loads(event['Records'][0]['Sns']['Message'])
# Load the ip ranges from the url
ip_ranges = json.loads(get_ip_groups_json(message['url'], message['md5']))
# extract the service ranges
global_cf_ranges = get_ranges_for_service(ip_ranges, SERVICE, "GLOBAL")
region_cf_ranges = get_ranges_for_service(ip_ranges, SERVICE, "REGION")
ip_ranges = {"GLOBAL": global_cf_ranges, "REGION": region_cf_ranges}
# update the security groups
result = update_security_groups(ip_ranges)
return result
def get_ip_groups_json(url, expected_hash):
print("Updating from " + url)
response = urllib2.urlopen(url)
ip_json = response.read()
m = hashlib.md5()
m.update(ip_json)
hash = m.hexdigest()
if hash != expected_hash:
raise Exception('MD5 Mismatch: got ' + hash + ' expected ' + expected_hash)
return ip_json
def get_ranges_for_service(ranges, service, subset):
service_ranges = list()
for prefix in ranges['prefixes']:
if prefix['service'] == service and ((subset == prefix['region'] and subset == "GLOBAL") or (subset != 'GLOBAL' and prefix['region'] != 'GLOBAL')):
print('Found ' + service + ' region: ' + prefix['region'] + ' range: ' + prefix['ip_prefix'])
service_ranges.append(prefix['ip_prefix'])
return service_ranges
def update_security_groups(new_ranges):
client = boto3.client('ec2')
global_security_groups = list()
regional_security_groups = list()
for sec_group_id in GLOBAL_SECURITY_GROUP_IDS:
global_security_groups.append(
get_security_groups_for_update(client, sec_group_id)
)
for sec_group_id in REGIONAL_SECURITY_GROUP_IDS:
regional_security_groups.append(
get_security_groups_for_update(client, sec_group_id)
)
print('Found ' + str(len(global_security_groups)) + ' Global HttpsSecurityGroups to update')
print('Found ' + str(len(regional_security_groups)) + ' Regional HttpsSecurityGroups to update')
result = list()
global_https_updated = 0
region_https_updated = 0
for groups in global_security_groups:
for group in groups:
if update_security_group(client, group, new_ranges["GLOBAL"], INGRESS_PORTS['Https']):
global_https_updated += 1
result.append('Updated: ' + group['GroupId'])
for groups in regional_security_groups:
for group in groups:
if update_security_group(client, group, new_ranges["REGION"], INGRESS_PORTS['Https']):
region_https_updated += 1
result.append('Updated: ' + group['GroupId'])
result.append(
'Updated: ' + str(global_https_updated) +
' of: ' + str(len(global_security_groups)) +
' CloudFront_g HttpsSecurityGroups'
)
result.append(
'Updated: ' + str(region_https_updated) +
' of: ' + str(len(regional_security_groups)) +
' CloudFront_r HttpsSecurityGroups'
)
return result
def update_security_group(client, group, new_ranges, port):
added = 0
removed = 0
if len(group['IpPermissions']) > 0:
for permission in group['IpPermissions']:
if permission['FromPort'] <= port and permission['ToPort'] >= port:
old_prefixes = list()
to_revoke = list()
to_add = list()
for range in permission['IpRanges']:
cidr = range['CidrIp']
old_prefixes.append(cidr)
if new_ranges.count(cidr) == 0:
to_revoke.append(range)
print(group['GroupId'] + ": Revoking " + cidr + ":" + str(permission['ToPort']))
for range in new_ranges:
if old_prefixes.count(range) == 0:
to_add.append({ 'CidrIp': range })
print(group['GroupId'] + ": Adding " + range + ":" + str(permission['ToPort']))
removed += revoke_permissions(client, group, permission, to_revoke)
added += add_permissions(client, group, permission, to_add)
else:
to_add = list()
for range in new_ranges:
to_add.append({ 'CidrIp': range })
print(group['GroupId'] + ": Adding " + range + ":" + str(port))
permission = { 'ToPort': port, 'FromPort': port, 'IpProtocol': 'tcp'}
added += add_permissions(client, group, permission, to_add)
print(group['GroupId'] + ": Added " + str(added) + ", Revoked " + str(removed))
return (added > 0 or removed > 0)
def revoke_permissions(client, group, permission, to_revoke):
if len(to_revoke) > 0:
revoke_params = {
'ToPort': permission['ToPort'],
'FromPort': permission['FromPort'],
'IpRanges': to_revoke,
'IpProtocol': permission['IpProtocol']
}
try:
client.revoke_security_group_ingress(GroupId=group['GroupId'], IpPermissions=[revoke_params])
except Exception as e:
print(str(e))
return len(to_revoke)
def add_permissions(client, group, permission, to_add):
if len(to_add) > 0:
add_params = {
'ToPort': permission['ToPort'],
'FromPort': permission['FromPort'],
'IpRanges': to_add,
'IpProtocol': permission['IpProtocol']
}
client.authorize_security_group_ingress(GroupId=group['GroupId'], IpPermissions=[add_params])
return len(to_add)
def get_security_groups_for_update(client, security_group_id):
response = client.describe_security_groups(GroupIds=[security_group_id])
return response['SecurityGroups']
'''
Sample Event From SNS:
{
"Records": [
{
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:EXAMPLE",
"EventSource": "aws:sns",
"Sns": {
"SignatureVersion": "1",
"Timestamp": "1970-01-01T00:00:00.000Z",
"Signature": "EXAMPLE",
"SigningCertUrl": "EXAMPLE",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"Message": "{\"create-time\": \"yyyy-mm-ddThh:mm:ss+00:00\", \"synctoken\": \"0123456789\", \"md5\": \"45be1ba64fe83acb7ef247bccbc45704\", \"url\": \"https://ip-ranges.amazonaws.com/ip-ranges.json\"}",
"Type": "Notification",
"UnsubscribeUrl": "EXAMPLE",
"TopicArn": "arn:aws:sns:EXAMPLE",
"Subject": "TestInvoke"
}
}
]
}
'''