Skip to content

Commit 17aff8a

Browse files
committed
web: Read Postgres secret from Docker secrets
This fixes an issue where the Postgres password was not found in the environment in Docker Swarm. We look for a ``POSTGRES_PASSWORD_FILE``, and fallback to a hardcoded ``/run/secrets`` path if we don't find it. Fixes: 312bf52 ("web: Add a custom data layer retrieval method") Ref: AP-530
1 parent 312bf52 commit 17aff8a

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

willa/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111

112112
_NEEDS_ENVIRON: list[str] = ['AWS_DEFAULT_REGION', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY',
113113
'LANGFUSE_HOST', 'LANGFUSE_PUBLIC_KEY', 'LANGFUSE_SECRET_KEY',
114-
'CHAINLIT_AUTH_SECRET']
114+
'CHAINLIT_AUTH_SECRET', 'POSTGRES_PASSWORD_FILE']
115115
"""A list of configuration keys that need to be set in the environment as well."""
116116

117117
for key in _NEEDS_ENVIRON:

willa/web/app.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,16 @@ def data_layer() -> ChainlitDataLayer:
5757
def _pg(var: str) -> str:
5858
return os.environ[f'POSTGRES_{var}']
5959

60+
def _secret() -> str:
61+
if 'POSTGRES_PASSWORD' in os.environ:
62+
return os.environ['POSTGRES_PASSWORD']
63+
64+
with open(os.environ.get('POSTGRES_PASSWORD_FILE',
65+
'/run/secrets/POSTGRES_PASSWORD'), 'r', encoding='utf8') as p_file:
66+
return p_file.read()
67+
6068
database_url = os.environ.get(
61-
'DATABASE_URL', f"postgresql://{_pg('USER')}:{_pg('PASSWORD')}@{_pg('HOST')}/{_pg('DB')}"
69+
'DATABASE_URL', f"postgresql://{_pg('USER')}:{_secret()}@{_pg('HOST')}/{_pg('DB')}"
6270
)
6371
return ChainlitDataLayer(database_url=database_url)
6472

0 commit comments

Comments
 (0)