feat: complete lab 7 grade 3 security controls

This commit is contained in:
Dmitrii Grishko
2026-11-14 20:08:27 +03:00
parent 8986ba6bef
commit c5e3ac3aff
49 changed files with 1670 additions and 149 deletions
+36 -51
View File
@@ -26,9 +26,9 @@ def main():
POSTGRES_USER="messenger",
POSTGRES_PASSWORD=secrets.token_hex(24),
)
defaults.update(API_HTTPS_PORT="8443", S3_ENDPOINT="http://s3:9000",
OTEL_EXPORTER_OTLP_ENDPOINT="http://otel-collector:4318", UPLOAD_CLEANUP_GRACE_SECONDS="600", UPLOAD_TMP_DIR="/tmp/uploads",
S3_PUBLIC_ENDPOINT="http://127.0.0.1:9000",
defaults.update(API_HTTPS_PORT="8443", S3_ENDPOINT="https://s3:9000",
OTEL_EXPORTER_OTLP_ENDPOINT="https://otel-collector:4318", UPLOAD_CLEANUP_GRACE_SECONDS="600", UPLOAD_TMP_DIR="/tmp/uploads",
S3_PUBLIC_ENDPOINT="https://localhost:9000",
S3_CORS_ORIGINS="http://127.0.0.1:5173,http://localhost:5173")
values = defaults | existing
for name, lower, upper in (
@@ -63,60 +63,45 @@ def main():
os.chmod(path, 0o600)
secret_dir = ROOT / "secrets"
secret_dir.mkdir(mode=0o700, exist_ok=True)
database_url = "postgres://{}:{}@db:5432/{}?sslmode=disable".format(
def create_secret(name, value):
target = secret_dir / name
if not target.exists():
target.write_text(value + "\n", encoding="utf-8")
target.chmod(0o644)
return target.read_text(encoding="utf-8").strip()
create_secret("grafana_admin_password", secrets.token_hex(24))
database_url = "postgres://{}:{}@db:5432/{}?sslmode=verify-full&sslrootcert=/run/tls/ca.crt".format(
quote(values["POSTGRES_USER"], safe=""),
quote(values["POSTGRES_PASSWORD"], safe=""),
quote(values["POSTGRES_DB"], safe=""),
)
for filename,content in (("broker_user","messenger"),("broker_password",secrets.token_hex(32))):
target=secret_dir/filename
if not target.exists():
target.write_text(content+"\n",encoding="utf-8")
os.chmod(target,0o644)
broker_url="amqp://{}:{}@broker:5672/%2F".format(
quote((secret_dir/"broker_user").read_text().strip(),safe=""),
quote((secret_dir/"broker_password").read_text().strip(),safe=""))
# Migrate only the generated legacy Redis URL; preserve custom AMQP settings.
broker_file=secret_dir/"broker_url"
if broker_file.exists() and broker_file.read_text().strip()=="redis://broker:6379/0":
broker_file.write_text(broker_url+"\n",encoding="utf-8")
create_secret("broker_user", "messenger")
create_secret("broker_password", secrets.token_hex(32))
redis_password = create_secret("redis_password", secrets.token_hex(32))
cache_url = f"rediss://messenger:{redis_password}@redis:6379/0"
create_secret("redis_acl", "user default off\nuser messenger on >" + redis_password
+ " ~messenger:* +ping +get +set +del +incr +expire +ttl +eval +evalsha +hello +client|setinfo")
for name, content in (
("DATABASE_URL_FILE", database_url),
("CACHE_URL_FILE", "redis://redis:6379/0"),
("BROKER_URL_FILE", broker_url),
("S3_ACCESS_KEY_ID_FILE", secrets.token_hex(12)),
("S3_SECRET_ACCESS_KEY_FILE", secrets.token_hex(32)),
("database_tls_url", database_url),
("cache_tls_url", cache_url),
):
container_path = Path(values[name])
if container_path.parent != Path("/run/secrets") or not container_path.name:
raise SystemExit(f"{name} must name a file directly inside /run/secrets")
target = (
secret_dir
/ {
"DATABASE_URL_FILE": "database_url",
"CACHE_URL_FILE": "cache_url",
"BROKER_URL_FILE": "broker_url",
"S3_ACCESS_KEY_ID_FILE": "s3_access_key_id",
"S3_SECRET_ACCESS_KEY_FILE": "s3_secret_access_key",
}[name]
)
if not target.exists():
# Containers run as an unprivileged user; the private host directory
# protects the files, while read permission lets the API consume them.
with target.open("x", encoding="utf-8") as file:
file.write(content + "\n")
os.chmod(target, 0o644)
# Existing S3 files remain the root credentials; API gets a new, separate identity.
for filename, content in (
("s3_api_access_key_id", secrets.token_hex(12)),
("s3_api_secret_access_key", secrets.token_hex(32)),
):
target = secret_dir / filename
if not target.exists():
with target.open("x", encoding="utf-8") as file:
file.write(content + "\n")
os.chmod(target, 0o644)
print("Local .env and connection files ready; existing values and files preserved.")
create_secret(name, content)
create_secret("s3_access_key_id", secrets.token_hex(12))
create_secret("s3_secret_access_key", secrets.token_hex(32))
database = quote(values["POSTGRES_DB"], safe="")
for role in ("api", "worker", "dispatcher", "cleanup"):
password = create_secret(f"database_{role}_password", secrets.token_hex(24))
create_secret(f"database_{role}_url",
f"postgres://messenger_{role}:{password}@db:5432/{database}"
"?sslmode=verify-full&sslrootcert=/run/tls/ca.crt")
if role in ("worker", "dispatcher"):
password = create_secret(f"broker_{role}_password", secrets.token_hex(24))
create_secret(f"broker_{role}_url", f"amqps://messenger_{role}:{password}@broker:5671/%2F")
if role != "dispatcher":
create_secret(f"s3_{role}_access_key_id", secrets.token_hex(12))
create_secret(f"s3_{role}_secret_access_key", secrets.token_hex(32))
print("Local connection files ready; existing values and files preserved.")
if __name__ == "__main__":