forked from mtusi2026/mtusi2026lab7
Защитить мессенджер TLS и OpenBao и проверить отзыв прав worker
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate local operator credentials and TLS files for this stand."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import secrets
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import pki
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SECRETS = ROOT / "secrets"
|
||||
ROLES = ("api", "worker", "relay", "reconciler")
|
||||
S3_ROLES = ("api", "worker", "reconciler", "test")
|
||||
|
||||
|
||||
def ensure(name, value):
|
||||
path = SECRETS / name
|
||||
if not path.exists():
|
||||
path.write_text(value + "\n")
|
||||
path.chmod(0o644)
|
||||
return path.read_text().strip()
|
||||
|
||||
|
||||
def initialize():
|
||||
os.umask(0o077)
|
||||
SECRETS.mkdir(mode=0o700, exist_ok=True)
|
||||
pki.init_ca()
|
||||
for name in (*pki.SERVERS, *pki.CLIENTS):
|
||||
pki.issue(name)
|
||||
env = ROOT / ".env"
|
||||
if not env.exists():
|
||||
shutil.copyfile(ROOT / ".env.example", env)
|
||||
for name in ("postgres_password", "grafana_password", "s3_root_password"):
|
||||
ensure(name, secrets.token_hex(24))
|
||||
ensure("s3_root_user", "operator-" + secrets.token_hex(8))
|
||||
for role in S3_ROLES:
|
||||
ensure(f"s3_{role}_access_key_id", "svc-" + secrets.token_hex(8))
|
||||
ensure(f"s3_{role}_secret_access_key", secrets.token_hex(24))
|
||||
for role in ROLES:
|
||||
for store in ("db", "cache", "broker"):
|
||||
ensure(f"{store}_{role}_password", secrets.token_hex(24))
|
||||
write_acls()
|
||||
provision_datasources()
|
||||
print("Local PKI, credentials and provisioning ready.")
|
||||
|
||||
|
||||
def write_acls():
|
||||
cache_ops = "reset on ~session-user:* +get +set +del +ping +hello +client|setinfo"
|
||||
broker_shared = "+ping +hello +client|setinfo"
|
||||
worker_ops = (
|
||||
"~attachment-jobs +xreadgroup +xautoclaim +xgroup|create "
|
||||
"+xack +xdel +eval +multi +exec (~invalid-jobs +xadd)"
|
||||
)
|
||||
for store, rules in {
|
||||
"cache": {"api": cache_ops, "operator": "on ~* +@all"},
|
||||
"broker": {
|
||||
"api": f"on {broker_shared}",
|
||||
"relay": f"on ~attachment-jobs +xadd {broker_shared}",
|
||||
"worker": f"on {worker_ops} {broker_shared}",
|
||||
"operator": "on ~* +@all",
|
||||
},
|
||||
}.items():
|
||||
lines = ["user default off"]
|
||||
for role, permissions in rules.items():
|
||||
password = ensure(f"{store}_{role}_password", secrets.token_hex(24))
|
||||
lines.append(f"user {role} {permissions} >{password}")
|
||||
target = SECRETS / f"{store}.acl"
|
||||
target.write_text("\n".join(lines) + "\n")
|
||||
target.chmod(0o644)
|
||||
|
||||
|
||||
def provision_datasources():
|
||||
trust = (pki.TLS / "trust" / "ca.crt").read_text()
|
||||
identity = pki.TLS / "grafana-client"
|
||||
secure = {
|
||||
"tlsCACert": trust,
|
||||
"tlsClientCert": (identity / "service.crt").read_text(),
|
||||
"tlsClientKey": (identity / "service.key").read_text(),
|
||||
}
|
||||
datasources = []
|
||||
for name, port in (("prometheus", 9090), ("loki", 3100), ("tempo", 3200)):
|
||||
config = {"tlsAuth": True, "tlsAuthWithCACert": True}
|
||||
if name == "loki":
|
||||
config["derivedFields"] = [
|
||||
{
|
||||
"name": "Trace",
|
||||
"matcherType": "label",
|
||||
"matcherRegex": "trace_id",
|
||||
"datasourceUid": "tempo",
|
||||
"url": "$${__value.raw}",
|
||||
}
|
||||
]
|
||||
if name == "tempo":
|
||||
config["tracesToLogsV2"] = {
|
||||
"datasourceUid": "loki",
|
||||
"spanStartTimeShift": "-2m",
|
||||
"spanEndTimeShift": "2m",
|
||||
"filterByTraceID": True,
|
||||
"tags": [{"key": "service.name", "value": "service_name"}],
|
||||
}
|
||||
datasources.append(
|
||||
{
|
||||
"name": name.title(),
|
||||
"uid": name,
|
||||
"type": name,
|
||||
"access": "proxy",
|
||||
"url": f"https://{name}:{port}",
|
||||
"editable": False,
|
||||
"isDefault": name == "prometheus",
|
||||
"jsonData": config,
|
||||
"secureJsonData": secure,
|
||||
}
|
||||
)
|
||||
directory = SECRETS / "grafana-datasources"
|
||||
directory.mkdir(exist_ok=True)
|
||||
directory.chmod(0o755)
|
||||
target = directory / "local.yaml"
|
||||
target.write_text(json.dumps({"apiVersion": 1, "datasources": datasources}, indent=2))
|
||||
target.chmod(0o644)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
initialize()
|
||||
Reference in New Issue
Block a user