Files
infrastructure/scripts/ci/secret_scan.py
T
spektr f48f831e82 M5: statische CI ohne Serverzugang (Gitea Actions) + 2 gebrochene Doku-Links fixen
- .gitea/workflows/ci-static.yml: Jobs secret-scan, yamllint, ansible-syntax,
  markdown-links; Job-Image python:3.13-bookworm per Digest gepinnt
- ci/requirements-ci.txt (ansible-core==2.21.4 wie Control-Node, yamllint==1.37.0),
  .yamllint, scripts/ci/{secret_scan.py,check_links.py,check_ansible_syntax.sh}
- lokal gegen das Repo geprueft: alle 4 Checks gruen
- Linkfixes: GNENC-001 (../ -> ../../), GNAI-018 (ein ../ zu viel)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-11-11 05:41:40 +01:00

61 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""Secret-Scan fuer die statische CI (GNAI-001 M5).
Prueft alle versionierten Dateien auf starke Secret-Muster:
Private-Key-Bloecke, AWS-Access-Key-IDs, WireGuard-PSK-artige Base64-Werte
(44 Zeichen, analog GNAI-013). Befunde werden mit Datei:Zeile gemeldet,
ausser erklaerende Beispieltexte (siehe GNAI-013 Abschnitt 4).
"""
import re
import subprocess
import sys
PATTERNS = [
("private-key-block", re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----")),
("aws-access-key", re.compile(r"\bAKIA[0-9A-Z]{16}\b")),
("wg-psk-like", re.compile(r"(?i)(preshared_?key|psk)\s*[:=]\s*[\"']?[A-Za-z0-9+/]{43}=[\"']?")),
("generic-secret-yaml", re.compile(r"(?i)^\s*(password|secret|client_secret|api_key)\s*:\s*[\"']?[^\s{\"'][^\"']{7,}[\"']?\s*$", re.M)),
]
# Erlaubte Beispiel-/Platzhalterwerte (dokumentierte Ausnahmen aus GNAI-013).
ALLOWLIST = ("<", "…", "…")
def tracked_files() -> list[str]:
out = subprocess.run(["git", "ls-files", "-z"], capture_output=True, check=True)
return [
f
for f in out.stdout.decode("utf-8", "surrogateescape").split("\0")
if f and not f.startswith((".venv/", ".ansible/"))
]
def main() -> int:
findings = []
for path in tracked_files():
try:
with open(path, "rb") as fh:
data = fh.read()
except OSError:
continue
if b"\x00" in data[:4096]:
continue # Binärdatei
text = data.decode("utf-8", "replace")
for lineno, line in enumerate(text.splitlines(), 1):
if any(a in line for a in ALLOWLIST) and "PRIVATE KEY" not in line:
continue
for name, rx in PATTERNS:
if rx.search(line):
findings.append(f"{path}:{lineno}: {name}: {line.strip()[:80]}")
if findings:
print("SECRET-SCAN: Befunde gefunden:")
for f in findings:
print(" ", f)
return 1
print(f"SECRET-SCAN: OK ({len(tracked_files())} Dateien geprüft)")
return 0
if __name__ == "__main__":
sys.exit(main())