forked from mtusi2026/mtusi2026lab6
Реализовать лабораторную 6 на 5: SLO и обнаружение инцидентов
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
"""Создаёт локальное окружение и secrets; существующие значения сохраняет."""
|
||||
from pathlib import Path
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import secrets
|
||||
import shutil
|
||||
from urllib.parse import quote
|
||||
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
if not (root / ".env").exists():
|
||||
shutil.copyfile(root / ".env.example", root / ".env")
|
||||
directory = root / "secrets"
|
||||
directory.mkdir(exist_ok=True, mode=0o700)
|
||||
directory.chmod(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"postgresql://messenger:{quote(password, safe='')}@postgres:5432/messenger",
|
||||
"cache_url": "redis://valkey:6379/0",
|
||||
"broker_password": secrets.token_hex(24),
|
||||
"s3_root_user": "root-" + secrets.token_hex(8),
|
||||
"s3_root_password": secrets.token_urlsafe(32),
|
||||
"s3_access_key_id": "api-" + secrets.token_hex(8),
|
||||
"s3_secret_access_key": secrets.token_urlsafe(32),
|
||||
}
|
||||
for name, value in values.items():
|
||||
path = directory / name
|
||||
if not path.exists():
|
||||
path.write_text(value)
|
||||
broker_url = directory / "broker_url"
|
||||
if not broker_url.exists():
|
||||
broker_url.write_text("redis://:" + (directory / "broker_password").read_text().strip() + "@broker:6379/0")
|
||||
broker_url.chmod(0o444)
|
||||
dotenv = dict(line.split("=", 1) for line in (root / ".env").read_text().splitlines()
|
||||
if line.strip() and not line.lstrip().startswith("#") and "=" in line)
|
||||
bucket = os.environ.get("S3_BUCKET", dotenv.get("S3_BUCKET", "messenger-attachments"))
|
||||
if re.fullmatch(r"[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]", bucket) is None:
|
||||
raise ValueError("Недопустимое имя S3 bucket")
|
||||
def local_secret(name):
|
||||
return (directory / name).read_text().strip()
|
||||
config = {"accounts": [{"id": "messenger-api", "displayName": "messenger-api"}], "identities": [
|
||||
{"name": "bootstrap", "credentials": [{"accessKey": local_secret("s3_root_user"),
|
||||
"secretKey": local_secret("s3_root_password")}], "actions": ["Admin"]},
|
||||
{"name": "messenger-api", "account": {"id": "messenger-api", "displayName": "messenger-api"},
|
||||
"credentials": [{"accessKey": local_secret("s3_access_key_id"), "secretKey": local_secret("s3_secret_access_key")}],
|
||||
"actions": ["Read:" + bucket, "List:" + bucket, "Write:" + bucket + "/attachments/*"]},
|
||||
]}
|
||||
config_file = directory / "s3_config"
|
||||
encoded_config = json.dumps(config)
|
||||
if not config_file.exists() or config_file.read_text() != encoded_config:
|
||||
temporary = directory / "s3_config.tmp"
|
||||
temporary.write_text(encoded_config)
|
||||
temporary.chmod(0o444)
|
||||
temporary.replace(config_file)
|
||||
# Каталог 0700 защищает файлы на хосте; 0444 позволяет USER 10001 читать bind-mounted secret.
|
||||
for name in ("postgres_password", "s3_config", *values):
|
||||
(directory / name).chmod(0o444)
|
||||
print("Local .env and secrets are ready; values are not printed")
|
||||
Reference in New Issue
Block a user