From bc7c3935cce7c5cba869ec7a4dde0354aec91cab Mon Sep 17 00:00:00 2001 From: Abraham Montoya Date: Mon, 10 Jun 2024 13:46:15 -0700 Subject: [PATCH] feat: handle env vars --- src/app/db/database.py | 8 ++++++-- src/app/email/sender.py | 16 ++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/app/db/database.py b/src/app/db/database.py index be5dfc1..7f26599 100644 --- a/src/app/db/database.py +++ b/src/app/db/database.py @@ -5,8 +5,12 @@ from src.app.aws.secrets import get_secret -if os.environ["STAGE"] != "local" and "POSTGRES_HOST" not in os.environ: - db_credentials = get_secret("stori-database-credentials") +if os.environ["STAGE"] != "local": + if "APP_RUNNER_SECRETS" in os.environ: + db_credentials = os.environ["APP_RUNNER_SECRETS"] + else: + db_credentials = get_secret("stori-database-credentials") + os.environ["POSTGRES_USER"] = db_credentials["username"] os.environ["POSTGRES_PASSWORD"] = db_credentials["password"] os.environ["POSTGRES_HOST"] = db_credentials["host"] diff --git a/src/app/email/sender.py b/src/app/email/sender.py index 82e68dc..ec16c51 100644 --- a/src/app/email/sender.py +++ b/src/app/email/sender.py @@ -13,12 +13,16 @@ def __init__(self, subject: str, recipient: str, body_content: str) -> None: self.recipient = recipient self.body_content = body_content - def set_credentials_from_aws_secrets(self): - """Set credentials from AWS SecretsManager.""" + def set_credentials(self): + """Set credentials from AWS SecretsManager or Env variables.""" - secret = get_secret(os.environ["SECRET_NAME"]) - os.environ["SENDGRID_SENDER_EMAIL"] = secret["SENDGRID_SENDER_EMAIL"] - os.environ["SENDGRID_API_KEY"] = secret["SENDGRID_API_KEY"] + if "APP_RUNNER_SECRETS" in os.environ: + secrets = os.environ["APP_RUNNER_SECRETS"] + else: + secrets = get_secret(os.environ["SECRET_NAME"]) + + os.environ["SENDGRID_SENDER_EMAIL"] = secrets["SENDGRID_SENDER_EMAIL"] + os.environ["SENDGRID_API_KEY"] = secrets["SENDGRID_API_KEY"] def send_email(self) -> None: """Send email to recipient.""" @@ -27,7 +31,7 @@ def send_email(self) -> None: "SENDGRID_SENDER_EMAIL" not in os.environ or "SENDGRID_API_KEY" not in os.environ ): - self.set_credentials_from_aws_secrets() + self.set_credentials() sender = os.environ["SENDGRID_SENDER_EMAIL"] sg = sendgrid.SendGridAPIClient(api_key=os.environ["SENDGRID_API_KEY"])