forked from mtusi2026/mtusi2026lab7
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
#!/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()
|