Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omnet/blob/main/cmd/tkad/test/identity.py # omnet aien-dev/omnet Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omnet / cmd / tkad / test / identity.py` 225 lines ยท 8119 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: Apache-2.0 3 """Store identity and header schema. 4 5 The daemon header carries one schema word and a 128-bit store identity, so a 6 client that follows a path can tell whether the store behind it is still the 7 same store it was talking to. This pins the four properties that make the 8 identity usable, and the header validation that keeps it honest: 9 10 * init draws a fresh identity, from the kernel CSPRNG, once; 11 * the identity survives a restart and follows the directory when it is 12 renamed, so it names the store and not the path; 13 * two independent stores never share an identity, so the A6 replacement case 14 is detectable rather than silent; 15 * a header whose schema, identity or reserved words were altered is refused 16 instead of being served. 17 """ 18 import base64 19 import hashlib 20 import json 21 import pathlib 22 import socket 23 import struct 24 import subprocess 25 import sys 26 import tempfile 27 import time 28 29 PROBE = "--probe" in sys.argv 30 BIN = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else "build/tkad") 31 if not BIN.is_absolute(): 32 BIN = pathlib.Path.cwd() / BIN 33 checks = 0 34 35 36 def check(value, message): 37 global checks 38 assert value, message 39 checks += 1 40 41 42 def exact(sock, count): 43 result = b"" 44 while len(result) < count: 45 part = sock.recv(count - len(result)) 46 if not part: 47 raise AssertionError("truncated daemon reply") 48 result += part 49 return result 50 51 52 class Daemon: 53 def __init__(self, directory, initialize=False): 54 self.directory = pathlib.Path(directory) 55 self.path = str(self.directory / "tkad.sock") 56 self.p = subprocess.Popen( 57 [str(BIN), str(self.directory)] + (["--init"] if initialize else []), 58 stdout=subprocess.PIPE, 59 stderr=subprocess.PIPE, 60 ) 61 deadline = time.monotonic() + 30 62 while time.monotonic() < deadline: 63 if self.p.poll() is not None: 64 raise AssertionError(f"startup rc={self.p.returncode}: {self.p.communicate()}") 65 try: 66 with socket.socket(socket.AF_UNIX) as s: 67 s.settimeout(.2) 68 s.connect(self.path) 69 return 70 except (FileNotFoundError, ConnectionRefusedError, TimeoutError): 71 time.sleep(.02) 72 raise AssertionError("socket readiness timeout") 73 74 def stop(self): 75 if self.p.poll() is None: 76 self.p.kill() 77 self.p.communicate(timeout=10) 78 79 def map(self): 80 return self.rpc(11, {}) 81 82 def rpc(self, route, body, admin=1): 83 payload = json.dumps(body, separators=(",", ":")).encode() 84 frame = struct.pack("<4sBBHQI", b"TKA\x01", route, admin, 0, 0, len(payload)) + payload 85 with socket.socket(socket.AF_UNIX) as s: 86 s.settimeout(30) 87 s.connect(self.path) 88 s.sendall(frame) 89 magic, status, reserved, size = struct.unpack("<4sHHI", exact(s, 12)) 90 check(magic == b"TKA\x01" and reserved == 0 and size <= 65536, "reply header") 91 answer = json.loads(exact(s, size)) 92 check(status == 200, f"route {route} status {status}: {answer}") 93 return answer 94 95 96 def store(state): 97 state.mkdir(mode=0o700) 98 d = Daemon(state, initialize=True) 99 try: 100 return d.map()["StoreID"] 101 finally: 102 d.stop() 103 104 105 def restart(state): 106 d = Daemon(state) 107 try: 108 return d.map()["StoreID"] 109 finally: 110 d.stop() 111 112 113 def header(state, word, value): 114 """Rewrite one header word with the framing and checksums recomputed.""" 115 snapshot = (state / "snapshot").read_bytes() 116 payloads = frames(snapshot) 117 struct.pack_into("