[+] add project
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-12-05 22:05:31 +03:00
parent 20afc2c790
commit 1d146756d7
63 changed files with 3501 additions and 149 deletions
+51
View File
@@ -0,0 +1,51 @@
import json
import ssl
from pathlib import Path
from urllib.request import Request, urlopen
from .config import Settings
def _post_json(url: str, payload: dict, ca: str) -> dict:
context = ssl.create_default_context(cafile=ca)
req = Request(url, data=json.dumps(payload).encode("utf-8"),
headers={"Content-Type": "application/json"}, method="POST")
with urlopen(req, context=context, timeout=10) as response:
return json.loads(response.read())
def login(settings: Settings, role: str) -> dict:
"""Authenticate via the role-scoped AppRole and fetch only that role's KV path."""
if not settings.bao_addr or not settings.bao_cacert:
raise RuntimeError("OpenBao address and TLS CA are required")
if not settings.bao_role_id or not settings.bao_secret_id:
raise RuntimeError("OpenBao AppRole role_id and secret_id are required")
ca = settings.bao_cacert
if not Path(ca).is_file():
raise RuntimeError("OpenBao TLS CA file is unavailable")
login = _post_json(
settings.bao_addr.rstrip("/") + "/v1/auth/approle/login",
{"role_id": settings.bao_role_id, "secret_id": settings.bao_secret_id},
ca,
)
token = login.get("auth", {}).get("client_token")
if not token:
raise RuntimeError("OpenBao AppRole authentication did not return a scoped token")
path = settings.bao_api_secret_path if role == "api" else settings.bao_worker_secret_path
context = ssl.create_default_context(cafile=ca)
req = Request(settings.bao_addr.rstrip("/") + "/v1/" + path.lstrip("v1/"),
headers={"X-Vault-Token": token})
with urlopen(req, context=context, timeout=10) as response:
envelope = json.loads(response.read())
data = envelope.get("data", {})
secrets = data.get("data", data)
if not isinstance(secrets, dict):
raise RuntimeError("OpenBao returned an invalid scoped secret envelope")
if role == "api":
required = ("database_url", "broker_url", "s3_access_key", "s3_secret_key")
else:
required = ("database_url", "broker_url", "s3_access_key", "s3_secret_key")
missing = [key for key in required if not secrets.get(key)]
if missing:
raise RuntimeError("OpenBao scoped secret path is missing required credentials")
return {"token": token, "secrets": secrets}