From 0898513c98a3a1503e2e04ac3c06303474f40468 Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Wed, 26 Jun 2024 10:40:22 -0500 Subject: [PATCH 1/7] Try to use MONGODB_CONNSTRING when MONGO_HOST is not set --- sdx_lc/utils/db_utils.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sdx_lc/utils/db_utils.py b/sdx_lc/utils/db_utils.py index a0a5a9f..c97922e 100644 --- a/sdx_lc/utils/db_utils.py +++ b/sdx_lc/utils/db_utils.py @@ -21,14 +21,16 @@ def __init__(self): mongo_port = os.getenv("MONGO_PORT") if mongo_host is None: - raise Exception("MONGO_HOST environment variable is not set") + mongo_connstring = os.getenv("MONGODB_CONNSTRING") + if mongo_connstring is None: + raise Exception("Neither MONGO_HOST nor MONGODB_CONNSTRING is set") if mongo_port is None: raise Exception("MONGO_PORT environment variable is not set") - - mongo_connstring = ( - f"mongodb://{mongo_user}:{mongo_pass}@{mongo_host}:{mongo_port}/" - ) + else: + mongo_connstring = ( + f"mongodb://{mongo_user}:{mongo_pass}@{mongo_host}:{mongo_port}/" + ) # Log DB URI, without a password. self.logger.info( From e8034f4191e51ffe7171b846cd54e2cd8fb5782d Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Wed, 26 Jun 2024 10:43:05 -0500 Subject: [PATCH 2/7] Obfuscate MongoDB password when logging --- sdx_lc/utils/db_utils.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/sdx_lc/utils/db_utils.py b/sdx_lc/utils/db_utils.py index c97922e..409b0a5 100644 --- a/sdx_lc/utils/db_utils.py +++ b/sdx_lc/utils/db_utils.py @@ -1,5 +1,6 @@ import logging import os +from urllib.parse import urlparse import pymongo @@ -7,6 +8,17 @@ DB_CONFIG_TABLE_NAME = os.environ.get("DB_CONFIG_TABLE_NAME") +def obfuscate_password_in_uri(uri: str) -> str: + """ + Replace password field in URIs with a `*`, for logging. + """ + parts = urlparse(uri) + if parts.password: + return f"{parts.scheme}://{parts.username}:*@{parts.hostname}:{parts.port}/" + else: + return uri + + class DbUtils(object): def __init__(self): self.db_name = DB_NAME @@ -33,9 +45,7 @@ def __init__(self): ) # Log DB URI, without a password. - self.logger.info( - f"[DB] Using mongodb://{mongo_user}@{mongo_host}:{mongo_port}/" - ) + self.logger.info(f"[DB] Using {obfuscate_password_in_uri(mongo_connstring)}") self.mongo_client = pymongo.MongoClient(mongo_connstring) From 0f8f87745a689c5f36a80dad280f5a709e24aae0 Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Wed, 26 Jun 2024 10:44:55 -0500 Subject: [PATCH 3/7] Raise an error when DB_NAME is not set --- sdx_lc/utils/db_utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sdx_lc/utils/db_utils.py b/sdx_lc/utils/db_utils.py index 409b0a5..48f20f3 100644 --- a/sdx_lc/utils/db_utils.py +++ b/sdx_lc/utils/db_utils.py @@ -4,7 +4,7 @@ import pymongo -DB_NAME = os.environ.get("DB_NAME") + DB_CONFIG_TABLE_NAME = os.environ.get("DB_CONFIG_TABLE_NAME") @@ -21,7 +21,11 @@ def obfuscate_password_in_uri(uri: str) -> str: class DbUtils(object): def __init__(self): - self.db_name = DB_NAME + self.db_name = os.getenv("DB_NAME") + + if not self.db_name: + raise Exception("DB_NAME environment variable is not set") + self.config_table_name = DB_CONFIG_TABLE_NAME self.logger = logging.getLogger(__name__) From 5c3a7750cd563e77b827e96508e719b5e46ab3b2 Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Wed, 26 Jun 2024 10:48:52 -0500 Subject: [PATCH 4/7] Raise an error when DB_CONFIG_TABLE_NAME env var is not set --- sdx_lc/utils/db_utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sdx_lc/utils/db_utils.py b/sdx_lc/utils/db_utils.py index 48f20f3..bbcca9f 100644 --- a/sdx_lc/utils/db_utils.py +++ b/sdx_lc/utils/db_utils.py @@ -5,9 +5,6 @@ import pymongo -DB_CONFIG_TABLE_NAME = os.environ.get("DB_CONFIG_TABLE_NAME") - - def obfuscate_password_in_uri(uri: str) -> str: """ Replace password field in URIs with a `*`, for logging. @@ -22,11 +19,13 @@ def obfuscate_password_in_uri(uri: str) -> str: class DbUtils(object): def __init__(self): self.db_name = os.getenv("DB_NAME") + self.config_table_name = os.getenv("DB_CONFIG_TABLE_NAME") if not self.db_name: raise Exception("DB_NAME environment variable is not set") - self.config_table_name = DB_CONFIG_TABLE_NAME + if not self.config_table_name: + raise Exception("DB_CONFIG_TABLE_NAME environment variable is not set") self.logger = logging.getLogger(__name__) self.logger.setLevel(logging.DEBUG) From 35e426b157ba604afd1374ba759fe72aa9a9af2d Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Wed, 26 Jun 2024 10:51:18 -0500 Subject: [PATCH 5/7] Use MongoDB default port of 27017 when MONGO_PORT is not set --- sdx_lc/utils/db_utils.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sdx_lc/utils/db_utils.py b/sdx_lc/utils/db_utils.py index bbcca9f..37d8601 100644 --- a/sdx_lc/utils/db_utils.py +++ b/sdx_lc/utils/db_utils.py @@ -33,15 +33,12 @@ def __init__(self): mongo_user = os.getenv("MONGO_USER") or "guest" mongo_pass = os.getenv("MONGO_PASS") or "guest" mongo_host = os.getenv("MONGO_HOST") - mongo_port = os.getenv("MONGO_PORT") + mongo_port = os.getenv("MONGO_PORT") or 27017 if mongo_host is None: mongo_connstring = os.getenv("MONGODB_CONNSTRING") if mongo_connstring is None: raise Exception("Neither MONGO_HOST nor MONGODB_CONNSTRING is set") - - if mongo_port is None: - raise Exception("MONGO_PORT environment variable is not set") else: mongo_connstring = ( f"mongodb://{mongo_user}:{mongo_pass}@{mongo_host}:{mongo_port}/" From e94604c58e2a6cdc1dc8d777b3421a00f92551fb Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Wed, 26 Jun 2024 10:58:36 -0500 Subject: [PATCH 6/7] Leave a hint about MONGODB_CONNSTRING in env template --- env.template | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/env.template b/env.template index 20a3716..7da7d88 100644 --- a/env.template +++ b/env.template @@ -18,6 +18,12 @@ MONGO_PORT=27017 MONGO_USER=guest MONGO_PASS=guest +# # Alternative to specifying the above variables for accessing +# # MongoDB, you can unset MONGO_HOST etc and use MONGODB_CONNSTRING +# # instead. This appears to be useful when using a MongoDB replica +# # set. See https://github.com/atlanticwave-sdx/sdx-lc/issues/153. +# MONGODB_CONNSTRING=mongodb://guest:guest@localhost:27017/ + DB_NAME=test-db DB_CONFIG_TABLE_NAME=test-1 From e241789af7ee62d4895c8fd7bdfb6a101d4e9ad2 Mon Sep 17 00:00:00 2001 From: Cong Wang Date: Fri, 19 Jul 2024 10:03:04 -0400 Subject: [PATCH 7/7] Update sdx_lc/utils/db_utils.py Hide password Co-authored-by: Italo Valcy S Brito --- sdx_lc/utils/db_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdx_lc/utils/db_utils.py b/sdx_lc/utils/db_utils.py index 37d8601..e999dca 100644 --- a/sdx_lc/utils/db_utils.py +++ b/sdx_lc/utils/db_utils.py @@ -11,7 +11,7 @@ def obfuscate_password_in_uri(uri: str) -> str: """ parts = urlparse(uri) if parts.password: - return f"{parts.scheme}://{parts.username}:*@{parts.hostname}:{parts.port}/" + return uri.replace(parts.password, "*") else: return uri