Лабораторная 7

This commit is contained in:
MacBook
2026-11-24 12:20:21 +03:00
parent 20afc2c790
commit b962fa0d5d
99 changed files with 11329 additions and 107 deletions
+16
View File
@@ -0,0 +1,16 @@
#!/usr/bin/env python3
"""Operator rotates worker DB password; old authentication is invalidated.
The secret travels to psql only through stdin, never argv/logs/evidence.
"""
import json,os,secrets,subprocess
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];os.chdir(ROOT);os.umask(0o077)
p=Path('secrets/operator/service-secrets.json');data=json.loads(p.read_text());pwd=secrets.token_hex(24)
from urllib.parse import urlsplit,urlunsplit
u=urlsplit(data['worker']['DATABASE_URL']);data['worker']['DATABASE_URL']=urlunsplit((u.scheme,'worker:'+pwd+'@'+u.hostname+':5432',u.path,u.query,u.fragment))
result=subprocess.run(['docker','compose','--env-file','.env','exec','-T','postgres','psql','-Xq','-v','ON_ERROR_STOP=1','-U','messenger','-d','messenger'],input="ALTER ROLE worker PASSWORD '"+pwd+"';\n",text=True,capture_output=True)
if result.returncode:raise SystemExit('database password rotation failed')
p.chmod(0o600);p.write_text(json.dumps(data));p.chmod(0o444);Path('secrets/db_worker').write_text(pwd)
subprocess.run(['python3','scripts/bao-operator.py','configure'],check=True)
subprocess.run(['docker','compose','--env-file','.env','restart','worker'],check=True,stdout=subprocess.DEVNULL)
print('Worker DB secret rotated, active worker recreated its pool; images unchanged.')