From 7f943c5e25bb6b96e091ae926604c5b279a43565 Mon Sep 17 00:00:00 2001 From: David Dudas Date: Tue, 12 Nov 2024 16:39:04 -0800 Subject: [PATCH] [Issue 2665] Add temp logging to help troubleshoot db connection failures (#2816) ## Summary Partially Fixes #2665 ### Time to review: __1 min__ ## Changes proposed > What was added, updated, or removed in this PR. Added `sslmode` to Postgres connection string. Also added some temporary logging to help troubleshoot db connection failures from analytics code. I will remove the logging in a future PR, as soon as I find the cause of the db connection failures. ## Context for reviewers > Testing instructions, background context, more in-depth details of the implementation, and anything else you'd like to call out or ask reviewers. Explain how the changes were verified. Analytics step functions have been deployed but when they execute their attempted connections to Postgres always fail. ## Additional information > Screenshots, GIF demos, code examples or output to help show the changes working as expected. --- analytics/config.py | 2 +- analytics/src/analytics/integrations/db.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/analytics/config.py b/analytics/config.py index 6d07bc3d5..08bce54b9 100644 --- a/analytics/config.py +++ b/analytics/config.py @@ -8,7 +8,7 @@ # reads environment variables from .env files defaulting to "local.env" class PydanticBaseEnvConfig(BaseSettings): - model_config = SettingsConfigDict(env_file="%s.env" % os.getenv("ENVIRONMENT", "local"), extra="ignore") # set extra to ignore so that it ignores variables irrelevant to the database config (e.g. metabase settings) + model_config = SettingsConfigDict(env_file="%s.env" % os.getenv("ENVIRONMENT", "local"), extra="allow") class DBSettings(PydanticBaseEnvConfig): db_host: str = Field(alias="DB_HOST") diff --git a/analytics/src/analytics/integrations/db.py b/analytics/src/analytics/integrations/db.py index 7ad722c71..c2160cf15 100644 --- a/analytics/src/analytics/integrations/db.py +++ b/analytics/src/analytics/integrations/db.py @@ -1,6 +1,8 @@ # pylint: disable=invalid-name, line-too-long """Get a connection to the database using a SQLAlchemy engine object.""" +import os + import boto3 from sqlalchemy import Engine, create_engine @@ -25,11 +27,15 @@ def get_db() -> Engine: db = get_db_settings() # inspired by simpler-grants-gov/blob/main/api/src/adapters/db/clients/postgres_client.py token = db.password if db.local_env is True else generate_iam_auth_token(db) + url = f"postgresql+psycopg://{db.user}:{token}@{db.db_host}:{db.port}?sslmode={db.ssl_mode}" + print(f"TEMP DEBUG: environment = {os.getenv('ENVIRONMENT', 'local')}") + print(f"TEMP DEBUG: db settings = {db}") + print(f"TEMP DEBUG: token has non-zero len? {len(str(token)) > 0}") return create_engine( - f"postgresql+psycopg://{db.user}:{token}@{db.db_host}:{db.port}", + url, pool_pre_ping=True, - hide_parameters=True, + hide_parameters=False, )