agentlighthouse-core 0.4.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.
- agentlighthouse_core-0.4.0.dist-info/METADATA +358 -0
- agentlighthouse_core-0.4.0.dist-info/RECORD +78 -0
- agentlighthouse_core-0.4.0.dist-info/WHEEL +4 -0
- agentlighthouse_core-0.4.0.dist-info/entry_points.txt +2 -0
- agentlighthouse_core-0.4.0.dist-info/licenses/LICENSE +21 -0
- al_core/__init__.py +12 -0
- al_core/_bundled/configs/audit.yaml +12 -0
- al_core/_bundled/configs/balanced.yaml +36 -0
- al_core/_bundled/configs/strict.yaml +32 -0
- al_core/_bundled/dashboard/assets/index-CN2LU6nV.css +1 -0
- al_core/_bundled/dashboard/assets/index-DX6cZzRi.js +40 -0
- al_core/_bundled/dashboard/index.html +15 -0
- al_core/_bundled/policies/default-deny.yaml +37 -0
- al_core/a2a/__init__.py +25 -0
- al_core/a2a/envelope.py +60 -0
- al_core/a2a/mediator.py +221 -0
- al_core/app.py +427 -0
- al_core/audit/__init__.py +149 -0
- al_core/audit/db.py +481 -0
- al_core/audit/pg.py +350 -0
- al_core/audit/pool.py +260 -0
- al_core/audit/siem.py +97 -0
- al_core/capability/__init__.py +28 -0
- al_core/capability/budget.py +56 -0
- al_core/capability/gate.py +334 -0
- al_core/capability/hitl.py +331 -0
- al_core/capability/policy.py +270 -0
- al_core/capability/store.py +209 -0
- al_core/capability/taint.py +79 -0
- al_core/cli/__init__.py +1 -0
- al_core/cli/main.py +1275 -0
- al_core/config/__init__.py +436 -0
- al_core/config/watch.py +51 -0
- al_core/detect/__init__.py +23 -0
- al_core/detect/base.py +131 -0
- al_core/detect/bip39_words.py +267 -0
- al_core/detect/budget.py +92 -0
- al_core/detect/detect_secrets_adapter.py +110 -0
- al_core/detect/engine.py +270 -0
- al_core/detect/scanners.py +454 -0
- al_core/egress/__init__.py +27 -0
- al_core/egress/nftables.py +59 -0
- al_core/egress/selftest.py +94 -0
- al_core/embed.py +51 -0
- al_core/gateway/__init__.py +29 -0
- al_core/gateway/content.py +154 -0
- al_core/gateway/decision.py +124 -0
- al_core/gateway/dnspin.py +131 -0
- al_core/gateway/forward.py +261 -0
- al_core/gateway/policy.py +66 -0
- al_core/gateway/ssrf.py +178 -0
- al_core/gateway/vkeys.py +205 -0
- al_core/gateway/web.py +801 -0
- al_core/identity/__init__.py +209 -0
- al_core/jsonstore.py +73 -0
- al_core/keys/__init__.py +141 -0
- al_core/killswitch.py +130 -0
- al_core/learn/__init__.py +17 -0
- al_core/learn/bundle.py +76 -0
- al_core/learn/miner.py +156 -0
- al_core/learn/scanner.py +77 -0
- al_core/learn/testgen.py +107 -0
- al_core/mcp/__init__.py +25 -0
- al_core/mcp/chain.py +147 -0
- al_core/mcp/descriptors.py +111 -0
- al_core/mcp/mediator.py +150 -0
- al_core/mcp/proxy.py +161 -0
- al_core/mcp/session.py +537 -0
- al_core/memory/__init__.py +15 -0
- al_core/memory/guard.py +330 -0
- al_core/memory/store.py +80 -0
- al_core/normalize/__init__.py +254 -0
- al_core/principal.py +59 -0
- al_core/receipt/__init__.py +192 -0
- al_core/resources.py +33 -0
- al_core/runtime.py +471 -0
- al_core/skills/__init__.py +30 -0
- al_core/skills/guard.py +218 -0
al_core/audit/db.py
ADDED
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
"""SQLite (WAL) mirror of the receipt ledger — query surface only.
|
|
2
|
+
|
|
3
|
+
**This is a mirror, never a source of truth** (invariant #7). The append-only
|
|
4
|
+
JSONL is canonical; on startup the mirror is rebuilt/reconciled from it. Nothing
|
|
5
|
+
reads a security decision back out of SQLite — it exists so operators and the
|
|
6
|
+
dashboard can query without parsing JSONL.
|
|
7
|
+
|
|
8
|
+
The mediator serves requests from a thread pool, so the single connection is
|
|
9
|
+
opened with ``check_same_thread=False`` and every access is guarded by a lock.
|
|
10
|
+
Writes are infrequent (one per decision), so contention is negligible.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import sqlite3
|
|
17
|
+
import threading
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from typing import Any, Iterable, Iterator
|
|
20
|
+
|
|
21
|
+
def org_of(actor: str) -> str:
|
|
22
|
+
"""Tenancy key from a receipt actor. ``spiffe://<org>/agent/<name>`` -> org;
|
|
23
|
+
everything else (``user:...``, mediator) -> ``system``. Phase-6 isolation
|
|
24
|
+
filters on this."""
|
|
25
|
+
if actor.startswith("spiffe://"):
|
|
26
|
+
rest = actor[len("spiffe://"):]
|
|
27
|
+
return rest.split("/", 1)[0] or "system"
|
|
28
|
+
return "system"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _org_filter(org: str | None) -> tuple[list[str], list[Any]]:
|
|
32
|
+
"""SQL clause + params scoping a query to one tenant (None = all orgs).
|
|
33
|
+
|
|
34
|
+
Every read path takes this: an operator scoped to an org must not be able
|
|
35
|
+
to reach another org's events through *any* endpoint — aggregates and
|
|
36
|
+
single-receipt lookups included, not just search."""
|
|
37
|
+
if org is None:
|
|
38
|
+
return [], []
|
|
39
|
+
if org == "system": # non-spiffe actors (mediator, console users)
|
|
40
|
+
return ["actor NOT LIKE 'spiffe://%'"], []
|
|
41
|
+
prefix = actor_prefix(org)
|
|
42
|
+
if prefix is None:
|
|
43
|
+
return ["FALSE"], []
|
|
44
|
+
return ["substr(actor, 1, ?) = ?"], [len(prefix), prefix]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def actor_prefix(org: str) -> str | None:
|
|
48
|
+
"""``spiffe://<org>/``, the literal start of every actor in ``org``. None
|
|
49
|
+
when no actor can belong to it: a scope holding ``/`` is not an org, it
|
|
50
|
+
reaches into another org's namespace.
|
|
51
|
+
|
|
52
|
+
Callers compare this prefix exactly. A ``LIKE`` pattern built from the org
|
|
53
|
+
would treat ``%`` and ``_`` in it as wildcards across tenants."""
|
|
54
|
+
return None if not org or "/" in org else f"spiffe://{org}/"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
_SCHEMA = """
|
|
58
|
+
CREATE TABLE IF NOT EXISTS events (
|
|
59
|
+
seq INTEGER PRIMARY KEY,
|
|
60
|
+
ts TEXT NOT NULL,
|
|
61
|
+
actor TEXT NOT NULL,
|
|
62
|
+
action TEXT NOT NULL,
|
|
63
|
+
target TEXT NOT NULL,
|
|
64
|
+
verdict TEXT NOT NULL,
|
|
65
|
+
block_reason TEXT,
|
|
66
|
+
prev_hash TEXT NOT NULL,
|
|
67
|
+
record_hash TEXT NOT NULL,
|
|
68
|
+
receipt_json TEXT NOT NULL
|
|
69
|
+
);
|
|
70
|
+
CREATE INDEX IF NOT EXISTS idx_events_actor ON events(actor);
|
|
71
|
+
CREATE INDEX IF NOT EXISTS idx_events_action ON events(action);
|
|
72
|
+
CREATE INDEX IF NOT EXISTS idx_events_verdict ON events(verdict);
|
|
73
|
+
|
|
74
|
+
CREATE TABLE IF NOT EXISTS identities (
|
|
75
|
+
spiffe_id TEXT PRIMARY KEY,
|
|
76
|
+
org TEXT NOT NULL,
|
|
77
|
+
name TEXT NOT NULL,
|
|
78
|
+
public_key_hex TEXT NOT NULL,
|
|
79
|
+
token_hash TEXT NOT NULL,
|
|
80
|
+
created_ts TEXT NOT NULL
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
CREATE TABLE IF NOT EXISTS quotas (
|
|
84
|
+
quota_key TEXT PRIMARY KEY,
|
|
85
|
+
window_start TEXT NOT NULL,
|
|
86
|
+
count INTEGER NOT NULL DEFAULT 0,
|
|
87
|
+
bytes INTEGER NOT NULL DEFAULT 0
|
|
88
|
+
);
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
_INSERT_EVENT = """
|
|
92
|
+
INSERT INTO events (seq, ts, actor, action, target, verdict,
|
|
93
|
+
block_reason, prev_hash, record_hash, receipt_json)
|
|
94
|
+
VALUES (:seq, :ts, :actor, :action, :target, :verdict,
|
|
95
|
+
:block_reason, :prev_hash, :record_hash, :receipt_json)
|
|
96
|
+
ON CONFLICT(seq) DO UPDATE SET
|
|
97
|
+
ts=excluded.ts, actor=excluded.actor, action=excluded.action,
|
|
98
|
+
target=excluded.target, verdict=excluded.verdict,
|
|
99
|
+
block_reason=excluded.block_reason, prev_hash=excluded.prev_hash,
|
|
100
|
+
record_hash=excluded.record_hash, receipt_json=excluded.receipt_json
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
_INSERT_IDENTITY = """
|
|
104
|
+
INSERT INTO identities (spiffe_id, org, name, public_key_hex, token_hash, created_ts)
|
|
105
|
+
VALUES (:spiffe_id, :org, :name, :public_key_hex, :token_hash, :created_ts)
|
|
106
|
+
ON CONFLICT(spiffe_id) DO UPDATE SET
|
|
107
|
+
org=excluded.org, name=excluded.name,
|
|
108
|
+
public_key_hex=excluded.public_key_hex,
|
|
109
|
+
token_hash=excluded.token_hash, created_ts=excluded.created_ts
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class SqliteMirror:
|
|
114
|
+
def __init__(self, path: str | Path, *, read_only: bool = False) -> None:
|
|
115
|
+
"""``read_only=True`` opens another process's live mirror for queries
|
|
116
|
+
only (URI ``mode=ro``): no schema writes, no WAL switch, no upserts.
|
|
117
|
+
This is how the control plane reads the data plane's evidence without
|
|
118
|
+
ever becoming a second writer of its chain or mirror."""
|
|
119
|
+
self._path = Path(path)
|
|
120
|
+
self._read_only = read_only
|
|
121
|
+
is_memory = str(self._path) == ":memory:"
|
|
122
|
+
if read_only:
|
|
123
|
+
# Fails if the file does not exist — callers treat that as "no
|
|
124
|
+
# evidence yet" and retry on the next query. Autocommit
|
|
125
|
+
# (isolation_level=None): a polling reader must never pin a read
|
|
126
|
+
# snapshot in an implicit transaction, or it would keep serving
|
|
127
|
+
# the past while the writer's chain grows.
|
|
128
|
+
self._conn = sqlite3.connect(
|
|
129
|
+
f"file:{self._path.as_posix()}?mode=ro", uri=True,
|
|
130
|
+
check_same_thread=False, isolation_level=None)
|
|
131
|
+
self._conn.row_factory = sqlite3.Row
|
|
132
|
+
self._lock = threading.RLock()
|
|
133
|
+
return
|
|
134
|
+
if not is_memory:
|
|
135
|
+
self._path.parent.mkdir(parents=True, exist_ok=True)
|
|
136
|
+
self._lock = threading.RLock()
|
|
137
|
+
self._conn = sqlite3.connect(str(self._path), check_same_thread=False)
|
|
138
|
+
self._conn.row_factory = sqlite3.Row
|
|
139
|
+
if not is_memory:
|
|
140
|
+
self._conn.execute("PRAGMA journal_mode=WAL")
|
|
141
|
+
self._conn.execute("PRAGMA foreign_keys=ON")
|
|
142
|
+
self._conn.executescript(_SCHEMA)
|
|
143
|
+
self._conn.commit()
|
|
144
|
+
|
|
145
|
+
def upsert_event(self, receipt: dict[str, Any]) -> None:
|
|
146
|
+
params = {
|
|
147
|
+
"seq": receipt["seq"],
|
|
148
|
+
"ts": receipt["ts"],
|
|
149
|
+
"actor": receipt["actor"],
|
|
150
|
+
"action": receipt["action"],
|
|
151
|
+
"target": receipt["target"],
|
|
152
|
+
"verdict": receipt["verdict"],
|
|
153
|
+
"block_reason": receipt.get("block_reason"),
|
|
154
|
+
"prev_hash": receipt["prev_hash"],
|
|
155
|
+
"record_hash": receipt["record_hash"],
|
|
156
|
+
"receipt_json": json.dumps(receipt, separators=(",", ":")),
|
|
157
|
+
}
|
|
158
|
+
with self._lock:
|
|
159
|
+
self._conn.execute(_INSERT_EVENT, params)
|
|
160
|
+
self._conn.commit()
|
|
161
|
+
|
|
162
|
+
def upsert_identity(self, ident: dict[str, Any]) -> None:
|
|
163
|
+
with self._lock:
|
|
164
|
+
self._conn.execute(_INSERT_IDENTITY, ident)
|
|
165
|
+
self._conn.commit()
|
|
166
|
+
|
|
167
|
+
def quota_try_consume(
|
|
168
|
+
self,
|
|
169
|
+
quota_key: str,
|
|
170
|
+
window_start: str,
|
|
171
|
+
*,
|
|
172
|
+
count_delta: int = 0,
|
|
173
|
+
bytes_delta: int = 0,
|
|
174
|
+
max_count: int | None = None,
|
|
175
|
+
max_bytes: int | None = None,
|
|
176
|
+
) -> tuple[bool, int, int]:
|
|
177
|
+
"""Atomic check-and-increment of one quota row.
|
|
178
|
+
|
|
179
|
+
A row whose ``window_start`` differs from the given one counts as zero
|
|
180
|
+
(window rollover). If applying the deltas would exceed a given maximum,
|
|
181
|
+
nothing is written and ``(False, current_count, current_bytes)`` is
|
|
182
|
+
returned; otherwise the new totals are persisted and returned with True.
|
|
183
|
+
"""
|
|
184
|
+
with self._lock:
|
|
185
|
+
row = self._conn.execute(
|
|
186
|
+
"SELECT window_start, count, bytes FROM quotas WHERE quota_key=?",
|
|
187
|
+
(quota_key,),
|
|
188
|
+
).fetchone()
|
|
189
|
+
in_window = row is not None and row["window_start"] == window_start
|
|
190
|
+
cur_count = row["count"] if in_window else 0
|
|
191
|
+
cur_bytes = row["bytes"] if in_window else 0
|
|
192
|
+
new_count, new_bytes = cur_count + count_delta, cur_bytes + bytes_delta
|
|
193
|
+
if (max_count is not None and new_count > max_count) or (
|
|
194
|
+
max_bytes is not None and new_bytes > max_bytes
|
|
195
|
+
):
|
|
196
|
+
return False, cur_count, cur_bytes
|
|
197
|
+
self._conn.execute(
|
|
198
|
+
"INSERT INTO quotas (quota_key, window_start, count, bytes) "
|
|
199
|
+
"VALUES (?, ?, ?, ?) "
|
|
200
|
+
"ON CONFLICT(quota_key) DO UPDATE SET "
|
|
201
|
+
"window_start=excluded.window_start, count=excluded.count, "
|
|
202
|
+
"bytes=excluded.bytes",
|
|
203
|
+
(quota_key, window_start, new_count, new_bytes),
|
|
204
|
+
)
|
|
205
|
+
self._conn.commit()
|
|
206
|
+
return True, new_count, new_bytes
|
|
207
|
+
|
|
208
|
+
def quota_usage(self, quota_key: str, window_start: str) -> tuple[int, int]:
|
|
209
|
+
"""(count, bytes) for the given window; zeros if absent or rolled over."""
|
|
210
|
+
with self._lock:
|
|
211
|
+
row = self._conn.execute(
|
|
212
|
+
"SELECT window_start, count, bytes FROM quotas WHERE quota_key=?",
|
|
213
|
+
(quota_key,),
|
|
214
|
+
).fetchone()
|
|
215
|
+
if row is None or row["window_start"] != window_start:
|
|
216
|
+
return 0, 0
|
|
217
|
+
return row["count"], row["bytes"]
|
|
218
|
+
|
|
219
|
+
def latest_seq(self) -> int | None:
|
|
220
|
+
with self._lock:
|
|
221
|
+
row = self._conn.execute("SELECT MAX(seq) AS m FROM events").fetchone()
|
|
222
|
+
return row["m"] if row and row["m"] is not None else None
|
|
223
|
+
|
|
224
|
+
def last_config_change(self) -> dict[str, Any] | None:
|
|
225
|
+
with self._lock:
|
|
226
|
+
row = self._conn.execute(
|
|
227
|
+
"SELECT receipt_json FROM events WHERE action='config_change' "
|
|
228
|
+
"ORDER BY seq DESC LIMIT 1"
|
|
229
|
+
).fetchone()
|
|
230
|
+
return json.loads(row["receipt_json"]) if row else None
|
|
231
|
+
|
|
232
|
+
def recent(self, limit: int = 50) -> list[dict[str, Any]]:
|
|
233
|
+
with self._lock:
|
|
234
|
+
rows = self._conn.execute(
|
|
235
|
+
"SELECT receipt_json FROM events ORDER BY seq DESC LIMIT ?", (limit,)
|
|
236
|
+
).fetchall()
|
|
237
|
+
return [json.loads(r["receipt_json"]) for r in rows]
|
|
238
|
+
|
|
239
|
+
# -- read-only aggregates (dashboard query surface; mirror only) ----------
|
|
240
|
+
|
|
241
|
+
def _group_count(self, column: str, extra: str = "",
|
|
242
|
+
org: str | None = None) -> dict[str, int]:
|
|
243
|
+
# ``column``/``extra`` are fixed identifiers from the callers below,
|
|
244
|
+
# never user input — safe to interpolate; values are always bound.
|
|
245
|
+
clauses, params = _org_filter(org)
|
|
246
|
+
if extra:
|
|
247
|
+
clauses.append(extra)
|
|
248
|
+
where = ("WHERE " + " AND ".join(clauses)) if clauses else ""
|
|
249
|
+
with self._lock:
|
|
250
|
+
rows = self._conn.execute(
|
|
251
|
+
f"SELECT {column} AS k, COUNT(*) AS c FROM events {where} "
|
|
252
|
+
f"GROUP BY {column} ORDER BY c DESC", params
|
|
253
|
+
).fetchall()
|
|
254
|
+
return {r["k"]: r["c"] for r in rows}
|
|
255
|
+
|
|
256
|
+
def verdict_counts(self, org: str | None = None) -> dict[str, int]:
|
|
257
|
+
return self._group_count("verdict", org=org)
|
|
258
|
+
|
|
259
|
+
def action_counts(self, org: str | None = None) -> dict[str, int]:
|
|
260
|
+
return self._group_count("action", org=org)
|
|
261
|
+
|
|
262
|
+
def block_reason_counts(self, org: str | None = None) -> dict[str, int]:
|
|
263
|
+
return self._group_count("block_reason", "block_reason IS NOT NULL", org=org)
|
|
264
|
+
|
|
265
|
+
def actor_counts(self, org: str | None = None) -> dict[str, int]:
|
|
266
|
+
return self._group_count("actor", org=org)
|
|
267
|
+
|
|
268
|
+
def org_counts(self, org: str | None = None) -> dict[str, int]:
|
|
269
|
+
"""Event counts per org, derived from the actor (Phase-6 tenancy view).
|
|
270
|
+
|
|
271
|
+
``spiffe://<org>/...`` -> org; ``user:...`` / mediator -> "system".
|
|
272
|
+
Scoped to ``org`` when given, so a tenant's own view lists only itself
|
|
273
|
+
— the org list is itself tenant-sensitive (it names your competitors)."""
|
|
274
|
+
out: dict[str, int] = {}
|
|
275
|
+
for actor, n in self.actor_counts(org).items():
|
|
276
|
+
out[org_of(actor)] = out.get(org_of(actor), 0) + n
|
|
277
|
+
return out
|
|
278
|
+
|
|
279
|
+
def count_events(self, org: str | None = None) -> int:
|
|
280
|
+
clauses, params = _org_filter(org)
|
|
281
|
+
where = ("WHERE " + " AND ".join(clauses)) if clauses else ""
|
|
282
|
+
with self._lock:
|
|
283
|
+
row = self._conn.execute(
|
|
284
|
+
f"SELECT COUNT(*) AS c FROM events {where}", params).fetchone()
|
|
285
|
+
return int(row["c"])
|
|
286
|
+
|
|
287
|
+
def recent_filtered(
|
|
288
|
+
self,
|
|
289
|
+
*,
|
|
290
|
+
limit: int = 50,
|
|
291
|
+
actor: str | None = None,
|
|
292
|
+
action: str | None = None,
|
|
293
|
+
verdict: str | None = None,
|
|
294
|
+
org: str | None = None,
|
|
295
|
+
) -> list[dict[str, Any]]:
|
|
296
|
+
"""Recent receipts, newest first, with optional exact-match filters."""
|
|
297
|
+
clauses, params = _org_filter(org)
|
|
298
|
+
for col, val in (("actor", actor), ("action", action), ("verdict", verdict)):
|
|
299
|
+
if val is not None:
|
|
300
|
+
clauses.append(f"{col}=?")
|
|
301
|
+
params.append(val)
|
|
302
|
+
where = ("WHERE " + " AND ".join(clauses)) if clauses else ""
|
|
303
|
+
params.append(max(1, min(limit, 500)))
|
|
304
|
+
with self._lock:
|
|
305
|
+
rows = self._conn.execute(
|
|
306
|
+
f"SELECT receipt_json FROM events {where} ORDER BY seq DESC LIMIT ?",
|
|
307
|
+
params,
|
|
308
|
+
).fetchall()
|
|
309
|
+
return [json.loads(r["receipt_json"]) for r in rows]
|
|
310
|
+
|
|
311
|
+
def stats(self, org: str | None = None) -> dict[str, Any]:
|
|
312
|
+
"""One-shot summary for the dashboard/CLI (no chain verification)."""
|
|
313
|
+
return {
|
|
314
|
+
"events": self.count_events(org),
|
|
315
|
+
"latest_seq": self.latest_seq(),
|
|
316
|
+
"verdicts": self.verdict_counts(org),
|
|
317
|
+
"actions": self.action_counts(org),
|
|
318
|
+
"block_reasons": self.block_reason_counts(org),
|
|
319
|
+
"actors": self.actor_counts(org),
|
|
320
|
+
"orgs": self.org_counts(org),
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
def receipt_by_seq(self, seq: int, org: str | None = None) -> dict[str, Any] | None:
|
|
324
|
+
"""One receipt. Scoped: another tenant's seq is *not found*, never
|
|
325
|
+
readable — a direct-seq fetch must not be an isolation bypass."""
|
|
326
|
+
clauses, params = _org_filter(org)
|
|
327
|
+
clauses.append("seq=?")
|
|
328
|
+
params.append(seq)
|
|
329
|
+
with self._lock:
|
|
330
|
+
row = self._conn.execute(
|
|
331
|
+
"SELECT receipt_json FROM events WHERE " + " AND ".join(clauses),
|
|
332
|
+
params,
|
|
333
|
+
).fetchone()
|
|
334
|
+
return json.loads(row["receipt_json"]) if row else None
|
|
335
|
+
|
|
336
|
+
def search(
|
|
337
|
+
self,
|
|
338
|
+
*,
|
|
339
|
+
query: str | None = None,
|
|
340
|
+
since: str | None = None,
|
|
341
|
+
until: str | None = None,
|
|
342
|
+
actor: str | None = None,
|
|
343
|
+
action: str | None = None,
|
|
344
|
+
verdict: str | None = None,
|
|
345
|
+
org: str | None = None,
|
|
346
|
+
limit: int = 100,
|
|
347
|
+
) -> list[dict[str, Any]]:
|
|
348
|
+
"""Filtered + text-searched receipts, newest first.
|
|
349
|
+
|
|
350
|
+
``query`` is a case-insensitive substring over actor/target/block_reason;
|
|
351
|
+
``since``/``until`` bound the ``ts`` (ISO strings, lexicographically
|
|
352
|
+
comparable); ``org`` scopes to one tenant (Phase-6 isolation). All
|
|
353
|
+
filters are exact-match and bound as parameters."""
|
|
354
|
+
clauses, params = _org_filter(org)
|
|
355
|
+
for col, val in (("actor", actor), ("action", action), ("verdict", verdict)):
|
|
356
|
+
if val is not None:
|
|
357
|
+
clauses.append(f"{col}=?")
|
|
358
|
+
params.append(val)
|
|
359
|
+
if since is not None:
|
|
360
|
+
clauses.append("ts >= ?")
|
|
361
|
+
params.append(since)
|
|
362
|
+
if until is not None:
|
|
363
|
+
clauses.append("ts <= ?")
|
|
364
|
+
params.append(until)
|
|
365
|
+
if query:
|
|
366
|
+
clauses.append("(actor LIKE ? OR target LIKE ? OR IFNULL(block_reason,'') LIKE ?)")
|
|
367
|
+
like = f"%{query}%"
|
|
368
|
+
params.extend([like, like, like])
|
|
369
|
+
where = ("WHERE " + " AND ".join(clauses)) if clauses else ""
|
|
370
|
+
params.append(max(1, min(limit, 500)))
|
|
371
|
+
with self._lock:
|
|
372
|
+
rows = self._conn.execute(
|
|
373
|
+
f"SELECT receipt_json FROM events {where} ORDER BY seq DESC LIMIT ?",
|
|
374
|
+
params,
|
|
375
|
+
).fetchall()
|
|
376
|
+
return [json.loads(r["receipt_json"]) for r in rows]
|
|
377
|
+
|
|
378
|
+
def iter_receipts(
|
|
379
|
+
self,
|
|
380
|
+
*,
|
|
381
|
+
since: str | None = None,
|
|
382
|
+
until: str | None = None,
|
|
383
|
+
org: str | None = None,
|
|
384
|
+
batch: int = 1000,
|
|
385
|
+
) -> Iterator[dict[str, Any]]:
|
|
386
|
+
"""Every receipt in the window, newest first — uncapped.
|
|
387
|
+
|
|
388
|
+
``search`` is capped for interactive endpoints; anything that must
|
|
389
|
+
*count* the ledger (a signed attestation) pages through here instead,
|
|
390
|
+
keyset-paginated on ``seq`` so memory stays bounded."""
|
|
391
|
+
base, base_params = _org_filter(org)
|
|
392
|
+
if since is not None:
|
|
393
|
+
base.append("ts >= ?")
|
|
394
|
+
base_params.append(since)
|
|
395
|
+
if until is not None:
|
|
396
|
+
base.append("ts <= ?")
|
|
397
|
+
base_params.append(until)
|
|
398
|
+
last_seq: int | None = None
|
|
399
|
+
while True:
|
|
400
|
+
clauses, params = list(base), list(base_params)
|
|
401
|
+
if last_seq is not None:
|
|
402
|
+
clauses.append("seq < ?")
|
|
403
|
+
params.append(last_seq)
|
|
404
|
+
where = ("WHERE " + " AND ".join(clauses)) if clauses else ""
|
|
405
|
+
params.append(max(1, batch))
|
|
406
|
+
with self._lock:
|
|
407
|
+
rows = self._conn.execute(
|
|
408
|
+
f"SELECT seq, receipt_json FROM events {where} "
|
|
409
|
+
"ORDER BY seq DESC LIMIT ?", params,
|
|
410
|
+
).fetchall()
|
|
411
|
+
if not rows:
|
|
412
|
+
return
|
|
413
|
+
for r in rows:
|
|
414
|
+
yield json.loads(r["receipt_json"])
|
|
415
|
+
last_seq = rows[-1]["seq"]
|
|
416
|
+
|
|
417
|
+
def time_series(self, *, buckets: int = 24, span_hours: int = 24,
|
|
418
|
+
now: str | None = None, org: str | None = None) -> list[dict[str, Any]]:
|
|
419
|
+
"""Counts per time bucket (verdict breakdown) over the trailing window.
|
|
420
|
+
|
|
421
|
+
``ts`` is an ISO-8601 string; SQLite ``strftime`` parses it. Buckets are
|
|
422
|
+
even divisions of ``span_hours`` ending at ``now`` (or the latest event).
|
|
423
|
+
Returns oldest-first list of {bucket_start, total, allow, block, strip,
|
|
424
|
+
warn}."""
|
|
425
|
+
org_clauses, org_params = _org_filter(org)
|
|
426
|
+
with self._lock:
|
|
427
|
+
if now is None:
|
|
428
|
+
# The window ends at the tenant's own latest event — an empty
|
|
429
|
+
# org must not inherit another tenant's clock.
|
|
430
|
+
where = ("WHERE " + " AND ".join(org_clauses)) if org_clauses else ""
|
|
431
|
+
row = self._conn.execute(
|
|
432
|
+
f"SELECT MAX(ts) AS m FROM events {where}", org_params).fetchone()
|
|
433
|
+
now = row["m"] if row and row["m"] else None
|
|
434
|
+
if now is None:
|
|
435
|
+
return []
|
|
436
|
+
clauses = [*org_clauses,
|
|
437
|
+
"ts >= strftime('%Y-%m-%dT%H:%M:%fZ', ?, ?)", "ts <= ?"]
|
|
438
|
+
rows = self._conn.execute(
|
|
439
|
+
"SELECT ts, verdict FROM events WHERE " + " AND ".join(clauses),
|
|
440
|
+
[*org_params, now, f"-{span_hours} hours", now],
|
|
441
|
+
).fetchall()
|
|
442
|
+
# Bucket in Python (portable; avoids SQLite epoch-math edge cases).
|
|
443
|
+
from datetime import datetime, timedelta
|
|
444
|
+
|
|
445
|
+
def parse(s: str) -> datetime:
|
|
446
|
+
return datetime.fromisoformat(s.replace("Z", "+00:00"))
|
|
447
|
+
|
|
448
|
+
end = parse(now)
|
|
449
|
+
start = end - timedelta(hours=span_hours)
|
|
450
|
+
width = (end - start) / buckets
|
|
451
|
+
series = [{"bucket_start": (start + width * i).isoformat().replace("+00:00", "Z"),
|
|
452
|
+
"total": 0, "allow": 0, "block": 0, "strip": 0, "warn": 0, "ask": 0}
|
|
453
|
+
for i in range(buckets)]
|
|
454
|
+
for r in rows:
|
|
455
|
+
try:
|
|
456
|
+
idx = int((parse(r["ts"]) - start) / width)
|
|
457
|
+
except (ValueError, ZeroDivisionError):
|
|
458
|
+
continue
|
|
459
|
+
idx = max(0, min(buckets - 1, idx))
|
|
460
|
+
series[idx]["total"] += 1
|
|
461
|
+
v = r["verdict"]
|
|
462
|
+
if v in series[idx]:
|
|
463
|
+
series[idx][v] += 1
|
|
464
|
+
return series
|
|
465
|
+
|
|
466
|
+
def reconcile(self, receipts: Iterable[dict[str, Any]]) -> int:
|
|
467
|
+
n = 0
|
|
468
|
+
for r in receipts:
|
|
469
|
+
self.upsert_event(r)
|
|
470
|
+
n += 1
|
|
471
|
+
return n
|
|
472
|
+
|
|
473
|
+
def close(self) -> None:
|
|
474
|
+
with self._lock:
|
|
475
|
+
self._conn.close()
|
|
476
|
+
|
|
477
|
+
def __enter__(self) -> "SqliteMirror":
|
|
478
|
+
return self
|
|
479
|
+
|
|
480
|
+
def __exit__(self, *exc: object) -> None:
|
|
481
|
+
self.close()
|