From 205cb1585ad34eaa1b73579fba72c063fae90e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 28 Nov 2026 18:59:17 +0300 Subject: [PATCH] feat(openbao): lab7 configure service roles and seed KV secrets --- scripts/configure-openbao.py | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 scripts/configure-openbao.py diff --git a/scripts/configure-openbao.py b/scripts/configure-openbao.py new file mode 100644 index 0000000..c4c5b75 --- /dev/null +++ b/scripts/configure-openbao.py @@ -0,0 +1,50 @@ +"""Настройка ролей и начальная загрузка секретов из файлов оператора.""" +import getpass +import json +import ssl +import urllib.error +import urllib.request +from pathlib import Path + +root = Path(__file__).resolve().parents[1] +private = root / "certs/private" +tls = ssl.create_default_context(cafile=str(private / "ingress/ca.crt")) +token = getpass.getpass("Токен оператора OpenBao: ") +roles = { + "api": {"db_api": "postgres/api.url", "cache_url": "redis/api.url", + "rabbit_api": "rabbitmq/api.url", "s3_api": "minio/api.secret"}, + "worker": {"db_worker": "postgres/worker.url", "rabbit_worker": "rabbitmq/worker.url", + "s3_worker": "minio/worker.secret"}, + "reconciler": {"db_reconciler": "postgres/reconciler.url", + "s3_reconciler": "minio/reconciler.secret"}, +} + + +def call(path, body=None, optional=False): + data = None if body is None else json.dumps(body).encode() + req = urllib.request.Request("https://127.0.0.1:18200/v1/" + path, data=data, + headers={"X-Vault-Token": token, "Content-Type": "application/json"}) + try: + with urllib.request.urlopen(req, context=tls, timeout=10) as response: + return json.loads(response.read() or b"{}") + except urllib.error.HTTPError as error: + if optional and error.code == 404: + return None + raise SystemExit(f"Операция {path} завершилась с HTTP {error.code}") from None + + +if "lab7/" not in call("sys/mounts")["data"]: + call("sys/mounts/lab7", {"type": "kv", "options": {"version": "2"}}) +if "cert/" not in call("sys/auth")["data"]: + call("sys/auth/cert", {"type": "cert"}) +for role, fields in roles.items(): + policy = "lab7-" + role + rule = 'path "lab7/data/%s" { capabilities = ["read"] }' % role + call("sys/policies/acl/" + policy, {"policy": rule}) + cert = (private / "telemetry" / (role + "-client.crt")).read_text() + call("auth/cert/certs/" + role, {"certificate": cert, "token_policies": [policy], + "token_ttl": "2m", "token_max_ttl": "5m"}) + if call("lab7/metadata/" + role, optional=True) is None: + values = {key: (private / path).read_text().strip() for key, path in fields.items()} + call("lab7/data/" + role, {"options": {"cas": 0}, "data": values}) + print("Настроена роль", role)