Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
ledger_integrity.py · 12.2 KB · 332 lines
1"""site_ledger_integrity -- the page somebody opens when they have been
2asked whether the records can be trusted.
3
4 GET /ledger-integrity every anchored ledger, checked
5 GET /ledger-integrity.json the same fold, for a monitor
6
7## It verifies; it does not anchor
8
9Kept separate from system_publish_head on purpose. A pass that both took
10anchors and pronounced them sound would be marking its own homework, and
11the failure would be invisible: the digest it wrote is trivially the
12digest it recomputes.
13
14## The banner is the page
15
16Everything below the banner is detail. The banner answers the only
17question that matters -- **is any of this held by somebody other than this
18server** -- and it answers it in plain words rather than a colour. With no
19notary configured, an anchor is a bookkeeping entry: anybody able to
20rewrite a ledger here can rewrite the row that describes it, sitting in
21the same directory, with the same permissions. Rendering that as a green
22tick would be the most misleading thing this package could do, so it
23renders as a warning that says exactly what is missing and what to set.
24
25The distinction the banner draws is not verified-versus-broken. It is
26**checkable-by-a-stranger versus checkable-only-by-us**, because the first
27is evidence and the second is a claim.
28
29## What "verified" means here, exactly
30
31That the first N rows of a ledger still hash to what an anchor recorded.
32Not that the ledger is complete, not that the rows are true, not that
33nothing was appended since -- appending is what a ledger is for and never
34disturbs an earlier prefix. See object_ledger_head for the three outcomes
35and why truncation is reported separately from a content mismatch.
36"""
37
38import html
39import json
40import os
41
42import object_ledger_head
43import object_notary
44import object_records
45import object_schemas
46
47ANCHORS = "anchors"
48SETTINGS_COLLECTION = "app_settings"
49ENDPOINTS_KEY = "notary.endpoints"
50
51_STYLE = """
52.li { max-width: 52rem; }
53.li h2 { font-size: 1rem; margin: 1.6rem 0 .4rem; }
54.li .banner { border: 1px solid var(--line, #38384a); border-radius: 8px;
55 padding: .9rem 1.1rem; margin: 1rem 0 1.6rem; }
56.li .banner.weak { border-color: var(--accent, #b5713a); }
57.li .banner h2 { margin: 0 0 .3rem; font-size: 1.05rem; }
58.li table { width: 100%; border-collapse: collapse; margin: .4rem 0 1.2rem; }
59.li th, .li td { text-align: left; padding: .35rem .5rem; vertical-align: top;
60 border-bottom: 1px solid var(--line, #38384a); font-size: .87rem; }
61.li .digest { font-family: ui-monospace, Menlo, monospace; font-size: .76rem;
62 word-break: break-all; }
63.li .ok { color: var(--accent, #b5713a); font-weight: 600; }
64.li .bad { color: #d05a5a; font-weight: 600; }
65.li .note { border-left: 3px solid var(--line, #55556a); padding: .1rem 0 .1rem .7rem;
66 margin: .6rem 0 1.2rem; font-size: .87rem; opacity: .85; }
67"""
68
69
70def _esc(value):
71 return html.escape(str(value if value is not None else ""), quote=True)
72
73
74def _base_dir():
75 return os.environ.get("DBBASIC_DATA_DIR", "data")
76
77
78def _text(value):
79 return str(value if value is not None else "").strip()
80
81
82def _settings(base):
83 try:
84 rows = object_records.read_collection_records(SETTINGS_COLLECTION,
85 base_dir=base)
86 except Exception:
87 return {}
88 return {_text(row.get("key")): _text(row.get("value"))
89 for row in rows if _text(row.get("key"))}
90
91
92def _anchor_to_spec(row):
93 """An anchor row as object_ledger_head.verify() wants it.
94
95 The field list comes out of the ROW, which is the decision that lets a
96 schema migration happen without every historical anchor turning red.
97 """
98 try:
99 row_count = int(_text(row.get("row_count")) or 0)
100 except ValueError:
101 row_count = 0
102 return {
103 "collection": _text(row.get("collection")),
104 "digest": _text(row.get("digest")),
105 "row_count": row_count,
106 "fields": [name for name in _text(row.get("covered_fields")).split(",")
107 if name],
108 }
109
110
111def report(*, base=None):
112 """Every anchored ledger, its current head, and each anchor checked.
113
114 One fold, two surfaces, so the page and the JSON cannot disagree.
115 """
116 base = _base_dir() if base is None else base
117 settings = _settings(base)
118 # The SAME parser the pass uses (object_notary), not a second one
119 # spelled the same way. When these drifted, the page reported an
120 # endpoint the pass was correctly ignoring.
121 endpoints = object_notary.endpoints_from_setting(
122 settings.get(ENDPOINTS_KEY, ""))
123
124 try:
125 anchor_rows = object_records.read_collection_records(ANCHORS,
126 base_dir=base)
127 except Exception:
128 anchor_rows = []
129
130 ledgers = {}
131 for row in anchor_rows:
132 ledgers.setdefault(_text(row.get("collection")), []).append(row)
133
134 entries = []
135 total_independent = 0
136 for collection in sorted(name for name in ledgers if name):
137 rows_for = ledgers[collection]
138 specs = [_anchor_to_spec(row) for row in rows_for]
139
140 try:
141 schema = object_schemas.get_schema(collection, base_dir=base)
142 fields = [_text(f.get("name")) for f in (schema or {}).get("fields") or []
143 if _text(f.get("name")) and _text(f.get("name")) != "_op"]
144 except Exception:
145 fields = []
146 try:
147 live = object_records.read_collection_records(collection, base_dir=base)
148 except Exception:
149 live = []
150
151 checked = []
152 for row, spec in zip(rows_for, specs):
153 verdict = object_ledger_head.verify(live, spec)
154 try:
155 count = int(_text(row.get("notary_count")) or 0)
156 except ValueError:
157 count = 0
158 total_independent += count
159 checked.append({
160 "taken_at": _text(row.get("created_at")),
161 "row_count": spec["row_count"],
162 "digest": spec["digest"],
163 "notary_count": count,
164 "notaries": _text(row.get("notaries")),
165 "status": _text(row.get("status")),
166 "verdict": verdict,
167 })
168 checked.sort(key=lambda entry: entry["taken_at"], reverse=True)
169
170 entries.append({
171 "collection": collection,
172 "present_rows": len(live),
173 "current_head": (object_ledger_head.head(live, fields,
174 collection=collection)
175 if fields else None),
176 "anchors": checked,
177 "break": object_ledger_head.locate(live, specs),
178 "independent_copies": max((c["notary_count"] for c in checked),
179 default=0),
180 })
181
182 anchored_independently = [e for e in entries if e["independent_copies"] > 0]
183 return {
184 "endpoints": endpoints,
185 "ledgers": entries,
186 "ledger_count": len(entries),
187 "independently_anchored": len(anchored_independently),
188 "total_independent_lodgements": total_independent,
189 "broken": [e["collection"] for e in entries if e["break"]["broken"]],
190 "evidence": bool(anchored_independently),
191 }
192
193
194# --- rendering ---------------------------------------------------------------
195
196def _banner_html(fold):
197 broken = fold["broken"]
198 if broken:
199 names = ", ".join(_esc(name) for name in broken)
200 return f"""
201<div class="banner weak">
202<h2 class="bad">An anchor does not verify</h2>
203<p>{names} no longer hashes to what was recorded. That means rows an anchor
204was taken over have been changed or removed since. Read the ledger's own
205section below for the window it happened in.</p>
206</div>"""
207
208 if not fold["ledgers"]:
209 return """
210<div class="banner weak">
211<h2>Nothing is anchored yet</h2>
212<p>No anchors have been taken, so there is nothing to verify. The daily
213pass (<code>system_publish_head</code>) writes one per ledger; run it by
214hand to start.</p>
215</div>"""
216
217 if not fold["evidence"]:
218 return f"""
219<div class="banner weak">
220<h2>Anchored, but only here</h2>
221<p>Every anchor verifies — and <strong>no independent party holds any of
222them</strong>. These rows sit in the same data directory as the ledgers
223they describe, under the same permissions, so anybody able to rewrite a
224ledger can rewrite its anchor beside it.</p>
225<p>That makes this page a useful check against <em>accident</em> and no
226check at all against <em>intent</em>. To change that, set
227<code>{_esc(ENDPOINTS_KEY)}</code> in settings to one or more notaries run
228by somebody else. Then rewriting history means also rewriting a record on
229a machine this operator does not control.</p>
230</div>"""
231
232 return f"""
233<div class="banner">
234<h2 class="ok">Verified, and held elsewhere</h2>
235<p>Every anchor verifies, and
236<strong>{fold['independently_anchored']} of {fold['ledger_count']}</strong>
237ledgers have a digest lodged with an independent notary
238({fold['total_independent_lodgements']} lodgement{
239 '' if fold['total_independent_lodgements'] == 1 else 's'} in all).
240Altering the anchored history of those ledgers means also altering a
241record held by somebody else.</p>
242</div>"""
243
244
245def _ledger_html(entry):
246 head = entry["current_head"]
247 head_line = (f'<p class="muted">Now: <strong>{entry["present_rows"]:,}</strong> '
248 f'rows &middot; <span class="digest">{_esc(head["digest"])}'
249 f'</span></p>' if head else
250 '<p class="muted">No schema on this box, so no current head '
251 'can be computed.</p>')
252
253 rows = "".join(
254 f'<tr><td><time datetime="{_esc(anchor["taken_at"])}">'
255 f'{_esc(anchor["taken_at"])}</time></td>'
256 f'<td>{anchor["row_count"]:,}</td>'
257 f'<td class="digest">{_esc(anchor["digest"][:16])}&hellip;</td>'
258 f'<td>{anchor["notary_count"] or "&mdash;"}</td>'
259 f'<td class="{"ok" if anchor["verdict"]["verified"] else "bad"}">'
260 f'{_esc(anchor["verdict"]["status"])}</td></tr>'
261 for anchor in entry["anchors"][:20])
262
263 window = (f'<p class="note bad">{_esc(entry["break"]["detail"])}</p>'
264 if entry["break"]["broken"] else "")
265
266 return f"""
267<h2>{_esc(entry["collection"])}</h2>
268{head_line}
269{window}
270<table>
271<thead><tr><th>Taken</th><th>Rows</th><th>Digest</th>
272<th>Independent copies</th><th>Checked</th></tr></thead>
273<tbody>{rows}</tbody></table>
274"""
275
276
277def _page(fold):
278 body = "".join(_ledger_html(entry) for entry in fold["ledgers"])
279 return f"""
280<div class="breadcrumb"><a href="/">Home</a> / Ledger integrity</div>
281<h1>Ledger integrity</h1>
282<p>Each ledger below has been digested at various points and those digests
283recorded. This page recomputes them against the ledgers as they stand now.
284A ledger that has grown is expected — appending never disturbs an earlier
285prefix, which is what makes an anchor checkable a year later.</p>
286{_banner_html(fold)}
287{body}
288<p class="note">"Verified" means the first N rows still hash to what was
289anchored. It does not mean the rows are true, that the ledger is complete,
290or that nothing was added since. What it rules out is a row being edited
291or removed after the fact — and it rules that out only as far as the
292anchor itself is beyond reach of whoever would do the editing.</p>
293<p><a href="/ledger-integrity.json">This page as JSON</a> &middot;
294<a href="/anchors">every anchor</a></p>
295"""
296
297
298def GET(request):
299 request = request or {}
300 path = _text(request.get("_path") or "/ledger-integrity").lower()
301 fold = report()
302
303 if path.endswith(".json"):
304 return {"status": 200,
305 "content_type": "application/json; charset=utf-8",
306 "body": json.dumps(fold, indent=2, sort_keys=True, default=str)}
307
308 return {
309 "status": 200,
310 "content_type": "text/html; charset=utf-8",
311 "body": f"""<!doctype html>
312<html lang="en">
313<head>
314<meta charset="utf-8">
315<meta name="viewport" content="width=device-width, initial-scale=1">
316<title>Ledger integrity</title>
317<link rel="stylesheet" href="/style">
318<style>{_STYLE}</style>
319</head>
320<body>
321<div class="wrap li">
322<header class="app"><h1><a href="/">DBBASIC</a></h1></header>
323{_page(fold)}
324</div>
325<script src="/nav"></script>
326</body>
327</html>""",
328 }
329
330
331def POST(request):
332 return GET(request)