package approval import ( "encoding/json" "errors" "fmt" "strings" "testing" "time" "git.novox.be/novox/mesh-sdk/go/operatorask" "git.novox.be/novox/mesh-sdk/go/seatdef" "git.novox.be/novox/mesh-sdk/go/seatlist" ) // call is a fixture call of the marked tool wipe. func call(name, args string) Call { return Call{Address: "shell." + name, Module: "shell", Name: name, Fingerprint: "src2:1", Caller: "agent", Args: json.RawMessage(args), Description: "Wipes a scratch directory.\nMore words."} } // Design 46 7-approval-mark §8, §10, §11: the act's fields give each call its digest, and the ask shows a call's arguments // in their shape and its headline cut (the gate's other cases run on the tool runner's fixture mesh, node-tools // internal/approval). func TestTheActAndItsAsk(t *testing.T) { base := call("wipe", `{"dir":"scratch","n":1}`) digest := func(c Call) string { act, err := ActOf(c, "n1") if err != nil { t.Fatal(err) } d, _ := operatorask.ActDigest(act) return d } act, _ := ActOf(base, "n1") want := operatorask.Act{"address": "shell.wipe", "node": "n1", "module": "shell", "tool": "wipe", "build": "src2:1", "arg.dir": "scratch", "arg.n": "1"} if len(act) != len(want) { t.Fatalf("act %v", act) } for k, v := range want { if act[k] != v { t.Errorf("act[%s] = %q, want %q", k, act[k], v) } } seen := map[string]string{digest(base): "the call"} for name, c := range map[string]Call{ "an omitted argument": call("wipe", `{"dir":"scratch"}`), "another number": call("wipe", `{"dir":"scratch","n":2}`), "an object": call("wipe", `{"dir":{"name":"scratch"},"n":1}`), "a changed build": func() Call { c := base; c.Fingerprint = "src2:2"; return c }(), "another node's seat": {Address: "node-login-shell.execute", Module: "shell", Seat: "node-login-shell", Name: "execute", Fingerprint: "src2:1"}, } { d := digest(c) if other, dup := seen[d]; dup { t.Errorf("%s gives the digest of %s", name, other) } seen[d] = name } if got, _ := Canonical(json.RawMessage(`{"b":[1.0,2.5e-7,1e21],"a":"é<"}`)); got != `{"a":"é<","b":[1,2.5e-7,1e+21]}` { t.Errorf("canonical form %s", got) } // §11: a path, an address and a long argument are shown in their shape and cut. text, whole := shown(json.RawMessage(`{"dir":"/home/someone/scratch","host":"10.0.0.7","ok":"yes"}`)) if whole || text != "dir: ‹path›\nhost: ‹address›\nok: yes" { t.Errorf("shown %q whole %v", text, whole) } long, _ := json.Marshal(map[string]string{"text": strings.Repeat("word ", 500)}) if text, whole := shown(long); whole || !strings.HasSuffix(text, "NOT SHOWN IN FULL") || len(text) > PartBytes+len("\nNOT SHOWN IN FULL") { t.Errorf("a long argument shown whole %v, %d bytes", whole, len(text)) } // §10: the headline, cut with … to 60 characters, ending Part shown. for _, tc := range []struct{ name, node, want string }{ {"wipe", "n1", "Run wipe on n1?"}, {strings.Repeat("x", 50), "n1", "Run " + strings.Repeat("x", 50) + "?"}, {strings.Repeat("y", 80), "n1", "Run " + strings.Repeat("y", 54) + "…?"}, } { if got := headlineOf("Run", tc.name, tc.node, true); got != tc.want || len([]rune(got)) > 60 { t.Errorf("headline %q, want %q", got, tc.want) } } if got := headlineOf("Run", "wipe", "n1", false); got != "Run wipe on n1? Part shown" { t.Errorf("headline %q", got) } } // unread is a catalog half whose first view is not read yet, beside the seat definitions the runner holds. type unread struct{ defs map[string]seatdef.Definition } func (unread) Ready() error { return errors.New("the registrations are not answered") } func (unread) Revision() string { return "" } func (unread) Served(string) (Entry, bool, string) { return Entry{}, false, "not read" } func (unread) Newest(string) (Entry, bool) { return Entry{}, false } func (u unread) Definitions() map[string]seatdef.Definition { return u.defs } func (unread) Entries() []Entry { return nil } // Design 47 G lead answers A-387, A-388, A-389 (and A-377): while its catalog half is unread, the gate lets through every // seat verb the definition the runner holds (here the bootstrapped list) classes `read` explicitly and does not mark // needs-approval, a module's own first-view tools, and a module tool another module's account calls, logged once per tool; // it holds every other call as before. func TestAnUnreadCatalogHalfLetsItsOwnReadsThrough(t *testing.T) { defs := map[string]seatdef.Definition{} for _, d := range seatlist.Bootstrapped() { defs[d.Seat] = d } defs["marked"] = seatdef.Definition{Seat: "marked", Verbs: []seatdef.Verb{{Name: "look", Class: seatdef.ClassRead, NeedsApproval: true}}} waited := 0 var logged []string g := New(Deps{Node: "n1", Runner: "node-tools", Catalog: unread{defs: defs}, Wait: func(time.Duration) error { waited++ return errors.New("still unread") }, Logf: func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) }}) for _, tc := range []struct { seat, name string runs bool env map[string]string caller string own bool }{ {"mesh-catalog", "registrations", true, nil, "", false}, {"mesh-catalog", "revision", true, nil, "", false}, {"mesh-catalog", "entry", true, nil, "", false}, {"mesh-delivery", "source", true, nil, "", false}, {"mesh-broker", "list", true, nil, "", false}, {"mesh-catalog", "seat-definitions", true, nil, "", false}, {"mesh-catalog", "modules", true, nil, "", false}, {"git", "pull", true, nil, "", false}, {"git", "merge-pull", false, nil, "", false}, // an act {"mesh-broker", "create", false, nil, "", false}, // an act {"mesh-delivery-checker", "try", false, nil, "", false}, // names no class, so no read (A-388) {"operator-channel", "tell", false, nil, "", false}, // names no class {"marked", "look", false, nil, "", false}, // a read marked needs-approval {"unheld", "list", false, nil, "", false}, // a seat the runner holds no definition of {"mesh-broker", "events", false, nil, "", false}, // A-377: a module's own first-view tool runs unread, an ordinary tool of the same module stays held. {"", "modules", true, map[string]string{"MESH_VIEW_CALLERS_MODULES": "grants"}, "", false}, {"", "first-things", true, map[string]string{"MESH_VIEW_CALLERS_FIRST_THINGS": "grants"}, "", false}, {"", "read_modules", false, map[string]string{"MESH_VIEW_CALLERS_MODULES": "grants"}, "", false}, {"mesh-catalog", "register", false, nil, "", false}, {"mesh-delivery", "registrations", false, nil, "", false}, {"", "registrations", false, nil, "", false}, // a module's tool of that name is no seat verb {"", "revision", false, nil, "", false}, // A-389: a module tool another module's account calls runs unmarked; called otherwise it is held. {"", "bridge_merged_message", true, nil, "n2.mesh-delivery-forge-bridge", false}, {"", "bridge_merged_message", true, nil, "n1.mesh-delivery-forge-bridge", false}, // logged once {"", "read_modules", true, nil, "n1.mesh-delivery-runner", false}, {"", "bridge_merged_message", false, nil, "n1.node-tools", false}, // the console's and its sessions' account {"", "bridge_merged_message", false, nil, "n2.node-tools", false}, // another machine's console {"", "bridge_merged_message", false, nil, "person.operator", false}, // a person {"", "bridge_merged_message", false, nil, "controller", false}, {"", "bridge_merged_message", false, nil, "unproven", false}, {"", "bridge_merged_message", false, nil, "n1.mesh-delivery-indexer", false}, // the tool's own module {"", "ledger_repair", false, nil, "n1.mesh-delivery-runner", true}, // the runner's own tool {"git", "merge-pull", false, nil, "n1.mesh-delivery-runner", false}, // a seat verb's act is no module tool } { ran := false address := tc.seat + "." + tc.name if tc.seat == "" { address = "mesh-delivery-indexer." + tc.name } caller := tc.caller if caller == "" { caller = "node-tools" } _, err := g.Run(Call{Address: address, Module: "mesh-delivery-indexer", Seat: tc.seat, Name: tc.name, Fingerprint: "fp1", Caller: caller, Env: tc.env, Own: tc.own}, func() (any, error) { ran = true; return "ok", nil }) if ran != tc.runs || (err == nil) != tc.runs { t.Errorf("%s: ran %v (%v), want ran %v", address, ran, err, tc.runs) } if !tc.runs && (err == nil || !strings.Contains(err.Error(), "are not read yet")) { t.Errorf("%s: refused with %v, want the marks not read yet", address, err) } } if waited != 20 { t.Errorf("the other calls waited for the catalog half %d times, want 20 (the start reads wait for nothing)", waited) } want := []string{ "[approval] mesh-delivery-indexer.bridge_merged_message called by n2.mesh-delivery-forge-bridge: run unmarked before the catalog half is read", "[approval] mesh-delivery-indexer.read_modules called by n1.mesh-delivery-runner: run unmarked before the catalog half is read", } if strings.Join(logged, "\n") != strings.Join(want, "\n") { t.Errorf("logged %q, want each module tool run unmarked once with its caller %q", logged, want) } }