From 72d30c0fc9ea9369d6ebbf1a8e2dbb10e6c24e16 Mon Sep 17 00:00:00 2001 From: Alex Sheverdin Date: Wed, 25 Sep 2024 16:16:37 +0100 Subject: [PATCH 1/4] Check SGX ZeroMQ connection --- web/routes/health.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/web/routes/health.py b/web/routes/health.py index 9ca41aa5..03196350 100644 --- a/web/routes/health.py +++ b/web/routes/health.py @@ -51,11 +51,6 @@ BLUEPRINT_NAME = 'health' -class SGXStatus(Enum): - CONNECTED = 0 - NOT_CONNECTED = 1 - - health_bp = Blueprint(BLUEPRINT_NAME, __name__) @@ -138,26 +133,27 @@ def ima_log_checks(): def sgx_info(): logger.debug(request) sgx = SgxClient(SGX_SERVER_URL, SGX_CERTIFICATES_FOLDER) + status_https = False + status_zmq = False + version = None try: - status = sgx.get_server_status() + if sgx.get_server_status() == 0: + status_https = True version = sgx.get_server_version() except Exception as e: # todo: catch specific error - edit sgx.py logger.info(e) - status = 1 - version = None - sgx_host = urlparse(SGX_SERVER_URL).hostname - tn = telnetlib.Telnet() - zmq_status = 0 + sgx_zmq = SgxClient(SGX_SERVER_URL, SGX_CERTIFICATES_FOLDER, zmq=True) try: - tn.open(sgx_host, ZMQ_PORT, timeout=ZMQ_TIMEOUT) - except Exception as err: - zmq_status = 1 - logger.error(err) - else: - tn.close() + if sgx_zmq.zmq.get_server_status() == 0: + status_zmq = True + if version is None: + version = sgx_zmq.zmq.get_server_version() + except Exception as e: # todo: catch specific error - edit sgx.py + logger.info(e) + res = { - 'status': zmq_status, - 'status_name': SGXStatus(status).name, + 'status_zmq': status_zmq, + 'status_https': status_https, 'sgx_server_url': SGX_SERVER_URL, 'sgx_keyname': g.config.sgx_key_name, 'sgx_wallet_version': version From 8ae6cecc4faa84abaf539449d2d464a76a5c0ade Mon Sep 17 00:00:00 2001 From: Alex Sheverdin Date: Wed, 25 Sep 2024 16:52:37 +0100 Subject: [PATCH 2/4] Update sgx tests --- tests/routes/health_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/routes/health_test.py b/tests/routes/health_test.py index 25a20d31..cb36bb87 100644 --- a/tests/routes/health_test.py +++ b/tests/routes/health_test.py @@ -159,8 +159,8 @@ def test_sgx(skale_bp, skale): assert data == { 'payload': { 'sgx_server_url': SGX_SERVER_URL, - 'status': 0, - 'status_name': 'CONNECTED', + 'status_zmq': True, + 'status_https': True, 'sgx_wallet_version': version, 'sgx_keyname': TEST_SGX_KEYNAME, }, From 257fd15abede10bd703a3d25c95c41292dc7b5bf Mon Sep 17 00:00:00 2001 From: Alex Sheverdin Date: Wed, 25 Sep 2024 18:44:56 +0100 Subject: [PATCH 3/4] Change the order of sgx checks and improve logging --- web/routes/health.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/web/routes/health.py b/web/routes/health.py index 03196350..faa2bbed 100644 --- a/web/routes/health.py +++ b/web/routes/health.py @@ -132,24 +132,24 @@ def ima_log_checks(): @health_bp.route(get_api_url(BLUEPRINT_NAME, 'sgx'), methods=['GET']) def sgx_info(): logger.debug(request) - sgx = SgxClient(SGX_SERVER_URL, SGX_CERTIFICATES_FOLDER) - status_https = False status_zmq = False + status_https = False version = None + sgx = SgxClient(SGX_SERVER_URL, SGX_CERTIFICATES_FOLDER, zmq=True) try: - if sgx.get_server_status() == 0: - status_https = True - version = sgx.get_server_version() - except Exception as e: # todo: catch specific error - edit sgx.py - logger.info(e) - sgx_zmq = SgxClient(SGX_SERVER_URL, SGX_CERTIFICATES_FOLDER, zmq=True) - try: - if sgx_zmq.zmq.get_server_status() == 0: + if sgx.zmq.get_server_status() == 0: status_zmq = True + version = sgx.zmq.get_server_version() + except Exception as err: + logger.error(f'Cannot make SGX ZMQ check {err}') + sgx_https = SgxClient(SGX_SERVER_URL, SGX_CERTIFICATES_FOLDER) + try: + if sgx_https.get_server_status() == 0: + status_https = True if version is None: - version = sgx_zmq.zmq.get_server_version() - except Exception as e: # todo: catch specific error - edit sgx.py - logger.info(e) + version = sgx_https.get_server_version() + except Exception as err: + logger.error(f'Cannot make SGX HTTPS check {err}') res = { 'status_zmq': status_zmq, From 39bd051a9d284b59d9f6fd5164cac7d7fdadfbfd Mon Sep 17 00:00:00 2001 From: Alex Sheverdin Date: Wed, 25 Sep 2024 18:52:56 +0100 Subject: [PATCH 4/4] Remove unused dependencies --- web/routes/health.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/web/routes/health.py b/web/routes/health.py index faa2bbed..f2336747 100644 --- a/web/routes/health.py +++ b/web/routes/health.py @@ -18,8 +18,6 @@ # along with this program. If not, see . import logging -import telnetlib -from enum import Enum from http import HTTPStatus @@ -27,7 +25,6 @@ from sgx import SgxClient -from urllib.parse import urlparse from core.node import get_check_report, get_skale_node_version from core.node import get_current_nodes from core.schains.checks import SChainChecks @@ -38,7 +35,6 @@ from core.schains.ima import get_ima_log_checks from core.schains.external_config import ExternalState from tools.sgx_utils import SGX_CERTIFICATES_FOLDER, SGX_SERVER_URL -from tools.configs import ZMQ_PORT, ZMQ_TIMEOUT from web.models.schain import SChainRecord from web.helper import ( construct_err_response,