From 5bda63fa057059b0271c854e404bf664843bb04a Mon Sep 17 00:00:00 2001 From: Michael Albert Date: Sat, 19 Mar 2022 11:03:20 +0100 Subject: [PATCH] Add config for verification of ssl certificates --- conf/example_config.yaml | 2 ++ .../slack-matrix-migration/files.py | 2 +- .../slack-matrix-migration/migrate.py | 12 ++++++------ .../slack-matrix-migration/utils.py | 6 +++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/conf/example_config.yaml b/conf/example_config.yaml index 64b8ecc..4987c1e 100644 --- a/conf/example_config.yaml +++ b/conf/example_config.yaml @@ -4,6 +4,8 @@ homeserver: https://example.org domain: example.org # Application Service Token from the matrix Homeserver as_token: very-secret +# Verify https certificate (True) or ignore 'unsafe' SSL certificates (False) +verify-ssl: True # Don't download files from Slack and upload them to Matrix skip-files: False # Path to the Slack Backup relative to the current directory or absolute diff --git a/slack-matrix-migration/slack-matrix-migration/files.py b/slack-matrix-migration/slack-matrix-migration/files.py index 1e0abb5..122447c 100644 --- a/slack-matrix-migration/slack-matrix-migration/files.py +++ b/slack-matrix-migration/slack-matrix-migration/files.py @@ -186,7 +186,7 @@ def uploadContentFromURI(content, uri, config, user): url = "%s/_matrix/media/r0/upload?user_id=%s&filename=%s" % (config["homeserver"],user,content["title"],) try: - r = requests.post(url, headers={'Authorization': 'Bearer ' + config["as_token"], 'Content-Type': content["mimetype"]}, data=file_content, verify=False) + r = requests.post(url, headers={'Authorization': 'Bearer ' + config["as_token"], 'Content-Type': content["mimetype"]}, data=file_content, verify=config["verify-ssl"]) except requests.exceptions.RequestException as e: # catastrophic error. bail. log.error( diff --git a/slack-matrix-migration/slack-matrix-migration/migrate.py b/slack-matrix-migration/slack-matrix-migration/migrate.py index cb36a54..963baea 100644 --- a/slack-matrix-migration/slack-matrix-migration/migrate.py +++ b/slack-matrix-migration/slack-matrix-migration/migrate.py @@ -198,7 +198,7 @@ def login(server_location): } # Get the access token - r = requests.post(url, json=data, verify=False) + r = requests.post(url, json=data, verify=config["verify-ssl"]) if r.status_code != 200: log.info("ERROR! Received %d %s" % (r.status_code, r.reason)) @@ -216,7 +216,7 @@ def login(server_location): def getMaxUploadSize(config, access_token): # get maxUploadSize from Homeserver url = "%s/_matrix/media/r0/config?access_token=%s" % (config_yaml["homeserver"],access_token,) - r = requests.get(url, verify=False) + r = requests.get(url, verify=config["verify-ssl"]) if r.status_code != 200: log.info("ERROR! Received %d %s" % (r.status_code, r.reason)) @@ -249,7 +249,7 @@ def register_user( "admin": admin, } try: - r = requests.put(url, json=data, headers=headers, verify=False) + r = requests.put(url, json=data, headers=headers, verify=config["verify-ssl"]) except requests.exceptions.RequestException as e: # catastrophic error. bail. log.error( @@ -305,7 +305,7 @@ def register_room( #_log.info("Sending registration request...") try: - r = requests.post(url, headers={'Authorization': 'Bearer ' + as_token}, json=body, verify=False, timeout=300 ) + r = requests.post(url, headers={'Authorization': 'Bearer ' + as_token}, json=body, verify=config["verify-ssl"], timeout=300 ) # except requests.exceptions.Timeout: # # Maybe set up for a retry, or continue in a retry loop # except requests.exceptions.TooManyRedirects: @@ -350,7 +350,7 @@ def autojoin_users( #_log.info("Sending registration request...") try: - r = requests.post(url, headers={'Authorization': 'Bearer ' + config["as_token"]}, verify=False) + r = requests.post(url, headers={'Authorization': 'Bearer ' + config["as_token"]}, verify=config["verify-ssl"]) except requests.exceptions.RequestException as e: log.error( "Logging an uncaught exception {}".format(e), @@ -840,7 +840,7 @@ def kick_imported_users(server_location, admin_user, access_token, tick): data = {"user_id": name} try: - r = requests.post(url, json=data, headers=headers, verify=False) + r = requests.post(url, json=data, headers=headers, verify=config["verify-ssl"]) except requests.exceptions.RequestException as e: # catastrophic error. bail. log.error( diff --git a/slack-matrix-migration/slack-matrix-migration/utils.py b/slack-matrix-migration/slack-matrix-migration/utils.py index e0ec3e5..dc64dfa 100644 --- a/slack-matrix-migration/slack-matrix-migration/utils.py +++ b/slack-matrix-migration/slack-matrix-migration/utils.py @@ -51,7 +51,7 @@ def send_event( #_log.info("Sending registration request...") try: - r = requests.put(url, headers={'Authorization': 'Bearer ' + config["as_token"]}, json=matrix_message, verify=False) + r = requests.put(url, headers={'Authorization': 'Bearer ' + config["as_token"]}, json=matrix_message, verify=config["verify-ssl"]) except requests.exceptions.RequestException as e: # catastrophic error. bail. log.error( @@ -70,7 +70,7 @@ def send_event( conf ) try: - r = requests.put(url, headers={'Authorization': 'Bearer ' + config["as_token"]}, json=matrix_message, verify=False) + r = requests.put(url, headers={'Authorization': 'Bearer ' + config["as_token"]}, json=matrix_message, verify=config["verify-ssl"]) except requests.exceptions.RequestException as e: # catastrophic error. bail. log.error( @@ -109,7 +109,7 @@ def invite_user( #_log.info("Sending registration request...") try: - r = requests.post(url, headers={'Authorization': 'Bearer ' + config["as_token"]}, json=body, verify=False) + r = requests.post(url, headers={'Authorization': 'Bearer ' + config["as_token"]}, json=body, verify=config["verify-ssl"]) except requests.exceptions.RequestException as e: # catastrophic error. bail. log.error(