Files
mtusi2026lab7/scripts/pki_identities.py
T
eqktrinandO3 Agent Claude 9867faa285 ЛР7: TLS/mTLS на всех границах, OpenBao вне dev-mode, least privilege
- внутренняя PKI: root -> ca-servers / ca-clients, leaf на каждый сервис;
  pki/, secrets/, bootstrap/ не коммитятся и не попадают в образ
- внешний HTTPS :8443 с проверкой CA и hostname (IP намеренно не в SAN)
- обязательный mTLS LB -> каждая API-реплика: relay проверяет цепочку,
  CN и серийник (identities.json) и scope; uvicorn слушает только loopback
- mTLS к RabbitMQ (TLS + EXTERNAL auth, CN -> user, ACL topology) и к
  OTLP-ingress (nginx: allowlist CN, Collector доступен только из otel)
- TLS к PostgreSQL (verify-full), S3, OpenBao, Loki/Tempo, Grafana
- OpenBao: file storage + volume, AppRole с secret_id 24h и token 20m,
  scoped KV-policy; root token/unseal только у оператора
- раздельные DB-роли и S3-ключи с prefix-rights; read_only/cap_drop,
  stateful-исключения задокументированы
- rate limit входа в PostgreSQL (общий для реплик), Argon2id + rehash,
  семафор хеширования; ротация/отзыв leaf и рабочего секрета без
  пересборки образов

Co-Authored-By: O3 Agent Claude <noreply@ozon.ru>
2026-11-27 19:58:28 +03:00

48 lines
1.4 KiB
Python

#!/usr/bin/env python3
"""Build pki/identities.json: which mTLS client identities are allowed and where.
The file is consumed by:
* app/relay.py — TLS relay in front of each API replica (LB -> API mTLS);
* the trust-matrix audit script — prints the effective allowlist.
Format:
{"<cn>": {"cn": "<cn>", "serials": ["<HEX>", ...], "scope": "/"|"/health/", "leaf": "<name>"}}
`scope` is the URL prefix the identity may reach on the business listener.
"""
import json
import pathlib
import ssl
import sys
ALLOWED = {
"ingress-client": "/",
"health-client": "/health/",
}
def cert_info(path):
decoded = ssl._ssl._test_decode_cert(str(path)) # noqa: SLF001 - stdlib helper
cn = dict(x[0] for x in decoded["subject"]).get("commonName", "")
serial = decoded["serialNumber"]
# Python versions disagree: int on some builds, hex string on others.
serial = format(serial, "X") if isinstance(serial, int) else str(serial).upper().replace("0X", "", 1)
return cn, serial
def main():
pki = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else "pki")
out = {}
for leaf, scope in ALLOWED.items():
path = pki / "leaf" / (leaf + ".crt")
if not path.exists():
continue
cn, serial = cert_info(path)
out[cn] = {"cn": cn, "serials": [serial], "scope": scope, "leaf": leaf}
json.dump(out, sys.stdout, indent=2, sort_keys=True)
sys.stdout.write("\n")
if __name__ == "__main__":
main()