askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_ledger_integrity.py · 26.4 KB · 604 lines
| 1 | """Anchoring a ledger: one digest that stands for a whole prefix, lodged |
| 2 | where the server that took it cannot reach. |
| 3 | |
| 4 | Three claims carry this file. |
| 5 | |
| 6 | A PREFIX DIGEST IS A CHAIN HEAD. For an append-only log, a hash chain is |
| 7 | just an incremental way of computing prefix digests. Folding directly |
| 8 | gives the same value, which is why the cheap version could ship without a |
| 9 | storage-layer change AND why moving the computation into the append path |
| 10 | later would not orphan an anchor already published. The test that pins it |
| 11 | is the one showing `head()` equals a hand-rolled fold of `link()`. |
| 12 | |
| 13 | THE DIGEST MUST SURVIVE ORDINARY MAINTENANCE. Compaction rewrites append |
| 14 | files and migrations add columns. Either one invalidating every historical |
| 15 | anchor would make the check cry wolf on a schedule, and a check that cries |
| 16 | wolf is one nobody reads. Both are tested here as the load-bearing cases |
| 17 | they are. |
| 18 | |
| 19 | AN ANCHOR NOBODY ELSE HOLDS IS NOT EVIDENCE. The page must say so in |
| 20 | words, because a green tick for a digest sitting in the same directory as |
| 21 | the ledger it describes would be the most misleading thing this package |
| 22 | could do. |
| 23 | """ |
| 24 | |
| 25 | import json |
| 26 | import pathlib |
| 27 | |
| 28 | import pytest |
| 29 | from conftest import stage_collection |
| 30 | |
| 31 | import object_execution |
| 32 | import object_ledger_head |
| 33 | import object_records |
| 34 | import python_object_runtime |
| 35 | |
| 36 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 37 | PACKAGES = REPO_ROOT / "packages" |
| 38 | INTEGRITY_OBJECTS = PACKAGES / "app-integrity" / "objects" |
| 39 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 40 | |
| 41 | FIELDS = ["id", "wallet_id", "amount_minor", "kind"] |
| 42 | |
| 43 | |
| 44 | def entry(row_id, amount, wallet="w1", kind="topup"): |
| 45 | return {"id": row_id, "wallet_id": wallet, |
| 46 | "amount_minor": str(amount), "kind": kind} |
| 47 | |
| 48 | |
| 49 | LEDGER = [entry("e1", 1000), entry("e2", -250), entry("e3", 5000)] |
| 50 | |
| 51 | |
| 52 | # --- the pure fold ------------------------------------------------------------ |
| 53 | |
| 54 | def test_the_head_is_exactly_a_chain_fold_over_the_rows(): |
| 55 | """The property the whole design rests on. If these two ever differ, |
| 56 | then moving the computation into the append path later WOULD orphan |
| 57 | every anchor already published, and the cheap version stops being a |
| 58 | safe place to start.""" |
| 59 | by_hand = object_ledger_head.genesis("wallet_entries", FIELDS) |
| 60 | for row in LEDGER: |
| 61 | by_hand = object_ledger_head.link(by_hand, row, FIELDS) |
| 62 | |
| 63 | folded = object_ledger_head.head(LEDGER, FIELDS, collection="wallet_entries") |
| 64 | assert folded["digest"] == by_hand |
| 65 | assert folded["row_count"] == 3 |
| 66 | |
| 67 | |
| 68 | def test_a_head_is_bound_to_its_collection_and_its_field_list(): |
| 69 | """Otherwise an anchor taken over wallet_entries could be checked |
| 70 | against payments and come out clean, and a field list differing by one |
| 71 | column would be a coincidence rather than a different chain.""" |
| 72 | a = object_ledger_head.head(LEDGER, FIELDS, collection="wallet_entries") |
| 73 | b = object_ledger_head.head(LEDGER, FIELDS, collection="payments") |
| 74 | c = object_ledger_head.head(LEDGER, FIELDS + ["note"], |
| 75 | collection="wallet_entries") |
| 76 | assert len({a["digest"], b["digest"], c["digest"]}) == 3 |
| 77 | |
| 78 | |
| 79 | def test_appending_never_disturbs_an_earlier_prefix(): |
| 80 | """The property that makes an anchor checkable a year later: a ledger |
| 81 | that has grown is the normal case, not a break.""" |
| 82 | anchor = object_ledger_head.head(LEDGER, FIELDS, collection="wallet_entries") |
| 83 | grown = LEDGER + [entry("e4", 99), entry("e5", 1)] |
| 84 | verdict = object_ledger_head.verify(grown, {**anchor}) |
| 85 | assert verdict["verified"] is True |
| 86 | assert verdict["present_rows"] == 5 |
| 87 | assert verdict["anchored_rows"] == 3 |
| 88 | |
| 89 | |
| 90 | def test_editing_a_row_inside_the_prefix_is_caught(): |
| 91 | anchor = object_ledger_head.head(LEDGER, FIELDS, collection="wallet_entries") |
| 92 | tampered = [dict(LEDGER[0]), {**LEDGER[1], "amount_minor": "-25"}, LEDGER[2]] |
| 93 | verdict = object_ledger_head.verify(tampered, anchor) |
| 94 | assert verdict["verified"] is False |
| 95 | assert verdict["status"] == "mismatch" |
| 96 | assert "changed after the fact" in verdict["detail"] |
| 97 | |
| 98 | |
| 99 | def test_removing_rows_reports_truncation_rather_than_a_mismatch(): |
| 100 | """Different remedy, so a different word: a mismatch means find what |
| 101 | changed, a truncation means find what is missing. Retention pruning an |
| 102 | anchored ledger lands here, and it SHOULD -- pruning really does |
| 103 | destroy the evidence the anchor was taken over.""" |
| 104 | anchor = object_ledger_head.head(LEDGER, FIELDS, collection="wallet_entries") |
| 105 | verdict = object_ledger_head.verify(LEDGER[:2], anchor) |
| 106 | assert verdict["status"] == "truncated" |
| 107 | assert verdict["anchored_rows"] == 3 and verdict["present_rows"] == 2 |
| 108 | |
| 109 | |
| 110 | def test_a_schema_gaining_a_column_does_not_turn_old_anchors_red(): |
| 111 | """The subtler trap, and the reason the field list is stored IN the |
| 112 | anchor. Reading today's schema instead would make an ordinary |
| 113 | migration indistinguishable from an attack, which teaches an operator |
| 114 | to ignore the alarm.""" |
| 115 | anchor = object_ledger_head.head(LEDGER, FIELDS, collection="wallet_entries") |
| 116 | migrated = [{**row, "reference": ""} for row in LEDGER] |
| 117 | assert object_ledger_head.verify(migrated, anchor)["verified"] is True |
| 118 | |
| 119 | # And a row that populates the NEW field still verifies, because the |
| 120 | # anchor never covered it. That is correct: an anchor cannot make a |
| 121 | # claim about a column that did not exist when it was taken. |
| 122 | populated = [{**row, "reference": "later"} for row in LEDGER] |
| 123 | assert object_ledger_head.verify(populated, anchor)["verified"] is True |
| 124 | |
| 125 | |
| 126 | def test_the_canonical_form_is_injective_across_field_boundaries(): |
| 127 | """A naive tab-join is only injective while no value can contain a |
| 128 | tab, which couples the integrity digest to the storage format. Length |
| 129 | prefixing means an attacker cannot move characters across a field |
| 130 | boundary and keep the digest.""" |
| 131 | left = [{"a": "xy", "b": "z"}] |
| 132 | right = [{"a": "x", "b": "yz"}] |
| 133 | fields = ["a", "b"] |
| 134 | assert (object_ledger_head.head(left, fields)["digest"] |
| 135 | != object_ledger_head.head(right, fields)["digest"]) |
| 136 | |
| 137 | # And the delimiter itself cannot be smuggled in. |
| 138 | assert (object_ledger_head.head([{"a": "1:x|", "b": ""}], fields)["digest"] |
| 139 | != object_ledger_head.head([{"a": "", "b": "1:x|"}], fields)["digest"]) |
| 140 | |
| 141 | |
| 142 | def test_a_ladder_of_anchors_brackets_where_the_break_is(): |
| 143 | """One anchor says 'something in the first N rows changed'. A ladder |
| 144 | says which day. That resolution is the reason the pass is daily.""" |
| 145 | long_ledger = [entry(f"e{n}", n * 10) for n in range(1, 21)] |
| 146 | ladder = [object_ledger_head.head(long_ledger, FIELDS, |
| 147 | collection="wallet_entries", count=n) |
| 148 | for n in (5, 10, 15, 20)] |
| 149 | |
| 150 | tampered = list(long_ledger) |
| 151 | tampered[12] = {**tampered[12], "amount_minor": "999999"} |
| 152 | |
| 153 | where = object_ledger_head.locate(tampered, ladder) |
| 154 | assert where["broken"] is True |
| 155 | assert where["last_good_row_count"] == 10 # row 13 is inside 11..15 |
| 156 | assert where["first_bad_row_count"] == 15 |
| 157 | assert "between row 11 and row 15" in where["detail"] |
| 158 | |
| 159 | |
| 160 | def test_an_untouched_ledger_reports_no_break_at_all(): |
| 161 | ladder = [object_ledger_head.head(LEDGER, FIELDS, |
| 162 | collection="wallet_entries", count=n) |
| 163 | for n in (1, 2, 3)] |
| 164 | assert object_ledger_head.locate(LEDGER, ladder)["broken"] is False |
| 165 | |
| 166 | |
| 167 | def test_an_anchor_with_no_field_list_is_unusable_rather_than_verified(): |
| 168 | """Failing open here would mean a corrupted anchor row reads as a |
| 169 | clean bill of health, which is the wrong direction for every possible |
| 170 | caller.""" |
| 171 | verdict = object_ledger_head.verify(LEDGER, {"digest": "abc", "fields": [], |
| 172 | "row_count": 3}) |
| 173 | assert verdict["status"] == "unusable" |
| 174 | assert verdict["verified"] is False |
| 175 | |
| 176 | |
| 177 | # --- through the objects ------------------------------------------------------ |
| 178 | |
| 179 | @pytest.fixture |
| 180 | def data_dir(tmp_path, monkeypatch): |
| 181 | base = tmp_path / "data" |
| 182 | stage_collection(base, "app-integrity", "anchors") |
| 183 | stage_collection(base, "app-settings", "app_settings") |
| 184 | stage_collection(base, "app-billing", "wallet_entries") |
| 185 | stage_collection(base, "app-billing", "wallets") |
| 186 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(base)) |
| 187 | # A notary on 127.0.0.1 is the only notary a test has. In |
| 188 | # production this is refused -- see the self-anchoring tests below. |
| 189 | monkeypatch.setenv("DBBASIC_NOTARY_ALLOW_LOOPBACK", "1") |
| 190 | return base |
| 191 | |
| 192 | |
| 193 | def run(object_id, payload=None, *, method="POST"): |
| 194 | return object_execution.execute_object( |
| 195 | RUNTIME, |
| 196 | object_execution.ObjectExecutionRequest(object_id, method=method, |
| 197 | payload=payload or {}), |
| 198 | roots=[INTEGRITY_OBJECTS]).result |
| 199 | |
| 200 | |
| 201 | def setting(data_dir, key, value): |
| 202 | return object_records.create_collection_record( |
| 203 | "app_settings", {"key": key, "value": value}, base_dir=data_dir) |
| 204 | |
| 205 | |
| 206 | def wallet_entry(data_dir, amount, wallet="w1"): |
| 207 | if not object_records.read_collection_records("wallets", base_dir=data_dir): |
| 208 | object_records.create_collection_record( |
| 209 | "wallets", {"id": wallet, "owner_id": "shop", "kind": "gift_card", |
| 210 | "code": "GC-1", "is_active": "true"}, |
| 211 | base_dir=data_dir) |
| 212 | return object_records.create_collection_record( |
| 213 | "wallet_entries", |
| 214 | {"wallet_id": wallet, "amount_minor": str(amount), "kind": "topup", |
| 215 | "owner_id": "shop"}, |
| 216 | base_dir=data_dir) |
| 217 | |
| 218 | |
| 219 | def test_a_pass_anchors_the_ledgers_that_exist_and_skips_the_rest(data_dir): |
| 220 | setting(data_dir, "ledger.anchored_collections", |
| 221 | "wallet_entries,fin_journal_lines") |
| 222 | wallet_entry(data_dir, 1000) |
| 223 | wallet_entry(data_dir, 2500) |
| 224 | |
| 225 | result = run("system_publish_head") |
| 226 | assert result["ok"] is True |
| 227 | by_collection = {r["collection"]: r for r in result["results"]} |
| 228 | assert by_collection["wallet_entries"]["row_count"] == 2 |
| 229 | assert by_collection["fin_journal_lines"]["status"] == "skipped" |
| 230 | |
| 231 | anchors = object_records.read_collection_records("anchors", base_dir=data_dir) |
| 232 | assert len(anchors) == 1 |
| 233 | assert anchors[0]["collection"] == "wallet_entries" |
| 234 | assert anchors[0]["row_count"] == "2" |
| 235 | assert anchors[0]["covered_fields"] |
| 236 | |
| 237 | |
| 238 | def test_a_dormant_ledger_writes_no_second_anchor(data_dir): |
| 239 | """Not tidiness: an anchor's whole content is (row_count, digest), so |
| 240 | an identical second one carries nothing a reader could act on while |
| 241 | making the ladder longer with no extra resolution.""" |
| 242 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 243 | wallet_entry(data_dir, 1000) |
| 244 | |
| 245 | run("system_publish_head") |
| 246 | second = run("system_publish_head") |
| 247 | |
| 248 | assert second["results"][0]["status"] == "unchanged" |
| 249 | assert len(object_records.read_collection_records( |
| 250 | "anchors", base_dir=data_dir)) == 1 |
| 251 | |
| 252 | wallet_entry(data_dir, 77) |
| 253 | third = run("system_publish_head") |
| 254 | assert third["results"][0]["row_count"] == 2 |
| 255 | assert len(object_records.read_collection_records( |
| 256 | "anchors", base_dir=data_dir)) == 2 |
| 257 | |
| 258 | |
| 259 | def test_with_no_notary_configured_the_pass_says_so_in_the_result(data_dir): |
| 260 | """A digest nobody else holds is a bookkeeping entry. The pass records |
| 261 | it anyway -- it is worth more than nothing and tomorrow's pass covers |
| 262 | the same history under a longer prefix -- but it must not be quiet |
| 263 | about what is missing.""" |
| 264 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 265 | wallet_entry(data_dir, 1000) |
| 266 | |
| 267 | result = run("system_publish_head") |
| 268 | assert result["independent_lodgements"] == 0 |
| 269 | assert "notary.endpoints" in result["warning"] |
| 270 | assert "held only by the server that took them" in result["warning"] |
| 271 | |
| 272 | anchors = object_records.read_collection_records("anchors", base_dir=data_dir) |
| 273 | assert anchors[0]["status"] == "recorded" |
| 274 | assert anchors[0]["notary_count"] == "0" |
| 275 | |
| 276 | |
| 277 | def test_an_unreachable_notary_still_leaves_the_anchor_recorded(data_dir): |
| 278 | """One dead endpoint must not cost the anchor. Failing closed here |
| 279 | would mean an outage silently produces a hole in the ladder, which is |
| 280 | exactly the period an incident would later be traced to.""" |
| 281 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 282 | setting(data_dir, "notary.endpoints", "http://127.0.0.1:9/nope") |
| 283 | wallet_entry(data_dir, 1000) |
| 284 | |
| 285 | result = run("system_publish_head") |
| 286 | assert result["results"][0]["status"] == "failed" |
| 287 | |
| 288 | anchors = object_records.read_collection_records("anchors", base_dir=data_dir) |
| 289 | assert len(anchors) == 1 |
| 290 | assert anchors[0]["notary_count"] == "0" |
| 291 | assert "unreachable" in anchors[0]["note"] |
| 292 | |
| 293 | |
| 294 | def test_the_pass_declares_EVENT_because_that_is_what_the_daemon_calls(): |
| 295 | """Declaring only POST is a scheduled pass that silently never runs, |
| 296 | and that has been a real production bug here more than once.""" |
| 297 | source = (PACKAGES / "app-integrity" / "objects" / "system" |
| 298 | / "publish_head.py").read_text() |
| 299 | assert "def EVENT(" in source |
| 300 | assert "POST = EVENT" in source |
| 301 | |
| 302 | |
| 303 | def test_page_views_is_not_anchored_by_default_and_the_default_says_why(): |
| 304 | """Anchor what somebody would profit from changing. page_views is the |
| 305 | highest-volume writer on the box and the least valuable thing in it, |
| 306 | so anchoring it would cost a full read of the largest file here every |
| 307 | day to protect traffic nobody profits from forging.""" |
| 308 | source = (PACKAGES / "app-integrity" / "objects" / "system" |
| 309 | / "publish_head.py").read_text() |
| 310 | assert "page_views" in source # named, as a decision |
| 311 | default_block = source.split("DEFAULT_COLLECTIONS = (")[1].split(")")[0] |
| 312 | assert "page_views" not in default_block |
| 313 | assert "wallet_entries" in default_block |
| 314 | assert "notarizations" in default_block # the notary watches itself |
| 315 | |
| 316 | |
| 317 | # --- the page ----------------------------------------------------------------- |
| 318 | |
| 319 | def integrity_page(*, path="/ledger-integrity"): |
| 320 | return object_execution.execute_object( |
| 321 | RUNTIME, |
| 322 | object_execution.ObjectExecutionRequest("site_ledger_integrity", |
| 323 | method="GET", |
| 324 | payload={"_path": path}), |
| 325 | roots=[INTEGRITY_OBJECTS]).result |
| 326 | |
| 327 | |
| 328 | def test_the_page_refuses_a_green_tick_for_a_digest_nobody_else_holds(data_dir): |
| 329 | """The single most important assertion in this file. Every anchor |
| 330 | verifies and the page must still say the check is worth nothing |
| 331 | against intent, because the anchors sit in the same directory as the |
| 332 | ledger under the same permissions.""" |
| 333 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 334 | wallet_entry(data_dir, 1000) |
| 335 | run("system_publish_head") |
| 336 | |
| 337 | # Normalised, because the assertions here are about the SENTENCES the |
| 338 | # page commits to and those wrap across source lines. A test that |
| 339 | # breaks when a paragraph is rewrapped teaches people to weaken it. |
| 340 | body = " ".join(integrity_page()["body"].split()) |
| 341 | assert "Anchored, but only here" in body |
| 342 | assert "no independent party holds any of them" in body |
| 343 | assert "anybody able to rewrite a ledger can rewrite its anchor" in body |
| 344 | assert "notary.endpoints" in body |
| 345 | assert "Verified, and held elsewhere" not in body |
| 346 | |
| 347 | |
| 348 | def test_the_page_catches_a_ledger_edited_behind_its_anchor(data_dir): |
| 349 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 350 | first = wallet_entry(data_dir, 1000) |
| 351 | wallet_entry(data_dir, 2000) |
| 352 | run("system_publish_head") |
| 353 | |
| 354 | object_records.update_collection_record( |
| 355 | "wallet_entries", first["id"], {"amount_minor": "999999"}, |
| 356 | base_dir=data_dir) |
| 357 | |
| 358 | fold = json.loads(integrity_page(path="/ledger-integrity.json")["body"]) |
| 359 | assert fold["broken"] == ["wallet_entries"] |
| 360 | |
| 361 | body = integrity_page()["body"] |
| 362 | assert "An anchor does not verify" in body |
| 363 | assert "wallet_entries" in body |
| 364 | |
| 365 | |
| 366 | def test_the_page_and_the_json_are_the_same_fold(data_dir): |
| 367 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 368 | wallet_entry(data_dir, 1000) |
| 369 | run("system_publish_head") |
| 370 | |
| 371 | fold = json.loads(integrity_page(path="/ledger-integrity.json")["body"]) |
| 372 | assert fold["ledger_count"] == 1 |
| 373 | assert fold["evidence"] is False |
| 374 | assert fold["ledgers"][0]["collection"] == "wallet_entries" |
| 375 | assert fold["ledgers"][0]["anchors"][0]["verdict"]["verified"] is True |
| 376 | |
| 377 | |
| 378 | def test_with_nothing_anchored_the_page_says_that_rather_than_looking_fine(data_dir): |
| 379 | body = integrity_page()["body"] |
| 380 | assert "Nothing is anchored yet" in body |
| 381 | assert "system_publish_head" in body |
| 382 | |
| 383 | |
| 384 | # --- the wire contract between the two packages ------------------------------- |
| 385 | # |
| 386 | # The one place a bug here would hide indefinitely. system_publish_head |
| 387 | # decides a lodgement succeeded by reading `found` out of the notary's |
| 388 | # answer; if that shape ever changed, every pass would quietly record |
| 389 | # "failed" and the integrity page would go on saying nothing independent |
| 390 | # holds the digests -- an alarm that is technically correct and completely |
| 391 | # misleading. Nothing else in either test file crosses the HTTP boundary |
| 392 | # these two packages actually talk over, so this is where the contract |
| 393 | # gets pinned. |
| 394 | |
| 395 | def _notary_server(data_dir): |
| 396 | """A real HTTP server that dispatches to the real action_notarize. |
| 397 | |
| 398 | Not a stub returning a canned body -- a stub would pass whatever shape |
| 399 | it was written against, which is exactly the bug being guarded here. |
| 400 | """ |
| 401 | import http.server |
| 402 | import threading |
| 403 | |
| 404 | notary_objects = PACKAGES / "app-notary" / "objects" |
| 405 | |
| 406 | class Handler(http.server.BaseHTTPRequestHandler): |
| 407 | def do_POST(self): |
| 408 | length = int(self.headers.get("Content-Length") or 0) |
| 409 | payload = json.loads(self.rfile.read(length) or b"{}") |
| 410 | payload["_identity"] = {"user_id": "peer"} |
| 411 | result = object_execution.execute_object( |
| 412 | RUNTIME, |
| 413 | object_execution.ObjectExecutionRequest( |
| 414 | "action_notarize", method="POST", payload=payload), |
| 415 | roots=[notary_objects]).result |
| 416 | body = json.dumps(result).encode("utf-8") |
| 417 | self.send_response(int(result.get("status") or 200)) |
| 418 | self.send_header("Content-Type", "application/json") |
| 419 | self.send_header("Content-Length", str(len(body))) |
| 420 | self.end_headers() |
| 421 | self.wfile.write(body) |
| 422 | |
| 423 | def log_message(self, *args): |
| 424 | pass |
| 425 | |
| 426 | server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler) |
| 427 | thread = threading.Thread(target=server.serve_forever, daemon=True) |
| 428 | thread.start() |
| 429 | return server, f"http://127.0.0.1:{server.server_port}" |
| 430 | |
| 431 | |
| 432 | def test_a_head_lodged_with_a_real_notary_comes_back_as_evidence(data_dir): |
| 433 | stage_collection(data_dir, "app-notary", "notarizations") |
| 434 | server, url = _notary_server(data_dir) |
| 435 | try: |
| 436 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 437 | setting(data_dir, "notary.endpoints", url) |
| 438 | wallet_entry(data_dir, 1000) |
| 439 | wallet_entry(data_dir, 2000) |
| 440 | |
| 441 | result = run("system_publish_head") |
| 442 | assert result["results"][0]["status"] == "published" |
| 443 | assert result["independent_lodgements"] == 1 |
| 444 | assert result["warning"] == "" |
| 445 | |
| 446 | # The digest the notary now holds is the digest the anchor claims. |
| 447 | anchors = object_records.read_collection_records("anchors", |
| 448 | base_dir=data_dir) |
| 449 | lodged = object_records.read_collection_records("notarizations", |
| 450 | base_dir=data_dir) |
| 451 | assert len(lodged) == 1 |
| 452 | assert lodged[0]["digest"] == anchors[0]["digest"] |
| 453 | assert lodged[0]["label"] == "wallet_entries head @ 2 rows" |
| 454 | |
| 455 | # And the page stops hedging, because now somebody else holds it. |
| 456 | body = " ".join(integrity_page()["body"].split()) |
| 457 | assert "Verified, and held elsewhere" in body |
| 458 | assert "Anchored, but only here" not in body |
| 459 | finally: |
| 460 | server.shutdown() |
| 461 | |
| 462 | |
| 463 | def test_re_lodging_an_unchanged_history_is_treated_as_success(data_dir): |
| 464 | """The notary answers `already_recorded` for a digest it has seen, and |
| 465 | that is a SUCCESS -- it means the history has an even older independent |
| 466 | timestamp than this pass. Reading it as a refusal would make every |
| 467 | re-anchor of a dormant ledger look like a failing notary.""" |
| 468 | stage_collection(data_dir, "app-notary", "notarizations") |
| 469 | server, url = _notary_server(data_dir) |
| 470 | try: |
| 471 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 472 | setting(data_dir, "notary.endpoints", url) |
| 473 | wallet_entry(data_dir, 1000) |
| 474 | run("system_publish_head") |
| 475 | |
| 476 | # A new row, so a new prefix and a new digest -- then remove the |
| 477 | # anchor's evidence of having lodged the FIRST digest and re-run |
| 478 | # against the same notary, which still holds it. |
| 479 | wallet_entry(data_dir, 500) |
| 480 | second = run("system_publish_head") |
| 481 | assert second["results"][0]["status"] == "published" |
| 482 | |
| 483 | lodged = object_records.read_collection_records("notarizations", |
| 484 | base_dir=data_dir) |
| 485 | assert len(lodged) == 2 # two prefixes, two digests |
| 486 | |
| 487 | again = _relodge_first(data_dir, url) |
| 488 | assert again is True |
| 489 | finally: |
| 490 | server.shutdown() |
| 491 | |
| 492 | |
| 493 | def _relodge_first(data_dir, url): |
| 494 | """Submit an already-held digest and confirm publish_head calls it ok.""" |
| 495 | import importlib.util |
| 496 | spec = importlib.util.spec_from_file_location( |
| 497 | "_ph", PACKAGES / "app-integrity" / "objects" / "system" / "publish_head.py") |
| 498 | module = importlib.util.module_from_spec(spec) |
| 499 | spec.loader.exec_module(module) |
| 500 | first = object_records.read_collection_records("anchors", |
| 501 | base_dir=data_dir)[0] |
| 502 | ok, detail = module._lodge(url, first["digest"], "re-lodge") |
| 503 | assert detail == "already held", detail |
| 504 | return ok |
| 505 | |
| 506 | |
| 507 | # --- self-anchoring, which would undo the whole honesty design ---------------- |
| 508 | |
| 509 | def test_a_notary_that_is_this_server_is_not_independent(): |
| 510 | """The hole worth closing before anyone finds it: point |
| 511 | notary.endpoints at your own box, every lodgement succeeds, and the |
| 512 | page swaps "anchored, but only here" for "verified, and held |
| 513 | elsewhere" -- a specific false claim, which is worse than the honest |
| 514 | missing one it replaced.""" |
| 515 | import object_notary |
| 516 | |
| 517 | assert object_notary.is_self_endpoint("http://localhost:8000") is True |
| 518 | assert object_notary.is_self_endpoint("http://127.0.0.1:9000") is True |
| 519 | assert object_notary.is_self_endpoint("https://127.53.1.9") is True |
| 520 | assert object_notary.is_self_endpoint( |
| 521 | "https://object.dbbasic.com", "https://object.dbbasic.com") is True |
| 522 | # Same host, different scheme and path -- still the same server. |
| 523 | assert object_notary.is_self_endpoint( |
| 524 | "http://object.dbbasic.com/notary", "https://object.dbbasic.com/") is True |
| 525 | |
| 526 | # And a genuinely different party is not caught. |
| 527 | assert object_notary.is_self_endpoint( |
| 528 | "https://notary.example.org", "https://object.dbbasic.com") is False |
| 529 | assert object_notary.is_self_endpoint("https://notary.example.org") is False |
| 530 | |
| 531 | |
| 532 | def test_pointing_the_pass_at_itself_records_zero_independent_copies(data_dir, |
| 533 | monkeypatch): |
| 534 | """End to end: the pass must not count it, must say why in the note, |
| 535 | and the page must keep hedging.""" |
| 536 | monkeypatch.delenv("DBBASIC_NOTARY_ALLOW_LOOPBACK", raising=False) |
| 537 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 538 | setting(data_dir, "portal.base_url", "https://books.example.com") |
| 539 | setting(data_dir, "notary.endpoints", "https://books.example.com") |
| 540 | wallet_entry(data_dir, 1000) |
| 541 | |
| 542 | result = run("system_publish_head") |
| 543 | assert result["results"][0]["status"] == "failed" |
| 544 | assert result["independent_lodgements"] == 0 |
| 545 | |
| 546 | anchor = object_records.read_collection_records("anchors", |
| 547 | base_dir=data_dir)[0] |
| 548 | assert anchor["notary_count"] == "0" |
| 549 | assert "is this server" in anchor["note"] |
| 550 | assert "somebody else" in anchor["note"] |
| 551 | |
| 552 | body = " ".join(integrity_page()["body"].split()) |
| 553 | assert "Anchored, but only here" in body |
| 554 | assert "Verified, and held elsewhere" not in body |
| 555 | |
| 556 | |
| 557 | def test_the_loopback_escape_is_an_env_var_and_not_a_setting(data_dir, |
| 558 | monkeypatch): |
| 559 | """A settings page is exactly where somebody about to fool themselves |
| 560 | would go looking for the switch, so the switch is not there.""" |
| 561 | monkeypatch.delenv("DBBASIC_NOTARY_ALLOW_LOOPBACK", raising=False) |
| 562 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 563 | setting(data_dir, "notary.endpoints", "http://127.0.0.1:9") |
| 564 | setting(data_dir, "notary.allow_loopback", "true") # ignored, on purpose |
| 565 | wallet_entry(data_dir, 1000) |
| 566 | |
| 567 | refused = run("system_publish_head") |
| 568 | assert refused["results"][0]["status"] == "failed" |
| 569 | assert "is this server" in object_records.read_collection_records( |
| 570 | "anchors", base_dir=data_dir)[0]["note"] |
| 571 | |
| 572 | |
| 573 | def test_none_means_not_configured_rather_than_a_host_called_none(data_dir): |
| 574 | """app_settings requires a non-empty value, so "nothing" needs a word. |
| 575 | Without this an operator who writes `none` to mean "no notary" gets a |
| 576 | daily failed lodgement against a host literally called `none` -- noise |
| 577 | indistinguishable from a real notary being down, which is the worst |
| 578 | shape a false alarm can take.""" |
| 579 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 580 | setting(data_dir, "notary.endpoints", "none") |
| 581 | wallet_entry(data_dir, 1000) |
| 582 | |
| 583 | result = run("system_publish_head") |
| 584 | assert result["endpoints"] == [] |
| 585 | assert result["results"][0]["status"] == "recorded" # not "failed" |
| 586 | assert "notary.endpoints" in result["warning"] |
| 587 | |
| 588 | anchor = object_records.read_collection_records("anchors", |
| 589 | base_dir=data_dir)[0] |
| 590 | assert anchor["note"] == "" |
| 591 | |
| 592 | |
| 593 | def test_the_page_and_the_pass_use_the_same_endpoint_parser(data_dir): |
| 594 | """They cannot import each other, so the parser lives in |
| 595 | object_notary. When they briefly each had their own, the page reported |
| 596 | an endpoint the pass was correctly ignoring -- a small lie in the one |
| 597 | place whose whole job is to be believed.""" |
| 598 | setting(data_dir, "ledger.anchored_collections", "wallet_entries") |
| 599 | setting(data_dir, "notary.endpoints", "none") |
| 600 | wallet_entry(data_dir, 1000) |
| 601 | |
| 602 | passed = run("system_publish_head") |
| 603 | page = json.loads(integrity_page(path="/ledger-integrity.json")["body"]) |
| 604 | assert passed["endpoints"] == page["endpoints"] == [] |