forked from mtusi2026/mtusi2026lab2
lab2
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create local synthetic credentials once. Never print their values."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
os.umask(0o077)
|
||||
folder = root / 'secrets'
|
||||
folder.mkdir(exist_ok=True)
|
||||
password_path = folder / 'postgres_password'
|
||||
if not password_path.exists():
|
||||
password_path.write_text(secrets.token_hex(24))
|
||||
password = password_path.read_text().strip()
|
||||
for name, value in {
|
||||
'database_url': 'postgresql://messenger:' + password + '@postgres:5432/messenger',
|
||||
}.items():
|
||||
path = folder / name
|
||||
if not path.exists():
|
||||
path.write_text(value)
|
||||
# Docker Desktop mounts are read by the non-root API user too. Directory remains private.
|
||||
for path in folder.iterdir():
|
||||
path.chmod(0o444)
|
||||
env = root / '.env'
|
||||
if not env.exists():
|
||||
env.write_text('API_PORT=8080\nSESSION_TTL_SECONDS=3600\nDB_POOL_SIZE=8\n')
|
||||
print('Local secrets and .env ready (values hidden; existing files preserved).')
|
||||
Reference in New Issue
Block a user