How dshapi/ai-spm works
1. Overall shape
- This repository is a multi-service AI security posture management platform.
- It splits into a user-facing chat/control path, a security telemetry and policy plane, and a compliance/analytics plane.
- Most backend services are Python 3.11 with FastAPI; the frontend is a React/Vite single-page app. Docker Compose is the deployment boundary.
2. User-facing entrypoints
- There are two primary UIs: the Orbyx Chat UI for interacting with the LLM gateway, and the admin portal for posture, inventory, cases, policies, and simulation views.
- The chat UI talks to the main API gateway. The admin UI talks mostly to the SPM APIs and consumes metrics/event-derived views.
- An optional auth overlay adds Traefik + Keycloak + forward-auth in front of the admin portal for local SSO.
3. Main request path for prompts
- User prompts enter the services/api gateway, which performs JWT auth, per-user rate limiting, prompt/security screening, OPA policy checks, and memory lookup.
- The gateway then calls the external LLM provider, currently Anthropic-compatible models, and may also call Tavily for web search or fetch tools.
- Responses go back through a second-pass output safety layer before being returned to the user.
4. Prompt security boundary
- Prompt safety is enforced by multiple layers rather than a single filter.
- guard-model handles content moderation, memory-service screens stored context for injection patterns, and OPA policies decide allow/escalate/block based on posture and auth context.
- output_guard and output-scanning logic in the API protect the egress side, especially for secrets and PII.
5. Event backbone
- Kafka is the central asynchronous bus. Tenant-scoped topics carry raw events, posture-enriched events, decisions, tool requests/results, memory events, approvals, freeze controls, and audit data.
- This is the main decoupling boundary: the interactive API emits events, while analytics, enforcement, and audit consumers react independently.
- Tenant isolation is structural: topic names, audit rows, and metrics are all tenant-scoped.
6. Telemetry enrichment and enforcement
- The processor enriches raw events with posture scoring and intent analysis.
- The policy decider evaluates OPA rules and emits enforcement actions such as allow, escalate, or block.
- The Flink CEP service adds behavioral analytics like burst detection, sustained-volume detection, and signal correlation for suspicious sessions.
7. Stateful services and persistence
- Redis stores rate-limit counters and conversation memory with TTL-based namespaces.
- PostgreSQL stores the SPM domain data: model registry, compliance evidence, posture snapshots, findings, and exported audit records.
- Alembic migrations exist under the SPM service and the agent-orchestrator service, showing the database schema is a first-class part of the architecture.
8. SPM control plane
- spm-api exposes the model registry, compliance reporting, posture queries, and model approval/freeze-related functionality.
- spm-aggregator consumes Kafka events and materializes them into Postgres plus Prometheus metrics.
- This plane is what drives the compliance dashboards and provides the authoritative record of approved models, posture history, and audit exports.
9. Tooling and agentic execution
- The platform supports agentic tool use through tool_parser, executor, and related agent orchestration code.
- Tool calls are not executed directly from the model: they are parsed, policy-checked, and often require approval before side-effecting actions run.
- Tool results feed back into Kafka and the agent flow, which keeps tool execution observable and auditable.
10. Autonomous threat hunting path
- The threat-hunting-agent is a separate autonomous scanner that does not depend on a user prompt.
- It consumes Kafka session/events and also runs scheduled scans across Redis, Postgres, and host/runtime signals.
- When it finds suspicious patterns, it posts structured threat findings into the agent-orchestrator-service, which deduplicates, prioritizes, and can open cases.
11. Policy and governance layer
- OPA is the policy engine for prompt, output, tool, memory, model, and jailbreak-related controls.
- The policy-simulator service lets you dry-run policy changes against sample events without affecting live traffic.
- The freeze-controller is a governance override: it can suspend users or models in real time via Kafka control events.
12. Observability and compliance
- Prometheus scrapes metrics from the services, and Grafana is pre-provisioned with engineering, compliance, and posture dashboards.
- Compliance is mapped to NIST AI RMF functions, and threat behavior can be tagged with MITRE ATLAS techniques.
- The audit trail is designed to be tamper-evident by writing to Kafka and mirroring into Postgres with idempotent export semantics.
13. Startup and bootstrap
- startup-orchestrator is the bootstrap boundary for the whole stack.
- On first boot it generates RSA keys for JWTs, creates Kafka topics, seeds OPA policies, and registers default models.
- That means the platform is intended to self-provision locally without depending on an external identity or provisioning system.
14. Shared code and configuration
- platform_shared contains cross-cutting primitives like config, Kafka helpers, security, policy explanation, risk scoring, and model/event schemas.
- The repo also contains shared policy bundles under opa/, dashboard provisioning under grafana/, and Prometheus config under prometheus/.
- Those directories are important architectural boundaries because they define platform-wide behavior rather than a single service.
Main entry points: api API gateway · topics.py Kafka bus · policies OPA policies · db Postgres
Components 24
User apps
Human-facing chat and admin interfaces.
- Chat UI · Orbyx chat interface for user prompts and session views.
ui/src - Admin portal · Posture, inventory, cases, policies, simulation, and analytics views.
ui/src/admin - Auth overlay · Optional local SSO front door for the admin UI.
auth
Interactive path
Prompt intake, safety checks, retrieval, and tool execution.
- API gateway · Main interactive entrypoint for prompts, auth, rate limiting, screening, memory, and response safety.
services/api - Memory service · Conversation memory and injection-aware context screening.
services/memory_service - Guard model · Content moderation and safety classification for prompts and events.
services/guard_model - Retrieval gateway · Retrieval and web-search/fetch boundary used by the prompt path.
services/retrieval_gateway - LLM provider · External model endpoint used for generation.
services/llm - Output guard · Second-pass scanning for secrets, PII, and unsafe output.
services/output_guard
Policy plane
Policy, enforcement, and autonomous threat hunting.
- Processor · Adds posture scoring and intent analysis to raw events.
services/processor - Policy decider · Evaluates rules and emits allow, escalate, or block decisions.
services/policy_decider - Policy simulator · Tests policy changes against sample events without impacting live traffic.
services/policy_simulator - Freeze controller · Real-time suspension of users or models via control events.
services/freeze_controller - Threat hunter · Scheduled and event-driven scanner for suspicious sessions and runtime signals.
services/threat-hunting-agent
SPM plane
Compliance, posture, registry, and audit materialization.
- Agent orchestrator · Deduplicates, prioritizes, and routes findings into cases and results.
services/agent-orchestrator-service - SPM API · Model registry, posture queries, compliance reports, approvals, and freezes.
services/spm_api - SPM aggregator · Materializes telemetry into Postgres and Prometheus metrics.
services/spm_aggregator
Platform layer
Shared libraries, storage, messaging, and bootstrap.
- Startup orchestrator · Seeds keys, topics, policies, and default models on first boot.
services/startup_orchestrator - Kafka bus · Tenant-scoped async bus for events, decisions, approvals, audit, and tool data.
platform_shared/topics.py - Postgres · Stores SPM domain data, findings, posture snapshots, and audit exports.
spm/db - Redis · Holds rate-limit counters and conversation memory namespaces.
services/memory_service - Shared core · Config, Kafka helpers, security, policy explanation, risk scoring, and schemas.
platform_shared - OPA policies · Shared policy rules for prompts, outputs, tools, memory, model, PII, and jailbreak control.
opa/policies - Grafana dashboards · Engineering and compliance dashboards provisioned with shared datasources.
grafana/dashboards
Connections 30
- Chat UI → API gateway: prompts · Chat sessions send user messages into the gateway.
- Admin portal → SPM API: control views · Admin portal reads posture, registry, and compliance views.
- Auth overlay → Admin portal: protects · Optional SSO overlay in front of the admin portal.
- API gateway → Memory service: lookup · Retrieves and screens conversation memory.
- API gateway → Guard model: moderates · Runs prompt safety classification.
- API gateway → Policy decider: policy check · Sends context for prompt and tool authorization decisions.
- API gateway → Retrieval gateway: search/fetch · Routes retrieval and web-search tool calls.
- API gateway → LLM provider: generate · Calls the external model provider for completion.
- API gateway → Output guard: egress scan · Applies output safety before returning a response.
- API gateway → Kafka bus: emit events · Publishes session, decision, and audit events to Kafka.
- Processor → Kafka bus: consume/emit · Consumes raw events and publishes enriched telemetry.
- Policy decider → Kafka bus: decisions · Writes allow, escalate, and block actions to the bus.
- Policy simulator → OPA policies: dry-run · Evaluates candidate rules against sample events.
- Freeze controller → Kafka bus: controls · Broadcasts suspension and freeze events.
- Threat hunter → Kafka bus: consume · Scans session and event streams for suspicious activity.
- Threat hunter → Agent orchestrator: findings · Submits structured threat findings for triage.
- Agent orchestrator → Postgres: persist · Stores findings, cases, sessions, policies, and audit-linked records.
- Agent orchestrator → Kafka bus: publish · Emits events for downstream consumers and auditability.
- SPM API → Postgres: query/write · Reads and updates authoritative SPM state.
- SPM aggregator → Kafka bus: consume · Materializes tenant-scoped telemetry from the event bus.
- SPM aggregator → Postgres: materialize · Writes compliance and posture aggregates to Postgres.
- Startup orchestrator → Kafka bus: seed topics · Creates the tenant-scoped topics used by the platform.
- Startup orchestrator → OPA policies: seed rules · Bootstraps default policy bundles.
- Startup orchestrator → Postgres: init data · Registers baseline models and platform records.
- Shared core → API gateway: shared code · Common security, config, and Kafka primitives.
- Shared core → SPM API: shared code · Shared schemas and helpers used across the control plane.
- Shared core → Threat hunter: shared code · Shared risk, trust, and policy utilities.
- OPA policies → API gateway: governs · Policy bundles inform prompt, output, tool, and memory checks.
- OPA policies → Policy decider: governs · OPA rules drive enforcement decisions.
- Grafana dashboards → SPM aggregator: visualizes · Dashboards consume the metrics materialized by the aggregator.