Files
AiSOC/scripts/support_bundle.py
T
Beenu AroraandBeenu Arora e78f31768c Move all fourteen ruff declarations to 0.16, and stop the lint gate at services/ (#861)
* Reformat the tree under ruff 0.16, excluding the subtree nobody may edit

Mechanical. `ruff format` with no other change, so the diff can be read as
"the formatter moved" rather than reviewed line by line: 218 files, 862
insertions against 1075 deletions, almost all of it collapsing calls that
0.4.10 wrapped and 0.16 fits inside the 140-column limit.

`plans/` is excluded in `ruff.toml` rather than reformatted. It is the
archived prototype subtree that `.github/workflows/codeql.yml` already
excludes and `scripts/check_mypy_baseline.py` excludes from every coverage
direction, and the project rules forbid editing anything under it -- so
formatting it would be a diff nobody may take. 844 lint findings and ~280
unformatted files live there. They are excluded because they cannot be
fixed, which is a different statement from saying the code is clean.

* Move all fourteen ruff declarations to 0.16, and stop the lint gate at services/

The bump alone reds every open pull request: `ruff format --check` is a hard
gate on the required `Python - Lint & Type-check` job and 0.16 formats
differently from 0.4.10. The reformat is the previous commit; this one moves
the pin, fixes the findings, and answers the scope question.

Fourteen declarations now read `>=0.16.8,<0.17` -- seven manifests, three
workflows, `.devcontainer/Dockerfile`, and the three `poetry.lock` files that
resolve it (now 0.16.9, which is byte-identical to 0.16.8 on both format and
lint across this tree).

The three findings 0.16 reports on `services/` are fixed, not suppressed: two
`C420` comprehensions become `dict.fromkeys` (both values are string literals,
so the shared-value hazard does not apply) and one `UP031` percent-format
becomes an f-string in a test that counts characters. All three rewrites were
asserted equal to the originals before being kept.

Scope. `ruff check services/` left `scripts/` -- every CI gate in this
repository, the code the rest of its claims are verified by -- linted by
nothing. Lint now covers `services/ scripts/ tests/ tools/`, the trees the
root `ruff.toml` governs that nothing else lints, and `ruff format --check .`
covers everything, because formatting has no semantic content and `.` needs
no edit when a tree is added. That surfaced 17 findings outside `services/`,
each fixed at the source: two `B007` loops switched to `.values()`, a `B904`
re-raise in `security_audit.py` now chains `from exc`, two `E402` imports
that genuinely follow a `sys.path` mutation carry a reason next to the
marker, and two over-length lines were wrapped.

`E501` is ignored for the two detection-spec data modules. Both open their
rule table with `# fmt: off`, so the formatter is already opted out of that
layout deliberately, and all 199 long lines are single dict or string
literals -- 72 positive fixtures, 66 negative, 50 `match_when` clauses, 11
descriptions. Two named files, not `scripts/`.

`packages/` and `plugins/` stay outside the lint gate, and the gap is a
number rather than a shrug: 91 findings and 2. Two of the five packages are
linted by their own workflows under their own configs, and bringing the
three published SDKs under this gate means behaviour-touching fixes that
belong in a change reviewed as such.

* Make packages/aisoc-detections clean under the ruff its own workflow now installs

`python-detections.yml` lints `aisoc_detections detections tests` under the
pin this PR moved, so the package has to be clean under 0.16 too. Two
findings, both fixed at the source rather than silenced: `Callable` moves to
`collections.abc`, and a `# noqa: BLE001` that 0.16 correctly reports as
unused becomes the sentence it was standing in for -- a detection module is
third-party content, so any exception at import is the author's and becomes a
DetectionError naming the file.

---------

Co-authored-by: Beenu Arora <beenu@MacBook-Pro-3.local>
2026-09-24 19:36:59 -07:00

305 lines
12 KiB
Python

#!/usr/bin/env python3
"""Collect a diagnostic bundle an operator can actually send us.
When a deployment misbehaves the exchange is usually several rounds of "what
does X say" before anyone can form a hypothesis. This collects the answers in
one pass.
The hard part is not collection, it is **redaction**, and getting it wrong is
worse than having no bundle: a support bundle is by construction the most
concentrated pile of configuration a deployment produces, and it is emailed.
So the rule here is inverted from the usual one — values are redacted by
default and only an explicit allow-list of names is kept in the clear. A
deny-list would mean every new secret-shaped setting is exposed until someone
remembers to add it, and nobody remembers.
Collected: service versions and health, connector sync state (which is where
"it isn't ingesting" is almost always answered), dead-letter depth, pending
graph migrations, recent error-level log lines, and the claim-gate summary.
Not collected: event contents, alert bodies, prompts, model output, or
anything from the lake. A bundle that carries customer telemetry is one an
operator is not allowed to send, which makes it useless.
Run: python3 scripts/support_bundle.py --out bundle.json
"""
from __future__ import annotations
import argparse
import json
import os
import platform
import re
import subprocess
import sys
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
REPO_ROOT = Path(__file__).resolve().parent.parent
#: Environment variables kept verbatim. Everything else is redacted, because
#: an allow-list fails safe and a deny-list fails open — and the failure is
#: silent either way until the bundle is already in an inbox.
ENV_ALLOW_LIST: frozenset[str] = frozenset(
{
"AISOC_ENV",
"ENVIRONMENT",
"AISOC_DEV_MODE",
"AISOC_DISABLE_CLICKHOUSE",
"AISOC_DISABLE_QDRANT",
"AISOC_CONNECTORS_DISABLE_SCHEDULER",
"AISOC_DEEP_INVESTIGATION",
"AISOC_MIGRATIONS_STRICT",
"AISOC_SIEM_WRITEBACK_ENABLED",
"AISOC_SIEM_WRITEBACK_EXECUTE",
"RETENTION_WORKER_ENABLED",
"RETENTION_WORKER_DRY_RUN",
"BACKUP_ENCRYPTION",
"HUNT_SCHEDULER_ENABLED",
"OAUTH_REFRESH_WORKER_ENABLED",
"WEEKLY_DIGEST_WORKER_ENABLED",
"LOG_LEVEL",
"TZ",
}
)
#: Names matching these are not even reported as present, because the name
#: itself can leak (a variable called ``ACME_PROD_DB_PASSWORD`` names the
#: customer and the environment).
#: Allow-listed names that also match the sensitive-name pattern, each with
#: the reason it is safe. A contradiction between the two lists otherwise
#: resolves silently, whichever way the code happens to check first — so an
#: overlap has to be written down to exist.
ALLOW_LIST_OVERRIDES: dict[str, str] = {
# Holds a mode (off|local|aws), never a key. Diagnostically important:
# "which vault format are the credentials written in" is the first
# question when a connector cannot decrypt.
"AISOC_CREDENTIAL_ENVELOPE": "holds a mode string, never key material",
}
#: Matched on underscore-delimited tokens, not substrings. A substring match
#: treats OAUTH_REFRESH_WORKER_ENABLED — a boolean flag — as a credential
#: because it contains "AUTH", and an allow-list that contradicts the
#: sensitive pattern resolves whichever way the code happens to check first.
SENSITIVE_NAME_RE = re.compile(
r"(?:^|_)(KEY|KEYS|SECRET|SECRETS|TOKEN|PASSWORD|PASSWD|PASS|CREDENTIAL|" r"CREDENTIALS|DSN|AUTH|PRIVATE|SALT|CERT|PEM)(?:_|$)",
re.IGNORECASE,
)
#: Redaction applied to every collected string, including log lines. Ordered
#: most-specific first: a bearer token also matches the generic long-string
#: pattern, and the specific label is more useful in a bug report.
_REDACTIONS: tuple[tuple[re.Pattern[str], str], ...] = (
(re.compile(r"\bBearer\s+[A-Za-z0-9._\-]{10,}", re.IGNORECASE), "Bearer <redacted>"),
(re.compile(r"\bvault:v[12]:[A-Za-z0-9+/=_\-:]{10,}"), "<vault-token redacted>"),
(re.compile(r"\b(?:sk|pk|rk)-[A-Za-z0-9]{16,}"), "<api-key redacted>"),
(re.compile(r"\bAKIA[0-9A-Z]{16}\b"), "<aws-access-key redacted>"),
(re.compile(r"\bghp_[A-Za-z0-9]{20,}\b"), "<github-token redacted>"),
(re.compile(r"\bxox[baprs]-[A-Za-z0-9-]{10,}\b"), "<slack-token redacted>"),
(re.compile(r"\beyJ[A-Za-z0-9_\-]{10,}\.[A-Za-z0-9_\-]{10,}\.[A-Za-z0-9_\-]{10,}"), "<jwt redacted>"),
# Any URL carrying credentials, which is how a DSN usually leaks.
(re.compile(r"://[^:/@\s]+:[^@/\s]+@"), "://<user>:<redacted>@"),
(re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----"), "<private-key redacted>"),
)
def redact(text: str) -> str:
"""Strip credential-shaped substrings. Applied to everything collected."""
if not isinstance(text, str):
return text
for pattern, replacement in _REDACTIONS:
text = pattern.sub(replacement, text)
return text
def collect_environment() -> dict[str, Any]:
"""Configuration flags, allow-listed. Values outside the list are shape-only."""
allowed: dict[str, str] = {}
redacted: list[str] = []
withheld = 0
for name, value in sorted(os.environ.items()):
if (
not name.startswith(
(
"AISOC_",
"POSTGRES_",
"REDIS_",
"KAFKA_",
"CLICKHOUSE_",
"NEO4J_",
"QDRANT_",
"OTEL_",
"BACKUP_",
"RETENTION_",
"HUNT_",
"OAUTH_",
"WEEKLY_",
)
)
and name not in ENV_ALLOW_LIST
and name not in ALLOW_LIST_OVERRIDES
):
continue
if name in ENV_ALLOW_LIST or name in ALLOW_LIST_OVERRIDES:
allowed[name] = redact(value)
elif SENSITIVE_NAME_RE.search(name):
# Not even listed: the name can identify the customer and the
# environment on its own.
withheld += 1
else:
# Set-or-not is usually the whole question ("is KAFKA_BOOTSTRAP_
# SERVERS configured"), and it carries no value.
redacted.append(name)
return {
"configured": allowed,
"set_but_redacted": sorted(redacted),
"withheld_sensitive_names": withheld,
"note": (
"Values are redacted by default; only an allow-list is kept in the "
"clear. Names matching a credential pattern are not listed at all, "
"because the name can identify the deployment."
),
}
def collect_versions() -> dict[str, Any]:
version_file = REPO_ROOT / "VERSION"
return {
"aisoc": version_file.read_text(encoding="utf-8").strip() if version_file.exists() else "unknown",
"python": sys.version.split()[0],
"platform": platform.platform(),
"git_sha": _run(["git", "rev-parse", "--short", "HEAD"]) or "unknown",
"git_dirty": bool(_run(["git", "status", "--porcelain"])),
}
def collect_gates() -> dict[str, Any]:
"""Claim-gate summary, which says what this build actually proves."""
matrix = REPO_ROOT / "docs" / "audit" / "CLAIM_TO_GATE_MATRIX.md"
if not matrix.exists():
return {"available": False}
rows = [
line
for line in matrix.read_text(encoding="utf-8").splitlines()
if line.strip().startswith("|") and ("GATED" in line or "PARTIAL" in line)
]
return {
"available": True,
"gated": sum(1 for r in rows if "NO GATE" not in r and "PARTIAL" not in r and "GATED" in r),
"partial": sum(1 for r in rows if "NO GATE" not in r and "PARTIAL" in r),
}
def collect_compose_state() -> dict[str, Any]:
"""Which containers are up. Almost always the first question."""
output = _run(["docker", "compose", "ps", "--format", "json"])
if not output:
return {"available": False, "note": "docker compose not reachable from here"}
services = []
for line in output.splitlines():
try:
row = json.loads(line)
except json.JSONDecodeError:
continue
services.append(
{
"name": row.get("Service") or row.get("Name"),
"state": row.get("State"),
"health": row.get("Health") or "n/a",
"status": row.get("Status"),
}
)
return {"available": True, "services": services}
def collect_service_health(base_url: str) -> dict[str, Any]:
"""/health from the API, which fans out to the stores it depends on."""
try:
import httpx
except ImportError:
return {"available": False, "note": "httpx not installed"}
try:
response = httpx.get(f"{base_url.rstrip('/')}/health", timeout=10.0)
return {
"available": True,
"status_code": response.status_code,
"body": json.loads(redact(response.text)) if response.text.startswith("{") else redact(response.text)[:2000],
}
except Exception as exc:
# An unreachable API is a finding, not an error: it is usually the
# answer rather than an obstacle to collecting one.
return {"available": False, "error": f"{type(exc).__name__}: {exc}"}
def collect_recent_errors(lines: int = 200) -> dict[str, Any]:
"""Error-level log lines, redacted. Bounded so a bundle stays sendable."""
output = _run(["docker", "compose", "logs", "--tail", str(lines), "--no-color"])
if not output:
return {"available": False}
errors = [redact(line)[:500] for line in output.splitlines() if re.search(r"\b(ERROR|CRITICAL|Traceback|FATAL)\b", line)]
return {
"available": True,
"scanned_lines": len(output.splitlines()),
"error_lines": errors[-100:],
"truncated": len(errors) > 100,
}
def _run(cmd: list[str], timeout: int = 20) -> str:
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout, cwd=REPO_ROOT)
return result.stdout.strip()
except Exception: # noqa: BLE001 - a missing tool is a normal outcome here
return ""
def build_bundle(*, api_url: str, include_logs: bool) -> dict[str, Any]:
bundle: dict[str, Any] = {
"generated_at": datetime.now(UTC).isoformat(),
"schema_version": 1,
"_readme": (
"Diagnostic bundle. Values are redacted by allow-list, not deny-list. "
"Contains no event data, alert bodies, prompts or model output — a "
"bundle carrying customer telemetry is one nobody is allowed to send."
),
"versions": collect_versions(),
"gates": collect_gates(),
"environment": collect_environment(),
"compose": collect_compose_state(),
"api_health": collect_service_health(api_url),
}
if include_logs:
bundle["recent_errors"] = collect_recent_errors()
return bundle
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--out", type=Path, help="Write here instead of stdout.")
parser.add_argument("--api-url", default=os.getenv("AISOC_API_URL", "http://localhost:8000"))
parser.add_argument(
"--no-logs",
action="store_true",
help="Skip log collection. Logs are redacted, but skipping is available for deployments where even redacted log text cannot leave.",
)
args = parser.parse_args(argv)
bundle = build_bundle(api_url=args.api_url, include_logs=not args.no_logs)
payload = json.dumps(bundle, indent=2, sort_keys=True, default=str)
if args.out:
args.out.write_text(payload + "\n", encoding="utf-8")
print(f"support bundle written to {args.out} ({len(payload)} bytes)")
print("Review it before sending. Redaction is thorough but a bundle is the most concentrated configuration a deployment produces.")
else:
print(payload)
return 0
if __name__ == "__main__":
raise SystemExit(main())