Skip to content

Instantly share code, notes, and snippets.

@shrkw
Created April 26, 2013 06:52
Show Gist options
  • Save shrkw/5465421 to your computer and use it in GitHub Desktop.
Save shrkw/5465421 to your computer and use it in GitHub Desktop.
flattening firewall rules
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# sample src
#
# D-U-DENY-1,match,source-address,any
# D-U-DENY-1,match,destination-address,192.0.2.163/32
# D-U-DENY-1,match,destination-address,198.51.100.202/32
# D-U-DENY-1,match,application,any
# D-U-DENY-1,then,deny,
# D-U-1,match,source-address,203.0.113.1/32
# D-U-1,match,destination-address,any
# D-U-1,match,application,DNS
# D-U-1,then,permit,
# D-U-2,match,source-address,203.0.113.1/32
# D-U-2,match,destination-address,any
# D-U-2,match,application,FTP
# D-U-2,then,permit,
sample_data_structure = {
'D-U-21' : [
{'source-address' : 'any', 'destination-address' : '192.0.2.163/32', 'application' : 'HTTP', 'then' : 'permit'},
{'source-address' : 'any', 'destination-address' : '192.0.2.163/32', 'application' : 'DNS', 'then' : 'permit'},
],
'D-U-22' : [
{'source-address' : '192.168.1.1/32', 'destination-address' : '192.0.2.163/32', 'application' : 'HTTP', 'then' : 'permit'},
{'source-address' : '192.168.1.1/32', 'destination-address' : '192.0.2.163/32', 'application' : 'DNS', 'then' : 'permit'},
{'source-address' : '192.168.1.1/32', 'destination-address' : '192.0.2.160/32', 'application' : 'HTTP', 'then' : 'permit'},
{'source-address' : '192.0.2.1/32', 'destination-address' : '192.0.2.160/32', 'application' : 'DNS', 'then' : 'permit'},
],
};
def pp(dic):
# import pprint
# pprint.pprint(res)
header = ['source-address', 'destination-address', 'application', 'then']
print 'id\t', '\t'.join(header)
for k, v_list in sorted(dic.items()):
for i_dic in v_list:
print k, '\t'.join([i_dic.get(i) for i in header])
def main(csv_file):
import csv
import copy
reader = csv.reader(open(csv_file, 'r'))
res = {}
for row in reader:
(id, unused, key, val) = row
if key == 'log':
# ignore 'log'
continue
# normalize format
if key == 'permit' or key == 'deny':
val = key
key = 'then'
if id in res:
tmp_list = res[id]
new_list = []
for tmp_dic in tmp_list:
if key in tmp_dic:
copied = copy.deepcopy(tmp_dic)
copied[key] = val
# list(set(new_list)) では動かない
for i_d in new_list:
if i_d == copied:
break
else:
new_list.append(copied)
else:
tmp_dic[key] = val
if len(new_list) != 0:
tmp_list.extend(new_list)
else:
tmp = {}
tmp[key] = val
res[id] = []
res[id].append(tmp)
pp(res)
if __name__ == '__main__':
import sys
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment