-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.py
208 lines (174 loc) · 6.21 KB
/
github.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
from botocore.vendored import requests
import os
import boto3
import json
def lambda_handler(event, context):
'''
Main Lambda Handler
'''
# Environment variables
aws_region = os.environ["AWS_REGION"]
sg_tag_name = os.environ["sg_tag_name"]
sg_tag_value = os.environ["sg_tag_value"]
api_github_endpoint = os.environ["api_github_endpoint"]
# Security group filters
sg_filters = {
"Name": f"tag:{sg_tag_name}",
"Values": [f"{sg_tag_value}"]
}
# Security group ports to whitelist ips
sg_ports = {
"FromPort": [80, 443],
"ToPort": [80, 443]
}
# Get github ips
try:
github_response = requests.get(f"{api_github_endpoint}").json()
list_of_ips = github_ips(github_response)
if not list_of_ips:
raise Exception(f"List of github ips is empty for endpoint: {api_github_endpoint}. Exiting...")
except Exception as error:
raise Exception(error)
# Read AWS accounts configs
try:
json_accounts_config = json.loads(open("config.json").read())
except Exception as error:
raise Exception(error)
# Update sg in each account
for account in json_accounts_config["Accounts"]:
assume_role_arn=account["assume_role_arn"]
try:
client = assume_role(assume_role_arn, "ec2")
print(f"Succesfully assumed role: {assume_role_arn}.")
except Exception as error:
print(f"Couldn't assume role: {assume_role_arn}.")
print(error)
continue
try:
sg_ids_list = get_sg_ids_by_tag(sg_filters, client)
except Exception as error:
raise Exception(error)
for sg_id in sg_ids_list:
try:
update_sg(list_of_ips, sg_id, sg_ports, client)
except Exception as error:
raise Exception(error)
print("")
return ("Finished updating security groups!")
def assume_role(role_arn, client_type):
sts_client = boto3.client("sts")
assumedRoleObject = sts_client.assume_role(
DurationSeconds=3600,
RoleArn=role_arn,
RoleSessionName="AssumeRoleSessionName"
)
credentials = assumedRoleObject["Credentials"]
client = boto3.client(
client_type,
aws_access_key_id = credentials["AccessKeyId"],
aws_secret_access_key = credentials["SecretAccessKey"],
aws_session_token = credentials["SessionToken"]
)
return client
def github_ips(json_data):
ips = []
for iplist in json_data:
if isinstance(json_data[iplist], list):
ips+=json_data[iplist]
return set(ips)
def check_cidr_validity(netaddress):
try:
ip, mask = netaddress.split("/")
return netaddress
except ValueError:
return (netaddress + "/32")
def get_sg_ids_by_tag(filters, client):
response = client.describe_security_groups(
Filters=[filters]
)
sg_ids = []
# Getting security group ids for provided filters
for sg in response["SecurityGroups"]:
sg_ids.append(sg["GroupId"])
return sg_ids
def update_sg(all_ips, sg_id, sg_ports, client):
'''
This function determines what IPs need to be added/removed from the
passed in security group
'''
# getting all the rules for the passed in security group
response = client.describe_security_groups(
Filters=[{
"Name": "group-id",
"Values": [sg_id]
}]
)
permissions = response["SecurityGroups"][0]["IpPermissions"]
add_permissions = []
remove_permissions = []
sg_ips_data = []
sg_ips_list = []
for permission in permissions:
if permission["IpRanges"]:
sg_ips_data.append({
"FromPort": permission["FromPort"],
"ToPort": permission["ToPort"],
"IpRanges": permission["IpRanges"],
"IpProtocol": permission["IpProtocol"]
})
for sg_ip in permission["IpRanges"]:
sg_ips_list.append({
"FromPort": permission["FromPort"],
"ToPort": permission["ToPort"],
"IpRanges": [{"CidrIp":sg_ip["CidrIp"]}],
"IpProtocol": permission["IpProtocol"]
})
for ip_entry in sg_ips_data:
for cidr in ip_entry["IpRanges"]:
ip_permission_deny = {
"FromPort": ip_entry["FromPort"],
"ToPort": ip_entry["ToPort"],
"IpRanges": [{"CidrIp":cidr["CidrIp"]}],
"IpProtocol": ip_entry["IpProtocol"]
}
if (cidr["CidrIp"] not in all_ips):
remove_permissions.append(ip_permission_deny)
elif (ip_entry["FromPort"] not in sg_ports["FromPort"] and
ip_entry["ToPort"] not in sg_ports["ToPort"]):
remove_permissions.append(ip_permission_deny)
for ip in all_ips:
for i in range(0, len(sg_ports["FromPort"])):
ip_permission_entry = {
"FromPort": sg_ports["FromPort"][i],
"ToPort": sg_ports["ToPort"][i],
"IpRanges": [{"CidrIp":ip}],
"IpProtocol": "tcp"
}
if ip_permission_entry not in sg_ips_list:
add_permissions.append(ip_permission_entry)
if remove_permissions:
print("Removing permissions from SG: %s." % (sg_id))
remove_sg_permissions(sg_id, remove_permissions, client)
else:
print("No rules to remove from SG: %s." % (sg_id))
if add_permissions:
print("Adding permissions to SG: %s." % (sg_id))
add_sg_permissions(sg_id, add_permissions, client)
else:
print("No rules to add to SG: %s." % (sg_id))
def add_sg_permissions(sg_id, permissions, client):
try:
client.authorize_security_group_ingress(
GroupId=sg_id,
IpPermissions=permissions
)
except Exception as error:
print(error)
def remove_sg_permissions(security_group, permissions, client):
try:
client.revoke_security_group_ingress(
GroupId=security_group,
IpPermissions=permissions
)
except Exception as error:
print(error)