-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapp.py
497 lines (410 loc) · 17.2 KB
/
app.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
__version__ = "22.06.26.01"
__author__ = "Oren Brigg & Ramona Renner"
__author_email__ = "obrigg@cisco.com / ramrenne@cisco.com"
__license__ = "Cisco Sample Code License, Version 1.1 - https://developer.cisco.com/site/license/cisco-sample-code-license/"
""" Copyright (c) 2021 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately 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.
"""
# Import Section
from flask import Flask, render_template, request, url_for, redirect, session
from requests.auth import HTTPBasicAuth
import backend
import requests
from time import ctime, sleep, time
from threading import Thread
# Global Variables
app = Flask(__name__)
app.secret_key = "CiscoISEShared"
# Methods
def convert_voucher_list(voucher_list):
"""
This function will receive a voucher list with MAC addresses (in "the cisco way" format)
and expire timestamps (in seconds since the epoch, in UTC) and convert it
to MAC addresses (in the format xx:xx:xx:xx:xx:xx) and expire dates (as strings in local time).
"""
for voucher in voucher_list:
if voucher["type"] == "host":
voucher["mac"] = normalize_mac_format(voucher["mac"])
voucher["ExpDate"] = ctime(voucher["duration"])
elif voucher["type"] == "port":
voucher["ExpDate"] = ctime(voucher["duration"])
return voucher_list
def propagate_backend_exception(backend_response):
"""
This function checks the backend response for ERROR indictors to raise an frontend
exception and thereby display a customized error message in case of an backend error.
"""
if "ERROR" in str(backend_response):
raise Exception(str(backend_response))
def voucher_cleanup_loop():
while True:
backend.voucher_cleanup()
sleep(10 * 60)
def normalize_mac_format(mac_address):
"""
This function normalizes mac addresses of different formats
e.g. xxxxxxxxxxxx, xx-xx-xx-xx-xx-xx, xx.xx.xx.xx.xx.xx or xxxx.xxxx.xxxx
to xx:xx:xx:xx:xx:xx
"""
# Remove all :, - and .
mac_address = mac_address.replace(".", "").replace(":", "").replace("-", "")
# Add : after every second char
normalized_mac_address = ":".join(
a + b for a, b in zip(mac_address[::2], mac_address[1::2])
)
return normalized_mac_address
# Routes
@app.route("/", methods=["GET"])
def index():
"""
This function shows an empty welcome page (GET)
"""
if type(session.get("logged_in")) == int and session.get("logged_in") > int(time()):
session["logged_in"] = int(time()) + backend.timeout
print(
f"Login for {session['username']} extended until: {ctime(session['logged_in'])}"
)
try:
if request.method == "GET":
return render_template("welcome.html")
except Exception as e:
print(e)
return render_template("endpointQuery.html", error=True, errorcode=e)
else:
print("First you need to login")
return redirect(url_for("login"))
@app.route("/deviceList", methods=["GET", "POST"])
def deviceList():
"""
This function will show all NAD devices in a table (GET request) and
redirect to the specific query page of a device (POST request). The redirection
can be manually triggered by a "Query Device" link in each table row and thereby
for each NAD device.
"""
if type(session.get("logged_in")) == int and session.get("logged_in") > int(time()):
session["logged_in"] = int(time()) + backend.timeout
print(
f"Login for {session['username']} extended until: {ctime(session['logged_in'])}"
)
try:
if request.method == "GET":
device_list = backend.get_all_NADs()
propagate_backend_exception(device_list)
return render_template("deviceList.html", device_list=device_list)
except Exception as e:
print(e)
return render_template(
"deviceList.html", error=True, errorcode=e, reloadlink="/"
)
else:
print("First you need to login")
return redirect(url_for("login"))
app.route("/deviceQuery?ip_address=<ip_address>")
@app.route("/deviceQuery", methods=["GET", "POST"])
def deviceQuery():
"""
This function shows an empty page with IP query field and button (GET)
or a list of queried devices with associated session information (POST).
"""
if type(session.get("logged_in")) == int and session.get("logged_in") > int(time()):
session["logged_in"] = int(time()) + backend.timeout
print(
f"Login for {session['username']} extended until: {ctime(session['logged_in'])}"
)
try:
if request.method == "GET":
ip_address = request.args.get("ip_address")
elif request.method == "POST":
ip_address = request.form.get("ip_address")
if ip_address == None:
ip_address = ""
relevant_sessions = {}
else:
relevant_sessions = backend.get_device_auth_sessions(ip_address)
propagate_backend_exception(relevant_sessions)
return render_template(
"deviceQuery.html",
ip_address=ip_address,
relevant_sessions=relevant_sessions,
)
except Exception as e:
print(e)
return render_template("deviceQuery.html", error=True, errorcode=e)
else:
print("First you need to login")
return redirect(url_for("login"))
app.route("/switchView?ip_address=<ip_address>&success=<success>")
app.route("/switchView?nad_id=<nad_id>&success=<success>")
@app.route("/switchView", methods=["GET", "POST"])
def switchView():
"""
This function shows an graphical representation of a switch and its port status.
"""
if type(session.get("logged_in")) == int and session.get("logged_in") > int(time()):
session["logged_in"] = int(time()) + backend.timeout
print(
f"Login for {session['username']} extended until: {ctime(session['logged_in'])}"
)
try:
if request.method == "GET":
nad_id = request.args.get("nad_id")
if nad_id == None:
ip_address = request.args.get("ip_address")
else:
ip_address = backend.get_nad_ip(nad_id)
print(f"IP: {ip_address}")
elif request.method == "POST":
ip_address = request.form.get("ip_address")
success = request.args.get("success")
if ip_address == None:
ip_address = ""
detailed_switch_status = {}
else:
detailed_switch_status = backend.get_device_ports(ip_address)
propagate_backend_exception(detailed_switch_status)
return render_template(
"switchView.html",
ip_address=ip_address,
success=success,
detailed_switch_status=detailed_switch_status,
)
except Exception as e:
print(e)
return render_template("switchView.html", error=True, errorcode=e)
else:
print("First you need to login")
return redirect(url_for("login"))
app.route("/portAction?ip_address=<ip_address>&interface=<interface>&action=<action>")
@app.route("/portAction", methods=["GET", "POST"])
def portAction():
"""
This function handles the port actions: clear port sessions and bypass.
"""
if type(session.get("logged_in")) == int and session.get("logged_in") > int(time()):
session["logged_in"] = int(time()) + backend.timeout
print(
f"Login for {session['username']} extended until: {ctime(session['logged_in'])}"
)
try:
ip_address = request.args.get("ip_address")
interface = request.args.get("interface")
action = request.args.get("action")
if action == "clear":
response = backend.clear_port_auth_sessions(ip_address, interface)
propagate_backend_exception(response)
elif action == "bypass":
response = backend.add_port_voucher(
ip_address, interface, 24, session["username"]
)
propagate_backend_exception(response)
return redirect("/switchView?ip_address=" + ip_address + "&success=True")
except Exception as e:
print(e)
return render_template("switchView.html", error=True, errorcode=e)
else:
print("First you need to login")
return redirect(url_for("login"))
app.route("/switchViewDetail?ip_address=<ip_address>&interface=<interface>")
@app.route("/switchViewDetail", methods=["GET", "POST"])
def switchViewDetail():
"""
This function shows the associated session information for a specific interface and device.
"""
if type(session.get("logged_in")) == int and session.get("logged_in") > int(time()):
session["logged_in"] = int(time()) + backend.timeout
print(
f"Login for {session['username']} extended until: {ctime(session['logged_in'])}"
)
ip_address = ""
interface = ""
try:
if request.method == "GET":
ip_address = request.args.get("ip_address")
interface = request.args.get("interface")
relevant_sessions = backend.get_port_auth_sessions(
ip_address, interface
)
propagate_backend_exception(relevant_sessions)
return render_template(
"switchViewDetail.html",
ip_address=ip_address,
interface=interface,
relevant_sessions=relevant_sessions,
)
except Exception as e:
print(e)
if "has no authentication sessions" in repr(e):
return render_template(
"switchViewDetail.html",
error=False,
ip_address=ip_address,
interface=interface,
relevant_sessions={},
)
elif "No access sessions on" in repr(e):
return render_template(
"switchViewDetail.html",
error=False,
no_access_session=True,
ip_address=ip_address,
interface=interface,
relevant_sessions={},
)
return render_template("switchViewDetail.html", error=True, errorcode=e)
else:
print("First you need to login")
return redirect(url_for("login"))
@app.route("/voucher", methods=["GET", "POST"])
def voucher():
"""
This function shows a list of vouchers (GET), allows to add new vouchers
or revokes existing once (POST). For better user experience the voucher data
is converted beforehand.
"""
if type(session.get("logged_in")) == int and session.get("logged_in") > int(time()):
session["logged_in"] = int(time()) + backend.timeout
print(
f"Login for {session['username']} extended until: {ctime(session['logged_in'])}"
)
try:
if request.method == "GET":
voucher_list = convert_voucher_list(backend.read_voucher_list())
propagate_backend_exception(voucher_list)
return render_template(
"voucher.html", voucher_list=voucher_list, new_voucher=False
)
elif request.method == "POST":
new_voucher = False
deleted_voucher = False
submit_type = request.form.get(
"voucher_sumbit"
) # Possible values: add, revoke_host, revoke_port
# Add
if submit_type == "add":
form_mac_address = normalize_mac_format(
request.form.get("mac_address_field")
)
voucher_duration = request.form.get("voucher_duration")
voucher_group = request.form.get("voucher_group")
response = backend.add_host_voucher(
form_mac_address,
int(voucher_duration),
voucher_group,
session["username"],
)
new_voucher = True
# Revoke
elif submit_type == "revoke_host":
host_mac = request.form.get("host_mac")
response = backend.revoke_host_voucher(
host_mac, session["username"]
)
deleted_voucher = True
elif submit_type == "revoke_port":
port_switch_ip = request.form.get("port_switch_ip")
port_interface = request.form.get("port_interface")
port_command = request.form.get("port_command")
response = backend.revoke_port_voucher(
port_switch_ip,
port_interface,
port_command,
session["username"],
)
deleted_voucher = True
propagate_backend_exception(response)
voucher_list = convert_voucher_list(backend.read_voucher_list())
propagate_backend_exception(voucher_list)
return render_template(
"voucher.html",
voucher_list=voucher_list,
new_voucher=new_voucher,
deleted_voucher=deleted_voucher,
)
except Exception as e:
print(e)
return render_template("voucher.html", error=True, errorcode=e)
else:
print("First you need to login")
return redirect(url_for("login"))
@app.route("/endpointQuery?mac_address=<mac_address>")
@app.route("/endpointQuery", methods=["GET", "POST"])
def endpointQuery():
"""
This function shows an empty page with MAC address query field and button (GET)
or a list of queried endpoint devices with associated auth status (POST).
"""
if type(session.get("logged_in")) == int and session.get("logged_in") > int(time()):
session["logged_in"] = int(time()) + backend.timeout
print(
f"Login for {session['username']} extended until: {ctime(session['logged_in'])}"
)
mac_address = ""
endpoint_list = ""
try:
if request.method == "GET":
mac_address = request.args.get("mac_address")
elif request.method == "POST":
mac_address = request.form.get("mac_address")
if mac_address == None:
mac_address = ""
endpoint_list = {}
else:
mac_address = normalize_mac_format(mac_address)
endpoint_list = backend.check_ise_auth_status(mac_address)
propagate_backend_exception(endpoint_list)
return render_template(
"endpointQuery.html",
endpoint_list=endpoint_list,
mac_address=mac_address,
)
except Exception as e:
print(e)
if "No authentication events for MAC Address" in repr(
e
) and "during the last 24.0 hours." in repr(e):
return render_template(
"endpointQuery.html",
error=False,
no_events=True,
endpoint_list={},
mac_address=mac_address,
)
return render_template("endpointQuery.html", error=True, errorcode=e)
else:
print("First you need to login")
return redirect(url_for("login"))
@app.route("/login", methods=["GET", "POST"])
def login():
# Reset an existing sessio
session["logged_in"] = 0
"""Login Form"""
if request.method == "GET":
return render_template("login.html")
else:
name = request.form["username"]
passw = request.form["password"]
x = backend.initialize_ise(name, passw)
if x == "Done":
print("login view - Successful Login")
session["logged_in"] = int(time()) + backend.timeout
session["username"] = name
print(f"Logged in until: {ctime(session['logged_in'])}")
return redirect(url_for("index"))
else:
print("Failed login")
return render_template("login.html")
# Main Function
if __name__ == "__main__":
t1 = Thread(target=voucher_cleanup_loop)
t1.start()
sleep(1)
app.run(host="0.0.0.0", debug=False, threaded=True)