From d27fab1ee267947610d7fdf882ad49850ebeba74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20D=C3=A9trez?= Date: Tue, 28 Jun 2022 17:12:42 +0200 Subject: Catch requests' ConnectionError This is a tentative fix for #44: catch exceptions raised by requests if it cannot connect to the log so that the witness can return the expected exit codes. --- sigsum-witness.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/sigsum-witness.py b/sigsum-witness.py index ef63c4c..5cd9f7a 100755 --- a/sigsum-witness.py +++ b/sigsum-witness.py @@ -279,7 +279,10 @@ def store_tree_head(tree_head): f.write(tree_head.text()) def fetch_tree_head_and_verify(log_verification_key): - req = requests.get(g_args.base_url + 'sigsum/v0/get-tree-head-to-cosign') + try: + req = requests.get(g_args.base_url + 'sigsum/v0/get-tree-head-to-cosign') + except requests.ConnectionError as err: + return None, (ERR_TREEHEAD_FETCH, f"ERROR: unable to fetch new tree head: {err}") if req.status_code != 200: return None, (ERR_TREEHEAD_FETCH, "ERROR: unable to fetch new tree head: {}".format(req.status_code)) @@ -293,7 +296,10 @@ def fetch_tree_head_and_verify(log_verification_key): def fetch_consistency_proof(first, second): url = g_args.base_url + 'sigsum/v0/get-consistency-proof/{}/{}'.format(first, second) - req = requests.get(url) + try: + req = requests.get(url) + except requests.ConnectionError as err: + return None, (ERR_TREEHEAD_FETCH, f"ERROR: unable to fetch consistency proof: {err}") if req.status_code != 200: return None, (ERR_CONSISTENCYPROOF_FETCH, "ERROR: unable to fetch consistency proof: {}".format(req.status_code)) @@ -354,7 +360,10 @@ def sign_send_store_tree_head(signing_key, log_key, tree_head): post_data = 'cosignature={}\n'.format(hexlify(signature).decode('ascii')) post_data += 'key_hash={}\n'.format(hash.hexdigest()) - req = requests.post(g_args.base_url + 'sigsum/v0/add-cosignature', post_data) + try: + req = requests.post(g_args.base_url + 'sigsum/v0/add-cosignature', post_data) + except requests.ConnectionError as err: + return (ERR_COSIG_POST, f"ERROR: Unable to post signature to log: {err}") if req.status_code != 200: return (ERR_COSIG_POST, "ERROR: Unable to post signature to log: {} => {}: {}". format(req.url, -- cgit v1.2.3