forked from ChelseaKR/habitable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_obslog.py
More file actions
456 lines (390 loc) · 15.2 KB
/
Copy pathtest_obslog.py
File metadata and controls
456 lines (390 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright 2026 Chelsea Kelly-Reif
"""Opt-in, on-device, metadata-only structured logging for the CLI and app server.
These tests pin the same contract the relay's logging honours, specialized to the
local surfaces: structured one-object-per-line JSON, off by default, and an absolute
no-plaintext gate — no filenames, passphrases, media bytes, case ids, or key material
ever reach the log stream.
"""
from __future__ import annotations
import base64
import io
import json
import logging
import sys
import threading
import time
import urllib.error
import urllib.request
from collections.abc import Callable, Iterator
from pathlib import Path
import piexif
import pytest
from PIL import Image
from habitable.appserver import make_app_server
from habitable.cli import main
from habitable.obslog import (
_JsonFormatter,
configure_logging,
enabled_from_env,
is_configured,
log_event,
reset_logging,
)
from habitable.tsa import LocalRfc3161TSA
from habitable.vault import Vault
# Keys any local log line may legitimately carry. A structural allowlist: it fails
# loudly if instrumentation ever grows a field (e.g. a path or id) that isn't clearly
# metadata, catching a leak before it ships.
_ALLOWED_KEYS = {
"ts",
"level",
"msg",
"command",
"ok",
"duration_ms",
"media_type",
"timestamped",
"had_location",
"extra_authorities",
"resolved",
"archived",
"sent",
"bytes_sent",
"fetched",
"messages_merged",
"captures_imported",
"method",
"path",
"status",
"latency_ms",
}
@pytest.fixture(autouse=True)
def _reset_obslog() -> Iterator[None]:
"""Keep the shared ``habitable`` logger clean between tests (no stale handler)."""
reset_logging()
yield
reset_logging()
def _lines(buffer: io.StringIO) -> list[str]:
return [ln for ln in buffer.getvalue().splitlines() if ln.strip()]
# --- (a) the formatter emits exactly one metadata-only JSON object per line -------
def test_formatter_emits_one_json_line_with_expected_keys() -> None:
record = logging.makeLogRecord(
{"msg": "capture", "levelname": "INFO", "created": 1_767_312_000.0}
)
record.event_fields = {"timestamped": True, "extra_authorities": 2, "skipped": None}
line = _JsonFormatter().format(record)
assert "\n" not in line # exactly one physical line
payload = json.loads(line) # must be valid JSON
assert payload["msg"] == "capture"
assert payload["level"] == "info"
assert payload["timestamped"] is True
assert payload["extra_authorities"] == 2
assert "ts" in payload
assert "skipped" not in payload # None values are dropped, never serialized
# --- enabled_from_env: only "json" opts in ----------------------------------------
@pytest.mark.parametrize(
("value", "expected"),
[("json", True), ("JSON", True), (" json ", True), ("", False), ("1", False), ("on", False)],
)
def test_enabled_from_env(value: str, expected: bool) -> None:
assert enabled_from_env({"HABITABLE_LOG": value}) is expected
def test_enabled_from_env_unset_is_false() -> None:
assert enabled_from_env({}) is False
# --- log_event: the scalar-only guard + off-by-default no-op -----------------------
def test_log_event_is_a_noop_until_configured() -> None:
# No handler installed: even a non-scalar field must not raise or emit anything.
assert not is_configured()
log_event("noop", payload=object(), blob=b"bytes") # must not raise
def test_log_event_rejects_non_scalar_fields() -> None:
buffer = io.StringIO()
configure_logging(buffer)
for bad in ({"nested": 1}, [1, 2], b"raw-bytes", Path("/secret/path")):
with pytest.raises(TypeError):
log_event("evt", field=bad)
assert _lines(buffer) == [] # nothing leaked before the guard tripped
def test_log_event_accepts_scalar_metadata_and_drops_none() -> None:
buffer = io.StringIO()
configure_logging(buffer)
log_event("evt", count=3, ok=True, dur=1.5, name="init", sha="deadbeef", absent=None)
lines = _lines(buffer)
assert len(lines) == 1
payload = json.loads(lines[0])
assert payload["count"] == 3 and payload["ok"] is True and payload["dur"] == 1.5
assert payload["name"] == "init" and payload["sha"] == "deadbeef"
assert "absent" not in payload
def test_configure_logging_is_idempotent() -> None:
buffer = io.StringIO()
configure_logging(buffer)
configure_logging(buffer) # a second call must not stack handlers (double lines)
log_event("evt", n=1)
assert len(_lines(buffer)) == 1
# --- CLI end-to-end -----------------------------------------------------------------
def _make_jpeg(path: Path, *, trailing: bytes = b"") -> None:
payload: dict[str, object] = {"0th": {}, "Exif": {}, "GPS": {}, "1st": {}, "thumbnail": None}
Image.new("RGB", (16, 16), (10, 20, 30)).save(path, "jpeg", exif=piexif.dump(payload))
if trailing:
with path.open("ab") as handle:
handle.write(trailing)
def _run_flow(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
*,
log: bool,
passphrase: str = "test-pass",
case: str = "case-x",
title: str = "leak-under-sink",
media_name: str = "photo.jpg",
media_trailing: bytes = b"",
) -> tuple[str, str]:
"""Run init -> issue -> timeline -> capture -> export via the CLI, capturing the
stdout and the structured stderr log stream. ``log`` toggles ``--log-format json``."""
out, err = io.StringIO(), io.StringIO()
monkeypatch.setattr(sys, "stdout", out)
monkeypatch.setattr(sys, "stderr", err)
vault = tmp_path / "vault"
media = tmp_path / media_name
packet = tmp_path / "packet"
_make_jpeg(media, trailing=media_trailing)
prefix = ["--log-format", "json"] if log else []
assert main([*prefix, "init", str(vault), "--case", case, "--passphrase", passphrase]) == 0
assert (
main(
[
*prefix,
"issue",
"--vault",
str(vault),
"--passphrase",
passphrase,
"--category",
"plumbing",
"--title",
title,
]
)
== 0
)
issue_id = Vault.open(vault, passphrase).document.issues()[0].issue_id
assert (
main(
[
*prefix,
"timeline",
"--vault",
str(vault),
"--passphrase",
passphrase,
"--issue",
issue_id,
"--kind",
"observed",
"--text",
title,
]
)
== 0
)
assert (
main(
[
*prefix,
"capture",
str(media),
"--vault",
str(vault),
"--passphrase",
passphrase,
"--issue",
issue_id,
"--dev-tsa",
]
)
== 0
)
assert (
main(
[
*prefix,
"export",
"--vault",
str(vault),
"--passphrase",
passphrase,
"--out",
str(packet),
"--dev-tsa", # seal offline: a unit test must never call a public TSA
]
)
== 0
)
return out.getvalue(), err.getvalue()
def test_cli_json_logs_are_parseable_and_end_to_end(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
_stdout, stderr = _run_flow(tmp_path, monkeypatch, log=True)
lines = [ln for ln in stderr.splitlines() if ln.strip()]
assert lines, "expected structured log lines on stderr"
events: dict[str, list[dict[str, object]]] = {}
for line in lines:
record = json.loads(line) # every line must be valid JSON
assert set(record) <= _ALLOWED_KEYS, f"unexpected (possibly leaky) key in {record}"
assert {"ts", "level", "msg"} <= set(record)
events.setdefault(str(record["msg"]), []).append(record)
# One command-boundary event per subcommand, each successful.
commands = [e["command"] for e in events["command"]]
assert commands == ["init", "issue", "timeline", "capture", "export"]
assert all(e["ok"] is True for e in events["command"])
assert all(isinstance(e["duration_ms"], (int, float)) for e in events["command"])
# The capture pipeline emitted its metadata-only trace.
assert events["capture"] and events["capture"][0]["timestamped"] is True
def test_cli_is_silent_by_default(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("HABITABLE_LOG", raising=False)
_stdout, stderr = _run_flow(tmp_path, monkeypatch, log=False)
assert stderr.strip() == "" # no flag, no env => no structured log output
assert not is_configured()
def test_cli_enabled_by_env(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HABITABLE_LOG", "json")
out, err = io.StringIO(), io.StringIO()
monkeypatch.setattr(sys, "stdout", out)
monkeypatch.setattr(sys, "stderr", err)
vault = tmp_path / "vault"
assert main(["init", str(vault), "--case", "c", "--passphrase", "p"]) == 0
lines = [ln for ln in err.getvalue().splitlines() if ln.strip()]
assert lines, "HABITABLE_LOG=json must enable logging without the flag"
assert json.loads(lines[-1])["command"] == "init"
# --- (d) the no-plaintext guard, paralleling tests/test_relay.py --------------------
def test_logs_never_leak_secrets_or_content(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A representative init/capture/export flow with logging on must emit none of the
sentinels: not the passphrase, media filename, media bytes, or case/issue text."""
passphrase = "SENTINEL-PASSPHRASE-8931"
case = "SENTINEL-CASE-4471"
text = "SENTINEL-CASE-TEXT-2208"
media_name = "SENTINEL-FILENAME-6650.jpg"
media_bytes = b"SENTINEL-MEDIA-BYTES-1174"
_stdout, stderr = _run_flow(
tmp_path,
monkeypatch,
log=True,
passphrase=passphrase,
case=case,
title=text,
media_name=media_name,
media_trailing=media_bytes,
)
assert stderr.strip(), "the flow should have produced log lines to assert over"
for sentinel in (passphrase, case, text, media_name, media_bytes.decode("ascii")):
assert sentinel not in stderr, f"secret leaked into the log stream: {sentinel!r}"
# The device fingerprint (key-derived identifier) must not ride along either.
fingerprint = Vault.open(tmp_path / "vault", passphrase).identity.public().fingerprint
assert fingerprint not in stderr
# And every emitted line is metadata-only structured JSON.
for line in stderr.splitlines():
if line.strip():
assert set(json.loads(line)) <= _ALLOWED_KEYS
# --- app server request logging: redacted routes, no bodies -------------------------
def _call(
url: str,
method: str,
path: str,
body: dict[str, object] | None = None,
*,
token: str = "",
) -> tuple[int, dict[str, object]]:
data = json.dumps(body).encode() if body is not None else None
headers = {"Content-Type": "application/json"}
if token:
headers["X-Habitable-Token"] = token
request = urllib.request.Request(f"{url}{path}", data=data, method=method, headers=headers)
try:
with urllib.request.urlopen(request, timeout=5) as response:
return response.status, json.loads(response.read())
except urllib.error.HTTPError as exc:
payload = json.loads(exc.read())
exc.close()
return exc.code, payload
def _wait_for_lines(buffer: io.StringIO, count: int, timeout: float = 3.0) -> list[str]:
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
lines = _lines(buffer)
if len(lines) >= count:
return lines
time.sleep(0.02)
return _lines(buffer)
def _wait_for_request_lines(buffer: io.StringIO, count: int, timeout: float = 3.0) -> list[str]:
"""Wait for request records, not unrelated events emitted by a request.
A capture emits its own metadata event before the handler writes the access
record. Counting all log lines therefore races with the final request log.
"""
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
lines = _lines(buffer)
requests = [line for line in lines if json.loads(line).get("msg") == "request"]
if len(requests) >= count:
return lines
time.sleep(0.02)
return _lines(buffer)
def test_appserver_logs_redacted_routes_without_bodies(
make_vault: Callable[..., Vault],
local_tsa: LocalRfc3161TSA,
make_jpeg: Callable[..., Path],
tmp_path: Path,
) -> None:
buffer = io.StringIO()
configure_logging(buffer)
vault = make_vault()
server = make_app_server("127.0.0.1", 0, vault, tsa=local_tsa, static_root=tmp_path / "noapp")
port = server.server_address[1]
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
url = f"http://127.0.0.1:{port}"
try:
token = server.session_token
_status, issue = _call(url, "POST", "/api/issues", {"category": "mold"}, token=token)
issue_id = str(issue["issue_id"])
# The issue id is a sentinel: it must never appear in a log line.
photo = make_jpeg(name="SENTINEL-APP-FILE.jpg")
media_b64 = base64.b64encode(photo.read_bytes()).decode()
_call(
url,
"POST",
f"/api/issues/{issue_id}/timeline",
{"kind": "observed", "text": "SENTINEL-APP-TIMELINE"},
token=token,
)
_call(
url,
"POST",
"/api/capture",
{"issue_id": issue_id, "filename": "SENTINEL-APP-FILE.jpg", "media_b64": media_b64},
token=token,
)
_call(url, "GET", "/api/status", token=token)
lines = _wait_for_request_lines(buffer, 4)
finally:
server.shutdown()
server.server_close()
thread.join(timeout=5)
assert len(lines) >= 4
records = [json.loads(line) for line in lines]
for record in records:
assert set(record) <= _ALLOWED_KEYS # every line is metadata-only
# Per-request access lines carry a redacted route template and timing.
requests = [r for r in records if r["msg"] == "request"]
assert len(requests) >= 4
paths = {r["path"] for r in requests}
for r in requests:
assert isinstance(r["latency_ms"], (int, float))
# Routes are redacted templates; the per-issue route never carries the issue id.
assert "/api/issues/{issue}/timeline" in paths
assert {"/api/issues", "/api/capture", "/api/status"} <= paths
# The capture pipeline's own metadata event rode the same enabled logger.
assert any(r["msg"] == "capture" for r in records)
text = "\n".join(lines)
assert token not in text # the API bearer credential is never logged
assert issue_id not in text # no issue id
assert "SENTINEL-APP-TIMELINE" not in text # no request body
assert "SENTINEL-APP-FILE" not in text # no filename
assert media_b64 not in text # no media bytes