forked from mtusi2026/mtusi2026lab7
ЛР7
This commit is contained in:
+252
@@ -0,0 +1,252 @@
|
||||
"""OpenBao operator tool for the lab7 stand (Python standard library only).
|
||||
|
||||
Runs in the short-lived "ops" container (make bao-init / bao-unseal / bao-bootstrap / ...).
|
||||
It is the ONLY place that reads the operator material in secrets/operator/:
|
||||
unseal-key-1..3 Shamir shares (threshold 2). In real life: three different people/places.
|
||||
root-token initial root token (used only by these operator commands)
|
||||
The files are created with mode 600, never mounted into services, never printed.
|
||||
|
||||
Commands:
|
||||
status initialized / sealed / version (no token needed)
|
||||
init operator init (3 shares, threshold 2); refuses to run twice
|
||||
unseal submit shares until unsealed (needed after every OpenBao restart)
|
||||
seal seal OpenBao (incident drill: sealed OpenBao)
|
||||
bootstrap idempotent: kv-v2 at secret/, cert auth, audit device, policies,
|
||||
cert roles for api/worker, application secrets from secrets/*
|
||||
rotate-db new password for DB role messenger_api: ALTER ROLE + new KV version
|
||||
revoke-tokens revoke every token issued by the cert auth method (forces re-login)
|
||||
token-ttl ROLE TTL change token TTL of a cert role (e.g. expiry drill: api 30s)
|
||||
|
||||
Plain ASCII on purpose (scripts/check_contract.py reads scripts/*.py with the default encoding).
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import secrets as rnd
|
||||
import ssl
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SECRETS = Path(os.environ.get("SECRETS_DIR", ROOT / "secrets"))
|
||||
OPERATOR = SECRETS / "operator"
|
||||
POLICIES = Path(os.environ.get("BAO_POLICY_DIR", ROOT / "infra" / "openbao" / "policies"))
|
||||
ADDR = os.environ.get("BAO_ADDR", "https://openbao:8200").rstrip("/")
|
||||
CACERT = os.environ.get("BAO_CACERT", str(SECRETS / "tls" / "ca" / "ca.crt"))
|
||||
SHARES, THRESHOLD = 3, 2
|
||||
|
||||
# role -> (certificate CN, policy, secret path). One policy per service, nothing shared.
|
||||
ROLES = {"messenger-api": ("api", "messenger-api", "messenger/api"),
|
||||
"messenger-worker": ("worker", "messenger-worker", "messenger/worker")}
|
||||
TOKEN_TTL, TOKEN_MAX_TTL = os.environ.get("BAO_TOKEN_TTL", "15m"), os.environ.get("BAO_TOKEN_MAX_TTL", "1h")
|
||||
|
||||
CTX = ssl.create_default_context(cafile=CACERT) # server certificate and hostname are verified
|
||||
|
||||
|
||||
class BaoError(Exception):
|
||||
def __init__(self, status, errors):
|
||||
super().__init__(f"HTTP {status}: {'; '.join(errors) or 'no details'}")
|
||||
self.status = status
|
||||
|
||||
|
||||
def call(method, path, body=None, token=None, ok=(200, 204)):
|
||||
data = None if body is None else json.dumps(body).encode()
|
||||
request = urllib.request.Request(ADDR + "/v1/" + path.lstrip("/"), data=data, method=method)
|
||||
request.add_header("Content-Type", "application/json")
|
||||
if token:
|
||||
request.add_header("X-Vault-Token", token)
|
||||
try:
|
||||
with urllib.request.urlopen(request, context=CTX, timeout=10) as response:
|
||||
raw = response.read()
|
||||
status = response.status
|
||||
except urllib.error.HTTPError as exc:
|
||||
raw, status = exc.read(), exc.code
|
||||
payload = json.loads(raw) if raw.strip() else {}
|
||||
if status not in ok:
|
||||
# Error bodies of OpenBao contain no secret values; tokens are never echoed.
|
||||
raise BaoError(status, payload.get("errors", []))
|
||||
return payload
|
||||
|
||||
|
||||
def secret_file(name: str) -> str:
|
||||
return (SECRETS / name).read_text(encoding="utf-8").strip()
|
||||
|
||||
|
||||
def write_private(path: Path, value: str):
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
os.chmod(path.parent, 0o700)
|
||||
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
||||
with os.fdopen(fd, "w") as fh:
|
||||
fh.write(value)
|
||||
|
||||
|
||||
def root_token() -> str:
|
||||
path = OPERATOR / "root-token"
|
||||
if not path.exists():
|
||||
sys.exit("secrets/operator/root-token not found: run make bao-init first")
|
||||
return path.read_text().strip()
|
||||
|
||||
|
||||
def cmd_status(_args):
|
||||
health = call("GET", "sys/seal-status")
|
||||
print(f"initialized={health['initialized']} sealed={health['sealed']} "
|
||||
f"progress={health.get('progress', 0)}/{health.get('t', 0)} version={health.get('version')} "
|
||||
f"storage={health.get('storage_type')}")
|
||||
return 0 if not health["sealed"] else 2
|
||||
|
||||
|
||||
def cmd_init(_args):
|
||||
if call("GET", "sys/init")["initialized"]:
|
||||
print("OpenBao already initialized: nothing to do (shares and root token stay with the operator)")
|
||||
return 0
|
||||
result = call("PUT", "sys/init", {"secret_shares": SHARES, "secret_threshold": THRESHOLD})
|
||||
for number, share in enumerate(result["keys_base64"], start=1):
|
||||
write_private(OPERATOR / f"unseal-key-{number}", share)
|
||||
write_private(OPERATOR / "root-token", result["root_token"])
|
||||
print(f"initialized: {SHARES} unseal shares (threshold {THRESHOLD}) and root token saved to secrets/operator/ "
|
||||
"(mode 600, not printed). Keep the shares apart: in production every share goes to a different person.")
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_unseal(_args):
|
||||
status = call("GET", "sys/seal-status")
|
||||
if not status["initialized"]:
|
||||
sys.exit("not initialized: run make bao-init")
|
||||
if not status["sealed"]:
|
||||
print("already unsealed")
|
||||
return 0
|
||||
keys = sorted(OPERATOR.glob("unseal-key-*"))
|
||||
if len(keys) < THRESHOLD:
|
||||
sys.exit(f"need {THRESHOLD} shares in secrets/operator/unseal-key-N")
|
||||
for path in keys[:THRESHOLD]:
|
||||
status = call("PUT", "sys/unseal", {"key": path.read_text().strip()})
|
||||
print(f"sealed={status['sealed']} (used {THRESHOLD} of {len(keys)} shares)")
|
||||
return 0 if not status["sealed"] else 1
|
||||
|
||||
|
||||
def cmd_seal(_args):
|
||||
call("PUT", "sys/seal", token=root_token())
|
||||
print("sealed: services keep their cached secrets, new logins and reads fail until make bao-unseal")
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_bootstrap(_args):
|
||||
token = root_token()
|
||||
if "secret/" not in call("GET", "sys/mounts", token=token)["data"]:
|
||||
call("POST", "sys/mounts/secret", {"type": "kv", "options": {"version": "2"}}, token=token)
|
||||
print("enabled kv-v2 at secret/")
|
||||
if "cert/" not in call("GET", "sys/auth", token=token)["data"]:
|
||||
call("POST", "sys/auth/cert", {"type": "cert"}, token=token)
|
||||
print("enabled cert auth at auth/cert/")
|
||||
# Audit device is declared in infra/openbao/bao.hcl: OpenBao 2.4 refuses to enable it via the API.
|
||||
ca_pem = Path(CACERT).read_text()
|
||||
for role, (common_name, policy, secret_path) in ROLES.items():
|
||||
call("PUT", f"sys/policies/acl/{policy}",
|
||||
{"policy": (POLICIES / f"{policy}.hcl").read_text(encoding="utf-8")}, token=token)
|
||||
call("POST", f"auth/cert/certs/{role}", {
|
||||
"display_name": role,
|
||||
"certificate": ca_pem, # trust anchor: our CA only
|
||||
"allowed_common_names": [common_name], # and only this identity
|
||||
"token_policies": [policy],
|
||||
"token_ttl": TOKEN_TTL, "token_max_ttl": TOKEN_MAX_TTL,
|
||||
"token_no_default_policy": False, # default policy = lookup-self / renew-self only
|
||||
}, token=token)
|
||||
put_kv(token, "messenger/api", {
|
||||
"db_password": secret_file("db_api_password"),
|
||||
"s3_access_key_id": secret_file("s3_api_access_key_id"),
|
||||
"s3_secret_access_key": secret_file("s3_api_secret_access_key"),
|
||||
"cache_password": secret_file("valkey_api_password"),
|
||||
})
|
||||
put_kv(token, "messenger/worker", {
|
||||
"db_password": secret_file("db_worker_password"),
|
||||
"s3_access_key_id": secret_file("s3_worker_access_key_id"),
|
||||
"s3_secret_access_key": secret_file("s3_worker_secret_access_key"),
|
||||
})
|
||||
print(f"policies messenger-api, messenger-worker; cert roles (CN api / worker, token ttl {TOKEN_TTL}, "
|
||||
f"max {TOKEN_MAX_TTL}); secrets secret/messenger/api and secret/messenger/worker written (values not printed)")
|
||||
return 0
|
||||
|
||||
|
||||
def put_kv(token, path, data):
|
||||
current = None
|
||||
try:
|
||||
current = call("GET", f"secret/data/{path}", token=token)["data"]["data"]
|
||||
except BaoError as exc:
|
||||
if exc.status != 404:
|
||||
raise
|
||||
if current != data:
|
||||
call("POST", f"secret/data/{path}", {"data": data}, token=token)
|
||||
|
||||
|
||||
def cmd_rotate_db(_args):
|
||||
"""Rotate the password of DB role messenger_api without rebuilding images or restarting the API.
|
||||
|
||||
Order: 1) ALTER ROLE in PostgreSQL, 2) new KV version in OpenBao, 3) the operator copy.
|
||||
Between 1) and the next API refresh (BAO_REFRESH_SECONDS) NEW DB connections of the API fail
|
||||
and are retried; connections that are already open keep working (PostgreSQL checks the password
|
||||
only at login). The API drains its pool when it sees the new value, so every connection after
|
||||
that is a new login with the new password.
|
||||
"""
|
||||
import psycopg # available in the application image the ops container runs from
|
||||
from psycopg import sql
|
||||
token = root_token()
|
||||
new = rnd.token_hex(24)
|
||||
owner = secret_file("postgres_password")
|
||||
with psycopg.connect(host=os.environ.get("DB_HOST", "postgres"), port=int(os.environ.get("DB_PORT", "5432")),
|
||||
dbname="messenger", user="messenger", password=owner,
|
||||
sslmode="verify-full", sslrootcert=CACERT, connect_timeout=5, autocommit=True) as conn:
|
||||
conn.execute(sql.SQL("ALTER ROLE messenger_api PASSWORD {}").format(sql.Literal(new)))
|
||||
data = call("GET", "secret/data/messenger/api", token=token)["data"]["data"]
|
||||
data["db_password"] = new
|
||||
result = call("POST", "secret/data/messenger/api", {"data": data}, token=token)
|
||||
(SECRETS / "db_api_password").write_text(new)
|
||||
print(f"rotated DB password of messenger_api: PostgreSQL updated, KV secret/messenger/api now version "
|
||||
f"{result['data']['version']} (value not printed)")
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_revoke_tokens(_args):
|
||||
call("PUT", "sys/leases/revoke-prefix/auth/cert/login", token=root_token())
|
||||
print("revoked all tokens issued by auth/cert: services must log in again with their certificates")
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_token_ttl(args):
|
||||
if len(args) != 2 or args[0] not in ("api", "worker"):
|
||||
sys.exit("usage: token-ttl api|worker TTL (e.g. 30s, 15m)")
|
||||
role = f"messenger-{args[0]}"
|
||||
token = root_token()
|
||||
current = call("GET", f"auth/cert/certs/{role}", token=token)["data"]
|
||||
max_ttl = args[1] if args[1].endswith("s") else TOKEN_MAX_TTL
|
||||
call("POST", f"auth/cert/certs/{role}", {
|
||||
"display_name": role, "certificate": current["certificate"],
|
||||
"allowed_common_names": current["allowed_common_names"], "token_policies": current["token_policies"],
|
||||
"token_ttl": args[1], "token_max_ttl": max_ttl}, token=token)
|
||||
print(f"{role}: token_ttl={args[1]} token_max_ttl={max_ttl} (applies to NEW logins)")
|
||||
return 0
|
||||
|
||||
|
||||
COMMANDS = {"status": cmd_status, "init": cmd_init, "unseal": cmd_unseal, "seal": cmd_seal,
|
||||
"bootstrap": cmd_bootstrap, "rotate-db": cmd_rotate_db, "revoke-tokens": cmd_revoke_tokens,
|
||||
"token-ttl": cmd_token_ttl}
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2 or sys.argv[1] not in COMMANDS:
|
||||
sys.exit("usage: bao.py " + "|".join(COMMANDS))
|
||||
deadline = time.monotonic() + 60
|
||||
while True: # OpenBao may still be starting after docker compose up
|
||||
try:
|
||||
return COMMANDS[sys.argv[1]](sys.argv[2:])
|
||||
except (urllib.error.URLError, ConnectionError, TimeoutError) as exc:
|
||||
if time.monotonic() > deadline:
|
||||
sys.exit(f"OpenBao at {ADDR} is not reachable: {getattr(exc, 'reason', exc)}")
|
||||
time.sleep(2)
|
||||
except BaoError as exc:
|
||||
sys.exit(f"OpenBao refused: {exc}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user