package main // Exhibit 18. A creator's machines are their ssh public keys. Pasting one on apps (Machines, with a // passkey approval of exactly that key at account) lets that machine push as them; removing it stops // it at once. apps lists each one with its last push, and a creator's workbench key is on the same // list, first; the card shows the robot's. // // The check, on live, as the robot (a creator): make a fresh ed25519 key (rc.ssh keygen, in the zoo's // own workspace: its private half never enters this program), approve it with the robot's passkey and // add it on apps, find it on apps's machine list, clone the robot's sample zoo-broken and push its // branch e18-live with it (a branch that goes nowhere), find that push on the list, remove the key on // apps, and push again: refused. The robot's own key (the zoo's, for exhibits 8 and 30) is never // touched; a key a killed round left behind (labelled zoo-18) goes first. // // On staging, apps acts in a pretend world (account-staging's names are anyone's to register): the // robot is no creator there, so its key-add must be refused, and that is what the staging zoo checks. import "core:crypto/hash" import "core:encoding/json" import "core:fmt" import "core:strings" import "ratcloud:rc" E18_LABEL :: "zoo-18" E18_APP :: "zoo-broken" // Every 6 hours (an hour when red): a round rewrites the robot's keys on the box twice (root // reinstalls the creators' keys each time) and puts a push into zoo-broken's push log, which exhibit // 8's refusals share. A revoke that stopped working still shows the same day. E18_EVERY :: 6 * 3600 E18_RETRY :: 3600 E18_STALE :: E18_EVERY + 2 * 3600 // A request through apps's door, then root's answer (rc-spool-apps answers within a second or two). e18_do :: proc(b: ^Browser, do_: string, fields_json: string) -> json.Value { base := rc.app_url("apps") r := post(b, cat(base, "/api/do"), fmt.tprintf(`{{"do":%s,%s}}`, jq(do_), fields_json)) if r.status != 200 || jstr(r.j, "id") == "" do return parse(fmt.tprintf(`{{"result":"unsent","message":%s}}`, jq(fmt.tprintf("apps answered %d: %s", r.status, clip(r.body, 200))))) id := jstr(r.j, "id") for _ in 0 ..< 120 { rc.sleep(500) q := get(b, fmt.tprintf("%s/api/req?id=%s", base, rc.percent_encode(id))) if res := jstr(q.j, "result"); q.j != nil && res != "" && res != "waiting" do return q.j } return parse(`{"result":"waiting","message":"root did not answer within 60 s"}`) } // The robot's machines as apps lists them: [{fp, type, label, last_push, workbench, on, refs}]. e18_machines :: proc(b: ^Browser) -> json.Array { r := get(b, cat(rc.app_url("apps"), "/api/apps")) if r.status != 200 do return nil out := make(json.Array) for k in jarr(r.j, "machines") do if _, ok := k.(json.Object); ok do append(&out, k) return out } e18_find :: proc(ms: json.Array, fp: string) -> json.Value { for k in ms do if jstr(k, "fp") == fp do return k return nil } // Wait up to secs for apps's list to have (or not have, `gone`) the key, with a push if `pushed`. e18_wait :: proc(b: ^Browser, fp: string, gone := false, pushed := false, secs := 15) -> json.Array { ms: json.Array for i := 0; i < secs * 10 / 7 + 1; i += 1 { ms = e18_machines(b) k := e18_find(ms, fp) if gone && k == nil do return ms if !gone && k != nil && (!pushed || jhas(k, "last_push")) do return ms rc.sleep(700) } return ms } // `ssh-keygen -l`'s fingerprint: SHA256 of the key's blob, unpadded base64. e18_fp :: proc(pub: string) -> string { f := strings.fields(pub) blob, _ := unb64(f[1] if len(f) > 1 else "") sum: [32]u8 hash.hash_bytes_to_buffer(.SHA256, blob, sum[:]) return cat("SHA256:", strings.trim_right(b64(sum[:]), "=")) } // One new commit on zoo-broken's e18-live, pushed with the key `key`: the push's exit code and answer. e18_push :: proc(key, what: string, cloned: ^bool) -> Ran_Tool { if !cloned^ { r := creator_git({op = "clone", repo = E18_APP, branch = "main", dir = "e18", key = key}) if r.code != 0 do return {r.code, cat("clone: ", r.out)} cloned^ = true } creator_git({op = "commit", dir = "e18", allow_empty = true, message = cat("exhibit 18: ", what)}) r := creator_git({op = "push", dir = "e18", branch = "e18-live", force = true, key = key}) return {r.code, unremote(r.out)} } E18_Data :: struct { at: string `json:"at"`, fp: string `json:"fp,omitempty"`, add: json.Value `json:"add,omitempty"`, push: string `json:"push,omitempty"`, last_push: json.Value `json:"last_push,omitempty"`, rm: json.Value `json:"rm,omitempty"`, push_after: string `json:"push_after,omitempty"`, machines: json.Value `json:"machines,omitempty"`, leftover: []json.Value `json:"leftover,omitempty"`, seconds: i64 `json:"seconds,omitempty"`, } e18_staging :: proc() -> Check { b, err := robot_at("apps") if err != "" do return fail(err) k := rc.ssh({op = "keygen", name = "e18", comment = E18_LABEL}) if k.code != 0 do return fail(cat("ssh keygen failed: ", k.err, string(k.out))) r := e18_do(b, "key-add", fmt.tprintf(`"key":%s,"label":%s,"approval":""`, jq(strings.trim_space(string(k.out))), jq(E18_LABEL))) ms := e18_machines(b) data := E18_Data{at = iso(now()), add = r, machines = ms} if jstr(r, "result") != "refused" do return fail(fmt.tprintf("staging's apps %s a machine for the robot, who is no creator in its pretend world: %s", jstr(r, "result") or_else "?", jstr(r, "message")), data) if len(ms) > 0 do return fail("staging's apps lists machines for the robot", data) return pass(fmt.tprintf("staging's apps acts in a pretend world: the robot is no creator there, and its key was refused (%s); the live zoo adds and removes one", jstr(r, "message")), data) } e18_round :: proc() -> Check { b, err := robot_at("apps") if err != "" do return fail(err) t0 := now() data := E18_Data{at = iso(t0)} failing :: proc(b: ^Browser, data: ^E18_Data, why: string) -> Check { data.machines = e18_machines(b) return fail(why, data^) } // A key an earlier round left behind goes first (labelled zoo-18; never the zoo's own keys). left := make([dynamic]json.Value) for k in e18_machines(b) do if jstr(k, "label") == E18_LABEL do append(&left, e18_do(b, "key-rm", fmt.tprintf(`"fp":%s`, jq(jstr(k, "fp"))))) data.leftover = left[:] creator_git({op = "rm", dir = "e18"}) defer creator_git({op = "rm", dir = "e18"}) k := rc.ssh({op = "keygen", name = "e18", comment = E18_LABEL}) if k.code != 0 do return failing(b, &data, cat("ssh keygen failed: ", k.err, string(k.out))) pub := strings.trim_space(string(k.out)) fp := e18_fp(pub) data.fp = fp added := false // Never leave the key behind on a red round. defer if added do e18_do(b, "key-rm", fmt.tprintf(`"fp":%s`, jq(fp))) cloned := false // 1. Paste it on apps, with the passkey approval of exactly this key. approval, aerr := robot_approve("apps", "key-add", fmt.tprintf(`{{"fp":%s,"label":%s}}`, jq(fp), jq(E18_LABEL))) if aerr != "" do return failing(b, &data, aerr) r := e18_do(b, "key-add", fmt.tprintf(`"key":%s,"label":%s,"approval":%s`, jq(pub), jq(E18_LABEL), jq(approval))) data.add = r if jstr(r, "result") != "done" do return failing(b, &data, fmt.tprintf("apps did not add the machine: %s, %s", jstr(r, "result") or_else "?", jstr(r, "message"))) added = true if e18_find(e18_wait(b, fp), fp) == nil do return failing(b, &data, "the added machine is not on apps's machine list") // 2. It pushes, and the list shows that push. p := e18_push("e18", "a push by a machine added on apps a moment ago", &cloned) data.push = clip(p.out, 400) if p.code != 0 do return failing(b, &data, cat("the added machine's push was refused: ", clip(p.out, 200))) kk := e18_find(e18_wait(b, fp, pushed = true), fp) data.last_push = jget(kk, "last_push") if !jhas(kk, "last_push") do return failing(b, &data, "apps's machine list does not show the added machine's push") // 3. Remove it on apps; its next push is refused. r = e18_do(b, "key-rm", fmt.tprintf(`"fp":%s`, jq(fp))) data.rm = r if jstr(r, "result") != "done" do return failing(b, &data, fmt.tprintf("apps did not remove the machine: %s, %s", jstr(r, "result") or_else "?", jstr(r, "message"))) added = false if e18_find(e18_wait(b, fp, gone = true), fp) != nil do return failing(b, &data, "the removed machine is still on apps's machine list") p = e18_push("e18", "a push by a machine removed on apps a moment ago", &cloned) data.push_after = clip(p.out, 400) if p.code == 0 do return failing(b, &data, "a removed machine's push was ACCEPTED") data.machines = e18_machines(b) data.seconds = now() - t0 return pass(fmt.tprintf("a key pasted on apps (approved with the robot's passkey) pushed %s's e18-live and the list showed it; removed on apps, its next push was refused: %s (%d s)", E18_APP, first_line(p.out), data.seconds), data) } e18 :: proc() -> Exhibit { return { n = 18, wing = "Building", title = "Add a laptop by pasting its key", promise = "Pasting a machine's public key on apps lets that machine push; removing it stops it.", block = "apps's Machines: paste a public key (~/.ssh/id_ed25519.pub) and a name, confirm it with your passkey, and that machine pushes as you; Remove stops it at once. Each machine shows its last push.", src = {"e18_add_a_laptop.odin", #load("e18_add_a_laptop.odin", string)}, files = srcs({"robot.odin", #load("robot.odin", string)}, {"creator.odin", #load("creator.odin", string)}), show = proc(me: ^rc.User) -> string { apps := rc.app_url("apps") yours := fmt.tprintf(`
Your machines are on apps (creators): paste a public key, confirm it with your passkey, and each one shows its last push. Your workbench's own key is on the same list, first, once you have opened the workbench.
`, h(apps)) d, s, found := stored_data(18, E18_Data) if env().env != "live" do return cat(`This is staging: its apps acts in a pretend world, where the robot is no creator, so its key is refused`, cat(" (", h(jstr(d.add, "message")), ")") if found else "", `. The live zoo adds and removes one.
`, yours) if !found || d.fp == "" do return cat(`Every six hours the robot, a creator, adds a machine on apps, pushes with it, removes it, and pushes again. It has not run yet.
`, yours) b := strings.builder_make() li :: proc(b: ^strings.Builder, tag: string, ok: bool, html: string) { fmt.sbprintf(b, `The robot, a creator, {{1}}:
%s (%s), pasted on apps and approved with its passkey: %s", E18_LABEL, h(d.fp), h(jstr(d.add, "message"))))
lp := d.last_push
li(&b, "pushed", jhas(lp), cat("with it, e18-live of ", E18_APP, fmt.tprintf("; apps's list showed the push (%s %s %s)", h(jstr(lp, "app")), h(jstr(lp, "env")), h(jstr(lp, "commit"))) if jhas(lp) else ""))
li(&b, "removed", jstr(d.rm, "result") == "done", cat("on apps: ", h(jstr(d.rm, "message"))))
li(&b, "refused", s.ok && d.push_after != "", cat("its next push: ", h(first_line(d.push_after if d.push_after != "" else "not tried"))))
strings.write_string(&b, "The robot's machines on apps now: {{1}}.
`, {{"1", strings.join(names[:], ", ")}}) } if !s.ok do rc.render_to(&b, `{{1}}
`, {{"1", s.detail}}) return cat(strings.to_string(b), yours) }, check = proc() -> Check { d, s, found := stored_data(18, E18_Data) last := parse_time(d.at) due := last == 0 || now() - last >= (E18_EVERY if s.ok else E18_RETRY) // Rounds run in the five-minute job; a "check now" reads the last one. if due && g_in_job do return e18_round() if env().env == "live" else e18_staging() if last == 0 do return fail("no round yet: the five-minute job starts one") if now() - last > E18_STALE do return fail(fmt.tprintf("the last round is %.1f hours old: the rounds have stopped", f64(now() - last) / 3600), d) // Between rounds, the robot's machine list as apps shows it now (its workbench's key appears there). if env().env == "live" && g_in_job { if b, err := robot_at("apps"); err == "" do d.machines = e18_machines(b) } _ = found return {s.ok, s.detail, to_json(d)} }, } }