выполнение лабораторной на 5

This commit is contained in:
kaktuz20000
2026-10-26 17:55:33 +03:00
parent ec540a1a72
commit bd0b9f6849
34 changed files with 2818 additions and 49 deletions
+80
View File
@@ -0,0 +1,80 @@
#!/usr/bin/env python3
import json
import os
import secrets
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SECRET_DIR = ROOT / "secrets"
def main():
SECRET_DIR.mkdir(mode=0o700, exist_ok=True)
names = (
"postgres_password",
"database_url",
"s3_access_key_id",
"s3_secret_access_key",
"s3_admin_access_key_id",
"s3_admin_secret_access_key",
"s3.json",
)
existing = [name for name in names if (SECRET_DIR / name).exists()]
if existing:
raise SystemExit(
"Secret files already exist; refusing to rotate credentials without a migration: "
+ ", ".join(existing)
)
db_password = secrets.token_hex(24)
values = {
"postgres_password": db_password,
"database_url": f"postgresql://messenger:{db_password}@postgres:5432/messenger",
"s3_access_key_id": "mngr" + secrets.token_hex(8),
"s3_secret_access_key": secrets.token_urlsafe(40),
"s3_admin_access_key_id": "admin" + secrets.token_hex(7),
"s3_admin_secret_access_key": secrets.token_urlsafe(40),
}
for name, value in values.items():
path = SECRET_DIR / name
descriptor = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
output.write(value + "\n")
s3_config = {
"identities": [
{
"name": "bootstrap-admin",
"credentials": [
{
"accessKey": values["s3_admin_access_key_id"],
"secretKey": values["s3_admin_secret_access_key"],
}
],
"actions": ["Admin"],
},
{
"name": "messenger-api",
"credentials": [
{
"accessKey": values["s3_access_key_id"],
"secretKey": values["s3_secret_access_key"],
}
],
"actions": [
"Read:messenger-attachments",
"Write:messenger-attachments",
"List:messenger-attachments",
],
},
]
}
descriptor = os.open(SECRET_DIR / "s3.json", os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
json.dump(s3_config, output, separators=(",", ":"))
output.write("\n")
print("Local secrets generated with owner-only permissions; values were not printed.")
if __name__ == "__main__":
main()
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
set -eu
exec python3 "$(dirname "$0")/bootstrap-secrets.py"
+16
View File
@@ -0,0 +1,16 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose restart postgres s3
docker compose up -d --wait postgres s3
attempt=0
until docker compose exec --index 1 -T api python3 -c 'from src.object_store import ObjectStore; store = ObjectStore(); key, _ = next(store.objects()); store.get(key, 10485760)' >/dev/null 2>&1; do
attempt=$((attempt + 1))
if [ "$attempt" -ge 4 ]; then
echo "S3 object reads did not recover after restart" >&2
exit 1
fi
sleep 2
done
docker compose restart api
docker compose up -d --wait api
+19
View File
@@ -0,0 +1,19 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
found=
for container_id in $(docker compose ps --quiet --status running api); do
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
found=$container_id
break
fi
done
if [ -z "$found" ]; then
printf '%s\n' 'No running API container matched the requested instance alias.' >&2
exit 1
fi
docker stop --time 5 "$found" >/dev/null