Complete lab 7 security infrastructure

This commit is contained in:
Yuliiiaaaa
2026-11-30 21:23:07 +03:00
parent 20afc2c790
commit c00d245263
55 changed files with 3515 additions and 28 deletions
+42
View File
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
"""Stage one service's AppRole files in tmpfs, then drop to the app UID."""
from __future__ import annotations
import os
from pathlib import Path
import sys
APP_UID = 10001
APP_GID = 10001
SOURCE = Path("/run/secrets-src")
TARGET = Path("/run/secrets")
def main() -> None:
if os.geteuid() != 0:
raise SystemExit("Container entrypoint must start as root only to stage read-only AppRole files")
TARGET.mkdir(parents=True, exist_ok=True, mode=0o700)
for name in ("role_id", "secret_id"):
source = SOURCE / name
value = source.read_bytes()
if not value.strip():
raise SystemExit(f"Empty AppRole file: {name}")
target = TARGET / name
target.write_bytes(value)
os.chmod(target, 0o400)
os.chown(target, APP_UID, APP_GID)
os.chmod(TARGET, 0o500)
os.chown(TARGET, APP_UID, APP_GID)
os.setgroups([])
os.setgid(APP_GID)
os.setuid(APP_UID)
command = sys.argv[1:] or [
"uvicorn", "src.app:app", "--host", "127.0.0.1", "--port", "8080",
"--limit-concurrency", "64", "--timeout-keep-alive", "5",
]
os.execvp(command[0], command)
if __name__ == "__main__":
main()