Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

feat: P0–P3 service-level hardening (graphql, plugin sigs, metrics, ci, dx)

Beenu Aroracb2d40ca74eb
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 196b4abb7..5c7524fb3 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -34,6 +34,35 @@ jobs:
- run: pnpm lint
- run: pnpm tsc --noEmit
+ # ──────────────────────────────────────────────
+ # Vitest smoke tests for the Next.js web app
+ # The web app used to have *zero* tests gated in CI — lint + typecheck
+ # were the only signal. These smoke tests render the components a
+ # reviewer cares about (Hero, ComparisonTable, Sidebar, TopBar,
+ # MarketplaceView, CaseWorkspace) and assert on the wording we agreed
+ # to keep honest (no "every commit", alert reduction tagged as a
+ # measurement, etc.). They run with jsdom; no browser required.
+ # ──────────────────────────────────────────────
+ web-test:
+ name: Web — Vitest smoke tests
+ runs-on: ubuntu-latest
+ needs: web-lint
+ defaults:
+ run:
+ working-directory: apps/web
+ steps:
+ - uses: actions/checkout@v4
+ - uses: pnpm/action-setup@v4
+ with:
+ version: 9
+ - uses: actions/setup-node@v4
+ with:
+ node-version: '20'
+ cache: pnpm
+ cache-dependency-path: pnpm-lock.yaml
+ - run: pnpm install --frozen-lockfile
+ - run: pnpm test
+
# ──────────────────────────────────────────────
# Build the Next.js web app
# ──────────────────────────────────────────────
@@ -100,10 +129,14 @@ jobs:
python-version: '3.12'
- name: Install Ruff & Mypy
run: pip install ruff mypy
- - name: Ruff lint
- run: ruff check services/api services/actions services/fusion services/agents || true
- - name: Ruff format check
- run: ruff format --check services/api services/actions services/fusion services/agents || true
+ # All services share the repo-root `ruff.toml`. Linting and formatting
+ # cover every Python service, not just the original four — the gate
+ # used to soft-fail with `|| true`; that's gone, so this is the real
+ # signal.
+ - name: Ruff lint (all services)
+ run: ruff check services/
+ - name: Ruff format check (all services)
+ run: ruff format --check services/
python-test:
name: Python — Tests
@@ -134,7 +167,10 @@ jobs:
SECRET_KEY: ci-dummy-secret-key-at-least-32bytes!
DATABASE_URL: postgresql+asyncpg://x:x@localhost/x
run: pytest tests/ -v --tb=short
- - name: Run agents unit tests (Pillar-1 eval suites)
+ - name: Run agents unit tests (Pillar-1 eval suites + audit helpers)
+ # `test_audit_helpers.py` imports `app.investigator.state` directly
+ # (it stubs out the package __init__ to avoid pulling in LangGraph),
+ # so it only needs stdlib + pydantic — already installed via API_DEPS.
run: |
pip install --quiet pytest pytest-asyncio
cd services/agents
@@ -143,8 +179,101 @@ jobs:
tests/test_alert_reduction.py \
tests/test_investigation_completeness.py \
tests/test_response_quality.py \
+ tests/test_audit_helpers.py \
-v --tb=short
+ # ──────────────────────────────────────────────
+ # Python micro-services unit tests
+ # `services/fusion`, `services/honeytokens`, and `services/purple-team`
+ # used to ship with no Python tests gated in CI at all — only ruff +
+ # format check. These suites cover the *pure* helpers in each service
+ # (alert fingerprinting/correlation keys, honeytoken generators,
+ # ATT&CK coverage matrix builder) — they need pydantic/pydantic-settings
+ # only, no Postgres, Redis, or Kafka. Real signal, fast.
+ # ──────────────────────────────────────────────
+ python-services-test:
+ name: Python — Service unit tests (fusion, honeytokens, purple-team)
+ runs-on: ubuntu-latest
+ needs: python-lint
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-python@v5
+ with:
+ python-version: '3.12'
+ cache: pip
+ - name: Install minimal deps
+ run: pip install --quiet pydantic pydantic-settings pytest
+ - name: Run fusion unit tests
+ working-directory: services/fusion
+ run: python -m pytest tests/ -v --tb=short
+ - name: Run honeytokens unit tests
+ working-directory: services/honeytokens
+ run: python -m pytest tests/ -v --tb=short
+ - name: Run purple-team unit tests
+ working-directory: services/purple-team
+ run: python -m pytest tests/ -v --tb=short
+
+ # ──────────────────────────────────────────────
+ # SDK packages (Python)
+ # The three Python SDKs ship as pip packages but never had a CI gate —
+ # regressions could land silently. This job installs each in editable
+ # mode and runs its bundled pytest suite. They're intentionally light
+ # on dependencies (httpx, pydantic, click, pyyaml) so the job is fast.
+ # ──────────────────────────────────────────────
+ sdk-python-test:
+ name: SDK — Python (pytest)
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-python@v5
+ with:
+ python-version: '3.12'
+ cache: pip
+ - name: Install SDKs in editable mode (with dev extras)
+ # Each `[dev]` extra pins pytest + pytest-asyncio; pytest-httpx is
+ # only needed by `aisoc-sdk` (the typed client tests), so we add it
+ # explicitly rather than expanding the package extras.
+ run: |
+ pip install --quiet \
+ -e packages/plugin-sdk-py \
+ -e packages/sdk-py \
+ -e packages/aisoc-cli \
+ pytest pytest-asyncio pytest-httpx
+ - name: Run aisoc-plugin-sdk tests
+ run: pytest packages/plugin-sdk-py/tests/ -v --tb=short
+ - name: Run aisoc-sdk tests
+ run: pytest packages/sdk-py/tests/ -v --tb=short
+ - name: Run aisoc-cli tests
+ run: pytest packages/aisoc-cli/tests/ -v --tb=short
+
+ # ──────────────────────────────────────────────
+ # SDK packages (TypeScript)
+ # `@aisoc/sdk` is a typed openapi-fetch client with a self-contained
+ # vitest suite (HTTP mocked, no real server). Same pnpm workspace
+ # pattern as the MCP job — install once at the root, then filter.
+ # ──────────────────────────────────────────────
+ sdk-ts-test:
+ name: SDK — TypeScript (vitest)
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+ - uses: pnpm/action-setup@v4
+ with:
+ version: 9
+ - uses: actions/setup-node@v4
+ with:
+ node-version: '20'
+ cache: pnpm
+ cache-dependency-path: pnpm-lock.yaml
+ - name: Install workspace deps
+ run: pnpm install --frozen-lockfile
+ - name: Type-check
+ run: pnpm --filter @aisoc/sdk typecheck
+ - name: Test
+ run: pnpm --filter @aisoc/sdk test
+ - name: Build
+ run: pnpm --filter @aisoc/sdk build
+
# ──────────────────────────────────────────────
# Go services (enrichment, ingest, demo-producer)
# ──────────────────────────────────────────────
@@ -171,7 +300,35 @@ jobs:
- name: Test
run: |
cd services/${{ matrix.service }}
- go test ./... -count=1 || true
+ go test ./... -count=1
+
+ # ──────────────────────────────────────────────
+ # Go SDK (`packages/plugin-sdk-go`)
+ # Lives outside `services/` so the matrix above doesn't pick it up.
+ # Has `client_test.go`, `loader_test.go`, `registry_test.go` already —
+ # they were never gated. Build + vet + test, no soft-fails.
+ # ──────────────────────────────────────────────
+ sdk-go-test:
+ name: SDK — Go (build, vet, test)
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-go@v5
+ with:
+ go-version: '1.22'
+ cache-dependency-path: packages/plugin-sdk-go/go.sum
+ - name: Build
+ run: |
+ cd packages/plugin-sdk-go
+ go build ./...
+ - name: Vet
+ run: |
+ cd packages/plugin-sdk-go
+ go vet ./...
+ - name: Test
+ run: |
+ cd packages/plugin-sdk-go
+ go test ./... -count=1
# ──────────────────────────────────────────────
# Pillar-1 Eval Gate: 4-suite eval harness (200-incident synthetic dataset)
diff --git a/.github/workflows/validate-detections.yml b/.github/workflows/validate-detections.yml
index 4eacbcc17..79fba34c2 100644
--- a/.github/workflows/validate-detections.yml
+++ b/.github/workflows/validate-detections.yml
@@ -1,19 +1,15 @@
name: Validate Detection Rules
+# Runs on every PR / push to main+develop, no path filter.
+# Keeping a path filter let regressions ride in on PRs that touched neighbouring
+# code without bumping a detections/** path — e.g. a refactor that breaks the
+# YAML loader. The validator stays fast (~30s for 6,900+ rules), so the cost of
+# running it always is negligible compared to that risk.
on:
push:
- paths:
- - "detections/**"
- - "scripts/validate_detections.py"
- - "scripts/generate_detections.py"
- - "scripts/detection_specs*.py"
- - ".github/workflows/validate-detections.yml"
+ branches: [main, develop]
pull_request:
- paths:
- - "detections/**"
- - "scripts/validate_detections.py"
- - "scripts/generate_detections.py"
- - "scripts/detection_specs*.py"
+ branches: [main, develop]
jobs:
validate:
diff --git a/.github/workflows/validate-playbooks.yml b/.github/workflows/validate-playbooks.yml
index 50078d2ce..f7ca25a23 100644
--- a/.github/workflows/validate-playbooks.yml
+++ b/.github/workflows/validate-playbooks.yml
@@ -1,19 +1,14 @@
name: Validate Playbook Pack
+# Runs on every PR / push to main+develop, no path filter.
+# Same reasoning as validate-detections: the playbook validator is fast and
+# catches schema drift introduced by refactors that don't touch playbooks/**
+# directly (e.g. edits to services/agents/app/playbook/models.py).
on:
push:
- paths:
- - "playbooks/**"
- - "services/agents/app/playbook/models.py"
- - "scripts/validate_playbooks.py"
- - "scripts/generate_playbooks.py"
- - ".github/workflows/validate-playbooks.yml"
+ branches: [main, develop]
pull_request:
- paths:
- - "playbooks/**"
- - "services/agents/app/playbook/models.py"
- - "scripts/validate_playbooks.py"
- - "scripts/generate_playbooks.py"
+ branches: [main, develop]
jobs:
validate:
diff --git a/.gitignore b/.gitignore
index aa23ae021..8fdbf1959 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,9 @@ eval_mitre_accuracy_report.json
.gocache/
*.tsbuildinfo
+# Detection-import upstream clones (populated by tools.detection_import)
+.import-cache/
+
# Docusaurus generated cache (apps/docs)
apps/docs/.docusaurus/
apps/docs/build/
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 0636efc5d..cc709b1f7 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -92,7 +92,7 @@ importers:
version: 0.312.0([email protected])
next:
specifier: 14.1.0
react:
specifier: ^18.2.0
version: 18.3.1
@@ -112,6 +112,15 @@ importers:
specifier: ^4.4.7
version: 4.5.7(@types/[email protected])([email protected])
devDependencies:
+ '@testing-library/jest-dom':
+ specifier: ^6.4.2
+ version: 6.9.1
+ '@testing-library/react':
+ specifier: ^14.2.0
+ '@testing-library/user-event':
+ specifier: ^14.5.2
+ version: 14.6.1(@testing-library/[email protected])
'@types/cytoscape':
specifier: ^3.21.0
version: 3.31.0
@@ -124,6 +133,9 @@ importers:
'@types/react-dom':
specifier: ^18.2.0
version: 18.3.7(@types/[email protected])
+ '@vitejs/plugin-react':
+ specifier: ^4.2.1
+ version: 4.7.0([email protected])
autoprefixer:
specifier: ^10.4.17
version: 10.5.0([email protected])
@@ -133,6 +145,9 @@ importers:
eslint-config-next:
specifier: 14.1.0
+ jsdom:
+ specifier: ^24.0.0
+ version: 24.1.3
postcss:
specifier: ^8.4.33
version: 8.5.13
@@ -142,6 +157,9 @@ importers:
typescript:
specifier: ^5.3.3
version: 5.9.3
+ vitest:
+ specifier: ^2.0.0
+ version: 2.1.9(@types/[email protected])([email protected])
packages/ocsf:
devDependencies:
@@ -169,7 +187,7 @@ importers:
version: 5.9.3
vitest:
specifier: ^2.0.0
- version: 2.1.9(@types/[email protected])
+ version: 2.1.9(@types/[email protected])([email protected])
packages/types:
devDependencies:
@@ -225,10 +243,14 @@ importers:
version: 5.9.3
vitest:
specifier: ^2.0.0
- version: 2.1.9(@types/[email protected])
+ version: 2.1.9(@types/[email protected])([email protected])
packages:
+ resolution: {integrity: sha512-Elp+iwUx5rN5+Y8xLt5/GRoG20WGoDCQ/1Fb+1LiGtvwbDavuSk0jhD/eZdckHAuzcDzccnkv+rEjyWfRx18gg==}
+ dev: true
+
/@algolia/[email protected]:
resolution: {integrity: sha512-8siuLG+FIns1AjZ/g2SDVwHz9S+ObacDQISEJvS8XsNei1zl3FXqfqQrBpmrG7ACWCyesXHbicMJtvRbg00FEw==}
engines: {node: '>= 14.0.0'}
@@ -441,6 +463,16 @@ packages:
engines: {node: '>=10'}
dev: true
+ /@asamuzakjp/[email protected]:
+ resolution: {integrity: sha512-K1A6z8tS3XsmCMM86xoWdn7Fkdn9m6RSVtocUrJYIwZnFVkng/PvkEoWtOWmP+Scc6saYWHWZYbndEEXxl24jw==}
+ dependencies:
+ '@csstools/css-calc': 2.1.4(@csstools/[email protected])(@csstools/[email protected])
+ '@csstools/css-color-parser': 3.1.0(@csstools/[email protected])(@csstools/[email protected])
+ '@csstools/css-parser-algorithms': 3.0.5(@csstools/[email protected])
+ '@csstools/css-tokenizer': 3.0.4
+ lru-cache: 10.4.3
+ dev: true
+
resolution: {integrity: sha512-9NhCeYjq9+3uxgdtp20LSiJXJvN0FeCtNGpJxuMFZ1Kv3cWUNb6DOhJwUvcVCzKGR66cw4njwM6hrJLqgOwbcw==}
engines: {node: '>=6.9.0'}
@@ -452,7 +484,6 @@ packages:
resolution: {integrity: sha512-LIVqM46zQWZhj17qA8wb4nW/ixr2y1Nw+r1etiAWgRM6U1IqP+LNhL1yg440jYZR72jCWcWbLWzIosH+uP1fqg==}
engines: {node: '>=6.9.0'}
- dev: false
resolution: {integrity: sha512-CGOfOJqWjg2qW/Mb6zNsDm+u5vFQ8DxXfbM09z69p5Z6+mE1ikP2jUXw+j42Pf1XTYED2Rni5f95npYeuwMDQA==}
@@ -475,7 +506,6 @@ packages:
semver: 6.3.1
transitivePeerDependencies:
- supports-color
- dev: false
resolution: {integrity: sha512-qsaF+9Qcm2Qv8SRIMMscAvG4O3lJ0F1GuMo5HR/Bp02LopNgnZBC/EkbevHFeGs4ls/oPz9v+Bsmzbkbe+0dUw==}
@@ -486,7 +516,6 @@ packages:
'@jridgewell/gen-mapping': 0.3.13
'@jridgewell/trace-mapping': 0.3.31
jsesc: 3.1.0
- dev: false
resolution: {integrity: sha512-fXSwMQqitTGeHLBC08Eq5yXz2m37E4pJX1qAU1+2cNedz/ifv/bVXft90VeSav5nFO61EcNgwr0aJxbyPaWBPg==}
@@ -504,7 +533,6 @@ packages:
browserslist: 4.28.2
lru-cache: 5.1.1
semver: 6.3.1
- dev: false
resolution: {integrity: sha512-RpLYy2sb51oNLjuu1iD3bwBqCBWUzjO0ocp+iaCP/lJtb2CPLcnC2Fftw+4sAzaMELGeWTgExSKADbdo0GFVzA==}
@@ -554,7 +582,6 @@ packages:
resolution: {integrity: sha512-+W6cISkXFa1jXsDEdYA8HeevQT/FULhxzR99pxphltZcVaugps53THCeiWA8SguxxpSp3gKPiuYfSWopkLQ4hw==}
engines: {node: '>=6.9.0'}
- dev: false
resolution: {integrity: sha512-cwM7SBRZcPCLgl8a7cY0soT1SptSzAlMH39vwiRpOQkJlh53r5hdHwLSCZpQdVLT39sZt+CRpNwYG4Y2v77atg==}
@@ -574,7 +601,6 @@ packages:
'@babel/types': 7.29.0
transitivePeerDependencies:
- supports-color
- dev: false
resolution: {integrity: sha512-67oXFAYr2cDLDVGLXTEABjdBJZ6drElUSI7WKp70NrpyISso3plG9SAGEF6y7zbha/wOzUByWWTJvEDVNIUGcA==}
@@ -588,7 +614,6 @@ packages:
'@babel/traverse': 7.29.0
transitivePeerDependencies:
- supports-color
- dev: false
resolution: {integrity: sha512-URMGH08NzYFhubNSGJrpUEphGKQwMQYBySzat5cAByY1/YgIRkULnIy3tAMeszlL/so2HbeilYloUmSpd7GdVw==}
@@ -600,7 +625,6 @@ packages:
resolution: {integrity: sha512-S9gzZ/bz83GRysI7gAD4wPT/AI3uCnY+9xn+Mx/KPs2JwHJIz1W8PZkg2cqyt3RNOBM8ejcXhV6y8Og7ly/Dug==}
engines: {node: '>=6.9.0'}
- dev: false
resolution: {integrity: sha512-7fiA521aVw8lSPeI4ZOD3vRFkoqkJcS+z4hFo82bFSH/2tNd6eJ5qCVMS5OzDmZh/kaHQeBaeyxK6wljcPtveA==}
@@ -643,7 +667,6 @@ packages:
resolution: {integrity: sha512-qMlSxKbpRlAridDExk92nSobyDdpPijUq2DW6oDnUqd0iOGxmQjyqhMIihI9+zv4LPyZdRje2cavWPbCbWm3eA==}
engines: {node: '>=6.9.0'}
- dev: false
resolution: {integrity: sha512-qSs4ifwzKJSV39ucNjsvc6WVHs6b7S03sOh2OcHF9UHfVPqWWALUsNUVzhSBiItjRZoLHx7nIarVjqKVusUZ1Q==}
@@ -652,7 +675,6 @@ packages:
resolution: {integrity: sha512-YvjJow9FxbhFFKDSuFnVCe2WxXk1zWc22fFePVNEaWJEu8IrZVlda6N0uHwzZrUM1il7NC9Mlp4MaJYbYd9JSg==}
engines: {node: '>=6.9.0'}
- dev: false
resolution: {integrity: sha512-z+PwLziMNBeSQJonizz2AGnndLsP2DeGHIxDAn+wdHOGuo4Fo1x1HBPPXeE9TAOPHNNWQKCSlA2VZyYyyibDnQ==}
@@ -671,7 +693,6 @@ packages:
dependencies:
'@babel/template': 7.28.6
'@babel/types': 7.29.0
- dev: false
resolution: {integrity: sha512-b3ctpQwp+PROvU/cttc4OYl4MzfJUWy6FZg+PMXfzmt/+39iHVF0sDfqay8TQM3JA2EUOyKcFZt75jWriQijsA==}
@@ -679,7 +700,6 @@ packages:
hasBin: true
dependencies:
'@babel/types': 7.29.0
- dev: false
resolution: {integrity: sha512-87GDMS3tsmMSi/3bWOte1UblL+YUTFMV8SZPZ2eSEL17s74Cw/l63rR6NmGVKMYW2GYi85nE+/d6Hw5N0bEk2Q==}
@@ -1316,6 +1336,26 @@ packages:
- supports-color
dev: false
+ resolution: {integrity: sha512-6UzkCs+ejGdZ5mFFC/OCUrv028ab2fp1znZmCZjAOBKiBK2jXD1O+BPSfX8X2qjJ75fZBMSnQn3Rq2mrBJK2mw==}
+ engines: {node: '>=6.9.0'}
+ peerDependencies:
+ '@babel/core': ^7.0.0-0
+ dependencies:
+ '@babel/core': 7.29.0
+ '@babel/helper-plugin-utils': 7.28.6
+ dev: true
+
+ resolution: {integrity: sha512-zbwoTsBruTeKB9hSq73ha66iFeJHuaFkUbwvqElnygoNbj/jHRsSeokowZFN3CZ64IvEqcmmkVe89OPXc7ldAw==}
+ engines: {node: '>=6.9.0'}
+ peerDependencies:
+ '@babel/core': ^7.0.0-0
+ dependencies:
+ '@babel/core': 7.29.0
+ '@babel/helper-plugin-utils': 7.28.6
+ dev: true
+
resolution: {integrity: sha512-61bxqhiRfAACulXSLd/GxqmAedUSrRZIu/cbaT18T1CetkTmtDN15it7i80ru4DVqRK1WMxQhXs+Lf9kajm5Ow==}
engines: {node: '>=6.9.0'}
@@ -1640,7 +1680,6 @@ packages:
'@babel/code-frame': 7.29.0
'@babel/parser': 7.29.3
'@babel/types': 7.29.0
- dev: false
resolution: {integrity: sha512-4HPiQr0X7+waHfyXPZpWPfWL/J7dcN1mx9gL6WdQVMbPnF3+ZhSMs8tCxN7oHddJE9fhNE7+lxdnlyemKfJRuA==}
@@ -1655,7 +1694,6 @@ packages:
debug: 4.4.3([email protected])
transitivePeerDependencies:
- supports-color
- dev: false
resolution: {integrity: sha512-LwdZHpScM4Qz8Xw2iKSzS+cfglZzJGvofQICy7W7v4caru4EaAmyUuO6BGrbyQ2mYV11W0U8j5mBhd14dd3B0A==}
@@ -1663,7 +1701,6 @@ packages:
dependencies:
'@babel/helper-string-parser': 7.27.1
'@babel/helper-validator-identifier': 7.28.5
- dev: false
resolution: {integrity: sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==}
@@ -1686,7 +1723,6 @@ packages:
/@csstools/[email protected]:
resolution: {integrity: sha512-S11EXWJyy0Mz5SYvRmY8nJYTFFd1LCNV+7cXyAgQtOOuzb4EsgfqDufL+9esx72/eLhsRdGZwaldu/h+E4t4BA==}
engines: {node: '>=18'}
- dev: false
/@csstools/[email protected](@csstools/[email protected])(@csstools/[email protected]):
resolution: {integrity: sha512-3N8oaj+0juUw/1H3YwmDDJXCgTB1gKU6Hc/bB502u9zR0q2vd786XJH9QfrKIEgFlZmhZiq6epXl4rHqhzsIgQ==}
@@ -1697,7 +1733,6 @@ packages:
dependencies:
'@csstools/css-parser-algorithms': 3.0.5(@csstools/[email protected])
'@csstools/css-tokenizer': 3.0.4
- dev: false
/@csstools/[email protected](@csstools/[email protected])(@csstools/[email protected]):
resolution: {integrity: sha512-nbtKwh3a6xNVIp/VRuXV64yTKnb1IjTAEEh3irzS+HkKjAOYLTGNb9pmVNntZ8iVBHcWDA2Dof0QtPgFI1BaTA==}
@@ -1710,7 +1745,6 @@ packages:
'@csstools/css-calc': 2.1.4(@csstools/[email protected])(@csstools/[email protected])
'@csstools/css-parser-algorithms': 3.0.5(@csstools/[email protected])
'@csstools/css-tokenizer': 3.0.4
- dev: false
/@csstools/[email protected](@csstools/[email protected]):
resolution: {integrity: sha512-DaDeUkXZKjdGhgYaHNJTV9pV7Y9B3b644jCLs9Upc3VeNGg6LWARAT6O+Q+/COo+2gg/bM5rhpMAtf70WqfBdQ==}
@@ -1719,12 +1753,10 @@ packages:
'@csstools/css-tokenizer': ^3.0.4
dependencies:
'@csstools/css-tokenizer': 3.0.4
- dev: false
/@csstools/[email protected]:
resolution: {integrity: sha512-Vd/9EVDiu6PPJt9yAh6roZP6El1xHrdvIVGjyBsHR0RYwNHgL7FJPyIIW4fANJNG6FtyZfvlRPpFI4ZM/lubvw==}
engines: {node: '>=18'}
- dev: false
/@csstools/[email protected](@csstools/[email protected])(@csstools/[email protected]):
resolution: {integrity: sha512-HAYH7d3TLRHDOUQK4mZKf9k9Ph/m8Akstg66ywKR4SFAigjs3yBiUeZtFxywiTm5moZMAp/5W/ZuFnNXXYLuuQ==}
@@ -3744,7 +3776,6 @@ packages:
dependencies:
'@jridgewell/gen-mapping': 0.3.13
'@jridgewell/trace-mapping': 0.3.31
- dev: false
/@jridgewell/[email protected]:
resolution: {integrity: sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==}
@@ -4583,6 +4614,10 @@ packages:
- supports-color
dev: true
+ /@rolldown/[email protected]:
+ resolution: {integrity: sha512-+d0F4MKMCbeVUJwG96uQ4SgAznZNSq93I3V+9NHA4OpvqG8mRCpGdKmK8l/dl02h2CCDHwW2FqilnTyDcAnqjA==}
+ dev: true
+
resolution: {integrity: sha512-dnlp69efPPg6Uaw2dVqzWRfAWRnYVb1XJ8CyyhIbZeaq4CA5/mLeZ1IEt9QqQxmbdvagjLIm2ZL8BxXv5lH4Yw==}
cpu: [arm]
@@ -5008,6 +5043,71 @@ packages:
defer-to-connect: 2.0.1
dev: false
+ /@testing-library/[email protected]:
+ resolution: {integrity: sha512-o4PXJQidqJl82ckFaXUeoAW+XysPLauYI43Abki5hABd853iMhitooc6znOnczgbTYmEP6U6/y1ZyKAIsvMKGg==}
+ engines: {node: '>=18'}
+ dependencies:
+ '@babel/code-frame': 7.29.0
+ '@babel/runtime': 7.29.2
+ '@types/aria-query': 5.0.4
+ aria-query: 5.3.0
+ dom-accessibility-api: 0.5.16
+ lz-string: 1.5.0
+ picocolors: 1.1.1
+ pretty-format: 27.5.1
+ dev: true
+
+ /@testing-library/[email protected]:
+ resolution: {integrity: sha512-FlS4ZWlp97iiNWig0Muq8p+3rVDjRiYE+YKGbAqXOu9nwJFFOdL00kFpz42M+4huzYi86vAK1sOOfyOG45muIQ==}
+ engines: {node: '>=14'}
+ dependencies:
+ '@babel/code-frame': 7.29.0
+ '@babel/runtime': 7.29.2
+ '@types/aria-query': 5.0.4
+ aria-query: 5.1.3
+ chalk: 4.1.2
+ dom-accessibility-api: 0.5.16
+ lz-string: 1.5.0
+ pretty-format: 27.5.1
+ dev: true
+
+ /@testing-library/[email protected]:
+ resolution: {integrity: sha512-zIcONa+hVtVSSep9UT3jZ5rizo2BsxgyDYU7WFD5eICBE7no3881HGeb/QkGfsJs6JTkY1aQhT7rIPC7e+0nnA==}
+ engines: {node: '>=14', npm: '>=6', yarn: '>=1'}
+ dependencies:
+ '@adobe/css-tools': 4.4.4
+ aria-query: 5.3.2
+ css.escape: 1.5.1
+ dom-accessibility-api: 0.6.3
+ picocolors: 1.1.1
+ redent: 3.0.0
+ dev: true
+
+ resolution: {integrity: sha512-H99XjUhWQw0lTgyMN05W3xQG1Nh4lq574D8keFf1dDoNTJgp66VbJozRaczoF+wsiaPJNt/TcnfpLGufGxSrZQ==}
+ engines: {node: '>=14'}
+ peerDependencies:
+ react: ^18.0.0
+ react-dom: ^18.0.0
+ dependencies:
+ '@babel/runtime': 7.29.2
+ '@testing-library/dom': 9.3.4
+ '@types/react-dom': 18.3.7(@types/[email protected])
+ react: 18.3.1
+ react-dom: 18.3.1([email protected])
+ transitivePeerDependencies:
+ - '@types/react'
+ dev: true
+
+ /@testing-library/[email protected](@testing-library/[email protected]):
+ resolution: {integrity: sha512-vq7fv0rnt+QTXgPxr5Hjc210p6YKq2kmdziLgnsZGgLJ9e6VAShx1pACLuRjd/AS/sr7phAR58OIIpf0LlmQNw==}
+ engines: {node: '>=12', npm: '>=6'}
+ peerDependencies:
+ '@testing-library/dom': '>=7.21.4'
+ dependencies:
+ '@testing-library/dom': 10.4.1
+ dev: true
+
resolution: {integrity: sha512-9tTaPJLSiejZKx+Bmog4uSubteqTvFrVrURwkmHixBo0G4seD0zUxp98E1DzUBJxLQ3NPwXrGKDiVjwx/DpPsg==}
requiresBuild: true
@@ -5016,6 +5116,39 @@ packages:
dev: true
optional: true
+ resolution: {integrity: sha512-rfT93uj5s0PRL7EzccGMs3brplhcrghnDoV26NqKhCAS1hVo+WdNsPvE/yb6ilfr5hi2MEk6d5EWJTKdxg8jVw==}
+ dev: true
+
+ resolution: {integrity: sha512-qoQprZvz5wQFJwMDqeseRXWv3rqMvhgpbXFfVyWhbx9X47POIA6i/+dXefEmZKoAgOaTdaIgNSMqMIU61yRyzA==}
+ dependencies:
+ '@babel/parser': 7.29.3
+ '@babel/types': 7.29.0
+ '@types/babel__generator': 7.27.0
+ '@types/babel__template': 7.4.4
+ '@types/babel__traverse': 7.28.0
+ dev: true
+
+ resolution: {integrity: sha512-ufFd2Xi92OAVPYsy+P4n7/U7e68fex0+Ee8gSG9KX7eo084CWiQ4sdxktvdl0bOPupXtVJPY19zk6EwWqUQ8lg==}
+ dependencies:
+ '@babel/types': 7.29.0
+ dev: true
+
+ resolution: {integrity: sha512-h/NUaSyG5EyxBIp8YRxo4RMe2/qQgvyowRwVMzhYhBCONbW8PUsg4lkFMrhgZhUe5z3L3MiLDuvyJ/CaPa2A8A==}
+ dependencies:
+ '@babel/parser': 7.29.3
+ '@babel/types': 7.29.0
+ dev: true
+
+ resolution: {integrity: sha512-8PvcXf70gTDZBgt9ptxJ8elBeBjcLOAcOtoO/mPJjtji1+CdGbHgm77om1GrsPxsiE+uXIpNSK64UYaIwQXd4Q==}
+ dependencies:
+ '@babel/types': 7.29.0
+ dev: true
+
resolution: {integrity: sha512-HLFeCYgz89uk22N5Qg3dvGvsv46B8GLvKKo1zKG4NybA8U2DiEO3w9lqGg29t/tfLRJpJ6iQxnVw4OnB7MoM9g==}
dependencies:
@@ -5584,6 +5717,23 @@ packages:
dev: true
optional: true
+ resolution: {integrity: sha512-gUu9hwfWvvEDBBmgtAowQCojwZmJ5mcLn3aufeCsitijs3+f2NsrPtlAWIR6OPiqljl96GVCUbLe0HyqIpVaoA==}
+ engines: {node: ^14.18.0 || >=16.0.0}
+ peerDependencies:
+ vite: ^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0
+ dependencies:
+ '@babel/core': 7.29.0
+ '@babel/plugin-transform-react-jsx-self': 7.27.1(@babel/[email protected])
+ '@babel/plugin-transform-react-jsx-source': 7.27.1(@babel/[email protected])
+ '@rolldown/pluginutils': 1.0.0-beta.27
+ '@types/babel__core': 7.20.5
+ react-refresh: 0.17.0
+ vite: 5.4.21(@types/[email protected])
+ transitivePeerDependencies:
+ - supports-color
+ dev: true
+
resolution: {integrity: sha512-UJCIkTBenHeKT1TTlKMJWy1laZewsRIzYighyYiJKZreqtdxSos/S1t+ktRMQWu2CKqaarrkeszJx1cgC5tGZw==}
dependencies:
@@ -5946,6 +6096,11 @@ packages:
dependencies:
color-convert: 2.0.1
+ resolution: {integrity: sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==}
+ engines: {node: '>=10'}
+ dev: true
+
resolution: {integrity: sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg==}
engines: {node: '>=12'}
@@ -5985,6 +6140,18 @@ packages:
tslib: 2.8.1
dev: false
+ resolution: {integrity: sha512-R5iJ5lkuHybztUfuOAznmboyjWq8O6sqNqtK7CLOqdydi54VNbORp49mb14KbWgG1QD3JFO9hJdZ+y4KutfdOQ==}
+ dependencies:
+ deep-equal: 2.2.3
+ dev: true
+
+ resolution: {integrity: sha512-b0P0sZPKtyu8HkeRAfCq0IfURZK+SuwMjY1UXGBU27wpAiTwQAIlq56IbIO+ytk/JjS1fMR14ee5WBBfKi5J6A==}
+ dependencies:
+ dequal: 2.0.3
+ dev: true
+
resolution: {integrity: sha512-COROpnaoap1E2F000S62r6A60uHZnmlvomhfyT2DlTcrY1OrBKn2UhH7qn5wTC9zMvD0AY7csdPSNwKP+7WiQw==}
engines: {node: '>= 0.4'}
@@ -6116,6 +6283,10 @@ packages:
engines: {node: '>= 0.4'}
dev: true
+ resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==}
+ dev: true
+
resolution: {integrity: sha512-FMhOoZV4+qR6aTUALKX2rEqGG+oyATvwBt9IIzVR5rMa2HRWPkxf+P+PAJLD1I/H5/II+HuZcBJYEFBpq39ong==}
engines: {node: ^10 || ^12 || >=14}
@@ -6663,6 +6834,13 @@ packages:
engines: {node: '>=10'}
dev: false
+ resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==}
+ engines: {node: '>= 0.8'}
+ dependencies:
+ delayed-stream: 1.0.0
+ dev: true
+
resolution: {integrity: sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg==}
@@ -6777,7 +6955,6 @@ packages:
resolution: {integrity: sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==}
- dev: false
resolution: {integrity: sha512-NXdYc3dLr47pBkpUCHtKSwIOQXLVn8dZEuywboCOJY/osA0wFSLlSawr3KN8qXJEyX66FcONTH8EIlVuK0yyFA==}
@@ -7012,6 +7189,10 @@ packages:
engines: {node: '>= 6'}
dev: false
+ resolution: {integrity: sha512-YUifsXXuknHlUsmlgyY0PKzgPOr7/FjCePfHNt0jxm83wHZi44VDMQ7/fGNkjY3/jV1MC+1CmZbaHzugyeRtpg==}
+ dev: true
+
resolution: {integrity: sha512-QbLeyz2Bgso1iRlh7IpWk6OKa3lLNGXsujVjDMPl9rOZpxKeiG69icLpbLCFxeURwmcdIfZqQyhlooKJYM4f8Q==}
dev: false
@@ -7103,6 +7284,14 @@ packages:
css-tree: 2.2.1
dev: false
+ resolution: {integrity: sha512-2z+rWdzbbSZv6/rhtvzvqeZQHrBaqgogqt85sqFNbabZOuFbCVFb8kPeEtZjiKkbrm395irpNKiYeFeLiQnFPg==}
+ engines: {node: '>=18'}
+ dependencies:
+ '@asamuzakjp/css-color': 3.2.0
+ rrweb-cssom: 0.8.0
+ dev: true
+
resolution: {integrity: sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==}
@@ -7237,6 +7426,14 @@ packages:
resolution: {integrity: sha512-sdQSFB7+llfUcQHUQO3+B8ERRj0Oa4w9POWMI/puGtuf7gFywGmkaLCElnudfTiKZV+NvHqL0ifzdrI8Ro7ESA==}
dev: true
+ resolution: {integrity: sha512-ZYP5VBHshaDAiVZxjbRVcFJpc+4xGgT0bK3vzy1HLN8jTO975HEbuYzZJcHoQEY5K1a0z8YayJkyVETa08eNTg==}
+ engines: {node: '>=18'}
+ dependencies:
+ whatwg-mimetype: 4.0.0
+ whatwg-url: 14.2.0
+ dev: true
+
resolution: {integrity: sha512-EmKO5V3OLXh1rtK2wgXRansaK1/mtVdTUEiEI0W8RkvgT05kfxaH29PliLnpLP73yYO6142Q72QNa8Wx/A5CqQ==}
engines: {node: '>= 0.4'}
@@ -7310,6 +7507,10 @@ packages:
resolution: {integrity: sha512-qIMFpTMZmny+MMIitAB6D7iVPEorVw6YQRWkvarTkT4tBeSLLiHzcwj6q0MmYSFCiVpiqPJTJEYIrpcPzVEIvg==}
dev: false
+ resolution: {integrity: sha512-YpgQiITW3JXGntzdUmyUR1V812Hn8T1YVXhCu+wO3OpS4eU9l4YdD3qjyiKdV6mvV29zapkMeD390UVEf2lkUg==}
+ dev: true
+
resolution: {integrity: sha512-GtpQYB283KrPp6nRw50q3U9/VfOutZOe103qlN7BPP6Ad27xYnOIWv4lPzo8HCAL+mMZofJ9KEy30fq6MfaK6Q==}
dependencies:
@@ -7327,6 +7528,30 @@ packages:
engines: {node: '>=6'}
dev: true
+ resolution: {integrity: sha512-ZIwpnevOurS8bpT4192sqAowWM76JDKSHYzMLty3BZGSswgq6pBaH3DhCSW5xVAZICZyKdOBPjwww5wfgT/6PA==}
+ engines: {node: '>= 0.4'}
+ dependencies:
+ array-buffer-byte-length: 1.0.2
+ call-bind: 1.0.9
+ es-get-iterator: 1.1.3
+ get-intrinsic: 1.3.0
+ is-arguments: 1.2.0
+ is-array-buffer: 3.0.5
+ is-date-object: 1.1.0
+ is-regex: 1.2.1
+ is-shared-array-buffer: 1.0.4
+ isarray: 2.0.5
+ object-is: 1.1.6
+ object-keys: 1.1.1
+ object.assign: 4.1.7
+ regexp.prototype.flags: 1.5.4
+ side-channel: 1.1.0
+ which-boxed-primitive: 1.1.1
+ which-collection: 1.0.2
+ which-typed-array: 1.1.20
+ dev: true
+
resolution: {integrity: sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==}
engines: {node: '>=4.0.0'}
@@ -7385,6 +7610,11 @@ packages:
has-property-descriptors: 1.0.2
object-keys: 1.1.1
+ resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==}
+ engines: {node: '>=0.4.0'}
+ dev: true
+
resolution: {integrity: sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==}
engines: {node: '>= 0.6'}
@@ -7463,6 +7693,14 @@ packages:
esutils: 2.0.3
dev: true
+ resolution: {integrity: sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg==}
+ dev: true
+
+ resolution: {integrity: sha512-7ZgogeTnjuHbo+ct10G9Ffp0mif17idi0IyWNVA/wcwcm7NPOD/WEHVP3n7n3MhXqxoIYm8d6MuZohYWIZ4T3w==}
+ dev: true
+
resolution: {integrity: sha512-gd3ypIPfOMr9h5jIKq8E3sHOTCjeirnl0WK5ZdS1AW0Odt0b1PaWaHdJ4Qk4klv+YB9aJBS7mESXjFoDQPu6DA==}
dependencies:
@@ -7611,7 +7849,6 @@ packages:
resolution: {integrity: sha512-aN97NXWF6AWBTahfVOIrB/NShkzi5H7F9r1s9mD3cDj4Ko5f2qhhVoYMibXF7GlLveb/D2ioWay8lxI97Ven3g==}
engines: {node: '>=0.12'}
- dev: false
resolution: {integrity: sha512-sqQamAnR14VgCr1A618A3sGrygcpK+HEbenA/HiEAkkUwcZIIB/tgWqHFxWgOyDh4nB4JCRimh79dR5Ywc9MDQ==}
@@ -7687,6 +7924,20 @@ packages:
resolution: {integrity: sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==}
engines: {node: '>= 0.4'}
+ resolution: {integrity: sha512-sPZmqHBe6JIiTfN5q2pEi//TwxmAFHwj/XEuYjTuse78i8KxaqMTTzxPoFKuzRpDpTJ+0NAbpfenkmH2rePtuw==}
+ dependencies:
+ call-bind: 1.0.9
+ get-intrinsic: 1.3.0
+ has-symbols: 1.1.0
+ is-arguments: 1.2.0
+ is-map: 2.0.3
+ is-set: 2.0.3
+ is-string: 1.1.1
+ isarray: 2.0.5
+ stop-iteration-iterator: 1.1.0
+ dev: true
+
resolution: {integrity: sha512-HVLACW1TppGYjJ8H6/jqH/pqOtKRw6wMlrB23xfExmFWxFquAIWCmwoLsOyN96K4a5KbmOf5At9ZUO3GZbetAw==}
engines: {node: '>= 0.4'}
@@ -8544,6 +8795,17 @@ packages:
engines: {node: '>= 14.17'}
dev: false
+ resolution: {integrity: sha512-8RipRLol37bNs2bhoV67fiTEvdTrbMUYcFTiy3+wuuOnUog2QBHCZWXDRijWQfAkhBj2Uf5UnVaiWwA5vdd82w==}
+ engines: {node: '>= 6'}
+ dependencies:
+ asynckit: 0.4.0
+ combined-stream: 1.0.8
+ es-set-tostringtag: 2.1.0
+ hasown: 2.0.3
+ mime-types: 2.1.35
+ dev: true
+
resolution: {integrity: sha512-wzsgA6WOq+09wrU1tsJ09udeR/YZRaeArL9e1wPbFg3GG2yDnC2ldKpxs4xunpFF9DgqCqOIra3bc1HWrJ37Ww==}
engines: {node: '>=0.4.x'}
@@ -8635,7 +8897,6 @@ packages:
resolution: {integrity: sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==}
engines: {node: '>=6.9.0'}
- dev: false
resolution: {integrity: sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==}
@@ -9032,6 +9293,13 @@ packages:
wbuf: 1.7.3
dev: false
+ resolution: {integrity: sha512-Y22oTqIU4uuPgEemfz7NDJz6OeKf12Lsu+QC+s3BVpda64lTiMYCyGwg5ki4vFxkMwQdeZDl2adZoqUgdFuTgQ==}
+ engines: {node: '>=18'}
+ dependencies:
+ whatwg-encoding: 3.1.1
+ dev: true
+
resolution: {integrity: sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==}
dev: false
@@ -9145,6 +9413,16 @@ packages:
resolution: {integrity: sha512-Pysuw9XpUq5dVc/2SMHpuTY01RFl8fttgcyunjL7eEMhGM3cI4eOmiCycJDVCo/7O7ClfQD3SaI6ftDzqOXYMA==}
dev: false
+ resolution: {integrity: sha512-T1gkAiYYDWYx3V5Bmyu7HcfcvL7mUrTWiM6yOfa3PIphViJ/gFPbvidQ+veqSOHci/PxBcDabeUNCzpOODJZig==}
+ engines: {node: '>= 14'}
+ dependencies:
+ agent-base: 7.1.4
+ debug: 4.4.3([email protected])
+ transitivePeerDependencies:
+ - supports-color
+ dev: true
+
resolution: {integrity: sha512-c1IyJYLYppU574+YI7R4QyX2ystMtVXZwIdzazUIPIJsHuWNd+mho2j+bKoHftndicGj9yh+xjd+l0yj7VeT1Q==}
engines: {node: '>=12.0.0'}
@@ -9210,6 +9488,13 @@ packages:
safer-buffer: 2.1.2
dev: false
+ resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==}
+ engines: {node: '>=0.10.0'}
+ dependencies:
+ safer-buffer: 2.1.2
+ dev: true
+
resolution: {integrity: sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw==}
engines: {node: '>=0.10.0'}
@@ -9255,7 +9540,6 @@ packages:
resolution: {integrity: sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg==}
engines: {node: '>=8'}
- dev: false
resolution: {integrity: sha512-Yg7+ztRkqslMAS2iFaU+Oa4KTSidr63OsFGlOrJoW981kIYO3CGCS3wA95P1mUi/IVSJkn0D479KTJpVpvFNuw==}
@@ -9333,6 +9617,14 @@ packages:
is-alphabetical: 2.0.1
is-decimal: 2.0.1
+ resolution: {integrity: sha512-7bVbi0huj/wrIAOzb8U1aszg9kdi3KN/CyU19CTI7tAoZYEZoL9yCDXpbXN+uPsuWnP02cyug1gleqq+TU+YCA==}
+ engines: {node: '>= 0.4'}
+ dependencies:
+ call-bound: 1.0.4
+ has-tostringtag: 1.0.2
+ dev: true
+
resolution: {integrity: sha512-DDfANUiiG2wC1qawP66qlTugJeL5HyzMpfr8lLK+jMQirGzNod0B12cFB/9q838Ru27sBwfw78/rdoU7RERz6A==}
engines: {node: '>= 0.4'}
@@ -9551,6 +9843,10 @@ packages:
dependencies:
isobject: 3.0.1
+ resolution: {integrity: sha512-bCYeRA2rVibKZd+s2625gGnGF/t7DSqDs4dP7CrLA1m7jKWz6pps0LpYLJN8Q64HtmPKJ1hrN3nzPNKFEKOUiQ==}
+ dev: true
+
resolution: {integrity: sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==}
dev: false
@@ -9768,11 +10064,46 @@ packages:
dependencies:
argparse: 2.0.1
+ resolution: {integrity: sha512-MyL55p3Ut3cXbeBEG7Hcv0mVM8pp8PBNWxRqchZnSfAiES1v1mRnMeFfaHWIPULpwsYfvO+ZmMZz5tGCnjzDUQ==}
+ engines: {node: '>=18'}
+ peerDependencies:
+ canvas: ^2.11.2
+ peerDependenciesMeta:
+ canvas:
+ optional: true
+ dependencies:
+ cssstyle: 4.6.0
+ data-urls: 5.0.0
+ decimal.js: 10.6.0
+ form-data: 4.0.5
+ html-encoding-sniffer: 4.0.0
+ http-proxy-agent: 7.0.2
+ https-proxy-agent: 7.0.6([email protected])
+ is-potential-custom-element-name: 1.0.1
+ nwsapi: 2.2.23
+ parse5: 7.3.0
+ rrweb-cssom: 0.7.1
+ saxes: 6.0.0
+ symbol-tree: 3.2.4
+ tough-cookie: 4.1.4
+ w3c-xmlserializer: 5.0.0
+ webidl-conversions: 7.0.0
+ whatwg-encoding: 3.1.1
+ whatwg-mimetype: 4.0.0
+ whatwg-url: 14.2.0
+ ws: 8.20.0
+ xml-name-validator: 5.0.0
+ transitivePeerDependencies:
+ - bufferutil
+ - supports-color
+ - utf-8-validate
+ dev: true
+
resolution: {integrity: sha512-/sM3dO2FOzXjKQhJuo0Q173wf2KOo8t4I8vHy6lF9poUp7bKT0/NHE8fPX23PwfhnykfqnC2xRxOnVw5XuGIaA==}
engines: {node: '>=6'}
hasBin: true
- dev: false
resolution: {integrity: sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==}
@@ -9806,7 +10137,6 @@ packages:
resolution: {integrity: sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==}
engines: {node: '>=6'}
hasBin: true
- dev: false
resolution: {integrity: sha512-zwOTdL3rFQ/lRdBnntKVOX6k5cKJwEc1HdilT71BWEu7J41gXIB2MRp+vxduPSwZJPWBxEzv4yH1wYLJGUHX4Q==}
@@ -9973,7 +10303,6 @@ packages:
resolution: {integrity: sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==}
dependencies:
yallist: 3.1.1
- dev: false
resolution: {integrity: sha512-3UZsqyswRXjW4t+nw+InICewSimjPKHuSxiFYqTshv9xkK3tPPntXk/lvXc9pKlXIxm3v9WKyoxcrB6YHhP+dg==}
@@ -9983,6 +10312,11 @@ packages:
react: 18.3.1
dev: false
+ resolution: {integrity: sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ==}
+ hasBin: true
+ dev: true
+
resolution: {integrity: sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==}
dependencies:
@@ -10646,7 +10980,6 @@ packages:
resolution: {integrity: sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==}
engines: {node: '>= 0.6'}
- dev: false
resolution: {integrity: sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==}
@@ -10664,7 +10997,6 @@ packages:
engines: {node: '>= 0.6'}
dependencies:
mime-db: 1.52.0
- dev: false
resolution: {integrity: sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A==}
@@ -10694,6 +11026,11 @@ packages:
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
dev: false
+ resolution: {integrity: sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==}
+ engines: {node: '>=4'}
+ dev: true
+
resolution: {integrity: sha512-AOSS0IdEB95ayVkxn5oGzNQwqAi2J0Jb/kKm43t7H73s8+f5873g0yuj0PNvK4dO75mu5DHg4nlgp4k6Kga8eg==}
engines: {node: '>= 12.13.0'}
@@ -10830,7 +11167,7 @@ packages:
resolution: {integrity: sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==}
resolution: {integrity: sha512-wlzrsbfeSU48YQBjZhDzOwhWhGsy+uQycR8bHAOt1LY1bn3zZEcDyHQOEoN3aWzQ8LHCAJ1nqrWCc9XF2+O45Q==}
engines: {node: '>=18.17.0'}
deprecated: This version has a security vulnerability. Please upgrade to a patched version. See https://nextjs.org/blog/security-update-2025-12-11 for more details.
@@ -10854,7 +11191,7 @@ packages:
postcss: 8.4.31
react: 18.3.1
react-dom: 18.3.1([email protected])
- styled-jsx: 5.1.1([email protected])
+ styled-jsx: 5.1.1(@babel/[email protected])([email protected])
optionalDependencies:
'@next/swc-darwin-arm64': 14.1.0
'@next/swc-darwin-x64': 14.1.0
@@ -10937,6 +11274,10 @@ packages:
webpack: 5.106.2
dev: false
+ resolution: {integrity: sha512-7wfH4sLbt4M0gCDzGE6vzQBo0bfTKjU7Sfpqy/7gs1qBfYz2vEJH6vXcBKpO3+6Yu1telwd0t9HpyOoLEQQbIQ==}
+ dev: true
+
resolution: {integrity: sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==}
engines: {node: '>=0.10.0'}
@@ -10950,6 +11291,14 @@ packages:
resolution: {integrity: sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==}
engines: {node: '>= 0.4'}
+ resolution: {integrity: sha512-F8cZ+KfGlSGi09lJT7/Nd6KJZ9ygtvYC0/UYYLI9nmQKLMnydpB9yvbv9K1uSkEu7FU9vYPmVwLg328tX+ot3Q==}
+ engines: {node: '>= 0.4'}
+ dependencies:
+ call-bind: 1.0.9
+ define-properties: 1.2.1
+ dev: true
+
resolution: {integrity: sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==}
engines: {node: '>= 0.4'}
@@ -11239,7 +11588,6 @@ packages:
resolution: {integrity: sha512-IInvU7fabl34qmi9gY8XOVxhYyMyuH2xUNpb2q8/Y+7552KlejkRvqvD19nMoUW/uQGGbqNpA6Tufu5FL5BZgw==}
dependencies:
entities: 6.0.1
- dev: false
resolution: {integrity: sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==}
@@ -12231,6 +12579,15 @@ packages:
renderkid: 3.0.0
dev: false
+ resolution: {integrity: sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ==}
+ engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0}
+ dependencies:
+ ansi-regex: 5.0.1
+ ansi-styles: 5.2.0
+ react-is: 17.0.2
+ dev: true
+
resolution: {integrity: sha512-28iF6xPQrP8Oa6uxE6a1biz+lWeTOAPKggvjB8HAs6nVMKZwf5bG++632Dx614hIWgUPkgivRfG+a8uAXGTIbA==}
engines: {node: '>=4'}
@@ -12285,6 +12642,12 @@ packages:
ipaddr.js: 1.9.1
dev: false
+ resolution: {integrity: sha512-JZd3gMVBAVQkSs6HdNZo9Sdo0LNcQeMNP3CozBJb3JYC/QUYZTnKxP+f8oWRX4rHP5EurWxqAHTSwUCjlNKa1w==}
+ dependencies:
+ punycode: 2.3.1
+ dev: true
+
resolution: {integrity: sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==}
engines: {node: '>=6'}
@@ -12321,6 +12684,10 @@ packages:
side-channel: 1.1.0
dev: false
+ resolution: {integrity: sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==}
+ dev: true
+
resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==}
@@ -12403,6 +12770,10 @@ packages:
resolution: {integrity: sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==}
+ resolution: {integrity: sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==}
+ dev: true
+
resolution: {integrity: sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==}
dev: false
@@ -12428,6 +12799,11 @@ packages:
webpack: 5.106.2
dev: false
+ resolution: {integrity: sha512-z6F7K9bV85EfseRCp2bzrpyQ0Gkw1uLoCel9XBVWPg/TjRj94SkJzUTGfOa4bs7iJvBWtQG0Wq7wnI0syw3EBQ==}
+ engines: {node: '>=0.10.0'}
+ dev: true
+
resolution: {integrity: sha512-9r+yi9+mgU33AKcj6IbT9oRCO78WriSj6t/cF8DWBZJ9aOGPOTEDvdUDz1FwKim7QXWwmHqtdHnRJfhAxEG46Q==}
engines: {node: '>=10'}
@@ -12653,6 +13029,14 @@ packages:
unified: 11.0.5
vfile: 6.0.3
+ resolution: {integrity: sha512-6tDA8g98We0zd0GvVeMT9arEOnTw9qM03L9cJXaCjrip1OO764RDBLBfrB4cwzNGDj5OA5ioymC9GkizgWJDUg==}
+ engines: {node: '>=8'}
+ dependencies:
+ indent-string: 4.0.0
+ strip-indent: 3.0.0
+ dev: true
+
resolution: {integrity: sha512-urBwgfrvVP/eAyXx4hluJivBKzuEbSQs9rKWCrCkbSxNv8mxPcUZKeuoF3Uy4mJl3Lwprp6yy5/39VWigZ4K6Q==}
dev: false
@@ -12854,7 +13238,6 @@ packages:
resolution: {integrity: sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==}
- dev: false
resolution: {integrity: sha512-0a1F4l73/ZFZOakJnQ3FvkJ2+gSTQWz/r2KE5OdDY0TxPm5h4GkqkWWfM47T7HsbnOtcJVEF4epCVy6u7Q3K+g==}
@@ -12972,6 +13355,14 @@ packages:
- supports-color
dev: false
+ resolution: {integrity: sha512-TrEMa7JGdVm0UThDJSx7ddw5nVm3UJS9o9CCIZ72B1vSyEZoziDqBYP3XIoi/12lKrJR8rE3jeFHMok2F/Mnsg==}
+ dev: true
+
+ resolution: {integrity: sha512-guoltQEx+9aMf2gDZ0s62EcV8lsXR+0w8915TC3ITdn2YueuNjdAYh/levpU9nFaoChh9RUS5ZdQMrKfVEN9tw==}
+ dev: true
+
resolution: {integrity: sha512-FI+pHEn7Wc4NqKXMXFM+VAYKEj/mRIcW4h24YVwVtyjI+EqGrLc2Hx/Ny0lrZ21cBWU2goLy36eqMcNj3AQJig==}
engines: {node: '>=12.0.0'}
@@ -13031,13 +13422,19 @@ packages:
resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==}
- dev: false
resolution: {integrity: sha512-6R3J5M4AcbtLUdZmRv2SygeVaM7IhrLXu9BmnOGmmACak8fiUtOsYNWUS4uK7upbmHIBbLBeFeI//477BKLBzA==}
engines: {node: '>=11.0.0'}
dev: false
+ resolution: {integrity: sha512-xAg7SOnEhrm5zI3puOOKyy1OMcMlIJZYNJY7xLBwSze0UjhPLnWfj2GF2EpT0jmzaJKIWKHLsaSSajf35bcYnA==}
+ engines: {node: '>=v12.22.7'}
+ dependencies:
+ xmlchars: 2.2.0
+ dev: true
+
resolution: {integrity: sha512-UOShsPwz7NrMUqhR6t0hWjFduvOzbtv7toDH1/hIrfRNIDBnnBWd0CwJTGvTpngVlmwGCdP9/Zl/tVrDqcuYzQ==}
dependencies:
@@ -13600,6 +13997,13 @@ packages:
engines: {node: '>=6'}
dev: false
+ resolution: {integrity: sha512-laJTa3Jb+VQpaC6DseHhF7dXVqHTfJPCRDaEbid/drOhgitgYku/letMUqOXFoWV0zIIUbjpdH2t+tYj4bQMRQ==}
+ engines: {node: '>=8'}
+ dependencies:
+ min-indent: 1.0.1
+ dev: true
+
resolution: {integrity: sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==}
engines: {node: '>=0.10.0'}
@@ -13619,7 +14023,7 @@ packages:
dependencies:
inline-style-parser: 0.2.7
resolution: {integrity: sha512-pW7uC1l4mBZ8ugbiZrcIsiIvVx1UmTfw7UkC3Um2tmfUq9Bhk8IiyEIPl6F8agHgjzku6j0xQEZbfA5uSgSaCw==}
engines: {node: '>= 12.0.0'}
peerDependencies:
@@ -13632,6 +14036,7 @@ packages:
babel-plugin-macros:
optional: true
dependencies:
+ '@babel/core': 7.29.0
client-only: 0.0.1
react: 18.3.1
dev: false
@@ -13709,6 +14114,10 @@ packages:
use-sync-external-store: 1.6.0([email protected])
dev: false
+ resolution: {integrity: sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==}
+ dev: true
+
resolution: {integrity: sha512-3ofp+LL8E+pK/JuPLPggVAIaEuhvIz4qNcf3nA1Xn2o/7fb7s/TYpHhwGDv1ZU3PkBluUVaF8PyCHcm48cKLWQ==}
engines: {node: '>=14.0.0'}
@@ -13861,6 +14270,23 @@ packages:
engines: {node: '>=6'}
dev: false
+ resolution: {integrity: sha512-Loo5UUvLD9ScZ6jh8beX1T6sO1w2/MpCRpEP7V280GKMVUQ0Jzar2U3UJPsrdbziLEMMhu3Ujnq//rhiFuIeag==}
+ engines: {node: '>=6'}
+ dependencies:
+ psl: 1.15.0
+ punycode: 2.3.1
+ universalify: 0.2.0
+ url-parse: 1.5.10
+ dev: true
+
+ resolution: {integrity: sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==}
+ engines: {node: '>=18'}
+ dependencies:
+ punycode: 2.3.1
+ dev: true
+
resolution: {integrity: sha512-rMuvhU4MCDbcbnleZTFezWsaZXRFemSqAM+7jPnzUl1fo9w3YEKOxAeui0fz3OI4EU4hf23iyA7uQRVko+UaBA==}
engines: {node: '>=10.0'}
@@ -14228,6 +14654,11 @@ packages:
unist-util-is: 6.0.1
unist-util-visit-parents: 6.0.2
+ resolution: {integrity: sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg==}
+ engines: {node: '>= 4.0.0'}
+ dev: true
+
resolution: {integrity: sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==}
engines: {node: '>= 10.0.0'}
@@ -14321,6 +14752,13 @@ packages:
webpack: 5.106.2
dev: false
+ resolution: {integrity: sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==}
+ dependencies:
+ querystringify: 2.2.0
+ requires-port: 1.0.0
+ dev: true
+
resolution: {integrity: sha512-jQL3lRnocaFtu3V00JToYz/4QkNWswxijDaCVNZRiRTO3HQDLsdu1ZtmIUvV4yPp+rvWm5j0y0TG/S61cuijTg==}
engines: {node: '>=10'}
@@ -14490,7 +14928,7 @@ packages:
fsevents: 2.3.3
dev: true
resolution: {integrity: sha512-MSmPM9REYqDGBI8439mA4mWhV5sKmDlBKWIYbA3lRb2PTHACE0mgKwA8yQ2xq9vxDTuk4iPrECBAEW2aoFXY0Q==}
engines: {node: ^18.0.0 || >=20.0.0}
hasBin: true
@@ -14526,6 +14964,7 @@ packages:
chai: 5.3.3
debug: 4.4.3([email protected])
expect-type: 1.3.0
+ jsdom: 24.1.3
magic-string: 0.30.21
pathe: 1.1.2
std-env: 3.10.0
@@ -14548,6 +14987,13 @@ packages:
- terser
dev: true
+ resolution: {integrity: sha512-o8qghlI8NZHU1lLPrpi2+Uq7abh4GGPpYANlalzWxyWteJOCsr/P+oPBA49TOLu5FTZO4d3F9MnWJfiMo4BkmA==}
+ engines: {node: '>=18'}
+ dependencies:
+ xml-name-validator: 5.0.0
+ dev: true
+
resolution: {integrity: sha512-Zn5uXdcFNIA1+1Ei5McRd+iRzfhENPCe7LeABkJtNulSxjma+l7ltNx55BWZkRlwRnpOgHqxnjyaDgJnNXnqzg==}
engines: {node: '>=10.13.0'}
@@ -14565,6 +15011,11 @@ packages:
resolution: {integrity: sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ==}
dev: false
+ resolution: {integrity: sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==}
+ engines: {node: '>=12'}
+ dev: true
+
resolution: {integrity: sha512-vJptkMm9pk5si4Bv922ZbKLV8UTT4zib4FPgXMhgzUny0bfDDkLXAVQs3ly3fS4/TN9ROFtb0NFrm04UXFE/Vw==}
engines: {node: '>= 10.13.0'}
@@ -14750,6 +15201,27 @@ packages:
engines: {node: '>=0.8.0'}
dev: false
+ resolution: {integrity: sha512-6qN4hJdMwfYBtE3YBTTHhoeuUrDBPZmbQaxWAqSALV/MeEnR5z1xd8UKud2RAkFoPkmB+hli1TZSnyi84xz1vQ==}
+ engines: {node: '>=18'}
+ deprecated: Use @exodus/bytes instead for a more spec-conformant and faster implementation
+ dependencies:
+ iconv-lite: 0.6.3
+ dev: true
+
+ resolution: {integrity: sha512-QaKxh0eNIi2mE9p2vEdzfagOKHCcj1pJ56EEHGQOVxp8r9/iszLUUV7v89x9O1p/T+NlTM5W7jW6+cz4Fq1YVg==}
+ engines: {node: '>=18'}
+ dev: true
+
+ resolution: {integrity: sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==}
+ engines: {node: '>=18'}
+ dependencies:
+ tr46: 5.1.1
+ webidl-conversions: 7.0.0
+ dev: true
+
resolution: {integrity: sha512-TbX3mj8n0odCBFVlY8AxkqcHASw3L60jIuF8jFP78az3C2YhmGvqbHBpAjTRH2/xqYunrJ9g1jSyjCjpoWzIAA==}
engines: {node: '>= 0.4'}
@@ -14887,7 +15359,6 @@ packages:
optional: true
utf-8-validate:
optional: true
- dev: false
resolution: {integrity: sha512-h3Fbisa2nKGPxCpm89Hk33lBLsnaGBvctQopaBSOW/uIs6FTe1ATyAnKFJrzVs9vpGdsTe73WF3V4lIsk4Gacw==}
@@ -14908,9 +15379,17 @@ packages:
sax: 1.6.0
dev: false
+ resolution: {integrity: sha512-EvGK8EJ3DhaHfbRlETOWAS5pO9MZITeauHKJyb8wyajUfQUenkIg2MvLDTZ4T/TgIcm3HU0TFBgWWboAZ30UHg==}
+ engines: {node: '>=18'}
+ dev: true
+
+ resolution: {integrity: sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==}
+ dev: true
+
resolution: {integrity: sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==}
- dev: false
resolution: {integrity: sha512-2PTINUwsRqSd+s8XxKaJWQlUuEMHJQyEuh2edBbW8KNJz0SJPwUSD2zRWqezFEdN7IzAgeuYHFUCF7o8zRdZ0A==}
diff --git a/ruff.toml b/ruff.toml
new file mode 100644
index 000000000..0d7073039
--- /dev/null
+++ b/ruff.toml
@@ -0,0 +1,44 @@
+# Repo-wide ruff configuration.
+#
+# Per-service `[tool.ruff]` blocks were consolidated here so the CI gate
+# (`ruff check services/api services/actions services/fusion services/agents`)
+# behaves the same way locally and in `.github/workflows/ci.yml`.
+
+line-length = 140
+target-version = "py311"
+
+[lint]
+select = ["E", "W", "F", "I", "B", "C4", "UP"]
+
+# UP042 (`class X(str, Enum)` → `enum.StrEnum`) is a Python 3.11+ stylistic
+# preference, not a correctness rule. The `(str, Enum)` pattern serializes
+# the same way through Pydantic v2 / SQLAlchemy / json.dumps and migrating
+# every existing model would create churn for no behavioral gain.
+ignore = ["UP042"]
+
+[lint.flake8-bugbear]
+# FastAPI's canonical dependency-injection pattern is
+# `param: T = Depends(...)` / `Query(...)` / `Security(...)`. Add them to the
+# bugbear allow-list so B008 keeps firing on real mistakes (e.g. `= dict()`).
+extend-immutable-calls = [
+ "fastapi.Depends",
+ "fastapi.Query",
+ "fastapi.Path",
+ "fastapi.Body",
+ "fastapi.Header",
+ "fastapi.Cookie",
+ "fastapi.Form",
+ "fastapi.File",
+ "fastapi.Security",
+]
+
+[lint.per-file-ignores]
+# SQLAlchemy declarative models use string forward references inside
+# `Mapped[...]` (e.g., `Mapped["Tenant"]`). These resolve via the
+# ORM registry at runtime; ruff's F821 fires anyway because it only
+# does static name resolution.
+"services/api/app/models/**/*.py" = ["F821"]
+
+# Test fixtures intentionally bind names they don't reference (e.g. a
+# helper that builds a plugin on disk for side effects only).
+"services/*/tests/**/*.py" = ["B008"]
diff --git a/scripts/validate_detections.py b/scripts/validate_detections.py
index 1966ccee4..1b997ffe5 100755
--- a/scripts/validate_detections.py
+++ b/scripts/validate_detections.py
@@ -2,24 +2,39 @@
"""
AiSOC Detection Rule Validator
================================
-Validates all YAML detection rules under detections/ for:
+Validates all YAML detection rules under ``detections/`` for:
+
- Valid YAML syntax
- - Required fields (id, name, severity, detection)
- - Severity is one of: low | medium | high | critical
- - Category is one of: network | endpoint | cloud | identity | application | data-exfil
- - No duplicate `id` values across all rules
- - id prefix matches the category directory
- - Each rule has matching positive + negative fixtures under detections/fixtures/
- - Fixture-replay: positive fixture must satisfy match_when, negative must not
-
-Fixture replay uses the canonical `matches()` function from
-`generate_detections.py` against the structured `match_when` dict from the
-spec table, NOT a re-parse of the rendered YAML condition string. The YAML
-condition is purely a serialized artifact for human readability — the spec
-is the source of truth.
-
-Exit code 0 = all rules valid
-Exit code 1 = one or more validation errors
+ - Required fields (``id``, ``name``, ``severity``, ``detection``)
+ - Severity is one of: ``low | medium | high | critical``
+ - Category is one of:
+ ``network | endpoint | cloud | identity | application | data-exfil``
+ - No duplicate ``id`` values across all tiers
+ - ``id`` prefix matches the rule's tier (``det-`` for native,
+ ``<source>-...`` for imported tiers)
+ - For native rules: matching positive + negative fixtures under
+ ``detections/fixtures/`` and successful fixture replay against the
+ canonical ``match_when`` spec
+ - For imported rules: a populated ``provenance`` block with the required
+ fields documented in ``tools/detection_import/README.md``
+
+Tiering
+-------
+* **native** — ``detections/{network,endpoint,cloud,identity,application,
+ data-exfil}/*.yaml``. Strict: fixtures + provenance optional.
+* **imported** — ``detections/{sigma,car,splunk,chronicle}-imports/<category>
+ /*.yaml`` (and ``.../_quarantine/<category>/*.yaml`` for rules that
+ parsed but cannot run as-is). Provenance required, fixtures optional.
+* **community** — ``detections/community/<category>/*.yaml``. Permissive;
+ provenance encouraged but not required.
+
+Exit codes
+----------
+* ``0`` — all rules valid
+* ``1`` — one or more validation errors
+
+Run ``python3 scripts/validate_detections.py --strict-fixtures`` to promote
+fixture warnings into hard failures (used by CI for native rules).
"""
from __future__ import annotations
@@ -43,7 +58,6 @@ SCRIPTS_DIR = ROOT / "scripts"
if str(SCRIPTS_DIR) not in sys.path:
sys.path.insert(0, str(SCRIPTS_DIR))
-# Canonical matcher and spec table — single source of truth for replay.
from generate_detections import matches # noqa: E402
from detection_specs_index import all_specs # noqa: E402
@@ -59,11 +73,45 @@ VALID_CATEGORIES = {
REQUIRED_FIELDS = ["id", "name", "severity", "detection"]
+# Provenance fields required on every imported rule. License and
+# license_url should be present so we can prove the redistribution chain;
+# imported_by is the importer module so future audits can grep for the
+# tool that produced the file.
+REQUIRED_PROVENANCE_FIELDS = (
+ "source",
+ "source_id",
+ "source_commit",
+ "license",
+ "imported_at",
+ "imported_by",
+ "upstream_path",
+)
+
+# Each imported tier directory maps to (display name, expected id prefix).
+# The id prefix matches what ``tools.detection_import.common.stable_id``
+# produces for that tier. Bumping importer source names? Update these too.
+IMPORTED_TIERS: dict[str, dict[str, str]] = {
+ "sigma-imports": {
+ "name": "sigmahq",
+ "id_prefix": "sigmahq-sigma-",
+ },
+ "car-imports": {
+ "name": "mitre-car",
+ "id_prefix": "mitre-car-",
+ },
+ "splunk-imports": {
+ "name": "splunk-security-content",
+ "id_prefix": "splunk-security-content-",
+ },
+ "chronicle-imports": {
+ "name": "chronicle-detection-rules",
+ "id_prefix": "chronicle-detection-rules-",
+ },
+}
+
# =============================================================================
-# Spec lookup — map (category, slug) → match_when dict from the canonical
-# spec tables so we can replay fixtures against the structured spec, not a
-# re-parsed YAML condition string.
+# Spec lookup — for native fixture replay only.
# =============================================================================
_SPEC_BY_KEY: dict[tuple[str, str], dict[str, Any]] = {
@@ -71,17 +119,106 @@ _SPEC_BY_KEY: dict[tuple[str, str], dict[str, Any]] = {
}
+# =============================================================================
+# Tier classification
+# =============================================================================
+
+
+def classify(path: Path) -> dict[str, Any]:
+ """Classify a rule file by tier from its on-disk path.
+
+ Returns a dict with keys:
+
+ * ``tier`` — ``"native" | "imported" | "community" | "unknown"``
+ * ``source`` — short source name for imported rules, else ``None``
+ * ``id_prefix`` — required id prefix for the tier, else ``None``
+ * ``category`` — the AiSOC category this rule belongs to (e.g.
+ ``cloud``), inferred from the directory layout
+ * ``is_quarantined`` — true if the rule lives under ``_quarantine/``
+ """
+ try:
+ rel_parts = path.relative_to(DETECTIONS_DIR).parts
+ except ValueError:
+ return {
+ "tier": "unknown",
+ "source": None,
+ "id_prefix": None,
+ "category": None,
+ "is_quarantined": False,
+ }
+
+ if not rel_parts:
+ return {
+ "tier": "unknown",
+ "source": None,
+ "id_prefix": None,
+ "category": None,
+ "is_quarantined": False,
+ }
+
+ head = rel_parts[0]
+
+ # Native: detections/<category>/<rule>.yaml
+ if head in VALID_CATEGORIES:
+ return {
+ "tier": "native",
+ "source": None,
+ "id_prefix": "det-",
+ "category": head,
+ "is_quarantined": False,
+ }
+
+ # Imported: detections/<source>-imports/[<_quarantine>/]<category>/<rule>.yaml
+ if head in IMPORTED_TIERS:
+ tier_meta = IMPORTED_TIERS[head]
+ is_quarantined = len(rel_parts) >= 2 and rel_parts[1] == "_quarantine"
+ if is_quarantined and len(rel_parts) >= 3:
+ category = rel_parts[2]
+ elif len(rel_parts) >= 2:
+ category = rel_parts[1]
+ else:
+ category = None
+ return {
+ "tier": "imported",
+ "source": tier_meta["name"],
+ "id_prefix": tier_meta["id_prefix"],
+ "category": category,
+ "is_quarantined": is_quarantined,
+ }
+
+ if head == "community":
+ category = rel_parts[1] if len(rel_parts) >= 2 else None
+ return {
+ "tier": "community",
+ "source": "community",
+ "id_prefix": None,
+ "category": category,
+ "is_quarantined": False,
+ }
+
+ return {
+ "tier": "unknown",
+ "source": None,
+ "id_prefix": None,
+ "category": None,
+ "is_quarantined": False,
+ }
+
+
# =============================================================================
# Validation
# =============================================================================
def validate_rule(
- path: Path, seen_ids: dict[str, Path]
+ path: Path,
+ seen_ids: dict[str, Path],
+ classification: dict[str, Any],
) -> tuple[list[str], dict[str, Any] | None]:
"""Validate a single detection rule file.
- Returns (errors, parsed_rule). If parsing failed, parsed_rule is None.
+ Returns ``(errors, parsed_rule)``. ``parsed_rule`` is ``None`` if YAML
+ parsing failed.
"""
errors: list[str] = []
@@ -109,26 +246,36 @@ def validate_rule(
f"{', '.join(sorted(VALID_SEVERITIES))}"
)
- category = rule.get("category")
- if category and category not in VALID_CATEGORIES:
+ rule_category = rule.get("category")
+ if rule_category and rule_category not in VALID_CATEGORIES:
errors.append(
- f"Invalid category '{category}'; must be one of: "
+ f"Invalid category '{rule_category}'; must be one of: "
f"{', '.join(sorted(VALID_CATEGORIES))}"
)
- category_dir = path.parent.name
- if category and category != category_dir:
+ expected_category = classification["category"]
+ if (
+ rule_category
+ and expected_category
+ and rule_category != expected_category
+ ):
errors.append(
- f"Rule category '{category}' does not match directory '{category_dir}'"
+ f"Rule category '{rule_category}' does not match directory "
+ f"'{expected_category}'"
)
- rule_id = rule["id"]
- if not str(rule_id).startswith("det-"):
- errors.append(f"Rule id '{rule_id}' must start with 'det-'")
+ rule_id = str(rule["id"])
+ expected_prefix = classification["id_prefix"]
+ if expected_prefix and not rule_id.startswith(expected_prefix):
+ errors.append(
+ f"Rule id '{rule_id}' must start with '{expected_prefix}' "
+ f"(tier: {classification['tier']})"
+ )
if rule_id in seen_ids:
errors.append(
- f"Duplicate id '{rule_id}' — already defined in {seen_ids[rule_id]}"
+ f"Duplicate id '{rule_id}' — already defined in "
+ f"{seen_ids[rule_id]}"
)
else:
seen_ids[rule_id] = path
@@ -136,23 +283,45 @@ def validate_rule(
detection = rule.get("detection")
if not isinstance(detection, dict):
errors.append(
- "'detection' field must be a YAML mapping with 'condition' or 'keywords'"
+ "'detection' field must be a YAML mapping with 'condition', "
+ "'keywords', or a tier-specific block (e.g. 'splunk_spl', "
+ "'chronicle_yaral')"
)
+ if classification["tier"] == "imported":
+ errors.extend(_validate_provenance(rule))
+
return errors, rule
+def _validate_provenance(rule: dict[str, Any]) -> list[str]:
+ """Validate the ``provenance`` block on an imported rule."""
+ errors: list[str] = []
+ provenance = rule.get("provenance")
+ if not isinstance(provenance, dict):
+ return [
+ "Imported rule is missing required 'provenance' block "
+ "(see tools/detection_import/README.md)"
+ ]
+ for field in REQUIRED_PROVENANCE_FIELDS:
+ value = provenance.get(field)
+ if value in (None, ""):
+ errors.append(f"provenance.{field} is required and must be non-empty")
+ return errors
+
+
def replay_fixture(
rule_path: Path, rule: dict[str, Any], strict: bool
) -> list[str]:
"""Replay positive + negative fixtures against the canonical spec.
- Looks up the spec for this rule by (category, slug), then evaluates
- the structured `match_when` dict against the on-disk fixtures using
- the same `matches()` function the runtime engine would use.
+ Native tier only. Looks up the spec for this rule by ``(category,
+ slug)``, then evaluates the structured ``match_when`` dict against the
+ on-disk fixtures using the same ``matches()`` function the runtime
+ engine would use.
- Returns a list of error messages. If `strict` is False, missing fixtures
- or missing specs degrade to soft warnings.
+ Returns a list of error messages. If ``strict`` is ``False``, missing
+ fixtures or missing specs degrade to soft warnings (``WARN: ...``).
"""
errors: list[str] = []
slug = rule_path.stem
@@ -166,9 +335,13 @@ def replay_fixture(
if pos_missing or neg_missing:
msg_parts = []
if pos_missing:
- msg_parts.append(f"missing positive fixture {pos_path.relative_to(ROOT)}")
+ msg_parts.append(
+ f"missing positive fixture {pos_path.relative_to(ROOT)}"
+ )
if neg_missing:
- msg_parts.append(f"missing negative fixture {neg_path.relative_to(ROOT)}")
+ msg_parts.append(
+ f"missing negative fixture {neg_path.relative_to(ROOT)}"
+ )
msg = "; ".join(msg_parts)
if strict:
errors.append(msg)
@@ -187,7 +360,7 @@ def replay_fixture(
match_when = spec.get("match_when")
if not match_when:
- return errors # nothing to replay
+ return errors
try:
with open(pos_path) as f:
@@ -224,20 +397,47 @@ def main() -> int:
total = 0
failed = 0
fixture_warnings = 0
+ tier_counts: dict[str, int] = {
+ "native": 0,
+ "imported": 0,
+ "community": 0,
+ "unknown": 0,
+ }
+ quarantine_count = 0
for path in yaml_files:
- # Skip auto-imported sigma rules — those are validated separately.
- if "sigma-imports" in path.parts:
- continue
rel = path.relative_to(ROOT)
+
+ # Skip the meta/index files at the top of detections/, e.g.
+ # detections/coverage.yaml, that aren't rule files. Those live
+ # directly under detections/ with no category dir.
+ try:
+ rel_under = path.relative_to(DETECTIONS_DIR)
+ except ValueError:
+ continue
+ if len(rel_under.parts) < 2:
+ continue
+ if rel_under.parts[0] == "fixtures":
+ continue
+
+ classification = classify(path)
+ tier = classification["tier"]
+ tier_counts[tier] = tier_counts.get(tier, 0) + 1
+ if classification["is_quarantined"]:
+ quarantine_count += 1
+
total += 1
- errors, rule = validate_rule(path, seen_ids)
+ errors, rule = validate_rule(path, seen_ids, classification)
rule_failed = bool(errors)
- # Fixture replay only for category rules (skip top-level files)
replay_errors: list[str] = []
- if rule and not rule_failed and path.parent.name in VALID_CATEGORIES:
+ if (
+ rule
+ and not rule_failed
+ and tier == "native"
+ and path.parent.name in VALID_CATEGORIES
+ ):
replay_errors = replay_fixture(path, rule, strict=strict)
warnings = [e for e in replay_errors if e.startswith("WARN:")]
@@ -249,20 +449,31 @@ def main() -> int:
if rule_failed:
failed += 1
- print(f"\nFAIL {rel}")
+ print(f"\nFAIL [{tier}] {rel}")
for e in errors:
print(f" - {e}")
else:
warn_suffix = f" ({len(warnings)} warn)" if warnings else ""
- print(f"PASS {rel}{warn_suffix}")
+ quarantine_suffix = " [quarantined]" if classification["is_quarantined"] else ""
+ print(f"PASS [{tier}] {rel}{warn_suffix}{quarantine_suffix}")
for w in warnings:
print(f" {w}")
- print(f"\n{'─' * 50}")
+ print(f"\n{'─' * 60}")
print(
f"Validated {total} rules — {total - failed} passed, {failed} failed, "
f"{fixture_warnings} fixture warnings"
)
+ print(
+ " Tiers: "
+ + ", ".join(
+ f"{tier}={count}"
+ for tier, count in sorted(tier_counts.items())
+ if count > 0
+ )
+ )
+ if quarantine_count:
+ print(f" Quarantined (parsed-but-disabled): {quarantine_count}")
return 1 if failed > 0 else 0
diff --git a/services/actions/app/api/router.py b/services/actions/app/api/router.py
index 8f6af41d5..b0c82b344 100644
--- a/services/actions/app/api/router.py
+++ b/services/actions/app/api/router.py
@@ -1,15 +1,16 @@
"""
Action Execution Service REST API.
"""
+
from __future__ import annotations
from typing import Any
-from uuid import UUID, uuid4
+from uuid import UUID
import structlog
from fastapi import APIRouter, HTTPException
-from app.models.action import ActionRequest, ActionResult, ActionStatus, ActionType
+from app.models.action import ActionRequest, ActionStatus, ActionType
from app.services.blast_radius import BlastRadiusGate
from app.services.executor_registry import EXECUTOR_REGISTRY
diff --git a/services/actions/app/executors/base.py b/services/actions/app/executors/base.py
index 417fff9e5..0eb4163c7 100644
--- a/services/actions/app/executors/base.py
+++ b/services/actions/app/executors/base.py
@@ -1,6 +1,7 @@
"""
Base executor interface for all action types.
"""
+
from __future__ import annotations
from abc import ABC, abstractmethod
diff --git a/services/actions/app/executors/endpoint.py b/services/actions/app/executors/endpoint.py
index 100ab3df9..e34eddb0f 100644
--- a/services/actions/app/executors/endpoint.py
+++ b/services/actions/app/executors/endpoint.py
@@ -1,6 +1,7 @@
"""
Endpoint action executors: isolate host, quarantine file, kill process.
"""
+
from __future__ import annotations
from datetime import datetime
diff --git a/services/actions/app/executors/network.py b/services/actions/app/executors/network.py
index f320485ab..7f9835328 100644
--- a/services/actions/app/executors/network.py
+++ b/services/actions/app/executors/network.py
@@ -2,6 +2,7 @@
Network action executors: block IP, block domain.
These are simulation stubs — integrate with your firewall/EDR/SIEM APIs.
"""
+
from __future__ import annotations
from datetime import datetime
diff --git a/services/actions/app/executors/notification.py b/services/actions/app/executors/notification.py
index 341611b71..de7dd08b4 100644
--- a/services/actions/app/executors/notification.py
+++ b/services/actions/app/executors/notification.py
@@ -1,6 +1,7 @@
"""
Notification executors: Slack alerts, ticket creation.
"""
+
from __future__ import annotations
from datetime import datetime
diff --git a/services/actions/app/main.py b/services/actions/app/main.py
index 2ee785efd..78a35f3ec 100644
--- a/services/actions/app/main.py
+++ b/services/actions/app/main.py
@@ -1,5 +1,5 @@
-import structlog
from fastapi import FastAPI
+
from app.api.router import router
app = FastAPI(
diff --git a/services/actions/app/models/action.py b/services/actions/app/models/action.py
index 3b638cf02..16d386c77 100644
--- a/services/actions/app/models/action.py
+++ b/services/actions/app/models/action.py
@@ -1,6 +1,7 @@
"""
Action models for the Action Execution Service.
"""
+
from __future__ import annotations
from datetime import datetime
@@ -38,10 +39,10 @@ class ActionStatus(str, Enum):
class BlastRadius(str, Enum):
- MINIMAL = "minimal" # Notification-only, no infra changes
- LOW = "low" # Reversible, limited scope
- MEDIUM = "medium" # Affects single resource, reversible
- HIGH = "high" # Affects multiple resources or users
+ MINIMAL = "minimal" # Notification-only, no infra changes
+ LOW = "low" # Reversible, limited scope
+ MEDIUM = "medium" # Affects single resource, reversible
+ HIGH = "high" # Affects multiple resources or users
CRITICAL = "critical" # Broad impact, potential service disruption
@@ -72,6 +73,7 @@ APPROVAL_REQUIRED_ACTIONS = {
class ActionRequest(BaseModel):
"""Request to execute an action."""
+
id: UUID = Field(default_factory=uuid4)
incident_id: UUID
tenant_id: UUID
@@ -86,6 +88,7 @@ class ActionRequest(BaseModel):
class ActionResult(BaseModel):
"""Result of an executed action."""
+
action_id: UUID
status: ActionStatus
blast_radius: BlastRadius
diff --git a/services/actions/app/services/blast_radius.py b/services/actions/app/services/blast_radius.py
index 06e51c2e1..83549f542 100644
--- a/services/actions/app/services/blast_radius.py
+++ b/services/actions/app/services/blast_radius.py
@@ -2,6 +2,7 @@
Blast-radius gate: evaluates action risk before execution.
Enforces approval workflows for high-risk actions.
"""
+
from __future__ import annotations
import structlog
diff --git a/services/actions/app/services/executor_registry.py b/services/actions/app/services/executor_registry.py
index abad3bc1c..f043a4850 100644
--- a/services/actions/app/services/executor_registry.py
+++ b/services/actions/app/services/executor_registry.py
@@ -1,6 +1,7 @@
"""
Executor registry: maps ActionType to executor implementation.
"""
+
from app.executors.endpoint import IsolateHostExecutor, KillProcessExecutor, QuarantineFileExecutor
from app.executors.network import BlockDomainExecutor, BlockIPExecutor
from app.executors.notification import CreateTicketExecutor, NotifySlackExecutor
diff --git a/services/agents/app/agents/enrichment_agent.py b/services/agents/app/agents/enrichment_agent.py
index c56a72ee7..942c7e91d 100644
--- a/services/agents/app/agents/enrichment_agent.py
+++ b/services/agents/app/agents/enrichment_agent.py
@@ -1,6 +1,7 @@
"""
Enrichment Agent: bulk-enriches all pending IOCs via the enrichment microservice.
"""
+
from __future__ import annotations
import structlog
@@ -33,21 +34,14 @@ async def run_enrichment(state: InvestigationState) -> InvestigationState:
if threat_class in ("malicious", "suspicious"):
malicious_count += 1
score = result.get("malicious_score", 0)
- state.add_finding(
- f"IOC '{ioc_value}' classified as {threat_class} "
- f"(malicious_score={score})"
- )
+ state.add_finding(f"IOC '{ioc_value}' classified as {threat_class} (malicious_score={score})")
state.ioc_enrichments = enriched
if malicious_count > 0:
- state.add_finding(
- f"Enrichment complete: {malicious_count}/{len(results)} IOCs are malicious/suspicious"
- )
+ state.add_finding(f"Enrichment complete: {malicious_count}/{len(results)} IOCs are malicious/suspicious")
else:
- state.add_finding(
- f"Enrichment complete: {len(results)} IOCs enriched, none flagged as malicious"
- )
+ state.add_finding(f"Enrichment complete: {len(results)} IOCs enriched, none flagged as malicious")
logger.info(
"Enrichment complete",
diff --git a/services/agents/app/agents/investigation_agent.py b/services/agents/app/agents/investigation_agent.py
index 440ebeb88..2c71eebaf 100644
--- a/services/agents/app/agents/investigation_agent.py
+++ b/services/agents/app/agents/investigation_agent.py
@@ -2,6 +2,7 @@
Investigation Agent: synthesizes findings from triage and enrichment,
generates a structured investigation report with recommended actions.
"""
+
from __future__ import annotations
import structlog
@@ -21,11 +22,7 @@ async def run_investigation(state: InvestigationState) -> InvestigationState:
state.iteration_count += 1
# Analyze enrichment results for threat patterns
- malicious_iocs = {
- k: v
- for k, v in state.ioc_enrichments.items()
- if v.get("threat_classification") in ("malicious", "suspicious")
- }
+ malicious_iocs = {k: v for k, v in state.ioc_enrichments.items() if v.get("threat_classification") in ("malicious", "suspicious")}
# Analyze MITRE techniques for attack stage
attack_stages = set()
@@ -35,10 +32,7 @@ async def run_investigation(state: InvestigationState) -> InvestigationState:
# --- Generate narrative findings ---
if malicious_iocs:
- state.add_finding(
- f"CONFIRMED THREAT: {len(malicious_iocs)} malicious IOC(s) identified. "
- f"Immediate containment recommended."
- )
+ state.add_finding(f"CONFIRMED THREAT: {len(malicious_iocs)} malicious IOC(s) identified. Immediate containment recommended.")
if attack_stages:
state.add_finding(
@@ -77,8 +71,7 @@ async def run_investigation(state: InvestigationState) -> InvestigationState:
# --- Exfiltration detection ---
if "Exfiltration" in attack_stages or "Command and Control" in attack_stages:
state.add_finding(
- "CRITICAL: Evidence of C2 or exfiltration stage detected. "
- "Recommend immediate network isolation and forensic acquisition."
+ "CRITICAL: Evidence of C2 or exfiltration stage detected. Recommend immediate network isolation and forensic acquisition."
)
state.proposed_actions.append(
ProposedAction(
@@ -92,9 +85,7 @@ async def run_investigation(state: InvestigationState) -> InvestigationState:
)
state.status = AgentStatus.COMPLETED
- state.add_finding(
- f"Investigation complete. Total proposed actions: {len(state.proposed_actions)}"
- )
+ state.add_finding(f"Investigation complete. Total proposed actions: {len(state.proposed_actions)}")
logger.info(
"Investigation complete",
diff --git a/services/agents/app/agents/triage_agent.py b/services/agents/app/agents/triage_agent.py
index 45d393cb1..c15915798 100644
--- a/services/agents/app/agents/triage_agent.py
+++ b/services/agents/app/agents/triage_agent.py
@@ -2,6 +2,7 @@
Triage Agent: first responder that classifies severity, extracts IOCs,
maps MITRE techniques, and decides whether automated response is safe.
"""
+
from __future__ import annotations
import structlog
@@ -12,14 +13,30 @@ from app.tools.mitre import map_techniques_to_kill_chain
logger = structlog.get_logger()
_CRITICAL_KEYWORDS = {
- "ransomware", "lateral movement", "credential dump", "domain admin",
- "exfiltration", "mimikatz", "cobalt strike", "c2", "beacon",
- "rootkit", "supply chain", "zero-day", "data breach",
+ "ransomware",
+ "lateral movement",
+ "credential dump",
+ "domain admin",
+ "exfiltration",
+ "mimikatz",
+ "cobalt strike",
+ "c2",
+ "beacon",
+ "rootkit",
+ "supply chain",
+ "zero-day",
+ "data breach",
}
_HIGH_KEYWORDS = {
- "phishing", "malware", "exploit", "privilege escalation",
- "brute force", "suspicious login", "anomaly", "backdoor",
+ "phishing",
+ "malware",
+ "exploit",
+ "privilege escalation",
+ "brute force",
+ "suspicious login",
+ "anomaly",
+ "backdoor",
}
diff --git a/services/agents/app/api/contextual.py b/services/agents/app/api/contextual.py
index dd8054c16..70557a54b 100644
--- a/services/agents/app/api/contextual.py
+++ b/services/agents/app/api/contextual.py
@@ -29,14 +29,16 @@ Page → action matrix (kept in sync with ``ContextualActions.tsx``):
Falls back to a deterministic synthetic response if ``OPENAI_API_KEY`` is
unset, so the demo path never breaks.
"""
+
from __future__ import annotations
import json
import os
import time
import uuid
-from datetime import datetime, timezone
-from typing import Any, AsyncIterator
+from collections.abc import AsyncIterator
+from datetime import UTC, datetime
+from typing import Any
import structlog
from fastapi import APIRouter, HTTPException
@@ -52,6 +54,7 @@ router = APIRouter(prefix="/api/v1/contextual", tags=["contextual"])
# Request / response shapes
# ---------------------------------------------------------------------------
+
class ContextualActionRequest(BaseModel):
"""A single contextual ambient-copilot call.
@@ -61,9 +64,7 @@ class ContextualActionRequest(BaseModel):
answer based on ``entity_id`` alone.
"""
- page: str = Field(
- ..., description="One of: alerts, cases, detections, playbooks"
- )
+ page: str = Field(..., description="One of: alerts, cases, detections, playbooks")
action: str = Field(..., description="Action key. See /actions for the catalogue.")
entity_id: str = Field(..., description="ID of the alert / case / rule / playbook.")
entity: dict[str, Any] | None = Field(
@@ -350,6 +351,7 @@ def _build_messages(req: ContextualActionRequest) -> tuple[str, str]:
# LLM dispatch
# ---------------------------------------------------------------------------
+
async def _call_llm(system: str, user: str, model: str) -> tuple[str, int]:
"""Invoke the configured LLM. Returns (markdown, tokens_used).
@@ -360,23 +362,18 @@ async def _call_llm(system: str, user: str, model: str) -> tuple[str, int]:
return _fallback_response(system, user), 0
try:
- from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
+ from langchain_openai import ChatOpenAI
except ImportError as exc:
logger.warning("contextual.llm.import_failed", error=str(exc))
return _fallback_response(system, user), 0
llm = ChatOpenAI(model=model, temperature=0.2)
- response = await llm.ainvoke(
- [SystemMessage(content=system), HumanMessage(content=user)]
- )
+ response = await llm.ainvoke([SystemMessage(content=system), HumanMessage(content=user)])
text = response.content if isinstance(response.content, str) else str(response.content)
tokens = 0
if hasattr(response, "response_metadata"):
- tokens = (
- response.response_metadata.get("token_usage", {}).get("total_tokens", 0)
- or 0
- )
+ tokens = response.response_metadata.get("token_usage", {}).get("total_tokens", 0) or 0
return text, tokens
@@ -391,8 +388,8 @@ async def _stream_llm(system: str, user: str, model: str) -> AsyncIterator[str]:
return
try:
- from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
+ from langchain_openai import ChatOpenAI
except ImportError:
text = _fallback_response(system, user)
for i in range(0, len(text), 8):
@@ -400,9 +397,7 @@ async def _stream_llm(system: str, user: str, model: str) -> AsyncIterator[str]:
return
llm = ChatOpenAI(model=model, temperature=0.2, streaming=True)
- async for chunk in llm.astream(
- [SystemMessage(content=system), HumanMessage(content=user)]
- ):
+ async for chunk in llm.astream([SystemMessage(content=system), HumanMessage(content=user)]):
if hasattr(chunk, "content") and chunk.content:
yield chunk.content if isinstance(chunk.content, str) else str(chunk.content)
@@ -429,13 +424,11 @@ def _fallback_response(system: str, user: str) -> str:
# Endpoints
# ---------------------------------------------------------------------------
+
@router.get("/actions", response_model=ContextualActionsCatalogue, summary="List supported contextual actions")
async def list_actions() -> ContextualActionsCatalogue:
return ContextualActionsCatalogue(
- pages={
- page: [ContextualActionDescriptor(**item) for item in items]
- for page, items in _ACTION_CATALOGUE.items()
- }
+ pages={page: [ContextualActionDescriptor(**item) for item in items] for page, items in _ACTION_CATALOGUE.items()}
)
@@ -481,7 +474,7 @@ async def run_action(req: ContextualActionRequest) -> ContextualActionResponse:
model=model,
elapsed_ms=elapsed_ms,
fallback=fallback,
- created_at=datetime.now(timezone.utc).isoformat(),
+ created_at=datetime.now(UTC).isoformat(),
)
# Structured log so contextual usage is queryable in observability tooling
diff --git a/services/agents/app/api/investigate.py b/services/agents/app/api/investigate.py
index fade66a83..4e3383fe5 100644
--- a/services/agents/app/api/investigate.py
+++ b/services/agents/app/api/investigate.py
@@ -9,6 +9,7 @@ Endpoints:
GET /api/v1/investigations/{run_id}/report.pdf → weasyprint PDF
WS /api/v1/investigations/{run_id}/stream → SSE-style step stream
"""
+
from __future__ import annotations
import asyncio
@@ -24,7 +25,7 @@ from fastapi import APIRouter, BackgroundTasks, HTTPException, WebSocket, WebSoc
from fastapi.responses import HTMLResponse, PlainTextResponse
from pydantic import BaseModel
-from app.investigator import InvestigatorOrchestrator, InvestigatorState
+from app.investigator import InvestigatorOrchestrator
logger = structlog.get_logger()
router = APIRouter(prefix="/api/v1", tags=["investigations"])
@@ -46,6 +47,7 @@ _orch = InvestigatorOrchestrator()
# Request / Response schemas
# ---------------------------------------------------------------------------
+
class InvestigateRequest(BaseModel):
alert_summary: str
raw_alert: dict[str, Any] = {}
@@ -63,6 +65,7 @@ class InvestigateResponse(BaseModel):
# Realtime broadcast helper
# ---------------------------------------------------------------------------
+
async def _emit_event(run_id: str, tenant_id: str, event: dict[str, Any]) -> None:
"""Forward an agent step event to the realtime service (best-effort)."""
url = f"{_REALTIME_URL}/internal/agent-event"
@@ -88,6 +91,7 @@ async def _emit_event(run_id: str, tenant_id: str, event: dict[str, Any]) -> Non
# Background task: runs investigation and streams steps to realtime service
# ---------------------------------------------------------------------------
+
async def _run_and_store(run_id: str, case_id: str, req: InvestigateRequest) -> None:
audit_log: list[dict[str, Any]] = []
# Reuse the API-issued run id as the ledger row id so consumers can
@@ -114,33 +118,43 @@ async def _run_and_store(run_id: str, case_id: str, req: InvestigateRequest) ->
elif event.get("type") == "done":
state_data = event.get("state", {})
- _runs[run_id].update({
- "status": "completed",
- "report_md": state_data.get("report_md", ""),
- "report_html": state_data.get("report_html", ""),
- "audit_log": audit_log,
- "recon": state_data.get("recon", {}),
- "forensic": state_data.get("forensic", {}),
- "responder": state_data.get("responder", {}),
- "completed_at": datetime.utcnow().isoformat(),
- "error": None,
- })
- await _emit_event(run_id, req.tenant_id, {
- "kind": "completed",
- "agent": "orchestrator",
- "summary": "Investigation completed",
- "data": {"status": "completed"},
- })
+ _runs[run_id].update(
+ {
+ "status": "completed",
+ "report_md": state_data.get("report_md", ""),
+ "report_html": state_data.get("report_html", ""),
+ "audit_log": audit_log,
+ "recon": state_data.get("recon", {}),
+ "forensic": state_data.get("forensic", {}),
+ "responder": state_data.get("responder", {}),
+ "completed_at": datetime.utcnow().isoformat(),
+ "error": None,
+ }
+ )
+ await _emit_event(
+ run_id,
+ req.tenant_id,
+ {
+ "kind": "completed",
+ "agent": "orchestrator",
+ "summary": "Investigation completed",
+ "data": {"status": "completed"},
+ },
+ )
elif event.get("type") == "error":
err_msg = event.get("error", "Unknown error")
_runs[run_id].update({"status": "failed", "error": err_msg})
- await _emit_event(run_id, req.tenant_id, {
- "kind": "error",
- "agent": "orchestrator",
- "summary": err_msg,
- "data": {"status": "failed"},
- })
+ await _emit_event(
+ run_id,
+ req.tenant_id,
+ {
+ "kind": "error",
+ "agent": "orchestrator",
+ "summary": err_msg,
+ "data": {"status": "failed"},
+ },
+ )
except Exception as exc: # noqa: BLE001
logger.error("investigation_bg_task failed", run_id=run_id, error=str(exc))
@@ -151,6 +165,7 @@ async def _run_and_store(run_id: str, case_id: str, req: InvestigateRequest) ->
# Routes
# ---------------------------------------------------------------------------
+
@router.post("/cases/{case_id}/investigate", response_model=InvestigateResponse)
async def launch_investigation(
case_id: str,
@@ -232,12 +247,12 @@ async def get_report_pdf(run_id: str):
media_type="application/pdf",
headers={"Content-Disposition": f'attachment; filename="aisoc-report-{run_id}.pdf"'},
)
- except ImportError:
+ except ImportError as exc:
# weasyprint not installed — return the HTML with a PDF content-type note
raise HTTPException(
status_code=501,
detail="PDF generation requires weasyprint. Install with: pip install weasyprint",
- )
+ ) from exc
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=500, detail=f"PDF generation failed: {exc}") from exc
@@ -272,12 +287,16 @@ async def stream_investigation(ws: WebSocket, run_id: str):
status = run.get("status", "running")
if status in ("completed", "failed"):
- await ws.send_text(json.dumps({
- "type": "done" if status == "completed" else "error",
- "case_id": case_id,
- "status": status,
- "error": run.get("error"),
- }))
+ await ws.send_text(
+ json.dumps(
+ {
+ "type": "done" if status == "completed" else "error",
+ "case_id": case_id,
+ "status": status,
+ "error": run.get("error"),
+ }
+ )
+ )
break
await asyncio.sleep(0.5)
else:
diff --git a/services/agents/app/api/playbooks.py b/services/agents/app/api/playbooks.py
index 443c82bbf..583ddcd07 100644
--- a/services/agents/app/api/playbooks.py
+++ b/services/agents/app/api/playbooks.py
@@ -10,6 +10,7 @@ Endpoints:
POST /api/v1/playbooks/{id}/run → execute a playbook against a context
GET /api/v1/playbooks/runs/{run_id} → get a run result
"""
+
from __future__ import annotations
import logging
@@ -23,7 +24,6 @@ from app.playbook import (
PlaybookEngine,
PlaybookRun,
PlaybookStore,
- RunStatus,
)
logger = logging.getLogger("aisoc.api.playbooks")
@@ -37,6 +37,7 @@ _runs: dict[str, PlaybookRun] = {}
# Request / Response helpers
# ---------------------------------------------------------------------------
+
class RunRequest(BaseModel):
context: dict[str, Any] = {}
dry_run: bool = False
@@ -46,6 +47,7 @@ class RunRequest(BaseModel):
# CRUD
# ---------------------------------------------------------------------------
+
@router.get("", summary="List all playbooks")
async def list_playbooks(enabled_only: bool = False) -> list[dict]:
store = PlaybookStore.default()
@@ -88,6 +90,7 @@ async def delete_playbook(playbook_id: str) -> None:
# Execution
# ---------------------------------------------------------------------------
+
async def _execute(playbook: Playbook, context: dict[str, Any], dry_run: bool, run_holder: list) -> None:
"""Background task: run the playbook and store the result."""
engine = PlaybookEngine()
@@ -107,19 +110,15 @@ async def run_playbook(
if not pb:
raise HTTPException(status_code=404, detail="Playbook not found")
- # We need a run_id before background task completes; pre-register it
- import uuid
- run_id_holder: list[str] = []
-
# Create a placeholder run immediately so the caller can poll it
- from app.playbook.engine import PlaybookRun as _PR, RunStatus as _RS
+ from app.playbook.engine import PlaybookRun as _PR
+ from app.playbook.engine import RunStatus as _RS
+
placeholder = _PR(pb, body.context)
placeholder.status = _RS.PENDING
_runs[placeholder.run_id] = placeholder
- background_tasks.add_task(
- _execute_and_update, pb, body.context, body.dry_run, placeholder.run_id
- )
+ background_tasks.add_task(_execute_and_update, pb, body.context, body.dry_run, placeholder.run_id)
return {
"run_id": placeholder.run_id,
@@ -129,9 +128,7 @@ async def run_playbook(
}
-async def _execute_and_update(
- playbook: Playbook, context: dict[str, Any], dry_run: bool, run_id: str
-) -> None:
+async def _execute_and_update(playbook: Playbook, context: dict[str, Any], dry_run: bool, run_id: str) -> None:
"""Background task: overwrite placeholder with real run."""
engine = PlaybookEngine()
pr = await engine.run(playbook, context, dry_run=dry_run)
diff --git a/services/agents/app/api/router.py b/services/agents/app/api/router.py
index 59395f0b5..ec55bf1cf 100644
--- a/services/agents/app/api/router.py
+++ b/services/agents/app/api/router.py
@@ -1,9 +1,9 @@
"""
Agent service REST API.
"""
+
from __future__ import annotations
-import asyncio
from datetime import datetime
from typing import Any
from uuid import UUID, uuid4
diff --git a/services/agents/app/core/telemetry.py b/services/agents/app/core/telemetry.py
index 93f95f352..ba508e518 100644
--- a/services/agents/app/core/telemetry.py
+++ b/services/agents/app/core/telemetry.py
@@ -20,13 +20,14 @@ Usage:
span.set_attribute("case.id", state.case_id)
span.set_attribute("agent", "recon")
"""
+
from __future__ import annotations
import logging
import os
from opentelemetry import trace
-from opentelemetry.sdk.resources import Resource, SERVICE_NAME, SERVICE_VERSION
+from opentelemetry.sdk.resources import SERVICE_NAME, SERVICE_VERSION, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
@@ -54,6 +55,7 @@ def _build_exporter(exporter_name: str):
if name == "jaeger":
try:
from opentelemetry.exporter.jaeger.thrift import JaegerExporter # type: ignore
+
return JaegerExporter(
agent_host_name=os.getenv("JAEGER_HOST", "jaeger"),
agent_port=int(os.getenv("JAEGER_PORT", "6831")),
@@ -65,13 +67,11 @@ def _build_exporter(exporter_name: str):
if name == "otlp":
try:
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter # type: ignore
+
endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://otel-collector:4317")
return OTLPSpanExporter(endpoint=endpoint, insecure=True)
except ImportError:
- logger.warning(
- "opentelemetry-exporter-otlp-proto-grpc not installed; "
- "set OTEL_EXPORTER=console for local dev"
- )
+ logger.warning("opentelemetry-exporter-otlp-proto-grpc not installed; set OTEL_EXPORTER=console for local dev")
return None
return None
@@ -102,12 +102,14 @@ def instrument_app(app) -> None: # noqa: ANN001
try:
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor # type: ignore
+
FastAPIInstrumentor.instrument_app(app)
except ImportError:
pass
try:
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor # type: ignore
+
HTTPXClientInstrumentor().instrument()
except ImportError:
pass
diff --git a/services/agents/app/graph/workflow.py b/services/agents/app/graph/workflow.py
index 22d356968..8f56e6f11 100644
--- a/services/agents/app/graph/workflow.py
+++ b/services/agents/app/graph/workflow.py
@@ -2,6 +2,7 @@
LangGraph workflow: wires Triage → Enrichment → Investigation agents.
Uses a simple sequential StateGraph (can be extended with conditional edges).
"""
+
from __future__ import annotations
import structlog
@@ -25,6 +26,7 @@ def _from_dict(d: dict) -> InvestigationState:
# ---- Node wrappers (LangGraph uses dict state, we wrap our Pydantic model) ----
+
async def triage_node(state: dict) -> dict:
s = _from_dict(state)
s = await run_triage(s)
diff --git a/services/agents/app/investigator/__init__.py b/services/agents/app/investigator/__init__.py
index 568891922..e77f24d19 100644
--- a/services/agents/app/investigator/__init__.py
+++ b/services/agents/app/investigator/__init__.py
@@ -1,4 +1,5 @@
"""AiSOC Multi-Agent Investigator — Pillar 1."""
+
from .orchestrator import InvestigatorOrchestrator, run_investigation
from .state import InvestigatorState
diff --git a/services/agents/app/investigator/forensic_agent.py b/services/agents/app/investigator/forensic_agent.py
index 6281b9e22..1d5826179 100644
--- a/services/agents/app/investigator/forensic_agent.py
+++ b/services/agents/app/investigator/forensic_agent.py
@@ -7,6 +7,7 @@ Responsibilities:
• Identify forensic artefacts (file paths, registry keys, network artefacts)
• Produce a confidence-scored forensic summary
"""
+
from __future__ import annotations
import json
@@ -15,8 +16,8 @@ import time
from typing import Any
import structlog
-from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
+from langchain_openai import ChatOpenAI
from .state import ForensicFindings, InvestigatorState, StepKind
from .tools import sha256_of
@@ -50,7 +51,7 @@ async def _llm_forensic(state: InvestigatorState) -> dict[str, Any]:
llm = ChatOpenAI(model=model, temperature=0)
enrichment_snippet = json.dumps(
- {k: v for k, v in list(state.enrichment_cache.items())[:10]},
+ dict(list(state.enrichment_cache.items())[:10]),
indent=2,
)[:3000]
@@ -83,10 +84,7 @@ async def _llm_forensic(state: InvestigatorState) -> dict[str, Any]:
latency_ms = int((time.monotonic() - t0) * 1000)
tokens = 0
if hasattr(response, "response_metadata"):
- tokens = (
- response.response_metadata.get("token_usage", {}).get("total_tokens", 0)
- or 0
- )
+ tokens = response.response_metadata.get("token_usage", {}).get("total_tokens", 0) or 0
state.log_llm_response(
agent="ForensicAgent",
response=content if isinstance(content, str) else str(content),
@@ -95,7 +93,7 @@ async def _llm_forensic(state: InvestigatorState) -> dict[str, Any]:
tokens_used=tokens,
latency_ms=latency_ms,
)
- json_match = re.search(r'\{[\s\S]*\}', content)
+ json_match = re.search(r"\{[\s\S]*\}", content)
if json_match:
return json.loads(json_match.group())
except Exception as exc: # noqa: BLE001
diff --git a/services/agents/app/investigator/ledger.py b/services/agents/app/investigator/ledger.py
index 7cc3b9cea..cb52ac63b 100644
--- a/services/agents/app/investigator/ledger.py
+++ b/services/agents/app/investigator/ledger.py
@@ -20,6 +20,7 @@ Design notes
string tenant id (e.g. ``"default"``); we resolve it to the canonical UUID
by looking it up in the ``tenants`` table on first write per run.
"""
+
from __future__ import annotations
import hashlib
@@ -27,7 +28,7 @@ import json
import os
import uuid
from datetime import datetime
-from typing import Any, Optional
+from typing import Any
import asyncpg
import structlog
@@ -35,17 +36,15 @@ import structlog
logger = structlog.get_logger()
-_POOL: Optional[asyncpg.Pool] = None
+_POOL: asyncpg.Pool | None = None
def _normalise_dsn(url: str) -> str:
"""Convert SQLAlchemy-style DSNs to plain Postgres ones for asyncpg."""
- return url.replace("postgresql+asyncpg://", "postgresql://").replace(
- "postgres+asyncpg://", "postgresql://"
- )
+ return url.replace("postgresql+asyncpg://", "postgresql://").replace("postgres+asyncpg://", "postgresql://")
-async def get_pool() -> Optional[asyncpg.Pool]:
+async def get_pool() -> asyncpg.Pool | None:
"""Lazy-init a connection pool. Returns None if no DATABASE_URL is set."""
global _POOL
if _POOL is not None:
@@ -77,9 +76,7 @@ async def close_pool() -> None:
_POOL = None
-async def _resolve_tenant_id(
- conn: asyncpg.Connection, tenant_ref: str
-) -> Optional[uuid.UUID]:
+async def _resolve_tenant_id(conn: asyncpg.Connection, tenant_ref: str) -> uuid.UUID | None:
"""Look up the canonical tenant UUID. Accepts a UUID string, slug, or name.
Returns None if no matching tenant exists. Callers should fall back to
@@ -105,9 +102,7 @@ async def _resolve_tenant_id(
async def _set_rls_context(conn: asyncpg.Connection, tenant_id: uuid.UUID) -> None:
"""Match the API service's set_rls_context — required so the audit-log
immutability trigger and tenant policies allow our INSERTs."""
- await conn.execute(
- "SELECT set_config('app.tenant_id', $1, true)", str(tenant_id)
- )
+ await conn.execute("SELECT set_config('app.tenant_id', $1, true)", str(tenant_id))
async def start_run(
@@ -118,7 +113,7 @@ async def start_run(
alert_summary: str,
raw_alert: dict[str, Any] | None,
model_used: str | None = None,
-) -> Optional[uuid.UUID]:
+) -> uuid.UUID | None:
"""Insert a new ``investigation_runs`` row. Returns the resolved tenant
UUID on success (caller can cache it for subsequent writes), None on
failure."""
@@ -177,7 +172,7 @@ async def record_event(
output_hash: str | None = None,
duration_ms: int = 0,
timestamp: datetime | None = None,
-) -> Optional[uuid.UUID]:
+) -> uuid.UUID | None:
"""Append an immutable event row. Returns the new event id, or None."""
pool = await get_pool()
if pool is None:
@@ -230,7 +225,7 @@ async def record_artifact(
kind: str,
content: str,
event_id: uuid.UUID | None = None,
-) -> Optional[uuid.UUID]:
+) -> uuid.UUID | None:
"""Persist a large blob (LLM transcript, full report) inline.
For now we store inline ``TEXT`` rather than offloading to S3 — the
diff --git a/services/agents/app/investigator/orchestrator.py b/services/agents/app/investigator/orchestrator.py
index a16dcd223..ada932b1f 100644
--- a/services/agents/app/investigator/orchestrator.py
+++ b/services/agents/app/investigator/orchestrator.py
@@ -8,12 +8,13 @@ The state dict is threaded through every node unchanged apart from the
node's own additions. An error in any node marks the state as "failed"
and short-circuits to END.
"""
+
from __future__ import annotations
-import asyncio
import uuid
+from collections.abc import AsyncIterator
from datetime import datetime
-from typing import Any, AsyncIterator
+from typing import Any
import structlog
from langgraph.graph import END, START, StateGraph
@@ -30,6 +31,7 @@ logger = structlog.get_logger()
try:
from app.core.telemetry import get_tracer as _get_tracer
+
_tracer = _get_tracer("aisoc.investigator")
except Exception:
_tracer = trace.get_tracer("aisoc.investigator")
@@ -39,6 +41,7 @@ except Exception:
# Error-wrapper: catches exceptions in any node and marks state as failed
# ---------------------------------------------------------------------------
+
def _safe_node(fn):
"""Wrap an async node so exceptions are captured in state instead of crashing the graph.
Also emits an OpenTelemetry span per node execution."""
@@ -73,6 +76,7 @@ def _safe_node(fn):
# Guard: skip remaining nodes if state is already failed/completed
# ---------------------------------------------------------------------------
+
async def _guard(state_dict: dict[str, Any]) -> str:
"""Conditional edge: 'continue' or 'end'."""
status = state_dict.get("status", "")
@@ -119,6 +123,7 @@ def _build_graph():
# Public API
# ---------------------------------------------------------------------------
+
class InvestigatorOrchestrator:
"""High-level wrapper around the compiled LangGraph."""
@@ -264,16 +269,8 @@ class InvestigatorOrchestrator:
new_entries = state.audit_log[emitted_count:]
for offset, entry in enumerate(new_entries):
seq = emitted_count + offset
- kind_val = (
- entry.kind.value
- if hasattr(entry.kind, "value")
- else str(entry.kind)
- )
- ts_str = (
- entry.timestamp.isoformat()
- if hasattr(entry.timestamp, "isoformat")
- else str(entry.timestamp)
- )
+ kind_val = entry.kind.value if hasattr(entry.kind, "value") else str(entry.kind)
+ ts_str = entry.timestamp.isoformat() if hasattr(entry.timestamp, "isoformat") else str(entry.timestamp)
# Persist before emitting so subscribers can deep-link
if tenant_uuid is not None:
await ledger.record_event(
diff --git a/services/agents/app/investigator/recon_agent.py b/services/agents/app/investigator/recon_agent.py
index c40c787de..a8c0f7646 100644
--- a/services/agents/app/investigator/recon_agent.py
+++ b/services/agents/app/investigator/recon_agent.py
@@ -7,6 +7,7 @@ Responsibilities:
• Map findings to MITRE ATT&CK techniques
• Identify potential threat-actor clusters
"""
+
from __future__ import annotations
import asyncio
@@ -14,8 +15,8 @@ import time
from typing import Any
import structlog
-from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
+from langchain_openai import ChatOpenAI
from .state import InvestigatorState, ReconFindings, StepKind
from .tools import enrich_ioc, extract_iocs, map_to_mitre, sha256_of
@@ -46,7 +47,8 @@ async def _llm_recon(state: InvestigatorState) -> dict[str, Any]:
Records the LLM prompt and response into the audit ledger so the
reasoning trace is replayable.
"""
- import os, json
+ import json
+ import os
model = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
llm = ChatOpenAI(model=model, temperature=0)
@@ -75,10 +77,7 @@ async def _llm_recon(state: InvestigatorState) -> dict[str, Any]:
latency_ms = int((time.monotonic() - t0) * 1000)
tokens = 0
if hasattr(response, "response_metadata"):
- tokens = (
- response.response_metadata.get("token_usage", {}).get("total_tokens", 0)
- or 0
- )
+ tokens = response.response_metadata.get("token_usage", {}).get("total_tokens", 0) or 0
state.log_llm_response(
agent="ReconAgent",
response=content if isinstance(content, str) else str(content),
@@ -89,7 +88,8 @@ async def _llm_recon(state: InvestigatorState) -> dict[str, Any]:
)
# Extract JSON from the response
import re
- json_match = re.search(r'\{[\s\S]*\}', content)
+
+ json_match = re.search(r"\{[\s\S]*\}", content)
if json_match:
return json.loads(json_match.group())
except Exception as exc: # noqa: BLE001
diff --git a/services/agents/app/investigator/report_writer_agent.py b/services/agents/app/investigator/report_writer_agent.py
index e007e7ad2..df292d140 100644
--- a/services/agents/app/investigator/report_writer_agent.py
+++ b/services/agents/app/investigator/report_writer_agent.py
@@ -6,6 +6,7 @@ Responsibilities:
• Produce HTML variant (for PDF generation via Playwright/weasyprint)
• Sign the report hash into the audit log
"""
+
from __future__ import annotations
import time
@@ -13,8 +14,8 @@ from datetime import datetime
from typing import Any
import structlog
-from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
+from langchain_openai import ChatOpenAI
from .state import InvestigatorState, StepKind
from .tools import sha256_of
@@ -70,7 +71,7 @@ def _build_context(state: InvestigatorState) -> str:
"Eradication: " + "; ".join(state.responder.eradication_steps[:5]),
"Recovery: " + "; ".join(state.responder.recovery_steps[:5]),
"",
- f"=== ENRICHMENT SAMPLE ===",
+ "=== ENRICHMENT SAMPLE ===",
json.dumps(dict(list(state.enrichment_cache.items())[:5]), indent=2)[:1500],
]
return "\n".join(sections)
@@ -80,6 +81,7 @@ def _md_to_html(md: str, case_id: str) -> str:
"""Lightweight Markdown → HTML converter (no external deps required for basic output)."""
try:
import markdown
+
body = markdown.markdown(md, extensions=["tables", "fenced_code"])
except ImportError:
# Fallback: wrap in <pre>
@@ -144,10 +146,7 @@ async def run_report_writer(state_dict: dict[str, Any]) -> dict[str, Any]:
latency_ms = int((time.monotonic() - t_llm) * 1000)
tokens = 0
if hasattr(response, "response_metadata"):
- tokens = (
- response.response_metadata.get("token_usage", {}).get("total_tokens", 0)
- or 0
- )
+ tokens = response.response_metadata.get("token_usage", {}).get("total_tokens", 0) or 0
state.log_llm_response(
agent="ReportWriterAgent",
response=state.report_md if isinstance(state.report_md, str) else str(state.report_md),
@@ -203,13 +202,13 @@ Root cause hypothesis: {state.forensic.root_cause_hypothesis}
Risk level: **{state.responder.risk_level}**
### Containment
-{chr(10).join(f'- {s}' for s in state.responder.containment_steps)}
+{chr(10).join(f"- {s}" for s in state.responder.containment_steps)}
### Eradication
-{chr(10).join(f'- {s}' for s in state.responder.eradication_steps)}
+{chr(10).join(f"- {s}" for s in state.responder.eradication_steps)}
### Recovery
-{chr(10).join(f'- {s}' for s in state.responder.recovery_steps)}
+{chr(10).join(f"- {s}" for s in state.responder.recovery_steps)}
---
*Generated by AiSOC — automated report (LLM unavailable)*
diff --git a/services/agents/app/investigator/responder_agent.py b/services/agents/app/investigator/responder_agent.py
index 517309d83..519f4345f 100644
--- a/services/agents/app/investigator/responder_agent.py
+++ b/services/agents/app/investigator/responder_agent.py
@@ -6,6 +6,7 @@ Responsibilities:
• Estimate effort and risk
• All actions are DRY-RUN only — no live execution occurs here
"""
+
from __future__ import annotations
import json
@@ -14,8 +15,8 @@ import time
from typing import Any
import structlog
-from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
+from langchain_openai import ChatOpenAI
from .state import InvestigatorState, ResponderPlan, StepKind
from .tools import sha256_of
@@ -54,8 +55,7 @@ async def _llm_responder(state: InvestigatorState) -> dict[str, Any]:
f"Confidence: {state.forensic.confidence:.0%}\n"
f"MITRE: {state.recon.mitre_techniques}\n"
f"Threat actors: {state.recon.threat_actors}\n\n"
- f"Timeline (last 5):\n"
- + json.dumps(state.forensic.timeline[-5:], indent=2)
+ f"Timeline (last 5):\n" + json.dumps(state.forensic.timeline[-5:], indent=2)
)
messages = [
@@ -80,10 +80,7 @@ async def _llm_responder(state: InvestigatorState) -> dict[str, Any]:
latency_ms = int((time.monotonic() - t0) * 1000)
tokens = 0
if hasattr(response, "response_metadata"):
- tokens = (
- response.response_metadata.get("token_usage", {}).get("total_tokens", 0)
- or 0
- )
+ tokens = response.response_metadata.get("token_usage", {}).get("total_tokens", 0) or 0
state.log_llm_response(
agent="ResponderAgent",
response=content if isinstance(content, str) else str(content),
@@ -92,7 +89,7 @@ async def _llm_responder(state: InvestigatorState) -> dict[str, Any]:
tokens_used=tokens,
latency_ms=latency_ms,
)
- json_match = re.search(r'\{[\s\S]*\}', content)
+ json_match = re.search(r"\{[\s\S]*\}", content)
if json_match:
return json.loads(json_match.group())
except Exception as exc: # noqa: BLE001
@@ -156,8 +153,7 @@ async def run_responder(state_dict: dict[str, Any]) -> dict[str, Any]:
state.log(
StepKind.RESPONDER,
"ResponderAgent",
- f"Generated {len(state.responder.recommended_actions)} recommended actions "
- f"(risk={state.responder.risk_level}, dry_run=True)",
+ f"Generated {len(state.responder.recommended_actions)} recommended actions (risk={state.responder.risk_level}, dry_run=True)",
duration_ms=elapsed_ms,
input_hash=sha256_of(state.forensic.model_dump()),
output_hash=sha256_of(state.responder.model_dump()),
diff --git a/services/agents/app/investigator/state.py b/services/agents/app/investigator/state.py
index f3570c920..7cb054b80 100644
--- a/services/agents/app/investigator/state.py
+++ b/services/agents/app/investigator/state.py
@@ -2,6 +2,7 @@
Extended state model for the multi-agent investigator pipeline.
Extends the existing InvestigationState with per-agent outputs and audit log.
"""
+
from __future__ import annotations
import hashlib
@@ -13,8 +14,6 @@ from uuid import UUID, uuid4
from pydantic import BaseModel, Field
-from app.models.state import InvestigationState # re-use base model
-
def _stable_hash(payload: Any) -> str:
"""Deterministic SHA-256 of an arbitrary JSON-serialisable payload.
@@ -53,7 +52,7 @@ class AuditEntry(BaseModel):
kind: StepKind
agent: str
summary: str
- input_hash: str | None = None # sha256 of serialised input
+ input_hash: str | None = None # sha256 of serialised input
output_hash: str | None = None # sha256 of serialised output
duration_ms: int = 0
metadata: dict[str, Any] = Field(default_factory=dict)
@@ -126,7 +125,7 @@ class InvestigatorState(BaseModel):
audit_log: list[AuditEntry] = Field(default_factory=list)
# Control
- status: str = "pending" # pending | running | completed | failed
+ status: str = "pending" # pending | running | completed | failed
error: str | None = None
iteration: int = 0
max_iterations: int = 6
@@ -137,9 +136,7 @@ class InvestigatorState(BaseModel):
# ---------- helpers ----------
def log(self, kind: StepKind, agent: str, summary: str, **meta: Any) -> None:
- self.audit_log.append(
- AuditEntry(kind=kind, agent=agent, summary=summary, metadata=meta)
- )
+ self.audit_log.append(AuditEntry(kind=kind, agent=agent, summary=summary, metadata=meta))
def log_entry(self, entry: AuditEntry) -> None:
"""Append a fully-formed entry (used when caller has computed hashes)."""
@@ -300,5 +297,5 @@ class InvestigatorState(BaseModel):
return self.model_dump(mode="json")
@classmethod
- def from_dict(cls, d: dict[str, Any]) -> "InvestigatorState":
+ def from_dict(cls, d: dict[str, Any]) -> InvestigatorState:
return cls.model_validate(d)
diff --git a/services/agents/app/investigator/tools.py b/services/agents/app/investigator/tools.py
index 3983211c6..aebd5af21 100644
--- a/services/agents/app/investigator/tools.py
+++ b/services/agents/app/investigator/tools.py
@@ -3,6 +3,7 @@ Tool wrappers used by every investigator agent.
Each tool is a plain async callable that can be registered with LangChain/LangGraph
tool-calling or invoked directly.
"""
+
from __future__ import annotations
import hashlib
@@ -24,6 +25,7 @@ _TIMEOUT = 10.0
# Enrichment tool
# ---------------------------------------------------------------------------
+
async def enrich_ioc(ioc_value: str, ioc_type: str) -> dict[str, Any]:
"""
Call the AiSOC enrichment micro-service for a single IOC.
@@ -46,6 +48,7 @@ async def enrich_ioc(ioc_value: str, ioc_type: str) -> dict[str, Any]:
# Case / alert lookup
# ---------------------------------------------------------------------------
+
async def fetch_case(case_id: str, api_token: str = "") -> dict[str, Any]:
"""Fetch full case details from the AiSOC API."""
headers = {"Authorization": f"Bearer {api_token}"} if api_token else {}
@@ -116,6 +119,7 @@ def map_to_mitre(text: str) -> list[str]:
# Utility helpers
# ---------------------------------------------------------------------------
+
def sha256_of(obj: Any) -> str:
serialised = json.dumps(obj, sort_keys=True, default=str)
return hashlib.sha256(serialised.encode()).hexdigest()
@@ -130,12 +134,8 @@ def extract_iocs(text: str) -> list[dict[str, str]]:
iocs: list[dict[str, str]] = []
- ip_pattern = re.compile(
- r"\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b"
- )
- domain_pattern = re.compile(
- r"\b(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}\b"
- )
+ ip_pattern = re.compile(r"\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b")
+ domain_pattern = re.compile(r"\b(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}\b")
sha256_pattern = re.compile(r"\b[0-9a-fA-F]{64}\b")
url_pattern = re.compile(r"https?://[^\s\"'<>]+")
diff --git a/services/agents/app/main.py b/services/agents/app/main.py
index 5beaa0e13..e22c7ef2f 100644
--- a/services/agents/app/main.py
+++ b/services/agents/app/main.py
@@ -1,19 +1,19 @@
import os
+from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
-from typing import AsyncGenerator
import structlog
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
-from app.api.router import router
+from app.api.contextual import router as contextual_router
from app.api.investigate import router as investigate_router
from app.api.playbooks import router as playbook_router
-from app.api.contextual import router as contextual_router
+from app.api.router import router
from app.core.telemetry import instrument_app
from app.investigator import ledger as investigation_ledger
from app.playbook import PlaybookStore
-from app.tools.mitre_full import load_attck_corpus, embed_techniques_into_qdrant
+from app.tools.mitre_full import embed_techniques_into_qdrant, load_attck_corpus
logger = structlog.get_logger()
@@ -93,13 +93,14 @@ instrument_app(app)
app.include_router(router, prefix="/api/v1")
app.include_router(investigate_router) # prefix already set in investigate.py
-app.include_router(playbook_router) # prefix: /api/v1/playbooks
-app.include_router(contextual_router) # prefix: /api/v1/contextual
+app.include_router(playbook_router) # prefix: /api/v1/playbooks
+app.include_router(contextual_router) # prefix: /api/v1/contextual
@app.get("/health")
async def health():
from app.tools.mitre_full import get_coverage_summary
+
summary = get_coverage_summary()
return {
"status": "healthy",
diff --git a/services/agents/app/models/state.py b/services/agents/app/models/state.py
index 01908d345..3e20dee06 100644
--- a/services/agents/app/models/state.py
+++ b/services/agents/app/models/state.py
@@ -1,6 +1,7 @@
"""
Agent state models for LangGraph workflows.
"""
+
from __future__ import annotations
from datetime import datetime
diff --git a/services/agents/app/playbook/__init__.py b/services/agents/app/playbook/__init__.py
index fb6ef9203..87557cbe8 100644
--- a/services/agents/app/playbook/__init__.py
+++ b/services/agents/app/playbook/__init__.py
@@ -1,4 +1,5 @@
"""AiSOC Playbook Engine — Pillar 2."""
+
from .engine import PlaybookEngine, PlaybookRun, RunStatus
from .models import (
Playbook,
diff --git a/services/agents/app/playbook/engine.py b/services/agents/app/playbook/engine.py
index 7ad1b1850..66af0123a 100644
--- a/services/agents/app/playbook/engine.py
+++ b/services/agents/app/playbook/engine.py
@@ -9,6 +9,7 @@ Design goals:
- Emits events to the realtime service so the UI can stream progress.
- Zero external dependencies beyond httpx + stdlib.
"""
+
from __future__ import annotations
import asyncio
@@ -35,6 +36,7 @@ _API_URL = os.getenv("API_URL", "http://api:8000")
# Run status
# ---------------------------------------------------------------------------
+
class RunStatus(str, Enum):
PENDING = "pending"
RUNNING = "running"
@@ -55,6 +57,7 @@ class StepStatus(str, Enum):
# Run record
# ---------------------------------------------------------------------------
+
class StepResult(dict): # thin dict subclass for JSON serialisation
pass
@@ -93,6 +96,7 @@ class PlaybookRun:
# Condition evaluation
# ---------------------------------------------------------------------------
+
def _resolve_field(context: dict[str, Any], field: str) -> Any:
"""Resolve a dot-path field from context, e.g. 'alert.severity'."""
parts = field.split(".")
@@ -130,6 +134,7 @@ def _evaluate_condition(condition: StepCondition, context: dict[str, Any]) -> bo
# Step handlers
# ---------------------------------------------------------------------------
+
async def _handle_enrich(step: PlaybookStep, context: dict[str, Any], http: httpx.AsyncClient) -> dict:
ioc = step.params.get("ioc") or context.get("ioc") or context.get("src_ip", "")
ioc_type = step.params.get("ioc_type", "ip")
@@ -221,6 +226,7 @@ _HANDLERS = {
# Realtime event helper
# ---------------------------------------------------------------------------
+
async def _emit(run_id: str, event_type: str, payload: dict, http: httpx.AsyncClient) -> None:
try:
await http.post(
@@ -237,6 +243,7 @@ async def _emit(run_id: str, event_type: str, payload: dict, http: httpx.AsyncCl
# Engine
# ---------------------------------------------------------------------------
+
class PlaybookEngine:
"""Executes playbooks step-by-step, emitting realtime events."""
@@ -275,9 +282,7 @@ class PlaybookEngine:
condition_passed = _evaluate_condition(step.condition, pr.context)
if not condition_passed:
- pr.step_results.append(
- {"step_id": step.id, "name": step.name, "status": StepStatus.SKIPPED}
- )
+ pr.step_results.append({"step_id": step.id, "name": step.name, "status": StepStatus.SKIPPED})
# Branching: use next_false if set
if step.next_false and step.next_false in step_index:
current_idx = step_index[step.next_false]
@@ -292,9 +297,7 @@ class PlaybookEngine:
current_idx = step_index[branch_id]
else:
current_idx += 1
- pr.step_results.append(
- {"step_id": step.id, "name": step.name, "status": StepStatus.SUCCESS, "branch": branch_id}
- )
+ pr.step_results.append({"step_id": step.id, "name": step.name, "status": StepStatus.SUCCESS, "branch": branch_id})
continue
await _emit(pr.run_id, "step.started", {"step": step.name, "type": step.type}, http)
@@ -321,15 +324,13 @@ class PlaybookEngine:
elapsed = time.perf_counter() - t0
logger.error("Step %s attempt %d failed: %s", step.name, attempt, exc)
if attempt <= step.retry_max:
- await asyncio.sleep(min(2 ** attempt, 30))
+ await asyncio.sleep(min(2**attempt, 30))
else:
step_status = StepStatus.FAILED
result = {"error": str(exc), "_elapsed_ms": round(elapsed * 1000)}
break
- pr.step_results.append(
- {"step_id": step.id, "name": step.name, "status": step_status, "result": result}
- )
+ pr.step_results.append({"step_id": step.id, "name": step.name, "status": step_status, "result": result})
# Merge result into context for downstream steps
pr.context.update({f"_step_{step.id}": result})
# Also flatten top-level keys without underscore prefix for convenience
@@ -337,11 +338,16 @@ class PlaybookEngine:
if not k.startswith("_"):
pr.context.setdefault(k, v)
- await _emit(pr.run_id, "step.done", {
- "step": step.name,
- "status": step_status,
- "result_keys": list(result.keys()),
- }, http)
+ await _emit(
+ pr.run_id,
+ "step.done",
+ {
+ "step": step.name,
+ "status": step_status,
+ "result_keys": list(result.keys()),
+ },
+ http,
+ )
if step_status == StepStatus.FAILED and step.on_failure == "abort":
pr.status = RunStatus.FAILED
diff --git a/services/agents/app/playbook/models.py b/services/agents/app/playbook/models.py
index 24e867028..40c3c2a8f 100644
--- a/services/agents/app/playbook/models.py
+++ b/services/agents/app/playbook/models.py
@@ -1,4 +1,5 @@
"""Playbook data models — Pydantic v2."""
+
from __future__ import annotations
import uuid
@@ -10,19 +11,21 @@ from pydantic import BaseModel, Field
class StepType(str, Enum):
"""Supported step action types."""
- ENRICH = "enrich" # Call enrichment service
- INVESTIGATE = "investigate" # Trigger AI investigator
- NOTIFY = "notify" # Send notification (Slack, email, webhook)
- BLOCK_IP = "block_ip" # Call firewall/EDR action
+
+ ENRICH = "enrich" # Call enrichment service
+ INVESTIGATE = "investigate" # Trigger AI investigator
+ NOTIFY = "notify" # Send notification (Slack, email, webhook)
+ BLOCK_IP = "block_ip" # Call firewall/EDR action
ISOLATE_HOST = "isolate_host"
CREATE_TICKET = "create_ticket"
CLOSE_CASE = "close_case"
- HTTP = "http" # Generic outbound HTTP call
- CONDITION = "condition" # Branching / gate
+ HTTP = "http" # Generic outbound HTTP call
+ CONDITION = "condition" # Branching / gate
class StepCondition(BaseModel):
"""Optional condition guard that must be true before this step runs."""
+
field: str = Field(..., description="JSONPath into run context, e.g. 'verdict'")
operator: Literal["eq", "ne", "gt", "lt", "contains", "exists"] = "eq"
value: Any = None
@@ -30,6 +33,7 @@ class StepCondition(BaseModel):
class PlaybookStep(BaseModel):
"""A single step in a playbook."""
+
id: str = Field(default_factory=lambda: str(uuid.uuid4())[:8])
name: str
type: StepType
@@ -45,6 +49,7 @@ class PlaybookStep(BaseModel):
class Playbook(BaseModel):
"""A complete playbook definition."""
+
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
name: str
description: str = ""
diff --git a/services/agents/app/playbook/store.py b/services/agents/app/playbook/store.py
index 92ae66042..8c51a7419 100644
--- a/services/agents/app/playbook/store.py
+++ b/services/agents/app/playbook/store.py
@@ -10,6 +10,7 @@ The store is a singleton; mount it at app startup:
store = PlaybookStore.default() # loads from data/playbooks/
"""
+
from __future__ import annotations
import json
@@ -20,7 +21,7 @@ from datetime import UTC, datetime
from pathlib import Path
from typing import Any
-from .models import Playbook, PlaybookStep, StepType
+from .models import Playbook
logger = logging.getLogger("aisoc.playbook.store")
@@ -40,15 +41,11 @@ class PlaybookStore:
store_dir: Path | None = None,
pack_root: Path | None = None,
) -> None:
- self._dir = store_dir or Path(
- os.getenv("PLAYBOOK_STORE_DIR", str(_DEFAULT_STORE_DIR))
- )
+ self._dir = store_dir or Path(os.getenv("PLAYBOOK_STORE_DIR", str(_DEFAULT_STORE_DIR)))
self._dir.mkdir(parents=True, exist_ok=True)
# Optional canonical pack tree (playbooks/packs/v1/<category>/<slug>.playbook.json).
# Loaded read-only — mutations always go to self._dir/index.json.
- self._pack_root = pack_root or Path(
- os.getenv("PLAYBOOK_PACK_ROOT", str(_DEFAULT_PACK_ROOT))
- )
+ self._pack_root = pack_root or Path(os.getenv("PLAYBOOK_PACK_ROOT", str(_DEFAULT_PACK_ROOT)))
self._manifest_path = self._dir / "index.json"
self._playbooks: dict[str, Playbook] = {}
self._load()
@@ -60,7 +57,7 @@ class PlaybookStore:
_instance: PlaybookStore | None = None
@classmethod
- def default(cls) -> "PlaybookStore":
+ def default(cls) -> PlaybookStore:
if cls._instance is None:
cls._instance = cls()
return cls._instance
@@ -194,6 +191,7 @@ class PlaybookStore:
if self._playbooks:
return 0
from .templates import STARTER_PLAYBOOKS
+
count = 0
for pb_dict in STARTER_PLAYBOOKS:
pb = Playbook.model_validate(pb_dict)
diff --git a/services/agents/app/playbook/templates.py b/services/agents/app/playbook/templates.py
index adc27a314..792e32ddb 100644
--- a/services/agents/app/playbook/templates.py
+++ b/services/agents/app/playbook/templates.py
@@ -4,6 +4,7 @@
These are imported by PlaybookStore.seed_defaults() and can also be
referenced from the marketplace/index.json.
"""
+
from __future__ import annotations
STARTER_PLAYBOOKS: list[dict] = [
@@ -36,7 +37,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 2 ─ Ransomware Response
{
"id": "tpl-ransomware-response",
@@ -78,7 +78,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 3 ─ Credential Stuffing
{
"id": "tpl-cred-stuffing",
@@ -110,7 +109,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 4 ─ Insider Threat Investigation
{
"id": "tpl-insider-threat",
@@ -145,7 +143,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 5 ─ Malware IOC Enrichment
{
"id": "tpl-malware-ioc-enrich",
@@ -177,7 +174,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 6 ─ Cloud Misconfiguration Alert
{
"id": "tpl-cloud-misconfig",
@@ -212,7 +208,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 7 ─ Lateral Movement Detection
{
"id": "tpl-lateral-movement",
@@ -244,7 +239,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 8 ─ Data Exfiltration Alert
{
"id": "tpl-data-exfil",
@@ -285,7 +279,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 9 ─ Zero-Day Exploit
{
"id": "tpl-zero-day",
@@ -326,7 +319,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 10 ─ BEC (Business Email Compromise)
{
"id": "tpl-bec",
@@ -361,7 +353,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 11 ─ Supply Chain Alert
{
"id": "tpl-supply-chain",
@@ -396,7 +387,6 @@ STARTER_PLAYBOOKS: list[dict] = [
"author": "AiSOC",
"enabled": True,
},
-
# 12 ─ Auto-close Informational
{
"id": "tpl-auto-close-info",
diff --git a/services/agents/app/tools/enrichment.py b/services/agents/app/tools/enrichment.py
index 9136906aa..b2700e389 100644
--- a/services/agents/app/tools/enrichment.py
+++ b/services/agents/app/tools/enrichment.py
@@ -1,6 +1,7 @@
"""
Tool: IOC enrichment via the enrichment microservice.
"""
+
import httpx
import structlog
diff --git a/services/agents/tests/test_alert_reduction.py b/services/agents/tests/test_alert_reduction.py
index 4d72aa104..a509ed8c4 100644
--- a/services/agents/tests/test_alert_reduction.py
+++ b/services/agents/tests/test_alert_reduction.py
@@ -1,10 +1,10 @@
"""
Pillar-1 Evaluation: Alert Reduction Ratio — Real Measurement
==============================================================
-Anvilogic publicly claims a 90% alert-reduction figure. We measure ours
-honestly, offline, against a deterministic 1000-alert noisy stream that
-intentionally includes near-duplicates, host/user variations, time-windowed
-bursts, and benign chatter.
+Closed-source AI SOC vendors publicly claim alert-reduction figures around
+90%. We measure ours honestly, offline, against a deterministic 1000-alert
+noisy stream that intentionally includes near-duplicates, host/user
+variations, time-windowed bursts, and benign chatter.
Unlike the other three pillar-1 suites (which are substrate self-consistency
gates over deterministic templates), this suite is a REAL MEASUREMENT: the
@@ -30,6 +30,7 @@ See `apps/docs/docs/benchmark.md` for what each suite actually measures.
Run:
pytest services/agents/tests/test_alert_reduction.py -v
"""
+
from __future__ import annotations
import hashlib
@@ -38,11 +39,11 @@ import unittest
from dataclasses import dataclass
from typing import Any
-
# ---------------------------------------------------------------------------
# Synthetic alert generator (deterministic)
# ---------------------------------------------------------------------------
+
@dataclass(frozen=True)
class Alert:
id: str
@@ -64,24 +65,49 @@ class Alert:
_RULES = [
- "rule.brute_force.ssh", "rule.brute_force.rdp", "rule.brute_force.smb",
- "rule.malware.ransom_note", "rule.lolbas.certutil", "rule.lolbas.bitsadmin",
- "rule.recon.smb_enum", "rule.lateral.psexec", "rule.persistence.run_key",
- "rule.cloud.iam_anomaly", "rule.cloud.s3_public", "rule.dns.tunnel",
- "rule.network.beacon", "rule.exfil.large_upload", "rule.endpoint.lsass_dump",
- "rule.identity.impossible_travel", "rule.identity.password_spray",
- "rule.app.sql_injection", "rule.app.ssrf", "rule.benign.scanner",
- "rule.benign.healthcheck", "rule.benign.backup_window",
+ "rule.brute_force.ssh",
+ "rule.brute_force.rdp",
+ "rule.brute_force.smb",
+ "rule.malware.ransom_note",
+ "rule.lolbas.certutil",
+ "rule.lolbas.bitsadmin",
+ "rule.recon.smb_enum",
+ "rule.lateral.psexec",
+ "rule.persistence.run_key",
+ "rule.cloud.iam_anomaly",
+ "rule.cloud.s3_public",
+ "rule.dns.tunnel",
+ "rule.network.beacon",
+ "rule.exfil.large_upload",
+ "rule.endpoint.lsass_dump",
+ "rule.identity.impossible_travel",
+ "rule.identity.password_spray",
+ "rule.app.sql_injection",
+ "rule.app.ssrf",
+ "rule.benign.scanner",
+ "rule.benign.healthcheck",
+ "rule.benign.backup_window",
]
_HOSTS = [
- "WIN-PROD-WEB02", "WIN-FIN-DB01", "LIN-K8S-NODE01", "LIN-EDGE-VPN02",
- "MAC-CFO-LAPTOP", "WIN-DC-PRIMARY", "WIN-EXCHANGE-01", "AWS-EC2-PROD-API",
+ "WIN-PROD-WEB02",
+ "WIN-FIN-DB01",
+ "LIN-K8S-NODE01",
+ "LIN-EDGE-VPN02",
+ "MAC-CFO-LAPTOP",
+ "WIN-DC-PRIMARY",
+ "WIN-EXCHANGE-01",
+ "AWS-EC2-PROD-API",
]
_USERS = [
+ "system",
]
_SEVERITIES_BY_RULE: dict[str, str] = {
@@ -141,9 +167,9 @@ def generate_noisy_alert_stream(count: int = 1000) -> list[Alert]:
# Parent-cluster sizing: small number of clusters for big duplication ratios.
# E.g. 25 pure-duplicate parents in a 1000-alert stream means ~10 dupes each.
- n_pure_parents = max(5, count // 40) # 25 in 1000
- n_near_parents = max(5, count // 30) # 33 in 1000
- n_storm_parents = max(3, count // 60) # 16 in 1000
+ n_pure_parents = max(5, count // 40) # 25 in 1000
+ n_near_parents = max(5, count // 30) # 33 in 1000
+ n_storm_parents = max(3, count // 60) # 16 in 1000
for i in range(count):
bucket = _h(i, "bucket") % 100 # 0..99
@@ -210,6 +236,7 @@ def generate_noisy_alert_stream(count: int = 1000) -> list[Alert]:
# Fusion logic
# ---------------------------------------------------------------------------
+
@dataclass
class FusedIncident:
rule_id: str
@@ -271,11 +298,7 @@ def fuse_alerts(
for inc in incidents:
if merged:
last = merged[-1]
- if (
- last.rule_id == inc.rule_id
- and last.host == inc.host
- and (inc.first_ts - last.last_ts) <= host_window_s
- ):
+ if last.rule_id == inc.rule_id and last.host == inc.host and (inc.first_ts - last.last_ts) <= host_window_s:
last.member_alert_ids.extend(inc.member_alert_ids)
last.score = max(last.score, inc.score)
last.last_ts = max(last.last_ts, inc.last_ts)
@@ -345,20 +368,20 @@ def compute_reduction(alerts: list[Alert], incidents: list[FusedIncident]) -> di
# pytest tests
# ---------------------------------------------------------------------------
+
class TestAlertReduction(unittest.TestCase):
"""Pillar-1 alert-reduction ratio honestly measured."""
def test_reduction_meets_target(self) -> None:
- """Reduction should be ≥ 70% — Anvilogic claims 90%, our public floor is 70%."""
+ """Reduction should be ≥ 70% — closed-source vendors claim ~90%, our public floor is 70%."""
alerts = generate_noisy_alert_stream(count=1000)
incidents = fuse_alerts(alerts)
metrics = compute_reduction(alerts, incidents)
- print(f"\n[eval] alert reduction: {metrics['reduction_pct']} "
- f"({metrics['alerts_in']} → {metrics['incidents_out']})")
+ print(f"\n[eval] alert reduction: {metrics['reduction_pct']} ({metrics['alerts_in']} → {metrics['incidents_out']})")
self.assertGreaterEqual(
- metrics["reduction"], 0.70,
- f"Alert reduction {metrics['reduction_pct']} below 70% target.\n"
- f"{json.dumps(metrics, indent=2)}",
+ metrics["reduction"],
+ 0.70,
+ f"Alert reduction {metrics['reduction_pct']} below 70% target.\n{json.dumps(metrics, indent=2)}",
)
def test_no_critical_alerts_dropped_as_noise(self) -> None:
@@ -369,9 +392,9 @@ class TestAlertReduction(unittest.TestCase):
surviving = {m for inc in incidents for m in inc.member_alert_ids}
missing = critical_ids - surviving
self.assertEqual(
- len(missing), 0,
- f"{len(missing)} critical alerts dropped during fusion (sample: "
- f"{list(missing)[:5]})",
+ len(missing),
+ 0,
+ f"{len(missing)} critical alerts dropped during fusion (sample: {list(missing)[:5]})",
)
def test_deterministic_run(self) -> None:
@@ -379,7 +402,8 @@ class TestAlertReduction(unittest.TestCase):
a1 = generate_noisy_alert_stream(count=500)
a2 = generate_noisy_alert_stream(count=500)
self.assertEqual(
- [a.__dict__ for a in a1], [a2_.__dict__ for a2_ in a2],
+ [a.__dict__ for a in a1],
+ [a2_.__dict__ for a2_ in a2],
)
def test_storm_collapse(self) -> None:
@@ -389,7 +413,8 @@ class TestAlertReduction(unittest.TestCase):
storm_incidents = [i for i in incidents if i.host.startswith("<storm:")]
# We expect at least a couple of storms in 1000 alerts
self.assertGreaterEqual(
- len(storm_incidents), 1,
+ len(storm_incidents),
+ 1,
"Expected at least 1 storm-incident in a 1000-alert deterministic stream.",
)
diff --git a/services/agents/tests/test_audit_helpers.py b/services/agents/tests/test_audit_helpers.py
index ec461cbc1..a1c4c81b7 100644
--- a/services/agents/tests/test_audit_helpers.py
+++ b/services/agents/tests/test_audit_helpers.py
@@ -10,6 +10,7 @@ can be imported without dragging in LangGraph or any LLM client (the package's
``__init__.py`` eagerly imports the orchestrator which depends on those). The
state module itself only uses stdlib + pydantic + ``app.models.state``.
"""
+
from __future__ import annotations
import importlib
@@ -52,9 +53,7 @@ def test_stable_hash_is_deterministic_across_dict_orderings() -> None:
def test_log_llm_prompt_records_hash_and_metadata() -> None:
state = _make_state()
prompt = "Classify this alert: brute-force on [email protected]"
- h = state.log_llm_prompt(
- agent="recon", prompt=prompt, model="gpt-4o-mini", purpose="recon-extract"
- )
+ h = state.log_llm_prompt(agent="recon", prompt=prompt, model="gpt-4o-mini", purpose="recon-extract")
assert len(state.audit_log) == 1
entry = state.audit_log[0]
assert isinstance(entry, AuditEntry)
diff --git a/services/agents/tests/test_investigation_completeness.py b/services/agents/tests/test_investigation_completeness.py
index 3f152f4d9..dcc58280b 100644
--- a/services/agents/tests/test_investigation_completeness.py
+++ b/services/agents/tests/test_investigation_completeness.py
@@ -32,6 +32,7 @@ See `apps/docs/docs/benchmark.md` for what each suite actually measures.
Run:
pytest services/agents/tests/test_investigation_completeness.py -v
"""
+
from __future__ import annotations
import json
@@ -41,7 +42,6 @@ from dataclasses import dataclass
from pathlib import Path
from typing import Any
-
_TESTS_DIR = Path(__file__).parent
_DATASET_PATH = _TESTS_DIR / "eval_data" / "synthetic_incidents.json"
@@ -49,8 +49,7 @@ _DATASET_PATH = _TESTS_DIR / "eval_data" / "synthetic_incidents.json"
def _load_dataset() -> list[dict[str, Any]]:
if not _DATASET_PATH.exists():
raise FileNotFoundError(
- f"Synthetic incidents dataset missing at {_DATASET_PATH}. "
- f"Run `python3 scripts/generate_eval_incidents.py` to regenerate."
+ f"Synthetic incidents dataset missing at {_DATASET_PATH}. Run `python3 scripts/generate_eval_incidents.py` to regenerate."
)
with _DATASET_PATH.open() as f:
return json.load(f)
@@ -173,7 +172,8 @@ class TestInvestigationCompleteness(unittest.TestCase):
"""Every incident in the eval dataset must declare evidence_keywords."""
missing = [i["id"] for i in SYNTHETIC_INCIDENTS_DATA if not i.get("evidence_keywords")]
self.assertEqual(
- missing, [],
+ missing,
+ [],
f"{len(missing)} incidents missing evidence_keywords (sample: {missing[:5]})",
)
@@ -186,7 +186,8 @@ class TestInvestigationCompleteness(unittest.TestCase):
f"{result.full_coverage_pct * 100:.1f}%)"
)
self.assertGreaterEqual(
- result.mean, 0.85,
+ result.mean,
+ 0.85,
f"Mean completeness {result.mean:.3f} below 0.85 floor.",
)
@@ -194,7 +195,8 @@ class TestInvestigationCompleteness(unittest.TestCase):
"""At least 60% of incidents should achieve full evidence coverage."""
result = evaluate_completeness()
self.assertGreaterEqual(
- result.full_coverage_pct, 0.60,
+ result.full_coverage_pct,
+ 0.60,
f"Only {result.full_coverage_pct * 100:.1f}% of incidents fully covered.",
)
@@ -203,15 +205,21 @@ class TestInvestigationCompleteness(unittest.TestCase):
result = evaluate_completeness(keep_per_incident=True)
zeroed = [r for r in (result.per_incident or []) if r["completeness"] == 0.0]
self.assertEqual(
- zeroed, [],
+ zeroed,
+ [],
f"{len(zeroed)} incidents had zero evidence coverage (sample: {zeroed[:3]})",
)
if __name__ == "__main__":
result = evaluate_completeness()
- print(json.dumps({
- "incidents": result.incidents,
- "mean_completeness": round(result.mean, 4),
- "full_coverage_pct": round(result.full_coverage_pct, 4),
- }, indent=2))
+ print(
+ json.dumps(
+ {
+ "incidents": result.incidents,
+ "mean_completeness": round(result.mean, 4),
+ "full_coverage_pct": round(result.full_coverage_pct, 4),
+ },
+ indent=2,
+ )
+ )
diff --git a/services/agents/tests/test_response_quality.py b/services/agents/tests/test_response_quality.py
index 4265f44a9..e1a56882b 100644
--- a/services/agents/tests/test_response_quality.py
+++ b/services/agents/tests/test_response_quality.py
@@ -37,10 +37,10 @@ See `apps/docs/docs/benchmark.md` for what each suite actually measures.
Run:
pytest services/agents/tests/test_response_quality.py -v
"""
+
from __future__ import annotations
import json
-import re
import unittest
from dataclasses import dataclass
from pathlib import Path
@@ -53,8 +53,7 @@ _DATASET_PATH = _TESTS_DIR / "eval_data" / "synthetic_incidents.json"
def _load_dataset() -> list[dict[str, Any]]:
if not _DATASET_PATH.exists():
raise FileNotFoundError(
- f"Synthetic incidents dataset missing at {_DATASET_PATH}. "
- f"Run `python3 scripts/generate_eval_incidents.py` to regenerate."
+ f"Synthetic incidents dataset missing at {_DATASET_PATH}. Run `python3 scripts/generate_eval_incidents.py` to regenerate."
)
with _DATASET_PATH.open() as f:
return json.load(f)
@@ -103,8 +102,7 @@ _PLAN_TEMPLATES: dict[str, dict[str, Any]] = {
"block_indicator": {
"action": "block_indicator",
"summary": (
- "Block the malicious indicator (IP/domain/hash) at the perimeter "
- "and EDR. Hunt for prior contacts across the environment."
+ "Block the malicious indicator (IP/domain/hash) at the perimeter and EDR. Hunt for prior contacts across the environment."
),
"steps": [
"Block the indicator at firewall, proxy, and DNS sinkhole.",
@@ -115,10 +113,7 @@ _PLAN_TEMPLATES: dict[str, dict[str, Any]] = {
},
"investigate": {
"action": "investigate",
- "summary": (
- "Open an investigation case, gather additional context, and "
- "monitor the host for further activity."
- ),
+ "summary": ("Open an investigation case, gather additional context, and monitor the host for further activity."),
"steps": [
"Open a case and assign a triage analyst.",
"Pull additional telemetry from EDR, network, and identity logs.",
@@ -127,10 +122,7 @@ _PLAN_TEMPLATES: dict[str, dict[str, Any]] = {
},
"rotate_credentials": {
"action": "rotate_credentials",
- "summary": (
- "Rotate the credentials of the affected identity, revoke API "
- "tokens, and audit recent access."
- ),
+ "summary": ("Rotate the credentials of the affected identity, revoke API tokens, and audit recent access."),
"steps": [
"Rotate the credentials and API tokens of the affected identity.",
"Revoke active sessions and OAuth grants.",
@@ -139,10 +131,7 @@ _PLAN_TEMPLATES: dict[str, dict[str, Any]] = {
},
"revoke_token": {
"action": "revoke_token",
- "summary": (
- "Revoke the compromised access token, audit recent API calls, "
- "and block the source IP if required."
- ),
+ "summary": ("Revoke the compromised access token, audit recent API calls, and block the source IP if required."),
"steps": [
"Revoke the compromised access token immediately.",
"Audit recent API calls made with the token.",
@@ -167,9 +156,7 @@ _PLAN_TEMPLATES: dict[str, dict[str, Any]] = {
"escalate": {
"action": "escalate",
"summary": (
- "Escalate to senior incident commander and engage the "
- "appropriate response team. Contain blast radius while "
- "awaiting decision."
+ "Escalate to senior incident commander and engage the appropriate response team. Contain blast radius while awaiting decision."
),
"steps": [
"Escalate to the on-call senior incident commander.",
@@ -181,8 +168,7 @@ _PLAN_TEMPLATES: dict[str, dict[str, Any]] = {
"monitor": {
"action": "monitor",
"summary": (
- "Place the affected entity under enhanced monitoring, log "
- "additional telemetry, and re-evaluate after the watch window."
+ "Place the affected entity under enhanced monitoring, log additional telemetry, and re-evaluate after the watch window."
),
"steps": [
"Place the affected entity under enhanced monitoring.",
@@ -232,15 +218,40 @@ def synthesize_response_plan(incident: dict[str, Any]) -> dict[str, Any]:
_ACTION_VERBS = {
- "isolate", "block", "disable", "reset", "revoke", "rotate", "snapshot",
- "quarantine", "contain", "audit", "hunt", "monitor", "patch", "remove",
- "kill", "remediate", "roll back", "rollback", "escalate", "page",
+ "isolate",
+ "block",
+ "disable",
+ "reset",
+ "revoke",
+ "rotate",
+ "snapshot",
+ "quarantine",
+ "contain",
+ "audit",
+ "hunt",
+ "monitor",
+ "patch",
+ "remove",
+ "kill",
+ "remediate",
+ "roll back",
+ "rollback",
+ "escalate",
+ "page",
"restore",
}
_CONTAINMENT_VERBS = {
- "isolate", "block", "disable", "revoke", "rotate", "quarantine", "contain",
- "roll back", "rollback", "escalate",
+ "isolate",
+ "block",
+ "disable",
+ "revoke",
+ "rotate",
+ "quarantine",
+ "contain",
+ "roll back",
+ "rollback",
+ "escalate",
}
_RESPONSE_CLASS_VERBS: dict[str, set[str]] = {
@@ -278,32 +289,20 @@ def judge_response_plan(plan: dict[str, Any], incident: dict[str, Any]) -> dict[
if severity in ("high", "critical"):
severity_aware = 1.0 if any(v in text for v in _CONTAINMENT_VERBS) else 0.0
else:
- severity_aware = (
- 1.0 if any(v in text for v in {"investigate", "monitor", "audit", "hunt"}) else 0.0
- )
+ severity_aware = 1.0 if any(v in text for v in {"investigate", "monitor", "audit", "hunt"}) else 0.0
# 3. mitre_aligned
- mitre_pool = (incident.get("expected_tactics") or []) + (
- incident.get("expected_techniques") or []
- )
- mitre_aligned = (
- 1.0
- if any(m.lower() in text for m in mitre_pool)
- else 0.0
- )
+ mitre_pool = (incident.get("expected_tactics") or []) + (incident.get("expected_techniques") or [])
+ mitre_aligned = 1.0 if any(m.lower() in text for m in mitre_pool) else 0.0
# 4. evidence_grounded
evidence_kws = incident.get("evidence_keywords") or []
- evidence_grounded = (
- 1.0 if any(kw.lower() in text for kw in evidence_kws) else 0.0
- )
+ evidence_grounded = 1.0 if any(kw.lower() in text for kw in evidence_kws) else 0.0
# 5. actionable
actionable = 1.0 if any(v in text for v in _ACTION_VERBS) else 0.0
- score = (
- action_aligned + severity_aware + mitre_aligned + evidence_grounded + actionable
- ) / 5.0
+ score = (action_aligned + severity_aware + mitre_aligned + evidence_grounded + actionable) / 5.0
return {
"action_aligned": action_aligned,
@@ -379,7 +378,8 @@ class TestResponseQuality(unittest.TestCase):
f"actionable: {result.crit_mean('actionable'):.2f}"
)
self.assertGreaterEqual(
- result.mean_score, 0.80,
+ result.mean_score,
+ 0.80,
f"Mean response-quality score {result.mean_score:.3f} below 0.80 floor.",
)
@@ -388,7 +388,8 @@ class TestResponseQuality(unittest.TestCase):
result = evaluate_response_quality(keep_per_incident=True)
not_actionable = [r for r in (result.per_incident or []) if r["actionable"] == 0.0]
self.assertEqual(
- not_actionable, [],
+ not_actionable,
+ [],
f"{len(not_actionable)} plans had no actionable verb (sample: {not_actionable[:3]}).",
)
@@ -405,7 +406,8 @@ class TestResponseQuality(unittest.TestCase):
if not row or row["severity_aware"] != 1.0:
bad.append(inc["id"])
self.assertEqual(
- bad, [],
+ bad,
+ [],
f"{len(bad)} high/critical incidents had no containment verb (sample: {bad[:3]}).",
)
@@ -413,15 +415,21 @@ class TestResponseQuality(unittest.TestCase):
"""≥ 90% of incidents must produce a plan whose action matches the response class."""
result = evaluate_response_quality()
self.assertGreaterEqual(
- result.crit_mean("action_aligned"), 0.90,
+ result.crit_mean("action_aligned"),
+ 0.90,
f"Only {result.crit_mean('action_aligned') * 100:.1f}% of plans aligned with response_class.",
)
if __name__ == "__main__":
result = evaluate_response_quality()
- print(json.dumps({
- "incidents": result.incidents,
- "mean_score": round(result.mean_score, 4),
- "criteria": {k: round(result.crit_mean(k), 4) for k in result.crit_sum},
- }, indent=2))
+ print(
+ json.dumps(
+ {
+ "incidents": result.incidents,
+ "mean_score": round(result.mean_score, 4),
+ "criteria": {k: round(result.crit_mean(k), 4) for k in result.crit_sum},
+ },
+ indent=2,
+ )
+ )
diff --git a/services/api/app/api/v1/deps.py b/services/api/app/api/v1/deps.py
index c0e5c9d53..7a0c032d7 100644
--- a/services/api/app/api/v1/deps.py
+++ b/services/api/app/api/v1/deps.py
@@ -20,8 +20,8 @@ activates the RLS policies defined in ``migrations/002_rls.sql``.
async def list_cases(db: TenantDBSession, user: AuthUser):
...
"""
+
import uuid
-from collections.abc import AsyncGenerator
from datetime import UTC, datetime
from typing import Annotated
@@ -40,7 +40,7 @@ from app.api.v1.dev_auth import (
)
from app.core.security import decode_token, has_permission, hash_api_key
from app.db.database import get_db
-from app.models.tenant import ApiKey, Tenant, User
+from app.models.tenant import ApiKey, User
bearer_scheme = HTTPBearer(auto_error=False)
@@ -77,11 +77,7 @@ class CurrentUser:
def require_permission(self, permission: str) -> None:
if self.scopes is not None:
# API-key path: check explicit scopes list
- allowed = (
- "*" in self.scopes
- or permission in self.scopes
- or f"{permission.split(':')[0]}:*" in self.scopes
- )
+ allowed = "*" in self.scopes or permission in self.scopes or f"{permission.split(':')[0]}:*" in self.scopes
if not allowed:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
@@ -102,14 +98,11 @@ class CurrentUser:
no rows in ``user_roles`` (e.g. fresh tenants not yet migrated).
"""
if self.scopes is not None:
- return (
- "*" in self.scopes
- or permission in self.scopes
- or f"{permission.split(':')[0]}:*" in self.scopes
- )
+ return "*" in self.scopes or permission in self.scopes or f"{permission.split(':')[0]}:*" in self.scopes
# Query RBAC tables
- from app.models.rbac import Permission as PermModel, Role, RolePermission, UserRole # noqa: PLC0415
+ from app.models.rbac import Permission as PermModel # noqa: PLC0415
+ from app.models.rbac import Role, RolePermission, UserRole
result = await db.execute(
select(PermModel.name)
@@ -121,11 +114,7 @@ class CurrentUser:
db_perms: list[str] = [row[0] for row in result.all()]
if db_perms:
- return (
- "*" in db_perms
- or permission in db_perms
- or f"{permission.split(':')[0]}:*" in db_perms
- )
+ return "*" in db_perms or permission in db_perms or f"{permission.split(':')[0]}:*" in db_perms
# Fallback to static map
return has_permission(self.role, permission)
@@ -165,11 +154,7 @@ async def _resolve_api_key(raw_key: str, db: AsyncSession) -> CurrentUser:
)
# Update last_used_at in background (fire-and-forget style — don't await)
- await db.execute(
- update(ApiKey)
- .where(ApiKey.id == api_key.id)
- .values(last_used_at=datetime.now(UTC))
- )
+ await db.execute(update(ApiKey).where(ApiKey.id == api_key.id).values(last_used_at=datetime.now(UTC)))
# Fetch the owning user for context (user_id may be NULL for service keys)
role = "api_service"
@@ -274,8 +259,10 @@ def require_permission(permission: str):
DBSession = Annotated[AsyncSession, Depends(get_db)]
AuthUser = Annotated[CurrentUser, Depends(get_current_user)]
+
# Re-export TenantDBSession for convenience so endpoints can import from one place
# Actual implementation lives in app.db.rls to avoid circular imports.
def _get_tenant_db_session() -> "Annotated[AsyncSession, ...]": # pragma: no cover
from app.db.rls import TenantDBSession as _T # noqa: PLC0415
+
return _T
diff --git a/services/api/app/api/v1/dev_auth.py b/services/api/app/api/v1/dev_auth.py
index 4dc7d7ce0..816a17879 100644
--- a/services/api/app/api/v1/dev_auth.py
+++ b/services/api/app/api/v1/dev_auth.py
@@ -11,6 +11,7 @@ data the UI sees actually belongs to the user that the API hands back.
Production safety: this module reads ``ENV`` at call time and refuses to
return a demo user unless ``ENV in {development, dev, local, demo}``.
"""
+
from __future__ import annotations
import os
diff --git a/services/api/app/api/v1/endpoints/alerts.py b/services/api/app/api/v1/endpoints/alerts.py
index 785b0d044..f387275a0 100644
--- a/services/api/app/api/v1/endpoints/alerts.py
+++ b/services/api/app/api/v1/endpoints/alerts.py
@@ -1,4 +1,5 @@
"""Alert management endpoints."""
+
import uuid
from datetime import UTC, datetime
from typing import Annotated
@@ -6,7 +7,6 @@ from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, Query, status
from pydantic import BaseModel
from sqlalchemy import and_, func, select, update
-from sqlalchemy.ext.asyncio import AsyncSession
from app.api.v1.deps import AuthUser, DBSession, require_permission
from app.db.rls import TenantDBSession
@@ -103,20 +103,12 @@ async def list_alerts(
filters.append(Alert.assigned_to_id == current_user.user_id)
# Count
- count_result = await db.execute(
- select(func.count()).select_from(Alert).where(and_(*filters))
- )
+ count_result = await db.execute(select(func.count()).select_from(Alert).where(and_(*filters)))
total = count_result.scalar_one()
# Fetch
offset = (page - 1) * page_size
- result = await db.execute(
- select(Alert)
- .where(and_(*filters))
- .order_by(Alert.created_at.desc())
- .offset(offset)
- .limit(page_size)
- )
+ result = await db.execute(select(Alert).where(and_(*filters)).order_by(Alert.created_at.desc()).offset(offset).limit(page_size))
alerts = result.scalars().all()
return AlertListResponse(
@@ -136,8 +128,6 @@ async def get_alert_stats(
"""Get alert statistics for the dashboard."""
from datetime import timedelta
- from sqlalchemy import case as sa_case
-
tenant_filter = Alert.tenant_id == current_user.tenant_id
# Total count
@@ -145,31 +135,23 @@ async def get_alert_stats(
total = total_result.scalar_one()
# By severity
- sev_result = await db.execute(
- select(Alert.severity, func.count()).where(tenant_filter).group_by(Alert.severity)
- )
+ sev_result = await db.execute(select(Alert.severity, func.count()).where(tenant_filter).group_by(Alert.severity))
by_severity = {row[0]: row[1] for row in sev_result.all()}
# By status
- status_result = await db.execute(
- select(Alert.status, func.count()).where(tenant_filter).group_by(Alert.status)
- )
+ status_result = await db.execute(select(Alert.status, func.count()).where(tenant_filter).group_by(Alert.status))
by_status = {row[0]: row[1] for row in status_result.all()}
# New last 24h
cutoff = datetime.now(UTC) - timedelta(hours=24)
- new_24h_result = await db.execute(
- select(func.count()).select_from(Alert).where(
- and_(tenant_filter, Alert.created_at >= cutoff)
- )
- )
+ new_24h_result = await db.execute(select(func.count()).select_from(Alert).where(and_(tenant_filter, Alert.created_at >= cutoff)))
new_last_24h = new_24h_result.scalar_one()
# Critical open
crit_result = await db.execute(
- select(func.count()).select_from(Alert).where(
- and_(tenant_filter, Alert.severity == "critical", Alert.status.in_(["new", "triaging", "in_progress"]))
- )
+ select(func.count())
+ .select_from(Alert)
+ .where(and_(tenant_filter, Alert.severity == "critical", Alert.status.in_(["new", "triaging", "in_progress"])))
)
critical_open = crit_result.scalar_one()
@@ -189,9 +171,7 @@ async def get_alert(
db: DBSession,
) -> AlertResponse:
"""Get a single alert by ID."""
- result = await db.execute(
- select(Alert).where(Alert.id == alert_id, Alert.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(Alert).where(Alert.id == alert_id, Alert.tenant_id == current_user.tenant_id))
alert = result.scalar_one_or_none()
if alert is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Alert not found")
@@ -206,9 +186,7 @@ async def update_alert(
db: DBSession,
) -> AlertResponse:
"""Update alert status, priority, tags, assignment, or case link."""
- result = await db.execute(
- select(Alert).where(Alert.id == alert_id, Alert.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(Alert).where(Alert.id == alert_id, Alert.tenant_id == current_user.tenant_id))
alert = result.scalar_one_or_none()
if alert is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Alert not found")
@@ -246,9 +224,7 @@ async def escalate_alert(
"""Escalate an alert (raises severity by one level)."""
severity_ladder = ["info", "low", "medium", "high", "critical"]
- result = await db.execute(
- select(Alert).where(Alert.id == alert_id, Alert.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(Alert).where(Alert.id == alert_id, Alert.tenant_id == current_user.tenant_id))
alert = result.scalar_one_or_none()
if alert is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Alert not found")
@@ -256,11 +232,7 @@ async def escalate_alert(
current_idx = severity_ladder.index(alert.severity) if alert.severity in severity_ladder else 2
new_severity = severity_ladder[min(current_idx + 1, len(severity_ladder) - 1)]
- await db.execute(
- update(Alert).where(Alert.id == alert_id).values(
- severity=new_severity, updated_at=datetime.now(UTC)
- )
- )
+ await db.execute(update(Alert).where(Alert.id == alert_id).values(severity=new_severity, updated_at=datetime.now(UTC)))
await db.commit()
await db.refresh(alert)
@@ -297,16 +269,10 @@ async def snooze_alert(
)
snoozed_until = datetime.now(UTC) + timedelta(minutes=body.duration_minutes or 0)
- result = await db.execute(
- select(Alert).where(
- Alert.id == alert_id, Alert.tenant_id == current_user.tenant_id
- )
- )
+ result = await db.execute(select(Alert).where(Alert.id == alert_id, Alert.tenant_id == current_user.tenant_id))
alert = result.scalar_one_or_none()
if alert is None:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail="Alert not found"
- )
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Alert not found")
await db.execute(
update(Alert)
diff --git a/services/api/app/api/v1/endpoints/api_keys.py b/services/api/app/api/v1/endpoints/api_keys.py
index 05bdecff7..907d810dd 100644
--- a/services/api/app/api/v1/endpoints/api_keys.py
+++ b/services/api/app/api/v1/endpoints/api_keys.py
@@ -21,6 +21,7 @@ VALID SCOPES
MIT License — AiSOC (open-source AI Security Operations Center)
"""
+
from __future__ import annotations
import uuid
@@ -41,15 +42,28 @@ router = APIRouter(prefix="/api-keys", tags=["api-keys"])
VALID_SCOPES: frozenset[str] = frozenset(
[
- "alerts:read", "alerts:write", "alerts:delete",
- "cases:read", "cases:write", "cases:delete",
- "playbooks:read", "playbooks:write", "playbooks:execute",
- "connectors:read", "connectors:write", "connectors:delete",
- "plugins:read", "plugins:execute", "plugins:admin",
- "rules:read", "rules:write",
+ "alerts:read",
+ "alerts:write",
+ "alerts:delete",
+ "cases:read",
+ "cases:write",
+ "cases:delete",
+ "playbooks:read",
+ "playbooks:write",
+ "playbooks:execute",
+ "connectors:read",
+ "connectors:write",
+ "connectors:delete",
+ "plugins:read",
+ "plugins:execute",
+ "plugins:admin",
+ "rules:read",
+ "rules:write",
"users:read",
- "threat_intel:read", "threat_intel:write",
- "reports:read", "reports:write",
+ "threat_intel:read",
+ "threat_intel:write",
+ "reports:read",
+ "reports:write",
"*",
]
)
@@ -66,6 +80,7 @@ def _validate_scopes(scopes: list[str]) -> None:
# ── Request / response schemas ────────────────────────────────────────────────
+
class CreateApiKeyRequest(BaseModel):
name: str = Field(..., min_length=1, max_length=255, description="Human-readable name")
scopes: list[str] = Field(default_factory=list, description="Permission scopes granted to this key")
@@ -103,6 +118,7 @@ class UpdateApiKeyRequest(BaseModel):
# ── Helper ────────────────────────────────────────────────────────────────────
+
def _api_key_to_out(ak: ApiKey) -> ApiKeyOut:
return ApiKeyOut(
id=ak.id,
@@ -118,6 +134,7 @@ def _api_key_to_out(ak: ApiKey) -> ApiKeyOut:
# ── Endpoints ─────────────────────────────────────────────────────────────────
+
@router.post("", response_model=CreateApiKeyResponse, status_code=status.HTTP_201_CREATED)
async def create_api_key(
body: CreateApiKeyRequest,
@@ -143,6 +160,7 @@ async def create_api_key(
expires_at = None
if body.expires_in_days is not None:
from datetime import timedelta
+
expires_at = datetime.now(UTC) + timedelta(days=body.expires_in_days)
api_key = ApiKey(
@@ -176,11 +194,7 @@ async def list_api_keys(
current_user: AuthUser,
) -> list[ApiKeyOut]:
"""List all API keys for the current tenant (raw keys are never returned)."""
- result = await db.execute(
- select(ApiKey)
- .where(ApiKey.tenant_id == current_user.tenant_id)
- .order_by(ApiKey.created_at.desc())
- )
+ result = await db.execute(select(ApiKey).where(ApiKey.tenant_id == current_user.tenant_id).order_by(ApiKey.created_at.desc()))
return [_api_key_to_out(ak) for ak in result.scalars().all()]
@@ -191,9 +205,7 @@ async def get_api_key(
current_user: AuthUser,
) -> ApiKeyOut:
"""Get metadata for a specific API key."""
- result = await db.execute(
- select(ApiKey).where(ApiKey.id == key_id, ApiKey.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(ApiKey).where(ApiKey.id == key_id, ApiKey.tenant_id == current_user.tenant_id))
ak = result.scalar_one_or_none()
if ak is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="API key not found")
@@ -208,9 +220,7 @@ async def update_api_key(
current_user: Annotated[Any, require_permission("users:write")],
) -> ApiKeyOut:
"""Update an API key's name, scopes, or expiry."""
- result = await db.execute(
- select(ApiKey).where(ApiKey.id == key_id, ApiKey.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(ApiKey).where(ApiKey.id == key_id, ApiKey.tenant_id == current_user.tenant_id))
ak = result.scalar_one_or_none()
if ak is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="API key not found")
@@ -229,6 +239,7 @@ async def update_api_key(
if body.expires_in_days is not None:
from datetime import timedelta
+
ak.expires_at = datetime.now(UTC) + timedelta(days=body.expires_in_days)
await db.commit()
@@ -243,9 +254,7 @@ async def revoke_api_key(
current_user: Annotated[Any, require_permission("users:write")],
) -> None:
"""Revoke (deactivate) an API key. The key can no longer be used for authentication."""
- result = await db.execute(
- select(ApiKey).where(ApiKey.id == key_id, ApiKey.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(ApiKey).where(ApiKey.id == key_id, ApiKey.tenant_id == current_user.tenant_id))
ak = result.scalar_one_or_none()
if ak is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="API key not found")
diff --git a/services/api/app/api/v1/endpoints/approvals.py b/services/api/app/api/v1/endpoints/approvals.py
index 3b7d26e0d..47924fcaa 100644
--- a/services/api/app/api/v1/endpoints/approvals.py
+++ b/services/api/app/api/v1/endpoints/approvals.py
@@ -13,6 +13,7 @@ Endpoints
* ``POST /approvals`` Create one (called by the agent service).
* ``POST /approvals/{id}/decide`` Approve or deny.
"""
+
from __future__ import annotations
import logging
@@ -154,9 +155,7 @@ async def get_approval(
)
).scalar_one_or_none()
if row is None:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail="Approval not found"
- )
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Approval not found")
return ApprovalResponse.model_validate(row)
@@ -220,9 +219,7 @@ async def decide_approval(
)
).scalar_one_or_none()
if row is None:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail="Approval not found"
- )
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Approval not found")
new_status = "approved" if body.decision == "approve" else "denied"
@@ -279,9 +276,7 @@ async def _notify_realtime(row: AgentApproval, *, event: str) -> None:
"risk_level": row.risk_level,
"status": row.status,
"decision_comment": row.decision_comment,
- "notify_user_ids": (
- [str(row.required_user_id)] if row.required_user_id else None
- ),
+ "notify_user_ids": ([str(row.required_user_id)] if row.required_user_id else None),
},
}
diff --git a/services/api/app/api/v1/endpoints/audit.py b/services/api/app/api/v1/endpoints/audit.py
index dca964135..1954638fb 100644
--- a/services/api/app/api/v1/endpoints/audit.py
+++ b/services/api/app/api/v1/endpoints/audit.py
@@ -2,6 +2,7 @@
GET /api/v1/audit — paginated, filterable audit trail
"""
+
import uuid
from typing import Annotated
@@ -61,9 +62,7 @@ async def list_audit_events(
q = q.where(AuditLog.actor_id == actor_id)
if search:
term = f"%{search}%"
- q = q.where(
- AuditLog.action.ilike(term) | AuditLog.actor_email.ilike(term)
- )
+ q = q.where(AuditLog.action.ilike(term) | AuditLog.actor_email.ilike(term))
count_q = select(func.count()).select_from(q.subquery())
total_result = await db.execute(count_q)
diff --git a/services/api/app/api/v1/endpoints/auth.py b/services/api/app/api/v1/endpoints/auth.py
index 39083d14d..e5ab6429a 100644
--- a/services/api/app/api/v1/endpoints/auth.py
+++ b/services/api/app/api/v1/endpoints/auth.py
@@ -1,12 +1,11 @@
"""Authentication endpoints: login, refresh, logout."""
+
import uuid
-from datetime import UTC, datetime, timedelta
-from typing import Annotated
+from datetime import UTC, datetime
-from fastapi import APIRouter, Depends, HTTPException, status
+from fastapi import APIRouter, HTTPException, status
from pydantic import BaseModel, EmailStr
from sqlalchemy import select, update
-from sqlalchemy.ext.asyncio import AsyncSession
from app.api.v1.deps import AuthUser, DBSession
from app.core.config import settings
@@ -51,9 +50,7 @@ class UserMeResponse(BaseModel):
@router.post("/login", response_model=TokenResponse)
async def login(request: LoginRequest, db: DBSession) -> TokenResponse:
"""Authenticate with email/password, return JWT tokens."""
- result = await db.execute(
- select(User).where(User.email == request.email, User.is_active == True)
- )
+ result = await db.execute(select(User).where(User.email == request.email, User.is_active.is_(True)))
user = result.scalar_one_or_none()
if user is None or not verify_password(request.password, user.hashed_password):
@@ -64,9 +61,7 @@ async def login(request: LoginRequest, db: DBSession) -> TokenResponse:
)
# Update last login
- await db.execute(
- update(User).where(User.id == user.id).values(last_login=datetime.now(UTC))
- )
+ await db.execute(update(User).where(User.id == user.id).values(last_login=datetime.now(UTC)))
token_data = {
"sub": str(user.id),
@@ -87,18 +82,14 @@ async def login(request: LoginRequest, db: DBSession) -> TokenResponse:
async def refresh_token(request: RefreshRequest, db: DBSession) -> TokenResponse:
"""Refresh access token using a valid refresh token."""
try:
- from jose import JWTError
-
payload = decode_token(request.refresh_token)
if payload.get("type") != "refresh":
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token type")
user_id = payload.get("sub")
except Exception as e:
- raise HTTPException(
- status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid refresh token"
- ) from e
+ raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid refresh token") from e
- result = await db.execute(select(User).where(User.id == uuid.UUID(user_id), User.is_active == True))
+ result = await db.execute(select(User).where(User.id == uuid.UUID(user_id), User.is_active.is_(True)))
user = result.scalar_one_or_none()
if user is None:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found")
diff --git a/services/api/app/api/v1/endpoints/cases.py b/services/api/app/api/v1/endpoints/cases.py
index a567a7d7e..6566fa77d 100644
--- a/services/api/app/api/v1/endpoints/cases.py
+++ b/services/api/app/api/v1/endpoints/cases.py
@@ -1,18 +1,18 @@
"""Case management endpoints."""
+
import os
import uuid
from datetime import UTC, datetime
from typing import Annotated, Any
import httpx
-from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query, status
+from fastapi import APIRouter, Depends, HTTPException, Query, status
from pydantic import BaseModel, Field
from sqlalchemy import and_, func, select, update
-from sqlalchemy.ext.asyncio import AsyncSession
from app.api.v1.deps import AuthUser, DBSession, require_permission
from app.db.rls import TenantDBSession
-from app.models.case import Case, CaseTask, CaseTimeline
+from app.models.case import Case, CaseTimeline
_AGENTS_URL = os.getenv("AGENTS_SERVICE_URL", "http://agents:8000")
@@ -121,15 +121,11 @@ async def list_cases(
if assigned_to_me:
filters.append(Case.assigned_to_id == current_user.user_id)
- count_result = await db.execute(
- select(func.count()).select_from(Case).where(and_(*filters))
- )
+ count_result = await db.execute(select(func.count()).select_from(Case).where(and_(*filters)))
total = count_result.scalar_one()
offset = (page - 1) * page_size
- result = await db.execute(
- select(Case).where(and_(*filters)).order_by(Case.created_at.desc()).offset(offset).limit(page_size)
- )
+ result = await db.execute(select(Case).where(and_(*filters)).order_by(Case.created_at.desc()).offset(offset).limit(page_size))
cases = result.scalars().all()
return CaseListResponse(
@@ -185,9 +181,7 @@ async def get_case(
db: DBSession,
) -> CaseResponse:
"""Get a case by ID."""
- result = await db.execute(
- select(Case).where(Case.id == case_id, Case.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(Case).where(Case.id == case_id, Case.tenant_id == current_user.tenant_id))
case = result.scalar_one_or_none()
if case is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Case not found")
@@ -202,9 +196,7 @@ async def update_case(
db: DBSession,
) -> CaseResponse:
"""Update a case."""
- result = await db.execute(
- select(Case).where(Case.id == case_id, Case.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(Case).where(Case.id == case_id, Case.tenant_id == current_user.tenant_id))
case = result.scalar_one_or_none()
if case is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Case not found")
@@ -223,14 +215,16 @@ async def update_case(
await db.execute(update(Case).where(Case.id == case_id).values(**updates))
# Add timeline event
- db.add(CaseTimeline(
- case_id=case_id,
- tenant_id=current_user.tenant_id,
- event_type="case_updated",
- content=f"Case updated by {current_user.email}: {', '.join(updates.keys())}",
- user_id=current_user.user_id,
- event_metadata={"changed_fields": list(updates.keys())},
- ))
+ db.add(
+ CaseTimeline(
+ case_id=case_id,
+ tenant_id=current_user.tenant_id,
+ event_type="case_updated",
+ content=f"Case updated by {current_user.email}: {', '.join(updates.keys())}",
+ user_id=current_user.user_id,
+ event_metadata={"changed_fields": list(updates.keys())},
+ )
+ )
await db.commit()
await db.refresh(case)
@@ -246,9 +240,7 @@ async def add_timeline_event(
) -> TimelineEventResponse:
"""Add a comment or event to the case timeline."""
# Verify case exists for this tenant
- result = await db.execute(
- select(Case).where(Case.id == case_id, Case.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(Case).where(Case.id == case_id, Case.tenant_id == current_user.tenant_id))
if result.scalar_one_or_none() is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Case not found")
@@ -287,6 +279,7 @@ async def get_timeline(
# Pillar-1: AI Investigation
# ---------------------------------------------------------------------------
+
class InvestigateRequest(BaseModel):
alert_summary: str = ""
raw_alert: dict[str, Any] = {}
@@ -311,9 +304,7 @@ async def investigate_case(
Proxies to the agents service Pillar-1 orchestrator.
"""
# Verify case ownership
- result = await db.execute(
- select(Case).where(Case.id == case_id, Case.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(Case).where(Case.id == case_id, Case.tenant_id == current_user.tenant_id))
case = result.scalar_one_or_none()
if case is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Case not found")
@@ -339,15 +330,17 @@ async def investigate_case(
raise HTTPException(status_code=503, detail=f"Agents service unavailable: {exc}") from exc
# Record timeline entry
- db.add(CaseTimeline(
- case_id=case_id,
- tenant_id=current_user.tenant_id,
- event_type="ai_investigation_started",
- content=f"AI investigation started (run_id={data.get('run_id')})",
- user_id=current_user.user_id,
- is_automated=True,
- event_metadata={"run_id": data.get("run_id")},
- ))
+ db.add(
+ CaseTimeline(
+ case_id=case_id,
+ tenant_id=current_user.tenant_id,
+ event_type="ai_investigation_started",
+ content=f"AI investigation started (run_id={data.get('run_id')})",
+ user_id=current_user.user_id,
+ is_automated=True,
+ event_metadata={"run_id": data.get("run_id")},
+ )
+ )
await db.commit()
return InvestigateResponse(**data)
@@ -379,6 +372,7 @@ async def get_case_report_md(
) -> Any:
"""Proxy: download Markdown incident report from the agents service."""
from fastapi.responses import PlainTextResponse
+
try:
async with httpx.AsyncClient(timeout=30.0) as client:
resp = await client.get(f"{_AGENTS_URL}/api/v1/investigations/{run_id}/report.md")
@@ -398,6 +392,7 @@ async def get_case_report_html(
) -> Any:
"""Proxy: download HTML incident report from the agents service."""
from fastapi.responses import HTMLResponse
+
try:
async with httpx.AsyncClient(timeout=30.0) as client:
resp = await client.get(f"{_AGENTS_URL}/api/v1/investigations/{run_id}/report.html")
@@ -417,6 +412,7 @@ async def get_case_report_pdf(
) -> Any:
"""Proxy: download PDF incident report (rendered by weasyprint in agents service)."""
from fastapi.responses import Response as FastAPIResponse
+
try:
async with httpx.AsyncClient(timeout=60.0) as client:
resp = await client.get(f"{_AGENTS_URL}/api/v1/investigations/{run_id}/report.pdf")
diff --git a/services/api/app/api/v1/endpoints/community.py b/services/api/app/api/v1/endpoints/community.py
index 5a74b7087..7b6d1a2fa 100644
--- a/services/api/app/api/v1/endpoints/community.py
+++ b/services/api/app/api/v1/endpoints/community.py
@@ -18,17 +18,26 @@ GET /community/playbooks – browse community playbooks
POST /community/playbooks/{id}/install – install playbook
PUT /community/playbooks/{id}/curate – admin: approve/reject playbook
"""
+
from __future__ import annotations
import base64
import hashlib
import json
import uuid
-from datetime import datetime, timezone
+from datetime import UTC, datetime
from enum import Enum
-from typing import Annotated, Any, Optional
-
-from fastapi import APIRouter, Body, Depends, File, HTTPException, Query, Request, UploadFile, status
+from typing import Annotated, Any
+
+from fastapi import (
+ APIRouter,
+ Body,
+ Depends,
+ HTTPException,
+ Query,
+ Request,
+ status,
+)
from pydantic import BaseModel, Field
from app.api.v1.deps import AuthUser, CurrentUser, require_permission
@@ -65,7 +74,7 @@ class CommunityPluginOut(BaseModel):
rating_count: int = 0
verified: bool = False
submitted_at: str
- approved_at: Optional[str] = None
+ approved_at: str | None = None
class CommunityPluginListOut(BaseModel):
@@ -75,12 +84,12 @@ class CommunityPluginListOut(BaseModel):
class ReviewAction(BaseModel):
action: str = Field(..., pattern="^(approve|reject)$")
- notes: Optional[str] = None
+ notes: str | None = None
class RatingIn(BaseModel):
score: int = Field(..., ge=1, le=5)
- comment: Optional[str] = None
+ comment: str | None = None
class CommunityDetectionOut(BaseModel):
@@ -93,7 +102,7 @@ class CommunityDetectionOut(BaseModel):
install_count: int = 0
logsource: dict[str, Any] = {}
submitted_at: str
- content: Optional[str] = None
+ content: str | None = None
class CommunityPlaybookOut(BaseModel):
@@ -105,7 +114,7 @@ class CommunityPlaybookOut(BaseModel):
status: PublishStatus = PublishStatus.PENDING
install_count: int = 0
submitted_at: str
- definition: Optional[dict[str, Any]] = None
+ definition: dict[str, Any] | None = None
# ── Plugin endpoints ──────────────────────────────────────────────────────────
@@ -143,11 +152,11 @@ async def publish_plugin(
try:
verify_ed25519_signature(registered_pub_key, tarball, signature)
verified = True
- except Exception:
+ except Exception as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid Ed25519 signature",
- )
+ ) from exc
plugin_id = manifest.get("id", str(uuid.uuid4()))
entry = {
@@ -164,7 +173,7 @@ async def publish_plugin(
"rating_count": 0,
"verified": verified,
"submitted_by": current_user.id,
- "submitted_at": datetime.now(timezone.utc).isoformat(),
+ "submitted_at": datetime.now(UTC).isoformat(),
"approved_at": None,
"tarball_sha256": hashlib.sha256(tarball).hexdigest(),
"_tarball": tarball,
@@ -176,9 +185,9 @@ async def publish_plugin(
@router.get("/plugins", response_model=CommunityPluginListOut)
async def list_community_plugins(
- status_filter: Optional[str] = Query(None, alias="status"),
- plugin_type: Optional[str] = Query(None),
- tags: Optional[str] = Query(None, description="Comma-separated tags"),
+ status_filter: str | None = Query(None, alias="status"),
+ plugin_type: str | None = Query(None),
+ tags: str | None = Query(None, description="Comma-separated tags"),
sort: str = Query("install_count", pattern="^(install_count|rating|submitted_at|name)$"),
order: str = Query("desc", pattern="^(asc|desc)$"),
page: int = Query(1, ge=1),
@@ -270,7 +279,7 @@ async def review_community_plugin(
if review.action == "approve":
p["status"] = PublishStatus.APPROVED
- p["approved_at"] = datetime.now(timezone.utc).isoformat()
+ p["approved_at"] = datetime.now(UTC).isoformat()
else:
p["status"] = PublishStatus.REJECTED
p["review_notes"] = review.notes
@@ -314,7 +323,7 @@ async def publish_detection(
"install_count": 0,
"rating": 0.0,
"rating_count": 0,
- "submitted_at": datetime.now(timezone.utc).isoformat(),
+ "submitted_at": datetime.now(UTC).isoformat(),
"sigma_yaml": content,
}
_community_detections[detection_id] = entry
@@ -326,10 +335,10 @@ async def publish_detection(
async def list_community_detections(
page: int = Query(1, ge=1),
page_size: int = Query(20, ge=1, le=100),
- search: Optional[str] = Query(None),
- logsource_category: Optional[str] = Query(None),
- logsource_product: Optional[str] = Query(None),
- level: Optional[str] = Query(None),
+ search: str | None = Query(None),
+ logsource_category: str | None = Query(None),
+ logsource_product: str | None = Query(None),
+ level: str | None = Query(None),
sort_by: str = Query("install_count", pattern="^(install_count|rating|name)$"),
) -> dict[str, Any]:
"""Browse community Sigma detection rules with pagination and filtering."""
@@ -341,10 +350,9 @@ async def list_community_detections(
if search:
q = search.lower()
items = [
- d for d in items
- if q in d.get("name", "").lower()
- or q in d.get("description", "").lower()
- or any(q in t.lower() for t in d.get("tags", []))
+ d
+ for d in items
+ if q in d.get("name", "").lower() or q in d.get("description", "").lower() or any(q in t.lower() for t in d.get("tags", []))
]
if logsource_category:
items = [d for d in items if d.get("logsource_category") == logsource_category]
@@ -415,7 +423,7 @@ async def submit_playbook(
"install_count": 0,
"rating": 0.0,
"rating_count": 0,
- "submitted_at": datetime.now(timezone.utc).isoformat(),
+ "submitted_at": datetime.now(UTC).isoformat(),
"definition": definition,
}
_community_playbooks[playbook_id] = entry
@@ -427,7 +435,7 @@ async def submit_playbook(
async def list_community_playbooks(
page: int = Query(1, ge=1),
page_size: int = Query(20, ge=1, le=100),
- search: Optional[str] = Query(None),
+ search: str | None = Query(None),
sort_by: str = Query("install_count", pattern="^(install_count|rating|name)$"),
) -> dict[str, Any]:
"""Browse approved community playbooks with optional search and sort."""
@@ -436,10 +444,9 @@ async def list_community_playbooks(
if search:
q = search.lower()
items = [
- p for p in items
- if q in p.get("name", "").lower()
- or q in p.get("description", "").lower()
- or any(q in t.lower() for t in p.get("tags", []))
+ p
+ for p in items
+ if q in p.get("name", "").lower() or q in p.get("description", "").lower() or any(q in t.lower() for t in p.get("tags", []))
]
if sort_by == "name":
@@ -492,6 +499,7 @@ async def curate_community_playbook(
# ── Helpers ───────────────────────────────────────────────────────────────────
-def _get_registered_pub_key(user_id: str) -> Optional[bytes]:
+
+def _get_registered_pub_key(user_id: str) -> bytes | None:
"""Retrieve user's registered Ed25519 public key (stub — wire to DB)."""
return None
diff --git a/services/api/app/api/v1/endpoints/compliance.py b/services/api/app/api/v1/endpoints/compliance.py
index ccc432a53..53400a72d 100644
--- a/services/api/app/api/v1/endpoints/compliance.py
+++ b/services/api/app/api/v1/endpoints/compliance.py
@@ -133,9 +133,7 @@ async def _get_framework_data(
) -> tuple[list[ComplianceControl], dict[uuid.UUID, list[ComplianceEvidence]]]:
"""Fetch controls and grouped evidence for a framework."""
ctrl_result = await db.execute(
- select(ComplianceControl)
- .where(ComplianceControl.framework == framework)
- .order_by(ComplianceControl.control_id)
+ select(ComplianceControl).where(ComplianceControl.framework == framework).order_by(ComplianceControl.control_id)
)
controls = ctrl_result.scalars().all()
@@ -224,8 +222,7 @@ async def list_frameworks(
) -> list[FrameworkInfo]:
"""Return all supported compliance frameworks with control counts."""
result = await db.execute(
- select(ComplianceControl.framework, func.count(ComplianceControl.id).label("cnt"))
- .group_by(ComplianceControl.framework)
+ select(ComplianceControl.framework, func.count(ComplianceControl.id).label("cnt")).group_by(ComplianceControl.framework)
)
counts = {row.framework: row.cnt for row in result}
@@ -251,9 +248,7 @@ async def get_soc2_dashboard(
db: TenantDBSession,
) -> SOC2Response:
"""Return SOC 2 evidence dashboard for the current tenant."""
- controls, ev_by_control = await _get_framework_data(
- db, current_user.tenant_id, "soc2"
- )
+ controls, ev_by_control = await _get_framework_data(db, current_user.tenant_id, "soc2")
controls_with_evidence = _build_controls_with_evidence(controls, ev_by_control)
summary = await get_soc2_summary(db, current_user.tenant_id)
@@ -285,9 +280,7 @@ async def export_soc2_evidence(
db: TenantDBSession,
) -> dict:
"""Export SOC 2 evidence as structured JSON for PDF generation."""
- controls, ev_by_control = await _get_framework_data(
- db, current_user.tenant_id, "soc2"
- )
+ controls, ev_by_control = await _get_framework_data(db, current_user.tenant_id, "soc2")
controls_with_evidence = _build_controls_with_evidence(controls, ev_by_control, max_evidence=3)
summary = await get_soc2_summary(db, current_user.tenant_id)
@@ -341,9 +334,7 @@ async def get_framework_dashboard(
"""Return compliance dashboard for the given framework."""
_validate_framework(framework)
- controls, ev_by_control = await _get_framework_data(
- db, current_user.tenant_id, framework
- )
+ controls, ev_by_control = await _get_framework_data(db, current_user.tenant_id, framework)
controls_with_evidence = _build_controls_with_evidence(controls, ev_by_control)
summary_dict = _compute_summary(controls_with_evidence)
@@ -364,9 +355,7 @@ async def get_framework_heatmap(
"""Return heatmap data (category × status counts) for the given framework."""
_validate_framework(framework)
- controls, ev_by_control = await _get_framework_data(
- db, current_user.tenant_id, framework
- )
+ controls, ev_by_control = await _get_framework_data(db, current_user.tenant_id, framework)
controls_with_evidence = _build_controls_with_evidence(controls, ev_by_control)
# Build category × status matrix
@@ -381,11 +370,7 @@ async def get_framework_heatmap(
statuses = ["approved", "collected", "review", "rejected", "missing"]
- cells = [
- HeatmapCell(category=cat, status=st, count=cell_map.get((cat, st), 0))
- for cat in categories_ordered
- for st in statuses
- ]
+ cells = [HeatmapCell(category=cat, status=st, count=cell_map.get((cat, st), 0)) for cat in categories_ordered for st in statuses]
return HeatmapResponse(
framework=framework,
@@ -405,9 +390,7 @@ async def collect_framework_evidence(
"""Trigger automatic evidence collection for the given framework."""
_validate_framework(framework)
- collected = await auto_collect_evidence(
- db, current_user.tenant_id, framework=framework
- )
+ collected = await auto_collect_evidence(db, current_user.tenant_id, framework=framework)
await db.commit()
return {
@@ -427,9 +410,7 @@ async def export_framework_evidence(
"""Export compliance evidence for the given framework as structured JSON."""
_validate_framework(framework)
- controls, ev_by_control = await _get_framework_data(
- db, current_user.tenant_id, framework
- )
+ controls, ev_by_control = await _get_framework_data(db, current_user.tenant_id, framework)
controls_with_evidence = _build_controls_with_evidence(controls, ev_by_control, max_evidence=3)
summary_dict = _compute_summary(controls_with_evidence)
diff --git a/services/api/app/api/v1/endpoints/connectors.py b/services/api/app/api/v1/endpoints/connectors.py
index 3bac15791..287a91494 100644
--- a/services/api/app/api/v1/endpoints/connectors.py
+++ b/services/api/app/api/v1/endpoints/connectors.py
@@ -1,12 +1,12 @@
"""Connector management endpoints."""
+
import uuid
from datetime import UTC, datetime
from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel
-from sqlalchemy import and_, select, update
-from sqlalchemy.ext.asyncio import AsyncSession
+from sqlalchemy import select, update
from app.api.v1.deps import AuthUser, DBSession, require_permission
from app.models.connector import Connector
@@ -56,9 +56,7 @@ async def list_connectors(
db: DBSession,
) -> list[ConnectorResponse]:
"""List all connectors for the tenant."""
- result = await db.execute(
- select(Connector).where(Connector.tenant_id == current_user.tenant_id).order_by(Connector.created_at)
- )
+ result = await db.execute(select(Connector).where(Connector.tenant_id == current_user.tenant_id).order_by(Connector.created_at))
connectors = result.scalars().all()
return [ConnectorResponse.model_validate(c) for c in connectors]
@@ -112,11 +110,7 @@ async def update_connector(
db: DBSession,
) -> ConnectorResponse:
"""Update a connector's configuration or state."""
- result = await db.execute(
- select(Connector).where(
- Connector.id == connector_id, Connector.tenant_id == current_user.tenant_id
- )
- )
+ result = await db.execute(select(Connector).where(Connector.id == connector_id, Connector.tenant_id == current_user.tenant_id))
connector = result.scalar_one_or_none()
if connector is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Connector not found")
@@ -143,11 +137,7 @@ async def delete_connector(
db: DBSession,
) -> None:
"""Delete a connector."""
- result = await db.execute(
- select(Connector).where(
- Connector.id == connector_id, Connector.tenant_id == current_user.tenant_id
- )
- )
+ result = await db.execute(select(Connector).where(Connector.id == connector_id, Connector.tenant_id == current_user.tenant_id))
connector = result.scalar_one_or_none()
if connector is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Connector not found")
@@ -162,18 +152,16 @@ async def test_connector(
db: DBSession,
) -> dict:
"""Test connectivity for a connector (stub - to be implemented per connector type)."""
- result = await db.execute(
- select(Connector).where(
- Connector.id == connector_id, Connector.tenant_id == current_user.tenant_id
- )
- )
+ result = await db.execute(select(Connector).where(Connector.id == connector_id, Connector.tenant_id == current_user.tenant_id))
connector = result.scalar_one_or_none()
if connector is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Connector not found")
# Update health check timestamp
await db.execute(
- update(Connector).where(Connector.id == connector_id).values(
+ update(Connector)
+ .where(Connector.id == connector_id)
+ .values(
last_health_check=datetime.now(UTC),
health_status="healthy",
)
diff --git a/services/api/app/api/v1/endpoints/detection_rules.py b/services/api/app/api/v1/endpoints/detection_rules.py
index abbf8caa3..02d4efafa 100644
--- a/services/api/app/api/v1/endpoints/detection_rules.py
+++ b/services/api/app/api/v1/endpoints/detection_rules.py
@@ -1,12 +1,12 @@
"""Detection rule management endpoints."""
+
import uuid
from datetime import UTC, datetime
from typing import Annotated, Any
from fastapi import APIRouter, Depends, HTTPException, Query, status
from pydantic import BaseModel, Field
-from sqlalchemy import and_, func, or_, select, update
-from sqlalchemy.ext.asyncio import AsyncSession
+from sqlalchemy import and_, or_, select, update
from app.api.v1.deps import AuthUser, DBSession, require_permission
from app.models.detection_rule import DetectionRule
@@ -76,7 +76,7 @@ async def list_rules(
filters = [
or_(
DetectionRule.tenant_id == current_user.tenant_id,
- and_(DetectionRule.tenant_id.is_(None), DetectionRule.is_builtin == True),
+ and_(DetectionRule.tenant_id.is_(None), DetectionRule.is_builtin.is_(True)),
)
]
if not include_builtin:
@@ -86,9 +86,7 @@ async def list_rules(
if rule_language:
filters.append(DetectionRule.rule_language == rule_language)
- result = await db.execute(
- select(DetectionRule).where(and_(*filters)).order_by(DetectionRule.name)
- )
+ result = await db.execute(select(DetectionRule).where(and_(*filters)).order_by(DetectionRule.name))
rules = result.scalars().all()
return [DetectionRuleResponse.model_validate(r) for r in rules]
@@ -204,11 +202,11 @@ async def delete_rule(
# ─── Rule Execution Endpoint ──────────────────────────────────────────────────
+
class ExecuteRuleRequest(BaseModel):
"""Payload for ad-hoc rule execution."""
- events: list[dict[str, Any]] = Field(
- ..., description="Events to test the rule against", max_length=1000
- )
+
+ events: list[dict[str, Any]] = Field(..., description="Events to test the rule against", max_length=1000)
class ExecuteRuleResponse(BaseModel):
@@ -290,17 +288,13 @@ async def execute_detection_rule(
# ─── Hunt Endpoint ────────────────────────────────────────────────────────────
+
class HuntRequest(BaseModel):
"""Payload for threat hunting across events."""
- rule_ids: list[uuid.UUID] | None = Field(
- None, description="Specific rule IDs to hunt with; omit to use all active rules"
- )
- rule_language: str | None = Field(
- None, description="Filter rules by language (sigma, yara, kql, eql)"
- )
- events: list[dict[str, Any]] = Field(
- ..., description="Events to hunt through", max_length=5000
- )
+
+ rule_ids: list[uuid.UUID] | None = Field(None, description="Specific rule IDs to hunt with; omit to use all active rules")
+ rule_language: str | None = Field(None, description="Filter rules by language (sigma, yara, kql, eql)")
+ events: list[dict[str, Any]] = Field(..., description="Events to hunt through", max_length=5000)
class HuntResponse(BaseModel):
@@ -332,7 +326,7 @@ async def hunt(
filters = [
or_(
DetectionRule.tenant_id == current_user.tenant_id,
- and_(DetectionRule.tenant_id.is_(None), DetectionRule.is_builtin == True),
+ and_(DetectionRule.tenant_id.is_(None), DetectionRule.is_builtin.is_(True)),
),
DetectionRule.status == "active",
]
diff --git a/services/api/app/api/v1/endpoints/graph.py b/services/api/app/api/v1/endpoints/graph.py
index d54c59af6..088ab8c59 100644
--- a/services/api/app/api/v1/endpoints/graph.py
+++ b/services/api/app/api/v1/endpoints/graph.py
@@ -2,14 +2,15 @@
Graph API endpoints: attack paths, blast radius, entity neighbors, MITRE coverage.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
-from typing import Any, Annotated
+from typing import Annotated, Any
from fastapi import APIRouter, Depends, HTTPException, Query, status
from pydantic import BaseModel, Field
-from app.api.v1.deps import get_current_user, CurrentUser
+from app.api.v1.deps import CurrentUser, get_current_user
from app.services import graph_service
router = APIRouter(prefix="/graph", tags=["graph"])
@@ -17,6 +18,7 @@ router = APIRouter(prefix="/graph", tags=["graph"])
# ─── Request / Response Schemas ───────────────────────────────────────────────
+
class GraphNode(BaseModel):
id: str
label: str
@@ -97,6 +99,7 @@ class UpsertCaseGraphRequest(BaseModel):
# ─── Endpoints ────────────────────────────────────────────────────────────────
+
@router.get(
"/attack-path/{case_id}",
response_model=AttackPathResponse,
@@ -121,7 +124,7 @@ async def get_attack_path(
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Graph query failed: {exc}",
- )
+ ) from exc
if not data["nodes"]:
raise HTTPException(
@@ -165,7 +168,7 @@ async def get_blast_radius(
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Graph query failed: {exc}",
- )
+ ) from exc
return BlastRadiusResponse(**data)
@@ -191,7 +194,7 @@ async def get_entity_neighbors(
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Graph query failed: {exc}",
- )
+ ) from exc
return EntityNeighborsResponse(**data)
@@ -213,7 +216,7 @@ async def get_mitre_coverage(
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Graph query failed: {exc}",
- )
+ ) from exc
return [
MitreCoverageItem(
@@ -228,6 +231,7 @@ async def get_mitre_coverage(
# ─── Write Endpoints ──────────────────────────────────────────────────────────
+
@router.post(
"/entities/host",
status_code=status.HTTP_201_CREATED,
diff --git a/services/api/app/api/v1/endpoints/investigations.py b/services/api/app/api/v1/endpoints/investigations.py
index 209b14d97..f6e2d94fd 100644
--- a/services/api/app/api/v1/endpoints/investigations.py
+++ b/services/api/app/api/v1/endpoints/investigations.py
@@ -19,11 +19,12 @@ Endpoints:
All endpoints respect tenant RLS via ``TenantDBSession``.
"""
+
from __future__ import annotations
import uuid
from datetime import datetime
-from typing import Annotated, Any
+from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, Query, status
from pydantic import BaseModel
@@ -193,9 +194,7 @@ async def list_runs(
limit: int = Query(default=50, ge=1, le=200),
) -> list[RunSummary]:
"""List recent investigation runs for the caller's tenant."""
- q = select(InvestigationRun).where(
- InvestigationRun.tenant_id == current_user.tenant_id
- )
+ q = select(InvestigationRun).where(InvestigationRun.tenant_id == current_user.tenant_id)
if case_id:
q = q.where(InvestigationRun.case_id == case_id)
if status_filter:
@@ -216,19 +215,9 @@ async def get_run(
"""Run summary plus count of attached events and artifacts."""
run = await _fetch_run(db, run_id, current_user.tenant_id)
- event_count = (
- await db.execute(
- select(func.count(InvestigationEvent.id)).where(
- InvestigationEvent.run_id == run_id
- )
- )
- ).scalar_one()
+ event_count = (await db.execute(select(func.count(InvestigationEvent.id)).where(InvestigationEvent.run_id == run_id))).scalar_one()
artifact_count = (
- await db.execute(
- select(func.count(InvestigationArtifact.id)).where(
- InvestigationArtifact.run_id == run_id
- )
- )
+ await db.execute(select(func.count(InvestigationArtifact.id)).where(InvestigationArtifact.run_id == run_id))
).scalar_one()
base = _run_to_summary(run)
@@ -297,12 +286,7 @@ async def replay_run(
bound should fall back to the paginated ``/events`` endpoint.
"""
await _fetch_run(db, run_id, current_user.tenant_id)
- q = (
- select(InvestigationEvent)
- .where(InvestigationEvent.run_id == run_id)
- .order_by(InvestigationEvent.seq.asc())
- .limit(max_events)
- )
+ q = select(InvestigationEvent).where(InvestigationEvent.run_id == run_id).order_by(InvestigationEvent.seq.asc()).limit(max_events)
events = (await db.execute(q)).scalars().all()
return [_event_to_out(e) for e in events]
@@ -399,11 +383,7 @@ async def list_artifacts(
db: TenantDBSession,
) -> list[ArtifactSummary]:
await _fetch_run(db, run_id, current_user.tenant_id)
- q = (
- select(InvestigationArtifact)
- .where(InvestigationArtifact.run_id == run_id)
- .order_by(InvestigationArtifact.created_at.asc())
- )
+ q = select(InvestigationArtifact).where(InvestigationArtifact.run_id == run_id).order_by(InvestigationArtifact.created_at.asc())
arts = (await db.execute(q)).scalars().all()
return [
ArtifactSummary(
diff --git a/services/api/app/api/v1/endpoints/oncall.py b/services/api/app/api/v1/endpoints/oncall.py
index d754b5040..fb138dc87 100644
--- a/services/api/app/api/v1/endpoints/oncall.py
+++ b/services/api/app/api/v1/endpoints/oncall.py
@@ -9,13 +9,14 @@ is the *current* snapshot per user so:
* push fan-out can target a specific user instead of an entire topic,
* the agent can respect "do not disturb" when an analyst snoozes.
"""
+
from __future__ import annotations
import uuid
from datetime import UTC, datetime, timedelta
-from typing import Annotated, Literal
+from typing import Literal
-from fastapi import APIRouter, Depends, HTTPException, Query, status
+from fastapi import APIRouter, HTTPException, Query, status
from pydantic import BaseModel, Field
from sqlalchemy import select
@@ -69,11 +70,7 @@ async def list_oncall(
``busy`` (so the next-best person is one tap away), and finally everyone
else for context.
"""
- stmt = (
- select(OnCallStatus, User)
- .join(User, User.id == OnCallStatus.user_id)
- .where(OnCallStatus.tenant_id == user.tenant_id)
- )
+ stmt = select(OnCallStatus, User).join(User, User.id == OnCallStatus.user_id).where(OnCallStatus.tenant_id == user.tenant_id)
if status_filter:
if status_filter not in _STATUS_VALUES:
raise HTTPException(
diff --git a/services/api/app/api/v1/endpoints/passkeys.py b/services/api/app/api/v1/endpoints/passkeys.py
index c31957511..fe2f25bba 100644
--- a/services/api/app/api/v1/endpoints/passkeys.py
+++ b/services/api/app/api/v1/endpoints/passkeys.py
@@ -30,6 +30,7 @@ Security notes
* On success we issue the same JWT pair as ``/auth/login`` so the rest of
the API surface is unaware of the auth method.
"""
+
from __future__ import annotations
import base64
@@ -45,8 +46,8 @@ from sqlalchemy import and_, select, update
from app.api.v1.deps import AuthUser, DBSession
from app.core.config import settings
-from app.db.rls import TenantDBSession
from app.core.security import create_access_token, create_refresh_token
+from app.db.rls import TenantDBSession
from app.models.responder import PasskeyChallenge, PasskeyCredential
from app.models.tenant import User
@@ -137,10 +138,7 @@ def _import_webauthn() -> Any:
except ImportError as exc: # pragma: no cover - exercised in CI when missing
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
- detail=(
- "Passkey (WebAuthn) support is not installed on this server. "
- "Install the `webauthn` Python package."
- ),
+ detail=("Passkey (WebAuthn) support is not installed on this server. Install the `webauthn` Python package."),
) from exc
@@ -205,21 +203,21 @@ async def passkey_register_begin(
# Pull existing credential IDs so the browser hides them in the picker.
existing_rows = (
- await db.execute(
- select(PasskeyCredential.credential_id).where(
- PasskeyCredential.user_id == user.user_id,
- PasskeyCredential.revoked_at.is_(None),
+ (
+ await db.execute(
+ select(PasskeyCredential.credential_id).where(
+ PasskeyCredential.user_id == user.user_id,
+ PasskeyCredential.revoked_at.is_(None),
+ )
)
)
- ).scalars().all()
+ .scalars()
+ .all()
+ )
- user_row = (
- await db.execute(select(User).where(User.id == user.user_id))
- ).scalar_one_or_none()
+ user_row = (await db.execute(select(User).where(User.id == user.user_id))).scalar_one_or_none()
if user_row is None:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
- )
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
options = webauthn.generate_registration_options(
rp_id=settings.PASSKEY_RP_ID,
@@ -227,9 +225,7 @@ async def passkey_register_begin(
user_id=str(user.user_id).encode("utf-8"),
user_name=user_row.email,
user_display_name=user_row.username or user_row.email,
- exclude_credentials=[
- {"id": _b64url_decode(cid), "type": "public-key"} for cid in existing_rows
- ],
+ exclude_credentials=[{"id": _b64url_decode(cid), "type": "public-key"} for cid in existing_rows],
)
options_json = json.loads(webauthn.options_to_json(options))
@@ -261,9 +257,7 @@ async def passkey_register_finish(
"""Verify the attestation response and persist the new credential."""
webauthn, _ = _import_webauthn()
- challenge_row = await _consume_challenge(
- db, encoded=body.challenge, purpose="register"
- )
+ challenge_row = await _consume_challenge(db, encoded=body.challenge, purpose="register")
if challenge_row.user_id != user.user_id:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
@@ -339,13 +333,17 @@ async def passkey_authenticate_begin(
if user_row is not None:
target_user = user_row
cred_rows = (
- await db.execute(
- select(PasskeyCredential).where(
- PasskeyCredential.user_id == user_row.id,
- PasskeyCredential.revoked_at.is_(None),
+ (
+ await db.execute(
+ select(PasskeyCredential).where(
+ PasskeyCredential.user_id == user_row.id,
+ PasskeyCredential.revoked_at.is_(None),
+ )
)
)
- ).scalars().all()
+ .scalars()
+ .all()
+ )
allow_credentials = [
{
"id": _b64url_decode(c.credential_id),
@@ -413,9 +411,7 @@ async def passkey_authenticate_finish(
)
).scalar_one_or_none()
if user_row is None:
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN, detail="User is inactive"
- )
+ raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="User is inactive")
try:
verified = webauthn.verify_authentication_response(
@@ -436,9 +432,7 @@ async def passkey_authenticate_finish(
cred_row.sign_count = int(verified.new_sign_count)
cred_row.last_used_at = _now()
- await db.execute(
- update(User).where(User.id == user_row.id).values(last_login=_now())
- )
+ await db.execute(update(User).where(User.id == user_row.id).values(last_login=_now()))
await db.commit()
token_data = {
@@ -465,25 +459,25 @@ async def list_my_credentials(
) -> CredentialsListResponse:
"""List the current user's active passkeys."""
rows = (
- await db.execute(
- select(PasskeyCredential)
- .where(
- and_(
- PasskeyCredential.user_id == user.user_id,
- PasskeyCredential.revoked_at.is_(None),
+ (
+ await db.execute(
+ select(PasskeyCredential)
+ .where(
+ and_(
+ PasskeyCredential.user_id == user.user_id,
+ PasskeyCredential.revoked_at.is_(None),
+ )
)
+ .order_by(PasskeyCredential.created_at.desc())
)
- .order_by(PasskeyCredential.created_at.desc())
)
- ).scalars().all()
- return CredentialsListResponse(
- items=[PasskeyCredentialOut.model_validate(r) for r in rows]
+ .scalars()
+ .all()
)
+ return CredentialsListResponse(items=[PasskeyCredentialOut.model_validate(r) for r in rows])
- "/credentials/{credential_id}", status_code=status.HTTP_204_NO_CONTENT
-)
[email protected]("/credentials/{credential_id}", status_code=status.HTTP_204_NO_CONTENT)
async def revoke_credential(
credential_id: uuid.UUID,
user: AuthUser,
diff --git a/services/api/app/api/v1/endpoints/playbooks.py b/services/api/app/api/v1/endpoints/playbooks.py
index 6992d43b0..52b7926ea 100644
--- a/services/api/app/api/v1/endpoints/playbooks.py
+++ b/services/api/app/api/v1/endpoints/playbooks.py
@@ -5,6 +5,7 @@ The api service acts as a gateway that forwards playbook CRUD and run
requests to the agents service. This keeps the public API contract in
one place while the engine lives in services/agents.
"""
+
from __future__ import annotations
import os
@@ -12,7 +13,6 @@ from typing import Any
import httpx
from fastapi import APIRouter, HTTPException, Request
-from fastapi.responses import JSONResponse
_AGENTS_URL = os.getenv("AGENTS_SERVICE_URL", "http://agents:8000")
@@ -38,6 +38,7 @@ async def _proxy(method: str, path: str, **kwargs) -> Any:
# CRUD
# ---------------------------------------------------------------------------
+
@router.get("", summary="List playbooks")
async def list_playbooks(enabled_only: bool = False):
return await _proxy("GET", "", params={"enabled_only": enabled_only})
diff --git a/services/api/app/api/v1/endpoints/plugins.py b/services/api/app/api/v1/endpoints/plugins.py
index b4f2115db..bd21306d4 100644
--- a/services/api/app/api/v1/endpoints/plugins.py
+++ b/services/api/app/api/v1/endpoints/plugins.py
@@ -12,6 +12,7 @@ POST /plugins/discover – re-scan AISOC_PLUGINS_DIR
MIT License — AiSOC (open-source AI Security Operations Center)
"""
+
from __future__ import annotations
from typing import Annotated, Any
@@ -19,12 +20,12 @@ from typing import Annotated, Any
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel
-from app.api.v1.deps import AuthUser, CurrentUser, require_permission
+from app.api.v1.deps import AuthUser, require_permission
from app.services.plugin_manager import (
LoadedPlugin,
PluginError,
- get_plugin_manager,
PluginManager,
+ get_plugin_manager,
)
router = APIRouter(prefix="/plugins", tags=["plugins"])
@@ -32,6 +33,7 @@ router = APIRouter(prefix="/plugins", tags=["plugins"])
# ── Response schemas ──────────────────────────────────────────────────────────
+
class PluginManifestOut(BaseModel):
id: str
name: str
@@ -91,6 +93,7 @@ def _mgr() -> PluginManager:
# ── Endpoints ─────────────────────────────────────────────────────────────────
+
@router.get("", response_model=list[PluginOut])
async def list_plugins(
plugin_type: str | None = None,
diff --git a/services/api/app/api/v1/endpoints/push.py b/services/api/app/api/v1/endpoints/push.py
index 088c91ac8..2360205f8 100644
--- a/services/api/app/api/v1/endpoints/push.py
+++ b/services/api/app/api/v1/endpoints/push.py
@@ -19,13 +19,14 @@ to ``REALTIME_BASE_URL``. The gateway:
* Forwards the JSON body unchanged so we don't have to keep two schemas in
sync.
"""
+
from __future__ import annotations
import logging
from typing import Annotated, Any
import httpx
-from fastapi import APIRouter, Body, Depends, HTTPException, status
+from fastapi import APIRouter, Body, HTTPException, status
from app.api.v1.deps import AuthUser
from app.core.config import settings
@@ -119,7 +120,7 @@ async def unsubscribe(
@router.post("/test")
async def test_notify(
user: AuthUser,
- body: Annotated[dict[str, Any] | None, Body(default=None)] = None,
+ body: Annotated[dict[str, Any] | None, Body()] = None,
) -> dict[str, Any]:
"""Send a test notification to the authenticated user's devices.
diff --git a/services/api/app/api/v1/endpoints/rbac.py b/services/api/app/api/v1/endpoints/rbac.py
index 7151b4749..5bf961bfb 100644
--- a/services/api/app/api/v1/endpoints/rbac.py
+++ b/services/api/app/api/v1/endpoints/rbac.py
@@ -7,8 +7,9 @@ Requires:
- ``roles:write`` to create / update / delete roles and assign users
- ``users:write`` to assign / revoke roles from users
"""
+
import uuid
-from typing import Annotated, Any
+from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel
@@ -114,14 +115,16 @@ async def list_roles(
out: list[RoleOut] = []
for role in roles:
perms = [PermissionOut.model_validate(rp.permission) for rp in role.role_permissions]
- out.append(RoleOut(
- id=role.id,
- tenant_id=role.tenant_id,
- name=role.name,
- description=role.description,
- is_system=role.is_system,
- permissions=perms,
- ))
+ out.append(
+ RoleOut(
+ id=role.id,
+ tenant_id=role.tenant_id,
+ name=role.name,
+ description=role.description,
+ is_system=role.is_system,
+ permissions=perms,
+ )
+ )
return out
@@ -133,9 +136,7 @@ async def create_role(
) -> RoleOut:
"""Create a custom role for the current tenant."""
# Check name uniqueness
- existing = await db.execute(
- select(Role).where(Role.tenant_id == current_user.tenant_id, Role.name == body.name)
- )
+ existing = await db.execute(select(Role).where(Role.tenant_id == current_user.tenant_id, Role.name == body.name))
if existing.scalar_one_or_none() is not None:
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=f"Role '{body.name}' already exists")
@@ -272,16 +273,12 @@ async def assign_role(
role = await _get_role_or_404(db, body.role_id, current_user.tenant_id)
# Ensure the target user belongs to this tenant
- user_res = await db.execute(
- select(User).where(User.id == user_id, User.tenant_id == current_user.tenant_id)
- )
+ user_res = await db.execute(select(User).where(User.id == user_id, User.tenant_id == current_user.tenant_id))
if user_res.scalar_one_or_none() is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found in tenant")
# Upsert
- existing = await db.execute(
- select(UserRole).where(UserRole.user_id == user_id, UserRole.role_id == role.id)
- )
+ existing = await db.execute(select(UserRole).where(UserRole.user_id == user_id, UserRole.role_id == role.id))
if existing.scalar_one_or_none() is not None:
return UserRoleOut(user_id=user_id, role_id=role.id, role_name=role.name)
@@ -299,9 +296,7 @@ async def revoke_role(
db: TenantDBSession,
) -> None:
"""Revoke a role from a user."""
- await db.execute(
- delete(UserRole).where(UserRole.user_id == user_id, UserRole.role_id == role_id)
- )
+ await db.execute(delete(UserRole).where(UserRole.user_id == user_id, UserRole.role_id == role_id))
await db.commit()
@@ -311,9 +306,7 @@ async def revoke_role(
async def _get_role_or_404(db: AsyncSession, role_id: uuid.UUID, tenant_id: uuid.UUID) -> Role:
- result = await db.execute(
- select(Role).where(Role.id == role_id, Role.tenant_id == tenant_id)
- )
+ result = await db.execute(select(Role).where(Role.id == role_id, Role.tenant_id == tenant_id))
role = result.scalar_one_or_none()
if role is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Role not found")
diff --git a/services/api/app/api/v1/endpoints/sla.py b/services/api/app/api/v1/endpoints/sla.py
index 68baf6f03..0a407e5d5 100644
--- a/services/api/app/api/v1/endpoints/sla.py
+++ b/services/api/app/api/v1/endpoints/sla.py
@@ -31,6 +31,7 @@ VALID_EVENT_TYPES = {"detected", "acknowledged", "resolved", "closed"}
# Pydantic schemas
# ---------------------------------------------------------------------------
+
class SLAConfigOut(BaseModel):
id: uuid.UUID
severity: str
@@ -73,6 +74,7 @@ class SLAEventOut(BaseModel):
# Endpoints
# ---------------------------------------------------------------------------
+
@router.get("/metrics")
async def get_sla_metrics(
current_user: Annotated[AuthUser, Depends(require_permission("sla:read"))],
@@ -89,11 +91,7 @@ async def get_sla_config(
db: TenantDBSession,
) -> list[SLAConfigOut]:
"""Return per-severity SLA configuration for the tenant."""
- rows = await db.execute(
- select(TenantSLAConfig).where(
- TenantSLAConfig.tenant_id == current_user.tenant_id
- )
- )
+ rows = await db.execute(select(TenantSLAConfig).where(TenantSLAConfig.tenant_id == current_user.tenant_id))
return rows.scalars().all() # type: ignore[return-value]
@@ -161,7 +159,9 @@ async def record_sla_event(
event_type=body.event_type,
occurred_at=body.occurred_at or datetime.utcnow(),
actor_id=current_user.id,
- metadata=body.metadata,
+ # ORM attr is `metadata_` (DB column is still `metadata`) — `metadata`
+ # itself is reserved on Declarative `Base`.
+ metadata_=body.metadata,
)
db.add(event)
await db.commit()
diff --git a/services/api/app/api/v1/endpoints/tenants.py b/services/api/app/api/v1/endpoints/tenants.py
index 3a562c1c8..48ac2919d 100644
--- a/services/api/app/api/v1/endpoints/tenants.py
+++ b/services/api/app/api/v1/endpoints/tenants.py
@@ -1,4 +1,5 @@
"""Tenant and user management endpoints."""
+
import uuid
from datetime import UTC, datetime
from typing import Annotated
@@ -6,7 +7,6 @@ from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel, EmailStr
from sqlalchemy import select, update
-from sqlalchemy.ext.asyncio import AsyncSession
from app.api.v1.deps import AuthUser, DBSession, require_permission
from app.core.security import get_password_hash
@@ -79,7 +79,9 @@ async def update_tenant_settings(
) -> TenantResponse:
"""Update tenant settings."""
await db.execute(
- update(Tenant).where(Tenant.id == current_user.tenant_id).values(
+ update(Tenant)
+ .where(Tenant.id == current_user.tenant_id)
+ .values(
settings=request.settings,
updated_at=datetime.now(UTC),
)
@@ -96,9 +98,7 @@ async def list_users(
db: DBSession,
) -> list[UserResponse]:
"""List all users in the current tenant."""
- result = await db.execute(
- select(User).where(User.tenant_id == current_user.tenant_id).order_by(User.created_at)
- )
+ result = await db.execute(select(User).where(User.tenant_id == current_user.tenant_id).order_by(User.created_at))
users = result.scalars().all()
return [UserResponse.model_validate(u) for u in users]
@@ -139,9 +139,7 @@ async def update_user(
db: DBSession,
) -> UserResponse:
"""Update a user."""
- result = await db.execute(
- select(User).where(User.id == user_id, User.tenant_id == current_user.tenant_id)
- )
+ result = await db.execute(select(User).where(User.id == user_id, User.tenant_id == current_user.tenant_id))
user = result.scalar_one_or_none()
if user is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
diff --git a/services/api/app/api/v1/router.py b/services/api/app/api/v1/router.py
index bddef8fbc..3722fa16a 100644
--- a/services/api/app/api/v1/router.py
+++ b/services/api/app/api/v1/router.py
@@ -1,4 +1,5 @@
"""API v1 router aggregating all endpoint modules."""
+
from fastapi import APIRouter
from app.api.v1.endpoints import (
diff --git a/services/api/app/auth/oidc.py b/services/api/app/auth/oidc.py
index f7b307718..e0fcc3870 100644
--- a/services/api/app/auth/oidc.py
+++ b/services/api/app/auth/oidc.py
@@ -22,13 +22,12 @@ Configuration (env vars):
from __future__ import annotations
import hashlib
-import hmac
import logging
import os
import secrets
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta
from typing import Any
-from urllib.parse import urlencode, urljoin
+from urllib.parse import urlencode
import httpx
import jwt as _jwt
@@ -49,8 +48,8 @@ _JWT_EXPIRE_MINUTES = int(os.getenv("JWT_EXPIRE_MINUTES", "480"))
def _issue_jwt(claims: dict[str, Any]) -> str:
payload = {
**claims,
- "iat": datetime.now(timezone.utc),
- "exp": datetime.now(timezone.utc) + timedelta(minutes=_JWT_EXPIRE_MINUTES),
+ "iat": datetime.now(UTC),
+ "exp": datetime.now(UTC) + timedelta(minutes=_JWT_EXPIRE_MINUTES),
}
return _jwt.encode(payload, _JWT_SECRET, algorithm=_JWT_ALGORITHM)
@@ -90,6 +89,7 @@ def _pop_state(state: str) -> dict[str, str] | None:
# ─── Routes ───────────────────────────────────────────────────────────────────
+
@router.get("/login")
async def oidc_login(request: Request, redirect: str = "/") -> Response:
"""Initiate OIDC authorization code flow."""
@@ -129,6 +129,7 @@ async def oidc_login(request: Request, redirect: str = "/") -> Response:
verifier = secrets.token_urlsafe(64)
challenge = hashlib.sha256(verifier.encode()).digest()
import base64
+
challenge_b64 = base64.urlsafe_b64encode(challenge).rstrip(b"=").decode()
params["code_challenge"] = challenge_b64
params["code_challenge_method"] = "S256"
@@ -211,19 +212,20 @@ async def oidc_callback(
userinfo = ui_resp.json()
merged = {**claims, **userinfo}
- token = _issue_jwt({
- "sub": merged.get("sub", ""),
- "email": merged.get("email", ""),
- "name": merged.get("name", ""),
- "picture": merged.get("picture", ""),
- "provider": "oidc",
- "iss_upstream": issuer,
- })
+ token = _issue_jwt(
+ {
+ "sub": merged.get("sub", ""),
+ "email": merged.get("email", ""),
+ "name": merged.get("name", ""),
+ "picture": merged.get("picture", ""),
+ "provider": "oidc",
+ "iss_upstream": issuer,
+ }
+ )
redirect_url = state_data.get("redirect", "/")
response = RedirectResponse(url=redirect_url, status_code=302)
- response.set_cookie("aisoc_token", token, httponly=True, samesite="lax",
- secure=request.url.scheme == "https")
+ response.set_cookie("aisoc_token", token, httponly=True, samesite="lax", secure=request.url.scheme == "https")
response.delete_cookie("oidc_state")
return response
@@ -245,13 +247,15 @@ async def oidc_userinfo(request: Request) -> JSONResponse:
except _jwt.PyJWTError as exc:
raise HTTPException(status_code=401, detail=f"Invalid token: {exc}") from exc
- return JSONResponse({
- "sub": claims.get("sub"),
- "email": claims.get("email"),
- "name": claims.get("name"),
- "picture": claims.get("picture"),
- "provider": claims.get("provider"),
- })
+ return JSONResponse(
+ {
+ "sub": claims.get("sub"),
+ "email": claims.get("email"),
+ "name": claims.get("name"),
+ "picture": claims.get("picture"),
+ "provider": claims.get("provider"),
+ }
+ )
@router.get("/logout")
diff --git a/services/api/app/auth/saml.py b/services/api/app/auth/saml.py
index 61aa504d9..f3b68f9ea 100644
--- a/services/api/app/auth/saml.py
+++ b/services/api/app/auth/saml.py
@@ -29,11 +29,11 @@ from __future__ import annotations
import logging
import os
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta
from typing import Any
import jwt as _jwt
-from fastapi import APIRouter, Form, HTTPException, Request
+from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import HTMLResponse, RedirectResponse, Response
logger = logging.getLogger(__name__)
@@ -50,14 +50,15 @@ _JWT_EXPIRE_MINUTES = int(os.getenv("JWT_EXPIRE_MINUTES", "480"))
def _issue_jwt(claims: dict[str, Any]) -> str:
payload = {
**claims,
- "iat": datetime.now(timezone.utc),
- "exp": datetime.now(timezone.utc) + timedelta(minutes=_JWT_EXPIRE_MINUTES),
+ "iat": datetime.now(UTC),
+ "exp": datetime.now(UTC) + timedelta(minutes=_JWT_EXPIRE_MINUTES),
}
return _jwt.encode(payload, _JWT_SECRET, algorithm=_JWT_ALGORITHM)
# ─── SAML settings builder ────────────────────────────────────────────────────
+
def _saml_settings() -> dict[str, Any]:
sp_acs = os.getenv("SAML_SP_ACS_URL", "http://localhost:8000/auth/saml/acs")
sp_entity = os.getenv("SAML_SP_ENTITY_ID", sp_acs)
@@ -102,6 +103,7 @@ def _saml_settings() -> dict[str, Any]:
# ─── Routes ───────────────────────────────────────────────────────────────────
+
@router.get("/login")
async def saml_login(request: Request, redirect: str = "/") -> Response:
"""Initiate SAML SSO — redirect to IdP."""
@@ -143,12 +145,16 @@ async def saml_acs(request: Request) -> Response:
attrs = auth.get_attributes()
name_id = auth.get_nameid()
- token = _issue_jwt({
- "sub": name_id,
- "email": _first(attrs.get("email") or attrs.get("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", [name_id])),
- "name": _first(attrs.get("displayName") or attrs.get("http://schemas.microsoft.com/identity/claims/displayname", [])),
- "provider": "saml",
- })
+ email_claim = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
+ name_claim = "http://schemas.microsoft.com/identity/claims/displayname"
+ token = _issue_jwt(
+ {
+ "sub": name_id,
+ "email": _first(attrs.get("email") or attrs.get(email_claim, [name_id])),
+ "name": _first(attrs.get("displayName") or attrs.get(name_claim, [])),
+ "provider": "saml",
+ }
+ )
relay_state = (await request.form()).get("RelayState", "/")
response = RedirectResponse(url=str(relay_state), status_code=302)
@@ -208,6 +214,7 @@ async def saml_logout(request: Request) -> Response:
# ─── Helpers ──────────────────────────────────────────────────────────────────
+
def _first(lst: list[str]) -> str:
return lst[0] if lst else ""
diff --git a/services/api/app/core/config.py b/services/api/app/core/config.py
index 3b5fa9bbb..b31186c83 100644
--- a/services/api/app/core/config.py
+++ b/services/api/app/core/config.py
@@ -3,12 +3,28 @@ AiSOC API Configuration
AiSOC — open-source AI Security Operations Center
MIT License
"""
+
+import logging
+import warnings
from functools import lru_cache
-from typing import Annotated, Any
+from typing import Any
-from pydantic import AnyHttpUrl, PostgresDsn, RedisDsn, field_validator
+from pydantic import PostgresDsn, RedisDsn, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
+# Default placeholders shipped in source. Anything matching these in a
+# non-development environment triggers a hard startup warning (see
+# ``warn_if_insecure_defaults`` below). Exposed as a constant so tests
+# and infra can reference the same canonical list.
+INSECURE_SECRET_KEY_DEFAULTS: frozenset[str] = frozenset(
+ {
+ "change-me-in-production-at-least-32-chars",
+ "dev_secret_key_change_in_production",
+ "changeme",
+ "secret",
+ }
+)
+
class Settings(BaseSettings):
model_config = SettingsConfigDict(
@@ -23,7 +39,7 @@ class Settings(BaseSettings):
APP_VERSION: str = "0.1.0"
ENV: str = "development"
ENVIRONMENT: str = "development" # alias for ENV
- VERSION: str = "0.1.0" # alias for APP_VERSION
+ VERSION: str = "0.1.0" # alias for APP_VERSION
DEBUG: bool = False
API_PREFIX: str = "/api/v1"
@@ -33,6 +49,13 @@ class Settings(BaseSettings):
REFRESH_TOKEN_EXPIRE_DAYS: int = 7
ALGORITHM: str = "HS256"
+ # Bearer token required to scrape ``/metrics`` outside development.
+ # Empty string disables the gate (development default). When set, the
+ # endpoint requires ``Authorization: Bearer <METRICS_TOKEN>`` and
+ # otherwise returns 401. Prefer a long random hex string mounted as
+ # a kubernetes secret / fly.io secret in production.
+ METRICS_TOKEN: str = ""
+
# Database
DATABASE_URL: PostgresDsn = "postgresql+asyncpg://aisoc:aisoc@localhost:5432/aisoc"
DATABASE_POOL_SIZE: int = 20
@@ -50,6 +73,10 @@ class Settings(BaseSettings):
CLICKHOUSE_PASSWORD: str = ""
# Kafka
+ # Canonical name across the stack is ``KAFKA_BOOTSTRAP_SERVERS`` (see
+ # ``.env.example`` and the docker-compose files). ``KAFKA_BROKERS`` is
+ # kept as a backward-compatible alias for older deployments.
+ KAFKA_BOOTSTRAP_SERVERS: str = "localhost:9092"
KAFKA_BROKERS: str = "localhost:9092"
KAFKA_TOPIC_EVENTS: str = "aisoc.normalized_events"
KAFKA_TOPIC_ALERTS: str = "aisoc.alerts"
@@ -79,6 +106,21 @@ class Settings(BaseSettings):
# Plugin system
AISOC_PLUGINS_DIR: str = "/opt/aisoc/plugins"
+ # Plugin signature trust gate.
+ # strict – signed-and-verified manifests are required; unsigned or
+ # invalid plugins are refused. Default in production.
+ # warn – unsigned/invalid plugins still load, but a structured
+ # warning log is emitted and the plugin record carries
+ # ``signature_status="warn"``. Useful while bootstrapping
+ # a key-rotation workflow.
+ # disabled – signature checks are skipped entirely. Only sane in a
+ # fully isolated dev sandbox.
+ # Public keys live in ``PLUGIN_TRUSTED_KEYS_DIR`` as PEM files; any
+ # plugin whose ``manifest.signature`` verifies under one of them is
+ # considered trusted.
+ PLUGIN_TRUST_MODE: str = "strict"
+ PLUGIN_TRUSTED_KEYS_DIR: str = "/opt/aisoc/plugin-keys"
+
# Mobile responder PWA
# Web push runs in the realtime service; this base URL is used by the API
# gateway proxy at /api/v1/push/* so the frontend never has to know the
@@ -110,9 +152,7 @@ class Settings(BaseSettings):
# demo tenant with 403, surfaces a banner, and pre-seeds canonical data.
AISOC_DEMO_MODE: bool = False
AISOC_DEMO_TENANT: str = "demo"
- AISOC_DEMO_BANNER: str = (
- "Demo data resets daily at 00:00 UTC. All write actions are disabled."
- )
+ AISOC_DEMO_BANNER: str = "Demo data resets daily at 00:00 UTC. All write actions are disabled."
# Optional services (toggle off for the lean Fly.io demo).
# When true the corresponding subsystem skips connection setup at boot
@@ -136,4 +176,45 @@ def get_settings() -> Settings:
return Settings()
+def _is_dev_env(env: str) -> bool:
+ return (env or "").strip().lower() in {"development", "dev", "local", "demo", "test"}
+
+
+def warn_if_insecure_defaults(s: Settings | None = None) -> list[str]:
+ """Emit a structured warning for each insecure default still in place.
+
+ Returns the list of warning messages emitted. Called from
+ ``app.main`` during startup so operators see the warnings in the
+ very first lines of the API container's stdout — same place they'd
+ look for a panic.
+
+ The list is also returned so a /health/secrets endpoint (or a
+ deploy-time CI check) can assert on it without re-implementing the
+ rule. We deliberately ``warnings.warn`` rather than ``logger.error``
+ so test suites can assert on the warning category.
+ """
+ s = s or settings
+ msgs: list[str] = []
+
+ if s.SECRET_KEY in INSECURE_SECRET_KEY_DEFAULTS:
+ msgs.append("SECRET_KEY is set to a known insecure placeholder; rotate before exposing this instance to the network.")
+
+ # /metrics auth: outside dev we expect a non-empty token.
+ if not _is_dev_env(s.ENVIRONMENT) and not s.METRICS_TOKEN:
+ msgs.append("METRICS_TOKEN is empty in a non-development environment — /metrics is currently unauthenticated.")
+
+ # Plugin trust mode: never silently default to disabled in prod.
+ if not _is_dev_env(s.ENVIRONMENT) and s.PLUGIN_TRUST_MODE.lower() == "disabled":
+ msgs.append("PLUGIN_TRUST_MODE=disabled outside development — plugins will load without signature verification.")
+
+ logger = logging.getLogger("aisoc.config")
+ for msg in msgs:
+ # ``stacklevel=2`` so the warning points at the caller of
+ # ``warn_if_insecure_defaults`` rather than this helper.
+ warnings.warn(msg, RuntimeWarning, stacklevel=2)
+ logger.warning("insecure_default: %s", msg)
+
+ return msgs
+
+
settings = get_settings()
diff --git a/services/api/app/core/logging.py b/services/api/app/core/logging.py
index 86de49fa3..fb290a380 100644
--- a/services/api/app/core/logging.py
+++ b/services/api/app/core/logging.py
@@ -1,4 +1,5 @@
"""Structured logging configuration using structlog."""
+
import logging
import sys
diff --git a/services/api/app/core/security.py b/services/api/app/core/security.py
index 15cd3e028..426e2d064 100644
--- a/services/api/app/core/security.py
+++ b/services/api/app/core/security.py
@@ -1,12 +1,13 @@
"""
Security utilities: JWT tokens, password hashing, RBAC, API key generation
"""
+
import hashlib
import secrets
from datetime import UTC, datetime, timedelta
from typing import Any
-from jose import JWTError, jwt
+from jose import jwt
from passlib.context import CryptContext
from app.core.config import settings
@@ -15,40 +16,70 @@ pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
ROLE_PERMISSIONS: dict[str, list[str]] = {
"platform_admin": ["*"],
+ # ``admin`` is the role string handed out by the dev-mode demo user
+ # (see ``app.api.v1.dev_auth``) and by some legacy seed scripts. It
+ # must resolve to the same privileges as ``platform_admin`` so that
+ # ``require_permission(...)`` does not silently deny while
+ # identity-only deps silently allow — that inconsistency was the
+ # source of the P0.3 audit finding.
+ "admin": ["*"],
"tenant_admin": [
- "alerts:read", "alerts:write", "alerts:delete",
- "cases:read", "cases:write", "cases:delete",
- "playbooks:read", "playbooks:write", "playbooks:execute",
- "connectors:read", "connectors:write", "connectors:delete",
- "users:read", "users:write",
- "rules:read", "rules:write",
- "reports:read", "reports:write",
+ "alerts:read",
+ "alerts:write",
+ "alerts:delete",
+ "cases:read",
+ "cases:write",
+ "cases:delete",
+ "playbooks:read",
+ "playbooks:write",
+ "playbooks:execute",
+ "connectors:read",
+ "connectors:write",
+ "connectors:delete",
+ "users:read",
+ "users:write",
+ "rules:read",
+ "rules:write",
+ "reports:read",
+ "reports:write",
"threat_intel:read",
- "settings:read", "settings:write",
+ "settings:read",
+ "settings:write",
],
"soc_lead": [
- "alerts:read", "alerts:write",
- "cases:read", "cases:write",
- "playbooks:read", "playbooks:execute",
+ "alerts:read",
+ "alerts:write",
+ "cases:read",
+ "cases:write",
+ "playbooks:read",
+ "playbooks:execute",
"connectors:read",
"users:read",
- "rules:read", "rules:write",
- "reports:read", "reports:write",
+ "rules:read",
+ "rules:write",
+ "reports:read",
+ "reports:write",
"threat_intel:read",
],
"soc_analyst": [
- "alerts:read", "alerts:write",
- "cases:read", "cases:write",
- "playbooks:read", "playbooks:execute",
+ "alerts:read",
+ "alerts:write",
+ "cases:read",
+ "cases:write",
+ "playbooks:read",
+ "playbooks:execute",
"connectors:read",
"threat_intel:read",
"reports:read",
],
"threat_hunter": [
"alerts:read",
- "cases:read", "cases:write",
- "threat_intel:read", "threat_intel:write",
- "rules:read", "rules:write",
+ "cases:read",
+ "cases:write",
+ "threat_intel:read",
+ "threat_intel:write",
+ "rules:read",
+ "rules:write",
"reports:read",
],
"viewer": [
@@ -58,8 +89,10 @@ ROLE_PERMISSIONS: dict[str, list[str]] = {
"threat_intel:read",
],
"api_service": [
- "alerts:read", "alerts:write",
- "cases:read", "cases:write",
+ "alerts:read",
+ "alerts:write",
+ "cases:read",
+ "cases:write",
"threat_intel:read",
],
}
@@ -103,9 +136,9 @@ def generate_api_key() -> tuple[str, str, str]:
- prefix – first 12 chars for display / lookup (e.g. ``aisoc_abc123``)
- hashed_key – SHA-256 hex digest stored in the DB
"""
- token = secrets.token_hex(24) # 48 hex chars = 192 bits entropy
+ token = secrets.token_hex(24) # 48 hex chars = 192 bits entropy
raw_key = f"aisoc_{token}"
- prefix = raw_key[:12] # "aisoc_" + first 6 hex chars
+ prefix = raw_key[:12] # "aisoc_" + first 6 hex chars
hashed_key = hashlib.sha256(raw_key.encode()).hexdigest()
return raw_key, prefix, hashed_key
@@ -118,9 +151,9 @@ def hash_api_key(raw_key: str) -> str:
def verify_ed25519_signature(public_key_bytes: bytes, message: bytes, signature: bytes) -> None:
"""Verify an Ed25519 signature. Raises ValueError on failure."""
try:
+ from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
from cryptography.hazmat.primitives.serialization import load_pem_public_key
- from cryptography.exceptions import InvalidSignature
pub_key = load_pem_public_key(public_key_bytes)
if not isinstance(pub_key, Ed25519PublicKey):
diff --git a/services/api/app/core/telemetry.py b/services/api/app/core/telemetry.py
index 5eb52efd0..703d87004 100644
--- a/services/api/app/core/telemetry.py
+++ b/services/api/app/core/telemetry.py
@@ -21,13 +21,14 @@ Usage:
with tracer.start_as_current_span("my-op") as span:
span.set_attribute("case.id", case_id)
"""
+
from __future__ import annotations
import logging
import os
from opentelemetry import trace
-from opentelemetry.sdk.resources import Resource, SERVICE_NAME, SERVICE_VERSION
+from opentelemetry.sdk.resources import SERVICE_NAME, SERVICE_VERSION, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
@@ -56,6 +57,7 @@ def _build_exporter(exporter_name: str):
if name == "jaeger":
try:
from opentelemetry.exporter.jaeger.thrift import JaegerExporter # type: ignore
+
return JaegerExporter(
agent_host_name=os.getenv("JAEGER_HOST", "jaeger"),
agent_port=int(os.getenv("JAEGER_PORT", "6831")),
@@ -66,14 +68,14 @@ def _build_exporter(exporter_name: str):
if name == "otlp":
try:
- from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter # type: ignore
+ from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
+ OTLPSpanExporter, # type: ignore
+ )
+
endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://otel-collector:4317")
return OTLPSpanExporter(endpoint=endpoint, insecure=True)
except ImportError:
- logger.warning(
- "opentelemetry-exporter-otlp-proto-grpc not installed; "
- "install it or set OTEL_EXPORTER=console"
- )
+ logger.warning("opentelemetry-exporter-otlp-proto-grpc not installed; install it or set OTEL_EXPORTER=console")
return None
# "none" or unknown → no exporter
@@ -112,6 +114,7 @@ def instrument_app(app) -> None: # noqa: ANN001
# FastAPI
try:
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor # type: ignore
+
FastAPIInstrumentor.instrument_app(app)
logger.debug("FastAPI auto-instrumented")
except ImportError:
@@ -120,6 +123,7 @@ def instrument_app(app) -> None: # noqa: ANN001
# SQLAlchemy (engine-level spans)
try:
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor # type: ignore
+
SQLAlchemyInstrumentor().instrument()
logger.debug("SQLAlchemy auto-instrumented")
except ImportError:
@@ -128,6 +132,7 @@ def instrument_app(app) -> None: # noqa: ANN001
# httpx (outbound HTTP calls — plugin invocations, enrichment calls, etc.)
try:
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor # type: ignore
+
HTTPXClientInstrumentor().instrument()
logger.debug("httpx auto-instrumented")
except ImportError:
diff --git a/services/api/app/db/database.py b/services/api/app/db/database.py
index b2986e5a1..161102cf1 100644
--- a/services/api/app/db/database.py
+++ b/services/api/app/db/database.py
@@ -1,4 +1,5 @@
"""Database connection management."""
+
from collections.abc import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
diff --git a/services/api/app/db/neo4j.py b/services/api/app/db/neo4j.py
index 55afd0f3e..9069a950b 100644
--- a/services/api/app/db/neo4j.py
+++ b/services/api/app/db/neo4j.py
@@ -2,21 +2,22 @@
Neo4j driver singleton for AiSOC graph layer.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
import asyncio
import logging
+from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
-from typing import AsyncGenerator, Optional
-from neo4j import AsyncGraphDatabase, AsyncDriver, AsyncSession
+from neo4j import AsyncDriver, AsyncGraphDatabase, AsyncSession
from neo4j.exceptions import ServiceUnavailable
from app.core.config import settings
logger = logging.getLogger(__name__)
-_driver: Optional[AsyncDriver] = None
+_driver: AsyncDriver | None = None
async def init_neo4j() -> None:
@@ -43,7 +44,7 @@ async def init_neo4j() -> None:
return
except ServiceUnavailable:
if attempt < 4:
- wait = 2 ** attempt
+ wait = 2**attempt
logger.warning("Neo4j not ready, retrying", attempt=attempt + 1, wait=wait)
await asyncio.sleep(wait)
else:
diff --git a/services/api/app/db/rls.py b/services/api/app/db/rls.py
index deeb01faa..7cd909535 100644
--- a/services/api/app/db/rls.py
+++ b/services/api/app/db/rls.py
@@ -19,6 +19,7 @@ defined in migrations/002_rls.sql.
For compatibility, ``get_current_user`` also exposes a helper
``set_rls_context(session, tenant_id)`` that can be called manually.
"""
+
from __future__ import annotations
import uuid
@@ -29,7 +30,7 @@ from fastapi import Depends
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
-from app.api.v1.deps import get_current_user, CurrentUser
+from app.api.v1.deps import CurrentUser, get_current_user
from app.db.database import AsyncSessionLocal
diff --git a/services/api/app/graphql/__init__.py b/services/api/app/graphql/__init__.py
index d1d2af555..f1b9a97e6 100644
--- a/services/api/app/graphql/__init__.py
+++ b/services/api/app/graphql/__init__.py
@@ -4,6 +4,7 @@ Mounted at /graphql — provides a typed query interface over alerts, cases,
investigations, and playbooks. The resolver layer re-uses the same
SQLAlchemy session and auth dependencies as the REST endpoints.
"""
+
from .schema import schema # noqa: F401 – re-export for main.py
__all__ = ["schema"]
diff --git a/services/api/app/graphql/query.py b/services/api/app/graphql/query.py
index e81521211..361e7ed95 100644
--- a/services/api/app/graphql/query.py
+++ b/services/api/app/graphql/query.py
@@ -2,19 +2,25 @@
All resolvers authenticate via the same ``get_current_user`` dependency
used by the REST layer, and use the shared SQLAlchemy ``AsyncSession``.
+The session is RLS-scoped via ``get_tenant_db`` (see ``schema.py``), so
+Postgres enforces tenant isolation. Resolvers also apply explicit
+``where(tenant_id == user.tenant_id)`` filters as defense-in-depth.
+
Playbook data is fetched from the agents service (HTTP proxy – same as
the REST /playbooks endpoints).
"""
+
from __future__ import annotations
import math
import os
-from typing import Optional
+import uuid
import httpx
import strawberry
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
+from sqlalchemy.sql import Select
from strawberry.types import Info
from app.graphql.types import (
@@ -45,12 +51,26 @@ def _db(info: Info) -> AsyncSession:
return info.context["db"]
-def _tenant_id(info: Info): # noqa: ANN201
- """Return the current user's tenant UUID (or None for superusers)."""
+def _tenant_id(info: Info) -> uuid.UUID | None:
+ """Return the current user's tenant UUID (or None for unauthenticated)."""
user = info.context.get("user")
return getattr(user, "tenant_id", None)
+def _scope(stmt: Select, info: Info, model) -> Select:
+ """Apply ``where(model.tenant_id == user.tenant_id)`` to ``stmt``.
+
+ Acts as defense-in-depth alongside Postgres RLS. If the user has no
+ tenant_id (unauthenticated, which should not reach here), an empty
+ UUID is used so the query returns no rows rather than leaking.
+ """
+ tid = _tenant_id(info)
+ if tid is None:
+ # Block — no anonymous access to tenant data
+ return stmt.where(model.tenant_id == uuid.UUID("00000000-0000-0000-0000-000000000000"))
+ return stmt.where(model.tenant_id == tid)
+
+
def _orm_to_alert(row: Alert) -> AlertType:
return AlertType(
id=row.id,
@@ -152,13 +172,16 @@ async def _proxy_get(path: str, params: dict | None = None): # noqa: ANN201
@strawberry.type
class Query:
-
# ── Alerts ────────────────────────────────────────────────────────────────
- @strawberry.field(description="Fetch a single alert by ID.")
- async def alert(self, info: Info, id: strawberry.ID) -> Optional[AlertType]:
+ @strawberry.field(description="Fetch a single alert by ID (tenant-scoped).")
+ async def alert(self, info: Info, id: strawberry.ID) -> AlertType | None:
db = _db(info)
- row = await db.get(Alert, id)
+ tid = _tenant_id(info)
+ if tid is None:
+ return None
+ result = await db.execute(select(Alert).where(Alert.id == id, Alert.tenant_id == tid))
+ row = result.scalar_one_or_none()
return _orm_to_alert(row) if row else None
@strawberry.field(description="Paginated list of alerts with optional filters.")
@@ -167,12 +190,12 @@ class Query:
info: Info,
page: int = 1,
page_size: int = 25,
- severity: Optional[str] = None,
- status: Optional[str] = None,
- search: Optional[str] = None,
+ severity: str | None = None,
+ status: str | None = None,
+ search: str | None = None,
) -> AlertPage:
db = _db(info)
- q = select(Alert)
+ q = _scope(select(Alert), info, Alert)
if severity:
q = q.where(Alert.severity == severity)
@@ -198,10 +221,14 @@ class Query:
# ── Cases ─────────────────────────────────────────────────────────────────
- @strawberry.field(description="Fetch a single case by ID.")
- async def case(self, info: Info, id: strawberry.ID) -> Optional[CaseType]:
+ @strawberry.field(description="Fetch a single case by ID (tenant-scoped).")
+ async def case(self, info: Info, id: strawberry.ID) -> CaseType | None:
db = _db(info)
- row = await db.get(Case, id)
+ tid = _tenant_id(info)
+ if tid is None:
+ return None
+ result = await db.execute(select(Case).where(Case.id == id, Case.tenant_id == tid))
+ row = result.scalar_one_or_none()
return _orm_to_case(row) if row else None
@strawberry.field(description="Paginated list of cases with optional filters.")
@@ -210,12 +237,12 @@ class Query:
info: Info,
page: int = 1,
page_size: int = 25,
- status: Optional[str] = None,
- priority: Optional[str] = None,
- search: Optional[str] = None,
+ status: str | None = None,
+ priority: str | None = None,
+ search: str | None = None,
) -> CasePage:
db = _db(info)
- q = select(Case)
+ q = _scope(select(Case), info, Case)
if status:
q = q.where(Case.status == status)
@@ -247,11 +274,11 @@ class Query:
info: Info,
page: int = 1,
page_size: int = 25,
- severity: Optional[str] = None,
- status: Optional[str] = None,
+ severity: str | None = None,
+ status: str | None = None,
) -> DetectionRulePage:
db = _db(info)
- q = select(DetectionRule)
+ q = _scope(select(DetectionRule), info, DetectionRule)
if severity:
q = q.where(DetectionRule.severity == severity)
@@ -281,10 +308,10 @@ class Query:
info: Info,
page: int = 1,
page_size: int = 25,
- enabled: Optional[bool] = None,
+ enabled: bool | None = None,
) -> ConnectorPage:
db = _db(info)
- q = select(Connector)
+ q = _scope(select(Connector), info, Connector)
if enabled is not None:
q = q.where(Connector.is_enabled == enabled)
@@ -359,23 +386,27 @@ class Query:
from datetime import UTC, datetime, timedelta
db = _db(info)
+ tid = _tenant_id(info)
+ if tid is None:
+ return SocStatsType(
+ total_alerts=0,
+ open_cases=0,
+ critical_alerts=0,
+ alerts_last_24h=0,
+ mean_time_to_detect_hours=None,
+ mean_time_to_respond_hours=None,
+ )
- total_alerts = (await db.execute(select(func.count()).select_from(Alert))).scalar_one()
+ total_alerts = (await db.execute(select(func.count()).select_from(Alert).where(Alert.tenant_id == tid))).scalar_one()
open_cases = (
- await db.execute(
- select(func.count()).select_from(Case).where(Case.status.in_(["open", "in_progress"]))
- )
+ await db.execute(select(func.count()).select_from(Case).where(Case.tenant_id == tid, Case.status.in_(["open", "in_progress"])))
).scalar_one()
critical_alerts = (
- await db.execute(
- select(func.count()).select_from(Alert).where(Alert.severity == "critical")
- )
+ await db.execute(select(func.count()).select_from(Alert).where(Alert.tenant_id == tid, Alert.severity == "critical"))
).scalar_one()
cutoff = datetime.now(UTC) - timedelta(hours=24)
alerts_24h = (
- await db.execute(
- select(func.count()).select_from(Alert).where(Alert.created_at >= cutoff)
- )
+ await db.execute(select(func.count()).select_from(Alert).where(Alert.tenant_id == tid, Alert.created_at >= cutoff))
).scalar_one()
return SocStatsType(
diff --git a/services/api/app/graphql/schema.py b/services/api/app/graphql/schema.py
index 4cd54a0f2..ae28891b7 100644
--- a/services/api/app/graphql/schema.py
+++ b/services/api/app/graphql/schema.py
@@ -4,32 +4,46 @@ Usage in main.py::
from app.graphql.schema import graphql_router
app.include_router(graphql_router, prefix="/graphql")
+
+Tenant isolation
+----------------
+The context factory uses ``get_tenant_db`` so every GraphQL transaction
+runs with ``app.current_tenant_id`` set, activating Postgres RLS.
+Resolvers also apply explicit ``where(tenant_id == user.tenant_id)``
+filters as defense-in-depth in case a model is not yet covered by RLS.
+
+GraphiQL exposure
+-----------------
+GraphiQL is enabled only when ``ENVIRONMENT == "development"``. In any
+other environment the introspection UI is disabled to reduce attack
+surface.
"""
+
from __future__ import annotations
-from typing import Any, Optional
+from typing import Any
import strawberry
from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession
from strawberry.fastapi import GraphQLRouter
-from app.api.v1.deps import get_current_user
-from app.db.database import get_db
+from app.api.v1.deps import CurrentUser, get_current_user
+from app.core.config import settings
+from app.db.rls import get_tenant_db
from app.graphql.query import Query
-
# ─── Context factory ──────────────────────────────────────────────────────────
async def get_graphql_context(
- db: AsyncSession = Depends(get_db),
- user=Depends(get_current_user),
+ db: AsyncSession = Depends(get_tenant_db),
+ user: CurrentUser = Depends(get_current_user),
) -> dict[str, Any]:
"""Build the per-request context dict injected into every resolver.
- The ``db`` session and authenticated ``user`` are available via
- ``info.context["db"]`` and ``info.context["user"]``.
+ The ``db`` session is RLS-scoped to the user's tenant, and the
+ authenticated ``user`` is available via ``info.context["user"]``.
"""
return {"db": db, "user": user}
@@ -40,8 +54,16 @@ schema = strawberry.Schema(query=Query)
# ─── FastAPI router ───────────────────────────────────────────────────────────
+# GraphiQL is only enabled in development. Production-like environments get a
+# pure JSON endpoint with no introspection UI.
+#
+# strawberry-graphql >= 0.231 replaced the old ``graphiql: bool`` kwarg with
+# ``graphql_ide: GraphQL_IDE | None`` (where ``"graphiql"`` is the default and
+# ``None`` disables the IDE entirely). We pin to that newer API.
+_graphql_ide: str | None = "graphiql" if settings.ENVIRONMENT.lower() == "development" else None
+
graphql_router = GraphQLRouter(
schema,
context_getter=get_graphql_context,
- # GraphiQL is always available; disable in production by passing graphiql=False
+ graphql_ide=_graphql_ide,
)
diff --git a/services/api/app/graphql/types.py b/services/api/app/graphql/types.py
index 4ce8a6e60..c70d0be31 100644
--- a/services/api/app/graphql/types.py
+++ b/services/api/app/graphql/types.py
@@ -3,37 +3,37 @@
These mirror the Pydantic response schemas used in the REST API so that
callers get the same field names/types regardless of protocol.
"""
+
from __future__ import annotations
import uuid
from datetime import datetime
-from typing import Optional
import strawberry
-
# ─── Alert ────────────────────────────────────────────────────────────────────
+
@strawberry.type(description="A normalised security alert ingested from any connector.")
class AlertType:
id: uuid.UUID
tenant_id: uuid.UUID
title: str
- description: Optional[str]
+ description: str | None
severity: str
status: str
priority: int
- category: Optional[str]
+ category: str | None
mitre_tactics: strawberry.scalars.JSON
mitre_techniques: strawberry.scalars.JSON
- connector_type: Optional[str]
- ai_score: Optional[float]
- ai_summary: Optional[str]
+ connector_type: str | None
+ ai_score: float | None
+ ai_summary: str | None
ai_recommendations: strawberry.scalars.JSON
affected_ips: strawberry.scalars.JSON
affected_hosts: strawberry.scalars.JSON
affected_users: strawberry.scalars.JSON
- case_id: Optional[uuid.UUID]
+ case_id: uuid.UUID | None
tags: strawberry.scalars.JSON
event_time: datetime
first_seen: datetime
@@ -53,27 +53,28 @@ class AlertPage:
# ─── Case ─────────────────────────────────────────────────────────────────────
+
@strawberry.type(description="A security case grouping one or more alerts.")
class CaseType:
id: uuid.UUID
tenant_id: uuid.UUID
case_number: str
title: str
- description: Optional[str]
+ description: str | None
status: str
priority: str
severity: str
case_type: str
mitre_tactics: strawberry.scalars.JSON
mitre_techniques: strawberry.scalars.JSON
- assigned_to_id: Optional[uuid.UUID]
- sla_deadline: Optional[datetime]
+ assigned_to_id: uuid.UUID | None
+ sla_deadline: datetime | None
sla_breached: bool
alert_ids: strawberry.scalars.JSON
tags: strawberry.scalars.JSON
ticket_refs: strawberry.scalars.JSON
- summary: Optional[str]
- resolution: Optional[str]
+ summary: str | None
+ resolution: str | None
created_at: datetime
updated_at: datetime
@@ -89,12 +90,13 @@ class CasePage:
# ─── Detection Rule ────────────────────────────────────────────────────────────
+
@strawberry.type(description="A SIEM detection rule (Sigma / YARA / custom).")
class DetectionRuleType:
id: uuid.UUID
tenant_id: uuid.UUID
name: str
- description: Optional[str]
+ description: str | None
rule_type: str
severity: str
status: str
@@ -115,16 +117,17 @@ class DetectionRulePage:
# ─── Connector ────────────────────────────────────────────────────────────────
+
@strawberry.type(description="An external data-source connector configuration.")
class ConnectorType:
id: uuid.UUID
tenant_id: uuid.UUID
name: str
connector_type: str
- description: Optional[str]
+ description: str | None
enabled: bool
status: str
- last_sync_at: Optional[datetime]
+ last_sync_at: datetime | None
total_events_processed: int
created_at: datetime
updated_at: datetime
@@ -141,17 +144,18 @@ class ConnectorPage:
# ─── Playbook ─────────────────────────────────────────────────────────────────
+
@strawberry.type(description="An automation playbook (stored in the agents service).")
class PlaybookType:
id: str
name: str
- description: Optional[str]
+ description: str | None
enabled: bool
trigger: strawberry.scalars.JSON
steps: strawberry.scalars.JSON
tags: strawberry.scalars.JSON
- created_at: Optional[str]
- updated_at: Optional[str]
+ created_at: str | None
+ updated_at: str | None
@strawberry.type
@@ -162,18 +166,19 @@ class PlaybookRunType:
trigger_event: strawberry.scalars.JSON
steps_executed: int
steps_total: int
- error: Optional[str]
+ error: str | None
started_at: str
- completed_at: Optional[str]
+ completed_at: str | None
# ─── Stats ────────────────────────────────────────────────────────────────────
+
@strawberry.type(description="High-level SOC statistics for the current tenant.")
class SocStatsType:
total_alerts: int
open_cases: int
critical_alerts: int
alerts_last_24h: int
- mean_time_to_detect_hours: Optional[float]
- mean_time_to_respond_hours: Optional[float]
+ mean_time_to_detect_hours: float | None
+ mean_time_to_respond_hours: float | None
diff --git a/services/api/app/main.py b/services/api/app/main.py
index c860088fc..ba8b9e2b2 100644
--- a/services/api/app/main.py
+++ b/services/api/app/main.py
@@ -1,28 +1,34 @@
"""AiSOC Core API - FastAPI Application Entry Point."""
+
+import hmac
import time
+from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
-from typing import AsyncGenerator
import structlog
-from fastapi import FastAPI, Request, Response
+from fastapi import FastAPI, HTTPException, Request, Response, Security, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
-from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_LATEST
+from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
+from prometheus_client import CONTENT_TYPE_LATEST, Counter, Histogram, generate_latest
from app.api.v1.router import api_router
from app.auth.oidc import router as oidc_router
from app.auth.saml import router as saml_router
-from app.core.config import settings
-from app.middleware.audit_middleware import AuditMiddleware
-from app.middleware.demo_mode import DemoModeMiddleware
+from app.core.config import settings, warn_if_insecure_defaults
from app.core.logging import configure_logging
from app.core.telemetry import instrument_app
from app.db.database import engine
-from app.db.neo4j import init_neo4j, close_neo4j
+from app.db.neo4j import close_neo4j, init_neo4j
from app.graphql.schema import graphql_router
+from app.middleware.audit_middleware import AuditMiddleware
+from app.middleware.demo_mode import DemoModeMiddleware
from app.models import Base
from app.services.plugin_manager import get_plugin_manager
+_DEV_ENVIRONMENTS = {"development", "dev", "local", "demo", "test"}
+_metrics_bearer = HTTPBearer(auto_error=False)
+
logger = structlog.get_logger(__name__)
# Prometheus metrics
@@ -44,6 +50,11 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
configure_logging()
logger.info("AiSOC API starting up", version=settings.VERSION, environment=settings.ENVIRONMENT)
+ # Surface insecure defaults (placeholder SECRET_KEY, missing METRICS_TOKEN
+ # outside dev, plugin trust mode disabled outside dev) at the top of the
+ # log stream so operators see them before anything else.
+ warn_if_insecure_defaults(settings)
+
# Create all database tables (dev only; use Alembic migrations in prod)
if settings.ENVIRONMENT == "development":
try:
@@ -79,10 +90,7 @@ def create_application() -> FastAPI:
"""Create and configure the FastAPI application."""
app = FastAPI(
title="AiSOC Platform API",
- description=(
- "AiSOC — open-source AI Security Operations Center. "
- "Autonomous threat detection, investigation, and response."
- ),
+ description=("AiSOC — open-source AI Security Operations Center. Autonomous threat detection, investigation, and response."),
version=settings.VERSION,
docs_url="/api/docs" if settings.ENVIRONMENT != "production" else None,
redoc_url="/api/redoc" if settings.ENVIRONMENT != "production" else None,
@@ -163,7 +171,43 @@ async def health_check() -> dict:
}
+def _metrics_environment_is_dev() -> bool:
+ return (settings.ENVIRONMENT or "").strip().lower() in _DEV_ENVIRONMENTS
+
+
@app.get("/metrics", tags=["system"])
-async def metrics() -> Response:
- """Prometheus metrics endpoint."""
+async def metrics(
+ creds: HTTPAuthorizationCredentials | None = Security(_metrics_bearer),
+) -> Response:
+ """Prometheus metrics endpoint.
+
+ Auth gate:
+
+ * If ``settings.METRICS_TOKEN`` is set, callers MUST present
+ ``Authorization: Bearer <METRICS_TOKEN>``. Comparison uses
+ ``hmac.compare_digest`` to avoid timing leaks.
+ * If ``METRICS_TOKEN`` is empty, the endpoint is open **only** in a
+ development-class environment. In any other environment we refuse
+ the scrape with a 401 so operators don't accidentally ship an
+ unauthenticated metrics endpoint to the open internet.
+ """
+ token = (settings.METRICS_TOKEN or "").strip()
+
+ if token:
+ presented = (creds.credentials if creds else "") or ""
+ if not hmac.compare_digest(presented, token):
+ raise HTTPException(
+ status_code=status.HTTP_401_UNAUTHORIZED,
+ detail="invalid metrics token",
+ headers={"WWW-Authenticate": 'Bearer realm="metrics"'},
+ )
+ elif not _metrics_environment_is_dev():
+ # Production-ish environment with no token configured: refuse rather
+ # than expose internal counters anonymously.
+ raise HTTPException(
+ status_code=status.HTTP_401_UNAUTHORIZED,
+ detail="metrics endpoint requires METRICS_TOKEN outside development",
+ headers={"WWW-Authenticate": 'Bearer realm="metrics"'},
+ )
+
return Response(generate_latest(), media_type=CONTENT_TYPE_LATEST)
diff --git a/services/api/app/middleware/audit_middleware.py b/services/api/app/middleware/audit_middleware.py
index f7955f19f..63a29eb93 100644
--- a/services/api/app/middleware/audit_middleware.py
+++ b/services/api/app/middleware/audit_middleware.py
@@ -6,19 +6,16 @@ it never blocks the hot path.
Non-authenticated requests and GET/HEAD/OPTIONS are silently skipped.
"""
+
from __future__ import annotations
import re
import uuid
-from typing import Callable
+from app.db.database import AsyncSessionLocal
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
-from app.core.config import settings
-from app.db.database import AsyncSessionLocal
-
-
_MUTATING = {"POST", "PUT", "PATCH", "DELETE"}
# Map URL patterns → (action, resource) label pairs for cleaner audit records.
@@ -61,6 +58,7 @@ def _extract_jwt_claims(request: Request) -> tuple[uuid.UUID | None, uuid.UUID |
token = auth.split(" ", 1)[1]
try:
import jwt # noqa: PLC0415
+
payload = jwt.decode(token, options={"verify_signature": False})
user_id = uuid.UUID(payload["sub"]) if payload.get("sub") else None
tenant_id = uuid.UUID(payload["tenant_id"]) if payload.get("tenant_id") else None
@@ -100,6 +98,7 @@ class AuditMiddleware(BaseHTTPMiddleware):
async with AsyncSessionLocal() as db:
from app.models.audit import AuditLog # noqa: PLC0415
+
forwarded = request.headers.get("x-forwarded-for")
actor_ip = (forwarded.split(",")[0].strip() if forwarded else None) or (
str(request.client.host) if request.client else None
diff --git a/services/api/app/middleware/demo_mode.py b/services/api/app/middleware/demo_mode.py
index 4bd87fb25..7c88e10d1 100644
--- a/services/api/app/middleware/demo_mode.py
+++ b/services/api/app/middleware/demo_mode.py
@@ -26,12 +26,11 @@ import logging
import re
from collections.abc import Awaitable, Callable
+from app.core.config import settings
from fastapi import Request, Response
from fastapi.responses import JSONResponse
from starlette.middleware.base import BaseHTTPMiddleware
-from app.core.config import settings
-
log = logging.getLogger(__name__)
# Always allowed (auth flows + read endpoints). Match by prefix.
diff --git a/services/api/app/models/__init__.py b/services/api/app/models/__init__.py
index c4e1c9e80..1d70bc72f 100644
--- a/services/api/app/models/__init__.py
+++ b/services/api/app/models/__init__.py
@@ -1,4 +1,5 @@
"""ORM models package - imports all models for Alembic and SQLAlchemy."""
+
from app.db.database import Base
from app.models.alert import Alert
from app.models.case import Case, CaseTask, CaseTimeline
diff --git a/services/api/app/models/alert.py b/services/api/app/models/alert.py
index e735eecd2..ca29d5ab0 100644
--- a/services/api/app/models/alert.py
+++ b/services/api/app/models/alert.py
@@ -1,9 +1,10 @@
"""Alert ORM model."""
+
import uuid
from datetime import UTC, datetime
from sqlalchemy import DateTime, Float, ForeignKey, Index, Integer, String, Text
-from sqlalchemy.dialects.postgresql import ARRAY, JSONB, UUID
+from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.db.database import Base
@@ -69,9 +70,7 @@ class Alert(Base):
first_seen: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
last_seen: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
resolved_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC), index=True
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC), index=True)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
diff --git a/services/api/app/models/audit.py b/services/api/app/models/audit.py
index 22caa4353..3535c748c 100644
--- a/services/api/app/models/audit.py
+++ b/services/api/app/models/audit.py
@@ -1,9 +1,9 @@
"""AuditLog ORM model — append-only event store."""
+
import uuid
from datetime import UTC, datetime
-from typing import Any
-from sqlalchemy import DateTime, ForeignKey, String, Text
+from sqlalchemy import DateTime, ForeignKey, String
from sqlalchemy.dialects.postgresql import INET, JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
@@ -19,9 +19,7 @@ class AuditLog(Base):
tenant_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True
)
- actor_id: Mapped[uuid.UUID | None] = mapped_column(
- UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
- )
+ actor_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
actor_email: Mapped[str | None] = mapped_column(String(255), nullable=True)
actor_ip: Mapped[str | None] = mapped_column(INET, nullable=True)
action: Mapped[str] = mapped_column(String(200), nullable=False)
@@ -29,6 +27,4 @@ class AuditLog(Base):
resource_id: Mapped[str | None] = mapped_column(String(200), nullable=True)
changes: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
metadata_: Mapped[dict | None] = mapped_column("metadata", JSONB, nullable=True)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC), nullable=False
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC), nullable=False)
diff --git a/services/api/app/models/case.py b/services/api/app/models/case.py
index 4840a8338..66a728c09 100644
--- a/services/api/app/models/case.py
+++ b/services/api/app/models/case.py
@@ -1,8 +1,9 @@
"""Case management ORM models."""
+
import uuid
from datetime import UTC, datetime
-from sqlalchemy import DateTime, ForeignKey, Integer, String, Text
+from sqlalchemy import DateTime, ForeignKey, String, Text
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
@@ -52,9 +53,7 @@ class Case(Base):
lessons_learned: Mapped[str | None] = mapped_column(Text, nullable=True)
closed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC), index=True
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC), index=True)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
@@ -62,9 +61,7 @@ class Case(Base):
)
tasks: Mapped[list["CaseTask"]] = relationship("CaseTask", back_populates="case", lazy="noload")
- timeline: Mapped[list["CaseTimeline"]] = relationship(
- "CaseTimeline", back_populates="case", lazy="noload"
- )
+ timeline: Mapped[list["CaseTimeline"]] = relationship("CaseTimeline", back_populates="case", lazy="noload")
class CaseTask(Base):
@@ -79,9 +76,7 @@ class CaseTask(Base):
assigned_to_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
due_date: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC)
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
case: Mapped["Case"] = relationship("Case", back_populates="tasks")
@@ -97,8 +92,6 @@ class CaseTimeline(Base):
event_metadata: Mapped[dict] = mapped_column("metadata", JSONB, default=dict)
user_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
is_automated: Mapped[bool] = mapped_column(default=False)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC), index=True
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC), index=True)
case: Mapped["Case"] = relationship("Case", back_populates="timeline")
diff --git a/services/api/app/models/compliance.py b/services/api/app/models/compliance.py
index 4c0631a5d..952accb4a 100644
--- a/services/api/app/models/compliance.py
+++ b/services/api/app/models/compliance.py
@@ -1,15 +1,26 @@
"""SQLAlchemy models for compliance framework tables."""
import uuid
-from datetime import datetime
+from datetime import UTC, datetime
from sqlalchemy import ForeignKey, String, Text
-from sqlalchemy.dialects.postgresql import JSONB, TIMESTAMPTZ, UUID
+from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
+from sqlalchemy.types import TIMESTAMP
from app.db.database import Base
+# Aliased for readability; on Postgres this renders as TIMESTAMPTZ.
+# (The PG dialect never exported a `TIMESTAMPTZ` symbol — that's just SQL
+# syntax. The correct way to get a tz-aware column is `TIMESTAMP(timezone=True)`.)
+TIMESTAMPTZ = TIMESTAMP(timezone=True)
+
+
+def _utcnow() -> datetime:
+ return datetime.now(UTC)
+
+
class ComplianceControl(Base):
__tablename__ = "compliance_controls"
@@ -19,7 +30,7 @@ class ComplianceControl(Base):
category: Mapped[str] = mapped_column(Text, nullable=False)
title: Mapped[str] = mapped_column(Text, nullable=False)
description: Mapped[str | None] = mapped_column(Text, nullable=True)
- created_at: Mapped[datetime] = mapped_column(TIMESTAMPTZ, nullable=False, default=datetime.utcnow)
+ created_at: Mapped[datetime] = mapped_column(TIMESTAMPTZ, nullable=False, default=_utcnow)
class ComplianceEvidence(Base):
@@ -32,7 +43,7 @@ class ComplianceEvidence(Base):
title: Mapped[str] = mapped_column(Text, nullable=False)
description: Mapped[str | None] = mapped_column(Text, nullable=True)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="collected")
- collected_at: Mapped[datetime] = mapped_column(TIMESTAMPTZ, nullable=False, default=datetime.utcnow)
+ collected_at: Mapped[datetime] = mapped_column(TIMESTAMPTZ, nullable=False, default=_utcnow)
collected_by: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
metadata_: Mapped[dict | None] = mapped_column("metadata", JSONB, nullable=True)
- created_at: Mapped[datetime] = mapped_column(TIMESTAMPTZ, nullable=False, default=datetime.utcnow)
+ created_at: Mapped[datetime] = mapped_column(TIMESTAMPTZ, nullable=False, default=_utcnow)
diff --git a/services/api/app/models/connector.py b/services/api/app/models/connector.py
index 5196e1ff0..6f6a655a7 100644
--- a/services/api/app/models/connector.py
+++ b/services/api/app/models/connector.py
@@ -1,4 +1,5 @@
"""Connector ORM model."""
+
import uuid
from datetime import UTC, datetime
@@ -26,9 +27,7 @@ class Connector(Base):
events_ingested: Mapped[int] = mapped_column(default=0)
error_count: Mapped[int] = mapped_column(default=0)
tags: Mapped[list] = mapped_column(JSONB, default=list)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC)
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
diff --git a/services/api/app/models/detection_rule.py b/services/api/app/models/detection_rule.py
index 4e385d014..3c3660bd7 100644
--- a/services/api/app/models/detection_rule.py
+++ b/services/api/app/models/detection_rule.py
@@ -1,8 +1,9 @@
"""Detection Rule ORM model."""
+
import uuid
from datetime import UTC, datetime
-from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text
+from sqlalchemy import Boolean, DateTime, Integer, String, Text
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
@@ -13,9 +14,7 @@ class DetectionRule(Base):
__tablename__ = "detection_rules"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
- tenant_id: Mapped[uuid.UUID | None] = mapped_column(
- UUID(as_uuid=True), nullable=True, index=True
- ) # NULL = platform-wide rule
+ tenant_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True, index=True) # NULL = platform-wide rule
name: Mapped[str] = mapped_column(String(255), nullable=False)
description: Mapped[str | None] = mapped_column(Text, nullable=True)
rule_language: Mapped[str] = mapped_column(String(30), nullable=False) # sigma/yara/kql/eql/etc
@@ -45,9 +44,7 @@ class DetectionRule(Base):
version: Mapped[int] = mapped_column(Integer, default=1)
created_by_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC)
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
diff --git a/services/api/app/models/investigation.py b/services/api/app/models/investigation.py
index fe6671928..eb8294a64 100644
--- a/services/api/app/models/investigation.py
+++ b/services/api/app/models/investigation.py
@@ -13,11 +13,11 @@ immutability trigger that this ORM definition cannot express. Dev environments
auto-create the tables via ``Base.metadata.create_all``; the SQL migration
applies the security guardrails on top.
"""
+
from __future__ import annotations
import uuid
from datetime import datetime
-from typing import Optional
from sqlalchemy import (
DateTime,
@@ -40,9 +40,7 @@ class InvestigationRun(Base):
__tablename__ = "investigation_runs"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
tenant_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("tenants.id", ondelete="CASCADE"),
@@ -50,29 +48,25 @@ class InvestigationRun(Base):
index=True,
)
case_id: Mapped[str] = mapped_column(String(200), nullable=False, index=True)
- alert_summary: Mapped[Optional[str]] = mapped_column(Text)
- raw_alert: Mapped[Optional[dict]] = mapped_column(JSONB)
- model_used: Mapped[Optional[str]] = mapped_column(String(100))
+ alert_summary: Mapped[str | None] = mapped_column(Text)
+ raw_alert: Mapped[dict | None] = mapped_column(JSONB)
+ model_used: Mapped[str | None] = mapped_column(String(100))
status: Mapped[str] = mapped_column(String(20), nullable=False, default="running")
- error: Mapped[Optional[str]] = mapped_column(Text)
+ error: Mapped[str | None] = mapped_column(Text)
total_tokens: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
total_cost_usd: Mapped[float] = mapped_column(Numeric(10, 4), nullable=False, default=0)
iterations: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
- started_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), nullable=False, server_default=func.now()
- )
- completed_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True))
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), nullable=False, server_default=func.now()
- )
+ started_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
+ completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
- events: Mapped[list["InvestigationEvent"]] = relationship(
+ events: Mapped[list[InvestigationEvent]] = relationship(
"InvestigationEvent",
back_populates="run",
cascade="all, delete-orphan",
order_by="InvestigationEvent.seq",
)
- artifacts: Mapped[list["InvestigationArtifact"]] = relationship(
+ artifacts: Mapped[list[InvestigationArtifact]] = relationship(
"InvestigationArtifact",
back_populates="run",
cascade="all, delete-orphan",
@@ -89,9 +83,7 @@ class InvestigationEvent(Base):
__tablename__ = "investigation_events"
__table_args__ = (UniqueConstraint("run_id", "seq", name="uq_inv_events_run_seq"),)
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
run_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("investigation_runs.id", ondelete="CASCADE"),
@@ -105,22 +97,18 @@ class InvestigationEvent(Base):
index=True,
)
seq: Mapped[int] = mapped_column(Integer, nullable=False)
- ts: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), nullable=False, server_default=func.now()
- )
+ ts: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
kind: Mapped[str] = mapped_column(String(40), nullable=False, index=True)
agent: Mapped[str] = mapped_column(String(80), nullable=False)
summary: Mapped[str] = mapped_column(Text, nullable=False)
- payload: Mapped[Optional[dict]] = mapped_column(JSONB)
- input_hash: Mapped[Optional[str]] = mapped_column(String(64))
- output_hash: Mapped[Optional[str]] = mapped_column(String(64))
+ payload: Mapped[dict | None] = mapped_column(JSONB)
+ input_hash: Mapped[str | None] = mapped_column(String(64))
+ output_hash: Mapped[str | None] = mapped_column(String(64))
duration_ms: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), nullable=False, server_default=func.now()
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
run: Mapped[InvestigationRun] = relationship("InvestigationRun", back_populates="events")
- artifacts: Mapped[list["InvestigationArtifact"]] = relationship(
+ artifacts: Mapped[list[InvestigationArtifact]] = relationship(
"InvestigationArtifact",
back_populates="event",
)
@@ -131,16 +119,14 @@ class InvestigationArtifact(Base):
__tablename__ = "investigation_artifacts"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
run_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("investigation_runs.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
- event_id: Mapped[Optional[uuid.UUID]] = mapped_column(
+ event_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True),
ForeignKey("investigation_events.id", ondelete="CASCADE"),
index=True,
@@ -152,15 +138,11 @@ class InvestigationArtifact(Base):
index=True,
)
kind: Mapped[str] = mapped_column(String(40), nullable=False, index=True)
- content: Mapped[Optional[str]] = mapped_column(Text)
- blob_ref: Mapped[Optional[str]] = mapped_column(Text)
+ content: Mapped[str | None] = mapped_column(Text)
+ blob_ref: Mapped[str | None] = mapped_column(Text)
sha256: Mapped[str] = mapped_column(String(64), nullable=False)
size_bytes: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), nullable=False, server_default=func.now()
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
run: Mapped[InvestigationRun] = relationship("InvestigationRun", back_populates="artifacts")
- event: Mapped[Optional[InvestigationEvent]] = relationship(
- "InvestigationEvent", back_populates="artifacts"
- )
+ event: Mapped[InvestigationEvent | None] = relationship("InvestigationEvent", back_populates="artifacts")
diff --git a/services/api/app/models/rbac.py b/services/api/app/models/rbac.py
index e5d6a0133..c0a932f85 100644
--- a/services/api/app/models/rbac.py
+++ b/services/api/app/models/rbac.py
@@ -1,4 +1,5 @@
"""RBAC ORM models: Role, Permission, RolePermission, UserRole."""
+
import uuid
from datetime import UTC, datetime
@@ -19,9 +20,7 @@ class Permission(Base):
description: Mapped[str | None] = mapped_column(Text, nullable=True)
category: Mapped[str | None] = mapped_column(String(100), nullable=True)
- role_permissions: Mapped[list["RolePermission"]] = relationship(
- "RolePermission", back_populates="permission", lazy="noload"
- )
+ role_permissions: Mapped[list["RolePermission"]] = relationship("RolePermission", back_populates="permission", lazy="noload")
class Role(Base):
@@ -46,9 +45,7 @@ class Role(Base):
role_permissions: Mapped[list["RolePermission"]] = relationship(
"RolePermission", back_populates="role", cascade="all, delete-orphan", lazy="noload"
)
- user_roles: Mapped[list["UserRole"]] = relationship(
- "UserRole", back_populates="role", cascade="all, delete-orphan", lazy="noload"
- )
+ user_roles: Mapped[list["UserRole"]] = relationship("UserRole", back_populates="role", cascade="all, delete-orphan", lazy="noload")
class RolePermission(Base):
@@ -56,12 +53,8 @@ class RolePermission(Base):
__tablename__ = "role_permissions"
- role_id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), ForeignKey("roles.id", ondelete="CASCADE"), primary_key=True
- )
- permission_id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), ForeignKey("permissions.id", ondelete="CASCADE"), primary_key=True
- )
+ role_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("roles.id", ondelete="CASCADE"), primary_key=True)
+ permission_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("permissions.id", ondelete="CASCADE"), primary_key=True)
role: Mapped["Role"] = relationship("Role", back_populates="role_permissions")
permission: Mapped["Permission"] = relationship("Permission", back_populates="role_permissions")
@@ -72,15 +65,9 @@ class UserRole(Base):
__tablename__ = "user_roles"
- user_id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), primary_key=True
- )
- role_id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), ForeignKey("roles.id", ondelete="CASCADE"), primary_key=True
- )
+ user_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), primary_key=True)
+ role_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("roles.id", ondelete="CASCADE"), primary_key=True)
assigned_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
- assigned_by: Mapped[uuid.UUID | None] = mapped_column(
- UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
- )
+ assigned_by: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
role: Mapped["Role"] = relationship("Role", back_populates="user_roles", foreign_keys=[role_id])
diff --git a/services/api/app/models/responder.py b/services/api/app/models/responder.py
index 209ad9a65..eb6c41892 100644
--- a/services/api/app/models/responder.py
+++ b/services/api/app/models/responder.py
@@ -13,6 +13,7 @@ tables that back the PWA experience:
These are intentionally separate from the existing models so the
responder surface can evolve without impacting the rest of the API.
"""
+
from __future__ import annotations
import uuid
@@ -37,15 +38,9 @@ class PasskeyCredential(Base):
__tablename__ = "passkey_credentials"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
- )
- tenant_id: Mapped[uuid.UUID] = mapped_column(
- ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True
- )
- user_id: Mapped[uuid.UUID] = mapped_column(
- ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
+ tenant_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True)
+ user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
credential_id: Mapped[str] = mapped_column(Text, nullable=False, unique=True)
public_key: Mapped[str] = mapped_column(Text, nullable=False)
sign_count: Mapped[int] = mapped_column(default=0)
@@ -53,15 +48,9 @@ class PasskeyCredential(Base):
device_name: Mapped[str] = mapped_column(String(120), nullable=False)
aaguid: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
is_discoverable: Mapped[bool] = mapped_column(Boolean, default=True)
- last_used_at: Mapped[datetime | None] = mapped_column(
- DateTime(timezone=True), nullable=True
- )
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC)
- )
- revoked_at: Mapped[datetime | None] = mapped_column(
- DateTime(timezone=True), nullable=True
- )
+ last_used_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
+ revoked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
__table_args__ = (
Index("ix_passkeys_user_active", "user_id", "revoked_at"),
@@ -74,26 +63,16 @@ class PasskeyChallenge(Base):
__tablename__ = "passkey_challenges"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
purpose: Mapped[str] = mapped_column(String(20), nullable=False)
challenge: Mapped[str] = mapped_column(Text, nullable=False, unique=True)
- user_id: Mapped[uuid.UUID | None] = mapped_column(
- ForeignKey("users.id", ondelete="CASCADE"), nullable=True
- )
- tenant_id: Mapped[uuid.UUID | None] = mapped_column(
- ForeignKey("tenants.id", ondelete="CASCADE"), nullable=True
- )
+ user_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=True)
+ tenant_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("tenants.id", ondelete="CASCADE"), nullable=True)
email_hint: Mapped[str | None] = mapped_column(String(255), nullable=True)
rp_id: Mapped[str] = mapped_column(String(255), nullable=False)
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
- consumed_at: Mapped[datetime | None] = mapped_column(
- DateTime(timezone=True), nullable=True
- )
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC)
- )
+ consumed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
__table_args__ = (
Index("ix_passkey_challenges_expiry", "expires_at"),
@@ -106,12 +85,8 @@ class OnCallStatus(Base):
__tablename__ = "oncall_status"
- user_id: Mapped[uuid.UUID] = mapped_column(
- ForeignKey("users.id", ondelete="CASCADE"), primary_key=True
- )
- tenant_id: Mapped[uuid.UUID] = mapped_column(
- ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True
- )
+ user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), primary_key=True)
+ tenant_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True)
status: Mapped[str] = mapped_column(String(20), default="offline")
schedule_ref: Mapped[str | None] = mapped_column(String(200), nullable=True)
rotation: Mapped[str | None] = mapped_column(String(80), nullable=True)
@@ -122,9 +97,7 @@ class OnCallStatus(Base):
default=lambda: datetime.now(UTC),
onupdate=lambda: datetime.now(UTC),
)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC)
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
__table_args__ = (Index("ix_oncall_tenant_status", "tenant_id", "status"),)
@@ -134,12 +107,8 @@ class AgentApproval(Base):
__tablename__ = "agent_approvals"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
- )
- tenant_id: Mapped[uuid.UUID] = mapped_column(
- ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
+ tenant_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True)
run_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True),
@@ -147,14 +116,10 @@ class AgentApproval(Base):
nullable=True,
)
case_id: Mapped[str | None] = mapped_column(String(200), nullable=True)
- alert_id: Mapped[uuid.UUID | None] = mapped_column(
- UUID(as_uuid=True), nullable=True
- )
+ alert_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
requested_by: Mapped[str] = mapped_column(String(120), default="agent")
- required_user_id: Mapped[uuid.UUID | None] = mapped_column(
- ForeignKey("users.id", ondelete="SET NULL"), nullable=True
- )
+ required_user_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
required_topic: Mapped[str | None] = mapped_column(String(80), nullable=True)
title: Mapped[str] = mapped_column(String(200), nullable=False)
@@ -163,20 +128,12 @@ class AgentApproval(Base):
action: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
status: Mapped[str] = mapped_column(String(20), default="pending", index=True)
- decided_by_id: Mapped[uuid.UUID | None] = mapped_column(
- ForeignKey("users.id", ondelete="SET NULL"), nullable=True
- )
- decided_at: Mapped[datetime | None] = mapped_column(
- DateTime(timezone=True), nullable=True
- )
+ decided_by_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
+ decided_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
decision_comment: Mapped[str | None] = mapped_column(Text, nullable=True)
- expires_at: Mapped[datetime | None] = mapped_column(
- DateTime(timezone=True), nullable=True
- )
+ expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=lambda: datetime.now(UTC), index=True
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC), index=True)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
diff --git a/services/api/app/models/sla.py b/services/api/app/models/sla.py
index 3dbe66c3b..1e5c64f4b 100644
--- a/services/api/app/models/sla.py
+++ b/services/api/app/models/sla.py
@@ -9,26 +9,20 @@ from sqlalchemy import CheckConstraint, DateTime, ForeignKey, Integer, String, U
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
-from app.db.base import Base
+from app.db.database import Base
class TenantSLAConfig(Base):
__tablename__ = "tenant_sla_config"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
- )
- tenant_id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
+ tenant_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False)
severity: Mapped[str] = mapped_column(String(16), nullable=False)
mttd_target: Mapped[int] = mapped_column(Integer, nullable=False, default=60)
mttr_target: Mapped[int] = mapped_column(Integer, nullable=False, default=240)
mttc_target: Mapped[int] = mapped_column(Integer, nullable=False, default=480)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=datetime.utcnow)
- updated_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), default=datetime.utcnow, onupdate=datetime.utcnow
- )
+ updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=datetime.utcnow, onupdate=datetime.utcnow)
__table_args__ = (
UniqueConstraint("tenant_id", "severity", name="uq_sla_config_tenant_severity"),
@@ -42,18 +36,17 @@ class TenantSLAConfig(Base):
class AlertSLAEvent(Base):
__tablename__ = "alert_sla_events"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
tenant_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False)
alert_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False)
severity: Mapped[str] = mapped_column(String(16), nullable=False)
event_type: Mapped[str] = mapped_column(String(32), nullable=False)
- occurred_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), nullable=False, default=datetime.utcnow
- )
+ occurred_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=datetime.utcnow)
actor_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
- metadata: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
+ # `metadata` is reserved on the Declarative `Base`; map the column to the
+ # same DB name but expose it under `metadata_` on the ORM side, mirroring
+ # what we already do in `app/models/compliance.py`.
+ metadata_: Mapped[dict] = mapped_column("metadata", JSONB, nullable=False, default=dict)
__table_args__ = (
CheckConstraint(
diff --git a/services/api/app/models/tenant.py b/services/api/app/models/tenant.py
index c9e6d64c6..a4d41dc08 100644
--- a/services/api/app/models/tenant.py
+++ b/services/api/app/models/tenant.py
@@ -1,8 +1,9 @@
"""Tenant, User, and ApiKey ORM models."""
+
import uuid
from datetime import UTC, datetime
-from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text
+from sqlalchemy import Boolean, DateTime, ForeignKey, String
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
diff --git a/services/api/app/scripts/seed_demo.py b/services/api/app/scripts/seed_demo.py
index 93c633d60..82d89d96c 100644
--- a/services/api/app/scripts/seed_demo.py
+++ b/services/api/app/scripts/seed_demo.py
@@ -10,10 +10,10 @@ The seed is idempotent — running it twice produces the same dataset and never
duplicates rows. Demo IDs are kept in sync with `app/api/v1/dev_auth.py` so the
auth bypass and the seeded data agree on who "[email protected]" is.
"""
+
from __future__ import annotations
import asyncio
-import os
import random
import sys
import uuid
@@ -107,8 +107,7 @@ _SYNTHETIC_INCIDENTS: list[tuple[str, list[str], list[str], str]] = [
"APT credential harvesting campaign targeting {user}",
["TA0006", "TA0003"],
["T1110.001", "T1547.001"],
- "Brute-force spray from {ip} against {user}. Successful login established persistence "
- "via registry Run key. IoCs match APT28 TTPs.",
+ "Brute-force spray from {ip} against {user}. Successful login established persistence via registry Run key. IoCs match APT28 TTPs.",
),
(
"Insider threat: bulk download of PII by {user}",
@@ -121,8 +120,7 @@ _SYNTHETIC_INCIDENTS: list[tuple[str, list[str], list[str], str]] = [
"Supply chain compromise: malicious npm package on {host}",
["TA0001", "TA0002"],
["T1195.001", "T1059.007"],
- "Compromised npm package `event-stream` installed by CI pipeline on {host}. "
- "Post-install hook executed reverse shell to {ip}.",
+ "Compromised npm package `event-stream` installed by CI pipeline on {host}. Post-install hook executed reverse shell to {ip}.",
),
(
"Kerberoasting and lateral movement from {host}",
@@ -156,8 +154,7 @@ _SYNTHETIC_INCIDENTS: list[tuple[str, list[str], list[str], str]] = [
"Identity provider compromise: SAML golden-ticket on {user}",
["TA0006", "TA0007"],
["T1606.002", "T1087.002"],
- "Forged SAML assertion detected. Attacker pivoted to Azure AD as {user}. "
- "Account enumeration across O365 tenant followed.",
+ "Forged SAML assertion detected. Attacker pivoted to Azure AD as {user}. Account enumeration across O365 tenant followed.",
),
(
"Cryptominer dropped via vulnerable Docker socket on {host}",
@@ -184,50 +181,43 @@ _SYNTHETIC_INCIDENTS: list[tuple[str, list[str], list[str], str]] = [
"Active Directory DCSync from non-DC host {host}",
["TA0006", "TA0004"],
["T1003.006", "T1078.002"],
- "Replication rights abused from workstation {host}. All domain NTLM hashes replicated. "
- "Matches skeleton key attack preparation.",
+ "Replication rights abused from workstation {host}. All domain NTLM hashes replicated. Matches skeleton key attack preparation.",
),
(
"Container escape via privileged pod on {host}",
["TA0004", "TA0007"],
["T1611", "T1082"],
- "Kubernetes privileged pod created by rogue service account. "
- "cgroup escape to host namespace. Node file system accessed from pod.",
+ "Kubernetes privileged pod created by rogue service account. cgroup escape to host namespace. Node file system accessed from pod.",
),
(
"Firmware implant detected on {host} UEFI partition",
["TA0003", "TA0005"],
["T1542.001", "T1027.002"],
- "UEFI secure-boot violation alert. Unknown module in firmware image. "
- "Matches MosaicRegressor UEFI implant signatures.",
+ "UEFI secure-boot violation alert. Unknown module in firmware image. Matches MosaicRegressor UEFI implant signatures.",
),
(
"Watering-hole attack: internal wiki delivering drive-by exploit",
["TA0001", "TA0002"],
["T1189", "T1203"],
- "Internal Confluence page injected with malicious JS. "
- "Visitor {user} on {host} exploited via CVE-2024-1234 browser vulnerability.",
+ "Internal Confluence page injected with malicious JS. Visitor {user} on {host} exploited via CVE-2024-1234 browser vulnerability.",
),
(
"Malicious USB autorun on air-gapped {host}",
["TA0001", "TA0009"],
["T1091", "T1005"],
- "USB device inserted on air-gapped system {host}. "
- "AutoRun executed Python stager. Sensitive documents staged for exfiltration.",
+ "USB device inserted on air-gapped system {host}. AutoRun executed Python stager. Sensitive documents staged for exfiltration.",
),
(
"OAuth consent phishing targeting {user}'s Microsoft account",
["TA0001", "TA0006"],
["T1528", "T1550.001"],
- "Malicious OAuth app granted Mail.Read and Files.Read.All to {user}. "
- "Inbox rules created to forward emails silently to attacker.",
+ "Malicious OAuth app granted Mail.Read and Files.Read.All to {user}. Inbox rules created to forward emails silently to attacker.",
),
(
"Memory-only implant (fileless) executed in {host} process",
["TA0002", "TA0005"],
["T1055.012", "T1620"],
- "Process hollowing detected: svchost.exe replaced with Cobalt Strike beacon. "
- "No disk artefacts. IoC matches CS watermark 0x5A4D.",
+ "Process hollowing detected: svchost.exe replaced with Cobalt Strike beacon. No disk artefacts. IoC matches CS watermark 0x5A4D.",
),
(
"DNS tunnelling for data exfiltration from {host}",
@@ -239,13 +229,22 @@ _SYNTHETIC_INCIDENTS: list[tuple[str, list[str], list[str], str]] = [
]
_HOSTS = [
- "WIN-FIN-DB01", "WIN-PROD-WEB02", "MAC-SARAH-LT", "LIN-K8S-NODE-03",
- "WIN-HR-DESKTOP", "DC01.corp.aisoc.dev", "WIN-DEVOPS-LT",
+ "WIN-FIN-DB01",
+ "WIN-PROD-WEB02",
+ "MAC-SARAH-LT",
+ "LIN-K8S-NODE-03",
+ "WIN-HR-DESKTOP",
+ "DC01.corp.aisoc.dev",
+ "WIN-DEVOPS-LT",
]
_USERS = [
]
@@ -255,17 +254,12 @@ def _random_ip() -> str:
def _pick_techniques(k: int = 2) -> list[dict]:
chosen = _rng.sample(_TECHNIQUES, k=k)
- return [
- {"tactic": t[1], "tactic_id": t[0], "technique": t[3], "technique_id": t[2]}
- for t in chosen
- ]
+ return [{"tactic": t[1], "tactic_id": t[0], "technique": t[3], "technique_id": t[2]} for t in chosen]
def _make_alert(tenant_id: uuid.UUID, when: datetime) -> Alert:
severity = _rng.choices(_SEVERITIES, weights=[15, 30, 35, 20])[0]
- status = _rng.choices(
- _STATUSES, weights=[40, 20, 15, 20, 5]
- )[0]
+ status = _rng.choices(_STATUSES, weights=[40, 20, 15, 20, 5])[0]
src_name, src_cat = _rng.choice(_SOURCES)
host = _rng.choice(_HOSTS)
user = _rng.choice(_USERS)
@@ -328,14 +322,16 @@ def _make_case(tenant_id: uuid.UUID, idx: int, when: datetime, alert_ids: list[u
return Case(
tenant_id=tenant_id,
case_number=f"CASE-{1000 + idx:04d}",
- title=_rng.choice([
- "Coordinated brute-force campaign across SaaS apps",
- "Possible data exfiltration via cloud storage",
- "Endpoint compromise — finance workstation",
- "Suspected insider threat — HR records access",
- "Ransomware precursor — staging activity detected",
- "Phishing wave targeting engineering team",
- ]),
+ title=_rng.choice(
+ [
+ "Coordinated brute-force campaign across SaaS apps",
+ "Possible data exfiltration via cloud storage",
+ "Endpoint compromise — finance workstation",
+ "Suspected insider threat — HR records access",
+ "Ransomware precursor — staging activity detected",
+ "Phishing wave targeting engineering team",
+ ]
+ ),
description="Auto-generated demo case. Multiple alerts correlated by entity and ATT&CK technique.",
status=status,
priority=severity,
@@ -417,9 +413,7 @@ async def _ensure_user(session, tenant: Tenant) -> User:
async def _seed_connectors(session, tenant: Tenant) -> int:
- result = await session.execute(
- select(Connector).where(Connector.tenant_id == tenant.id)
- )
+ result = await session.execute(select(Connector).where(Connector.tenant_id == tenant.id))
if result.scalars().first() is not None:
return 0
rows = _make_connectors(tenant.id)
@@ -441,14 +435,8 @@ def _make_synthetic_case(tenant_id: uuid.UUID, idx: int, when: datetime, alert_i
description = desc_tpl.format(host=host, user=user, ip=ip)
# Build ATT&CK arrays from the scenario's explicit tactic/technique lists
- tactics = [
- {"id": t_id, "name": next((t[1] for t in _TECHNIQUES if t[0] == t_id), t_id)}
- for t_id in tactic_ids
- ]
- techniques = [
- {"id": t_id, "name": next((t[3] for t in _TECHNIQUES if t[2] == t_id), t_id)}
- for t_id in technique_ids
- ]
+ tactics = [{"id": t_id, "name": next((t[1] for t in _TECHNIQUES if t[0] == t_id), t_id)} for t_id in tactic_ids]
+ techniques = [{"id": t_id, "name": next((t[3] for t in _TECHNIQUES if t[2] == t_id), t_id)} for t_id in technique_ids]
severity = _rng.choices(_SEVERITIES, weights=[25, 40, 25, 10])[0]
status = _rng.choices(
@@ -491,7 +479,7 @@ async def _seed_alerts_and_cases(session, tenant: Tenant, *, alert_count: int =
now = datetime.now(UTC)
alerts: list[Alert] = []
- for i in range(alert_count):
+ for _ in range(alert_count):
when = now - timedelta(minutes=_rng.randint(1, 60 * 24 * 14))
alerts.append(_make_alert(tenant.id, when))
session.add_all(alerts)
diff --git a/services/api/app/services/compliance.py b/services/api/app/services/compliance.py
index 9ae1d2e93..4f08f88d6 100644
--- a/services/api/app/services/compliance.py
+++ b/services/api/app/services/compliance.py
@@ -7,7 +7,7 @@ connectors, etc.) and maps it to SOC 2 controls.
from __future__ import annotations
import uuid
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
@@ -15,7 +15,6 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.models.audit import AuditLog
from app.models.compliance import ComplianceControl, ComplianceEvidence
-
# Map SOC 2 control_ids → evidence collection functions
_CONTROL_COLLECTORS: dict[str, str] = {
"CC6.1": "access_controls",
@@ -36,13 +35,11 @@ async def auto_collect_evidence(
) -> list[ComplianceEvidence]:
"""Collect evidence for all controls in a framework from platform data."""
- now = datetime.now(tz=timezone.utc)
+ now = datetime.now(tz=UTC)
thirty_days_ago = now - timedelta(days=30)
# Fetch all controls for framework
- controls_result = await db.execute(
- select(ComplianceControl).where(ComplianceControl.framework == framework)
- )
+ controls_result = await db.execute(select(ComplianceControl).where(ComplianceControl.framework == framework))
controls = controls_result.scalars().all()
# Count audit log events for the tenant in last 30 days
@@ -83,7 +80,9 @@ async def auto_collect_evidence(
},
"CC6.2": {
"title": f"User provisioning audit trail — {access_events} events (30d)",
- "description": "New user access requests are authorized before credentials are issued. RBAC role assignment events confirm process.",
+ "description": (
+ "New user access requests are authorized before credentials are issued. RBAC role assignment events confirm process."
+ ),
"status": "collected" if access_events > 0 else "review",
},
"CC6.3": {
@@ -163,9 +162,7 @@ async def get_soc2_summary(
"""Return a summary of SOC 2 readiness for a tenant."""
# Get all controls
- controls_result = await db.execute(
- select(ComplianceControl).where(ComplianceControl.framework == "soc2")
- )
+ controls_result = await db.execute(select(ComplianceControl).where(ComplianceControl.framework == "soc2"))
controls = controls_result.scalars().all()
total_controls = len(controls)
@@ -174,9 +171,7 @@ async def get_soc2_summary(
# Get latest evidence per control for this tenant
evidence_result = await db.execute(
- select(ComplianceEvidence)
- .where(ComplianceEvidence.tenant_id == tenant_id)
- .order_by(ComplianceEvidence.collected_at.desc())
+ select(ComplianceEvidence).where(ComplianceEvidence.tenant_id == tenant_id).order_by(ComplianceEvidence.collected_at.desc())
)
all_evidence = evidence_result.scalars().all()
@@ -195,7 +190,5 @@ async def get_soc2_summary(
"review": statuses.count("review"),
"approved": statuses.count("approved"),
"rejected": statuses.count("rejected"),
- "pct": round(
- (statuses.count("collected") + statuses.count("approved")) / total_controls * 100
- ),
+ "pct": round((statuses.count("collected") + statuses.count("approved")) / total_controls * 100),
}
diff --git a/services/api/app/services/graph_service.py b/services/api/app/services/graph_service.py
index 7c701f80c..dba2c067b 100644
--- a/services/api/app/services/graph_service.py
+++ b/services/api/app/services/graph_service.py
@@ -2,12 +2,12 @@
Graph service: entity node management, attack-path queries, blast-radius traversal.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
import logging
-import uuid
-from datetime import datetime, timezone
-from typing import Any, Optional
+from datetime import UTC, datetime
+from typing import Any
from app.db.neo4j import get_session
@@ -16,6 +16,7 @@ logger = logging.getLogger(__name__)
# ─── Entity Upsert Helpers ────────────────────────────────────────────────────
+
async def upsert_host(
host_id: str,
hostname: str,
@@ -33,7 +34,7 @@ async def upsert_host(
"ip_address": ip_address,
"os": os,
"criticality": criticality,
- "updated_at": datetime.now(timezone.utc).isoformat(),
+ "updated_at": datetime.now(UTC).isoformat(),
**(extra or {}),
}
cypher = """
@@ -61,7 +62,7 @@ async def upsert_user(
"email": email,
"department": department,
"risk_score": risk_score,
- "updated_at": datetime.now(timezone.utc).isoformat(),
+ "updated_at": datetime.now(UTC).isoformat(),
}
cypher = """
MERGE (u:User {id: $id})
@@ -88,7 +89,7 @@ async def upsert_alert_node(
"tenant_id": tenant_id,
"title": title,
"severity": severity,
- "created_at": datetime.now(timezone.utc).isoformat(),
+ "created_at": datetime.now(UTC).isoformat(),
}
async with get_session() as s:
@@ -124,7 +125,7 @@ async def upsert_alert_node(
)
# Link to MITRE Techniques
- for technique_id in (mitre_techniques or []):
+ for technique_id in mitre_techniques or []:
await s.run(
"""
MATCH (a:Alert {id: $alert_id})
@@ -134,11 +135,11 @@ async def upsert_alert_node(
""",
alert_id=alert_id,
technique_id=technique_id,
- ts=datetime.now(timezone.utc).isoformat(),
+ ts=datetime.now(UTC).isoformat(),
)
# Link to IOCs
- for ioc_val in (ioc_values or []):
+ for ioc_val in ioc_values or []:
await s.run(
"""
MATCH (a:Alert {id: $alert_id})
@@ -149,7 +150,7 @@ async def upsert_alert_node(
alert_id=alert_id,
ioc_val=ioc_val,
tenant_id=tenant_id,
- ts=datetime.now(timezone.utc).isoformat(),
+ ts=datetime.now(UTC).isoformat(),
)
@@ -166,7 +167,7 @@ async def upsert_case_node(
"tenant_id": tenant_id,
"title": title,
"severity": severity,
- "created_at": datetime.now(timezone.utc).isoformat(),
+ "created_at": datetime.now(UTC).isoformat(),
}
async with get_session() as s:
@@ -176,7 +177,7 @@ async def upsert_case_node(
props=case_props,
)
- for alert_id in (alert_ids or []):
+ for alert_id in alert_ids or []:
await s.run(
"""
MATCH (c:Case {id: $case_id})
@@ -204,7 +205,7 @@ async def upsert_ioc(
"malicious": malicious,
"confidence": confidence,
"tags": tags or [],
- "updated_at": datetime.now(timezone.utc).isoformat(),
+ "updated_at": datetime.now(UTC).isoformat(),
}
async with get_session() as s:
await s.run(
@@ -216,6 +217,7 @@ async def upsert_ioc(
# ─── Query Helpers ────────────────────────────────────────────────────────────
+
async def get_attack_path(case_id: str, tenant_id: str, max_depth: int = 6) -> dict[str, Any]:
"""
Traverse the graph from a Case node to reconstruct the attack path.
@@ -268,7 +270,8 @@ async def _attack_path_fallback(case_id: str, tenant_id: str) -> dict[str, Any]:
# Get case
r = await s.run(
"MATCH (c:Case {id: $id, tenant_id: $tid}) RETURN c",
- id=case_id, tid=tenant_id,
+ id=case_id,
+ tid=tenant_id,
)
rec = await r.single()
if not rec:
@@ -380,9 +383,7 @@ async def get_blast_radius(entity_id: str, entity_type: str, tenant_id: str, hop
}
-async def _blast_radius_fallback(
- entity_id: str, entity_type: str, tenant_id: str, hops: int
-) -> dict[str, Any]:
+async def _blast_radius_fallback(entity_id: str, entity_type: str, tenant_id: str, hops: int) -> dict[str, Any]:
"""Simple 2-hop blast radius without APOC."""
label_map = {"host": "Host", "user": "User", "ioc": "IOC", "alert": "Alert"}
label = label_map.get(entity_type.lower(), "Host")
diff --git a/services/api/app/services/plugin_manager.py b/services/api/app/services/plugin_manager.py
index 6c606d6ad..b3333d23a 100644
--- a/services/api/app/services/plugin_manager.py
+++ b/services/api/app/services/plugin_manager.py
@@ -13,15 +13,34 @@ Plugin layout expected on disk (two manifest formats accepted):
aisoc-plugin.json ← legacy manifest format (still supported)
plugin.py
+Signature verification
+----------------------
+Every plugin executes arbitrary Python via ``importlib`` + ``exec_module``,
+so the loader treats unsigned code as hostile. Each plugin directory may
+ship a ``plugin.sig`` file whose contents are an Ed25519 signature, in
+hex, over a canonical JSON encoding of the manifest plus the SHA-256
+hash of every ``*.py`` file in the directory (sorted). The signing key
+must match one of the PEM-encoded public keys in
+``PLUGIN_TRUSTED_KEYS_DIR``.
+
+The ``PLUGIN_TRUST_MODE`` setting controls behaviour:
+
+* ``strict`` – default in prod. Refuse to load unsigned/invalid plugins.
+* ``warn`` – load but mark ``signature_status`` as ``unsigned`` or
+ ``invalid`` and log loudly.
+* ``disabled`` – skip checks entirely (dev sandbox only).
+
OCI image support (oras pull):
Pass an OCI reference to install_from_oci() — the manager pulls the image
layer via the ORAS CLI (must be installed) and extracts it into PLUGINS_DIR.
MIT License — AiSOC (open-source AI Security Operations Center)
"""
+
from __future__ import annotations
import asyncio
+import hashlib
import importlib.util
import inspect
import json
@@ -38,6 +57,7 @@ import structlog
try:
import yaml as _yaml
+
_YAML_AVAILABLE = True
except ModuleNotFoundError:
_YAML_AVAILABLE = False
@@ -48,18 +68,29 @@ logger = structlog.get_logger(__name__)
_MANIFEST_YAML = "plugin.yaml"
_MANIFEST_JSON = "aisoc-plugin.json"
+# Signature artifact written by ``aisoc plugin sign`` — hex-encoded Ed25519
+# over the canonical manifest+source digest.
+_SIGNATURE_FILE = "plugin.sig"
+
# v4.0 expanded valid types (connector|enricher|responder|detection|widget) + legacy
VALID_PLUGIN_TYPES = {"enricher", "action", "connector", "responder", "detection", "widget"}
+# Trust modes for the signature gate (see module docstring).
+TRUST_MODE_STRICT = "strict"
+TRUST_MODE_WARN = "warn"
+TRUST_MODE_DISABLED = "disabled"
+_VALID_TRUST_MODES = {TRUST_MODE_STRICT, TRUST_MODE_WARN, TRUST_MODE_DISABLED}
+
# ── Manifest model ────────────────────────────────────────────────────────────
+
@dataclass
class PluginManifest:
id: str
name: str
version: str
- plugin_type: str # connector | enricher | responder | detection | widget | action
+ plugin_type: str # connector | enricher | responder | detection | widget | action
description: str = ""
author: str = ""
tags: list[str] = field(default_factory=list)
@@ -68,10 +99,10 @@ class PluginManifest:
homepage: str = ""
license: str = ""
min_aisoc_version: str = ""
- oci_image: str = "" # optional OCI image reference (registry/repo:tag)
+ oci_image: str = "" # optional OCI image reference (registry/repo:tag)
@classmethod
- def from_dict(cls, data: dict[str, Any]) -> "PluginManifest":
+ def from_dict(cls, data: dict[str, Any]) -> PluginManifest:
return cls(
id=data["id"],
name=data["name"],
@@ -90,14 +121,21 @@ class PluginManifest:
# ── Loaded plugin record ──────────────────────────────────────────────────────
+
@dataclass
class LoadedPlugin:
manifest: PluginManifest
plugin_dir: Path
- instance: Any # the Plugin() object from plugin.py
+ instance: Any # the Plugin() object from plugin.py
loaded_at: float = field(default_factory=time.time)
error: str | None = None
enabled: bool = True
+ # ``verified`` – signature verified against a key in PLUGIN_TRUSTED_KEYS_DIR.
+ # ``invalid`` – signature present but verification failed (warn mode only).
+ # ``unsigned`` – no signature artifact present (warn mode only).
+ # ``skipped`` – trust mode was ``disabled``; no check was attempted.
+ signature_status: str = "skipped"
+ signing_key_id: str | None = None
@property
def plugin_id(self) -> str:
@@ -106,8 +144,10 @@ class LoadedPlugin:
# ── PluginError ───────────────────────────────────────────────────────────────
+
class PluginError(Exception):
"""Raised when plugin operations fail."""
+
def __init__(self, plugin_id: str, message: str) -> None:
super().__init__(f"[{plugin_id}] {message}")
self.plugin_id = plugin_id
@@ -115,6 +155,7 @@ class PluginError(Exception):
# ── Manifest helpers ──────────────────────────────────────────────────────────
+
def _read_manifest(plugin_dir: Path) -> dict[str, Any]:
"""
Read the plugin manifest from plugin.yaml (preferred) or aisoc-plugin.json (legacy).
@@ -146,8 +187,152 @@ def _read_manifest(plugin_dir: Path) -> dict[str, Any]:
raise PluginError(plugin_dir.name, f"no manifest found (expected {_MANIFEST_YAML} or {_MANIFEST_JSON})")
+# ── Signature helpers ─────────────────────────────────────────────────────────
+
+
+def _canonical_plugin_digest(plugin_dir: Path, manifest_raw: dict[str, Any]) -> bytes:
+ """Return the 32-byte SHA-256 digest of the canonical signing payload.
+
+ The signing payload is a canonical JSON document that includes:
+
+ * the manifest dict with the ``signature``/``trust`` keys removed,
+ * a sorted map of ``{relative_path: sha256_hex}`` for every ``*.py``
+ file in the plugin directory tree.
+
+ Any change to the manifest or any source file invalidates the
+ signature, so an attacker cannot swap ``plugin.py`` after signing.
+ The digest is what publishers actually sign with their Ed25519 key.
+ """
+ sanitized = {k: v for k, v in manifest_raw.items() if k not in {"signature", "trust"}}
+
+ file_hashes: dict[str, str] = {}
+ for path in sorted(plugin_dir.rglob("*.py")):
+ if not path.is_file():
+ continue
+ rel = path.relative_to(plugin_dir).as_posix()
+ h = hashlib.sha256()
+ with path.open("rb") as fh:
+ for chunk in iter(lambda: fh.read(65536), b""):
+ h.update(chunk)
+ file_hashes[rel] = h.hexdigest()
+
+ payload = {"manifest": sanitized, "files": file_hashes}
+ canonical = json.dumps(payload, sort_keys=True, separators=(",", ":")).encode("utf-8")
+ return hashlib.sha256(canonical).digest()
+
+
+def _read_signature(plugin_dir: Path) -> bytes | None:
+ """Return the raw signature bytes, or ``None`` if no sig file exists.
+
+ The on-disk format is permissive: callers can ship a binary blob OR a
+ hex-encoded text file (the publishing pipeline uses hex because it
+ survives copy/paste and code review). We try hex first and fall back
+ to raw bytes; an Ed25519 signature is always 64 bytes long, which
+ rules out a text file containing newlines or other whitespace.
+ """
+ sig_path = plugin_dir / _SIGNATURE_FILE
+ if not sig_path.exists():
+ return None
+
+ raw = sig_path.read_bytes()
+
+ # Try hex first — text-only plugin.sig is the documented publishing format.
+ text = raw.strip()
+ try:
+ decoded = bytes.fromhex(text.decode("ascii"))
+ if len(decoded) == 64: # Ed25519 signatures are exactly 64 bytes
+ return decoded
+ except (UnicodeDecodeError, ValueError):
+ pass
+
+ # Otherwise treat the file as raw signature bytes.
+ if len(raw) == 64:
+ return raw
+
+ raise PluginError(
+ plugin_dir.name,
+ f"invalid {_SIGNATURE_FILE}: expected 64 raw bytes or hex-encoded equivalent",
+ )
+
+
+def _load_trusted_keys(keys_dir: Path) -> list[tuple[str, bytes]]:
+ """Load every PEM file under ``keys_dir`` as a candidate trust anchor.
+
+ Returns a list of ``(key_id, pem_bytes)`` tuples, where ``key_id`` is
+ the file stem (e.g. ``aisoc-core`` for ``aisoc-core.pem``). Missing
+ directories yield an empty list — callers decide whether that's fatal
+ based on trust mode.
+ """
+ if not keys_dir.exists() or not keys_dir.is_dir():
+ return []
+ keys: list[tuple[str, bytes]] = []
+ for path in sorted(keys_dir.iterdir()):
+ if path.suffix.lower() not in {".pem", ".pub"}:
+ continue
+ try:
+ keys.append((path.stem, path.read_bytes()))
+ except OSError as exc:
+ logger.warning("could not read trusted key", path=str(path), error=str(exc))
+ return keys
+
+
+def _verify_plugin_signature(
+ plugin_dir: Path,
+ manifest_raw: dict[str, Any],
+ trust_mode: str,
+ trusted_keys: list[tuple[str, bytes]],
+) -> tuple[str, str | None]:
+ """Verify ``plugin.sig`` and return ``(signature_status, key_id)``.
+
+ Raises ``PluginError`` when ``trust_mode='strict'`` and verification
+ fails. In ``warn`` mode failures are logged and the loader proceeds
+ with ``signature_status='unsigned'`` (no sig file) or ``'invalid'``
+ (sig present but verification failed / no trusted keys). In
+ ``disabled`` mode we short-circuit to ``'skipped'`` without touching
+ the disk.
+ """
+ if trust_mode == TRUST_MODE_DISABLED:
+ return "skipped", None
+
+ sig_bytes = _read_signature(plugin_dir)
+ plugin_id = manifest_raw.get("id", plugin_dir.name)
+
+ if sig_bytes is None:
+ msg = f"plugin '{plugin_id}' has no {_SIGNATURE_FILE}"
+ if trust_mode == TRUST_MODE_STRICT:
+ raise PluginError(plugin_id, msg + " (PLUGIN_TRUST_MODE=strict)")
+ logger.warning("plugin unsigned but trust_mode=warn", plugin_id=plugin_id)
+ return "unsigned", None
+
+ if not trusted_keys:
+ msg = "no trusted public keys configured"
+ if trust_mode == TRUST_MODE_STRICT:
+ raise PluginError(plugin_id, msg + " (set PLUGIN_TRUSTED_KEYS_DIR)")
+ logger.warning("no trusted keys; signature ignored", plugin_id=plugin_id)
+ return "invalid", None
+
+ digest = _canonical_plugin_digest(plugin_dir, manifest_raw)
+
+ # Try every configured key; any single match is enough to trust the plugin.
+ from app.core.security import verify_ed25519_signature # noqa: PLC0415
+
+ for key_id, pem in trusted_keys:
+ try:
+ verify_ed25519_signature(pem, digest, sig_bytes)
+ return "verified", key_id
+ except ValueError:
+ continue
+
+ msg = f"signature did not verify against any of {len(trusted_keys)} trusted keys"
+ if trust_mode == TRUST_MODE_STRICT:
+ raise PluginError(plugin_id, msg)
+ logger.warning("plugin signature invalid but trust_mode=warn", plugin_id=plugin_id)
+ return "invalid", None
+
+
# ── Plugin Manager ────────────────────────────────────────────────────────────
+
class PluginManager:
"""
Singleton-style manager that:
@@ -165,6 +350,7 @@ class PluginManager:
else:
try:
from app.core.config import settings as _cfg # noqa: PLC0415
+
self._plugins_dir = Path(_cfg.AISOC_PLUGINS_DIR)
except Exception:
self._plugins_dir = Path(os.getenv("AISOC_PLUGINS_DIR", "/opt/aisoc/plugins"))
@@ -213,6 +399,10 @@ class PluginManager:
manifest = PluginManifest.from_dict(raw)
+ # ── Signature gate (runs BEFORE we exec arbitrary Python) ────────────
+ trust_mode, trusted_keys = self._trust_config()
+ signature_status, signing_key_id = _verify_plugin_signature(plugin_dir, raw, trust_mode, trusted_keys)
+
plugin_module_path = plugin_dir / "plugin.py"
if not plugin_module_path.exists():
raise PluginError(manifest.id, "plugin.py not found")
@@ -236,7 +426,13 @@ class PluginManager:
instance = plugin_cls()
async with self._lock:
- loaded = LoadedPlugin(manifest=manifest, plugin_dir=plugin_dir, instance=instance)
+ loaded = LoadedPlugin(
+ manifest=manifest,
+ plugin_dir=plugin_dir,
+ instance=instance,
+ signature_status=signature_status,
+ signing_key_id=signing_key_id,
+ )
self._plugins[manifest.id] = loaded
logger.info(
@@ -245,9 +441,32 @@ class PluginManager:
name=manifest.name,
version=manifest.version,
type=manifest.plugin_type,
+ signature_status=signature_status,
+ signing_key_id=signing_key_id,
)
return manifest.id
+ def _trust_config(self) -> tuple[str, list[tuple[str, bytes]]]:
+ """Resolve PLUGIN_TRUST_MODE + load trusted public keys from disk."""
+ try:
+ from app.core.config import settings as _cfg # noqa: PLC0415
+
+ mode = (_cfg.PLUGIN_TRUST_MODE or TRUST_MODE_STRICT).lower()
+ keys_dir = Path(_cfg.PLUGIN_TRUSTED_KEYS_DIR)
+ except Exception: # pragma: no cover — config import failures handled in tests
+ mode = os.getenv("PLUGIN_TRUST_MODE", TRUST_MODE_STRICT).lower()
+ keys_dir = Path(os.getenv("PLUGIN_TRUSTED_KEYS_DIR", "/opt/aisoc/plugin-keys"))
+
+ if mode not in _VALID_TRUST_MODES:
+ logger.warning(
+ "invalid PLUGIN_TRUST_MODE; falling back to strict",
+ value=mode,
+ allowed=sorted(_VALID_TRUST_MODES),
+ )
+ mode = TRUST_MODE_STRICT
+
+ return mode, _load_trusted_keys(keys_dir)
+
# ── OCI image install (oras pull) ─────────────────────────────────────────
async def install_from_oci(self, oci_ref: str, plugin_id_hint: str | None = None) -> str:
@@ -306,9 +525,11 @@ class PluginManager:
dest = self._plugins_dir / plugin_id
if dest.exists():
import shutil
+
shutil.rmtree(dest)
import shutil
+
shutil.copytree(extracted, dest)
logger.info("OCI plugin extracted", ref=oci_ref, dest=str(dest))
diff --git a/services/api/app/services/rule_engine.py b/services/api/app/services/rule_engine.py
index d2363d83f..cb4280279 100644
--- a/services/api/app/services/rule_engine.py
+++ b/services/api/app/services/rule_engine.py
@@ -3,6 +3,7 @@ Detection rule execution engine.
Supports: Sigma (pySigma), YARA, KQL (simulated), EQL (simulated).
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
import json
@@ -11,7 +12,6 @@ import re
import time
import uuid
from dataclasses import dataclass, field
-from datetime import datetime, timezone
from enum import Enum
from typing import Any
@@ -30,6 +30,7 @@ class RuleLanguage(str, Enum):
@dataclass
class RuleMatch:
"""A single rule match result."""
+
rule_id: str
rule_name: str
rule_language: str
@@ -45,6 +46,7 @@ class RuleMatch:
@dataclass
class HuntResult:
"""Result from a threat hunt across events."""
+
hunt_id: str
tenant_id: str
rules_evaluated: int
@@ -58,6 +60,7 @@ class HuntResult:
# ─── Sigma Runner ─────────────────────────────────────────────────────────────
+
def _run_sigma(rule_body: str, events: list[dict[str, Any]]) -> tuple[list[dict], str | None]:
"""
Execute a Sigma rule against a list of events.
@@ -65,9 +68,8 @@ def _run_sigma(rule_body: str, events: list[dict[str, Any]]) -> tuple[list[dict]
Returns (matched_events, error_message).
"""
try:
- from sigma.rule import SigmaRule
from sigma.backends.opensearch import OpensearchLuceneBackend
- from sigma.collection import SigmaCollection
+ from sigma.rule import SigmaRule
sigma_rule = SigmaRule.from_yaml(rule_body)
backend = OpensearchLuceneBackend()
@@ -97,6 +99,7 @@ def _sigma_fallback(rule_body: str, events: list[dict[str, Any]]) -> list[dict[s
"""
try:
import yaml
+
rule = yaml.safe_load(rule_body)
detection = rule.get("detection", {})
@@ -122,7 +125,8 @@ def _eval_sigma_detection(detection: dict, flat_event: dict[str, Any]) -> bool:
if isinstance(sel_def, dict):
match = True
for field_name, field_val in sel_def.items():
- field_lower = field_name.lower().rstrip("|contains|startswith|endswith")
+ # Strip Sigma modifier suffixes (e.g. `cmdline|contains` -> `cmdline`).
+ field_lower = field_name.lower().split("|", 1)[0]
ev_val = str(flat_event.get(field_lower, "")).lower()
if isinstance(field_val, list):
hit = any(str(v).lower() in ev_val for v in field_val)
@@ -159,6 +163,7 @@ def _eval_condition(condition: str, selections: dict[str, bool]) -> bool:
# ─── YARA Runner ──────────────────────────────────────────────────────────────
+
def _run_yara(rule_body: str, events: list[dict[str, Any]]) -> tuple[list[dict], str | None]:
"""
Execute a YARA rule against event payloads.
@@ -166,6 +171,7 @@ def _run_yara(rule_body: str, events: list[dict[str, Any]]) -> tuple[list[dict],
"""
try:
import yara
+
compiled = yara.compile(source=rule_body)
matched = []
for event in events:
@@ -198,6 +204,7 @@ def _event_to_bytes(event: dict[str, Any]) -> bytes:
# ─── KQL / EQL Runner (simulated) ─────────────────────────────────────────────
+
def _run_kql(rule_body: str, events: list[dict[str, Any]]) -> tuple[list[dict], str | None]:
"""
Simplified KQL evaluator.
@@ -239,6 +246,7 @@ def _run_eql(rule_body: str, events: list[dict[str, Any]]) -> tuple[list[dict],
# ─── Lucene Query Matcher ─────────────────────────────────────────────────────
+
def _lucene_match(query: str, event: dict[str, Any]) -> bool:
"""Simple Lucene query evaluator for field:value pairs."""
flat = _flatten_dict(event)
@@ -261,6 +269,7 @@ def _flatten_dict(d: dict[str, Any], prefix: str = "") -> dict[str, Any]:
# ─── Main Execute Function ────────────────────────────────────────────────────
+
def execute_rule(
rule_id: str,
rule_name: str,
@@ -334,6 +343,7 @@ def _severity_score(severity: str) -> float:
# ─── Hunt Runner ─────────────────────────────────────────────────────────────
+
async def run_hunt(
tenant_id: str,
rules: list[dict[str, Any]],
@@ -367,14 +377,16 @@ async def run_hunt(
if result.matched:
rules_matched += 1
matched_events.extend(result.match_details.get("matched_events", []))
- match_summary.append({
- "rule_id": result.rule_id,
- "rule_name": result.rule_name,
- "severity": result.severity,
- "match_count": len(result.match_details.get("matched_events", [])),
- "score": result.score,
- "execution_time_ms": result.execution_time_ms,
- })
+ match_summary.append(
+ {
+ "rule_id": result.rule_id,
+ "rule_name": result.rule_name,
+ "severity": result.severity,
+ "match_count": len(result.match_details.get("matched_events", [])),
+ "score": result.score,
+ "execution_time_ms": result.execution_time_ms,
+ }
+ )
elapsed_ms = (time.monotonic() - start) * 1000
diff --git a/services/api/app/services/sla.py b/services/api/app/services/sla.py
index ad53c29fa..529d0ad4d 100644
--- a/services/api/app/services/sla.py
+++ b/services/api/app/services/sla.py
@@ -11,23 +11,19 @@ import uuid
from datetime import datetime, timedelta
from typing import Any
-from sqlalchemy import func, select, text
+from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.sla import AlertSLAEvent, TenantSLAConfig
-
# ---------------------------------------------------------------------------
# Internal helpers
# ---------------------------------------------------------------------------
-async def _get_sla_configs(
- db: AsyncSession, tenant_id: uuid.UUID
-) -> dict[str, dict[str, int]]:
+
+async def _get_sla_configs(db: AsyncSession, tenant_id: uuid.UUID) -> dict[str, dict[str, int]]:
"""Return {severity: {mttd_target, mttr_target, mttc_target}} for the tenant."""
- rows = await db.execute(
- select(TenantSLAConfig).where(TenantSLAConfig.tenant_id == tenant_id)
- )
+ rows = await db.execute(select(TenantSLAConfig).where(TenantSLAConfig.tenant_id == tenant_id))
configs = rows.scalars().all()
return {
c.severity: {
@@ -87,6 +83,7 @@ def _compute_durations(
# Public API
# ---------------------------------------------------------------------------
+
async def compute_sla_metrics(
db: AsyncSession,
tenant_id: uuid.UUID,
@@ -113,9 +110,9 @@ async def compute_sla_metrics(
per_severity: dict[str, dict[str, Any]] = {}
default_targets = {
"critical": {"mttd_target": 15, "mttr_target": 60, "mttc_target": 120},
- "high": {"mttd_target": 30, "mttr_target": 120, "mttc_target": 240},
- "medium": {"mttd_target": 60, "mttr_target": 240, "mttc_target": 480},
- "low": {"mttd_target": 120, "mttr_target": 480, "mttc_target": 1440},
+ "high": {"mttd_target": 30, "mttr_target": 120, "mttc_target": 240},
+ "medium": {"mttd_target": 60, "mttr_target": 240, "mttc_target": 480},
+ "low": {"mttd_target": 120, "mttr_target": 480, "mttc_target": 1440},
}
for sev in ["critical", "high", "medium", "low"]:
diff --git a/services/api/pyproject.toml b/services/api/pyproject.toml
index b63f32b7d..7a9bcc56c 100644
--- a/services/api/pyproject.toml
+++ b/services/api/pyproject.toml
@@ -48,12 +48,8 @@ mypy = "^1.10.0"
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
-[tool.ruff]
-line-length = 100
-target-version = "py311"
-
-[tool.ruff.lint]
-select = ["E", "W", "F", "I", "B", "C4", "UP"]
+# Ruff config lives in the repo-root `ruff.toml` so the CI gate is consistent
+# across services. Don't add `[tool.ruff]` here — it would shadow the root.
[tool.mypy]
python_version = "3.11"
diff --git a/services/api/tests/test_demo_mode.py b/services/api/tests/test_demo_mode.py
index 3e506d327..02db2c87c 100644
--- a/services/api/tests/test_demo_mode.py
+++ b/services/api/tests/test_demo_mode.py
@@ -18,11 +18,10 @@ from __future__ import annotations
from typing import Any
import pytest
-from fastapi import FastAPI
-from fastapi.testclient import TestClient
-
from app.core.config import settings
from app.middleware.demo_mode import DemoModeMiddleware
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
@pytest.fixture
diff --git a/services/api/tests/test_graphql.py b/services/api/tests/test_graphql.py
index 6fea6ebec..51135c025 100644
--- a/services/api/tests/test_graphql.py
+++ b/services/api/tests/test_graphql.py
@@ -4,19 +4,18 @@ These tests exercise the schema in isolation (no real DB required) by
using strawberry's execute_sync / execute helpers directly. Integration
tests against a live Postgres are out of scope here; those live in e2e/.
"""
+
from __future__ import annotations
-import pytest
import strawberry
-
# ─── Helpers ──────────────────────────────────────────────────────────────────
def _make_schema():
"""Import the schema lazily so env vars don't need to be set at collection time."""
- import sys
import os
+ import sys
sys.path.insert(0, ".")
os.environ.setdefault("ENVIRONMENT", "development")
@@ -61,8 +60,7 @@ class TestGraphQLSchema:
assert page_type in self.sdl, f"{page_type} missing from SDL"
def test_query_fields_present(self):
- for field in ("alert", "alerts", "case", "cases", "detectionRules", "connectors",
- "playbooks", "playbookRuns", "socStats"):
+ for field in ("alert", "alerts", "case", "cases", "detectionRules", "connectors", "playbooks", "playbookRuns", "socStats"):
assert field in self.sdl, f"Query field '{field}' missing from SDL"
def test_introspection_query(self):
@@ -78,3 +76,131 @@ class TestGraphQLSchema:
def test_soc_stats_fields(self):
assert "totalAlerts" in self.sdl or "total_alerts" in self.sdl
assert "openCases" in self.sdl or "open_cases" in self.sdl
+
+
+# ─── Tenant isolation tests ───────────────────────────────────────────────────
+
+
+class _FakeUser:
+ def __init__(self, tenant_id):
+ self.tenant_id = tenant_id
+
+
+class _FakeInfo:
+ def __init__(self, user):
+ self.context = {"user": user, "db": None}
+
+
+class TestGraphQLTenantIsolation:
+ """Resolver helpers must scope every query to the caller's tenant.
+
+ These tests exercise the ``_scope`` and ``_tenant_id`` helpers directly
+ so we get fast, deterministic assertions without booting Postgres.
+ A cross-tenant fetch must produce a ``WHERE tenant_id = <caller>`` clause
+ that no row from another tenant can satisfy.
+ """
+
+ def setup_method(self):
+ _make_schema() # ensure env defaults are in place
+
+ @staticmethod
+ def _uuid_in_sql(tid, compiled: str) -> bool:
+ """SQLAlchemy may compile UUIDs as 32-char hex (no dashes) or canonical 36-char form."""
+ s = str(tid)
+ return s in compiled or s.replace("-", "") in compiled
+
+ def test_scope_adds_tenant_filter(self):
+ import uuid as _uuid
+
+ from app.graphql.query import _scope
+ from app.models.alert import Alert
+ from sqlalchemy import select
+
+ tid = _uuid.uuid4()
+ info = _FakeInfo(_FakeUser(tid))
+ stmt = _scope(select(Alert), info, Alert)
+ compiled = str(stmt.compile(compile_kwargs={"literal_binds": True}))
+ assert self._uuid_in_sql(tid, compiled)
+ assert "tenant_id" in compiled.lower()
+
+ def test_scope_blocks_when_no_user(self):
+ from app.graphql.query import _scope
+ from app.models.alert import Alert
+ from sqlalchemy import select
+
+ info = _FakeInfo(user=None)
+ # _scope should still emit a tenant_id filter (with the zero UUID)
+ # so the query returns no rows rather than leaking.
+ stmt = _scope(select(Alert), info, Alert)
+ compiled = str(stmt.compile(compile_kwargs={"literal_binds": True}))
+ assert "tenant_id" in compiled.lower()
+ assert (
+ "00000000-0000-0000-0000-000000000000" in compiled
+ or "00000000000000000000000000000000" in compiled
+ )
+
+ def test_scope_isolates_tenants(self):
+ """A query scoped to tenant A must not match rows from tenant B."""
+ import uuid as _uuid
+
+ from app.graphql.query import _scope
+ from app.models.alert import Alert
+ from sqlalchemy import select
+
+ tenant_a = _uuid.uuid4()
+ tenant_b = _uuid.uuid4()
+
+ info_a = _FakeInfo(_FakeUser(tenant_a))
+ stmt_a = _scope(select(Alert), info_a, Alert)
+ compiled_a = str(stmt_a.compile(compile_kwargs={"literal_binds": True}))
+
+ assert self._uuid_in_sql(tenant_a, compiled_a)
+ assert not self._uuid_in_sql(tenant_b, compiled_a)
+
+
+class TestGraphiQLDisabledOutsideDev:
+ """GraphiQL UI must be off in non-development environments."""
+
+ def test_graphiql_disabled_in_production(self, monkeypatch):
+ # Force a fresh import path with ENVIRONMENT=production
+ import importlib
+ import sys
+
+ monkeypatch.setenv("ENVIRONMENT", "production")
+ monkeypatch.setenv("SECRET_KEY", "test-secret-key-at-least-32-bytes!!")
+ monkeypatch.setenv("DATABASE_URL", "postgresql+asyncpg://x:x@localhost/x")
+
+ # Drop cached config + schema so they pick up the new env.
+ # Note: pop the package too — `app.graphql/__init__.py` re-exports the
+ # `schema` *instance* under the name ``schema``, which would shadow the
+ # submodule on subsequent imports.
+ for mod in (
+ "app.core.config",
+ "app.graphql",
+ "app.graphql.schema",
+ ):
+ sys.modules.pop(mod, None)
+
+ # Import the *module* directly (not via re-export from the package)
+ schema_mod = importlib.import_module("app.graphql.schema")
+ importlib.reload(schema_mod)
+ assert schema_mod._graphql_ide is None
+
+ def test_graphiql_enabled_in_development(self, monkeypatch):
+ import importlib
+ import sys
+
+ monkeypatch.setenv("ENVIRONMENT", "development")
+ monkeypatch.setenv("SECRET_KEY", "test-secret-key-at-least-32-bytes!!")
+ monkeypatch.setenv("DATABASE_URL", "postgresql+asyncpg://x:x@localhost/x")
+
+ for mod in (
+ "app.core.config",
+ "app.graphql",
+ "app.graphql.schema",
+ ):
+ sys.modules.pop(mod, None)
+
+ schema_mod = importlib.import_module("app.graphql.schema")
+ importlib.reload(schema_mod)
+ assert schema_mod._graphql_ide == "graphiql"
diff --git a/services/api/tests/test_plugin_manager.py b/services/api/tests/test_plugin_manager.py
index 5553c07dc..ae7d8ae66 100644
--- a/services/api/tests/test_plugin_manager.py
+++ b/services/api/tests/test_plugin_manager.py
@@ -6,6 +6,7 @@ PluginManager against temporary on-disk plugin fixtures.
MIT License — AiSOC (open-source AI Security Operations Center)
"""
+
from __future__ import annotations
import json
@@ -13,17 +14,31 @@ import textwrap
from pathlib import Path
import pytest
-
from app.services.plugin_manager import (
- LoadedPlugin,
PluginError,
PluginManager,
PluginManifest,
)
+# ── shared fixtures ───────────────────────────────────────────────────────────
+
+
[email protected](autouse=True)
+def _disable_plugin_trust(monkeypatch):
+ """Default to ``PLUGIN_TRUST_MODE=disabled`` for legacy tests.
+
+ Signature-specific tests opt in to ``strict``/``warn`` explicitly via
+ their own monkeypatch. Without this fixture, the in-place strict
+ default would refuse every unsigned fixture plugin.
+ """
+ from app.core.config import settings # noqa: PLC0415
+
+ monkeypatch.setattr(settings, "PLUGIN_TRUST_MODE", "disabled", raising=False)
+
# ── helpers ───────────────────────────────────────────────────────────────────
+
def _write_plugin(
base: Path,
name: str,
@@ -61,6 +76,7 @@ def _write_plugin(
# ── manifest / load ───────────────────────────────────────────────────────────
+
@pytest.mark.asyncio
async def test_discover_finds_valid_plugin(tmp_path):
_write_plugin(tmp_path, "my-enricher")
@@ -120,9 +136,7 @@ async def test_discover_skips_missing_required_field(tmp_path):
async def test_discover_skips_invalid_plugin_type(tmp_path):
d = tmp_path / "weird"
d.mkdir()
- (d / "aisoc-plugin.json").write_text(
- json.dumps({"id": "x", "name": "X", "version": "1", "plugin_type": "magic"})
- )
+ (d / "aisoc-plugin.json").write_text(json.dumps({"id": "x", "name": "X", "version": "1", "plugin_type": "magic"}))
(d / "plugin.py").write_text("class Plugin:\n pass\n")
mgr = PluginManager(plugins_dir=tmp_path)
loaded = await mgr.discover()
@@ -133,9 +147,7 @@ async def test_discover_skips_invalid_plugin_type(tmp_path):
async def test_discover_skips_missing_plugin_py(tmp_path):
d = tmp_path / "no-code"
d.mkdir()
- (d / "aisoc-plugin.json").write_text(
- json.dumps({"id": "x", "name": "X", "version": "1", "plugin_type": "enricher"})
- )
+ (d / "aisoc-plugin.json").write_text(json.dumps({"id": "x", "name": "X", "version": "1", "plugin_type": "enricher"}))
mgr = PluginManager(plugins_dir=tmp_path)
loaded = await mgr.discover()
assert loaded == []
@@ -145,9 +157,7 @@ async def test_discover_skips_missing_plugin_py(tmp_path):
async def test_discover_skips_plugin_without_plugin_class(tmp_path):
d = tmp_path / "no-class"
d.mkdir()
- (d / "aisoc-plugin.json").write_text(
- json.dumps({"id": "x", "name": "X", "version": "1", "plugin_type": "enricher"})
- )
+ (d / "aisoc-plugin.json").write_text(json.dumps({"id": "x", "name": "X", "version": "1", "plugin_type": "enricher"}))
(d / "plugin.py").write_text("# no Plugin class here\n")
mgr = PluginManager(plugins_dir=tmp_path)
loaded = await mgr.discover()
@@ -156,6 +166,7 @@ async def test_discover_skips_plugin_without_plugin_class(tmp_path):
# ── list / get ────────────────────────────────────────────────────────────────
+
@pytest.mark.asyncio
async def test_list_plugins(tmp_path):
_write_plugin(tmp_path, "enricher-a", "enricher")
@@ -180,6 +191,7 @@ async def test_get_plugin_not_found(tmp_path):
# ── enable / disable ──────────────────────────────────────────────────────────
+
@pytest.mark.asyncio
async def test_enable_disable(tmp_path):
_write_plugin(tmp_path, "toggleable")
@@ -209,6 +221,7 @@ async def test_disable_missing_raises(tmp_path):
# ── unload / reload ───────────────────────────────────────────────────────────
+
@pytest.mark.asyncio
async def test_unload(tmp_path):
_write_plugin(tmp_path, "temp-plugin")
@@ -229,7 +242,7 @@ async def test_unload_missing_raises(tmp_path):
@pytest.mark.asyncio
async def test_reload(tmp_path):
- plugin_dir = _write_plugin(tmp_path, "reloadable")
+ _write_plugin(tmp_path, "reloadable")
mgr = PluginManager(plugins_dir=tmp_path)
await mgr.discover()
@@ -251,6 +264,7 @@ async def test_reload_missing_raises(tmp_path):
# ── invocation ────────────────────────────────────────────────────────────────
+
@pytest.mark.asyncio
async def test_run_enricher(tmp_path):
_write_plugin(tmp_path, "ip-enrich", "enricher")
@@ -375,10 +389,9 @@ async def test_run_non_dict_result_wrapped(tmp_path):
# ── PluginManifest dataclass ──────────────────────────────────────────────────
+
def test_plugin_manifest_from_dict_minimal():
- m = PluginManifest.from_dict(
- {"id": "a", "name": "A", "version": "1", "plugin_type": "enricher"}
- )
+ m = PluginManifest.from_dict({"id": "a", "name": "A", "version": "1", "plugin_type": "enricher"})
assert m.id == "a"
assert m.tags == []
assert m.config_schema == {}
@@ -400,3 +413,226 @@ def test_plugin_manifest_from_dict_full():
assert m.author == "Alice"
assert len(m.tags) == 2
assert m.config_schema["type"] == "object"
+
+
+# ── signature gate ────────────────────────────────────────────────────────────
+#
+# These tests exercise the Ed25519 signature path that protects ``_load_plugin``
+# from executing arbitrary unsigned ``plugin.py`` files. The gate has three
+# trust modes:
+# strict – unsigned/invalid → load is refused
+# warn – unsigned/invalid → load proceeds, marked ``signature_status``
+# ``unsigned`` / ``invalid``
+# disabled – signature checks skipped entirely
+#
+# We materialize a real Ed25519 keypair at runtime, compute the canonical
+# digest the loader expects, and write the signature next to the manifest.
+
+
+def _signed_plugin(
+ base: Path,
+ name: str,
+ keys_dir: Path,
+ *,
+ sign_with_wrong_key: bool = False,
+ corrupt_signature: bool = False,
+) -> tuple[Path, str]:
+ """Create a plugin signed by a fresh trusted keypair and return its dir+id.
+
+ The trusted public key is written to ``keys_dir`` so the loader will
+ pick it up via ``PLUGIN_TRUSTED_KEYS_DIR``. If ``sign_with_wrong_key``
+ is set, the signature is produced by an *untrusted* key whose public
+ component is never registered. ``corrupt_signature`` flips a byte in
+ the produced signature so verification fails.
+ """
+ from app.services.plugin_manager import _canonical_plugin_digest # noqa: PLC0415
+ from cryptography.hazmat.primitives import serialization # noqa: PLC0415
+ from cryptography.hazmat.primitives.asymmetric.ed25519 import ( # noqa: PLC0415
+ Ed25519PrivateKey,
+ )
+
+ plugin_dir = _write_plugin(base, name)
+
+ # Keys
+ trusted_priv = Ed25519PrivateKey.generate()
+ trusted_pub_pem = trusted_priv.public_key().public_bytes(
+ encoding=serialization.Encoding.PEM,
+ format=serialization.PublicFormat.SubjectPublicKeyInfo,
+ )
+ keys_dir.mkdir(parents=True, exist_ok=True)
+ (keys_dir / "trusted.pem").write_bytes(trusted_pub_pem)
+
+ # Whoever actually signs the manifest
+ if sign_with_wrong_key:
+ signer = Ed25519PrivateKey.generate() # not in keys_dir
+ else:
+ signer = trusted_priv
+
+ raw = json.loads((plugin_dir / "aisoc-plugin.json").read_text())
+ digest = _canonical_plugin_digest(plugin_dir, raw)
+ sig = signer.sign(digest)
+ if corrupt_signature:
+ sig = bytes([sig[0] ^ 0xFF]) + sig[1:]
+
+ # Hex is the documented on-disk format produced by ``aisoc plugin sign``.
+ (plugin_dir / "plugin.sig").write_text(sig.hex())
+ return plugin_dir, f"test.{name}"
+
+
+def trusted_keys_dir(tmp_path):
+ """A scratch directory used as ``PLUGIN_TRUSTED_KEYS_DIR``."""
+ d = tmp_path / "keys"
+ d.mkdir()
+ return d
+
+
+def _set_trust(monkeypatch, mode: str, keys_dir: Path) -> None:
+ from app.core.config import settings # noqa: PLC0415
+
+ monkeypatch.setattr(settings, "PLUGIN_TRUST_MODE", mode, raising=False)
+ monkeypatch.setattr(settings, "PLUGIN_TRUSTED_KEYS_DIR", str(keys_dir), raising=False)
+
+
+class TestPluginSignatureGate:
+ """``_load_plugin`` must verify Ed25519 signatures before executing code."""
+
+ @pytest.mark.asyncio
+ async def test_strict_refuses_unsigned_plugin(self, tmp_path, trusted_keys_dir, monkeypatch):
+ _set_trust(monkeypatch, "strict", trusted_keys_dir)
+ _write_plugin(tmp_path, "unsigned-plugin")
+
+ mgr = PluginManager(plugins_dir=tmp_path)
+ loaded = await mgr.discover()
+ # discover() swallows PluginError; the plugin must NOT be registered.
+ assert loaded == []
+ assert mgr.get_plugin("test.unsigned-plugin") is None
+
+ @pytest.mark.asyncio
+ async def test_strict_refuses_invalid_signature(self, tmp_path, trusted_keys_dir, monkeypatch):
+ _set_trust(monkeypatch, "strict", trusted_keys_dir)
+ _signed_plugin(
+ tmp_path,
+ "tampered-plugin",
+ trusted_keys_dir,
+ corrupt_signature=True,
+ )
+
+ mgr = PluginManager(plugins_dir=tmp_path)
+ loaded = await mgr.discover()
+ assert loaded == []
+ assert mgr.get_plugin("test.tampered-plugin") is None
+
+ @pytest.mark.asyncio
+ async def test_strict_refuses_untrusted_signer(self, tmp_path, trusted_keys_dir, monkeypatch):
+ _set_trust(monkeypatch, "strict", trusted_keys_dir)
+ _signed_plugin(
+ tmp_path,
+ "stranger-plugin",
+ trusted_keys_dir,
+ sign_with_wrong_key=True,
+ )
+
+ mgr = PluginManager(plugins_dir=tmp_path)
+ loaded = await mgr.discover()
+ assert loaded == []
+ assert mgr.get_plugin("test.stranger-plugin") is None
+
+ @pytest.mark.asyncio
+ async def test_strict_accepts_valid_signature(self, tmp_path, trusted_keys_dir, monkeypatch):
+ _set_trust(monkeypatch, "strict", trusted_keys_dir)
+ _, plugin_id = _signed_plugin(tmp_path, "good-plugin", trusted_keys_dir)
+
+ mgr = PluginManager(plugins_dir=tmp_path)
+ loaded = await mgr.discover()
+ assert plugin_id in loaded
+ record = mgr.get_plugin(plugin_id)
+ assert record is not None
+ assert record.signature_status == "verified"
+ assert record.signing_key_id is not None
+
+ @pytest.mark.asyncio
+ async def test_warn_loads_unsigned_with_status(self, tmp_path, trusted_keys_dir, monkeypatch):
+ _set_trust(monkeypatch, "warn", trusted_keys_dir)
+ _write_plugin(tmp_path, "warn-plugin")
+
+ mgr = PluginManager(plugins_dir=tmp_path)
+ loaded = await mgr.discover()
+ assert loaded == ["test.warn-plugin"]
+ record = mgr.get_plugin("test.warn-plugin")
+ assert record is not None
+ assert record.signature_status == "unsigned"
+ assert record.signing_key_id is None
+
+ @pytest.mark.asyncio
+ async def test_warn_marks_invalid_signature(self, tmp_path, trusted_keys_dir, monkeypatch):
+ _set_trust(monkeypatch, "warn", trusted_keys_dir)
+ _signed_plugin(
+ tmp_path,
+ "warn-tampered",
+ trusted_keys_dir,
+ corrupt_signature=True,
+ )
+
+ mgr = PluginManager(plugins_dir=tmp_path)
+ loaded = await mgr.discover()
+ assert loaded == ["test.warn-tampered"]
+ record = mgr.get_plugin("test.warn-tampered")
+ assert record is not None
+ assert record.signature_status == "invalid"
+
+ @pytest.mark.asyncio
+ async def test_disabled_skips_verification(self, tmp_path, trusted_keys_dir, monkeypatch):
+ _set_trust(monkeypatch, "disabled", trusted_keys_dir)
+ _write_plugin(tmp_path, "skip-plugin")
+
+ mgr = PluginManager(plugins_dir=tmp_path)
+ loaded = await mgr.discover()
+ assert loaded == ["test.skip-plugin"]
+ record = mgr.get_plugin("test.skip-plugin")
+ assert record is not None
+ assert record.signature_status == "skipped"
+
+ @pytest.mark.asyncio
+ async def test_invalid_trust_mode_falls_back_to_strict(self, tmp_path, trusted_keys_dir, monkeypatch):
+ # An unrecognised mode must NOT silently downgrade to ``disabled`` —
+ # the loader treats it as ``strict`` and refuses unsigned plugins.
+ _set_trust(monkeypatch, "yolo", trusted_keys_dir)
+ _write_plugin(tmp_path, "bogus-mode-plugin")
+
+ mgr = PluginManager(plugins_dir=tmp_path)
+ loaded = await mgr.discover()
+ assert loaded == []
+
+ def test_canonical_digest_is_stable(self, tmp_path):
+ """Same content → same digest, regardless of file ordering."""
+ from app.services.plugin_manager import _canonical_plugin_digest # noqa: PLC0415
+
+ d = tmp_path / "stable"
+ d.mkdir()
+ (d / "aisoc-plugin.json").write_text(json.dumps({"id": "x", "name": "X", "version": "1", "plugin_type": "enricher"}))
+ (d / "plugin.py").write_text("class Plugin: pass\n")
+ # Adding the optional .sig file must NOT change the digest — it is
+ # the artefact we are signing, not part of the input.
+ (d / "plugin.sig").write_bytes(b"placeholder")
+
+ raw = json.loads((d / "aisoc-plugin.json").read_text())
+ digest_a = _canonical_plugin_digest(d, raw)
+ digest_b = _canonical_plugin_digest(d, raw)
+ assert digest_a == digest_b
+ # 32-byte SHA-256
+ assert len(digest_a) == 32
+
+ def test_canonical_digest_changes_when_code_changes(self, tmp_path):
+ from app.services.plugin_manager import _canonical_plugin_digest # noqa: PLC0415
+
+ d = tmp_path / "mutating"
+ d.mkdir()
+ (d / "aisoc-plugin.json").write_text(json.dumps({"id": "x", "name": "X", "version": "1", "plugin_type": "enricher"}))
+ (d / "plugin.py").write_text("class Plugin: pass\n")
+ raw = json.loads((d / "aisoc-plugin.json").read_text())
+ before = _canonical_plugin_digest(d, raw)
+
+ (d / "plugin.py").write_text("class Plugin:\n POISONED = True\n")
+ after = _canonical_plugin_digest(d, raw)
+ assert before != after
diff --git a/services/api/tests/test_security_defaults.py b/services/api/tests/test_security_defaults.py
new file mode 100644
index 000000000..9c0606add
--- /dev/null
+++ b/services/api/tests/test_security_defaults.py
@@ -0,0 +1,150 @@
+"""Tests for the P0.3 hardening: insecure-default warnings + /metrics auth.
+
+The audit finding was that the API silently shipped:
+
+* a placeholder ``SECRET_KEY``
+* an unauthenticated ``/metrics`` endpoint (Prometheus text exposed to
+ anyone who could reach the pod)
+* ``PLUGIN_TRUST_MODE=disabled`` with no operator-visible warning
+
+These tests pin the corrective behavior so future refactors cannot
+regress it without a failing test.
+"""
+
+from __future__ import annotations
+
+import pytest
+from app.core.config import (
+ INSECURE_SECRET_KEY_DEFAULTS,
+ Settings,
+ warn_if_insecure_defaults,
+)
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+
+
+def _make_settings(**overrides) -> Settings:
+ """Construct a Settings instance without reading the host .env file."""
+ base: dict = {
+ "ENVIRONMENT": "production",
+ "SECRET_KEY": "a" * 64,
+ "METRICS_TOKEN": "long-random-token",
+ "PLUGIN_TRUST_MODE": "strict",
+ }
+ base.update(overrides)
+ # ``_env_file=None`` skips .env discovery so test runs are deterministic
+ # regardless of the developer's local config.
+ return Settings(_env_file=None, **base)
+
+
+# ─── warn_if_insecure_defaults ─────────────────────────────────────────────
+
+
+def test_secret_key_placeholder_in_prod_warns():
+ bad = next(iter(INSECURE_SECRET_KEY_DEFAULTS))
+ s = _make_settings(SECRET_KEY=bad)
+ msgs = warn_if_insecure_defaults(s)
+ assert any("SECRET_KEY" in m for m in msgs)
+
+
+def test_secret_key_placeholder_in_dev_still_warns():
+ bad = next(iter(INSECURE_SECRET_KEY_DEFAULTS))
+ s = _make_settings(ENVIRONMENT="development", SECRET_KEY=bad)
+ msgs = warn_if_insecure_defaults(s)
+ # SECRET_KEY check is environment-independent: we always want to nag.
+ assert any("SECRET_KEY" in m for m in msgs)
+
+
+def test_empty_metrics_token_in_prod_warns():
+ s = _make_settings(METRICS_TOKEN="")
+ msgs = warn_if_insecure_defaults(s)
+ assert any("METRICS_TOKEN" in m for m in msgs)
+
+
+def test_empty_metrics_token_in_dev_does_not_warn():
+ s = _make_settings(ENVIRONMENT="development", METRICS_TOKEN="")
+ msgs = warn_if_insecure_defaults(s)
+ assert not any("METRICS_TOKEN" in m for m in msgs)
+
+
+def test_plugin_trust_disabled_in_prod_warns():
+ s = _make_settings(PLUGIN_TRUST_MODE="disabled")
+ msgs = warn_if_insecure_defaults(s)
+ assert any("PLUGIN_TRUST_MODE" in m for m in msgs)
+
+
+def test_plugin_trust_disabled_in_dev_does_not_warn():
+ s = _make_settings(ENVIRONMENT="development", PLUGIN_TRUST_MODE="disabled")
+ msgs = warn_if_insecure_defaults(s)
+ assert not any("PLUGIN_TRUST_MODE" in m for m in msgs)
+
+
+def test_clean_prod_settings_emit_no_warnings():
+ s = _make_settings()
+ msgs = warn_if_insecure_defaults(s)
+ assert msgs == []
+
+
+# ─── /metrics auth gate ────────────────────────────────────────────────────
+
+
+def metrics_app(monkeypatch):
+ """A FastAPI app with just the /metrics endpoint, isolated from real settings."""
+ import app.main as main_module
+
+ # Bypass the lifespan (DB, Neo4j, plugin discovery) by mounting the
+ # endpoint directly on a fresh app — we are only testing the auth gate.
+ test_app = FastAPI()
+ test_app.add_api_route("/metrics", main_module.metrics, methods=["GET"])
+ return test_app, main_module
+
+
+def test_metrics_requires_token_when_configured(metrics_app, monkeypatch):
+ test_app, main_module = metrics_app
+ monkeypatch.setattr(main_module.settings, "METRICS_TOKEN", "s3cret")
+ monkeypatch.setattr(main_module.settings, "ENVIRONMENT", "production")
+
+ client = TestClient(test_app)
+ assert client.get("/metrics").status_code == 401
+ assert client.get("/metrics", headers={"Authorization": "Bearer wrong"}).status_code == 401
+ ok = client.get("/metrics", headers={"Authorization": "Bearer s3cret"})
+ assert ok.status_code == 200
+ assert ok.headers["content-type"].startswith("text/plain")
+
+
+def test_metrics_open_in_dev_when_token_unset(metrics_app, monkeypatch):
+ test_app, main_module = metrics_app
+ monkeypatch.setattr(main_module.settings, "METRICS_TOKEN", "")
+ monkeypatch.setattr(main_module.settings, "ENVIRONMENT", "development")
+
+ client = TestClient(test_app)
+ assert client.get("/metrics").status_code == 200
+
+
+def test_metrics_refused_in_prod_when_token_unset(metrics_app, monkeypatch):
+ test_app, main_module = metrics_app
+ monkeypatch.setattr(main_module.settings, "METRICS_TOKEN", "")
+ monkeypatch.setattr(main_module.settings, "ENVIRONMENT", "production")
+
+ client = TestClient(test_app)
+ resp = client.get("/metrics")
+ assert resp.status_code == 401
+ assert "METRICS_TOKEN" in resp.text
+
+
+def test_metrics_token_compare_is_constant_time(metrics_app, monkeypatch):
+ """Sanity check: we wired up hmac.compare_digest, not ==.
+
+ We can't observe wall-clock timing reliably in CI, so we just assert
+ that the exact-match path returns 200 and a near-match returns 401.
+ The implementation comment in main.py documents the constant-time
+ intent; this test fails loudly if someone replaces it with ``==``.
+ """
+ test_app, main_module = metrics_app
+ monkeypatch.setattr(main_module.settings, "METRICS_TOKEN", "abc123")
+ monkeypatch.setattr(main_module.settings, "ENVIRONMENT", "production")
+
+ client = TestClient(test_app)
+ assert client.get("/metrics", headers={"Authorization": "Bearer abc123"}).status_code == 200
+ assert client.get("/metrics", headers={"Authorization": "Bearer abc124"}).status_code == 401
diff --git a/services/connectors/app/api/router.py b/services/connectors/app/api/router.py
index 7baf3c47f..0b5f99cac 100644
--- a/services/connectors/app/api/router.py
+++ b/services/connectors/app/api/router.py
@@ -1,9 +1,8 @@
"""
Connectors service REST API.
"""
-from __future__ import annotations
-from typing import Any
+from __future__ import annotations
import structlog
from fastapi import APIRouter, HTTPException
diff --git a/services/connectors/app/connectors/aws_security_hub.py b/services/connectors/app/connectors/aws_security_hub.py
index ab52e940d..a33f15711 100644
--- a/services/connectors/app/connectors/aws_security_hub.py
+++ b/services/connectors/app/connectors/aws_security_hub.py
@@ -2,9 +2,10 @@
AWS Security Hub connector.
Fetches findings from AWS Security Hub via boto3.
"""
+
from __future__ import annotations
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta
from typing import Any
import structlog
@@ -26,13 +27,14 @@ class AWSSecurityHubConnector(BaseConnector):
def _get_client(self):
try:
import boto3
+
kwargs: dict[str, Any] = {"region_name": self._region}
if self._access_key and self._secret_key:
kwargs["aws_access_key_id"] = self._access_key
kwargs["aws_secret_access_key"] = self._secret_key
return boto3.client("securityhub", **kwargs)
- except ImportError:
- raise RuntimeError("boto3 is required for AWS Security Hub connector. Install it with: pip install boto3")
+ except ImportError as exc:
+ raise RuntimeError("boto3 is required for AWS Security Hub connector. Install it with: pip install boto3") from exc
async def test_connection(self) -> dict[str, Any]:
try:
@@ -44,7 +46,7 @@ class AWSSecurityHubConnector(BaseConnector):
async def fetch_alerts(self, since_seconds: int = 300) -> list[dict[str, Any]]:
client = self._get_client()
- since = (datetime.now(timezone.utc) - timedelta(seconds=since_seconds)).strftime("%Y-%m-%dT%H:%M:%SZ")
+ since = (datetime.now(UTC) - timedelta(seconds=since_seconds)).strftime("%Y-%m-%dT%H:%M:%SZ")
findings = []
paginator = client.get_paginator("get_findings")
diff --git a/services/connectors/app/connectors/base.py b/services/connectors/app/connectors/base.py
index 3eef06516..cb9b5405a 100644
--- a/services/connectors/app/connectors/base.py
+++ b/services/connectors/app/connectors/base.py
@@ -1,6 +1,7 @@
"""
Base connector interface for all integrations.
"""
+
from __future__ import annotations
from abc import ABC, abstractmethod
diff --git a/services/connectors/app/connectors/crowdstrike.py b/services/connectors/app/connectors/crowdstrike.py
index a5dd294e1..947d793df 100644
--- a/services/connectors/app/connectors/crowdstrike.py
+++ b/services/connectors/app/connectors/crowdstrike.py
@@ -2,9 +2,10 @@
CrowdStrike Falcon connector.
Fetches detections from the CrowdStrike Falcon API.
"""
+
from __future__ import annotations
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta
from typing import Any
import httpx
@@ -53,7 +54,7 @@ class CrowdStrikeConnector(BaseConnector):
if not self._access_token:
await self._authenticate()
- since = (datetime.now(timezone.utc) - timedelta(seconds=since_seconds)).isoformat()
+ since = (datetime.now(UTC) - timedelta(seconds=since_seconds)).isoformat()
headers = {"Authorization": f"Bearer {self._access_token}"}
async with httpx.AsyncClient(timeout=30.0) as client:
@@ -92,17 +93,18 @@ class CrowdStrikeConnector(BaseConnector):
severity_map = {1: "low", 2: "medium", 3: "high", 4: "critical"}
severity = severity_map.get(raw.get("max_severity", 2), "medium")
+ behaviors = raw.get("behaviors") or [{}]
+ title = behaviors[0].get("display_name", "CrowdStrike Detection")
+
return {
"source": self.connector_id,
"external_id": raw.get("detection_id", ""),
- "title": raw.get("behaviors", [{}])[0].get("display_name", "CrowdStrike Detection") if raw.get("behaviors") else "CrowdStrike Detection",
+ "title": title,
"description": f"CrowdStrike detection on {raw.get('device', {}).get('hostname', 'unknown')}",
"severity": severity,
"src_ip": raw.get("device", {}).get("external_ip"),
"hostname": raw.get("device", {}).get("hostname"),
- "mitre_techniques": [
- b.get("technique_id", "") for b in raw.get("behaviors", []) if b.get("technique_id")
- ],
+ "mitre_techniques": [b.get("technique_id", "") for b in raw.get("behaviors", []) if b.get("technique_id")],
"raw_event": raw,
"created_at": raw.get("created_timestamp"),
}
diff --git a/services/connectors/app/connectors/microsoft_sentinel.py b/services/connectors/app/connectors/microsoft_sentinel.py
index 0b9368317..ba471c75c 100644
--- a/services/connectors/app/connectors/microsoft_sentinel.py
+++ b/services/connectors/app/connectors/microsoft_sentinel.py
@@ -2,9 +2,10 @@
Microsoft Sentinel connector.
Fetches security incidents from Microsoft Sentinel via Azure REST API.
"""
+
from __future__ import annotations
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta
from typing import Any
import httpx
@@ -65,7 +66,7 @@ class MicrosoftSentinelConnector(BaseConnector):
if not self._access_token:
await self._authenticate()
- since = (datetime.now(timezone.utc) - timedelta(seconds=since_seconds)).strftime("%Y-%m-%dT%H:%M:%SZ")
+ since = (datetime.now(UTC) - timedelta(seconds=since_seconds)).strftime("%Y-%m-%dT%H:%M:%SZ")
api_url = _SENTINEL_API.format(
sub_id=self._subscription_id,
rg=self._resource_group,
diff --git a/services/connectors/app/connectors/okta.py b/services/connectors/app/connectors/okta.py
index 7864ee7b7..14f299f0d 100644
--- a/services/connectors/app/connectors/okta.py
+++ b/services/connectors/app/connectors/okta.py
@@ -2,9 +2,10 @@
Okta connector.
Fetches system log events (suspicious activity, failed logins, MFA push spam).
"""
+
from __future__ import annotations
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta
from typing import Any
import httpx
@@ -43,7 +44,7 @@ class OktaConnector(BaseConnector):
return {"success": False, "connector": self.connector_id, "error": str(exc)}
async def fetch_alerts(self, since_seconds: int = 300) -> list[dict[str, Any]]:
- since = (datetime.now(timezone.utc) - timedelta(seconds=since_seconds)).strftime("%Y-%m-%dT%H:%M:%S.000Z")
+ since = (datetime.now(UTC) - timedelta(seconds=since_seconds)).strftime("%Y-%m-%dT%H:%M:%S.000Z")
# Focus on security-relevant event types
security_events = [
diff --git a/services/connectors/app/connectors/splunk.py b/services/connectors/app/connectors/splunk.py
index 2f920b48e..1707ea5bd 100644
--- a/services/connectors/app/connectors/splunk.py
+++ b/services/connectors/app/connectors/splunk.py
@@ -2,6 +2,7 @@
Splunk connector.
Runs saved searches and fetches notable events from Splunk SIEM.
"""
+
from __future__ import annotations
from typing import Any
@@ -38,7 +39,8 @@ class SplunkConnector(BaseConnector):
params={"output_mode": "json"},
)
resp.raise_for_status()
- return {"success": True, "connector": self.connector_id, "version": resp.json().get("entry", [{}])[0].get("content", {}).get("version")}
+ version = resp.json().get("entry", [{}])[0].get("content", {}).get("version")
+ return {"success": True, "connector": self.connector_id, "version": version}
except Exception as exc:
return {"success": False, "connector": self.connector_id, "error": str(exc)}
@@ -60,6 +62,7 @@ class SplunkConnector(BaseConnector):
# Wait for completion (simple polling)
import asyncio
+
for _ in range(10):
status_resp = await client.get(
f"{self._base_url}/services/search/jobs/{sid}",
diff --git a/services/connectors/app/main.py b/services/connectors/app/main.py
index 7f8aa1949..b0b63ca0f 100644
--- a/services/connectors/app/main.py
+++ b/services/connectors/app/main.py
@@ -1,6 +1,7 @@
"""
AiSOC Connectors Service
"""
+
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
diff --git a/services/fusion/app/core/config.py b/services/fusion/app/core/config.py
index 404a76e07..20e0b7150 100644
--- a/services/fusion/app/core/config.py
+++ b/services/fusion/app/core/config.py
@@ -1,5 +1,5 @@
-from pydantic_settings import BaseSettings
from pydantic import Field
+from pydantic_settings import BaseSettings
class Settings(BaseSettings):
@@ -10,25 +10,15 @@ class Settings(BaseSettings):
environment: str = Field(default="development", alias="ENVIRONMENT")
# Kafka
- kafka_bootstrap_servers: str = Field(
- default="localhost:9092", alias="KAFKA_BOOTSTRAP_SERVERS"
- )
- kafka_topic_alerts_raw: str = Field(
- default="aisoc.alerts.raw", alias="KAFKA_TOPIC_ALERTS_RAW"
- )
- kafka_topic_alerts_fused: str = Field(
- default="aisoc.alerts.fused", alias="KAFKA_TOPIC_ALERTS_FUSED"
- )
- kafka_consumer_group: str = Field(
- default="aisoc-fusion-consumer", alias="KAFKA_CONSUMER_GROUP"
- )
+ kafka_bootstrap_servers: str = Field(default="localhost:9092", alias="KAFKA_BOOTSTRAP_SERVERS")
+ kafka_topic_alerts_raw: str = Field(default="aisoc.alerts.raw", alias="KAFKA_TOPIC_ALERTS_RAW")
+ kafka_topic_alerts_fused: str = Field(default="aisoc.alerts.fused", alias="KAFKA_TOPIC_ALERTS_FUSED")
+ kafka_consumer_group: str = Field(default="aisoc-fusion-consumer", alias="KAFKA_CONSUMER_GROUP")
# Redis
redis_url: str = Field(default="redis://localhost:6379/2", alias="REDIS_URL")
dedup_window_seconds: int = Field(default=300, alias="DEDUP_WINDOW_SECONDS")
- correlation_window_seconds: int = Field(
- default=3600, alias="CORRELATION_WINDOW_SECONDS"
- )
+ correlation_window_seconds: int = Field(default=3600, alias="CORRELATION_WINDOW_SECONDS")
# Database
database_url: str = Field(
@@ -37,18 +27,12 @@ class Settings(BaseSettings):
)
# Fusion settings
- dedup_similarity_threshold: float = Field(
- default=0.85, alias="DEDUP_SIMILARITY_THRESHOLD"
- )
+ dedup_similarity_threshold: float = Field(default=0.85, alias="DEDUP_SIMILARITY_THRESHOLD")
max_alerts_per_incident: int = Field(default=500, alias="MAX_ALERTS_PER_INCIDENT")
- incident_auto_close_hours: int = Field(
- default=72, alias="INCIDENT_AUTO_CLOSE_HOURS"
- )
+ incident_auto_close_hours: int = Field(default=72, alias="INCIDENT_AUTO_CLOSE_HOURS")
# Enrichment service
- enrichment_service_url: str = Field(
- default="http://localhost:8082", alias="ENRICHMENT_SERVICE_URL"
- )
+ enrichment_service_url: str = Field(default="http://localhost:8082", alias="ENRICHMENT_SERVICE_URL")
class Config:
env_file = ".env"
diff --git a/services/fusion/app/core/logging.py b/services/fusion/app/core/logging.py
index afa3d818c..04f7484fa 100644
--- a/services/fusion/app/core/logging.py
+++ b/services/fusion/app/core/logging.py
@@ -1,6 +1,8 @@
import logging
import sys
+
import structlog
+
from app.core.config import settings
diff --git a/services/fusion/app/models/alert.py b/services/fusion/app/models/alert.py
index 1678fd48e..34f1cba12 100644
--- a/services/fusion/app/models/alert.py
+++ b/services/fusion/app/models/alert.py
@@ -2,6 +2,7 @@
Alert and Incident models for the Fusion service.
These are Pydantic models used for message processing (not ORM).
"""
+
from __future__ import annotations
import hashlib
@@ -108,7 +109,7 @@ class FusedAlert(BaseModel):
enrichments: dict[str, Any] = Field(default_factory=dict)
# ML scores — populated by MLScorer
- anomaly_score: float = 0.0 # 0.0 = normal, 1.0 = highly anomalous (Isolation Forest)
+ anomaly_score: float = 0.0 # 0.0 = normal, 1.0 = highly anomalous (Isolation Forest)
priority_score: float = 0.0 # 0.0–1.0 priority rank (LightGBM ranker)
fused_at: datetime = Field(default_factory=datetime.utcnow)
diff --git a/services/fusion/app/services/correlator.py b/services/fusion/app/services/correlator.py
index 22252c46a..236e673cc 100644
--- a/services/fusion/app/services/correlator.py
+++ b/services/fusion/app/services/correlator.py
@@ -2,9 +2,9 @@
Alert correlation engine: groups related alerts into incidents using entity and
MITRE ATT&CK context stored in Redis.
"""
-import json
+
from datetime import datetime
-from uuid import UUID, uuid4
+from uuid import uuid4
import redis.asyncio as aioredis
import structlog
@@ -98,9 +98,7 @@ class Correlator:
)
return incident
- async def _merge_alert(
- self, incident: IncidentSummary, alert: RawAlert
- ) -> IncidentSummary:
+ async def _merge_alert(self, incident: IncidentSummary, alert: RawAlert) -> IncidentSummary:
incident.alert_count += 1
incident.alert_ids.append(str(alert.id))
incident.severity = _max_severity(incident.severity, alert.severity)
diff --git a/services/fusion/app/services/deduplicator.py b/services/fusion/app/services/deduplicator.py
index 062e71c93..59f3536fb 100644
--- a/services/fusion/app/services/deduplicator.py
+++ b/services/fusion/app/services/deduplicator.py
@@ -1,7 +1,6 @@
"""
Alert deduplication using fingerprinting and a Redis sliding-window cache.
"""
-from datetime import datetime
import redis.asyncio as aioredis
import structlog
diff --git a/services/fusion/app/services/fusion_engine.py b/services/fusion/app/services/fusion_engine.py
index 69005a418..bfedd6554 100644
--- a/services/fusion/app/services/fusion_engine.py
+++ b/services/fusion/app/services/fusion_engine.py
@@ -1,6 +1,7 @@
"""
Core fusion engine: orchestrates deduplication → correlation → ML scoring.
"""
+
from __future__ import annotations
from uuid import UUID
@@ -60,9 +61,7 @@ class FusionEngine:
# --- Step 2: Correlation ---
correlated, incident = await self._correlator.correlate(alert)
- decision = (
- FusionDecision.CORRELATED if correlated else FusionDecision.NEW_INCIDENT
- )
+ decision = FusionDecision.CORRELATED if correlated else FusionDecision.NEW_INCIDENT
fused = FusedAlert(
id=alert.id,
diff --git a/services/fusion/app/services/ml_scorer.py b/services/fusion/app/services/ml_scorer.py
index ecf05bb96..44b53f1a4 100644
--- a/services/fusion/app/services/ml_scorer.py
+++ b/services/fusion/app/services/ml_scorer.py
@@ -13,10 +13,10 @@ feedback accumulates. They fall back to heuristics if not yet trained.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
import asyncio
-import json
import math
from datetime import datetime
from typing import Any
@@ -70,6 +70,7 @@ def _featurize(alert: RawAlert) -> list[float]:
# Heuristic fallbacks (used when ML models aren't trained yet)
# ─────────────────────────────────────────────────────────────────────────────
+
def _heuristic_anomaly(alert: RawAlert) -> float:
"""Simple heuristic anomaly score: more IOC fields = potentially more anomalous."""
score = 0.0
@@ -88,13 +89,15 @@ def _heuristic_priority(alert: RawAlert) -> float:
"""Simple heuristic priority score based on severity and IOC density."""
sev = _SEVERITY_MAP.get(alert.severity.value if hasattr(alert.severity, "value") else str(alert.severity), 2)
base = sev / 4.0
- ioc_bonus = sum([
- 0.1 if alert.src_ip else 0.0,
- 0.05 if alert.hostname else 0.0,
- 0.05 if alert.username else 0.0,
- 0.1 if alert.file_hash else 0.0,
- 0.05 if alert.mitre_techniques else 0.0,
- ])
+ ioc_bonus = sum(
+ [
+ 0.1 if alert.src_ip else 0.0,
+ 0.05 if alert.hostname else 0.0,
+ 0.05 if alert.username else 0.0,
+ 0.1 if alert.file_hash else 0.0,
+ 0.05 if alert.mitre_techniques else 0.0,
+ ]
+ )
return min(base + ioc_bonus, 1.0)
@@ -115,8 +118,8 @@ class MLScorer:
"""
def __init__(self) -> None:
- self._iso_forest = None # sklearn IsolationForest
- self._lgbm_ranker = None # lightgbm LGBMRanker
+ self._iso_forest = None # sklearn IsolationForest
+ self._lgbm_ranker = None # lightgbm LGBMRanker
self._iso_trained = False
self._lgbm_trained = False
@@ -138,16 +141,12 @@ class MLScorer:
self._feature_buffer.append(features)
if self._iso_trained and self._iso_forest is not None:
- anomaly_score = await asyncio.get_event_loop().run_in_executor(
- None, self._predict_anomaly, features
- )
+ anomaly_score = await asyncio.get_event_loop().run_in_executor(None, self._predict_anomaly, features)
else:
anomaly_score = _heuristic_anomaly(alert)
if self._lgbm_trained and self._lgbm_ranker is not None:
- priority_score = await asyncio.get_event_loop().run_in_executor(
- None, self._predict_priority, features
- )
+ priority_score = await asyncio.get_event_loop().run_in_executor(None, self._predict_priority, features)
else:
priority_score = _heuristic_priority(alert)
@@ -178,12 +177,8 @@ class MLScorer:
features = list(self._feature_buffer)
feedback = list(self._feedback_buffer)
- iso_result = await asyncio.get_event_loop().run_in_executor(
- None, self._train_isolation_forest, features
- )
- lgbm_result = await asyncio.get_event_loop().run_in_executor(
- None, self._train_lgbm_ranker, features, feedback
- )
+ iso_result = await asyncio.get_event_loop().run_in_executor(None, self._train_isolation_forest, features)
+ lgbm_result = await asyncio.get_event_loop().run_in_executor(None, self._train_lgbm_ranker, features, feedback)
return {
"isolation_forest": iso_result,
@@ -210,6 +205,7 @@ class MLScorer:
"""Run IsolationForest prediction synchronously."""
try:
import numpy as np
+
X = np.array(features).reshape(1, -1)
# decision_function: negative = anomaly, normalize to [0,1]
raw = self._iso_forest.decision_function(X)[0]
@@ -225,6 +221,7 @@ class MLScorer:
"""Run LightGBM ranker prediction synchronously."""
try:
import numpy as np
+
X = np.array(features).reshape(1, -1)
raw = self._lgbm_ranker.predict(X)[0]
# Normalize to [0,1] via sigmoid
@@ -269,8 +266,8 @@ class MLScorer:
if len(feedback) < _MIN_FEEDBACK_FOR_RANKER:
return {"status": "skipped", "reason": f"need {_MIN_FEEDBACK_FOR_RANKER} feedback items, have {len(feedback)}"}
try:
- import numpy as np
import lightgbm as lgb
+ import numpy as np
# Build training set from feedback + heuristic features
X_rows = []
@@ -320,10 +317,8 @@ class MLScorer:
n_features = len(self._feature_buffer)
n_feedback = len(self._feedback_buffer)
- should_iso = (not self._iso_trained and n_features >= _MIN_SAMPLES_FOR_TRAINING) or \
- (self._iso_trained and n_features % 500 == 0)
- should_lgbm = (not self._lgbm_trained and n_feedback >= _MIN_FEEDBACK_FOR_RANKER) or \
- (self._lgbm_trained and n_feedback % 100 == 0)
+ should_iso = (not self._iso_trained and n_features >= _MIN_SAMPLES_FOR_TRAINING) or (self._iso_trained and n_features % 500 == 0)
+ should_lgbm = (not self._lgbm_trained and n_feedback >= _MIN_FEEDBACK_FOR_RANKER) or (self._lgbm_trained and n_feedback % 100 == 0)
if should_iso or should_lgbm:
asyncio.create_task(self._retrain_background(should_iso, should_lgbm))
diff --git a/services/fusion/app/workers/consumer.py b/services/fusion/app/workers/consumer.py
index a5ea5f950..ac2e515bc 100644
--- a/services/fusion/app/workers/consumer.py
+++ b/services/fusion/app/workers/consumer.py
@@ -2,12 +2,11 @@
Kafka consumer worker: reads raw alerts, runs them through the fusion engine,
and publishes fused results back to Kafka.
"""
-import asyncio
+
import json
-from aiokafka import AIOKafkaConsumer, AIOKafkaProducer
-from aiokafka.errors import KafkaError
import structlog
+from aiokafka import AIOKafkaConsumer, AIOKafkaProducer
from app.core.config import settings
from app.models.alert import FusionDecision, RawAlert
diff --git a/services/fusion/pyproject.toml b/services/fusion/pyproject.toml
index 5f58b0cea..e693d6561 100644
--- a/services/fusion/pyproject.toml
+++ b/services/fusion/pyproject.toml
@@ -38,9 +38,8 @@ mypy = "^1.8.0"
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
-[tool.ruff]
-target-version = "py311"
-line-length = 100
+# Ruff config lives in the repo-root `ruff.toml` so the CI gate is consistent
+# across services. Don't add `[tool.ruff]` here — it would shadow the root.
[tool.mypy]
python_version = "3.11"
diff --git a/services/fusion/tests/__init__.py b/services/fusion/tests/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/services/fusion/tests/test_alert_model.py b/services/fusion/tests/test_alert_model.py
new file mode 100644
index 000000000..e9e17d041
--- /dev/null
+++ b/services/fusion/tests/test_alert_model.py
@@ -0,0 +1,102 @@
+"""Unit tests for fusion alert models — pure, no infra needed."""
+
+from __future__ import annotations
+
+from uuid import UUID, uuid4
+
+import pytest
+from app.models.alert import AlertSeverity, RawAlert
+
+
+def _alert(**overrides) -> RawAlert:
+ base = {
+ "tenant_id": UUID("11111111-1111-1111-1111-111111111111"),
+ "source": "test-source",
+ "title": "Suspicious login",
+ }
+ base.update(overrides)
+ return RawAlert(**base)
+
+
+class TestRawAlertFingerprint:
+ def test_fingerprint_is_stable_across_calls(self) -> None:
+ a = _alert(src_ip="10.0.0.1", mitre_techniques=["T1078", "T1110"])
+ assert a.fingerprint() == a.fingerprint()
+
+ def test_fingerprint_is_stable_across_instances(self) -> None:
+ a = _alert(src_ip="10.0.0.1", mitre_techniques=["T1078", "T1110"])
+ b = _alert(src_ip="10.0.0.1", mitre_techniques=["T1078", "T1110"])
+ # Different ids/timestamps must NOT change fingerprint
+ assert a.id != b.id
+ assert a.fingerprint() == b.fingerprint()
+
+ def test_fingerprint_ignores_volatile_fields(self) -> None:
+ """`description`, `risk_score`, `tags`, `raw_event` aren't part of the fingerprint."""
+ a = _alert(src_ip="10.0.0.1", description="first", risk_score=0.1, tags=["a"])
+ b = _alert(src_ip="10.0.0.1", description="second", risk_score=0.9, tags=["b"])
+ assert a.fingerprint() == b.fingerprint()
+
+ def test_fingerprint_changes_when_entity_changes(self) -> None:
+ a = _alert(src_ip="10.0.0.1")
+ b = _alert(src_ip="10.0.0.2")
+ assert a.fingerprint() != b.fingerprint()
+
+ def test_fingerprint_changes_when_tenant_changes(self) -> None:
+ a = _alert(tenant_id=uuid4(), src_ip="10.0.0.1")
+ b = _alert(tenant_id=uuid4(), src_ip="10.0.0.1")
+ assert a.fingerprint() != b.fingerprint()
+
+ def test_fingerprint_is_order_independent_for_techniques(self) -> None:
+ a = _alert(src_ip="10.0.0.1", mitre_techniques=["T1078", "T1110"])
+ b = _alert(src_ip="10.0.0.1", mitre_techniques=["T1110", "T1078"])
+ assert a.fingerprint() == b.fingerprint()
+
+ def test_fingerprint_is_64_char_hex(self) -> None:
+ a = _alert()
+ fp = a.fingerprint()
+ assert len(fp) == 64
+ int(fp, 16) # raises if not hex
+
+
+class TestRawAlertCorrelationKey:
+ def test_correlation_key_prefers_src_ip(self) -> None:
+ a = _alert(
+ src_ip="10.0.0.1",
+ hostname="host1",
+ username="alice",
+ mitre_tactics=["initial-access"],
+ )
+ key = a.correlation_key()
+ assert "10.0.0.1" in key
+ assert "host1" not in key
+ assert key.endswith(":initial-access")
+
+ def test_correlation_key_falls_through_to_hostname(self) -> None:
+ a = _alert(hostname="server-42", mitre_tactics=["execution"])
+ key = a.correlation_key()
+ assert "server-42" in key
+ assert key.endswith(":execution")
+
+ def test_correlation_key_unknown_when_no_entity_or_tactic(self) -> None:
+ a = _alert()
+ assert a.correlation_key().endswith(":unknown:unknown")
+
+ def test_correlation_key_includes_tenant(self) -> None:
+ tenant = UUID("22222222-2222-2222-2222-222222222222")
+ a = _alert(tenant_id=tenant, src_ip="1.1.1.1", mitre_tactics=["impact"])
+ assert a.correlation_key().startswith(str(tenant))
+
+
+class TestAlertSeverityEnum:
+ @pytest.mark.parametrize(
+ ("name", "value"),
+ [
+ ("CRITICAL", "critical"),
+ ("HIGH", "high"),
+ ("MEDIUM", "medium"),
+ ("LOW", "low"),
+ ("INFO", "info"),
+ ],
+ )
+ def test_severity_values(self, name: str, value: str) -> None:
+ assert AlertSeverity[name].value == value
diff --git a/services/honeytokens/alembic/env.py b/services/honeytokens/alembic/env.py
index 7e572847d..ccaa2b36d 100644
--- a/services/honeytokens/alembic/env.py
+++ b/services/honeytokens/alembic/env.py
@@ -1,14 +1,15 @@
"""Alembic migration environment for Honeytokens service."""
+
from __future__ import annotations
import asyncio
import os
from logging.config import fileConfig
-from alembic import context
+from app.models.honeytoken import Base
from sqlalchemy.ext.asyncio import create_async_engine
-from app.models.honeytoken import Base
+from alembic import context
config = context.config
diff --git a/services/honeytokens/alembic/versions/0001_initial_honeytoken_tables.py b/services/honeytokens/alembic/versions/0001_initial_honeytoken_tables.py
index 3976f86f8..91faf75d0 100644
--- a/services/honeytokens/alembic/versions/0001_initial_honeytoken_tables.py
+++ b/services/honeytokens/alembic/versions/0001_initial_honeytoken_tables.py
@@ -4,12 +4,14 @@ Revision ID: 0001
Revises:
Create Date: 2026-05-03
"""
+
from __future__ import annotations
import sqlalchemy as sa
-from alembic import op
from sqlalchemy.dialects.postgresql import JSONB, UUID
+from alembic import op
+
revision = "0001"
down_revision = None
branch_labels = None
@@ -33,12 +35,8 @@ def upgrade() -> None:
sa.Column("metadata", JSONB, nullable=False, server_default="{}"),
sa.Column("status", sa.String(16), nullable=False, server_default="active"),
sa.Column("expires_at", sa.DateTime(timezone=True), nullable=True),
- sa.Column(
- "created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False
- ),
- sa.Column(
- "updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False
- ),
+ sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
+ sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
sa.Column("created_by", sa.String(128), nullable=True),
)
op.create_index("ix_honeytokens_tenant_status", "honeytokens", ["tenant_id", "status"])
@@ -76,9 +74,7 @@ def upgrade() -> None:
nullable=False,
),
)
- op.create_index(
- "ix_honeytoken_triggers_triggered_at", "honeytoken_triggers", ["triggered_at"]
- )
+ op.create_index("ix_honeytoken_triggers_triggered_at", "honeytoken_triggers", ["triggered_at"])
def downgrade() -> None:
diff --git a/services/honeytokens/app/api/routes.py b/services/honeytokens/app/api/routes.py
index 0a3f97208..af69c54c2 100644
--- a/services/honeytokens/app/api/routes.py
+++ b/services/honeytokens/app/api/routes.py
@@ -1,19 +1,20 @@
"""FastAPI routes for the Honeytokens service."""
+
from __future__ import annotations
import uuid
-from datetime import datetime, timezone
+from datetime import UTC, datetime
from typing import Annotated, Any
-from fastapi import APIRouter, Depends, HTTPException, Query, Request
+from fastapi import APIRouter, Depends, HTTPException, Query
from pydantic import BaseModel, Field
-from sqlalchemy import select, desc, update
-from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
+from sqlalchemy import desc, select
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app.core.config import settings
from app.models.honeytoken import Honeytoken, HoneytokenTrigger
from app.services.alerting import send_alert
-from app.services.generator import generate_token, TOKEN_GENERATORS
+from app.services.generator import TOKEN_GENERATORS, generate_token
router = APIRouter(prefix="/api/v1/honeytokens", tags=["honeytokens"])
@@ -117,12 +118,7 @@ async def list_tokens(
token_type: str | None = Query(None),
limit: int = Query(50, ge=1, le=500),
) -> list[TokenOut]:
- q = (
- select(Honeytoken)
- .where(Honeytoken.tenant_id == tenant_id)
- .order_by(desc(Honeytoken.created_at))
- .limit(limit)
- )
+ q = select(Honeytoken).where(Honeytoken.tenant_id == tenant_id).order_by(desc(Honeytoken.created_at)).limit(limit)
if status:
q = q.where(Honeytoken.status == status)
if token_type:
@@ -178,7 +174,7 @@ async def webhook_trigger(body: WebhookTriggerPayload, db: DB) -> dict:
if not token:
raise HTTPException(status_code=404, detail="Token not found")
- now = datetime.now(timezone.utc)
+ now = datetime.now(UTC)
trigger = HoneytokenTrigger(
honeytoken_id=token.id,
@@ -211,7 +207,7 @@ async def webhook_trigger(body: WebhookTriggerPayload, db: DB) -> dict:
if alert_sent:
trigger.alert_sent = True
- trigger.alert_sent_at = datetime.now(timezone.utc)
+ trigger.alert_sent_at = datetime.now(UTC)
await db.commit()
return {
diff --git a/services/honeytokens/app/main.py b/services/honeytokens/app/main.py
index 0641dad12..a4be37586 100644
--- a/services/honeytokens/app/main.py
+++ b/services/honeytokens/app/main.py
@@ -1,4 +1,5 @@
"""Honeytokens service — FastAPI application entry point."""
+
from __future__ import annotations
import logging
@@ -13,11 +14,11 @@ from app.core.config import settings
# ---------------------------------------------------------------------------
try:
from opentelemetry import trace
- from opentelemetry.sdk.resources import Resource, SERVICE_NAME
- from opentelemetry.sdk.trace import TracerProvider
- from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
+ from opentelemetry.sdk.resources import SERVICE_NAME, Resource
+ from opentelemetry.sdk.trace import TracerProvider
+ from opentelemetry.sdk.trace.export import BatchSpanProcessor
_resource = Resource.create({SERVICE_NAME: settings.service_name})
_provider = TracerProvider(resource=_resource)
diff --git a/services/honeytokens/app/models/honeytoken.py b/services/honeytokens/app/models/honeytoken.py
index 6f7346c76..ec5992cfb 100644
--- a/services/honeytokens/app/models/honeytoken.py
+++ b/services/honeytokens/app/models/honeytoken.py
@@ -1,10 +1,11 @@
"""SQLAlchemy ORM models for the Honeytokens service."""
+
from __future__ import annotations
import uuid
from datetime import datetime
-from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Integer, String, Text, func
+from sqlalchemy import Boolean, DateTime, Float, ForeignKey, String, Text, func
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
@@ -18,9 +19,7 @@ class Honeytoken(Base):
__tablename__ = "honeytokens"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid()
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid())
tenant_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False, index=True)
# Human-readable name / description
@@ -41,12 +40,8 @@ class Honeytoken(Base):
# active | triggered | expired | revoked
expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), server_default=func.now(), nullable=False
- )
- updated_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)
+ updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
# Who created it
created_by: Mapped[str | None] = mapped_column(String(128), nullable=True)
@@ -57,9 +52,7 @@ class HoneytokenTrigger(Base):
__tablename__ = "honeytoken_triggers"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid()
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid())
honeytoken_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("honeytokens.id", ondelete="CASCADE"),
@@ -83,6 +76,4 @@ class HoneytokenTrigger(Base):
alert_sent: Mapped[bool] = mapped_column(Boolean, nullable=False, server_default="false")
alert_sent_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
- triggered_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), server_default=func.now(), nullable=False
- )
+ triggered_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)
diff --git a/services/honeytokens/app/services/alerting.py b/services/honeytokens/app/services/alerting.py
index d31829352..5d22edc15 100644
--- a/services/honeytokens/app/services/alerting.py
+++ b/services/honeytokens/app/services/alerting.py
@@ -1,4 +1,5 @@
"""First-touch alerting for honeytoken triggers."""
+
from __future__ import annotations
import hashlib
@@ -6,7 +7,7 @@ import hmac
import json
import logging
import uuid
-from datetime import datetime, timezone
+from datetime import datetime
import httpx
diff --git a/services/honeytokens/app/services/generator.py b/services/honeytokens/app/services/generator.py
index 302c70a5b..0281d1f1b 100644
--- a/services/honeytokens/app/services/generator.py
+++ b/services/honeytokens/app/services/generator.py
@@ -1,4 +1,5 @@
"""Honeytoken generator — creates plausible-looking tokens of various types."""
+
from __future__ import annotations
import base64
@@ -7,7 +8,7 @@ import os
import secrets
import string
import uuid
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta
from typing import Any
from app.core.config import settings
@@ -80,7 +81,7 @@ def generate_token(
token_data = TOKEN_GENERATORS[token_type]()
ttl = ttl_days if ttl_days is not None else settings.token_ttl_days
- expires_at = datetime.now(timezone.utc) + timedelta(days=ttl)
+ expires_at = datetime.now(UTC) + timedelta(days=ttl)
return {
"id": uuid.uuid4(),
diff --git a/services/honeytokens/tests/__init__.py b/services/honeytokens/tests/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/services/honeytokens/tests/test_generator.py b/services/honeytokens/tests/test_generator.py
new file mode 100644
index 000000000..47bc5ed3d
--- /dev/null
+++ b/services/honeytokens/tests/test_generator.py
@@ -0,0 +1,118 @@
+"""Unit tests for the honeytoken generator — pure, no DB."""
+
+from __future__ import annotations
+
+import re
+import uuid
+from datetime import UTC, datetime
+
+import pytest
+from app.services.generator import TOKEN_GENERATORS, generate_token
+
+TENANT = uuid.UUID("99999999-9999-9999-9999-999999999999")
+
+
+class TestGenerateTokenValidation:
+ def test_unknown_token_type_raises(self) -> None:
+ with pytest.raises(ValueError, match="Unknown token_type"):
+ generate_token(
+ token_type="not-a-real-type",
+ name="x",
+ description=None,
+ tenant_id=TENANT,
+ created_by=None,
+ )
+
+ @pytest.mark.parametrize("token_type", list(TOKEN_GENERATORS.keys()))
+ def test_each_known_type_returns_valid_record(self, token_type: str) -> None:
+ record = generate_token(
+ token_type=token_type,
+ name="my token",
+ description="desc",
+ tenant_id=TENANT,
+ created_by="alice",
+ )
+ # Required fields
+ assert isinstance(record["id"], uuid.UUID)
+ assert record["tenant_id"] == TENANT
+ assert record["name"] == "my token"
+ assert record["description"] == "desc"
+ assert record["token_type"] == token_type
+ assert record["status"] == "active"
+ assert record["created_by"] == "alice"
+ # Token data is embedded under metadata_.token_data
+ assert "token_data" in record["metadata_"]
+ assert isinstance(record["metadata_"]["token_data"], dict)
+
+
+class TestGenerateTokenUniqueness:
+ def test_two_tokens_have_distinct_ids(self) -> None:
+ a = generate_token("custom", "a", None, TENANT, None)
+ b = generate_token("custom", "a", None, TENANT, None)
+ assert a["id"] != b["id"]
+
+ def test_two_aws_keys_have_distinct_access_keys(self) -> None:
+ a = generate_token("aws_key", "a", None, TENANT, None)
+ b = generate_token("aws_key", "a", None, TENANT, None)
+ assert a["metadata_"]["token_data"]["access_key_id"] != b["metadata_"]["token_data"]["access_key_id"]
+
+
+class TestGenerateTokenShape:
+ def test_aws_key_has_expected_prefix_and_length(self) -> None:
+ record = generate_token("aws_key", "k", None, TENANT, None)
+ td = record["metadata_"]["token_data"]
+ assert td["access_key_id"].startswith("AKIA")
+ assert len(td["access_key_id"]) == 20
+ assert td["secret_access_key"] # non-empty
+
+ def test_url_token_is_https(self) -> None:
+ record = generate_token("url", "u", None, TENANT, None)
+ assert record["metadata_"]["token_data"]["url"].startswith("https://")
+
+ def test_email_token_uses_canary_alias(self) -> None:
+ record = generate_token("email", "e", None, TENANT, None)
+ email = record["metadata_"]["token_data"]["email"]
+ assert email.startswith("canary+")
+ assert email.endswith("@example.com")
+
+ def test_dns_token_is_subdomain(self) -> None:
+ record = generate_token("dns", "d", None, TENANT, None)
+ fqdn = record["metadata_"]["token_data"]["fqdn"]
+ assert fqdn.endswith(".canary.example.com")
+
+ def test_api_key_token_starts_with_sk(self) -> None:
+ record = generate_token("api_key", "a", None, TENANT, None)
+ assert record["metadata_"]["token_data"]["api_key"].startswith("sk-")
+
+ def test_file_token_has_checksum(self) -> None:
+ record = generate_token("file", "f", None, TENANT, None)
+ td = record["metadata_"]["token_data"]
+ assert re.fullmatch(r"[0-9a-f]{64}", td["checksum"])
+ assert td["filename"].endswith(".zip")
+
+
+class TestGenerateTokenTtl:
+ def test_default_ttl_uses_settings(self) -> None:
+ record = generate_token("custom", "c", None, TENANT, None)
+ # default settings.token_ttl_days = 365
+ delta = record["expires_at"] - datetime.now(UTC)
+ assert delta.days >= 360
+ assert delta.days <= 366
+
+ def test_explicit_ttl_overrides_default(self) -> None:
+ record = generate_token("custom", "c", None, TENANT, None, ttl_days=7)
+ delta = record["expires_at"] - datetime.now(UTC)
+ assert 6 <= delta.days <= 7
+
+ def test_zero_ttl_is_respected(self) -> None:
+ record = generate_token("custom", "c", None, TENANT, None, ttl_days=0)
+ delta = record["expires_at"] - datetime.now(UTC)
+ # Same day, slightly negative or near zero
+ assert delta.total_seconds() < 60
+
+ def test_metadata_is_merged_with_token_data(self) -> None:
+ meta = {"label": "phishing-trap", "owner": "soc"}
+ record = generate_token("custom", "c", None, TENANT, None, metadata=meta)
+ assert record["metadata_"]["label"] == "phishing-trap"
+ assert record["metadata_"]["owner"] == "soc"
+ assert "token_data" in record["metadata_"]
diff --git a/services/ingest/internal/config/config.go b/services/ingest/internal/config/config.go
index 7315a7695..600b093df 100644
--- a/services/ingest/internal/config/config.go
+++ b/services/ingest/internal/config/config.go
@@ -36,9 +36,12 @@ type Config struct {
// Load reads configuration from environment variables
func Load() (*Config, error) {
cfg := &Config{
- HTTPPort: mustGetEnvInt("HTTP_PORT", 8080),
- KafkaBrokers: getEnv("KAFKA_BROKERS", "localhost:9092"),
- KafkaTopic: getEnv("KAFKA_TOPIC", "aisoc.raw_events"),
+ HTTPPort: mustGetEnvInt("HTTP_PORT", 8080),
+ // Canonical env var is ``KAFKA_BOOTSTRAP_SERVERS`` (matches
+ // ``.env.example`` and docker-compose). ``KAFKA_BROKERS`` is honored
+ // as a back-compat alias for older deployments.
+ KafkaBrokers: getEnvFallback("KAFKA_BOOTSTRAP_SERVERS", "KAFKA_BROKERS", "localhost:9092"),
+ KafkaTopic: getEnv("KAFKA_TOPIC", "aisoc.raw_events"),
RedisAddr: getEnv("REDIS_ADDR", "localhost:6379"),
DatabaseDSN: getEnv("DATABASE_DSN", ""),
AttckDataPath: getEnv("ATTCK_DATA_PATH", "/data/enterprise-attack.json"),
@@ -74,6 +77,18 @@ func getEnv(key, fallback string) string {
return fallback
}
+// getEnvFallback returns the first non-empty env var from primary, then
+// alternate, otherwise the fallback. Used for backward-compatible env aliases.
+func getEnvFallback(primary, alternate, fallback string) string {
+ if v := os.Getenv(primary); v != "" {
+ return v
+ }
+ if v := os.Getenv(alternate); v != "" {
+ return v
+ }
+ return fallback
+}
+
func mustGetEnvInt(key string, fallback int) int {
v := os.Getenv(key)
if v == "" {
diff --git a/services/purple-team/alembic/env.py b/services/purple-team/alembic/env.py
index 8490b25a9..f169c164d 100644
--- a/services/purple-team/alembic/env.py
+++ b/services/purple-team/alembic/env.py
@@ -1,14 +1,15 @@
"""Alembic migration environment for Purple Team service."""
+
from __future__ import annotations
import asyncio
import os
from logging.config import fileConfig
-from alembic import context
+from app.models.purple_team import Base
from sqlalchemy.ext.asyncio import create_async_engine
-from app.models.purple_team import Base
+from alembic import context
config = context.config
diff --git a/services/purple-team/alembic/versions/0001_initial_purple_team_tables.py b/services/purple-team/alembic/versions/0001_initial_purple_team_tables.py
index a2f4c935c..bfb1dfe06 100644
--- a/services/purple-team/alembic/versions/0001_initial_purple_team_tables.py
+++ b/services/purple-team/alembic/versions/0001_initial_purple_team_tables.py
@@ -4,12 +4,14 @@ Revision ID: 0001
Revises:
Create Date: 2026-05-03
"""
+
from __future__ import annotations
import sqlalchemy as sa
-from alembic import op
from sqlalchemy.dialects.postgresql import JSONB, UUID
+from alembic import op
+
revision = "0001"
down_revision = None
branch_labels = None
@@ -36,14 +38,10 @@ def upgrade() -> None:
sa.Column("executor", sa.String(32), nullable=False),
sa.Column("input_arguments", JSONB, nullable=False, server_default="{}"),
sa.Column("raw_yaml", JSONB, nullable=False, server_default="{}"),
- sa.Column(
- "created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False
- ),
+ sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
)
op.create_index("ix_atomic_tests_tenant_id", "purple_team_atomic_tests", ["tenant_id"])
- op.create_index(
- "ix_atomic_tests_technique_id", "purple_team_atomic_tests", ["technique_id"]
- )
+ op.create_index("ix_atomic_tests_technique_id", "purple_team_atomic_tests", ["technique_id"])
op.create_index(
"ix_atomic_tests_tenant_technique",
"purple_team_atomic_tests",
@@ -74,17 +72,11 @@ def upgrade() -> None:
sa.Column("alert_id", sa.String(64), nullable=True),
sa.Column("detection_latency_seconds", sa.Float, nullable=True),
sa.Column("executed_by", sa.String(128), nullable=True),
- sa.Column(
- "created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False
- ),
+ sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
)
op.create_index("ix_executions_tenant_id", "purple_team_executions", ["tenant_id"])
- op.create_index(
- "ix_executions_technique_id", "purple_team_executions", ["technique_id"]
- )
- op.create_index(
- "ix_executions_created_at", "purple_team_executions", ["created_at"]
- )
+ op.create_index("ix_executions_technique_id", "purple_team_executions", ["technique_id"])
+ op.create_index("ix_executions_created_at", "purple_team_executions", ["created_at"])
op.create_table(
"purple_team_tabletop_sessions",
@@ -102,16 +94,10 @@ def upgrade() -> None:
sa.Column("findings", JSONB, nullable=False, server_default="[]"),
sa.Column("status", sa.String(16), nullable=False, server_default="active"),
sa.Column("created_by", sa.String(128), nullable=True),
- sa.Column(
- "created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False
- ),
- sa.Column(
- "updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False
- ),
- )
- op.create_index(
- "ix_tabletop_sessions_tenant_id", "purple_team_tabletop_sessions", ["tenant_id"]
+ sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
+ sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
)
+ op.create_index("ix_tabletop_sessions_tenant_id", "purple_team_tabletop_sessions", ["tenant_id"])
def downgrade() -> None:
diff --git a/services/purple-team/app/api/routes.py b/services/purple-team/app/api/routes.py
index 0ee0f64a5..d426149d9 100644
--- a/services/purple-team/app/api/routes.py
+++ b/services/purple-team/app/api/routes.py
@@ -1,9 +1,10 @@
"""FastAPI routes for the Purple Team service."""
+
from __future__ import annotations
import logging
import uuid
-from datetime import datetime, timezone
+from datetime import UTC, datetime
from typing import Any
from fastapi import APIRouter, HTTPException, Query
@@ -13,7 +14,7 @@ from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from app.core.config import settings
-from app.models.purple_team import AtomicTest, Base, TabletopSession, TestExecution
+from app.models.purple_team import AtomicTest, TabletopSession, TestExecution
from app.services.atomic_loader import load_atomics
from app.services.caldera_client import CalderaClient
from app.services.coverage import build_coverage_matrix
@@ -250,7 +251,7 @@ async def run_caldera_operation(body: CalderaRunRequest) -> TestExecution:
test_name=body.operation_name,
caldera_operation_id=op.get("id"),
status="running",
- started_at=datetime.now(timezone.utc),
+ started_at=datetime.now(UTC),
executed_by=body.executed_by,
)
session.add(execution)
@@ -288,9 +289,7 @@ async def list_executions(
)
async def report_detection(execution_id: uuid.UUID, body: ReportDetectionRequest) -> TestExecution:
async with _async_session() as session:
- result = await session.execute(
- select(TestExecution).where(TestExecution.id == execution_id)
- )
+ result = await session.execute(select(TestExecution).where(TestExecution.id == execution_id))
ex = result.scalar_one_or_none()
if ex is None:
raise HTTPException(status_code=404, detail="Execution not found")
@@ -309,9 +308,7 @@ async def report_detection(execution_id: uuid.UUID, body: ReportDetectionRequest
@router.get("/api/v1/purple-team/coverage", tags=["Coverage"])
async def get_coverage(tenant_id: uuid.UUID) -> dict:
async with _async_session() as session:
- result = await session.execute(
- select(TestExecution).where(TestExecution.tenant_id == tenant_id)
- )
+ result = await session.execute(select(TestExecution).where(TestExecution.tenant_id == tenant_id))
executions = result.scalars().all()
rows = [
@@ -330,9 +327,7 @@ async def get_coverage(tenant_id: uuid.UUID) -> dict:
# ---------------------------------------------------------------------------
# Tabletop simulator
# ---------------------------------------------------------------------------
- "/api/v1/purple-team/tabletop", response_model=TabletopOut, tags=["Tabletop"]
-)
[email protected]("/api/v1/purple-team/tabletop", response_model=TabletopOut, tags=["Tabletop"])
async def create_tabletop(body: TabletopCreateRequest) -> TabletopSession:
async with _async_session() as session:
ts = TabletopSession(
@@ -349,9 +344,7 @@ async def create_tabletop(body: TabletopCreateRequest) -> TabletopSession:
return ts
- "/api/v1/purple-team/tabletop", response_model=list[TabletopOut], tags=["Tabletop"]
-)
[email protected]("/api/v1/purple-team/tabletop", response_model=list[TabletopOut], tags=["Tabletop"])
async def list_tabletops(
tenant_id: uuid.UUID,
status: str | None = Query(None),
@@ -372,9 +365,7 @@ async def list_tabletops(
)
async def get_tabletop(session_id: uuid.UUID) -> TabletopSession:
async with _async_session() as session:
- result = await session.execute(
- select(TabletopSession).where(TabletopSession.id == session_id)
- )
+ result = await session.execute(select(TabletopSession).where(TabletopSession.id == session_id))
ts = result.scalar_one_or_none()
if ts is None:
raise HTTPException(status_code=404, detail="Session not found")
@@ -388,9 +379,7 @@ async def get_tabletop(session_id: uuid.UUID) -> TabletopSession:
)
async def add_finding(session_id: uuid.UUID, body: TabletopAddFindingRequest) -> TabletopSession:
async with _async_session() as session:
- result = await session.execute(
- select(TabletopSession).where(TabletopSession.id == session_id)
- )
+ result = await session.execute(select(TabletopSession).where(TabletopSession.id == session_id))
ts = result.scalar_one_or_none()
if ts is None:
raise HTTPException(status_code=404, detail="Session not found")
@@ -401,11 +390,11 @@ async def add_finding(session_id: uuid.UUID, body: TabletopAddFindingRequest) ->
"finding": body.finding,
"severity": body.severity,
"owner": body.owner,
- "added_at": datetime.now(timezone.utc).isoformat(),
+ "added_at": datetime.now(UTC).isoformat(),
}
)
ts.findings = findings
- ts.updated_at = datetime.now(timezone.utc)
+ ts.updated_at = datetime.now(UTC)
await session.commit()
await session.refresh(ts)
return ts
@@ -418,14 +407,12 @@ async def add_finding(session_id: uuid.UUID, body: TabletopAddFindingRequest) ->
)
async def complete_tabletop(session_id: uuid.UUID) -> TabletopSession:
async with _async_session() as session:
- result = await session.execute(
- select(TabletopSession).where(TabletopSession.id == session_id)
- )
+ result = await session.execute(select(TabletopSession).where(TabletopSession.id == session_id))
ts = result.scalar_one_or_none()
if ts is None:
raise HTTPException(status_code=404, detail="Session not found")
ts.status = "completed"
- ts.updated_at = datetime.now(timezone.utc)
+ ts.updated_at = datetime.now(UTC)
await session.commit()
await session.refresh(ts)
return ts
diff --git a/services/purple-team/app/core/config.py b/services/purple-team/app/core/config.py
index e01b7519c..827cf6021 100644
--- a/services/purple-team/app/core/config.py
+++ b/services/purple-team/app/core/config.py
@@ -1,13 +1,12 @@
"""Purple Team service configuration."""
+
from __future__ import annotations
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
- model_config = SettingsConfigDict(
- env_prefix="PURPLE_TEAM_", env_file=".env", extra="ignore"
- )
+ model_config = SettingsConfigDict(env_prefix="PURPLE_TEAM_", env_file=".env", extra="ignore")
# Database
database_url: str = "postgresql+asyncpg://aisoc:aisoc@localhost:5432/aisoc"
@@ -21,9 +20,7 @@ class Settings(BaseSettings):
art_atomics_path: str = "/opt/atomic-red-team/atomics"
# ATT&CK STIX bundle URL (for coverage mapping)
- attack_stix_url: str = (
- "https://raw.githubusercontent.com/mitre/cti/master/enterprise-attack/enterprise-attack.json"
- )
+ attack_stix_url: str = "https://raw.githubusercontent.com/mitre/cti/master/enterprise-attack/enterprise-attack.json"
# OTel
otel_endpoint: str = "http://localhost:4317"
diff --git a/services/purple-team/app/main.py b/services/purple-team/app/main.py
index 477f9156f..757e2026f 100644
--- a/services/purple-team/app/main.py
+++ b/services/purple-team/app/main.py
@@ -1,4 +1,5 @@
"""Purple Team service — FastAPI application entry point."""
+
from __future__ import annotations
import logging
@@ -13,11 +14,11 @@ from app.core.config import settings
# ---------------------------------------------------------------------------
try:
from opentelemetry import trace
- from opentelemetry.sdk.resources import Resource, SERVICE_NAME
- from opentelemetry.sdk.trace import TracerProvider
- from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
+ from opentelemetry.sdk.resources import SERVICE_NAME, Resource
+ from opentelemetry.sdk.trace import TracerProvider
+ from opentelemetry.sdk.trace.export import BatchSpanProcessor
_resource = Resource.create({SERVICE_NAME: settings.service_name})
_provider = TracerProvider(resource=_resource)
@@ -44,10 +45,7 @@ from app.api.routes import router # noqa: E402
app = FastAPI(
title="AiSOC Purple Team Service",
- description=(
- "Atomic Red Team execution, Caldera adversary emulation, "
- "ATT&CK coverage heatmap, and tabletop exercise simulator."
- ),
+ description=("Atomic Red Team execution, Caldera adversary emulation, ATT&CK coverage heatmap, and tabletop exercise simulator."),
version="0.1.0",
)
diff --git a/services/purple-team/app/models/purple_team.py b/services/purple-team/app/models/purple_team.py
index 20854fee3..9c971001b 100644
--- a/services/purple-team/app/models/purple_team.py
+++ b/services/purple-team/app/models/purple_team.py
@@ -1,4 +1,5 @@
"""SQLAlchemy ORM models for the Purple Team service."""
+
from __future__ import annotations
import uuid
@@ -18,9 +19,7 @@ class AtomicTest(Base):
__tablename__ = "purple_team_atomic_tests"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid()
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid())
tenant_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False, index=True)
# ATT&CK reference
@@ -41,9 +40,7 @@ class AtomicTest(Base):
# Raw YAML content
raw_yaml: Mapped[dict] = mapped_column(JSONB, nullable=False, server_default="{}")
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), server_default=func.now(), nullable=False
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)
class TestExecution(Base):
@@ -51,9 +48,7 @@ class TestExecution(Base):
__tablename__ = "purple_team_executions"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid()
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid())
tenant_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False, index=True)
# What was executed
@@ -86,9 +81,7 @@ class TestExecution(Base):
# Who ran it
executed_by: Mapped[str | None] = mapped_column(String(128), nullable=True)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), server_default=func.now(), nullable=False
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)
class TabletopSession(Base):
@@ -96,9 +89,7 @@ class TabletopSession(Base):
__tablename__ = "purple_team_tabletop_sessions"
- id: Mapped[uuid.UUID] = mapped_column(
- UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid()
- )
+ id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid())
tenant_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False, index=True)
name: Mapped[str] = mapped_column(String(256), nullable=False)
@@ -111,14 +102,8 @@ class TabletopSession(Base):
# Structured findings from the discussion
findings: Mapped[list] = mapped_column(JSONB, nullable=False, server_default="[]")
- status: Mapped[str] = mapped_column(
- String(16), nullable=False, server_default="active"
- ) # active | completed | archived
+ status: Mapped[str] = mapped_column(String(16), nullable=False, server_default="active") # active | completed | archived
created_by: Mapped[str | None] = mapped_column(String(128), nullable=True)
- created_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), server_default=func.now(), nullable=False
- )
- updated_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
- )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)
+ updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
diff --git a/services/purple-team/app/services/atomic_loader.py b/services/purple-team/app/services/atomic_loader.py
index 28cf5432d..30367f98c 100644
--- a/services/purple-team/app/services/atomic_loader.py
+++ b/services/purple-team/app/services/atomic_loader.py
@@ -1,8 +1,8 @@
"""Load and parse Atomic Red Team YAML test definitions."""
+
from __future__ import annotations
import logging
-import os
from pathlib import Path
from typing import Any
diff --git a/services/purple-team/app/services/caldera_client.py b/services/purple-team/app/services/caldera_client.py
index 533fd339f..71199bc21 100644
--- a/services/purple-team/app/services/caldera_client.py
+++ b/services/purple-team/app/services/caldera_client.py
@@ -1,4 +1,5 @@
"""Thin async client for the Caldera REST API."""
+
from __future__ import annotations
import logging
@@ -32,9 +33,7 @@ class CalderaClient:
async def get_ability(self, ability_id: str) -> dict[str, Any]:
async with httpx.AsyncClient(timeout=10) as c:
- r = await c.get(
- f"{self.base_url}/api/v2/abilities/{ability_id}", headers=self._headers
- )
+ r = await c.get(f"{self.base_url}/api/v2/abilities/{ability_id}", headers=self._headers)
r.raise_for_status()
return r.json()
@@ -46,9 +45,7 @@ class CalderaClient:
async def get_operation(self, operation_id: str) -> dict[str, Any]:
async with httpx.AsyncClient(timeout=10) as c:
- r = await c.get(
- f"{self.base_url}/api/v2/operations/{operation_id}", headers=self._headers
- )
+ r = await c.get(f"{self.base_url}/api/v2/operations/{operation_id}", headers=self._headers)
r.raise_for_status()
return r.json()
@@ -67,9 +64,7 @@ class CalderaClient:
"state": "running",
}
async with httpx.AsyncClient(timeout=15) as c:
- r = await c.post(
- f"{self.base_url}/api/v2/operations", json=payload, headers=self._headers
- )
+ r = await c.post(f"{self.base_url}/api/v2/operations", json=payload, headers=self._headers)
r.raise_for_status()
return r.json()
diff --git a/services/purple-team/tests/test_coverage.py b/services/purple-team/tests/test_coverage.py
index 17fbd10e3..66f4bdea2 100644
--- a/services/purple-team/tests/test_coverage.py
+++ b/services/purple-team/tests/test_coverage.py
@@ -3,7 +3,6 @@
from __future__ import annotations
import pytest
-
from app.services.coverage import TACTIC_ORDER, build_coverage_matrix
@@ -41,9 +40,7 @@ class TestBuildCoverageMatrix:
assert result["summary"]["total_techniques"] == 0
def test_single_passing_execution_with_detection(self) -> None:
- result = build_coverage_matrix(
- [_exec("T1078", "initial-access", status="success", detected=True)]
- )
+ result = build_coverage_matrix([_exec("T1078", "initial-access", status="success", detected=True)])
techs = result["techniques"]["initial-access"]
assert len(techs) == 1
t = techs[0]
diff --git a/services/threatintel/app/clients/cisa_kev.py b/services/threatintel/app/clients/cisa_kev.py
index 6c95c2bab..2ae4c3ba9 100644
--- a/services/threatintel/app/clients/cisa_kev.py
+++ b/services/threatintel/app/clients/cisa_kev.py
@@ -3,12 +3,13 @@ CISA Known Exploited Vulnerabilities (KEV) catalog client.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
-import structlog
from typing import Any
import httpx
+import structlog
logger = structlog.get_logger(__name__)
diff --git a/services/threatintel/app/clients/misp.py b/services/threatintel/app/clients/misp.py
index b310a3c0f..7d27387df 100644
--- a/services/threatintel/app/clients/misp.py
+++ b/services/threatintel/app/clients/misp.py
@@ -3,13 +3,14 @@ MISP client for fetching threat intelligence events and attributes.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
-import structlog
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta
from typing import Any
import httpx
+import structlog
logger = structlog.get_logger(__name__)
@@ -45,9 +46,7 @@ class MispClient:
limit: int = 500,
) -> list[dict[str, Any]]:
"""Fetch MISP events published within the last N hours."""
- since_ts = int(
- (datetime.now(timezone.utc) - timedelta(hours=since_hours)).timestamp()
- )
+ since_ts = int((datetime.now(UTC) - timedelta(hours=since_hours)).timestamp())
params = {
"timestamp": since_ts,
"limit": limit,
@@ -78,9 +77,7 @@ class MispClient:
async def get_event(self, event_id: str) -> dict[str, Any] | None:
"""Fetch a single MISP event by ID."""
- async with httpx.AsyncClient(
- headers=self._headers, verify=self._verify_ssl, timeout=30.0
- ) as client:
+ async with httpx.AsyncClient(headers=self._headers, verify=self._verify_ssl, timeout=30.0) as client:
try:
resp = await client.get(f"{self._base_url}/events/{event_id}")
resp.raise_for_status()
diff --git a/services/threatintel/app/clients/otx.py b/services/threatintel/app/clients/otx.py
index d4786e528..539bf0a27 100644
--- a/services/threatintel/app/clients/otx.py
+++ b/services/threatintel/app/clients/otx.py
@@ -3,12 +3,13 @@ AlienVault OTX (Open Threat Exchange) client.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
-import structlog
from typing import Any
import httpx
+import structlog
logger = structlog.get_logger(__name__)
@@ -51,9 +52,7 @@ class OtxClient:
pulses: list[dict[str, Any]] = []
page = 1
- async with httpx.AsyncClient(
- headers=self._headers, timeout=60.0
- ) as client:
+ async with httpx.AsyncClient(headers=self._headers, timeout=60.0) as client:
while True:
params: dict[str, Any] = {"limit": limit, "page": page}
if modified_since:
@@ -89,18 +88,20 @@ class OtxClient:
ioc_type = self._map_indicator_type(ind.get("type", ""))
if not ioc_type:
continue
- iocs.append({
- "type": ioc_type,
- "value": ind.get("indicator", ""),
- "description": ind.get("description", ""),
- "tags": pulse.get("tags", []),
- "source": "otx",
- "source_ref": f"otx:{pulse_id}",
- "pulse_name": pulse_name,
- "tlp": pulse.get("tlp", "white"),
- "malware_families": pulse.get("malware_families", []),
- "attack_ids": pulse.get("attack_ids", []),
- })
+ iocs.append(
+ {
+ "type": ioc_type,
+ "value": ind.get("indicator", ""),
+ "description": ind.get("description", ""),
+ "tags": pulse.get("tags", []),
+ "source": "otx",
+ "source_ref": f"otx:{pulse_id}",
+ "pulse_name": pulse_name,
+ "tlp": pulse.get("tlp", "white"),
+ "malware_families": pulse.get("malware_families", []),
+ "attack_ids": pulse.get("attack_ids", []),
+ }
+ )
return iocs
@staticmethod
diff --git a/services/threatintel/app/clients/taxii.py b/services/threatintel/app/clients/taxii.py
index fccb88f79..8117ee056 100644
--- a/services/threatintel/app/clients/taxii.py
+++ b/services/threatintel/app/clients/taxii.py
@@ -3,13 +3,13 @@ TAXII 2.1 client for fetching STIX bundles from threat intelligence feeds.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
-import json
-import structlog
-from typing import Any, Iterator
+from typing import Any
import httpx
+import structlog
logger = structlog.get_logger(__name__)
@@ -76,9 +76,7 @@ class TaxiiClient:
root = api_root.strip("/") if api_root else ""
root_segment = f"/{root}" if root else ""
- url = (
- f"{self._base_url}{root_segment}/collections/{collection_id}/objects/"
- )
+ url = f"{self._base_url}{root_segment}/collections/{collection_id}/objects/"
try:
resp = await client.get(url, params=params)
resp.raise_for_status()
diff --git a/services/threatintel/app/config.py b/services/threatintel/app/config.py
index a2c48adde..f2b86868c 100644
--- a/services/threatintel/app/config.py
+++ b/services/threatintel/app/config.py
@@ -1,4 +1,5 @@
"""Configuration for the AiSOC Threat Intelligence service."""
+
from __future__ import annotations
from pydantic_settings import BaseSettings
@@ -36,8 +37,6 @@ class Settings(BaseSettings):
# Kafka
KAFKA_BOOTSTRAP_SERVERS: str = "redpanda:9092"
KAFKA_TOPIC_THREAT_INTEL: str = "aisoc.threat_intel"
- # Legacy alias
- KAFKA_BROKERS: str = "redpanda:9092"
KAFKA_TOPIC_IOC: str = "aisoc.ioc_enrichments"
# TAXII 2.1
@@ -45,7 +44,7 @@ class Settings(BaseSettings):
TAXII_USERNAME: str = ""
TAXII_PASSWORD: str = ""
TAXII_API_ROOT: str = ""
- TAXII_COLLECTION_IDS: str = "" # comma-separated collection IDs
+ TAXII_COLLECTION_IDS: str = "" # comma-separated collection IDs
# MISP
MISP_URL: str = ""
@@ -64,10 +63,10 @@ class Settings(BaseSettings):
OPENCTI_TOKEN: str = ""
# Poll intervals (seconds)
- TAXII_POLL_INTERVAL: int = 900 # 15 min
- MISP_POLL_INTERVAL: int = 1800 # 30 min
- OTX_POLL_INTERVAL: int = 1800 # 30 min
- CISA_KEV_POLL_INTERVAL: int = 86400 # 24 h
+ TAXII_POLL_INTERVAL: int = 900 # 15 min
+ MISP_POLL_INTERVAL: int = 1800 # 30 min
+ OTX_POLL_INTERVAL: int = 1800 # 30 min
+ CISA_KEV_POLL_INTERVAL: int = 86400 # 24 h
# Legacy alias
CISA_POLL_INTERVAL: int = 86400
diff --git a/services/threatintel/app/feeds/handlers.py b/services/threatintel/app/feeds/handlers.py
index a59be2c7e..4f38f5c5d 100644
--- a/services/threatintel/app/feeds/handlers.py
+++ b/services/threatintel/app/feeds/handlers.py
@@ -7,29 +7,31 @@ parse/normalize the data, and hand off to ThreatIntelPipeline.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
-import structlog
from typing import TYPE_CHECKING, Any
-from app.clients.taxii import TaxiiClient
+import structlog
+
+from app.clients.cisa_kev import CisaKevClient
from app.clients.misp import MispClient
from app.clients.otx import OtxClient
-from app.clients.cisa_kev import CisaKevClient
+from app.clients.taxii import TaxiiClient
from app.parsers.stix import StixParser
if TYPE_CHECKING:
from app.feeds.pipeline import ThreatIntelPipeline
- from app.config import Settings
logger = structlog.get_logger(__name__)
# ─── TAXII Feed Handler ───────────────────────────────────────────────────────
+
async def handle_taxii_feed(
client: TaxiiClient,
- pipeline: "ThreatIntelPipeline",
+ pipeline: ThreatIntelPipeline,
api_root: str,
collection_id: str,
) -> None:
@@ -68,9 +70,10 @@ async def handle_taxii_feed(
# ─── MISP Feed Handler ────────────────────────────────────────────────────────
+
async def handle_misp_feed(
client: MispClient,
- pipeline: "ThreatIntelPipeline",
+ pipeline: ThreatIntelPipeline,
since_hours: int = 24,
) -> None:
"""
@@ -94,9 +97,10 @@ async def handle_misp_feed(
# ─── OTX Feed Handler ─────────────────────────────────────────────────────────
+
async def handle_otx_feed(
client: OtxClient,
- pipeline: "ThreatIntelPipeline",
+ pipeline: ThreatIntelPipeline,
) -> None:
"""
Fetch subscribed OTX pulses and extract IOC indicators.
@@ -119,9 +123,10 @@ async def handle_otx_feed(
# ─── CISA KEV Feed Handler ────────────────────────────────────────────────────
+
async def handle_cisa_kev_feed(
client: CisaKevClient,
- pipeline: "ThreatIntelPipeline",
+ pipeline: ThreatIntelPipeline,
) -> None:
"""
Fetch CISA KEV catalog and ingest vulnerability IOCs.
diff --git a/services/threatintel/app/feeds/pipeline.py b/services/threatintel/app/feeds/pipeline.py
index 93502c0b0..0639a2eaf 100644
--- a/services/threatintel/app/feeds/pipeline.py
+++ b/services/threatintel/app/feeds/pipeline.py
@@ -10,17 +10,19 @@ Handles the full lifecycle for each normalized IOC/actor batch:
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
import json
+from datetime import UTC, datetime
+from typing import Any
+
import structlog
-from datetime import datetime, timezone
-from typing import Any, Optional
from app.storage.bloom import RedisBloomFilter
+from app.storage.neo4j import Neo4jStore
from app.storage.opensearch import OpenSearchStore
from app.storage.qdrant import QdrantStore
-from app.storage.neo4j import Neo4jStore
logger = structlog.get_logger(__name__)
@@ -66,7 +68,7 @@ class ThreatIntelPipeline:
duplicates = 0
for ioc in iocs:
- bloom_key = f"{ioc.get('type','?')}:{ioc.get('value','?')}"
+ bloom_key = f"{ioc.get('type', '?')}:{ioc.get('value', '?')}"
if await self._bloom.contains(bloom_key):
duplicates += 1
else:
@@ -151,14 +153,16 @@ class ThreatIntelPipeline:
source: str,
) -> None:
"""Emit Kafka messages for downstream consumers."""
- ts = datetime.now(timezone.utc).isoformat()
+ ts = datetime.now(UTC).isoformat()
for item in items:
- payload = json.dumps({
- "event_type": event_type,
- "source": source,
- "timestamp": ts,
- "data": item,
- }).encode()
+ payload = json.dumps(
+ {
+ "event_type": event_type,
+ "source": source,
+ "timestamp": ts,
+ "data": item,
+ }
+ ).encode()
try:
await self._kafka.send(self._kafka_topic, value=payload)
except Exception as exc:
diff --git a/services/threatintel/app/feeds/scheduler.py b/services/threatintel/app/feeds/scheduler.py
index 936c9cfc2..cd94dccd7 100644
--- a/services/threatintel/app/feeds/scheduler.py
+++ b/services/threatintel/app/feeds/scheduler.py
@@ -6,11 +6,12 @@ pipes normalized IOCs through the deduplication + storage pipeline.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
-import structlog
from typing import TYPE_CHECKING
+import structlog
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.interval import IntervalTrigger
@@ -29,7 +30,7 @@ class FeedScheduler:
feeds (CISA KEV once a day) can coexist without contention.
"""
- def __init__(self, pipeline: "ThreatIntelPipeline") -> None:
+ def __init__(self, pipeline: ThreatIntelPipeline) -> None:
self._pipeline = pipeline
self._scheduler = AsyncIOScheduler()
diff --git a/services/threatintel/app/main.py b/services/threatintel/app/main.py
index 1627b0000..1ec80c2a5 100644
--- a/services/threatintel/app/main.py
+++ b/services/threatintel/app/main.py
@@ -7,14 +7,15 @@ into OpenSearch, Qdrant, and Neo4j.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
-import structlog
+from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from functools import partial
-from typing import AsyncGenerator
import redis.asyncio as aioredis
+import structlog
from aiokafka import AIOKafkaProducer
from fastapi import FastAPI
from neo4j import AsyncGraphDatabase
@@ -63,6 +64,7 @@ actors_ingested = Counter("threatintel_actors_ingested_total", "Total actors ing
# ─── Application lifespan ─────────────────────────────────────────────────────
+
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
logger.info("ThreatIntel service starting…")
@@ -73,16 +75,12 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
# ── OpenSearch ────────────────────────────────────────────────────────────
os_client = AsyncOpenSearch(
hosts=[{"host": settings.OPENSEARCH_HOST, "port": settings.OPENSEARCH_PORT}],
- http_auth=(settings.OPENSEARCH_USER, settings.OPENSEARCH_PASSWORD)
- if settings.OPENSEARCH_USER
- else None,
+ http_auth=(settings.OPENSEARCH_USER, settings.OPENSEARCH_PASSWORD) if settings.OPENSEARCH_USER else None,
use_ssl=False,
)
# ── Qdrant ────────────────────────────────────────────────────────────────
- qdrant_client = AsyncQdrantClient(
- host=settings.QDRANT_HOST, port=settings.QDRANT_PORT
- )
+ qdrant_client = AsyncQdrantClient(host=settings.QDRANT_HOST, port=settings.QDRANT_PORT)
# ── Neo4j ─────────────────────────────────────────────────────────────────
neo4j_driver = AsyncGraphDatabase.driver(
@@ -94,9 +92,7 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
kafka_producer = None
if settings.KAFKA_BOOTSTRAP_SERVERS:
try:
- kafka_producer = AIOKafkaProducer(
- bootstrap_servers=settings.KAFKA_BOOTSTRAP_SERVERS
- )
+ kafka_producer = AIOKafkaProducer(bootstrap_servers=settings.KAFKA_BOOTSTRAP_SERVERS)
await kafka_producer.start()
except Exception as exc:
logger.warning("Kafka producer unavailable", error=str(exc))
@@ -131,14 +127,22 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
password=settings.TAXII_PASSWORD,
)
- misp_client = MispClient(
- url=settings.MISP_URL,
- api_key=settings.MISP_API_KEY,
- ) if settings.MISP_URL else None
+ misp_client = (
+ MispClient(
+ url=settings.MISP_URL,
+ api_key=settings.MISP_API_KEY,
+ )
+ if settings.MISP_URL
+ else None
+ )
- otx_client = OtxClient(
- api_key=settings.OTX_API_KEY,
- ) if settings.OTX_API_KEY else None
+ otx_client = (
+ OtxClient(
+ api_key=settings.OTX_API_KEY,
+ )
+ if settings.OTX_API_KEY
+ else None
+ )
kev_client = CisaKevClient()
@@ -230,9 +234,7 @@ async def health() -> dict:
return {
"status": "ok",
"redis": redis_ok,
- "scheduler": app.state.scheduler._scheduler.running
- if hasattr(app.state, "scheduler")
- else False,
+ "scheduler": app.state.scheduler._scheduler.running if hasattr(app.state, "scheduler") else False,
}
@@ -245,7 +247,5 @@ async def search_iocs(
) -> dict:
"""Search stored IOCs."""
pipeline: ThreatIntelPipeline = app.state.pipeline
- iocs = await pipeline._os.search_iocs(
- value=value, ioc_type=ioc_type, source=source, limit=limit
- )
+ iocs = await pipeline._os.search_iocs(value=value, ioc_type=ioc_type, source=source, limit=limit)
return {"total": len(iocs), "iocs": iocs}
diff --git a/services/threatintel/app/parsers/stix.py b/services/threatintel/app/parsers/stix.py
index 898c0f3d8..26fbb1c62 100644
--- a/services/threatintel/app/parsers/stix.py
+++ b/services/threatintel/app/parsers/stix.py
@@ -3,12 +3,14 @@ STIX 2.1 object parser — converts STIX objects to normalized IOC/actor dicts.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
import hashlib
-import structlog
from typing import Any
+import structlog
+
logger = structlog.get_logger(__name__)
# STIX types we care about as IOCs
@@ -88,26 +90,29 @@ class StixParser:
# Extract values from STIX pattern e.g. [ipv4-addr:value = '1.2.3.4']
import re
+
# Simple regex extraction
for match in re.finditer(r"\[(\S+):(\S+)\s*=\s*'([^']+)'\]", pattern):
obj_type, prop, value = match.groups()
ioc_type = self._map_stix_type(obj_type, prop)
if not ioc_type:
continue
- iocs.append({
- "type": ioc_type,
- "value": value,
- "labels": labels,
- "name": name,
- "confidence": confidence,
- "valid_from": valid_from,
- "valid_until": valid_until,
- "stix_id": obj.get("id"),
- "source": "stix",
- "source_ref": obj.get("id", ""),
- "tags": labels,
- "tlp": self._extract_marking(obj),
- })
+ iocs.append(
+ {
+ "type": ioc_type,
+ "value": value,
+ "labels": labels,
+ "name": name,
+ "confidence": confidence,
+ "valid_from": valid_from,
+ "valid_until": valid_until,
+ "stix_id": obj.get("id"),
+ "source": "stix",
+ "source_ref": obj.get("id", ""),
+ "tags": labels,
+ "tlp": self._extract_marking(obj),
+ }
+ )
return iocs
def _observable_to_ioc(self, obj: dict[str, Any]) -> dict[str, Any] | None:
@@ -165,22 +170,24 @@ class StixParser:
actors = []
for obj in self._objects:
if obj.get("type") in ("threat-actor", "intrusion-set"):
- actors.append({
- "stix_id": obj.get("id"),
- "type": obj.get("type"),
- "name": obj.get("name", ""),
- "description": obj.get("description", ""),
- "aliases": obj.get("aliases", []),
- "goals": obj.get("goals", []),
- "labels": obj.get("labels", []),
- "sophistication": obj.get("sophistication", ""),
- "resource_level": obj.get("resource_level", ""),
- "primary_motivation": obj.get("primary_motivation", ""),
- "first_seen": obj.get("first_seen", ""),
- "last_seen": obj.get("last_seen", ""),
- "source": "stix",
- "tlp": self._extract_marking(obj),
- })
+ actors.append(
+ {
+ "stix_id": obj.get("id"),
+ "type": obj.get("type"),
+ "name": obj.get("name", ""),
+ "description": obj.get("description", ""),
+ "aliases": obj.get("aliases", []),
+ "goals": obj.get("goals", []),
+ "labels": obj.get("labels", []),
+ "sophistication": obj.get("sophistication", ""),
+ "resource_level": obj.get("resource_level", ""),
+ "primary_motivation": obj.get("primary_motivation", ""),
+ "first_seen": obj.get("first_seen", ""),
+ "last_seen": obj.get("last_seen", ""),
+ "source": "stix",
+ "tlp": self._extract_marking(obj),
+ }
+ )
return actors
# ─── Relationships ────────────────────────────────────────────────────────
@@ -194,15 +201,17 @@ class StixParser:
rel_type = obj.get("relationship_type", "")
if rel_type not in (_REL_USES, _REL_INDICATES, _REL_ATTRIBUTED_TO, _REL_TARGETS, _REL_MITIGATES):
continue
- rels.append({
- "stix_id": obj.get("id"),
- "rel_type": rel_type,
- "source_ref": obj.get("source_ref", ""),
- "target_ref": obj.get("target_ref", ""),
- "description": obj.get("description", ""),
- "start_time": obj.get("start_time", ""),
- "stop_time": obj.get("stop_time", ""),
- })
+ rels.append(
+ {
+ "stix_id": obj.get("id"),
+ "rel_type": rel_type,
+ "source_ref": obj.get("source_ref", ""),
+ "target_ref": obj.get("target_ref", ""),
+ "description": obj.get("description", ""),
+ "start_time": obj.get("start_time", ""),
+ "stop_time": obj.get("stop_time", ""),
+ }
+ )
return rels
# ─── Helpers ──────────────────────────────────────────────────────────────
diff --git a/services/threatintel/app/storage/bloom.py b/services/threatintel/app/storage/bloom.py
index 5d4277fb3..cc01c5fa0 100644
--- a/services/threatintel/app/storage/bloom.py
+++ b/services/threatintel/app/storage/bloom.py
@@ -7,19 +7,19 @@ expensive storage writes.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
import math
-import structlog
-from typing import Optional
import mmh3
import redis.asyncio as aioredis
+import structlog
logger = structlog.get_logger(__name__)
_DEFAULT_CAPACITY = 10_000_000 # 10M IOCs
-_DEFAULT_ERROR_RATE = 0.001 # 0.1% false-positive rate
+_DEFAULT_ERROR_RATE = 0.001 # 0.1% false-positive rate
_BLOOM_KEY = "threatintel:bloom:iocs"
diff --git a/services/threatintel/app/storage/neo4j.py b/services/threatintel/app/storage/neo4j.py
index b7d5065ac..3f500d7fd 100644
--- a/services/threatintel/app/storage/neo4j.py
+++ b/services/threatintel/app/storage/neo4j.py
@@ -6,11 +6,12 @@ knowledge graph for attack path and blast-radius queries.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
-import structlog
from typing import Any
+import structlog
from neo4j import AsyncDriver
logger = structlog.get_logger(__name__)
diff --git a/services/threatintel/app/storage/opensearch.py b/services/threatintel/app/storage/opensearch.py
index 07890e4cd..1686e8baf 100644
--- a/services/threatintel/app/storage/opensearch.py
+++ b/services/threatintel/app/storage/opensearch.py
@@ -3,13 +3,14 @@ OpenSearch storage layer for threat intelligence IOCs and actors.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
import hashlib
-import structlog
-from datetime import datetime, timezone
+from datetime import UTC, datetime
from typing import Any
+import structlog
from opensearchpy import AsyncOpenSearch, helpers
logger = structlog.get_logger(__name__)
@@ -76,7 +77,7 @@ class OpenSearchStore:
if not iocs:
return 0
- now = datetime.now(timezone.utc).isoformat()
+ now = datetime.now(UTC).isoformat()
actions = []
for ioc in iocs:
doc_id = _stable_id(ioc.get("type", ""), ioc.get("value", ""), ioc.get("source", ""))
@@ -85,12 +86,14 @@ class OpenSearchStore:
"ingested_at": now,
"last_seen": now,
}
- actions.append({
- "_op_type": "index",
- "_index": _IOC_INDEX,
- "_id": doc_id,
- "_source": doc,
- })
+ actions.append(
+ {
+ "_op_type": "index",
+ "_index": _IOC_INDEX,
+ "_id": doc_id,
+ "_source": doc,
+ }
+ )
success, errors = await helpers.async_bulk(self._os, actions, raise_on_error=False)
if errors:
@@ -102,16 +105,18 @@ class OpenSearchStore:
if not actors:
return 0
- now = datetime.now(timezone.utc).isoformat()
+ now = datetime.now(UTC).isoformat()
actions = []
for actor in actors:
doc_id = _stable_id("actor", actor.get("name", ""), actor.get("source", "stix"))
- actions.append({
- "_op_type": "index",
- "_index": _ACTOR_INDEX,
- "_id": doc_id,
- "_source": {**actor, "ingested_at": now},
- })
+ actions.append(
+ {
+ "_op_type": "index",
+ "_index": _ACTOR_INDEX,
+ "_id": doc_id,
+ "_source": {**actor, "ingested_at": now},
+ }
+ )
success, _ = await helpers.async_bulk(self._os, actions, raise_on_error=False)
return success
diff --git a/services/threatintel/app/storage/qdrant.py b/services/threatintel/app/storage/qdrant.py
index e488c98dd..b63be82d0 100644
--- a/services/threatintel/app/storage/qdrant.py
+++ b/services/threatintel/app/storage/qdrant.py
@@ -3,11 +3,12 @@ Qdrant vector storage for semantic threat intelligence search.
AiSOC — open-source AI Security Operations Center (MIT License)
"""
+
from __future__ import annotations
-import structlog
from typing import Any
+import structlog
from qdrant_client import AsyncQdrantClient
from qdrant_client.models import (
Distance,
@@ -43,9 +44,7 @@ class QdrantStore:
if col not in existing_names:
await self._client.create_collection(
collection_name=col,
- vectors_config=VectorParams(
- size=_EMBEDDING_DIM, distance=Distance.COSINE
- ),
+ vectors_config=VectorParams(size=_EMBEDDING_DIM, distance=Distance.COSINE),
)
logger.info("Created Qdrant collection", collection=col)
@@ -68,6 +67,7 @@ class QdrantStore:
# Qdrant expects integer IDs — use hash of value+type for stable IDs
import hashlib
+
points = [
PointStruct(
id=abs(int(hashlib.md5(f"{ioc['type']}:{ioc['value']}".encode()).hexdigest(), 16)) % (2**63),
@@ -88,10 +88,11 @@ class QdrantStore:
if not actors:
return
- texts = [f"{a.get('name','')} {a.get('description','')}" for a in actors]
+ texts = [f"{a.get('name', '')} {a.get('description', '')}" for a in actors]
vectors = await self._embed(texts)
import hashlib
+
points = [
PointStruct(
id=abs(int(hashlib.md5(actor.get("name", "").encode()).hexdigest(), 16)) % (2**63),
@@ -127,6 +128,7 @@ class QdrantStore:
"""Generate embeddings using fastembed or a stub."""
try:
from fastembed import TextEmbedding
+
model = TextEmbedding(model_name="BAAI/bge-small-en-v1.5")
embeddings = list(model.embed(texts))
# Pad/truncate to expected dimension
diff --git a/services/ueba/alembic/env.py b/services/ueba/alembic/env.py
index c43f414c6..b90e15d84 100644
--- a/services/ueba/alembic/env.py
+++ b/services/ueba/alembic/env.py
@@ -1,4 +1,5 @@
"""Alembic migration environment for UEBA service."""
+
from __future__ import annotations
import asyncio
@@ -6,9 +7,8 @@ import os
from logging.config import fileConfig
from alembic import context
-from sqlalchemy.ext.asyncio import create_async_engine
-
from app.models.ueba import Base
+from sqlalchemy.ext.asyncio import create_async_engine
config = context.config
diff --git a/services/ueba/alembic/versions/0001_initial_ueba_tables.py b/services/ueba/alembic/versions/0001_initial_ueba_tables.py
index 8064e3cfd..f46c2b27a 100644
--- a/services/ueba/alembic/versions/0001_initial_ueba_tables.py
+++ b/services/ueba/alembic/versions/0001_initial_ueba_tables.py
@@ -4,11 +4,12 @@ Revision ID: 0001
Revises:
Create Date: 2026-05-03
"""
+
from __future__ import annotations
-from alembic import op
import sqlalchemy as sa
-from sqlalchemy.dialects.postgresql import UUID, JSONB
+from alembic import op
+from sqlalchemy.dialects.postgresql import JSONB, UUID
revision = "0001"
down_revision = None
diff --git a/services/ueba/app/api/routes.py b/services/ueba/app/api/routes.py
index 51ab79d36..3b758f856 100644
--- a/services/ueba/app/api/routes.py
+++ b/services/ueba/app/api/routes.py
@@ -1,20 +1,19 @@
"""UEBA REST API routes."""
+
from __future__ import annotations
import uuid
-from datetime import datetime, timedelta, timezone
+from datetime import UTC, datetime, timedelta
from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, Query
from pydantic import BaseModel, Field
-from sqlalchemy import select, desc
-from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
+from sqlalchemy import desc, select
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app.core.config import settings
-from app.models.ueba import UEBAAnomaly, EntityBaseline, PeerGroup
-from app.services.baseline import BaselineService
+from app.models.ueba import EntityBaseline, PeerGroup, UEBAAnomaly
from app.services.scoring import ScoringService
-from app.services.peer_group import PeerGroupService
router = APIRouter(prefix="/api/v1/ueba", tags=["ueba"])
@@ -37,6 +36,7 @@ DB = Annotated[AsyncSession, Depends(get_db)]
# Schemas
# ---------------------------------------------------------------------------
+
class ScoreEventRequest(BaseModel):
tenant_id: uuid.UUID
entity_type: str = Field(..., pattern="^(user|device|ip)$")
@@ -82,6 +82,7 @@ class BaselineOut(BaseModel):
# Endpoints
# ---------------------------------------------------------------------------
+
@router.post("/score", response_model=AnomalyOut | None, status_code=200)
async def score_event(body: ScoreEventRequest, db: DB) -> AnomalyOut | None:
"""Score a single event and return the anomaly record if anomalous."""
@@ -109,7 +110,7 @@ async def list_anomalies(
hours: int = Query(24, ge=1, le=720),
limit: int = Query(50, ge=1, le=500),
) -> list[AnomalyOut]:
- since = datetime.now(timezone.utc) - timedelta(hours=hours)
+ since = datetime.now(UTC) - timedelta(hours=hours)
q = (
select(UEBAAnomaly)
.where(UEBAAnomaly.tenant_id == tenant_id, UEBAAnomaly.detected_at >= since)
@@ -145,12 +146,7 @@ async def list_baselines(
entity_type: str | None = Query(None),
limit: int = Query(50, ge=1, le=200),
) -> list[BaselineOut]:
- q = (
- select(EntityBaseline)
- .where(EntityBaseline.tenant_id == tenant_id)
- .order_by(desc(EntityBaseline.updated_at))
- .limit(limit)
- )
+ q = select(EntityBaseline).where(EntityBaseline.tenant_id == tenant_id).order_by(desc(EntityBaseline.updated_at)).limit(limit)
if entity_type:
q = q.where(EntityBaseline.entity_type == entity_type)
result = await db.execute(q)
@@ -162,9 +158,7 @@ async def list_peer_groups(
db: DB,
tenant_id: uuid.UUID = Query(...),
) -> list[dict]:
- result = await db.execute(
- select(PeerGroup).where(PeerGroup.tenant_id == tenant_id)
- )
+ result = await db.execute(select(PeerGroup).where(PeerGroup.tenant_id == tenant_id))
return [
{
"id": pg.id,
diff --git a/services/ueba/app/core/config.py b/services/ueba/app/core/config.py
index e20664e5d..c7f85cef7 100644
--- a/services/ueba/app/core/config.py
+++ b/services/ueba/app/core/config.py
@@ -14,10 +14,10 @@ class Settings(BaseSettings):
kafka_consumer_group: str = "ueba-service"
# UEBA tuning
- baseline_window_days: int = 30 # days of history for baseline computation
- anomaly_threshold: float = 3.0 # z-score threshold for anomaly flagging
- peer_group_min_size: int = 3 # minimum peers for peer-group analysis
- scoring_batch_size: int = 100 # events processed per scoring batch
+ baseline_window_days: int = 30 # days of history for baseline computation
+ anomaly_threshold: float = 3.0 # z-score threshold for anomaly flagging
+ peer_group_min_size: int = 3 # minimum peers for peer-group analysis
+ scoring_batch_size: int = 100 # events processed per scoring batch
# OTel
otel_endpoint: str = "http://localhost:4317"
diff --git a/services/ueba/app/main.py b/services/ueba/app/main.py
index adcbcf51c..fbd105a10 100644
--- a/services/ueba/app/main.py
+++ b/services/ueba/app/main.py
@@ -1,4 +1,5 @@
"""UEBA FastAPI application entry-point."""
+
from __future__ import annotations
import asyncio
@@ -14,11 +15,11 @@ from app.core.config import settings
# ---------------------------------------------------------------------------
try:
from opentelemetry import trace
- from opentelemetry.sdk.resources import Resource, SERVICE_NAME
- from opentelemetry.sdk.trace import TracerProvider
- from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
+ from opentelemetry.sdk.resources import SERVICE_NAME, Resource
+ from opentelemetry.sdk.trace import TracerProvider
+ from opentelemetry.sdk.trace.export import BatchSpanProcessor
resource = Resource.create({SERVICE_NAME: settings.service_name})
provider = TracerProvider(resource=resource)
diff --git a/services/ueba/app/models/ueba.py b/services/ueba/app/models/ueba.py
index de729376c..381e4a012 100644
--- a/services/ueba/app/models/ueba.py
+++ b/services/ueba/app/models/ueba.py
@@ -1,22 +1,20 @@
"""SQLAlchemy ORM models for UEBA."""
+
from __future__ import annotations
import uuid
from datetime import datetime
from sqlalchemy import (
- BigInteger,
DateTime,
Float,
- ForeignKey,
Index,
Integer,
String,
- Text,
func,
)
from sqlalchemy.dialects.postgresql import JSONB, UUID
-from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
+from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
@@ -30,7 +28,7 @@ class EntityBaseline(Base):
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
tenant_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False)
- entity_type: Mapped[str] = mapped_column(String(32), nullable=False) # user | device | ip
+ entity_type: Mapped[str] = mapped_column(String(32), nullable=False) # user | device | ip
entity_id: Mapped[str] = mapped_column(String(256), nullable=False)
# Feature statistics (JSON: {"feature": {"mean": X, "std": Y, "count": N}})
@@ -41,13 +39,9 @@ class EntityBaseline(Base):
window_start: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
window_end: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
- updated_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
- )
+ updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
- __table_args__ = (
- Index("ix_ueba_baseline_tenant_entity", "tenant_id", "entity_type", "entity_id"),
- )
+ __table_args__ = (Index("ix_ueba_baseline_tenant_entity", "tenant_id", "entity_type", "entity_id"),)
class UEBAAnomaly(Base):
@@ -65,8 +59,8 @@ class UEBAAnomaly(Base):
event_type: Mapped[str] = mapped_column(String(128), nullable=False)
# Anomaly scoring
- anomaly_score: Mapped[float] = mapped_column(Float, nullable=False) # composite z-score
- risk_level: Mapped[str] = mapped_column(String(16), nullable=False) # low|medium|high|critical
+ anomaly_score: Mapped[float] = mapped_column(Float, nullable=False) # composite z-score
+ risk_level: Mapped[str] = mapped_column(String(16), nullable=False) # low|medium|high|critical
features: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
# {"feature": {"value": X, "mean": Y, "std": Z, "z_score": W}}
@@ -74,9 +68,7 @@ class UEBAAnomaly(Base):
peer_group_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
peer_deviation_score: Mapped[float | None] = mapped_column(Float, nullable=True)
- detected_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), server_default=func.now()
- )
+ detected_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
acknowledged: Mapped[bool] = mapped_column(default=False)
__table_args__ = (
@@ -96,10 +88,6 @@ class PeerGroup(Base):
label: Mapped[str] = mapped_column(String(128), nullable=False)
member_count: Mapped[int] = mapped_column(Integer, default=0)
feature_stats: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
- updated_at: Mapped[datetime] = mapped_column(
- DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
- )
+ updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
- __table_args__ = (
- Index("ix_ueba_peergroup_tenant", "tenant_id", "entity_type"),
- )
+ __table_args__ = (Index("ix_ueba_peergroup_tenant", "tenant_id", "entity_type"),)
diff --git a/services/ueba/app/services/baseline.py b/services/ueba/app/services/baseline.py
index a1d732e19..7325a76fb 100644
--- a/services/ueba/app/services/baseline.py
+++ b/services/ueba/app/services/baseline.py
@@ -4,12 +4,12 @@ Maintains a rolling Welford online-statistics baseline per entity.
On each call to ``update_baseline`` the running mean and variance are
updated without having to re-read the full window of historical events.
"""
+
from __future__ import annotations
import math
import uuid
-from datetime import datetime, timedelta, timezone
-from typing import Any
+from datetime import UTC, datetime, timedelta
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
@@ -17,11 +17,11 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.core.config import settings
from app.models.ueba import EntityBaseline
-
# ---------------------------------------------------------------------------
# Welford online statistics helpers
# ---------------------------------------------------------------------------
+
def _welford_update(
stats: dict[str, dict[str, float]],
feature: str,
@@ -74,6 +74,7 @@ def compute_z_score(
# DB-backed baseline service
# ---------------------------------------------------------------------------
+
class BaselineService:
def __init__(self, session: AsyncSession) -> None:
self._db = session
@@ -93,7 +94,7 @@ class BaselineService:
)
row = result.scalar_one_or_none()
if row is None:
- now = datetime.now(timezone.utc)
+ now = datetime.now(UTC)
row = EntityBaseline(
tenant_id=tenant_id,
entity_type=entity_type,
@@ -121,7 +122,7 @@ class BaselineService:
stats = _welford_update(stats, feature, value)
baseline.feature_stats = stats
- baseline.window_end = datetime.now(timezone.utc)
+ baseline.window_end = datetime.now(UTC)
await self._db.flush()
return baseline
diff --git a/services/ueba/app/services/kafka_consumer.py b/services/ueba/app/services/kafka_consumer.py
index 2b2918ecf..50323ecd0 100644
--- a/services/ueba/app/services/kafka_consumer.py
+++ b/services/ueba/app/services/kafka_consumer.py
@@ -30,20 +30,19 @@ Anomalies are published to ``ueba.anomalies``:
"detected_at": "2026-05-03T12:00:01Z"
}
"""
+
from __future__ import annotations
-import asyncio
import json
import logging
import uuid
-from datetime import datetime, timezone
from typing import Any
-from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
+from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.core.config import settings
-from app.services.scoring import ScoringService
from app.services.peer_group import PeerGroupService
+from app.services.scoring import ScoringService
LOG = logging.getLogger(__name__)
@@ -127,7 +126,10 @@ class UEBAKafkaConsumer:
)
LOG.info(
"Anomaly emitted: entity=%s/%s score=%.2f risk=%s",
- entity_type, entity_id, anomaly.anomaly_score, anomaly.risk_level,
+ entity_type,
+ entity_id,
+ anomaly.anomaly_score,
+ anomaly.risk_level,
)
async def run(self) -> None:
diff --git a/services/ueba/app/services/peer_group.py b/services/ueba/app/services/peer_group.py
index 5335c9019..d6baab028 100644
--- a/services/ueba/app/services/peer_group.py
+++ b/services/ueba/app/services/peer_group.py
@@ -4,11 +4,11 @@ Entities are grouped by an externally-supplied ``peer_group_id`` (e.g. derived
from HR department, role, or subnet). The service maintains aggregate statistics
for each group and computes how far an individual event deviates from group norms.
"""
+
from __future__ import annotations
import math
import uuid
-from typing import Any
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
@@ -101,5 +101,5 @@ class PeerGroupService:
if not z_scores:
return None
- rss = math.sqrt(sum(z ** 2 for z in z_scores))
+ rss = math.sqrt(sum(z**2 for z in z_scores))
return min(rss, 10.0)
diff --git a/services/ueba/app/services/scoring.py b/services/ueba/app/services/scoring.py
index ef53098e1..2fc53ed58 100644
--- a/services/ueba/app/services/scoring.py
+++ b/services/ueba/app/services/scoring.py
@@ -3,18 +3,16 @@
Combines per-feature z-scores into a composite risk score and classifies
events as low / medium / high / critical anomalies.
"""
+
from __future__ import annotations
import math
import uuid
-from datetime import datetime, timezone
-from typing import Any
-from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.config import settings
-from app.models.ueba import UEBAAnomaly, PeerGroup
+from app.models.ueba import UEBAAnomaly
from app.services.baseline import BaselineService
from app.services.peer_group import PeerGroupService
@@ -56,9 +54,7 @@ class ScoringService:
"""Score an event. Returns a persisted ``UEBAAnomaly`` if anomalous, else None."""
# 1. Score against personal baseline
- scored = await self._baseline.score_features(
- tenant_id, entity_type, entity_id, features
- )
+ scored = await self._baseline.score_features(tenant_id, entity_type, entity_id, features)
composite = _composite_score(scored)
# 2. Update baseline with this event
@@ -67,9 +63,7 @@ class ScoringService:
# 3. Peer-group deviation
peer_dev: float | None = None
if peer_group_id:
- peer_dev = await self._peer.deviation_score(
- tenant_id, peer_group_id, features
- )
+ peer_dev = await self._peer.deviation_score(tenant_id, peer_group_id, features)
# Blend peer deviation into composite
if peer_dev is not None:
composite = (composite + peer_dev) / 2
diff --git a/tools/__init__.py b/tools/__init__.py
new file mode 100644
index 000000000..3eecb6fab
--- /dev/null
+++ b/tools/__init__.py
@@ -0,0 +1,6 @@
+"""Repo-level tools (detection importers, dataset generators, etc.).
+
+Each subpackage is independent. Imports assume the repo root is on
+``sys.path`` (true when invoked via ``python3 -m tools.<subpackage>.<module>``
+from the repo root).
+"""