Complete lab 7 security infrastructure
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -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()
|
||||
Reference in New Issue
Block a user