Files
2026-11-13 11:08:23 +03:00

22 lines
1.1 KiB
Python

#!/usr/bin/env python3
"""Create local secrets once. Never print their contents or rotate existing values."""
import base64, secrets, shutil
from pathlib import Path
root=Path(__file__).resolve().parents[1]
directory=root/'secrets'
directory.mkdir(exist_ok=True,mode=0o700)
password_file=directory/'postgres_password'
if not password_file.exists():
password_file.write_text(secrets.token_hex(24))
password=password_file.read_text().strip()
values={'database_url':f'Host=postgres;Port=5432;Database=messenger;Username=messenger;Password={password}',
'cache_url':'redis:6379','cursor_key':base64.b64encode(secrets.token_bytes(32)).decode()}
for name,value in values.items():
p=directory/name
if not p.exists(): p.write_text(value)
# Compose file-backed secrets must be readable by non-root API UID.
# Parent directory is owner-only on the host; no secret files enter the image.
for p in directory.iterdir(): p.chmod(0o444)
if not (root/'.env').exists(): shutil.copyfile(root/'.env.example',root/'.env')
print('Local configuration ready (existing secrets preserved).')