forked from JNPRAutomate/BGP_Flowspec_automation_with_PyEZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
524 lines (383 loc) · 19.4 KB
/
main.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
#
# Copyright (c) 2018 Juniper Networks, Inc.
# All rights reserved.
#
# Use is subject to license terms.
#
# Licensed under the Apache License, Version 2.0 (the ?License?); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at http://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import cherrypy
import hashlib
import datetime
import yaml
import re
from jinja2 import Environment, FileSystemLoader
from jnpr.junos.utils.config import Config
from jnpr.junos import Device
from jnpr.junos.exception import ConfigLoadError, CommitError
from data.fr import FlowRoutesTable, FlowFilterTable
class MyDev(object):
def __init__(self):
self.dev_user = None
self.dev_pw = None
self.age_out_interval = None
self.flow_active = dict()
self.flow_config = dict()
self.filter_active = dict()
self.routers = list()
def addNewFlowRoute(self, flowRouteData=None):
env = Environment(autoescape=False,
loader=FileSystemLoader('./template'), trim_blocks=False, lstrip_blocks=False)
template = env.get_template('set-flow-route.conf')
# print template.render(flowRouteData)
my_router = None
for router in self.routers:
for name, value in router.iteritems():
if 'rr' in value['type']:
my_router = [value['ip']]
with Device(host=my_router[0], user=self.dev_user, password=self.dev_pw) as dev:
try:
cu = Config(dev)
cu.lock()
cu.load(template_path='template/set-flow-route.conf', template_vars=flowRouteData, merge=True)
cu.commit()
cu.unlock()
except ConfigLoadError as cle:
return False, cle.message
self.flow_config[flowRouteData['flowRouteName']] = {
'dstPrefix': flowRouteData['dstPrefix'] if 'dstPrefix' in flowRouteData else None,
'srcPrefix': flowRouteData['srcPrefix'] if 'srcPrefix' in flowRouteData else None,
'protocol': flowRouteData['protocol'] if 'protocol' in flowRouteData else None,
'dstPort': flowRouteData['dstPort'] if 'dstPort' in flowRouteData else None,
'srcPort': flowRouteData['srcPort'] if 'srcPort' in flowRouteData else None,
'action': flowRouteData['action']}
return True, 'Successfully added new flow route'
def modFlowRoute(self, flowRouteData=None):
my_router = None
for router in self.routers:
for name, value in router.iteritems():
if 'rr' in value['type']:
my_router = [value['ip']]
with Device(host=my_router[0], user=self.dev_user, password=self.dev_pw) as dev:
try:
cu = Config(dev)
cu.lock()
cu.load(template_path='template/mod-flow-route.conf', template_vars=flowRouteData)
cu.commit()
cu.unlock()
except CommitError as ce:
return False, ce.message
self.flow_config[flowRouteData['flowRouteName']] = {
'dstPrefix': flowRouteData['dstPrefix'] if 'dstPrefix' in flowRouteData else None,
'srcPrefix': flowRouteData['srcPrefix'] if 'srcPrefix' in flowRouteData else None,
'protocol': flowRouteData['protocol'] if 'protocol' in flowRouteData else None,
'dstPort': flowRouteData['dstPort'] if 'dstPort' in flowRouteData else None,
'srcPort': flowRouteData['srcPort'] if 'srcPort' in flowRouteData else None,
'action': flowRouteData['action']}
return True, 'Successfully modified flow route'
def delFlowRoute(self, flowRouteData=None):
my_router = None
for router in self.routers:
for name, value in router.iteritems():
if 'rr' in value['type']:
my_router = [value['ip']]
with Device(host=my_router[0], user=self.dev_user, password=self.dev_pw) as dev:
try:
cu = Config(dev)
cu.lock()
cu.load(template_path='template/delete-flow-route.conf', template_vars=flowRouteData, merge=True)
cu.commit()
cu.unlock()
except ConfigLoadError as cle:
return False, cle.message
self.flow_config.pop(flowRouteData['flowRouteName'], None)
return True, 'Sucessfully deleted flow route'
def getActiveFlowRoutes(self):
t = datetime.datetime.strptime(self.age_out_interval, "%H:%M:%S")
self.flow_active = dict()
for router in self.routers:
for name, value in router.iteritems():
with Device(host=value['ip'], user=self.dev_user, password=self.dev_pw) as dev:
# data = dev.rpc.get_config(filter_xml='routing-options/flow/route/name')
frt = FlowRoutesTable(dev)
frt.get()
for flow in frt:
destination = flow.destination.split(',')
for index, item in enumerate(destination):
_item = item.split('=')
destination[index] = _item[1] if len(_item) > 1 else _item[0]
hash_object = hashlib.sha512(b'{0}{1}'.format(str(destination), str(value['ip'])))
hex_dig = hash_object.hexdigest()
_age = dict()
if len(flow.age) <= 2:
_age['current'] = datetime.timedelta(seconds=int(flow.age))
elif len(flow.age) == 4 or len(flow.age) == 5:
ms = flow.age.split(':')
_age['current'] = datetime.timedelta(minutes=int(ms[0]), seconds=int(ms[1]))
elif len(flow.age) == 7 or len(flow.age) == 8:
ms = flow.age.split(':')
_age['current'] = datetime.timedelta(hours=int(ms[0]), minutes=int(ms[1]),
seconds=int(ms[2]))
else:
pattern = r'(.*)\s(.*?):(.*?):(.*)'
regex = re.compile(pattern)
age = re.findall(regex, flow.age)
_age['current'] = datetime.timedelta(days=int(age[0][0][:-1]), hours=int(age[0][1]),
minutes=int(age[0][2]), seconds=int(age[0][3]))
pattern = r'([^\s]+)'
regex = re.compile(pattern)
_krt_actions = re.findall(regex, flow.tsi)
if len(_krt_actions) <= 4:
krt_actions = _krt_actions
else:
krt_actions = _krt_actions[4]
# Junos 14.1RX different XPATH for BGP communities
version = dev.facts['version'].split('R')[0].split('.')
if int(version[0]) <= 14 and int(version[1]) <= 1:
if isinstance(flow.action_141, str):
if 'traffic-action' in flow.action_141:
commAction = flow.action_141.split(":")[1].lstrip().strip()
else:
commAction = flow.action_141
elif isinstance(flow.action_141, list):
commAction = flow.action_141[1].split(':')[1].lstrip().strip()
else:
commAction = flow.action_141
else:
if isinstance(flow.action, str):
if 'traffic-action' in flow.action:
commAction = flow.action.split(":")[1].lstrip().strip()
else:
commAction = flow.action
elif isinstance(flow.action, list):
commAction = flow.action[1].split(':')[1].lstrip().strip()
else:
commAction = flow.action
if hex_dig not in self.flow_active:
self.flow_active[hex_dig] = {'router': name, 'term': flow.term, 'destination': destination,
'commAction': commAction, 'krtAction': krt_actions,
'age': str(_age['current']),
'hash': hex_dig, 'status': 'new'}
else:
if 'term:N/A' in flow['term']:
self.flow_active.pop(hex_dig, None)
if _age['current']:
if _age['current'] > datetime.timedelta(hours=t.hour, minutes=t.minute,
seconds=t.second):
self.flow_active[hex_dig]['status'] = 'old'
try:
if hex_dig in self.flow_active:
self.flow_active[hex_dig].update({'term': flow.term, 'destination': destination,
'commAction': commAction,
'krtAction': krt_actions,
'age': str(_age['current'])})
except KeyError as ke:
return False, ke.message
return True, self.flow_active
def getActiveFlowRouteFilter(self):
if self.routers:
for router in self.routers:
for name, value in router.iteritems():
self.filter_active[name] = list()
with Device(host=value['ip'], user=self.dev_user, password=self.dev_pw) as dev:
frft = FlowFilterTable(dev)
frft.get()
for filter in frft:
data = filter.name.split(',')
for didx, item in enumerate(data):
_item = item.split('=')
data[didx] = _item[1] if len(_item) > 1 else _item[0]
self.filter_active[name].append({'data': data, 'packet_count': filter.packet_count,
'byte_count': filter.byte_count})
return True, self.filter_active
def loadFlowRouteConfig(self):
dev_ip = list()
for router in self.routers:
for name, value in router.iteritems():
if 'rr' in value['type']:
dev_ip.append(value['ip'])
with Device(host=dev_ip[0], user=self.dev_user, password=self.dev_pw, normalize=True) as dev:
version = dev.facts['version'].split('R')[0].split('.')
# Junos 14.1RX does not support json so let's go with XML here
if int(version[0]) <= 14 and int(version[1]) <= 1:
data = dev.rpc.get_config(options={'format': 'xml'}, filter_xml='routing-options/flow')
for route in data.iter('route'):
my_list = list()
for item in route:
if 'name' in item.tag:
my_list.append(item.text)
self.flow_config[item.text] = {}
elif 'match' in item.tag:
tag = None
for child in item.iterchildren():
if 'destination-port' in child.tag:
tag = 'dstPort'
elif 'source-port' in child.tag:
tag = 'srcPort'
elif 'destination' in child.tag:
tag = 'dstPrefix'
elif 'source' in child.tag:
tag = 'srcPrefix'
elif 'protocol' in child.tag:
tag = 'protocol'
self.flow_config[my_list[0]][tag] = child.text
elif 'then' in item.tag:
_action = dict()
for child in item.iterchildren():
for value in child.iter():
_action[child.tag] = {'value': value.text}
self.flow_config[my_list[0]]['action'] = _action
return True, self.flow_config
else:
data = dev.rpc.get_config(options={'format': 'json'})
if 'route' in data['configuration']['routing-options']['flow']:
for route in data['configuration']['routing-options']['flow']['route']:
_action = dict()
for key, value in route['then'].iteritems():
if value[0]:
_action[key] = {'value': value}
else:
_action[key] = {'value': None}
self.flow_config[route['name']] = {
'dstPrefix': route['match']['destination'] if 'destination' in route['match'] else None,
'srcPrefix': route['match']['source'] if 'source' in route['match'] else None,
'protocol': route['match']['protocol'] if 'protocol' in route['match'] else None,
'dstPort': route['match']['destination-port'] if 'destination-port' in route[
'match'] else None,
'srcPort': route['match']['source-port'] if 'source-port' in route['match'] else None,
'action': _action}
return True, self.flow_config
else:
return False, self.flow_config
def save_settings(self, dev_user=None, dev_pw=None, routers=None, age_out_interval=None):
self.dev_user = dev_user
self.dev_pw = dev_pw
self.age_out_interval = age_out_interval
# self.routers = routers
# with open('ui/config.yml', 'w') as fp:
# config = {'dev_user': self.dev_user, 'dev_pw': self.dev_pw, 'routers': self.routers,
# 'age_out_interval': self.age_out_interval}
# yaml.safe_dump(config, fp, default_flow_style=False)
def load_settings(self):
with open('ui/config.yml', 'r') as fp:
_config = fp.read()
config = yaml.safe_load(_config)
self.dev_user = config['dev_user']
self.dev_pw = config['dev_pw']
self.age_out_interval = config['age_out_interval']
self.routers = config['routers']
class BGPFlow(object):
@cherrypy.expose
def index(self):
return open('ui/index.html', 'r')
@cherrypy.expose
class BGPFlowWS(object):
def __init__(self, my_dev=None):
self.my_dev = my_dev
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def GET(self, action=None):
if action == 'active':
data = self.my_dev.getActiveFlowRoutes()
return data
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def POST(self, action=None):
if action == 'add':
input_json = cherrypy.request.json
resp = self.my_dev.addNewFlowRoute(flowRouteData=input_json)
return resp
elif action == 'mod':
input_json = cherrypy.request.json
resp = self.my_dev.modFlowRoute(flowRouteData=input_json)
return resp
elif action == 'del':
input_json = cherrypy.request.json
resp = self.my_dev.delFlowRoute(flowRouteData=input_json)
return resp
elif action == 'save':
input_json = cherrypy.request.json
self.my_dev.save_settings(dev_user=input_json['user'], dev_pw=input_json['password'],
age_out_interval=input_json['age_out_interval'])
return True, 'Successfully saved configuration settings'
else:
return False, 'Action not defined'
@cherrypy.expose
class Frt(object):
def __init__(self, my_dev=None):
self.my_dev = my_dev
@cherrypy.tools.json_out()
def POST(self):
resp = self.my_dev.getActiveFlowRoutes()
return resp
@cherrypy.expose
class Frtc(object):
def __init__(self, my_dev=None):
self.my_dev = my_dev
@cherrypy.tools.json_out()
def POST(self):
resp = self.my_dev.loadFlowRouteConfig()
return resp
@cherrypy.expose
class Frft(object):
def __init__(self, my_dev=None):
self.my_dev = my_dev
@cherrypy.tools.json_out()
def POST(self):
resp = self.my_dev.getActiveFlowRouteFilter()
return resp
if __name__ == '__main__':
cherrypy.config.update({'log.screen': False,
'log.access_file': '',
'log.error_file': ''})
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd()),
'tools.staticdir.on': True,
'tools.staticdir.dir': 'ui'
},
'/api': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')],
},
'/api/frt': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')],
},
'/api/frct': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')],
},
'/api/frft': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')],
},
}
my_dev = MyDev()
my_dev.load_settings()
webapp = BGPFlow()
webapp.api = BGPFlowWS(my_dev=my_dev)
webapp.api.frt = Frt(my_dev=my_dev)
webapp.api.frct = Frtc(my_dev=my_dev)
webapp.api.frft = Frft(my_dev=my_dev)
cherrypy.config.update({'log.screen': False,
'server.socket_host': '0.0.0.0',
'server.socket_port': 8080,
})
cherrypy.quickstart(webapp, '/', conf)