forked from mtusi2026/mtusi2026lab3
36 lines
1.4 KiB
Python
36 lines
1.4 KiB
Python
"""Create local synthetic credentials once without printing secret values."""
|
|
from pathlib import Path
|
|
import secrets
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def main():
|
|
directory = ROOT / "secrets"
|
|
directory.mkdir(exist_ok=True)
|
|
for name in ("postgres_password", "redis_password", "cursor_key"):
|
|
path = directory / name
|
|
if not path.exists():
|
|
path.write_text(secrets.token_hex(32) + "\n", encoding="utf-8", newline="\n")
|
|
else:
|
|
# Shell command substitution on Linux must not retain a Windows CR.
|
|
value = path.read_text(encoding="utf-8").strip()
|
|
path.write_text(value + "\n", encoding="utf-8", newline="\n")
|
|
pg_password = (directory / "postgres_password").read_text().strip()
|
|
redis_password = (directory / "redis_password").read_text().strip()
|
|
for name, value in {
|
|
"database_url": f"postgresql://messenger:{pg_password}@postgres:5432/messenger",
|
|
"cache_url": f"redis://:{redis_password}@redis:6379/0",
|
|
}.items():
|
|
path = directory / name
|
|
if not path.exists():
|
|
path.write_text(value + "\n", encoding="utf-8", newline="\n")
|
|
env = ROOT / ".env"
|
|
if not env.exists():
|
|
env.write_bytes((ROOT / ".env.example").read_bytes())
|
|
print("PASS: local .env and secret files exist; existing values preserved")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|