Skip to content

Commit

Permalink
Updated to use PostgresDSN.build.
Browse files Browse the repository at this point in the history
This required the addtion of INST_DB_SCHEME, which I've updated in the .env files, but will
write stories to update the AWS secrets.

Removed the INST_DB_CONN from the env. If we want to keep this, I can add an if in the
field_validator to check for existence, but that makes encoding the pwd tricky again.  Unless
we assume if the INST_DB_CONN exists, that it's local dev and ignore pwd encoding.
  • Loading branch information
jcadam14 committed Jan 2, 2024
1 parent e14e72b commit 4b9ade1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ addopts = [
]
testpaths = ["tests"]
env = [
"INST_CONN=postgresql+asyncpg://user:user@localhost/main",
"INST_DB_SCHEMA=main",
"INST_DB_USER=user",
"INST_DB_PWD=user",
"INST_DB_HOST=localhost:5432",
"INST_DB_NAME=fi",
"INST_DB_SCHEME=postgresql+asyncpg",
"KC_URL=http://localhost",
"KC_REALM=",
"KC_ADMIN_CLIENT_ID=",
Expand Down
2 changes: 1 addition & 1 deletion src/.env.local
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ INST_DB_USER=fi
INST_DB_PWD=fi
INST_DB_HOST=localhost:5432
INST_DB_SCHEMA=public
INST_CONN=postgresql+asyncpg://${INST_DB_USER}:${INST_DB_PWD}@${INST_DB_HOST}/${INST_DB_NAME}
INST_DB_SCHEME=postgresql+asyncpg
JWT_OPTS_VERIFY_AT_HASH="false"
JWT_OPTS_VERIFY_AUD="false"
JWT_OPTS_VERIFY_ISS="false"
2 changes: 1 addition & 1 deletion src/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ INST_DB_USER=
INST_DB_PWD=
INST_DB_HOST=
INST_DB_SCHEMA=
INST_CONN=postgresql+asyncpg://${INST_DB_USER}:${INST_DB_PWD}@${INST_DB_HOST}/${INST_DB_NAME}
INST_DB_SCHEME=postgresql+asyncpg
29 changes: 16 additions & 13 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import logging
import re
from urllib import parse
from typing import Dict, Any
from typing import Dict, Any, Optional

from pydantic import TypeAdapter, field_validator, ValidationInfo
from pydantic.networks import HttpUrl, PostgresDsn
Expand All @@ -17,8 +15,13 @@


class Settings(BaseSettings):
inst_conn: PostgresDsn
inst_db_schema: str = "public"
inst_db_name: str
inst_db_user: str
inst_db_pwd: str
inst_db_host: str
inst_db_scheme: str
inst_conn: Optional[PostgresDsn] = None
auth_client: str
auth_url: HttpUrl
token_url: HttpUrl
Expand All @@ -36,15 +39,15 @@ def __init__(self, **data):

@field_validator("inst_conn", mode="before")
@classmethod
def encode_db_password(cls, postgres_dsn, info: ValidationInfo) -> Any:
log = logging.getLogger()
pwd = re.search(".*:.*:(.*)@", postgres_dsn)
if pwd:
pwd_str = pwd.group(1)
encoded_password = parse.quote(pwd_str, safe="")
return postgres_dsn.replace(pwd_str, encoded_password)
else:
log.error(f"Postgres DSN did not contain a properly formatted URL: {postgres_dsn}")
def build_postgres_dsn(cls, postgres_dsn, info: ValidationInfo) -> Any:
postgres_dsn = PostgresDsn.build(
scheme=info.data.get("inst_db_scheme"),
username=info.data.get("inst_db_user"),
password=parse.quote(info.data.get("inst_db_pwd"), safe=""),
host=info.data.get("inst_db_host"),
path=info.data.get("inst_db_name"),
)
return str(postgres_dsn)

def set_jwt_opts(self) -> None:
"""
Expand Down
12 changes: 9 additions & 3 deletions tests/app/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
from config import Settings


def test_psql_password_encoding():
mock_config = {"inst_conn": "postgresql+asyncpg://test:\\z9-/tgb76@test/test"}
def test_postgres_dsn_building():
mock_config = {
"inst_db_name": "test",
"inst_db_user": "user",
"inst_db_pwd": "\\z9-/tgb76#@",
"inst_db_host": "test:5432",
"inst_db_scehma": "test",
}
settings = Settings(**mock_config)
assert str(settings.inst_conn) == "postgresql+asyncpg://test:%5Cz9-%2Ftgb76@test/test"
assert str(settings.inst_conn) == "postgresql+asyncpg://user:%5Cz9-%2Ftgb76%23%40@test:5432/test"


def test_jwt_opts_valid_values():
Expand Down

0 comments on commit 4b9ade1

Please sign in to comment.