-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbender.py
450 lines (377 loc) · 16.5 KB
/
bender.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/bin/python
#
"""Implement a simple database representing network policy
This allows the definition of "host groups" and "service templates"
which allow policy statements like "Allow Workstations to access
SMTP on Servers"
While this version uses a CSV file, it should be easily
extensible to use more conventional databases.
"""
import sys, io
import csv as _csv
class host_group:
"""host_group(table_name)
A host_group as a name with a number of servers associated with it.
Because this inspects the format of the database, it makes it easy to
extend what is stored in the database, allowing many foreign key
references for database reconciliation and meta-data.
This requires only a "name" (of the host group) and "member" be defined.
A server may belong to multiple groups. """
def __init__(self, uri, table_name):
"""Define the host group based on the fields in the table_name.
Peeking into the database to get all columns; use the field names
to generate a list of dictionary objects that can be managed.
"""
self._host_groups = [] # empty list of host_group dictionaries
self._host_fields = () # set of field names
self._host_dialect = '' # dialect of CSV file
self.table_name = table_name
engine, table_name = uri.split('://')
# Open, Peek into the CSV, and create DictReader
try:
reader_fd = io.open(table_name, 'rb')
dialect = _csv.Sniffer().sniff(reader_fd.read(1024))
reader_fd.seek(0)
dreader = _csv.DictReader(reader_fd, dialect=dialect)
except:
raise
# check to make sure that "name","member" at least exist
self._host_fields = dreader.fieldnames
self._host_dialect = dreader.dialect
for req_field in ['hg_name', 'hg_member']:
if not req_field in self._host_fields:
print >>sys.stderr, "hostdb needs \"name\" and \"member\" columns"
sys.exit(1)
# Load the CSV into the _host_groups list of dictionaries
for row in dreader:
# assemble the arguments in the right order
self._host_groups.append(row)
def save(self, table_name):
"""Persist (commit) changes to the database indicated"""
fields = self._host_fields
w_fd = io.open(table_name, 'wb')
dw = _csv.DictWriter(w_fd, fields, dialect=self._host_dialect)
dw.writeheader()
for r in self._host_groups:
try:
dw.writerow(r)
except:
print "Writing row", r
raise
w_fd.close()
def add(self, **kwargs):
"""Add a new member to the database, with the field values"""
exists = self.select(**kwargs) # will match partial
for e in exists:
self.delete(e)
if not kwargs in self._host_groups:
self._host_groups.append(kwargs)
def delete(self, d):
"""Delete the member from the database"""
return self._host_groups.remove(d)
def len(self):
"""Return the number of overall members stored"""
return len(self._host_groups)
def select(self, **kwargs):
"""Select a subset of members, selected by the field/value criteria"""
def f(x):
for field in kwargs:
if (x[field]) and (kwargs[field] != x[field]):
return None
return x
return filter(f, self._host_groups)
def __iter__(self):
"""Return an iterator structure for moving through the list
of members"""
return list.__iter__(self._host_groups)
def fields(self):
"""Return the relevant member fields, in order"""
return self._host_fields
#####
class service_template:
"""service_template(table_name)
A service_template is a pattern representing the communications
protocols needed by an application. Only 'name', 'port' and 'protocol'
are required, though additional fields can help increase security."""
def __init__(self, uri, table_name):
"""Define the service_template based on the columns in the
database.
Peeking into the database to get all columns; use the
field names to generate dictionary objects that can be managed."""
self._svc_groups = [] # empty list of host_group dictionaries
self._svc_fields = () # set of field names
self._svc_dialect = '' # dialect of CSV
self.table_name = table_name
engine, table_name = uri.split('://')
# Open, Peek into the CSV, and create DictReader
try:
reader_fd = io.open(table_name, 'rb')
dialect = _csv.Sniffer().sniff(reader_fd.read(1024))
reader_fd.seek(0)
dreader = _csv.DictReader(reader_fd, dialect=dialect)
except:
raise
# check to make sure that "name" and "port" at least exist
self._svc_fields = dreader.fieldnames
self._svc_dialect = dreader.dialect
for req_field in ['st_name', 'st_port']:
if not req_field in self._svc_fields:
print >>sys.stderr, "Server templates need \"name\" and \"member\" columns"
sys.exit(1)
# Load the CSV into the _svc_groups list of dictionaries
for row in dreader:
# assemble the arguments in the right order
self._svc_groups.append(row)
def save(self, table_name):
"""Persist (commit) changes to the database indicated"""
fields = self._svc_fields
w_fd = io.open(table_name, 'wb')
dw = _csv.DictWriter(w_fd, fields, dialect=self._svc_dialect)
dw.writeheader()
for r in self._svc_groups:
dw.writerow(r)
w_fd.close()
def add(self, **kwargs):
"""Add a new service template to the database, with the field values"""
exists = self.select(**kwargs) # will match partial
for e in exists:
self.delete(e)
if not kwargs in self._svc_groups:
self._svc_groups.append(kwargs)
def delete(self, d):
"""Delete the service template line from the database"""
return self._svc_groups.remove(d)
def len(self):
"""Return the number of service lines (not templates) in the database"""
return len(self._svc_groups)
def select(self, **kwargs):
"""Select a subset of services, indicated by the field/value criteria"""
def f(x):
for field in kwargs:
if (x[field]) and (kwargs[field] != x[field]):
return None
return x
return filter(f, self._svc_groups)
def __iter__(self):
"""Return an iterator structure for moving through the
list of services"""
return list.__iter__(self._svc_groups)
def fields(self):
"""Return the relevant member fields, in order"""
return self._svc_fields
#####
class policy_group:
"""policy_group(table_name)
A policy group is a simple database of policy statements that use host
groups and service templates defined in other bender calls.
A policy group expresses
"Source Group accesses Destination Group for Service"
and gives a name to that statement."""
def __init__(self, uri, table_name):
"""Define the policy group based on the fields in the table_name.
Peeking into the database to get all columns; use the field names
to generate a list of dictionary objects that can be managed."""
self._policy_groups = [] # empty list of policy statements
self._policy_fields = () # set of field names
self._policy_dialect = '' # dialect of CSV
self.table_name = table_name
engine, table_name = uri.split('://')
# Open, peek into the CSV and create DictReader
try:
reader_fd = io.open(table_name, 'rb')
dialect = _csv.Sniffer().sniff(reader_fd.read(1024))
reader_fd.seek(0)
dreader = _csv.DictReader(reader_fd, dialect=dialect)
except:
raise
# make sure that name, source, destination, template all exist
self._policy_fields = dreader.fieldnames
self._policy_dialect = dreader.dialect
for req_field in ['p_name', 'p_source', 'p_destination', 'p_template']:
if not req_field in self._policy_fields:
print >>sys.stderr, "Policy groups need ", req_field, \
"not seen in", table_name
sys.exit(1)
# Load the CSV into the _policy_groups list of dictionaries
for row in dreader:
self._policy_groups.append(row)
def save(self, table_name):
"""Persist (commit) changes to the database indicated"""
fields = self._policy_fields
w_fd = io.open(table_name, 'wb')
dw = _csv.DictWriter(w_fd, fields, dialect=self._policy_dialect)
dw.writeheader()
for r in self._policy_groups:
try:
dw.writerow(r)
except:
print "Writing row", r
raise
w_fd.close()
def add(self, **kwargs):
"""Add a new entry to the database, with the field values"""
exists = self.select(**kwargs) # will match partial
for e in exists:
self.delete(e)
if not kwargs in self._policy_groups:
self._policy_groups.append(kwargs)
def delete(self, d):
"""Delete the policy from the database"""
return self._policy_groups.remove(d)
def len(self):
"""Return the number of overall members stored"""
return len(self._policy_groups)
def select(self, **kwargs):
"""Return an array of selected policy groups based on the
arguments passed in"""
def f(x):
for field in kwargs:
if (x[field] != '') and (kwargs[field] != x[field]):
return None
return x
return filter(f, self._policy_groups)
def __iter__(self):
"""Return an iterator structure for moving through the
list of members"""
return list.__iter__(self._policy_groups)
def fields(self):
"""Return the relevant member fields, in order"""
return self._policy_fields
#####
class policy_render:
"""Render policy statements into source/destination/protocol tuples. This
the output of policy statements at a point in time. Comparisons can
then be made to determine if updates to the environment are necessary"""
def __init__(self, uri, table_name):
"""Define the rendered policies in the named database."""
self._sdp_groups = [] # empty list of sdp dictionaries
self._sdp_fields = () # set of field names
self._sdp_dialect = '' # remember dialect type
self.table_name = table_name
engine, table_name = uri.split('://')
try:
sdp_fd = io.open(table_name, 'rb')
dialect = _csv.Sniffer().sniff(sdp_fd.read(2048), delimiters=',')
sdp_fd.seek(0)
dreader = _csv.DictReader(sdp_fd, dialect=dialect)
except:
raise
# check to make sure that 'source', 'destination' and 'port' at least exist
self._sdp_fields = dreader.fieldnames
self._sdp_dialect = dreader.dialect
for req_field in ['sdp_group', 'sdp_source', 'sdp_destination', 'sdp_source_ip', \
'sdp_destination_ip', 'sdp_bidir', 'sdp_port', 'sdp_protocol']:
if not req_field in self._sdp_fields:
print >>sys.stderr, "SDP needs", req_field, "defined in the database"
sys.exit(1)
# load the CSV into the _sdp_groups list of dictionaries
for row in dreader:
self._sdp_groups.append(row)
def len(self):
"""Return the number of rendered policy lines in the database"""
# if '_sdp_groups' not in locals():
# return 0
return len(self._sdp_groups)
def __iter__(self):
"""Return an iterator structure for moving through the list of members"""
return list.__iter__(self._sdp_groups)
def fields(self):
"""Return the relevant member fields, in order"""
return self._sdp_fields
def zero(self):
"""Reset/clear the rendered policy data"""
while len(self._sdp_groups):
self._sdp_groups.pop()
def save(self, table_name):
"""Persist (commit) rendered policy to the database indicated"""
fields = self._sdp_fields
w_fd = io.open(table_name, 'wb')
dw = _csv.DictWriter(w_fd, fields, dialect=self._sdp_dialect)
dw.writeheader()
for r in self._sdp_groups:
dw.writerow(r)
w_fd.close()
def add(self, **kwargs):
"""Add a new SDP member to the database, with the field values"""
exists = self.select(**kwargs) # will match on all fields
for e in exists:
self.delete(e)
if not kwargs in self._sdp_groups:
self._sdp_groups.append(kwargs)
def delete(self, d):
"""Delete the SDP line from the database"""
return self._sdp_groups.remove(d)
def select(self, **kwargs):
"""Select the SDP sets, indicated by the field/value criteria"""
def f(x):
for field in kwargs:
if (x[field]) and (kwargs[field] != x[field]):
return None
return x
return filter(f, self._sdp_groups)
####################
if __name__ == '__main__':
import socket
# basic test of host_group objects
ho = host_group('testdata/mock-hostdb.csv')
print "Number of host groups", ho.len()
#
sel = ho.select(member='ghidora')
print "Groups referencing ghidora:", len(sel)
for h in sel:
print "\tPolicy:", h['name']
h['owner'] = 'brisco'
#
print "Adding host group item, current len", ho.len()
ho.add(name='workstation', member='ghidora', type='none', \
owner='tomoso', rp='tomoso')
print "Added item to host groups, now len", ho.len()
#
ho.save('testdata/mock-hostdb.csv')
#
# basic test of service template object
so = service_template('testdata/mock-svcdb.csv')
print "Number of service templates", so.len()
# Now read a default policy statement - "forward_mail"
po = policy_group('testdata/mock-poldb.csv')
email_list = po.select(name='forward_email')
email = email_list[0]
print "Policy forward_email: %s can access %s on %s" % (email['source'],\
email['template'], email['destination'])
# Now add a policy statement
print "Adding policy for time service, policy length is", po.len()
po.add(name='sync_time', source='workstation', destination='server', template='time')
print "Added time policy, len now", po.len()
po.save('testdata/mock-poldb.csv')
# now, generate a SDP group for policy "forward_email" -
# "workstations access email on servers"
wkstn = ho.select(name=email['source'])
email_srvrs = ho.select(name=email['destination'])
smtp = so.select(name=email['template'])
sr = policy_render('testdata/mock-sdpdb.csv')
print "Rendered policies", sr.len()
src_dst_list = sr.select(source='ghidora', destination='dracula')
print "Select SDP for source=ghidora, destination=dracula"
for s in src_dst_list:
print "\t%s to %s on port %s/%s (%s)" % (s['source'], s['destination'],\
s['port'], s['protocol'], s['name'])
for w in wkstn:
for e in email_srvrs:
if w['member'] == e['member']:
continue
for s in smtp:
sr_name = w['name']+"_"+e['name']+"_"+s['name']
print "%s,%s,%s,%s/%s" % (sr_name, w['member'], e['member'], \
s['protocol'], s['port'])
# get source/dest IP address
try:
source_ip = socket.gethostbyname(w['member'])
destination_ip = socket.gethostbyname(e['member'])
except:
print "Not valid hostname", w['member'], "or", e['member']
raise
sr.add(group="fake", name=sr_name, source=w['member'],
source_ip=source_ip, destination=e['member'],
destination_ip=destination_ip, port=s['port'], protocol=s['protocol'])
print "Total of", sr.len(), "SDP lines added"
sr.save('testdata/mock-sdpdb.csv')