#!/usr/bin/env python3 """Verify the CAIN-42 etcd coordination cluster from its public HTTP API. No CAIN imports; Python 3.8+ standard library only. python3 verify_etcd_cluster.py ETCD_CLUSTER.json Re-queries the leader's public etcd API and compares it with the capture: LEADER /v3/maintenance/status reports the captured raft leader id and the same cluster id MEMBERS /v3/cluster/member/list returns exactly the members in the capture (id and name) VERSION /version returns the captured etcd server version HEALTH /health on the leader returns {"health":"true"} Prints VERDICT: PASS or VERDICT: FAIL with what differed. """ from __future__ import annotations import json import sys import urllib.request def post(url, payload=b"{}"): req = urllib.request.Request(url, data=payload, headers={"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=15) as r: return json.loads(r.read()) def get(url): with urllib.request.urlopen(url, timeout=15) as r: return json.loads(r.read()) def main(argv): cap = json.load(open(argv[1])) cluster_id = cap["cluster_id"] base = cap["leader"]["client_url"] problems = [] status = post(base + "/v3/maintenance/status") header = status.get("header", {}) if header.get("cluster_id") != cluster_id: problems.append("leader cluster id %r != captured %r" % (header.get("cluster_id"), cluster_id)) if str(status.get("leader")) != str(cap["leader"]["member_id"]): problems.append("raft leader %r != captured %r" % (status.get("leader"), cap["leader"]["member_id"])) listed = post(base + "/v3/cluster/member/list") got = sorted((str(m["ID"]), m["name"]) for m in listed["members"]) want = sorted((str(m["id"]), m["name"]) for m in cap["members"]) if got != want: problems.append("members differ: live %s != captured %s" % (got, want)) version = get(base + "/version") if version.get("etcdserver") != cap["etcd_version"]: problems.append("etcd server version %r != captured %r" % (version.get("etcdserver"), cap["etcd_version"])) if get(base + "/health").get("health") != "true": problems.append("leader /health is not true") for p in problems: print(" -", p) print("VERDICT:", "PASS" if not problems else "FAIL") return 0 if not problems else 1 if __name__ == "__main__": sys.exit(main(sys.argv))