airmdr 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
airmdr/__init__.py ADDED
@@ -0,0 +1,33 @@
1
+ """AirMDR: investigate a security alert from your terminal or from
2
+ Python.
3
+
4
+ pip install airmdr
5
+ airmdr demo # watch a recorded investigation, no account
6
+ airmdr login # free trial
7
+ airmdr investigate alert.json
8
+ """
9
+
10
+ from airmdr._version import __version__
11
+ from airmdr.client import AirMDR
12
+ from airmdr.errors import (
13
+ AirMDRError,
14
+ ConnectionFailed,
15
+ LimitReached,
16
+ NotFound,
17
+ NotSignedIn,
18
+ )
19
+ from airmdr.run import Case, Run
20
+ from airmdr.steps import Step
21
+
22
+ __all__ = [
23
+ "AirMDR",
24
+ "AirMDRError",
25
+ "Case",
26
+ "ConnectionFailed",
27
+ "LimitReached",
28
+ "NotFound",
29
+ "NotSignedIn",
30
+ "Run",
31
+ "Step",
32
+ "__version__",
33
+ ]
airmdr/__main__.py ADDED
@@ -0,0 +1,3 @@
1
+ from airmdr.cli import main
2
+
3
+ raise SystemExit(main())
airmdr/_demo.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "alert": {
3
+ "source": "SOC Radar",
4
+ "title": "Dark web data leak: genomiq.io credentials exposed"
5
+ },
6
+ "duration_seconds": 150,
7
+ "verdict": "benign",
8
+ "confidence": 92,
9
+ "summary": "genomiq.io keyword found in Meridian Partners ransomware leak (DataShadow) — GenomIQ is a listed client, not the breached party; no sensitive data exposed.",
10
+ "events": [
11
+ {
12
+ "type": "status",
13
+ "ts": 0
14
+ },
15
+ {
16
+ "type": "tool_result",
17
+ "name": "read",
18
+ "call_id": "toolu_013eCtHpbwfobd8CNZYDqgCU",
19
+ "ts": 2505
20
+ },
21
+ {
22
+ "type": "tool_result",
23
+ "name": "read",
24
+ "call_id": "toolu_01XMLFKaYEB9a2CyLJSBqkBX",
25
+ "ts": 2783
26
+ },
27
+ {
28
+ "type": "tool_result",
29
+ "name": "read",
30
+ "call_id": "toolu_01R1zhEhSLJumyd5KQ4sGqDT",
31
+ "ts": 3061
32
+ },
33
+ {
34
+ "type": "tool_call",
35
+ "name": "sub_agent",
36
+ "call_id": "toolu_01WrJiztakuwvQrN6GgYvqmG",
37
+ "input": {
38
+ "title": "What the leaked file contains"
39
+ },
40
+ "ts": 12523
41
+ },
42
+ {
43
+ "type": "tool_call",
44
+ "name": "sub_agent",
45
+ "call_id": "toolu_01NvCJ6BJmegV8aExZcSLRiD",
46
+ "input": {
47
+ "title": "Who hosts the leak site"
48
+ },
49
+ "ts": 12801
50
+ },
51
+ {
52
+ "type": "tool_call",
53
+ "name": "sub_agent",
54
+ "call_id": "toolu_01AaDx7MzvVMvCyASKZZ81M7",
55
+ "input": {
56
+ "title": "What SOC Radar knows about this alarm"
57
+ },
58
+ "ts": 13080
59
+ },
60
+ {
61
+ "type": "tool_result",
62
+ "name": "sub_agent",
63
+ "call_id": "toolu_01AaDx7MzvVMvCyASKZZ81M7",
64
+ "ts": 103525
65
+ },
66
+ {
67
+ "type": "tool_result",
68
+ "name": "sub_agent",
69
+ "call_id": "toolu_01WrJiztakuwvQrN6GgYvqmG",
70
+ "ts": 119388
71
+ },
72
+ {
73
+ "type": "tool_result",
74
+ "name": "sub_agent",
75
+ "call_id": "toolu_01NvCJ6BJmegV8aExZcSLRiD",
76
+ "ts": 139147
77
+ },
78
+ {
79
+ "type": "tool_result",
80
+ "name": "read",
81
+ "call_id": "toolu_017ZpnBwn5STPRzZw2himZtL",
82
+ "ts": 145826
83
+ },
84
+ {
85
+ "type": "tool_result",
86
+ "name": "create_case",
87
+ "call_id": "toolu_01BCBit4mZ1T5He9ms4jzP4j",
88
+ "ts": 146939
89
+ },
90
+ {
91
+ "type": "agent_result",
92
+ "ts": 149722
93
+ }
94
+ ]
95
+ }
airmdr/_http.py ADDED
@@ -0,0 +1,104 @@
1
+ """The one place this package talks to the network. Standard library
2
+ only, so installing ``airmdr`` pulls in nothing else."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import json
7
+ import urllib.error
8
+ import urllib.request
9
+ from collections.abc import Iterator
10
+ from typing import Any
11
+
12
+ from airmdr import errors
13
+ from airmdr._version import __version__
14
+
15
+ API = "/api/sdk/v1"
16
+ # The stream sends a heartbeat every 15 seconds; a minute of silence
17
+ # means the connection is gone.
18
+ _STREAM_TIMEOUT = 60
19
+ _TIMEOUT = 90
20
+
21
+
22
+ class Transport:
23
+ """Sends requests to an AirMDR server. ``surface`` says what is
24
+ calling (``cli`` or ``python``); it is logged, and gates nothing."""
25
+
26
+ def __init__(
27
+ self, base_url: str, key: str | None = None, surface: str = "python"
28
+ ) -> None:
29
+ self.base_url = base_url.rstrip("/")
30
+ self.key = key
31
+ self.surface = surface
32
+
33
+ def _headers(self, *, auth: bool, body: bool) -> dict[str, str]:
34
+ headers = {
35
+ "Accept": "application/json",
36
+ "User-Agent": f"airmdr-{self.surface}/{__version__}",
37
+ "X-AirMDR-Client": f"{self.surface}/{__version__}",
38
+ }
39
+ if body:
40
+ headers["Content-Type"] = "application/json"
41
+ if auth:
42
+ if not self.key:
43
+ raise errors.NotSignedIn(
44
+ "Not signed in. Run: airmdr login", code="missing_key"
45
+ )
46
+ headers["Authorization"] = f"Bearer {self.key}"
47
+ return headers
48
+
49
+ def _open(
50
+ self, method: str, path: str, payload: Any, auth: bool, timeout: int
51
+ ):
52
+ data = None if payload is None else json.dumps(payload).encode("utf-8")
53
+ req = urllib.request.Request(
54
+ self.base_url + path,
55
+ data=data,
56
+ method=method,
57
+ headers=self._headers(auth=auth, body=data is not None),
58
+ )
59
+ try:
60
+ return urllib.request.urlopen(req, timeout=timeout) # noqa: S310
61
+ except urllib.error.HTTPError as exc:
62
+ raw = exc.read()
63
+ try:
64
+ body = json.loads(raw.decode("utf-8") or "{}")
65
+ except ValueError:
66
+ body = {}
67
+ raise errors.from_response(exc.code, body) from None
68
+ except (TimeoutError, urllib.error.URLError, OSError) as exc:
69
+ reason = getattr(exc, "reason", exc)
70
+ raise errors.ConnectionFailed(
71
+ f"Could not reach AirMDR at {self.base_url} ({reason}). "
72
+ "Check your connection and try again.",
73
+ code="unreachable",
74
+ ) from None
75
+
76
+ def request(
77
+ self, method: str, path: str, payload: Any = None, *, auth: bool = True
78
+ ) -> dict[str, Any]:
79
+ with self._open(method, path, payload, auth, _TIMEOUT) as resp:
80
+ raw = resp.read()
81
+ try:
82
+ body = json.loads(raw.decode("utf-8") or "{}")
83
+ except ValueError:
84
+ body = {}
85
+ return body if isinstance(body, dict) else {}
86
+
87
+ def stream(self, path: str) -> Iterator[dict[str, Any]]:
88
+ """A server-sent event stream, one parsed event at a time."""
89
+ with self._open("GET", path, None, True, _STREAM_TIMEOUT) as resp:
90
+ try:
91
+ for raw in resp:
92
+ line = raw.decode("utf-8", "replace").strip()
93
+ if not line.startswith("data:"):
94
+ continue
95
+ try:
96
+ event = json.loads(line[5:].strip())
97
+ except ValueError:
98
+ continue
99
+ if isinstance(event, dict):
100
+ yield event
101
+ except (TimeoutError, OSError):
102
+ # The connection dropped mid-run. The caller decides
103
+ # whether to reconnect; the run itself is unaffected.
104
+ return
airmdr/_version.py ADDED
@@ -0,0 +1 @@
1
+ __version__ = "0.1.0"