Implement Lab 7 security and 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,49 @@
|
||||
"""Fetch narrowly scoped runtime configuration from OpenBao AppRole."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
def _read(path_env: str) -> str:
|
||||
path = os.getenv(path_env, "")
|
||||
if not path:
|
||||
raise RuntimeError(f"{path_env} is required when BAO_ENABLED=1")
|
||||
return Path(path).read_text(encoding="utf-8").strip()
|
||||
|
||||
|
||||
def load_service_secrets() -> None:
|
||||
if os.getenv("BAO_ENABLED", "0").lower() not in {"1", "true", "yes"}:
|
||||
return
|
||||
|
||||
address = os.environ["BAO_ADDR"].rstrip("/")
|
||||
ca_file = os.environ["BAO_CACERT"]
|
||||
role_id = _read("BAO_ROLE_ID_FILE")
|
||||
secret_id = _read("BAO_SECRET_ID_FILE")
|
||||
secret_path = os.environ["BAO_SECRET_PATH"].strip("/")
|
||||
|
||||
session = requests.Session()
|
||||
session.verify = ca_file
|
||||
response = session.post(
|
||||
f"{address}/v1/auth/approle/login",
|
||||
json={"role_id": role_id, "secret_id": secret_id},
|
||||
timeout=5,
|
||||
)
|
||||
response.raise_for_status()
|
||||
token = response.json()["auth"]["client_token"]
|
||||
|
||||
response = session.get(
|
||||
f"{address}/v1/secret/data/{secret_path}",
|
||||
headers={"X-Vault-Token": token},
|
||||
timeout=5,
|
||||
)
|
||||
response.raise_for_status()
|
||||
values = response.json()["data"]["data"]
|
||||
|
||||
allowed = {
|
||||
"DATABASE_URL", "CACHE_URL", "S3_ENDPOINT", "S3_ACCESS_KEY",
|
||||
"S3_SECRET_KEY", "S3_PUBLIC_ENDPOINT_URL", "RABBITMQ_URL", "OTEL_EXPORTER_OTLP_ENDPOINT",
|
||||
}
|
||||
for key, value in values.items():
|
||||
if key in allowed and isinstance(value, str):
|
||||
os.environ[key] = value
|
||||
Reference in New Issue
Block a user