Skip to content

Commit

Permalink
feat: handle env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
montoyaobeso committed Jun 10, 2024
1 parent d178bd5 commit bc7c393
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/app/db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
16 changes: 10 additions & 6 deletions src/app/email/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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"])
Expand Down

0 comments on commit bc7c393

Please sign in to comment.