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(threatintel): OpenCTI feed client validated against live platform

Alexandru Mateescu4b3357eaa94f
diff --git a/.env.example b/.env.example
index 1b818a2d5..9abab3cd9 100644
--- a/.env.example
+++ b/.env.example
@@ -275,6 +275,13 @@ TAXII_FEEDS=https://cti-taxii.mitre.org/taxii/,enterprise-attack,,
MISP_URL=
MISP_API_KEY=
OTX_API_KEY=
+# OpenCTI — GraphQL API of a running OpenCTI platform (6.x/7.x).
+# Works with an internal deployment (RFC1918) or a public instance;
+# under AIRGAP_MODE=strict a public URL is skipped unless allowlisted.
+OPENCTI_URL=
+OPENCTI_TOKEN=
+OPENCTI_VERIFY_SSL=true
+OPENCTI_POLL_INTERVAL=1800
CISA_KEV_ENABLED=true
ABUSE_CH_ENABLED=true
diff --git a/services/threatintel/app/clients/opencti.py b/services/threatintel/app/clients/opencti.py
new file mode 100644
index 000000000..4136c3dee
--- /dev/null
+++ b/services/threatintel/app/clients/opencti.py
@@ -0,0 +1,294 @@
+"""
+OpenCTI GraphQL client.
+
+Talks to an OpenCTI platform over its GraphQL API (`POST /graphql`), using
+the current 6.x/7.x `FilterGroup` filter format and cursor pagination to
+pull recently-updated `Indicator` objects incrementally.
+
+The GraphQL document was validated against a live OpenCTI platform
+(version 7.261008.0): one real request/response pair is captured as
+`tests/fixtures/opencti_stix_core_objects.json` and replayed by
+`tests/test_opencti_client.py`. Two details of that capture are worth
+recording because the printed schema alone does not settle them:
+
+- the incremental filter key is `updated_at` (the bare `updated` key is
+ silently accepted and matches nothing), and
+- on this build `objectLabel` and `killChainPhases` resolve to flat lists
+ while `externalReferences` is a relay-style connection with `edges`.
+
+AiSOC — open-source AI Security Operations Center (MIT License)
+"""
+
+from __future__ import annotations
+
+import re
+from datetime import UTC, datetime, timedelta
+from typing import Any
+
+import httpx
+import structlog
+
+logger = structlog.get_logger(__name__)
+
+# Cursor-paginated incremental pull of Indicator objects. Inline fragment is
+# required: stixCoreObjects returns the StixCoreObject interface, so
+# Indicator-only fields must be selected through `... on Indicator`.
+STIX_CORE_INDICATORS_QUERY = """
+query AiSocIndicators($first: Int!, $after: ID, $filters: FilterGroup) {
+ stixCoreObjects(
+ types: ["Indicator"]
+ first: $first
+ after: $after
+ orderBy: updated_at
+ orderMode: asc
+ filters: $filters
+ ) {
+ edges {
+ cursor
+ node {
+ id
+ standard_id
+ entity_type
+ ... on Indicator {
+ name
+ description
+ pattern
+ pattern_type
+ pattern_version
+ revoked
+ confidence
+ indicator_types
+ x_opencti_score
+ x_opencti_main_observable_type
+ created
+ modified
+ objectLabel { value }
+ killChainPhases { kill_chain_name phase_name }
+ externalReferences { edges { node { source_name url } } }
+ }
+ }
+ }
+ pageInfo { hasNextPage endCursor }
+ }
+}
+"""
+
+# One STIX comparison clause inside a pattern: <type>:<prop> = '<value>'.
+# Leading '[' excluded: pattern strings arrive as "[ipv4-addr:value = '...']"
+# and the bracket must not leak into the observable-type capture.
+_PATTERN_CLAUSE_RE = re.compile(r"([^\s\[]+?):([^\s=]+?)\s*=\s*'([^']+)'")
+
+# (observable-type, property) -> AiSOC IOC vocabulary, mirroring
+# app.parsers.stix.StixParser._map_stix_type so every feed produces the
+# same type strings into pipeline.ingest_iocs.
+_CLAUSE_TYPE_MAP: dict[tuple[str, str], str] = {
+ ("ipv4-addr", "value"): "ipv4-addr",
+ ("ipv6-addr", "value"): "ipv6-addr",
+ ("domain-name", "value"): "domain-name",
+ ("url", "value"): "url",
+ ("email-addr", "value"): "email-addr",
+ ("directory", "path"): "directory",
+ ("mutex", "name"): "mutex",
+ ("mac-address", "value"): "mac-addr",
+}
+
+# x_opencti_main_observable_type -> fallback IOC type when a pattern clause
+# does not map (e.g. patterns with composite observables).
+_MAIN_OBSERVABLE_MAP: dict[str, str] = {
+ "ipv4-addr": "ipv4-addr",
+ "ipv6-addr": "ipv6-addr",
+ "domain-name": "domain-name",
+ "url": "url",
+ "email-addr": "email-addr",
+ "file": "file-hash:SHA-256",
+ "stixfile": "file-hash:SHA-256",
+ "artifact": "file-hash:SHA-256",
+ "mac-address": "mac-addr",
+ "mutex": "mutex",
+ "autonomous-system": "asn",
+ "cryptocurrency-wallet": "cryptowallet",
+ "text": "other",
+}
+
+
+def parse_pattern(pattern: str) -> list[tuple[str, str]]:
+ """Extract ``[(ioc_type, value), ...]`` from a STIX indicator pattern.
+
+ Handles single- and multi-clause patterns
+ (``[ipv4-addr:value = '1.2.3.4' OR ipv4-addr:value = '5.6.7.8']``) and
+ file hash properties (``file:hashes.'SHA-256' = '<hex>'``). Unmapped
+ clause types fall back to the raw ``type:property`` string so nothing is
+ silently dropped.
+ """
+ out: list[tuple[str, str]] = []
+ for obj_type, prop, value in _PATTERN_CLAUSE_RE.findall(pattern or ""):
+ norm_type = obj_type.lower()
+ if norm_type == "file" and prop.lower().startswith("hashes"):
+ hash_name = prop.split(".")[-1].strip("'").upper()
+ ioc_type = f"file-hash:{hash_name}"
+ else:
+ ioc_type = _CLAUSE_TYPE_MAP.get((norm_type, prop), f"{norm_type}:{prop}")
+ if value:
+ out.append((ioc_type, value))
+ return out
+
+
+def score_to_reliability(score: int | None) -> str:
+ """Map an OpenCTI confidence score (0-100) to STIX confidence bands."""
+ if score is None:
+ return "C"
+ if score >= 90:
+ return "A"
+ if score >= 75:
+ return "B"
+ if score >= 50:
+ return "C"
+ if score >= 25:
+ return "D"
+ return "E"
+
+
+def _label_values(node: dict[str, Any]) -> list[str]:
+ # Schema: Indicator-side selection resolves objectLabel to [Label!]!
+ # where Label.value is String! (flat list, no relay edges on this build).
+ return [lbl.get("value", "") for lbl in (node.get("objectLabel") or []) if isinstance(lbl, dict)]
+
+
+class OpenCtiClient:
+ """
+ Async OpenCTI GraphQL client.
+
+ Fetches recently-updated Indicator objects with cursor pagination,
+ filters revoked indicators, and normalizes them into the MISP-vocabulary
+ IOC dicts consumed by ``ThreatIntelPipeline.ingest_iocs``.
+ """
+
+ def __init__(
+ self,
+ url: str,
+ token: str,
+ verify_ssl: bool = True,
+ page_size: int = 500,
+ ) -> None:
+ base = url.rstrip("/")
+ self._graphql_url = base if base.endswith("/graphql") else f"{base}/graphql"
+ self._verify_ssl = verify_ssl
+ self._page_size = max(1, min(page_size, 1000))
+ self._headers = {
+ "Authorization": f"Bearer {token}",
+ "Content-Type": "application/json",
+ }
+ self._last_sync: datetime | None = None
+
+ @staticmethod
+ def _filters(since: datetime) -> dict[str, Any]:
+ return {
+ "mode": "and",
+ "filters": [
+ {
+ "key": ["updated_at"],
+ "values": [since.strftime("%Y-%m-%dT%H:%M:%SZ")],
+ "operator": "gt",
+ }
+ ],
+ "filterGroups": [],
+ }
+
+ async def get_recent_indicators(self, since_hours: int = 24) -> list[dict[str, Any]]:
+ """Fetch non-revoked Indicator nodes updated since the last sync.
+
+ The first call looks back ``since_hours``; subsequent calls resume
+ from the previous successful sync watermark.
+ """
+ since = self._last_sync or (datetime.now(UTC) - timedelta(hours=since_hours))
+ nodes: list[dict[str, Any]] = []
+ after: str | None = None
+
+ async with httpx.AsyncClient(headers=self._headers, verify=self._verify_ssl, timeout=60.0) as client:
+ while True:
+ resp = await client.post(
+ self._graphql_url,
+ json={
+ "query": STIX_CORE_INDICATORS_QUERY,
+ "variables": {
+ "first": self._page_size,
+ "after": after,
+ "filters": self._filters(since),
+ },
+ },
+ )
+ resp.raise_for_status()
+ body = resp.json()
+ if "errors" in body:
+ # Fail loudly: a silently-wrong query looks like an empty
+ # feed, which is the worst possible failure mode for a TI
+ # source.
+ raise RuntimeError(f"OpenCTI GraphQL errors: {body['errors'][:2]}")
+ block = (body.get("data") or {}).get("stixCoreObjects") or {}
+ for edge in block.get("edges") or []:
+ node = edge.get("node") or {}
+ if node.get("revoked"):
+ continue
+ nodes.append(node)
+ page = block.get("pageInfo") or {}
+ if not page.get("hasNextPage") or not page.get("endCursor"):
+ break
+ after = page["endCursor"]
+
+ self._last_sync = datetime.now(UTC)
+ logger.info("OpenCTI indicators fetched", count=len(nodes))
+ return nodes
+
+ def extract_iocs(self, node: dict[str, Any]) -> list[dict[str, Any]]:
+ """Normalize one Indicator node into AiSOC IOC dicts."""
+ pattern = str(node.get("pattern") or "")
+ clauses = parse_pattern(pattern)
+ if not clauses:
+ main = _MAIN_OBSERVABLE_MAP.get(str(node.get("x_opencti_main_observable_type") or "").lower(), "")
+ clauses = [(main, "")] if main else []
+
+ labels = _label_values(node)
+ tlp = next(
+ (lbl.split(":", 1)[-1] for lbl in labels if lbl.lower().startswith(("tlp:", "lp:"))),
+ "white",
+ )
+ kill_chain = [
+ f"{phase.get('kill_chain_name', '')}:{phase.get('phase_name', '')}".strip(":")
+ for phase in (node.get("killChainPhases") or [])
+ if isinstance(phase, dict)
+ ]
+ references = [
+ {
+ "source": (edge.get("node") or {}).get("source_name"),
+ "url": (edge.get("node") or {}).get("url"),
+ }
+ for edge in ((node.get("externalReferences") or {}).get("edges") or [])
+ ]
+ score = node.get("x_opencti_score")
+
+ stix_id = str(node.get("standard_id") or node.get("id") or "")
+ iocs: list[dict[str, Any]] = []
+ for ioc_type, value in clauses:
+ if not value:
+ continue
+ iocs.append(
+ {
+ "type": ioc_type,
+ "value": value,
+ "category": (node.get("indicator_types") or ["unknown"])[0],
+ "to_ids": True,
+ "comment": str(node.get("description") or "")[:500],
+ "labels": labels,
+ "tags": labels + kill_chain,
+ "name": str(node.get("name") or "")[:300],
+ "confidence": node.get("confidence"),
+ "score": score,
+ "reliability": score_to_reliability(score),
+ "tlp": tlp,
+ "external_references": references,
+ "source": "opencti",
+ "source_ref": f"opencti:{stix_id}",
+ "last_modified": node.get("modified"),
+ }
+ )
+ return iocs
diff --git a/services/threatintel/app/config.py b/services/threatintel/app/config.py
index 2b3a5fc22..b691ef2e6 100644
--- a/services/threatintel/app/config.py
+++ b/services/threatintel/app/config.py
@@ -75,11 +75,13 @@ class Settings(BaseSettings):
# OpenCTI
OPENCTI_URL: str = ""
OPENCTI_TOKEN: str = ""
+ OPENCTI_VERIFY_SSL: bool = True
# Poll intervals (seconds)
TAXII_POLL_INTERVAL: int = 900 # 15 min
MISP_POLL_INTERVAL: int = 1800 # 30 min
OTX_POLL_INTERVAL: int = 1800 # 30 min
+ OPENCTI_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 4f38f5c5d..99175e323 100644
--- a/services/threatintel/app/feeds/handlers.py
+++ b/services/threatintel/app/feeds/handlers.py
@@ -16,6 +16,7 @@ import structlog
from app.clients.cisa_kev import CisaKevClient
from app.clients.misp import MispClient
+from app.clients.opencti import OpenCtiClient
from app.clients.otx import OtxClient
from app.clients.taxii import TaxiiClient
from app.parsers.stix import StixParser
@@ -143,3 +144,30 @@ async def handle_cisa_kev_feed(
except Exception as exc:
logger.error("CISA KEV feed handler failed", error=str(exc))
+
+
+# ─── OpenCTI Feed Handler ─────────────────────────────────────────────────────
+
+
+async def handle_opencti_feed(
+ client: OpenCtiClient,
+ pipeline: ThreatIntelPipeline,
+ since_hours: int = 24,
+) -> None:
+ """
+ Poll an OpenCTI platform for recent Indicators and extract IOCs.
+ """
+ logger.info("Polling OpenCTI feed", since_hours=since_hours)
+
+ try:
+ nodes = await client.get_recent_indicators(since_hours=since_hours)
+ all_iocs: list[dict[str, Any]] = []
+ for node in nodes:
+ all_iocs.extend(client.extract_iocs(node))
+
+ if all_iocs:
+ stats = await pipeline.ingest_iocs(all_iocs, source="opencti")
+ logger.info("OpenCTI IOCs ingested", **stats, indicator_count=len(nodes))
+
+ except Exception as exc:
+ logger.error("OpenCTI feed handler failed", error=str(exc))
diff --git a/services/threatintel/app/main.py b/services/threatintel/app/main.py
index 7b0a48e7a..f09719244 100644
--- a/services/threatintel/app/main.py
+++ b/services/threatintel/app/main.py
@@ -31,6 +31,7 @@ from app.api.actor_attribution import router as actor_attribution_router
from app.api.indicators import router as indicators_router
from app.clients.cisa_kev import CisaKevClient
from app.clients.misp import MispClient
+from app.clients.opencti import OpenCtiClient
from app.clients.otx import OtxClient
from app.clients.taxii import TaxiiClient
from app.config import settings
@@ -38,6 +39,7 @@ from app.core.kafka_security import kafka_client_kwargs
from app.feeds.handlers import (
handle_cisa_kev_feed,
handle_misp_feed,
+ handle_opencti_feed,
handle_otx_feed,
handle_taxii_feed,
)
@@ -204,6 +206,16 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
else None
)
+ opencti_client = (
+ OpenCtiClient(
+ url=settings.OPENCTI_URL,
+ token=settings.OPENCTI_TOKEN,
+ verify_ssl=settings.OPENCTI_VERIFY_SSL,
+ )
+ if settings.OPENCTI_URL and settings.OPENCTI_TOKEN
+ else None
+ )
+
kev_client = CisaKevClient()
# ── Scheduler ─────────────────────────────────────────────────────────────
@@ -245,6 +257,18 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
interval_seconds=settings.OTX_POLL_INTERVAL,
)
+ # OpenCTI (typically an internal deployment — the air-gap helper
+ # permits RFC1918/internal hosts and skips public URLs unless
+ # explicitly allowlisted, same contract as the other feeds).
+ if opencti_client and _airgap_check_feed_url("opencti", settings.OPENCTI_URL):
+ scheduler.register(
+ feed_name="opencti",
+ handler=partial(
+ handle_opencti_feed, client=opencti_client, pipeline=pipeline
+ ),
+ interval_seconds=settings.OPENCTI_POLL_INTERVAL,
+ )
+
# CISA KEV (always enabled — no key required) — public CISA URL, so
# an air-gapped deployment must mirror the JSON internally and
# override via an internal allowlist entry, otherwise we skip.
diff --git a/services/threatintel/tests/fixtures/opencti_stix_core_objects.json b/services/threatintel/tests/fixtures/opencti_stix_core_objects.json
new file mode 100644
index 000000000..f5e1d1317
--- /dev/null
+++ b/services/threatintel/tests/fixtures/opencti_stix_core_objects.json
@@ -0,0 +1 @@
+{"data": {"stixCoreObjects": {"edges": [{"cursor": "WzE3OTE5NjMwNjgxMTEsImluZGljYXRvci0tODc1MmViNWYtOGI1OC01MTllLThmNDAtMTA4NzA1ZWNlMmJlIl0=", "node": {"id": "9f14abf4-fe01-4470-b15c-1ed7a8a9de94", "standard_id": "indicator--8752eb5f-8b58-519e-8f40-108705ece2be", "entity_type": "Indicator", "name": "AiSOC validation: Cobalt Strike C2", "description": "Synthetic validation indicator seeded by AiSOC integration test (RFC5737 documentation range).", "pattern": "[ipv4-addr:value = '203.0.113.66']", "pattern_type": "stix", "pattern_version": "2.1", "revoked": false, "confidence": 100, "indicator_types": ["malicious-activity"], "x_opencti_score": 85, "x_opencti_main_observable_type": "IPv4-Addr", "created": "2026-10-14T07:31:08.047Z", "modified": "2026-10-14T07:31:08.111Z", "objectLabel": [], "killChainPhases": [{"kill_chain_name": "mitre-attack-v20", "phase_name": "command-and-control"}], "externalReferences": {"edges": []}}}, {"cursor": "WzE3OTE5NjMwNjg0OTgsImluZGljYXRvci0tMzdkZDEwYzItNmRmNy01ZDJmLTgyY2QtNWQzZDMyZTkxZjMxIl0=", "node": {"id": "451f22bb-99ee-4cbf-9c41-486fd216252a", "standard_id": "indicator--37dd10c2-6df7-5d2f-82cd-5d3d32e91f31", "entity_type": "Indicator", "name": "AiSOC validation: phishing C2 domain", "description": "Synthetic validation indicator (RFC2606 reserved TLD).", "pattern": "[domain-name:value = 'evil-c2-validation.example']", "pattern_type": "stix", "pattern_version": "2.1", "revoked": false, "confidence": 100, "indicator_types": ["malicious-activity"], "x_opencti_score": 75, "x_opencti_main_observable_type": "Domain-Name", "created": "2026-10-14T07:31:08.498Z", "modified": "2026-10-14T07:31:08.498Z", "objectLabel": [], "killChainPhases": [], "externalReferences": {"edges": []}}}, {"cursor": "WzE3OTE5NjMwNjg5NTEsImluZGljYXRvci0tOGQzMGU5NzgtYzZkZC01NDM5LTllZDAtYmQ2MTIzODkxNWMzIl0=", "node": {"id": "18c59129-9fe2-47d0-85ae-01872da2cccc", "standard_id": "indicator--8d30e978-c6dd-5439-9ed0-bd61238915c3", "entity_type": "Indicator", "name": "AiSOC validation: maldoc SHA-256", "description": "Synthetic validation indicator (known empty-string SHA-256).", "pattern": "[file:hashes.'SHA-256' = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855']", "pattern_type": "stix", "pattern_version": "2.1", "revoked": false, "confidence": 100, "indicator_types": ["malicious-activity"], "x_opencti_score": 90, "x_opencti_main_observable_type": "StixFile", "created": "2026-10-14T07:31:08.951Z", "modified": "2026-10-14T07:31:08.951Z", "objectLabel": [], "killChainPhases": [], "externalReferences": {"edges": []}}}], "pageInfo": {"hasNextPage": false, "endCursor": "WzE3OTE5NjMwNjg5NTEsImluZGljYXRvci0tOGQzMGU5NzgtYzZkZC01NDM5LTllZDAtYmQ2MTIzODkxNWMzIl0="}}}}
diff --git a/services/threatintel/tests/fixtures/request.json b/services/threatintel/tests/fixtures/request.json
new file mode 100644
index 000000000..a13499690
--- /dev/null
+++ b/services/threatintel/tests/fixtures/request.json
@@ -0,0 +1 @@
+{"query": "\nquery AiSocIndicators($first: Int!, $after: ID, $filters: FilterGroup) {\n stixCoreObjects(\n types: [\"Indicator\"]\n first: $first\n after: $after\n orderBy: updated_at\n orderMode: asc\n filters: $filters\n ) {\n edges {\n cursor\n node {\n id\n standard_id\n entity_type\n ... on Indicator {\n name\n description\n pattern\n pattern_type\n pattern_version\n revoked\n confidence\n indicator_types\n x_opencti_score\n x_opencti_main_observable_type\n created\n modified\n objectLabel { value }\n killChainPhases { kill_chain_name phase_name }\n externalReferences { edges { node { source_name url } } }\n }\n }\n }\n pageInfo { hasNextPage endCursor }\n }\n}\n", "variables": {"first": 500, "after": null, "filters": {"mode": "and", "filters": [{"key": ["updated_at"], "values": ["2026-09-01T00:00:00Z"], "operator": "gt"}], "filterGroups": []}}}
diff --git a/services/threatintel/tests/test_opencti_client.py b/services/threatintel/tests/test_opencti_client.py
new file mode 100644
index 000000000..06e36bc76
--- /dev/null
+++ b/services/threatintel/tests/test_opencti_client.py
@@ -0,0 +1,154 @@
+"""
+Tests for the OpenCTI client (app.clients.opencti).
+
+Replays a fixture captured from a live OpenCTI platform (7.261008.0) on
+2026-10-14: a real GraphQL request/response pair against
+`POST /graphql` with the FilterGroup incremental query. Synthetic
+indicators (RFC5737/RFC2606 ranges) seeded during platform validation.
+
+AiSOC — open-source AI Security Operations Center (MIT License)
+"""
+
+from __future__ import annotations
+
+import json
+import pathlib
+
+import pytest
+from app.clients.opencti import (
+ OpenCtiClient,
+ _label_values,
+ parse_pattern,
+ score_to_reliability,
+)
+
+FIXTURE_DIR = pathlib.Path(__file__).parent / "fixtures"
+
+
+def live_response() -> dict:
+ return json.loads((FIXTURE_DIR / "opencti_stix_core_objects.json").read_text())
+
+
+def live_request() -> dict:
+ return json.loads((FIXTURE_DIR / "request.json").read_text())
+
+
+def client() -> OpenCtiClient:
+ return OpenCtiClient(
+ url="http://10.41.192.56:8080",
+ token="test-token",
+ )
+
+
+class TestParsePattern:
+ def test_ipv4(self) -> None:
+ assert parse_pattern("[ipv4-addr:value = '203.0.113.66']") == [("ipv4-addr", "203.0.113.66")]
+
+ def test_domain(self) -> None:
+ assert parse_pattern("[domain-name:value = 'evil.example']") == [("domain-name", "evil.example")]
+
+ def test_sha256(self) -> None:
+ h = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+ assert parse_pattern(f"[file:hashes.'SHA-256' = '{h}']") == [("file-hash:SHA-256", h)]
+
+ def test_md5(self) -> None:
+ assert parse_pattern("[file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e']") == [
+ ("file-hash:MD5", "d41d8cd98f00b204e9800998ecf8427e")
+ ]
+
+ def test_multi_clause(self) -> None:
+ result = parse_pattern("[ipv4-addr:value = '1.2.3.4' OR ipv4-addr:value = '5.6.7.8']")
+ assert result == [("ipv4-addr", "1.2.3.4"), ("ipv4-addr", "5.6.7.8")]
+
+ def test_unmapped_type_falls_back(self) -> None:
+ result = parse_pattern("[x-custom-type:value = 'abc']")
+ assert result == [("x-custom-type:value", "abc")]
+
+ def test_empty(self) -> None:
+ assert parse_pattern("") == []
+
+
+class TestLiveFixtureNormalization:
+ """Normalize every node of the captured live response."""
+
+ def test_fixture_shape(self, live_response: dict) -> None:
+ block = live_response["data"]["stixCoreObjects"]
+ assert block["pageInfo"]["hasNextPage"] is False
+ assert len(block["edges"]) >= 1
+ for edge in block["edges"]:
+ node = edge["node"]
+ assert node["entity_type"] == "Indicator"
+ assert node["pattern"]
+ assert edge["cursor"]
+
+ def test_all_nodes_produce_typed_iocs(self, client: OpenCtiClient, live_response: dict) -> None:
+ for edge in live_response["data"]["stixCoreObjects"]["edges"]:
+ node = edge["node"]
+ iocs = client.extract_iocs(node)
+ assert iocs, f"no IOCs extracted from {node['standard_id']}"
+ for ioc in iocs:
+ assert ioc["type"]
+ assert ioc["value"]
+ assert ioc["source"] == "opencti"
+ assert ioc["source_ref"].startswith("opencti:indicator--")
+ assert ioc["category"] == "malicious-activity"
+ assert ioc["to_ids"] is True
+
+ def test_ipv4_node(self, client: OpenCtiClient, live_response: dict) -> None:
+ node = next(
+ e["node"]
+ for e in live_response["data"]["stixCoreObjects"]["edges"]
+ if e["node"]["x_opencti_main_observable_type"] == "IPv4-Addr"
+ )
+ iocs = client.extract_iocs(node)
+ assert iocs[0]["type"] == "ipv4-addr"
+ assert iocs[0]["value"] == "203.0.113.66"
+ assert iocs[0]["score"] == node["x_opencti_score"]
+ assert iocs[0]["reliability"] == "B" # score 85
+
+ def test_sha256_node(self, client: OpenCtiClient, live_response: dict) -> None:
+ node = next(e["node"] for e in live_response["data"]["stixCoreObjects"]["edges"] if "SHA-256" in e["node"]["pattern"])
+ iocs = client.extract_iocs(node)
+ assert iocs[0]["type"] == "file-hash:SHA-256"
+ assert len(iocs[0]["value"]) == 64
+
+ def test_killchain_phase_flattened(self, client: OpenCtiClient, live_response: dict) -> None:
+ node = next(e["node"] for e in live_response["data"]["stixCoreObjects"]["edges"] if e["node"]["killChainPhases"])
+ iocs = client.extract_iocs(node)
+ assert "mitre-attack-v20:command-and-control" in iocs[0]["tags"]
+
+
+class TestReliability:
+ @pytest.mark.parametrize(
+ ("score", "band"),
+ [(None, "C"), (100, "A"), (90, "A"), (85, "B"), (75, "B"), (50, "C"), (25, "D"), (10, "E")],
+ )
+ def test_bands(self, score: int | None, band: str) -> None:
+ assert score_to_reliability(score) == band
+
+
+class TestLabelValues:
+ def test_flat_labels(self) -> None:
+ node = {"objectLabel": [{"value": "tlp:white"}, {"value": "botnet"}]}
+ assert _label_values(node) == ["tlp:white", "botnet"]
+
+ def test_missing_is_empty(self) -> None:
+ assert _label_values({}) == []
+
+
+class TestGraphqlDocument:
+ def test_matches_live_request_shape(self, live_request: dict) -> None:
+ """The shipped query must be the one the platform accepted."""
+ doc = live_request["query"]
+ assert "FilterGroup" in doc
+ assert "updated_at" in doc
+ assert "... on Indicator" in doc
+ assert "$after: ID" in doc
+ # variables used for the incremental watermark
+ assert live_request["variables"]["filters"]["mode"] == "and"
+ f = live_request["variables"]["filters"]["filters"][0]
+ assert f["key"] == ["updated_at"]
+ assert f["operator"] == "gt"