51 lines
2.4 KiB
Python
51 lines
2.4 KiB
Python
"""Настройка ролей и начальная загрузка секретов из файлов оператора."""
|
|
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)
|