-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip_list.py
executable file
·406 lines (332 loc) · 13.8 KB
/
ip_list.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
#!/usr/bin/env python3
# coding=utf-8
"""
Version 1.3
Script to manage IP list usages
and to remove allow rule IP pattern
All actions operate on the newest saved or active configuration
and will save a new configuration.
"""
from urllib import request
import ssl
import json
import os
import sys
import re
from argparse import ArgumentParser
from http.cookiejar import CookieJar
import signal
import itertools
from zipfile import ZipFile
from io import BytesIO
import xml.etree.ElementTree as ET
DEFAULT_API_KEY_FILE = "./api_key"
parser = ArgumentParser()
parser.add_argument("-n", dest="host", metavar="hostname",
required=True,
help="Airlock Gateway hostname")
group_action = parser.add_mutually_exclusive_group(required=True)
group_action.add_argument("-a", dest="action", action="store_const",
const="add", help="Add IP list")
group_action.add_argument("-r", dest="action", action="store_const",
const="remove", help="Remove IP list")
group_action.add_argument("-s", dest="action", action="store_const",
const="show", help="Show IP list usage")
group_action.add_argument("-d", dest="action", action="store_const",
const="delete",
help="Delete IP Pattern in allow rule")
group_sel = parser.add_mutually_exclusive_group(required=True)
group_sel.add_argument("-m", dest="mapping_selector_pattern",
metavar="pattern",
help="Pattern matching mapping name, e.g. ^mapping_a$")
group_sel.add_argument("-l", dest="mapping_selector_label", metavar="label",
help="Label for mapping selection")
parser.add_argument("-i", dest="iplist", metavar="pattern",
help="Pattern matching IP list, e.g. ^IP-list99$")
group_type = parser.add_mutually_exclusive_group(required=False)
group_type.add_argument("-w", dest="blacklist", action="store_false",
help="Modify whitelist", default=None)
group_type.add_argument("-b", dest="blacklist", action="store_true",
help="Modify blacklist", default=None)
parser.add_argument("-o", dest="log_only",
choices=['enable', 'disable'],
help="Enable/disable 'log only' on all affected "
"mappings for the specified IP list type")
parser.add_argument("-f", dest="confirm", action="store_false",
help="Force, no confirmation needed")
parser.add_argument("-k", dest="api_key_file", metavar="api_key_file",
default=DEFAULT_API_KEY_FILE,
help="Path to API key file "
f"(default {DEFAULT_API_KEY_FILE})")
parser.add_argument("-t", dest="allow_rule_title",
default="Allow all",
help="New title for the modified allow rule")
args = parser.parse_args()
sys.tracebacklimit = 0
if args.action in ['add', 'remove'] and not (args.iplist or args.log_only):
parser.error("IP list (-i) required for 'add' or 'remove' action"
" if log-only is not specified")
if args.action in ['add', 'remove', 'show'] and args.blacklist is None:
parser.error("blacklist or whitelist (-w / -b) not specified")
TARGET_GATEWAY = f'https://{args.host}'
CONFIG_XML_NAME = "alec_table.xml"
DEFAULT_ALLOW_RULE_IP_PATTERN_ID = "-100"
DEFAULT_ALLOW_RULE_IP_PATTERN_NODE = """
<IpPattern>
<Name>(default) No Restriction</Name>
<Comment>All IP addresses.
The empty pattern matches any string.</Comment>
<Active>false</Active>
<Pattern>
<PatternString></PatternString>
<IgnoreCase>false</IgnoreCase>
<InvertPattern>false</InvertPattern>
<AirlockRegexFormat>true</AirlockRegexFormat>
</Pattern>
<DefaultId>-100</DefaultId>
</IpPattern>
"""
try:
api_key = open(args.api_key_file, 'r').read().strip()
except (IOError, FileNotFoundError) as error:
print(f'Can not read API key from {args.api_key_file}')
sys.exit(-1)
DEFAULT_HEADERS = {"Authorization": f'Bearer {api_key}'}
# we need a cookie store
opener = request.build_opener(request.HTTPCookieProcessor(CookieJar()))
# ignore invalid SSL cert on the management interface
if (not os.environ.get('PYTHONHTTPSVERIFY', '') and
getattr(ssl, '_create_unverified_context', None)):
ssl._create_default_https_context = ssl._create_unverified_context
# method to send REST calls
def send_request(
method,
path,
body="",
accept_header="application/json",
content_type="application/json"):
DEFAULT_HEADERS['Accept'] = accept_header
DEFAULT_HEADERS['Content-Type'] = content_type
if content_type == "application/json":
body = body.encode('utf-8')
req = request.Request(TARGET_GATEWAY + "/airlock/rest/" + path,
body, DEFAULT_HEADERS)
req.get_method = lambda: method
r = opener.open(req)
return r.read()
def terminate_and_exit(text):
send_request("POST", "session/terminate")
sys.exit(text)
# signal handler
def register_cleanup_handler():
def cleanup(signum, frame):
terminate_and_exit("Terminate session")
for sig in (signal.SIGABRT, signal.SIGILL, signal.SIGINT, signal.SIGSEGV,
signal.SIGTERM):
signal.signal(sig, cleanup)
def load_last_config():
resp = json.loads(send_request("GET", "configuration/configurations"))
send_request("POST", 'configuration/configurations/'
f"{resp['data'][0]['id']}/load")
def get_all_mappings():
resp = json.loads(send_request("GET", "configuration/mappings"))['data']
return sorted(resp, key=lambda x: x['attributes']['name'])
def get_mappings():
# filter mappings
return ([
{'id': x['id'],
'name': x['attributes']['name'],
'ip_rules': x['attributes']['ipRules']}
for x in get_all_mappings() if (
args.mapping_selector_pattern and
re.search(args.mapping_selector_pattern,
x['attributes']['name']) or
args.mapping_selector_label in x['attributes']['labels']
)
])
def get_all_ip_lists():
# get all ip lists
resp = json.loads(send_request("GET", "configuration/ip-address-lists"))
return resp['data']
def get_ip_lists():
ip_lists = []
if args.iplist:
ip_lists = ([
{'id': x['id'], 'name': x['attributes']['name']}
for x in get_all_ip_lists() if (
re.search(args.iplist, x['attributes']['name'])
)
])
return ip_lists
def show_usages(mappings, list_type):
dump = {}
for mapping in mappings:
dump[mapping['id']] = {
'name': mapping['name'],
'log_only': mapping['ip_rules']
[f'ipAddress{list_type.title()}']
['logOnly'],
'ip_lists': []
}
resp_al = send_request("GET", "/configuration/ip-address-lists")
for r in json.loads(resp_al)['data']:
key_name = f'ip-address-{list_type}'
if 'relationships' in r and key_name in r['relationships']:
for m in r['relationships'][key_name]['data']:
if m['id'] in dump:
dump[m['id']]['ip_lists'].append(r['id'])
all_ip_lists = get_all_ip_lists()
for mapping_infos in dump.values():
print(mapping_infos['name'])
print(f"\tlog-only: {mapping_infos['log_only']}")
print('\tIP {}: {}'.format(list_type, ', '.join(
i['attributes']['name'] for i in all_ip_lists if
i['id'] in mapping_infos['ip_lists'])))
def patch_log_only(mappings, list_type):
for mapping in mappings:
data = {
"data": {
"attributes": {
"ipRules": {
f"ipAddress{list_type.title()}": {
"logOnly": True if args.log_only == 'enable'
else False
}
}
},
"id": mapping['id'],
"type": "mapping"
}
}
send_request("PATCH", f"configuration/mappings/{mapping['id']}",
json.dumps(data))
def patch_usage(mappings, list_type, ip_lists):
for mapping in mappings:
selected_ip_list = []
for ip_list in ip_lists:
selected_ip_list.append({"id": ip_list['id'],
"type": "ip-address-list"})
data = {"data": selected_ip_list}
method = "PATCH" if args.action == 'add' else "DELETE"
send_request(method,
"configuration/mappings/{}/relationships/ip-address-{}"
.format(mapping['id'], list_type), json.dumps(data))
def create_change_info(affected_mapping_names, list_type, ip_lists):
change_info = ''
if args.action == "add":
change_info += "Add IP list"
elif args.action == "remove":
change_info += "Remove IP list"
elif args.action == "delete":
change_info += "Remove IP pattern in allow rules"
change_info += ' for the following mapping(s): \n\t{}'\
.format('\n\t'.join(affected_mapping_names))
return change_info
def export_mappings(mappings):
mapping_xmls = []
for mapping in mappings:
resp = send_request(method="GET",
path="configuration/mappings/{}/export".format(
mapping['id']),
accept_header="application/zip")
z = ZipFile(BytesIO(resp))
mapping_xml = z.open(CONFIG_XML_NAME).read()
mapping_xmls.append(mapping_xml)
return mapping_xmls
def changeTitle(allowRuleElement, title_index):
name = allowRuleElement.find('Name')
suffix = "" if title_index == 1 else f" {title_index}"
name.text = args.allow_rule_title + suffix
return name.text
def modify_mappings(mapping_xmls):
new_mapping_xmls = []
ip_default_pattern = ET.fromstring(DEFAULT_ALLOW_RULE_IP_PATTERN_NODE)
for mapping_xml in mapping_xmls:
doc = ET.fromstring(mapping_xml)
mapping_name = doc.find('./Mappings/Mapping/Name').text
allow_rules = doc.find('.//AllowRules')
title_index = 1
for allow_rule in allow_rules:
allow_rule_name = allow_rule.find('./Name').text
ip_pattern = allow_rule.find('./IpPattern')
ip_pattern_id = ip_pattern.find('.//DefaultId')
if ip_pattern_id.text != DEFAULT_ALLOW_RULE_IP_PATTERN_ID:
pattern_name = ip_pattern.find('./Name').text
ip_pattern.clear()
ip_pattern.extend(ip_default_pattern)
new_title = changeTitle(allow_rule, title_index)
title_index += 1
print(f"In mapping '{mapping_name}' "
f"allow rule name '{allow_rule_name}' "
f"changed to '{new_title}' "
f"and IP pattern '{pattern_name}' removed")
new_mapping_xmls.append(ET.tostring(doc))
return new_mapping_xmls
def upload_mappings(mapping_xmls):
for mapping_xml in mapping_xmls:
mapping_zip = BytesIO()
with ZipFile(mapping_zip, mode="w") as zf:
zf.writestr(CONFIG_XML_NAME, mapping_xml)
mapping_zip.seek(0)
send_request(method="PUT",
path="configuration/mappings/import",
body=mapping_zip.read(),
content_type="application/zip")
def get_mapping_names(mapping_xmls):
mapping_names = []
for mapping_xml in mapping_xmls:
doc = ET.fromstring(mapping_xml)
mapping_name = doc.find('./Mappings/Mapping/Name').text
mapping_names.append(mapping_name)
return sorted(mapping_names)
def confirm(change_info):
if not args.confirm:
return True
print(change_info)
if input('\nContinue to save the config? [y/n] ') == 'y':
return True
print("Nothing changed")
return False
def save_config(change_info):
data = {"comment": "REST: " + change_info.replace('\n\t', ', ')
.replace(': ,', ':')}
# save config
send_request("POST", "configuration/configurations/save", json.dumps(data))
print(f"\nConfig saved with comment: {data['comment']}")
# create session
send_request("POST", "session/create")
register_cleanup_handler()
# load last config (active or saved)
load_last_config()
# filter mappings
mappings = get_mappings()
if not mappings:
terminate_and_exit("No mapping found - exit")
# filter ip lists
ip_lists = []
if args.iplist:
ip_lists = get_ip_lists()
if not ip_lists:
terminate_and_exit("No IP list found")
list_type = "blacklists" if args.blacklist else "whitelists"
if args.action == "show":
show_usages(mappings, list_type)
else:
if args.log_only:
patch_log_only(mappings, list_type)
if args.action in ['add', 'remove']:
patch_usage(mappings, list_type, ip_lists)
affected_mapping_names = sorted(x['name'] for x in mappings)
if args.action == "delete":
mapping_xmls = export_mappings(mappings)
modified_mapping_xmls = modify_mappings(mapping_xmls)
upload_mappings(modified_mapping_xmls)
affected_mapping_names = get_mapping_names(modified_mapping_xmls)
if (affected_mapping_names):
change_info = create_change_info(affected_mapping_names, list_type,
ip_lists)
confirm(change_info) or terminate_and_exit(0)
save_config(change_info)
else:
print("Nothing to do (no mappings affected)")
terminate_and_exit(0)