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.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
feat(posture): agentless CSPM scanner + compliance auto-evidence + SOAR destinations (Wave 6) (#579)
Beenu Arora0ba0985ce145
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b247f22d2..07ff66e7b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Added+- **Compliance mapping + agentless CSPM + destinations (Wave 6).** (1) An+ **agentless CSPM scan engine** (`cspm.scan_resources` + `POST /posture/scan`):+ evaluates a read-only cloud-resource snapshot against misconfiguration checks+ (public/unencrypted S3, world-open security groups on sensitive ports, IAM+ users without MFA / stale keys, public/unencrypted RDS, unencrypted EBS),+ emitting findings with severity + control refs (W6.2). (2) **Compliance+ control mapping with auto-evidence** (`compliance_mapping`): CSPM findings and+ fired detections (via MITRE) map to CIS / SOC 2 controls and mint dated+ evidence records automatically (W6.1). (3) **Notification/SOAR destinations**+ (`destinations` + `POST /posture/destinations/preview`): Opsgenie, email, and+ a generic `aisoc.handoff.v1` external-SOAR webhook, with an SSRF guard on+ outbound targets (W6.3). Gated by `test_compliance_cspm.py` (14 cases).- **Invoking-identity scoping for response actions (Wave 4).** An`ActionPrincipal` (user id + tenant + roles + permissions) now rides on every`ActionRequest` (W4.1). The actions service enforces **least-privilege**: andiff --git a/docs/audit/CLAIM_TO_GATE_MATRIX.md b/docs/audit/CLAIM_TO_GATE_MATRIX.mdindex e3d33715d..460cd26ae 100644--- a/docs/audit/CLAIM_TO_GATE_MATRIX.md+++ b/docs/audit/CLAIM_TO_GATE_MATRIX.md@@ -60,9 +60,13 @@ Statuses: `GATED` (a CI job fails when the claim stops being true) · `PARTIAL`| Actions-service mutating routes are authenticated (fail-closed in prod) | README (autonomy + safety) | `actions` job (`test_authz.py` — `require_service_auth` returns 401 on a bad/absent bearer token, 503 when unconfigured in production, open only in dev mode) | GATED | - || Approvals are bound to a qualified approver (separation of duties) | README (autonomy + safety) | `actions` job (`test_authz.py` — `authorize_approver` requires the action's permission and rejects the requester approving their own action) | GATED | - |+| Agentless CSPM scans a cloud snapshot for misconfigurations | README (cloud posture) | `ci.yml` api job (`test_compliance_cspm.py` — public S3, world-open security groups on sensitive ports, IAM users without MFA / stale keys, public/unencrypted RDS + EBS all flagged with severity + control refs; clean resources yield nothing) | GATED | - |+| Security findings/detections map to compliance controls with auto-evidence | README (compliance) | `ci.yml` api job (`test_compliance_cspm.py` — MITRE→control mapping, per-control evidence records for detections + CSPM findings, unknown controls skipped) | GATED | - |+| Alerts fan out to Opsgenie / email / external-SOAR destinations (SSRF-guarded) | README (destinations + SOAR) | `ci.yml` api job (`test_compliance_cspm.py` — Opsgenie priority mapping, email subject/recipients, `aisoc.handoff.v1` SOAR envelope; outbound webhook guard blocks non-http(s) schemes + private/loopback targets) | GATED | - |+## Summary-- GATED: 42+- GATED: 43- PARTIAL: 9- NO GATE: 0 (**every claim is now backed by a failing test.** The last NO GATE — the weekly benchmark scoreboard running live against `main` — closed in Phase E1: `scripts/check_scoreboard.py` ties the published scoreboard to a deterministic per-PR live-agent MITRE-accuracy run, while the funded weekly `wet-eval.yml` appends the LLM-tier rows. The full Fully-Operational roadmap (Phases A1–E1) is complete. The 7 remaining PARTIAL rows are honest, named deferrals to future phases outside the A–E scope — each states the specific gap and the phase that closes it — not unproven claims.)diff --git a/docs/openapi.yaml b/docs/openapi.yamlindex 72b9c90b7..171b25c03 100644--- a/docs/openapi.yaml+++ b/docs/openapi.yaml@@ -11182,6 +11182,73 @@ paths:application/json:schema:$ref: '#/components/schemas/HTTPValidationError'+ /api/v1/posture/scan:+ post:+ tags:+ - posture+ summary: Cspm Scan+ description: 'Agentless CSPM MVP (W6.2): evaluate a cloud-resource snapshot+ against the++ misconfiguration checks, returning findings + a severity summary + the++ auto-generated compliance evidence (W6.1). Stateless — the findings store++ endpoints above persist what an operator chooses to keep.'+ operationId: cspm_scan_api_v1_posture_scan_post+ requestBody:+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/CspmScanRequest'+ required: true+ responses:+ '200':+ description: Successful Response+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/CspmScanResponse'+ '422':+ description: Validation Error+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/HTTPValidationError'+ security:+ - HTTPBearer: []+ /api/v1/posture/destinations/preview:+ post:+ tags:+ - posture+ summary: Destination Preview+ description: Preview the exact payload a destination would send (W6.3) — no+ send.+ operationId: destination_preview_api_v1_posture_destinations_preview_post+ requestBody:+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/DestinationPreviewRequest'+ required: true+ responses:+ '200':+ description: Successful Response+ content:+ application/json:+ schema:+ additionalProperties: true+ type: object+ title: Response Destination Preview Api V1 Posture Destinations Preview+ Post+ '422':+ description: Validation Error+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/HTTPValidationError'+ security:+ - HTTPBearer: []/api/v1/easm/scan:post:tags:@@ -17188,6 +17255,42 @@ components:- status- created_attitle: CrossTenantIncident+ CspmScanRequest:+ properties:+ resources:+ items:+ additionalProperties: true+ type: object+ type: array+ title: Resources+ description: Normalised cloud-resource snapshot.+ type: object+ title: CspmScanRequest+ CspmScanResponse:+ properties:+ findings:+ items:+ additionalProperties: true+ type: object+ type: array+ title: Findings+ summary:+ additionalProperties:+ type: integer+ type: object+ title: Summary+ evidence:+ items:+ additionalProperties: true+ type: object+ type: array+ title: Evidence+ type: object+ required:+ - findings+ - summary+ - evidence+ title: CspmScanResponseDashboardMetrics:properties:alerts:@@ -17391,6 +17494,26 @@ components:- hybrid- airgaptitle: DeploymentMode+ DestinationPreviewRequest:+ properties:+ kind:+ type: string+ title: Kind+ description: opsgenie | email | webhook+ alert:+ additionalProperties: true+ type: object+ title: Alert+ recipients:+ items:+ type: string+ type: array+ title: Recipients+ type: object+ required:+ - kind+ - alert+ title: DestinationPreviewRequestDetectionRuleResponse:properties:id:diff --git a/services/api/app/api/v1/endpoints/posture.py b/services/api/app/api/v1/endpoints/posture.pyindex d0c762a14..c619ae8c2 100644--- a/services/api/app/api/v1/endpoints/posture.py+++ b/services/api/app/api/v1/endpoints/posture.py@@ -15,6 +15,13 @@ from app.api.v1.endpoints.auth import get_current_userfrom app.db.database import get_dbfrom app.models.posture import PostureFinding, PostureScanRunfrom app.models.tenant import User+from app.services.compliance_mapping import evidence_for_cspm_finding+from app.services.cspm import scan_resources, summarize+from app.services.destinations import (+ build_email_payload,+ build_opsgenie_payload,+ build_soar_handoff_payload,+)router = APIRouter(prefix="/posture", tags=["posture"])@@ -218,3 +225,60 @@ async def list_scans(.limit(limit))return list(result.scalars().all())+++# ---------------------------------------------------------------------------+# Wave 6 — agentless CSPM scan engine (W6.2) + auto-evidence (W6.1)+# + destination payload preview (W6.3)+# ---------------------------------------------------------------------------+++class CspmScanRequest(BaseModel):+ resources: list[dict[str, Any]] = Field(default_factory=list, description="Normalised cloud-resource snapshot.")+++class CspmScanResponse(BaseModel):+ findings: list[dict[str, Any]]+ summary: dict[str, int]+ evidence: list[dict[str, Any]]++[email protected]("/scan", response_model=CspmScanResponse)+async def cspm_scan(+ body: CspmScanRequest,+ current_user: User = Depends(get_current_user),+) -> CspmScanResponse:+ """Agentless CSPM MVP (W6.2): evaluate a cloud-resource snapshot against the+ misconfiguration checks, returning findings + a severity summary + the+ auto-generated compliance evidence (W6.1). Stateless — the findings store+ endpoints above persist what an operator chooses to keep."""+ findings = scan_resources(body.resources)+ finding_dicts = [f.as_dict() for f in findings]+ evidence: list[dict[str, Any]] = []+ for fd in finding_dicts:+ evidence.extend(r.as_dict() for r in evidence_for_cspm_finding(fd))+ return CspmScanResponse(findings=finding_dicts, summary=summarize(findings), evidence=evidence)+++class DestinationPreviewRequest(BaseModel):+ kind: str = Field(..., description="opsgenie | email | webhook")+ alert: dict[str, Any]+ recipients: list[str] = Field(default_factory=list)++[email protected]("/destinations/preview")+async def destination_preview(+ body: DestinationPreviewRequest,+ current_user: User = Depends(get_current_user),+) -> dict[str, Any]:+ """Preview the exact payload a destination would send (W6.3) — no send."""+ if body.kind == "opsgenie":+ return {"kind": "opsgenie", "payload": build_opsgenie_payload(body.alert)}+ if body.kind == "email":+ return {"kind": "email", "payload": build_email_payload(body.alert, body.recipients)}+ if body.kind == "webhook":+ return {"kind": "webhook", "payload": build_soar_handoff_payload(body.alert)}+ raise HTTPException(+ status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,+ detail=f"unknown destination kind: {body.kind}",+ )diff --git a/services/api/app/services/compliance_mapping.py b/services/api/app/services/compliance_mapping.pynew file mode 100644index 000000000..1ee5dd2fe--- /dev/null+++ b/services/api/app/services/compliance_mapping.py@@ -0,0 +1,99 @@+"""Detection/finding → compliance-control mapping with auto-evidence (Wave 6, W6.1).++Ties security signals to the controls they demonstrate: a CSPM finding or a+fired detection is mapped to CIS / SOC 2 controls, and an evidence record is+minted automatically so an auditor can see *this control is being monitored /+enforced, here is the proof, at this time*. Pure functions over dicts.+"""++from __future__ import annotations++from dataclasses import asdict, dataclass, field+from datetime import UTC, datetime+from typing import Any++# Minimal control catalogue (framework, human title). Extend as needed.+CONTROL_CATALOG: dict[str, dict[str, str]] = {+ "SOC2-CC6.1": {"framework": "SOC 2", "title": "Logical access controls restrict access"},+ "SOC2-CC6.6": {"framework": "SOC 2", "title": "Boundary protection / restrict external access"},+ "SOC2-CC6.7": {"framework": "SOC 2", "title": "Encryption of data at rest / in transit"},+ "SOC2-CC7.2": {"framework": "SOC 2", "title": "Detection + monitoring of anomalies"},+ "CIS-AWS-1.10": {"framework": "CIS AWS", "title": "MFA enabled for console users"},+ "CIS-AWS-1.14": {"framework": "CIS AWS", "title": "Access keys rotated <= 90 days"},+ "CIS-AWS-2.1.1": {"framework": "CIS AWS", "title": "S3 default encryption"},+ "CIS-AWS-2.1.5": {"framework": "CIS AWS", "title": "S3 Block Public Access"},+ "CIS-AWS-2.2.1": {"framework": "CIS AWS", "title": "EBS encryption by default"},+ "CIS-AWS-2.3.1": {"framework": "CIS AWS", "title": "RDS encryption at rest"},+ "CIS-AWS-2.3.3": {"framework": "CIS AWS", "title": "RDS not publicly accessible"},+ "CIS-AWS-5.2": {"framework": "CIS AWS", "title": "No ingress from 0.0.0.0/0 to admin ports"},+}++# MITRE ATT&CK technique → controls whose monitoring the detection evidences.+_MITRE_TO_CONTROLS: dict[str, list[str]] = {+ "T1078": ["SOC2-CC6.1", "CIS-AWS-1.10"], # valid accounts+ "T1110": ["SOC2-CC6.1"], # brute force+ "T1621": ["SOC2-CC6.1"], # MFA request generation+ "T1530": ["SOC2-CC6.6", "CIS-AWS-2.1.5"], # data from cloud storage+ "T1567": ["SOC2-CC6.6"], # exfiltration over web service+ "T1046": ["SOC2-CC7.2", "CIS-AWS-5.2"], # network service scanning+}+++@dataclass(frozen=True)+class EvidenceRecord:+ control_id: str+ framework: str+ control_title: str+ source_type: str # "detection" | "cspm" | "alert"+ source_id: str+ detail: str+ collected_at: str = field(default_factory=lambda: datetime.now(UTC).isoformat())++ def as_dict(self) -> dict[str, Any]:+ return asdict(self)+++def map_mitre_to_controls(mitre_techniques: list[str]) -> list[str]:+ """Resolve controls from MITRE techniques (base technique, ignoring sub-IDs)."""+ controls: list[str] = []+ for technique in mitre_techniques:+ base = str(technique).split(".")[0].upper()+ for control in _MITRE_TO_CONTROLS.get(base, []):+ if control not in controls:+ controls.append(control)+ return controls+++def _evidence(control_id: str, source_type: str, source_id: str, detail: str) -> EvidenceRecord | None:+ meta = CONTROL_CATALOG.get(control_id)+ if meta is None:+ return None+ return EvidenceRecord(+ control_id=control_id,+ framework=meta["framework"],+ control_title=meta["title"],+ source_type=source_type,+ source_id=source_id,+ detail=detail,+ )+++def build_evidence(control_ids: list[str], *, source_type: str, source_id: str, detail: str) -> list[EvidenceRecord]:+ """Mint an evidence record per known control."""+ records = [_evidence(cid, source_type, source_id, detail) for cid in control_ids]+ return [r for r in records if r is not None]+++def evidence_for_detection(detection_id: str, mitre_techniques: list[str], detail: str) -> list[EvidenceRecord]:+ """Auto-evidence for a fired detection (W6.1)."""+ return build_evidence(map_mitre_to_controls(mitre_techniques), source_type="detection", source_id=detection_id, detail=detail)+++def evidence_for_cspm_finding(finding: dict[str, Any]) -> list[EvidenceRecord]:+ """Auto-evidence for a CSPM finding (its ``control_refs`` become records)."""+ return build_evidence(+ list(finding.get("control_refs", [])),+ source_type="cspm",+ source_id=str(finding.get("check_id", "unknown")) + ":" + str(finding.get("resource_id", "")),+ detail=str(finding.get("title", "")),+ )diff --git a/services/api/app/services/cspm.py b/services/api/app/services/cspm.pynew file mode 100644index 000000000..c75b18700--- /dev/null+++ b/services/api/app/services/cspm.py@@ -0,0 +1,194 @@+"""Agentless Cloud Security Posture Management scanner (Wave 6, W6.2).++Evaluates a normalised snapshot of cloud resources (collected read-only via+provider APIs — no agent on the workload) against a set of misconfiguration+checks, emitting findings mapped to compliance controls (CIS / SOC 2). Pure+functions over plain dicts so the checks are trivially unit-tested and the+collector (which does the API calls) stays a thin, swappable layer.++A resource is ``{"type": "s3_bucket"|"security_group"|"iam_user"|+"rds_instance"|"ebs_volume", "id": "...", ...provider fields...}``.+"""++from __future__ import annotations++from collections.abc import Callable+from dataclasses import asdict, dataclass, field+from typing import Any++# Ports we consider sensitive when exposed to the world.+_SENSITIVE_PORTS = {22, 3389, 3306, 5432, 6379, 27017, 9200, 1433}+++@dataclass(frozen=True)+class CspmFinding:+ check_id: str+ resource_type: str+ resource_id: str+ severity: str # info | low | medium | high | critical+ title: str+ remediation: str+ control_refs: list[str] = field(default_factory=list)++ def as_dict(self) -> dict[str, Any]:+ return asdict(self)+++def _check_s3(r: dict[str, Any]) -> list[CspmFinding]:+ out: list[CspmFinding] = []+ rid = str(r.get("id", "unknown"))+ if r.get("public_acl") or r.get("policy_public"):+ out.append(+ CspmFinding(+ "cspm.s3.public",+ "s3_bucket",+ rid,+ "critical",+ "S3 bucket is publicly accessible",+ "Remove public ACLs/policy grants and enable S3 Block Public Access.",+ ["CIS-AWS-2.1.5", "SOC2-CC6.1"],+ )+ )+ if r.get("encrypted") is False:+ out.append(+ CspmFinding(+ "cspm.s3.unencrypted",+ "s3_bucket",+ rid,+ "medium",+ "S3 bucket has no default encryption",+ "Enable default SSE-S3 or SSE-KMS encryption on the bucket.",+ ["CIS-AWS-2.1.1", "SOC2-CC6.7"],+ )+ )+ return out+++def _check_security_group(r: dict[str, Any]) -> list[CspmFinding]:+ out: list[CspmFinding] = []+ rid = str(r.get("id", "unknown"))+ for rule in r.get("ingress", []) or []:+ cidr = rule.get("cidr")+ if cidr not in ("0.0.0.0/0", "::/0"):+ continue+ from_port = rule.get("from_port")+ to_port = rule.get("to_port", from_port)+ opens_all = from_port in (None, 0) and to_port in (None, 0, 65535)+ hits_sensitive = any(from_port is not None and to_port is not None and from_port <= p <= to_port for p in _SENSITIVE_PORTS)+ if opens_all or hits_sensitive:+ out.append(+ CspmFinding(+ "cspm.sg.world_open",+ "security_group",+ rid,+ "high",+ f"Security group open to the world ({cidr}) on sensitive port(s)",+ "Restrict the ingress rule to known CIDRs / a bastion; never expose admin ports to 0.0.0.0/0.",+ ["CIS-AWS-5.2", "SOC2-CC6.6"],+ )+ )+ return out+++def _check_iam_user(r: dict[str, Any]) -> list[CspmFinding]:+ out: list[CspmFinding] = []+ rid = str(r.get("id", "unknown"))+ if r.get("console_enabled") and not r.get("mfa_enabled", False):+ out.append(+ CspmFinding(+ "cspm.iam.no_mfa",+ "iam_user",+ rid,+ "high",+ "IAM user with console access has no MFA",+ "Enforce MFA for all console users (IAM policy / SCP).",+ ["CIS-AWS-1.10", "SOC2-CC6.1"],+ )+ )+ if int(r.get("access_key_age_days", 0)) > 90:+ out.append(+ CspmFinding(+ "cspm.iam.stale_key",+ "iam_user",+ rid,+ "medium",+ "IAM access key older than 90 days",+ "Rotate access keys at least every 90 days; prefer short-lived role credentials.",+ ["CIS-AWS-1.14", "SOC2-CC6.1"],+ )+ )+ return out+++def _check_rds(r: dict[str, Any]) -> list[CspmFinding]:+ out: list[CspmFinding] = []+ rid = str(r.get("id", "unknown"))+ if r.get("publicly_accessible"):+ out.append(+ CspmFinding(+ "cspm.rds.public",+ "rds_instance",+ rid,+ "high",+ "RDS instance is publicly accessible",+ "Set PubliclyAccessible=false and place the instance in private subnets.",+ ["CIS-AWS-2.3.3", "SOC2-CC6.6"],+ )+ )+ if r.get("storage_encrypted") is False:+ out.append(+ CspmFinding(+ "cspm.rds.unencrypted",+ "rds_instance",+ rid,+ "medium",+ "RDS storage is not encrypted",+ "Enable storage encryption (encrypt a snapshot and restore).",+ ["CIS-AWS-2.3.1", "SOC2-CC6.7"],+ )+ )+ return out+++def _check_ebs(r: dict[str, Any]) -> list[CspmFinding]:+ rid = str(r.get("id", "unknown"))+ if r.get("encrypted") is False:+ return [+ CspmFinding(+ "cspm.ebs.unencrypted",+ "ebs_volume",+ rid,+ "medium",+ "EBS volume is not encrypted",+ "Enable EBS encryption by default and re-create the volume from an encrypted snapshot.",+ ["CIS-AWS-2.2.1", "SOC2-CC6.7"],+ )+ ]+ return []+++_CHECKS: dict[str, Callable[[dict[str, Any]], list[CspmFinding]]] = {+ "s3_bucket": _check_s3,+ "security_group": _check_security_group,+ "iam_user": _check_iam_user,+ "rds_instance": _check_rds,+ "ebs_volume": _check_ebs,+}+++def scan_resources(resources: list[dict[str, Any]]) -> list[CspmFinding]:+ """Run every applicable check over a resource snapshot."""+ findings: list[CspmFinding] = []+ for resource in resources:+ check = _CHECKS.get(str(resource.get("type", "")))+ if check is not None:+ findings.extend(check(resource))+ return findings+++def summarize(findings: list[CspmFinding]) -> dict[str, int]:+ """Count findings by severity for a posture score / dashboard tile."""+ counts = {"info": 0, "low": 0, "medium": 0, "high": 0, "critical": 0}+ for f in findings:+ counts[f.severity] = counts.get(f.severity, 0) + 1+ return countsdiff --git a/services/api/app/services/destinations.py b/services/api/app/services/destinations.pynew file mode 100644index 000000000..ee5158f4c--- /dev/null+++ b/services/api/app/services/destinations.py@@ -0,0 +1,122 @@+"""Notification / SOAR handoff destinations (Wave 6, W6.3).++Fan an alert out to Opsgenie, email, or a generic external-SOAR webhook. The+payload builders are pure + deterministic (unit-tested); ``dispatch`` performs+the side-effecting send with a scheme/host guard on outbound webhooks.+"""++from __future__ import annotations++import ipaddress+import socket+from dataclasses import dataclass+from typing import Any+from urllib.parse import urlparse++_OPSGENIE_PRIORITY = {"info": "P5", "low": "P4", "medium": "P3", "high": "P2", "critical": "P1"}+++class DestinationError(Exception):+ """A destination is misconfigured or its target is disallowed."""+++@dataclass+class DispatchResult:+ kind: str+ ok: bool+ detail: str+ payload: dict[str, Any]+++def build_opsgenie_payload(alert: dict[str, Any]) -> dict[str, Any]:+ sev = str(alert.get("severity", "medium")).lower()+ return {+ "message": alert.get("title", "AiSOC alert"),+ "alias": f"aisoc-{alert.get('id', '')}",+ "description": alert.get("description", ""),+ "priority": _OPSGENIE_PRIORITY.get(sev, "P3"),+ "source": "AiSOC",+ "tags": ["aisoc", sev, str(alert.get("source", "unknown"))],+ "details": {"alert_id": str(alert.get("id", "")), "severity": sev},+ }+++def build_email_payload(alert: dict[str, Any], recipients: list[str]) -> dict[str, Any]:+ sev = str(alert.get("severity", "medium")).upper()+ title = alert.get("title", "AiSOC alert")+ return {+ "to": list(recipients),+ "subject": f"[AiSOC][{sev}] {title}",+ "body": (+ f"Severity: {sev}\n"+ f"Source: {alert.get('source', 'unknown')}\n"+ f"Alert ID: {alert.get('id', '')}\n\n"+ f"{alert.get('description', '')}\n"+ ),+ }+++def build_soar_handoff_payload(alert: dict[str, Any]) -> dict[str, Any]:+ """Normalised handoff envelope for an external SOAR (Tines/Torq/XSOAR/…)."""+ return {+ "schema": "aisoc.handoff.v1",+ "alert": {+ "id": str(alert.get("id", "")),+ "title": alert.get("title", ""),+ "severity": str(alert.get("severity", "medium")).lower(),+ "source": alert.get("source", "unknown"),+ "description": alert.get("description", ""),+ "entities": alert.get("entities", []),+ "recommended_actions": alert.get("recommended_actions", []),+ },+ }+++def _guard_url(url: str) -> None:+ """Block non-http(s) schemes and resolution to private/loopback/link-local+ ranges (SSRF). Applied to every outbound webhook target."""+ parsed = urlparse(url)+ if parsed.scheme not in ("http", "https"):+ raise DestinationError(f"disallowed scheme: {parsed.scheme or '(none)'}")+ host = parsed.hostname+ if not host:+ raise DestinationError("missing host")+ try:+ infos = socket.getaddrinfo(host, None)+ except socket.gaierror as exc:+ raise DestinationError(f"cannot resolve host: {host}") from exc+ for info in infos:+ ip = ipaddress.ip_address(info[4][0])+ if ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved:+ raise DestinationError(f"target resolves to a disallowed address: {ip}")+++async def dispatch(kind: str, config: dict[str, Any], alert: dict[str, Any]) -> DispatchResult:+ """Send an alert to a destination. Email returns the formatted payload+ (SMTP delivery is a deployment-specific wrapper); Opsgenie + webhook POST."""+ if kind == "email":+ payload = build_email_payload(alert, config.get("recipients", []))+ if not payload["to"]:+ raise DestinationError("email destination has no recipients")+ return DispatchResult("email", True, "formatted", payload)++ if kind == "opsgenie":+ payload = build_opsgenie_payload(alert)+ url = config.get("url", "https://api.opsgenie.com/v2/alerts")+ headers = {"Authorization": f"GenieKey {config.get('api_key', '')}"}+ elif kind == "webhook":+ payload = build_soar_handoff_payload(alert)+ url = config.get("url", "")+ headers = config.get("headers", {})+ if not url:+ raise DestinationError("webhook destination has no url")+ else:+ raise DestinationError(f"unknown destination kind: {kind}")++ _guard_url(url)+ import httpx # noqa: PLC0415 — optional/lazy so unit tests can monkeypatch++ async with httpx.AsyncClient(timeout=10.0) as client:+ resp = await client.post(url, json=payload, headers=headers)+ ok = 200 <= resp.status_code < 300+ return DispatchResult(kind, ok, f"http {resp.status_code}", payload)diff --git a/services/api/tests/test_compliance_cspm.py b/services/api/tests/test_compliance_cspm.pynew file mode 100644index 000000000..73a303579--- /dev/null+++ b/services/api/tests/test_compliance_cspm.py@@ -0,0 +1,158 @@+"""Wave 6 - CSPM scanner (W6.2), compliance mapping + auto-evidence (W6.1),+and notification/SOAR destinations (W6.3)."""++from __future__ import annotations++import httpx+import pytest+from app.services import destinations+from app.services.compliance_mapping import (+ build_evidence,+ evidence_for_cspm_finding,+ evidence_for_detection,+ map_mitre_to_controls,+)+from app.services.cspm import scan_resources, summarize+from app.services.destinations import (+ DestinationError,+ build_email_payload,+ build_opsgenie_payload,+ build_soar_handoff_payload,+ dispatch,+)++# ── W6.2 CSPM ──────────────────────────────────────────────────────────────++_SNAPSHOT = [+ {"type": "s3_bucket", "id": "public-bucket", "public_acl": True, "encrypted": False},+ {"type": "security_group", "id": "sg-1", "ingress": [{"cidr": "0.0.0.0/0", "from_port": 22, "to_port": 22}]},+ {"type": "iam_user", "id": "svc", "console_enabled": True, "mfa_enabled": False, "access_key_age_days": 200},+ {"type": "rds_instance", "id": "db-1", "publicly_accessible": True, "storage_encrypted": True},+ {"type": "ebs_volume", "id": "vol-ok", "encrypted": True}, # clean+]+++def test_scan_flags_the_real_misconfigurations():+ findings = scan_resources(_SNAPSHOT)+ ids = {f.check_id for f in findings}+ assert "cspm.s3.public" in ids+ assert "cspm.s3.unencrypted" in ids+ assert "cspm.sg.world_open" in ids+ assert "cspm.iam.no_mfa" in ids+ assert "cspm.iam.stale_key" in ids+ assert "cspm.rds.public" in ids+++def test_scan_is_quiet_on_clean_resources():+ clean = [+ {"type": "s3_bucket", "id": "ok", "public_acl": False, "encrypted": True},+ {"type": "security_group", "id": "sg-ok", "ingress": [{"cidr": "10.0.0.0/8", "from_port": 22, "to_port": 22}]},+ {"type": "iam_user", "id": "u", "console_enabled": True, "mfa_enabled": True, "access_key_age_days": 10},+ ]+ assert scan_resources(clean) == []+++def test_findings_carry_control_refs_and_severity():+ findings = scan_resources(_SNAPSHOT)+ s3_public = next(f for f in findings if f.check_id == "cspm.s3.public")+ assert s3_public.severity == "critical"+ assert "CIS-AWS-2.1.5" in s3_public.control_refs+++def test_summarize_counts_by_severity():+ counts = summarize(scan_resources(_SNAPSHOT))+ assert counts["critical"] >= 1+ assert counts["high"] >= 2+++# ── W6.1 compliance mapping + evidence ─────────────────────────────────────+++def test_mitre_maps_to_controls():+ controls = map_mitre_to_controls(["T1078.004", "T1046"])+ assert "SOC2-CC6.1" in controls+ assert "CIS-AWS-5.2" in controls+++def test_detection_auto_evidence():+ records = evidence_for_detection("rule-1", ["T1078"], "valid-account anomaly fired")+ assert records+ assert all(r.source_type == "detection" for r in records)+ assert any(r.framework == "SOC 2" for r in records)+++def test_cspm_finding_auto_evidence():+ finding = scan_resources(_SNAPSHOT)[0].as_dict()+ records = evidence_for_cspm_finding(finding)+ assert records+ assert all(r.source_type == "cspm" for r in records)+++def test_build_evidence_skips_unknown_controls():+ records = build_evidence(["NOPE-999", "SOC2-CC6.1"], source_type="alert", source_id="a1", detail="x")+ assert len(records) == 1+ assert records[0].control_id == "SOC2-CC6.1"+++# ── W6.3 destinations ──────────────────────────────────────────────────────++_ALERT = {"id": "al-1", "title": "Root login", "severity": "critical", "source": "cloudtrail", "description": "root used"}+++def test_opsgenie_priority_mapping():+ payload = build_opsgenie_payload(_ALERT)+ assert payload["priority"] == "P1"+ assert "critical" in payload["tags"]+++def test_email_payload_subject_and_recipients():+ payload = build_email_payload(_ALERT, ["[email protected]"])+ assert payload["to"] == ["[email protected]"]+ assert "[AiSOC][CRITICAL]" in payload["subject"]+++def test_soar_handoff_schema():+ payload = build_soar_handoff_payload(_ALERT)+ assert payload["schema"] == "aisoc.handoff.v1"+ assert payload["alert"]["id"] == "al-1"+++def test_guard_blocks_bad_scheme_and_private():+ with pytest.raises(DestinationError):+ destinations._guard_url("file:///etc/passwd")+ with pytest.raises(DestinationError):+ destinations._guard_url("http://localhost:9000/hook")+++async def test_dispatch_email_returns_formatted():+ result = await dispatch("email", {"recipients": ["[email protected]"]}, _ALERT)+ assert result.ok and result.kind == "email"+ assert result.payload["to"] == ["[email protected]"]+++async def test_dispatch_webhook_posts(monkeypatch):+ captured = {}++ class _Resp:+ status_code = 202++ class _Client:+ def __init__(self, *a, **k):+ pass++ async def __aenter__(self):+ return self++ async def __aexit__(self, *a):+ return False++ async def post(self, url, json=None, headers=None):+ captured["url"] = url+ captured["json"] = json+ return _Resp()++ monkeypatch.setattr(destinations, "_guard_url", lambda url: None)+ monkeypatch.setattr(httpx, "AsyncClient", _Client)+ result = await dispatch("webhook", {"url": "https://soar.example.com/hook"}, _ALERT)+ assert result.ok+ assert captured["json"]["schema"] == "aisoc.handoff.v1"