forked from mtusi2026/mtusi2026lab2
ЛР 2 — БВТ2303 — Брезгунова — на 5
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate private LOCAL configuration once; never display credentials."""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
def main():
|
||||
env = ROOT / '.env'
|
||||
folder = ROOT / 'secrets'
|
||||
folder.mkdir(exist_ok=True)
|
||||
if not env.exists():
|
||||
password = secrets.token_hex(24)
|
||||
env.write_text('POSTGRES_PASSWORD='+password+'\nSESSION_TTL_SECONDS=3600\nHOST_PORT=8080\n', encoding='utf-8')
|
||||
settings = dict(line.split('=', 1) for line in env.read_text(encoding='utf-8').splitlines() if line and not line.startswith('#') and '=' in line)
|
||||
password = settings.get('POSTGRES_PASSWORD')
|
||||
if not password or password == 'GENERATED_BY_BOOTSTRAP':
|
||||
raise SystemExit('Use a local generated POSTGRES_PASSWORD in .env or remove an unused example .env before bootstrap.')
|
||||
from urllib.parse import quote
|
||||
url = 'postgres://messenger:'+quote(password, safe='')+'@postgres:5432/messenger?sslmode=disable&connect_timeout=2&statement_timeout=2000&lock_timeout=1000'
|
||||
(folder / 'database_url').write_text(url+'\n', encoding='utf-8')
|
||||
(folder / 'cache_url').write_text('redis://redis:6379/0\n', encoding='utf-8')
|
||||
# File-backed Compose secrets keep host permissions on Linux. The private
|
||||
# directory protects them on the host; 0644 lets the unprivileged API read
|
||||
# individual bind-mounted files inside its container.
|
||||
folder.chmod(0o700)
|
||||
env.chmod(0o600)
|
||||
for path in [folder / 'database_url', folder / 'cache_url']:
|
||||
path.chmod(0o644)
|
||||
print('Local configuration ready. Credentials are kept in ignored .env and secrets/.')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user