-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbender_ui.py
More file actions
239 lines (214 loc) · 9.06 KB
/
Copy pathbender_ui.py
File metadata and controls
239 lines (214 loc) · 9.06 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
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
#!/usr/bin/python
"""Interface to a simple policy database representing network policy
This allows owners to update the own "host groups" and "service templates"
which allow policy statements.
While this version uses a CSV files, it should be easily
extensible to use more conventional databases.
"""
import bender_obj as bender
import socket
from flask import Flask, request, url_for, render_template, redirect
b_ui = Flask(__name__, static_url_path='/static')
# Load the demo databases from mock data
hg = bender.host_group('csv://mockdata/mock-hostdb.csv','hostgroups')
sg = bender.service_template('csv://mockdata/mock-svcdb.csv','service_templates')
pg = bender.policy_group('csv://mockdata/mock-poldb.csv', 'policy')
sdp = bender.policy_render('csv://mockdata/mock-sdpdb.csv', 'sdp')
# gethostaddr - similar to socket.gethostbyname() - but use getaddrinfo() to deal
# with IPv6 addresses
def gethostaddr(name):
# a _very_ bad way to if name is an IP address or IP network (v4 or v6)
s = name
if s.strip('0123456789/.:abcdefABCDEF') == '':
return name # _probably_ an IPv4 or IPv6 address or network
try:
h_infos = socket.getaddrinfo(name,None,0,0,socket.SOL_TCP)
except socket.gaierror as e:
e_msg = stocket.gai_strerror(e)
print "bender.gethostaddr(name):", e_msg
errors = errors + errors_nl + e_msg
errors_nl = '\r\n'
raise e
# go for the first item returned in the array
# print "Name",name,"address",h_infos[0][4][0]
return h_infos[0][4][0]
@b_ui.route('/index')
@b_ui.route('/')
def index_hostgroups():
r_info = []
q_info = []
p_info = []
sdp_info = []
# sort when displaying, so members of the same group appear next to each other
for h in sorted(hg, key=lambda k: k['hg_name']):
r_info.append(h.copy())
for s in sorted(sg, key=lambda k: k['st_name']):
q_info.append(s.copy())
for p in sorted(pg, key=lambda k: k['p_name']):
p_info.append(p.copy())
for sd in sorted(sdp, key=lambda k: k['sdp_group']):
sdp_info.append(sd.copy())
return render_template('bender_index.html',
groupinfo=r_info, svcinfo=q_info,
polinfo=p_info, sdpinfo=sdp_info)
#####################################################
# Group management
#####################################################
@b_ui.route('/delgroup', methods=['POST'])
def delete_group():
g = request.form['group']
if not g:
print "Web: *** delete_group - no group specified:", g
return redirect(url_for('index_hostgroups'))
print "Web: delete group ", g
dg = hg.select(hg_name=g)
for d in dg:
hg.delete(d)
return redirect(url_for('index_hostgroups'))
@b_ui.route('/delmember', methods=['POST'])
def delete_member():
m = request.form['member']
g = request.form['group']
if not m or not g:
print "Web: *** delete_member - no member specified:", m, "group:",g
return redirect(url_for('index_hostgroups'))
dm = hg.select(hg_name=g, hg_member=m)
print "Web: delete member ", m, "returned", dm
for m in dm:
hg.delete(m)
return redirect(url_for('index_hostgroups'))
@b_ui.route('/addgroup', methods=['POST'])
def add_group():
m = request.form['groupmember']
g = request.form['groupname']
t = request.form['grouptype']
o = request.form['groupowner']
r = request.form['grouprp']
if not m or not g:
print "Web: *** add_group - no member or group specified:", g
return redirect(url_for('index_hostgroups'))
print "Web: add member", m, "to group", g
hg.add(hg_name=g, hg_member=m, hg_type=t, hg_owner=o, hg_rp=r)
return redirect(url_for('index_hostgroups')+"#groups")
@b_ui.route('/savegroup', methods=['POST'])
def save_group():
hg.save("mockdata/mock-hostdb.csv")
return redirect(url_for('index_hostgroups')+"#groups")
#####################################################
# service management
#####################################################
@b_ui.route('/deletesvc', methods=['POST'])
def delete_service():
sname = request.form['name']
print "Web: *** delete_service", sname
sl = sg.select(st_name=sname)
for s in sl:
sg.delete(s)
return redirect(url_for('index_hostgroups')+"#services")
@b_ui.route('/deletesvcline', methods=['POST'])
def delete_service_line():
lname = request.form['name']
print "Web: *** delete_service line", lname
# sl = sg.select(st_name=sname)
sl = sg.select(st_name=request.form['name'],
st_port=request.form['port'],
st_protocol=request.form['protocol'],
st_transport=request.form['transport'],
st_bidir=request.form['bidir'],
st_owner=request.form['owner'],
st_rp=request.form['rp'])
for s in sl:
sg.delete(s)
return redirect(url_for('index_hostgroups')+"#services")
@b_ui.route('/addservice', methods=['POST'])
def add_service():
name = request.form['name']
port = request.form['port']
protocol = request.form['protocol']
transport = request.form['transport']
bidir = request.form['bidir']
owner = request.form['owner']
rp = request.form['rp']
sg.add(st_name=name, st_port=port, st_protocol=protocol, st_bidir=bidir,
st_transport=transport, st_owner=owner, st_rp=rp)
return redirect(url_for('index_hostgroups')+"#services")
@b_ui.route('/saveservice', methods=['POST'])
def save_service():
sg.save("mockdata/mock-svcdb.csv")
return redirect(url_for('index_hostgroups')+"#services")
#####################################################
# Policy management
#####################################################
@b_ui.route('/delpolicyline', methods=['POST'])
def delete_policy_line():
name = request.form['name']
source = request.form['source']
destination = request.form['destination']
template = request.form['template']
dpol = pg.select(p_name=name, p_source=source,
p_destination=destination, p_template=template)
for d in dpol:
pg.delete(d)
return redirect(url_for('index_hostgroups')+"#policies")
@b_ui.route('/delpolicy', methods=['POST'])
def delete_policy():
name = request.form['name']
dpol = pg.select(p_name=name)
for d in dpol:
pg.delete(d)
return redirect(url_for('index_hostgroups')+"#policies")
@b_ui.route('/addpolicy', methods=['POST'])
def add_policy():
name = request.form['name']
source = request.form['source']
destination = request.form['destination']
template = request.form['template']
pg.add(p_name=name,
p_source=source,
p_destination=destination,
p_template=template)
return redirect(url_for('index_hostgroups')+"#policies")
@b_ui.route('/savepolicy', methods=['POST'])
def save_policy():
pg.save("mockdata/mock-poldb.csv")
return redirect(url_for('index_hostgroups')+"#policies")
#####################################################
# Rendering functions
#####################################################
@b_ui.route('/rendersdp', methods=['POST'])
def render_sdp():
# Generate all policies
# for all policies
# for all hosts in the policy source host group
# for all hosts in the policy destination host group
# for all services in the service template
# save the source,destination,port information
for p in pg:
for src in hg.select(hg_name=p['p_source']):
for dst in hg.select(hg_name=p['p_destination']):
for svc in sg.select(st_name=p['p_template']):
name = "%s_%s_%s" % (src['hg_name'], dst['hg_name'], svc['st_name'])
try:
source_ip = gethostaddr(src['hg_member'])
destination_ip = gethostaddr(dst['hg_member'])
except:
print "Error looking up", src['hg_member'], "or", dst['hg_member']
continue # just skip it?
if src['hg_member'] != dst['hg_member']:
# print "\tSDP Add:", src['hg_member'], "and", dst['hg_member'], "for", svc['st__name']
sdp.add(sdp_group=p['p_name'], sdp_name=name,
sdp_source=src['hg_member'], sdp_source_ip=source_ip,
sdp_destination=dst['hg_member'],
sdp_destination_ip=destination_ip,
sdp_bidir=svc['st_bidir'], sdp_port=svc['st_port'],
sdp_protocol=svc['st_protocol'])
sdp.save('mockdata/mock-sdpdb.csv')
return redirect(url_for('index_hostgroups')+"#renderedpolicies")
@b_ui.route('/resetsdp', methods=['POST'])
def reset_sdp():
# just erase the whole thing - usually done prior to a full recompute
sdp.zero()
sdp.save('mockdata/mock-sdpdb.csv')
return redirect(url_for('index_hostgroups')+"#renderedpolicies")
if __name__ == '__main__':
b_ui.run(debug=True, use_reloader=False, host='0.0.0.0', port=5010)