From ced00d14cb83c83647cb51eeba15bc5dd794b1c9 Mon Sep 17 00:00:00 2001 From: terrtia Date: Thu, 4 Jul 2024 15:52:20 +0200 Subject: [PATCH] fix: [dashboard] Filter out dashboard event-stream SSL.SSLEOFError from the log if a client disconnects --- bin/lib/ail_logger.py | 4 -- var/www/Flask_server.py | 23 ++++++++-- var/www/modules/dashboard/Flask_dashboard.py | 33 ++++++++------- .../modules/dashboard/templates/index.html | 42 +++++++++++-------- var/www/static/js/indexjavascript.js | 4 ++ 5 files changed, 65 insertions(+), 41 deletions(-) diff --git a/bin/lib/ail_logger.py b/bin/lib/ail_logger.py index 34160582..71eec2d6 100755 --- a/bin/lib/ail_logger.py +++ b/bin/lib/ail_logger.py @@ -27,7 +27,3 @@ def get_config(name=None): config = json.load(f) config['handlers']['file']['filename'] = os.path.join(os.environ['AIL_HOME'], 'logs', name) return config - - -if __name__ == '__main__': - pass diff --git a/var/www/Flask_server.py b/var/www/Flask_server.py index 77506231..593e0f4b 100755 --- a/var/www/Flask_server.py +++ b/var/www/Flask_server.py @@ -81,13 +81,30 @@ log_dir = os.path.join(os.environ['AIL_HOME'], 'logs') if not os.path.isdir(log_dir): os.makedirs(log_dir) -logging.config.dictConfig(ail_logger.get_config(name='flask')) +# ========= LOGS =========# + +class FilterLogErrors(logging.Filter): + def filter(self, record): + # print(dict(record.__dict__)) + if record.levelname == 'ERROR': + if record.msg.startswith('Error on request:'): + if 'ssl.SSLEOFError: EOF occurred in violation of protocol' in record.msg: + return False + return True + + +logging.config.dictConfig(ail_logger.get_config(name='flask')) +flask_logger = logging.getLogger() +ignore_filter = FilterLogErrors() +for handler in flask_logger.handlers: + handler.addFilter(ignore_filter) -# ========= =========# # ========= TLS =========# -ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) + +ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ssl_context.load_cert_chain(certfile=os.path.join(Flask_dir, 'server.crt'), keyfile=os.path.join(Flask_dir, 'server.key')) +ssl_context.suppress_ragged_eofs = True # print(ssl_context.get_ciphers()) # ========= =========# diff --git a/var/www/modules/dashboard/Flask_dashboard.py b/var/www/modules/dashboard/Flask_dashboard.py index 5ee4428c..74b39283 100644 --- a/var/www/modules/dashboard/Flask_dashboard.py +++ b/var/www/modules/dashboard/Flask_dashboard.py @@ -11,7 +11,7 @@ import datetime import time import flask -from flask import Flask, render_template, jsonify, request, Blueprint, url_for +from flask import Flask, render_template, jsonify, request, Blueprint, url_for, stream_with_context from Role_Manager import login_admin, login_analyst, login_read_only from flask_login import login_required @@ -38,33 +38,34 @@ max_dashboard_logs = Flask_config.max_dashboard_logs dashboard = Blueprint('dashboard', __name__, template_folder='templates') # ============ FUNCTIONS ============ - def event_stream(): pubsub = r_serv_log.pubsub() pubsub.psubscribe("Script" + '.*') - for msg in pubsub.listen(): + try: + for msg in pubsub.listen(): + mtype = msg['type'] + pattern = msg['pattern'] + channel = msg['channel'] + data = msg['data'] - # print(msg) - type = msg['type'] - pattern = msg['pattern'] - channel = msg['channel'] - data = msg['data'] + msg = {'channel': channel, 'type': mtype, 'pattern': pattern, 'data': data} - msg = {'channel': channel, 'type': type, 'pattern': pattern, 'data': data} - - level = (msg['channel']).split('.')[1] - if msg['type'] == 'pmessage' and level != "DEBUG": - yield 'data: %s\n\n' % json.dumps(msg) + level = (msg['channel']).split('.')[1] + if msg['type'] == 'pmessage' and level != "DEBUG": + yield 'data: %s\n\n' % json.dumps(msg) + except GeneratorExit: + print("Generator Exited") + pubsub.unsubscribe() def event_stream_dashboard(): try: while True: - # jsonify(row1=get_queues()) data = {'queues': get_queues()} yield f'data: {json.dumps(data)}\n\n' time.sleep(1) except GeneratorExit: - print("Generator Exited") + print("Generator dashboard Exited") + pass def get_queues(): # We may want to put the llen in a pipeline to do only one query. @@ -117,7 +118,7 @@ def logs(): @login_required @login_read_only def _dashboard(): - return flask.Response(event_stream_dashboard(), content_type="text/event-stream") + return flask.Response(stream_with_context(event_stream_dashboard()), content_type="text/event-stream") @dashboard.route("/_get_last_logs_json") @login_required diff --git a/var/www/modules/dashboard/templates/index.html b/var/www/modules/dashboard/templates/index.html index c823236e..9e2f4d86 100644 --- a/var/www/modules/dashboard/templates/index.html +++ b/var/www/modules/dashboard/templates/index.html @@ -18,7 +18,7 @@