Skip to content

Commit

Permalink
Release 20240604 (#354)
Browse files Browse the repository at this point in the history
* Added support for sending email via smtp

  Also kept support for the old method

* Updated email code and removed old auth route

* Bug fix and updated deployment variables
  • Loading branch information
caparker authored Jun 6, 2024
1 parent cc3c1cd commit 016c7e7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 30 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Deploy prod

on:
on:
push:
branches:
- main
Expand Down Expand Up @@ -41,6 +41,9 @@ env:
ORIGIN: ${{ secrets.ORIGIN }}

EMAIL_SENDER: ${{ secrets.EMAIL_SENDER }}
SMTP_EMAIL_HOST: ${{ secrets.SMTP_EMAIL_HOST }}
SMTP_EMAIL_USER: ${{ secrets.SMTP_EMAIL_USER }}
SMTP_EMAIL_PASSWORD: ${{ secrets.SMTP_EMAIL_PASSWORD }}

EXPLORER_API_KEY: ${{ secrets.EXPLORER_API_KEY }}

Expand Down Expand Up @@ -68,7 +71,7 @@ jobs:
run: |
yarn install
yarn run deploy
- name: Install CDK
run: |
npm install -g [email protected]
Expand All @@ -81,7 +84,7 @@ jobs:
working-directory: ./cdk
run: |
pip install -r requirements.txt
cdk deploy openaq-api-prod --require-approval never
cdk deploy openaq-api-prod --require-approval never
update-docs:
needs: deploy
Expand All @@ -93,7 +96,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: "18"

- uses: actions/setup-python@v3
with:
python-version: '3.11'
Expand All @@ -105,4 +108,4 @@ jobs:
npm install -g rdme@latest
python generate_schema.py
VERSION=
rdme openapi openapi.json --version=$VERSION --key=${{ secrets.README_KEY }} --id=${{ secrets.README_API_DEFINITION_ID }}
rdme openapi openapi.json --version=$VERSION --key=${{ secrets.README_KEY }} --id=${{ secrets.README_API_DEFINITION_ID }}
9 changes: 6 additions & 3 deletions .github/workflows/deploy-staging.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Deploy staging

on:
on:
push:
branches:
- release-*
Expand Down Expand Up @@ -40,10 +40,13 @@ env:
RATE_TIME: 1

EMAIL_SENDER: ${{ secrets.EMAIL_SENDER }}
SMTP_EMAIL_HOST: ${{ secrets.SMTP_EMAIL_HOST }}
SMTP_EMAIL_USER: ${{ secrets.SMTP_EMAIL_USER }}
SMTP_EMAIL_PASSWORD: ${{ secrets.SMTP_EMAIL_PASSWORD }}

EXPLORER_API_KEY: ${{ secrets.EXPLORER_API_KEY }}


jobs:
deploy:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -80,4 +83,4 @@ jobs:
working-directory: ./cdk
run: |
pip install -r requirements.txt
cdk deploy openaq-api-staging --require-approval never
cdk deploy openaq-api-staging --require-approval never
4 changes: 2 additions & 2 deletions openaq_api/openaq_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
UnprocessableEntityLog,
WarnLog,
)
from openaq_api.routers.auth import router as auth_router
#from openaq_api.routers.auth import router as auth_router
from openaq_api.routers.averages import router as averages_router
from openaq_api.routers.cities import router as cities_router
from openaq_api.routers.countries import router as countries_router
Expand Down Expand Up @@ -254,7 +254,7 @@ def favico():
app.include_router(providers.router)
app.include_router(sensors.router)

app.include_router(auth_router)
# app.include_router(auth_router)
app.include_router(averages_router)
app.include_router(cities_router)
app.include_router(countries_router)
Expand Down
9 changes: 9 additions & 0 deletions openaq_api/openaq_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class Settings(BaseSettings):

EMAIL_SENDER: str | None = None

SMTP_EMAIL_HOST: str | None = None
SMTP_EMAIL_USER: str | None = None
SMTP_EMAIL_PASSWORD: str | None = None

EXPLORER_API_KEY: str

@computed_field(return_type=str, alias="DATABASE_READ_URL")
Expand All @@ -49,6 +53,11 @@ def DATABASE_READ_URL(self):
def DATABASE_WRITE_URL(self):
return f"postgresql://{self.DATABASE_WRITE_USER}:{self.DATABASE_WRITE_PASSWORD}@{self.DATABASE_HOST}:{self.DATABASE_PORT}/{self.DATABASE_DB}"

@computed_field(return_type=str, alias="DATABASE_WRITE_URL")
@property
def USE_SMTP_EMAIL(self):
return None not in [self.SMTP_EMAIL_HOST, self.SMTP_EMAIL_USER, self.SMTP_EMAIL_PASSWORD]

model_config = SettingsConfigDict(extra="ignore", env_file=get_env())


Expand Down
44 changes: 24 additions & 20 deletions openaq_api/openaq_api/v3/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import pathlib
from email.message import EmailMessage
import smtplib

import boto3
from fastapi import APIRouter, Body, Depends, HTTPException, Request, status
Expand All @@ -26,6 +27,24 @@
include_in_schema=False,
)

def send_email(destination_email: str, msg: EmailMessage):
if settings.USE_SMTP_EMAIL:
return send_smtp_email(destination_email, msg)
else:
return send_ses_email(destination_email, msg)

def send_smtp_email(destination_email: str, msg: EmailMessage):
with smtplib.SMTP_SSL(settings.SMTP_EMAIL_HOST, 465) as s:
s.login(settings.SMTP_EMAIL_USER, settings.SMTP_EMAIL_PASSWORD)
return s.send_message(msg)


def send_ses_email(destination_email: str, msg: EmailMessage):
return ses_client.send_raw_email(
Source=settings.EMAIL_SENDER,
Destinations=[f"{full_name} <{destination_email}>"],
RawMessage={"Data": msg.as_string()},
)

def send_change_password_email(full_name: str, email: str):
ses_client = boto3.client("ses")
Expand Down Expand Up @@ -55,11 +74,7 @@ def send_change_password_email(full_name: str, email: str):
msg["Subject"] = "OpenAQ Explorer - Password changed"
msg["From"] = settings.EMAIL_SENDER
msg["To"] = email
response = ses_client.send_raw_email(
Source=settings.EMAIL_SENDER,
Destinations=[f"{full_name} <{email}>"],
RawMessage={"Data": msg.as_string()},
)
response = send_email(email, msg)
logger.info(
SESEmailLog(
detail=json.dumps(
Expand Down Expand Up @@ -103,11 +118,7 @@ def send_verification_email(verification_code: str, full_name: str, email: str):
msg["Subject"] = "OpenAQ Explorer - Verify your email"
msg["From"] = settings.EMAIL_SENDER
msg["To"] = email
response = ses_client.send_raw_email(
Source=settings.EMAIL_SENDER,
Destinations=[f"{full_name} <{email}>"],
RawMessage={"Data": msg.as_string()},
)
response = send_email(email, msg)
logger.info(
SESEmailLog(
detail=json.dumps(
Expand Down Expand Up @@ -151,11 +162,7 @@ def send_password_reset_email(verification_code: str, email: str):
msg["Subject"] = "OpenAQ Explorer - Reset password request"
msg["From"] = settings.EMAIL_SENDER
msg["To"] = email
response = ses_client.send_raw_email(
Source=settings.EMAIL_SENDER,
Destinations=[f"<{email}>"],
RawMessage={"Data": msg.as_string()},
)
response = send_email(email, msg)
logger.info(
SESEmailLog(
detail=json.dumps(
Expand Down Expand Up @@ -199,11 +206,8 @@ def send_password_changed_email(email: str):
msg["Subject"] = "OpenAQ Explorer - Reset password success"
msg["From"] = settings.EMAIL_SENDER
msg["To"] = email
response = ses_client.send_raw_email(
Source=settings.EMAIL_SENDER,
Destinations=[f"<{email}>"],
RawMessage={"Data": msg.as_string()},
)
response = send_email(email, msg)

logger.info(
SESEmailLog(
detail=json.dumps(
Expand Down

0 comments on commit 016c7e7

Please sign in to comment.