File size: 2,359 Bytes
7cfea77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""End-to-end demo: two example self-scan reports (the exact shape
scan_own_domains.py's main() already produces), fed through the ledger and
diffed. Example data, clearly marked as such - not a live scan.

    python run_demo.py
"""
from __future__ import annotations

import os
import tempfile

from sipa_scan_ledger.adapter import records_from_self_scan_report
from sipa_scan_ledger.chain import ScanLedger
from sipa_scan_ledger.diff import diff_scans
from sipa_scan_ledger.verify import verify

# EXAMPLE reports, shaped exactly like scan_own_domains.py's real output -
# not a live scan result.
REPORT_DAY_1 = {
    "ran_at": "2026-10-16T08:00:00+00:00",
    "targets": {
        "sipa-os.org": [],
        "focus.sipa-os.org": [
            {"severity": "medium", "title": "Missing Content-Security-Policy"},
            {"severity": "low", "title": "Missing Referrer-Policy"},
        ],
    },
}

REPORT_DAY_8 = {
    "ran_at": "2026-10-23T08:00:00+00:00",
    "targets": {
        "sipa-os.org": [
            {"severity": "high", "title": "TLS cert expires in 6 days"},
        ],
        "focus.sipa-os.org": [
            {"severity": "low", "title": "Missing Referrer-Policy"},
        ],
    },
}


def main() -> None:
    log_path = os.path.join(tempfile.mkdtemp(), "scan_history.jsonl")
    ledger = ScanLedger(log_path)

    ledger.append_many(records_from_self_scan_report(REPORT_DAY_1))
    ledger.append_many(records_from_self_scan_report(REPORT_DAY_8))

    print("== sipa-os.org: 8 days apart ==")
    history = ledger.history_for("sipa-os.org")
    d = diff_scans(history[0], history[1])
    print(f"  new findings:      {d.new_findings}")
    print(f"  resolved findings: {d.resolved_findings}")
    print(f"  got_worse={d.got_worse()}  (a cert expiring soon showed up between scans)")

    print("\n== focus.sipa-os.org: 8 days apart ==")
    history = ledger.history_for("focus.sipa-os.org")
    d = diff_scans(history[0], history[1])
    print(f"  new findings:      {d.new_findings}")
    print(f"  resolved findings: {d.resolved_findings}")
    print(f"  improved={d.improved()}  (the CSP finding got fixed between scans)")

    res = verify(log_path)
    print(f"\nscan history: {res.count} entries - {'OK' if res.ok else 'FAIL: ' + str(res.problems)}")
    print(f"log file: {log_path}")


if __name__ == "__main__":
    main()