#!/usr/bin/env python3 """Проверка перед commit (lab7): значения секретов не встречаются ни в одном файле репозитория вне secrets/ и .env. Значения не печатаются — только имена файлов. Источники значений: * secrets/**: пароли, ключи S3, password_hash RabbitMQ, ЗАКРЫТЫЕ КЛЮЧИ leaf (тело PEM по строкам); * $OPERATOR_DIR (по умолчанию ~/.mtusi2026lab7-operator): закрытые ключи CA, root token и unseal shares OpenBao (init.json). Публичные сертификаты (*.crt) секретом не являются и не проверяются. python3 scripts/scan_secrets.py (код 0 — чисто)""" import json import os import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OPERATOR_DIR = Path(os.environ.get("OPERATOR_DIR", Path.home() / ".mtusi2026lab7-operator")) SKIP_DIRS = {".git", "secrets", ".venv", "__pycache__", "node_modules"} values = set() def add_pem(text): for line in text.splitlines(): line = line.strip() if len(line) >= 16 and not line.startswith("-----"): values.add(line) def walk(x): if isinstance(x, dict): for k, v in x.items(): if k in ("accessKey", "secretKey", "password_hash", "root_token", "unseal_keys_b64", "unseal_keys_hex"): for item in (v if isinstance(v, list) else [v]): values.add(str(item)) walk(v) elif isinstance(x, list): for v in x: walk(v) sources = [f for f in (ROOT / "secrets").rglob("*") if f.is_file() and f.suffix != ".crt"] sources += [f for f in OPERATOR_DIR.rglob("*") if f.is_file()] if OPERATOR_DIR.is_dir() else [] for f in sources: text = f.read_text(encoding="utf-8", errors="replace").strip() if f.suffix == ".json": walk(json.loads(text)) elif "PRIVATE KEY" in text: add_pem(text) elif len(text) >= 8 and "\n" not in text: values.add(text) if "://" in text and "@" in text: values.add(text.split("://", 1)[1].split("@", 1)[0].split(":", 1)[-1]) hits = [] for p in ROOT.rglob("*"): if p.is_dir() or set(p.relative_to(ROOT).parts) & SKIP_DIRS or p.name == ".env": continue try: data = p.read_text(encoding="utf-8", errors="ignore") except OSError: continue if any(v in data for v in values): hits.append(str(p.relative_to(ROOT))) print("checked %d secret values from %d files; files with leaks: %s" % (len(values), len(sources), hits or "none")) sys.exit(1 if hits else 0)