From 94a2e6ede09bc8d0317e959aa28db184fa6b581b Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 22:10:50 +0100 Subject: [PATCH] H-A-335: a held module's share of a holder's words is what runs The reads runtime on ace restarted twice in two minutes because its MESH_SECRET_PLACES changed: the holders of node-reads and node-files were told the node's secret places and owned paths from held modules' stubs, which differ pass by pass while a part is withheld. While a module is held, the resolution now takes its share from each runtime process as it runs: the secret places are those the modules not held compose merged with every place that runs, and the owned paths keep each running entry a held module owns. Two passes with the same held set compose every runtime process the same; a module that answered and changed is still composed from its build. Builds: design 47 H H-A-335, H-A-337, H-A-306 (2), (3) --- internal/compose/composed.go | 33 +++++ internal/compose/composed_test.go | 126 ++++++++++++++++++++ internal/compose/frombus/frombus.go | 2 + internal/compose/input.go | 5 + internal/compose/node.go | 16 +-- internal/compose/resolution/declaration.go | 14 ++- internal/compose/resolution/files.go | 28 ++++- internal/compose/resolution/files_test.go | 8 +- internal/compose/resolution/holding.go | 59 +++++++++ internal/compose/resolution/reads.go | 20 +++- internal/compose/resolution/reads_test.go | 6 +- internal/compose/resolution/runtime.go | 8 +- internal/compose/resolution/runtime_test.go | 6 +- 13 files changed, 299 insertions(+), 32 deletions(-) create mode 100644 internal/compose/resolution/holding.go diff --git a/internal/compose/composed.go b/internal/compose/composed.go index f8f6d567..d3eb61af 100644 --- a/internal/compose/composed.go +++ b/internal/compose/composed.go @@ -457,6 +457,39 @@ func RuntimeRunning(running []engcomp.Composed) map[string]string { return nil } +// RuntimesRunning is the environment of each runtime process as the node runs it, by its id: the tool runner's +// (`.runtime`) and each runtime of a module of its own account (`.own-runtime`), or nil when running +// holds none (Node.RunsRuntimes). +func RuntimesRunning(running []engcomp.Composed) map[string]map[string]string { + var out map[string]map[string]string + for _, c := range running { + for _, raw := range c.Resources { + var r struct { + ID string `json:"id"` + Module string `json:"module"` + Type string `json:"type"` + Env map[string]string `json:"env"` + } + if json.Unmarshal(raw, &r) != nil || r.Env == nil { + continue + } + owner := r.Module + if owner == "" { + owner, _ = declaration.ModuleGuess(r.ID) + } + if runtimeOwner(r.ID, r.Module, r.Type) == "" && (r.Type != "process" || owner == "" || + r.ID != owner+"."+catalogue.OwnRuntimeID()) { + continue + } + if out == nil { + out = map[string]map[string]string{} + } + out[r.ID] = r.Env + } + } + return out +} + // runtimeOwner is the module of the tool runner's process a resource is (`.runtime`), or "" when it is none. func runtimeOwner(id, module, kind string) string { if kind != "process" { diff --git a/internal/compose/composed_test.go b/internal/compose/composed_test.go index d28fb74d..b28bade1 100644 --- a/internal/compose/composed_test.go +++ b/internal/compose/composed_test.go @@ -22,6 +22,7 @@ func fpFor(c byte) string { return meshcatalogclient.SourcePrefix2 + strings.Rep // it; a module given no fingerprint is named as skipped and composed nowhere. func TestCompositionsAreTheDeclarationSplitByModule(t *testing.T) { t.Run("a held tool runner keeps its program for the modules of their own account", aHeldToolRunnerKeepsItsProgramForTheModulesOfTheirOwnAccount) + t.Run("a held module's share of the holders' words is what runs", aHeldModulesShareOfTheHoldersWordsIsWhatRuns) in := aSmallMesh(t) d, _, err := Compose(in, "laptop") if err != nil { @@ -292,3 +293,128 @@ func aHeldToolRunnerKeepsItsProgramForTheModulesOfTheirOwnAccount(t *testing.T) t.Errorf("mesh-self-check is not run by the program node-tools runs: %v", out) } } + +// H-A-335, H-A-337, H-A-306: the holder of node-reads (a runtime of its own) and the holder of node-files (in the tool +// runner's) are told the node's secret places and owned paths, to which every module gives a share. While a module is +// held its share is what runs, never its stub's, so two passes with the same held set compose both runtimes the same +// whatever its stub says (ace, 2026-11-21 21:54: the reads runtime restarted twice in two minutes for MESH_SECRET_PLACES); +// a module that answered and changed is composed from its build. +func aHeldModulesShareOfTheHoldersWordsIsWhatRuns(t *testing.T) { + dg := func(c byte) string { return "sha256:" + strings.Repeat(string(c), 64) } + runtime := catalogue.Manifest{Module: "node-tools", Version: "1", ToolRunner: true, + OwnSecrets: catalogue.OwnSecrets{"broker": {Path: "/var/lib/mesh/node-tools/broker"}}, + Bundles: []catalogue.Bundle{{Name: "runtime", Source: catalogue.ArtifactStoreScheme + "node-tools/runtime/blobs/" + dg('a'), + Digest: dg('a'), Language: "go", Binary: "node-tools"}}} + tools := func(module string) []catalogue.Bundle { + return []catalogue.Bundle{{Name: "tools", Source: catalogue.ArtifactStoreScheme + module + "/tools/blobs/" + dg('d'), + Digest: dg('d'), Language: "go", Loads: []string{module + "-tools"}}} + } + reads := catalogue.Manifest{Module: "mesh-reads", Version: "1", RunsAs: "mesh-reads", SecretsOwner: "mesh-reads", + Claims: []catalogue.Claim{{Name: catalogue.ReadsSeat}}, + OwnSecrets: catalogue.OwnSecrets{"broker": {Path: "/var/lib/mesh-reads/broker", Taken: catalogue.TakenAtStart}}, + Bundles: tools("mesh-reads")} + files := catalogue.Manifest{Module: "files", Version: "1", Claims: []catalogue.Claim{{Name: catalogue.FilesSeat}}, + Bundles: tools("files")} + store := catalogue.Manifest{Module: "artifacts", Version: "1", + Claims: []catalogue.Claim{{Name: "container-image-registry", Scope: catalogue.ScopeMesh}}, + Listens: []catalogue.Listening{{Name: "registry", Port: 5100, From: catalogue.FromMesh}}} + // lamp as it ran (its secret under /var/lib/lamp/old, its state at /var/lib/lamp-old), and its new build. + lampAt := func(secret, state string) catalogue.Manifest { + return catalogue.Manifest{Module: "lamp", Version: "1", + OwnSecrets: catalogue.OwnSecrets{"token": {Path: secret}}, + Resources: []map[string]any{{"id": "state", "type": "directory", "path": state, "mode": "0700"}}} + } + oldLamp, newLamp := lampAt("/var/lib/lamp/old/token", "/var/lib/lamp-old"), lampAt("/var/lib/lamp/new/token", "/var/lib/lamp-new") + ranPlaces := `["/var/lib/lamp/old","/var/lib/mesh-reads","/var/lib/mesh/node-tools"]` + ranOwned := `{"/var/lib/lamp-old":"lamp (its own tools)"}` + process := func(id, module string, words map[string]map[string]string) json.RawMessage { + body, _ := json.Marshal(words) + env, _ := json.Marshal(map[string]string{catalogue.RuntimeToolEnv: string(body)}) + return json.RawMessage(`{"id":"` + id + `","module":"` + module + `","type":"process","env":` + string(env) + `}`) + } + ran := []engcomp.Composed{ + {Node: "anchor", Module: "node-tools", Resources: []json.RawMessage{process("node-tools.runtime", "node-tools", + map[string]map[string]string{"files": {catalogue.ReadsSecretPlaces: ranPlaces, catalogue.FilesOwnedPaths: ranOwned}})}}, + {Node: "anchor", Module: "mesh-reads", Resources: []json.RawMessage{process("mesh-reads."+catalogue.OwnRuntimeID(), "mesh-reads", + map[string]map[string]string{"mesh-reads": {catalogue.ReadsSecretPlaces: ranPlaces}})}}, + } + for _, row := range []struct { + name string + held bool + // The stub lamp is held as at each of two passes: what a withheld part gives it differs pass by pass. + stubs []catalogue.Manifest + // What lamp gives of the places and owned paths, and what it no longer gives. + wantPlace, gonePlace, wantOwned, goneOwned string + }{ + {"held, its stub differing pass by pass", true, []catalogue.Manifest{Stub(newLamp), Stub(catalogue.Manifest{Module: "lamp", Version: "1"})}, + "/var/lib/lamp/old", "/var/lib/lamp/new", "/var/lib/lamp-old", "/var/lib/lamp-new"}, + {"answered, its build changed", false, []catalogue.Manifest{newLamp, newLamp}, + "/var/lib/lamp/new", "/var/lib/lamp/old", "/var/lib/lamp-new", "/var/lib/lamp-old"}, + } { + in := aMesh(t) + in.Catalogue["node-tools"], in.Catalogue["artifacts"], in.Catalogue["mesh-reads"] = runtime, store, reads + in.Catalogue["files"], in.Catalogue["lamp"] = files, oldLamp + anchor := nodeOf(t, &in, "anchor") + anchor.Assigned = append(anchor.Assigned, "artifacts", "node-tools", "mesh-reads", "files", "lamp") + in.BusMinted = []string{"anchor.node-tools", "anchor.mesh-reads"} + anchor.SealingKey = keyOf(t, "anchor") + if row.held { + anchor.Held = map[string]bool{"lamp": true} + } + anchor.RunsRuntime, anchor.RunsRuntimes = RuntimeRunning(ran), RuntimesRunning(ran) + fps := map[string]string{overlay.Name: fpFor('a'), "artifacts": fpFor('5'), "node-tools": fpFor('e'), + "mesh-reads": fpFor('6'), "files": fpFor('7'), "lamp": fpFor('8')} + digests := map[string][]string{} + said := map[string]string{} + for _, stub := range row.stubs { + anchor.Kept = map[string]catalogue.Manifest{"lamp": stub} + out, _, _, err := CompositionsIn(in, "anchor", fps, Foreseeing) + if err != nil { + t.Fatalf("%s: %v", row.name, err) + } + for _, c := range out { + for _, raw := range c.Resources { + var r struct { + ID string `json:"id"` + Env map[string]string `json:"env"` + } + if json.Unmarshal(raw, &r) != nil { + continue + } + holder := map[string]string{"node-tools.runtime": "files", "mesh-reads." + catalogue.OwnRuntimeID(): "mesh-reads"}[r.ID] + if holder == "" { + continue + } + dig, err := engcomp.DigestOf(c) + if err != nil { + t.Fatal(err) + } + digests[r.ID] = append(digests[r.ID], dig) + var words map[string]map[string]string + _ = json.Unmarshal([]byte(r.Env[catalogue.RuntimeToolEnv]), &words) + said[r.ID+" places"] = words[holder][catalogue.ReadsSecretPlaces] + if holder == "files" { + said[r.ID+" owned"] = words[holder][catalogue.FilesOwnedPaths] + } + } + } + } + for _, id := range []string{"node-tools.runtime", "mesh-reads." + catalogue.OwnRuntimeID()} { + if d := digests[id]; len(d) != 2 || d[0] != d[1] { + t.Errorf("%s: two passes composed %s at %v", row.name, id, d) + } + } + for what, words := range said { + want, gone := row.wantPlace, row.gonePlace + if strings.HasSuffix(what, " owned") { + want, gone = row.wantOwned, row.goneOwned + } + if !strings.Contains(words, `"`+want+`"`) || strings.Contains(words, `"`+gone+`"`) { + t.Errorf("%s: %s are %s, want %s among them and not %s", row.name, what, words, want, gone) + } + } + if len(said) != 3 { + t.Errorf("%s: the holders are told %v", row.name, said) + } + } +} diff --git a/internal/compose/frombus/frombus.go b/internal/compose/frombus/frombus.go index 316fcad2..2d00c72b 100644 --- a/internal/compose/frombus/frombus.go +++ b/internal/compose/frombus/frombus.go @@ -477,6 +477,8 @@ func readBase(ctx context.Context, r Reader, mesh Mesh, s Scope) (base, error) { n.RunsTools = compose.ToolsRunning(s.Running) // And the runtime's process as it runs, whose slice of each held module is kept verbatim (H-A-306). n.RunsRuntime = compose.RuntimeRunning(s.Running) + // And every runtime process as it runs, whose words give each held module's share of a holder's (H-A-335). + n.RunsRuntimes = compose.RuntimesRunning(s.Running) for _, module := range s.InOwn { if n.InOwn == nil { n.InOwn = map[string]bool{} diff --git a/internal/compose/input.go b/internal/compose/input.go index d185e399..dae6f2c9 100644 --- a/internal/compose/input.go +++ b/internal/compose/input.go @@ -278,6 +278,11 @@ type Node struct { // in catalogue.RuntimeToolEnv, its fingerprint among them) is copied from it verbatim, so a pass that holds a module // for a part withheld composes the runtime as it runs (design 47 H, H-A-303, H-A-306). RunsRuntime map[string]string + // RunsRuntimes is the environment of each runtime process as it runs, by its id (RuntimesRunning of what the node + // runs): the tool runner's (`.runtime`) and each runtime of a module of its own account + // (`.own-runtime`); nil when none runs. A held module's share of a holder's words that every module gives a + // share of (the secret places, the owned paths) is taken from them (resolution.Holding; design 47 H, H-A-335, H-A-337). + RunsRuntimes map[string]map[string]string } // ComposedHow is how a node-engine composes its node from an Input (Input.ComposedHow). diff --git a/internal/compose/node.go b/internal/compose/node.go index 30f05154..43c7c8e8 100644 --- a/internal/compose/node.go +++ b/internal/compose/node.go @@ -176,10 +176,10 @@ func ComposeIn(in Input, node string, mode Mode) (Declared, []string, error) { // none (resolution.NoStoreError, whatever the shape: an image, a source, a tools bundle, a runtime program, a templates // archive) is left out of the declaration with its words and the rest composes again; the node is never refused whole for it // (design 47 G A-461, A-452). The rendering returned carries the modules so left out, for the filter and the rest to read. -func composeWithoutStoreFetchers(plan catalogue.Resolution, with catalogue.Rendering) (catalogue.Composed, catalogue.Rendering, map[string]string, error) { +func composeWithoutStoreFetchers(plan catalogue.Resolution, with catalogue.Rendering, holding resolution.Holding) (catalogue.Composed, catalogue.Rendering, map[string]string, error) { var held map[string]string for { - composed, err := resolution.Compose(plan, with) + composed, err := resolution.ComposeHolding(plan, with, holding) var none *resolution.NoStoreError if !errors.As(err, &none) { return composed, with, held, err @@ -217,7 +217,13 @@ func (c *composer) declaration(nodeName string, mode Mode) (Declared, error) { with := rendered.Rendering with.Generators = gens plan := resolved.Plan - composed, with, storeHeld, err := composeWithoutStoreFetchers(plan, with) + node, err := c.in.node(nodeName) + if err != nil { + return Declared{}, err + } + // A held module's share of a holder's words is what runs of each runtime process (design 47 H, H-A-335, H-A-337). + holding := resolution.Holding{Held: node.Held, Runs: node.RunsRuntimes} + composed, with, storeHeld, err := composeWithoutStoreFetchers(plan, with, holding) if err != nil { return Declared{}, err } @@ -227,10 +233,6 @@ func (c *composer) declaration(nodeName string, mode Mode) (Declared, error) { if err != nil { return Declared{}, err } - node, err := c.in.node(nodeName) - if err != nil { - return Declared{}, err - } // A module that fetches from a store no node reaches is held with its words, as a module whose claim cannot be held is: it // stays on the build it runs, its composition kept (A-452, A-461). held := resolved.Held diff --git a/internal/compose/resolution/declaration.go b/internal/compose/resolution/declaration.go index faa2202d..3647fcbc 100644 --- a/internal/compose/resolution/declaration.go +++ b/internal/compose/resolution/declaration.go @@ -105,12 +105,18 @@ func (e *UnreadManifestError) Error() string { return e.said } // Compose is Declaration with the owner of every resource said. func Compose(r mcat.Resolution, with mcat.Rendering) (mcat.Composed, error) { + return ComposeHolding(r, with, Holding{}) +} + +// ComposeHolding is Compose for a node that holds modules this pass (Holding): a held module's share of a holder's words +// is what runs (design 47 H, H-A-335, H-A-337). +func ComposeHolding(r mcat.Resolution, with mcat.Rendering, h Holding) (mcat.Composed, error) { owner := map[string]string{} received := map[string]map[string][]mcat.Contribution{} leftOut := map[string]string{} var foreseen, unplaced, handedOnly []string handed := &handedFiles{plans: map[string]mcat.Plan{}} - resources, err := resolutionCompose(r, with, owner, received, leftOut, &foreseen, &unplaced, &handedOnly, handed) + resources, err := resolutionCompose(r, with, h, owner, received, leftOut, &foreseen, &unplaced, &handedOnly, handed) if err != nil { return mcat.Composed{}, err } @@ -137,7 +143,7 @@ func Compose(r mcat.Resolution, with mcat.Rendering) (mcat.Composed, error) { // BusMembershipPath is where the host reads it — the same constant on both sides. func BusMembershipID() string { return "bus-membership" } -func resolutionCompose(r mcat.Resolution, with mcat.Rendering, owner map[string]string, +func resolutionCompose(r mcat.Resolution, with mcat.Rendering, h Holding, owner map[string]string, received map[string]map[string][]mcat.Contribution, leftOut map[string]string, foreseen, unplaced, handedOnly *[]string, handed *handedFiles) ([]map[string]any, error) { // **A setting is judged where it is stored, and an impossible one costs a module, not a @@ -1065,7 +1071,7 @@ func resolutionCompose(r mcat.Resolution, with mcat.Rendering, owner map[string] // bundle delivered above and holding the credential sealed above, so both exist before it starts // — the order written here is the order the machine applies. if resolutionRuntimeHere(r) { - process, err := resolutionRuntimeProcess(r, with) + process, err := resolutionRuntimeProcess(r, with, h) if err != nil { return nil, err } @@ -1074,7 +1080,7 @@ func resolutionCompose(r mcat.Resolution, with mcat.Rendering, owner map[string] out = append(out, process) } // And a runtime of its own for each module of its own account, after it (novox/hq ADR 0259 §8). - owns, err := resolutionOwnRuntimes(r, with) + owns, err := resolutionOwnRuntimes(r, with, h) if err != nil { return nil, err } diff --git a/internal/compose/resolution/files.go b/internal/compose/resolution/files.go index 0b7d706a..835ede27 100644 --- a/internal/compose/resolution/files.go +++ b/internal/compose/resolution/files.go @@ -52,12 +52,14 @@ func resolutionStepsBackHere(r mcat.Resolution, m mcat.Manifest, sp mcat.SeatPat } // filesWords is what the holder of node-files is given beside its own words: the node's secret places and the -// paths modules own here. -func resolutionFilesWords(r mcat.Resolution, m mcat.Manifest, words map[string]string, with mcat.Rendering) (map[string]string, error) { +// paths modules own here. A held module's share of each is what runs of the process (the id process) that serves m +// (Holding, resolutionSecretPlaces, heldOwnedPaths). +func resolutionFilesWords(r mcat.Resolution, m mcat.Manifest, words map[string]string, with mcat.Rendering, h Holding, process string) (map[string]string, error) { if !m.ClaimsSeat(mcat.FilesSeat) { return words, nil } - places, err := resolutionSecretPlaces(r, with) + ran := h.ran(process, m.Module) + places, err := resolutionSecretPlaces(r, with, h.Held, ran) if err != nil { return nil, err } @@ -65,7 +67,7 @@ func resolutionFilesWords(r mcat.Resolution, m mcat.Manifest, words map[string]s if err != nil { return nil, err } - owned, err := json.Marshal(resolutionOwnedPaths(r)) + owned, err := json.Marshal(heldOwnedPaths(r, h.Held, ran)) if err != nil { return nil, err } @@ -82,3 +84,21 @@ func resolutionFilesWords(r mcat.Resolution, m mcat.Manifest, words map[string]s words[mcat.FilesOwnedPaths] = string(owned) return words, nil } + +// heldOwnedPaths is ownedPaths with a held module's entries as they run (design 47 H, H-A-335, H-A-337): while any module +// is held and the holder's words as it runs (ran) say the owned paths, the entries the modules not held compose, and each +// running entry whose owner is a held module where those name no owner of its path (a module that answered applies, +// H-A-306 (3)). Else ownedPaths. +func heldOwnedPaths(r mcat.Resolution, held map[string]bool, ran map[string]string) map[string]string { + var running map[string]string + if body, says := ran[mcat.FilesOwnedPaths]; !says || json.Unmarshal([]byte(body), &running) != nil { + return resolutionOwnedPaths(r) + } + owned := resolutionOwnedPaths(notHeld(r, held)) + for p, entry := range running { + if _, composed := owned[p]; !composed && held[ownerOf(entry)] { + owned[p] = entry + } + } + return owned +} diff --git a/internal/compose/resolution/files_test.go b/internal/compose/resolution/files_test.go index 625f40d5..6f965d13 100644 --- a/internal/compose/resolution/files_test.go +++ b/internal/compose/resolution/files_test.go @@ -18,7 +18,7 @@ func TestTheHolderOfNodeFilesIsToldSecretPlacesAndOwnedPaths(t *testing.T) { app := mcat.Manifest{Module: "app", Version: "1", Resources: []map[string]any{{"id": "conf", "type": "file", "path": "/etc/app/app.conf"}}} r := mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{files, host, app}} - words, err := resolutionFilesWords(r, files, nil, mcat.Rendering{}) + words, err := resolutionFilesWords(r, files, nil, mcat.Rendering{}, Holding{}, "") if err != nil { t.Fatal(err) } @@ -51,7 +51,7 @@ func TestTheHolderOfNodeFilesIsToldSecretPlacesAndOwnedPaths(t *testing.T) { mcat.UseSeats(reversed) } both := mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{files, uplink, resolver}} - w, err := resolutionFilesWords(both, files, nil, mcat.Rendering{}) + w, err := resolutionFilesWords(both, files, nil, mcat.Rendering{}, Holding{}, "") if err != nil { t.Fatal(err) } @@ -65,12 +65,12 @@ func TestTheHolderOfNodeFilesIsToldSecretPlacesAndOwnedPaths(t *testing.T) { // the controller tells them, and not the paths its running resources are at (a directory the mesh placed). carried := mcat.Manifest{Module: "de-spiegel", Version: "1", OwnsPaths: []string{"/var/lib/de-spiegel/server.env"}, Resources: []map[string]any{{"id": "state", "type": "directory", "path": "/var/lib/de-spiegel"}}} - w, err := resolutionFilesWords(mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{files, carried}}, files, nil, mcat.Rendering{}) + w, err := resolutionFilesWords(mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{files, carried}}, files, nil, mcat.Rendering{}, Holding{}, "") if err != nil || !strings.Contains(w[mcat.FilesOwnedPaths], `"/var/lib/de-spiegel/server.env":"de-spiegel (its own tools)"`) || strings.Contains(w[mcat.FilesOwnedPaths], `"/var/lib/de-spiegel":`) { t.Errorf("a carried module does not own its manifest's paths alone: %v %v", w[mcat.FilesOwnedPaths], err) } - if w, _ := resolutionFilesWords(r, app, nil, mcat.Rendering{}); w[mcat.FilesOwnedPaths] != "" { + if w, _ := resolutionFilesWords(r, app, nil, mcat.Rendering{}, Holding{}, ""); w[mcat.FilesOwnedPaths] != "" { t.Error("a module not holding node-files is told the owned paths") } } diff --git a/internal/compose/resolution/holding.go b/internal/compose/resolution/holding.go new file mode 100644 index 00000000..4a10ac4c --- /dev/null +++ b/internal/compose/resolution/holding.go @@ -0,0 +1,59 @@ +package resolution + +import ( + "encoding/json" + "strings" + + mcat "git.novox.be/novox/mesh-sdk/go/catalogue" +) + +// Holding is what a node holds while it composes itself (design 47 H, H-A-306, H-A-335, H-A-337): the modules held this +// pass, each composed as its stub, and each runtime process as it runs. A holder's words that every module gives a share +// of (the secret places, the owned paths) take a held module's share from what runs, never from its stub, so two passes +// with the same held set compose every runtime process the same. The zero Holding holds nothing: everything is composed +// from the manifests, as before. +type Holding struct { + // Held is each module held on this node this pass (compose.Node.Held). + Held map[string]bool + // Runs is the environment of each runtime process as it runs, by its id: the tool runner's (`.runtime`) and + // each runtime of a module of its own account (`.own-runtime`). + Runs map[string]map[string]string +} + +// ran is module's words in process's mcat.RuntimeToolEnv as it runs, or nil when nothing is held, the process does not +// run, or it gives module no words. +func (h Holding) ran(process, module string) map[string]string { + if len(h.Held) == 0 { + return nil + } + env, runs := h.Runs[process] + if !runs { + return nil + } + var words map[string]map[string]string + if json.Unmarshal([]byte(env[mcat.RuntimeToolEnv]), &words) != nil { + return nil + } + return words[module] +} + +// notHeld is r with only the modules not in held, or r itself when none is held. +func notHeld(r mcat.Resolution, held map[string]bool) mcat.Resolution { + if len(held) == 0 { + return r + } + modules := make([]mcat.Manifest, 0, len(r.Modules)) + for _, m := range r.Modules { + if !held[m.Module] { + modules = append(modules, m) + } + } + r.Modules = modules + return r +} + +// ownerOf is the module an owned path's entry names (resolutionOwnedPaths: ` ()`). +func ownerOf(entry string) string { + module, _, _ := strings.Cut(entry, " (") + return module +} diff --git a/internal/compose/resolution/reads.go b/internal/compose/resolution/reads.go index 66be41ce..13023f60 100644 --- a/internal/compose/resolution/reads.go +++ b/internal/compose/resolution/reads.go @@ -19,7 +19,13 @@ import ( // // **Failure is loud.** A place still holding a placeholder, or not absolute, fails the composition with the // module named: a list that silently shrank would be a refusal the decision promises and nothing does. -func resolutionSecretPlaces(r mcat.Resolution, with mcat.Rendering) ([]string, error) { +// +// **A held module's share is what runs** (design 47 H, H-A-335, H-A-337): the list says no module per place, so while +// any module is held and the holder's words as it runs (ran) say the places, the places are those the modules not held +// compose, merged with every place in what runs, then reduced as above. A pass with the same held set gives the same +// list, a module that answered still adds its new places, and a place a module dropped stays refused until no module +// is held: the list refuses more, never less. +func resolutionSecretPlaces(r mcat.Resolution, with mcat.Rendering, held map[string]bool, ran map[string]string) ([]string, error) { seen := map[string]bool{} var problems []string add := func(module, what, path string) { @@ -38,6 +44,13 @@ func resolutionSecretPlaces(r mcat.Resolution, with mcat.Rendering) ([]string, e } seen[path] = true } + var running []string + if body, says := ran[mcat.ReadsSecretPlaces]; says && json.Unmarshal([]byte(body), &running) == nil { + r = notHeld(r, held) + for _, p := range running { + add("what runs", "secret place", p) + } + } for _, m := range r.Modules { for name, own := range m.OwnSecrets { add(m.Module, "own secret "+name, own.Path) @@ -91,11 +104,12 @@ func providesSecret(m mcat.Manifest) bool { } // readsWords is what the holder of node-reads is given beside its own words: the node's secret places. -func resolutionReadsWords(r mcat.Resolution, m mcat.Manifest, words map[string]string, with mcat.Rendering) (map[string]string, error) { +// A held module's share of them is what runs of the process (the id process) that serves m (Holding). +func resolutionReadsWords(r mcat.Resolution, m mcat.Manifest, words map[string]string, with mcat.Rendering, h Holding, process string) (map[string]string, error) { if !m.ClaimsSeat(mcat.ReadsSeat) { return words, nil } - places, err := resolutionSecretPlaces(r, with) + places, err := resolutionSecretPlaces(r, with, h.Held, h.ran(process, m.Module)) if err != nil { return nil, err } diff --git a/internal/compose/resolution/reads_test.go b/internal/compose/resolution/reads_test.go index ced1bd18..cfe475bc 100644 --- a/internal/compose/resolution/reads_test.go +++ b/internal/compose/resolution/reads_test.go @@ -39,7 +39,7 @@ func TestTheHolderOfNodeReadsIsToldWhereItsNodeKeepsASecret(t *testing.T) { app := mcat.Manifest{Module: "app", Version: "1", Secrets: map[string]string{"postgres-database": "/etc/app/db.secret"}, OwnSecrets: mcat.OwnSecrets{"token": {Path: "/home/ops/.config/app/token"}}} r := mcat.Resolution{Node: "anchor", Account: "ops", Modules: []mcat.Manifest{reads, desk, vault, app, goRuntime}} - owns, err := resolutionOwnRuntimes(r, with) + owns, err := resolutionOwnRuntimes(r, with, Holding{}) if err != nil { t.Fatal(err) } @@ -71,11 +71,11 @@ func TestASecretPlaceThatCannotBeResolvedFailsTheComposition(t *testing.T) { r := mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{ {Module: "app", Version: "1", Keeps: "${dir:keep}/sealed"}, }} - if _, err := resolutionSecretPlaces(r, mcat.Rendering{}); err == nil || !strings.Contains(err.Error(), "app") { + if _, err := resolutionSecretPlaces(r, mcat.Rendering{}, nil, nil); err == nil || !strings.Contains(err.Error(), "app") { t.Errorf("an unresolved place was dropped: %v", err) } r.Modules[0].Keeps = "relative/place" - if _, err := resolutionSecretPlaces(r, mcat.Rendering{}); err == nil { + if _, err := resolutionSecretPlaces(r, mcat.Rendering{}, nil, nil); err == nil { t.Error("a relative place was dropped") } } diff --git a/internal/compose/resolution/runtime.go b/internal/compose/resolution/runtime.go index ad854231..675010e6 100644 --- a/internal/compose/resolution/runtime.go +++ b/internal/compose/resolution/runtime.go @@ -73,7 +73,7 @@ func interpreterFor(module, language string) (string, error) { // runtime runs as the operator's account when the machine has one, which is what lets a tool that // needs root escalate as the operator would (ADR 0175 §4); on a machine with no account it runs as // root, and the two operator words are not set. -func resolutionRuntimeProcess(r mcat.Resolution, with mcat.Rendering) (map[string]any, error) { +func resolutionRuntimeProcess(r mcat.Resolution, with mcat.Rendering, h Holding) (map[string]any, error) { var runtime *mcat.Manifest for i := range r.Modules { if mcat.IsToolRunner(r.Modules[i]) { @@ -137,7 +137,7 @@ func resolutionRuntimeProcess(r mcat.Resolution, with mcat.Rendering) (map[strin return nil, err } // The holder of node-files is told the same, and the paths modules own here (novox/hq ADR 0394 §4). - if words, err = resolutionFilesWords(r, m, words, with); err != nil { + if words, err = resolutionFilesWords(r, m, words, with, h, runtime.Module+"."+RuntimeProcessID()); err != nil { return nil, err } if len(words) > 0 { @@ -209,7 +209,7 @@ func (e *NoRuntimeToRunError) Error() string { return e.said } // the machine's runtime program — the same build, run from the same source — serving that one module alone, // as the module's own account, on the module's own bus credential. Never the machine's runtime, which runs // as the operator's account and carries every module on the machine. -func resolutionOwnRuntimes(r mcat.Resolution, with mcat.Rendering) ([]map[string]any, error) { +func resolutionOwnRuntimes(r mcat.Resolution, with mcat.Rendering, h Holding) ([]map[string]any, error) { var own []mcat.Manifest for _, m := range r.Modules { // A held module's stand-in (Stubbed) has no runtime of its own composed, and so none of this function's rules @@ -275,7 +275,7 @@ func resolutionOwnRuntimes(r mcat.Resolution, with mcat.Rendering) ([]map[string return nil, err } // The holder of node-reads is told where this node keeps its secrets (novox/hq ADR 0310 §5). - if words, err = resolutionReadsWords(r, m, words, with); err != nil { + if words, err = resolutionReadsWords(r, m, words, with, h, m.Module+"."+mcat.OwnRuntimeID()); err != nil { return nil, err } // A build seat's holder gives way on its node, and its steps are told their part of the node's share diff --git a/internal/compose/resolution/runtime_test.go b/internal/compose/resolution/runtime_test.go index da6ae5bc..3fdd4216 100644 --- a/internal/compose/resolution/runtime_test.go +++ b/internal/compose/resolution/runtime_test.go @@ -328,7 +328,7 @@ func TestAModuleOfItsOwnAccountIsServedByARuntimeOfItsOwn(t *testing.T) { if c.noEngine { modules = modules[:4] } - runtimes, err := resolutionOwnRuntimes(mcat.Resolution{Node: "anchor", Modules: modules}, with) + runtimes, err := resolutionOwnRuntimes(mcat.Resolution{Node: "anchor", Modules: modules}, with, Holding{}) if err != nil { t.Fatal(err) } @@ -354,7 +354,7 @@ func TestAModuleOfItsOwnAccountIsServedByARuntimeOfItsOwn(t *testing.T) { } with.Settings = nil // Without the machine's runtime to run it from, it is refused in words. - if _, err := resolutionOwnRuntimes((mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{telegram}}), with); err == nil { + if _, err := resolutionOwnRuntimes((mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{telegram}}), with, Holding{}); err == nil { t.Error("a module of its own account composed without a runtime program") } } @@ -378,7 +378,7 @@ func TestAModuleOfItsOwnAccountIsRefusedTheOperatorsAndTheAgentsAccount(t *testi telegram.RunsAs, telegram.SecretsOwner = account, account telegram.OwnSecrets = mcat.OwnSecrets{"broker": {Path: "/var/lib/telegram/broker"}} _, err := resolutionOwnRuntimes(mcat.Resolution{Node: "anchor", Account: "ops", AgentAccount: "agent", - Modules: []mcat.Manifest{telegram, goRuntime}}, with) + Modules: []mcat.Manifest{telegram, goRuntime}}, with, Holding{}) if err == nil || !strings.Contains(err.Error(), account) { t.Errorf("telegram running as %s was composed: %v", account, err) }