Skip to content

Commit

Permalink
Merge pull request #154 from atlanticwave-sdx/153.connstring
Browse files Browse the repository at this point in the history
Use `MONGODB_CONNSTRING` when `MONGO_HOST` is not set
  • Loading branch information
italovalcy authored Jul 24, 2024
2 parents 7abd910 + e241789 commit e1d842a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
6 changes: 6 additions & 0 deletions env.template
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
44 changes: 28 additions & 16 deletions sdx_lc/utils/db_utils.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
import logging
import os
from urllib.parse import urlparse

import pymongo

DB_NAME = os.environ.get("DB_NAME")
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 uri.replace(parts.password, "*")
else:
return uri


class DbUtils(object):
def __init__(self):
self.db_name = DB_NAME
self.config_table_name = DB_CONFIG_TABLE_NAME
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")

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)

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:
raise Exception("MONGO_HOST environment variable is not 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}/"
)
mongo_connstring = os.getenv("MONGODB_CONNSTRING")
if mongo_connstring is None:
raise Exception("Neither MONGO_HOST nor MONGODB_CONNSTRING is set")
else:
mongo_connstring = (
f"mongodb://{mongo_user}:{mongo_pass}@{mongo_host}:{mongo_port}/"
)

# 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)

Expand Down

0 comments on commit e1d842a

Please sign in to comment.