Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
247d89e152 | ||
|
|
7ade318cfa | ||
|
|
7e96d61268 | ||
|
|
9d43fa4b24 | ||
|
|
cd3ef2fd41 | ||
|
|
b75481733b |
@@ -77,7 +77,6 @@ func Check(root string) (map[string]map[string][]string, error) {
|
||||
}
|
||||
set := map[string]bool{}
|
||||
setBy(manifest, set)
|
||||
settingWords(manifest, set)
|
||||
reads := map[string]map[string]bool{}
|
||||
consts := map[string]map[string]string{} // directory -> constant -> word
|
||||
var goFiles []string
|
||||
@@ -181,17 +180,6 @@ func setBy(v any, set map[string]bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// settingWords collects the words the node-engine composes for every setting the manifest's `config` declares,
|
||||
// MESH_CONFIG_<KEY>, the key upper-cased with '-' and '.' as '_', into the module's processes and its tool runner slice
|
||||
// alike (design 47 H2 §781; H-A-370, H-A-400 (2)).
|
||||
func settingWords(v any, set map[string]bool) {
|
||||
m, _ := v.(map[string]any)
|
||||
config, _ := m["config"].(map[string]any)
|
||||
for key := range config {
|
||||
set["MESH_CONFIG_"+strings.NewReplacer("-", "_", ".", "_").Replace(strings.ToUpper(key))] = true
|
||||
}
|
||||
}
|
||||
|
||||
// Listed is the list's `<module> <WORD>` lines; a line starting with # and a blank line are not entries.
|
||||
func Listed(path string) (map[string]bool, error) {
|
||||
f, err := os.Open(path)
|
||||
|
||||
@@ -32,14 +32,13 @@ func module(t *testing.T, root, name, manifest string, files map[string]string)
|
||||
// not; tests, node_modules and dist are not read.
|
||||
func TestAWordReadThatNothingSetsIsFound(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
module(t, root, "reader", `{"module":"reader","config":{"ask-taken-bound":{"type":"string"}},"build":{"artifacts":[{"name":"tools","env":{"MESH_SET":"x"}}]},`+
|
||||
module(t, root, "reader", `{"module":"reader","build":{"artifacts":[{"name":"tools","env":{"MESH_SET":"x"}}]},`+
|
||||
`"resources":[{"id":"p","type":"process","environment":{"variables":{"MESH_VARIABLE":"y"}}}]}`, map[string]string{
|
||||
"cmd/a/main.go": `package main; const reach = "MESH_REACH"; var _ = os.Getenv(reach); var _ = os.Getenv("MESH_SET")`,
|
||||
"cmd/a/other.go": `package main; var _, _ = os.LookupEnv("MESH_LITERAL"); var _ = os.Getenv("MESH_MODULE")`,
|
||||
"cmd/a/write.go": `package main; var _ = []string{"MESH_WRITTEN_FOR_A_CHILD=1"}`,
|
||||
"cmd/a/a_test.go": `package main; var _ = os.Getenv("MESH_IN_A_TEST")`,
|
||||
// 47 H2 §781, H-A-370: a declared setting's word, which the node-engine composes; one undeclared is set by nothing.
|
||||
"client.ts": `const a = process.env.MESH_TS ?? "d"; const b = env["MESH_BRACKET"]; const c = env.MESH_VARIABLE; const d = env.MESH_CONFIG_ASK_TAKEN_BOUND; const e = env.MESH_CONFIG_UNDECLARED;`,
|
||||
"cmd/a/main.go": `package main; const reach = "MESH_REACH"; var _ = os.Getenv(reach); var _ = os.Getenv("MESH_SET")`,
|
||||
"cmd/a/other.go": `package main; var _, _ = os.LookupEnv("MESH_LITERAL"); var _ = os.Getenv("MESH_MODULE")`,
|
||||
"cmd/a/write.go": `package main; var _ = []string{"MESH_WRITTEN_FOR_A_CHILD=1"}`,
|
||||
"cmd/a/a_test.go": `package main; var _ = os.Getenv("MESH_IN_A_TEST")`,
|
||||
"client.ts": `const a = process.env.MESH_TS ?? "d"; const b = env["MESH_BRACKET"]; const c = env.MESH_VARIABLE;`,
|
||||
"files/env": "MESH_FROM_FILE=${config:x}\n",
|
||||
"index.ts": `const f = env.MESH_FROM_FILE;`,
|
||||
"test/x.test.ts": `env.MESH_IN_A_TEST_DIR`,
|
||||
@@ -51,17 +50,16 @@ func TestAWordReadThatNothingSetsIsFound(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]map[string][]string{"reader": {
|
||||
"MESH_REACH": {"cmd/a/main.go"},
|
||||
"MESH_LITERAL": {"cmd/a/other.go"},
|
||||
"MESH_TS": {"client.ts"},
|
||||
"MESH_BRACKET": {"client.ts"},
|
||||
"MESH_CONFIG_UNDECLARED": {"client.ts"},
|
||||
"MESH_REACH": {"cmd/a/main.go"},
|
||||
"MESH_LITERAL": {"cmd/a/other.go"},
|
||||
"MESH_TS": {"client.ts"},
|
||||
"MESH_BRACKET": {"client.ts"},
|
||||
}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("found %v, want %v", got, want)
|
||||
}
|
||||
refused, stale := Judge(got, map[string]bool{"reader MESH_REACH": true, "reader MESH_GONE": true})
|
||||
if !reflect.DeepEqual(refused, []string{"reader MESH_BRACKET", "reader MESH_CONFIG_UNDECLARED", "reader MESH_LITERAL", "reader MESH_TS"}) ||
|
||||
if !reflect.DeepEqual(refused, []string{"reader MESH_BRACKET", "reader MESH_LITERAL", "reader MESH_TS"}) ||
|
||||
!reflect.DeepEqual(stale, []string{"reader MESH_GONE"}) {
|
||||
t.Errorf("refused %v, stale %v", refused, stale)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
// exempt from that, and said so. Every build module, test step or not, must also hold a test that calls
|
||||
// conformance.LeftRunning with its own start sweep, so a step a previous process left running is ended at its start
|
||||
// (issue 1890), and one that calls conformance.Passing, so a step whose network did not answer is said as a transport fault
|
||||
// (issue 1748), and one that calls conformance.StepEnds, so its steps can say a pipeline step's end (design 47 G §24). Its manifest asks the capability seat-settle-delay (ADR 0664 §5).
|
||||
// (issue 1748). Its manifest asks the capability seat-settle-delay (ADR 0664 §5).
|
||||
package failurekit
|
||||
|
||||
import (
|
||||
@@ -91,25 +91,6 @@ func Judge(root string) ([]Judged, error) {
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and no test of it calls conformance.LeftRunning "+
|
||||
"(mesh-build-go buildstep/conformance): run the kit's left-running case with the module's own start sweep (issue 1890)", seat))
|
||||
}
|
||||
// A-489b: every build module is asked pipeline steps, so every one runs the case that its steps can say a step's end: without a
|
||||
// SayStep every step ask is refused live ("this build module says no step's end"), as mesh-build-node's was.
|
||||
ends, err := runsTheKit(dir, "conformance.StepEnds(")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
counts, err := runsTheKit(dir, "conformance.RunsCounted(")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !counts {
|
||||
// A-489c
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and no test of it calls conformance.RunsCounted "+
|
||||
"(mesh-delivery-sdk steprun/conformance): run the kit's case that its puller counts the runs that started, not the bus's deliveries (A-489a)", seat))
|
||||
}
|
||||
if !ends {
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and no test of it calls conformance.StepEnds "+
|
||||
"(mesh-delivery-sdk steprun/conformance): run the kit's step-ends case with the module's own steps (design 47 G §24)", seat))
|
||||
}
|
||||
// Every build module meets the network (a fetch, a download, an install, a publish), so every one runs the passing case: its
|
||||
// steps say a network that did not answer as a transport fault the bus gives the ask again for (ADR 0664, issue 1748).
|
||||
says, err := runsTheKit(dir, "conformance.Passing(")
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
func TestABuildModuleWithATestStepMustRunTheKit(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
const imp = "package main\n\nimport \"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun/conformance\"\n\n"
|
||||
const sweep = "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\nfunc TestEnds(t *testing.T) { conformance.StepEnds(t, steprun.AskSteps{}) }\nfunc TestCounted(t *testing.T) { conformance.RunsCounted(t, nil) }\n"
|
||||
const sweep = "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\n"
|
||||
kit := imp + "func TestKit(t *testing.T) { conformance.Run(t, conformance.GoTest, nil) }\n" + sweep
|
||||
for path, body := range map[string]string{
|
||||
"modules/a/module.json": `{"claims":[{"name":"node-build-a"}],"tools":["build_a_fetch","build_a_test"]}`,
|
||||
@@ -30,10 +30,6 @@ func TestABuildModuleWithATestStepMustRunTheKit(t *testing.T) {
|
||||
"modules/h/cmd/h/kit_test.go": imp + "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\n",
|
||||
"modules/i/module.json": `{"claims":[{"name":"node-build-i"}],"capabilities":["other"],"tools":["build_i_build"]}`, // the whole kit, and no seat-settle-delay
|
||||
"modules/i/cmd/i/kit_test.go": imp + sweep,
|
||||
"modules/j/module.json": `{"claims":[{"name":"node-build-j"}],"capabilities":["seat-settle-delay"],"tools":["build_j_build"]}`, // the rest of the kit, and no step-ends case
|
||||
"modules/j/cmd/j/kit_test.go": imp + "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\nfunc TestCounted(t *testing.T) { conformance.RunsCounted(t, nil) }\n",
|
||||
"modules/k/module.json": `{"claims":[{"name":"node-build-k"}],"capabilities":["seat-settle-delay"],"tools":["build_k_build"]}`, // no runs-counted case
|
||||
"modules/k/cmd/k/kit_test.go": imp + "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\nfunc TestEnds(t *testing.T) { conformance.StepEnds(t, steprun.AskSteps{}) }\n",
|
||||
"modules/e/module.json": `{"claims":[{"name":"node-build-e"}],"tools":["build_e_test"]}`,
|
||||
"modules/e/cmd/e/kit.go": kit, // not a test file
|
||||
} {
|
||||
@@ -49,9 +45,9 @@ func TestABuildModuleWithATestStepMustRunTheKit(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]string{"a": "refused", "b": "passes", "c": "exempt", "e": "refused", "f": "refused", "g": "refused", "h": "refused", "i": "refused", "j": "refused", "k": "refused"}
|
||||
want := map[string]string{"a": "refused", "b": "passes", "c": "exempt", "e": "refused", "f": "refused", "g": "refused", "h": "refused", "i": "refused"}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("judged %+v, want a, b, c, e, f, g, h, i, j and k", got)
|
||||
t.Fatalf("judged %+v, want a, b, c, e, f, g, h and i", got)
|
||||
}
|
||||
for _, j := range got {
|
||||
is := "passes"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "adwaita",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"moved": true,
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
|
||||
@@ -119,16 +119,3 @@ removed for `retention` on its seat (`release`, `remove`). It reads a reference
|
||||
and `<seat>/<module>/<artifact>@<digest>`, as one artifact, and says artifacts-gone in the form the build named it. A
|
||||
call a seat refuses stops the sweep and writes the finding `sweep-refused` in the module's findings; the next sweep no
|
||||
seat refuses clears it (`sweepseats.go`).
|
||||
|
||||
### One pending pass
|
||||
|
||||
A finished build or a changed want marks the one pending pass (design 47 H, H-A-250); a pass running when one is marked
|
||||
runs once more when it ends, however many events marked it. The builder's builds are a followed view: the watch's first view
|
||||
at start, then its updates, kept in memory (H-A-255), each build's end taken from the builder's log's build-ended entries the
|
||||
pass reads once, and only for a build the log lacks from its key's history (H-A-258); a watch asked again after an error replaces the view whole with its handover (H-A-257). A pass reads nothing
|
||||
of them on the bus, judges nothing before the first view is in, and sweeps and observes S25
|
||||
from that one judgement. A list the registry
|
||||
seats cannot answer ends the pass with the finding `artifact-list-unread`, written once; nothing asks the list again
|
||||
until the next event, and a pass that reads it clears the finding. A pass that cannot judge the builds (the view not yet taken in, or a
|
||||
done build's end unread) ends the same way with the finding `artifact-builds-unread` (H-A-260) (`holder.go`,
|
||||
`sweepseats.go`).
|
||||
|
||||
@@ -116,16 +116,12 @@ func (runtimeBus) Publish(subject string, body any) error {
|
||||
return stdio.SeatPublish(subject, body, "")
|
||||
}
|
||||
|
||||
// follow watches the builder's done keys and the wants: the builds are a followed view, the watch's first view then its
|
||||
// updates kept in memory (design 47 H, H-A-255); a build's end or a want changed marks the one pending pass (H-A-250 (1)),
|
||||
// which sweeps and observes from the views. Event-driven: nothing reads them on a timer.
|
||||
// follow watches the builder's done keys and the wants: a build's end sweeps and
|
||||
// observes again; a want changed observes again. Event-driven: nothing reads them on a timer.
|
||||
func follow(h *Holder, v *views.Reader) {
|
||||
<-stdio.Served()
|
||||
watch := func(state string, handle func(stdio.StateChange) error) {
|
||||
for {
|
||||
if state == BuildsRead {
|
||||
h.Watching() // each handover, the first or one asked again, replaces the view whole (H-A-257)
|
||||
}
|
||||
if err := stdio.State(state).Watch("", handle); err == nil {
|
||||
return
|
||||
} else {
|
||||
@@ -134,8 +130,12 @@ func follow(h *Holder, v *views.Reader) {
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
}
|
||||
observe := func() {
|
||||
if err := h.Observe(context.Background()); err != nil {
|
||||
h.logf("image-missing could not be observed: %v", err)
|
||||
}
|
||||
}
|
||||
watch(BuildsRead, func(c stdio.StateChange) error {
|
||||
h.Heard(c)
|
||||
if c.Current || c.Op != stdio.OpPut {
|
||||
return nil
|
||||
}
|
||||
@@ -143,12 +143,10 @@ func follow(h *Holder, v *views.Reader) {
|
||||
State string `json:"state"`
|
||||
}
|
||||
if json.Unmarshal(c.Value, &b) == nil && b.State == "done" {
|
||||
h.Wanted("after the build " + c.Key)
|
||||
go func() { h.AfterBuild(c.Key); observe() }()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
// The watch hands every current value over before it returns: the first view is in.
|
||||
h.TakenIn()
|
||||
// The wants are the assigner's: the first view then its events (ADR 0615). What Watch hands over before it returns
|
||||
// is the first view, not a change.
|
||||
var current atomic.Bool
|
||||
@@ -156,7 +154,7 @@ func follow(h *Holder, v *views.Reader) {
|
||||
for {
|
||||
err := v.Watch(WantsRead, func(string, []byte, bool) {
|
||||
if !current.Load() {
|
||||
h.Wanted("a want changed")
|
||||
go observe()
|
||||
}
|
||||
})
|
||||
if err == nil {
|
||||
@@ -166,5 +164,5 @@ func follow(h *Holder, v *views.Reader) {
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
current.Store(false)
|
||||
h.Wanted("the first view of the wants")
|
||||
observe()
|
||||
}
|
||||
|
||||
@@ -38,10 +38,6 @@ import (
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
)
|
||||
|
||||
// ErrBuildsUnread is a judgement refused because the builds cannot be judged: the followed view not yet taken in, or a
|
||||
// done key's end unread (H-A-258, H-A-260).
|
||||
var ErrBuildsUnread = errors.New("the builder's builds cannot be judged")
|
||||
|
||||
// What the holder reads and keeps.
|
||||
const (
|
||||
WantsRead = assignerModule + ".wants"
|
||||
@@ -99,19 +95,8 @@ type Holder struct {
|
||||
|
||||
sweep sync.Mutex // one sweep at a time
|
||||
mu sync.Mutex
|
||||
// pmu guards the pending pass (H-A-250 (1)): running while a pass runs, wanted when an event marked it since.
|
||||
pmu sync.Mutex
|
||||
running bool
|
||||
wanted bool
|
||||
wantedBy string
|
||||
fmu sync.Mutex // one pass of Observe at a time
|
||||
ended map[string]time.Time // a done key's end, read once from its history
|
||||
// done is the followed view of the builder's builds (design 47 H, H-A-255): the done builds by key, taken in from the
|
||||
// watch's first view and its updates (Heard); taken says the first view is in. A pass judges from it, reading nothing.
|
||||
done map[string]Build
|
||||
taken bool
|
||||
// next is the handover a watch asked (again) is giving, which replaces done whole once it is in (H-A-257).
|
||||
next map[string]Build
|
||||
fmu sync.Mutex // one pass of Observe at a time
|
||||
ended map[string]time.Time // a done key's end, read once from its history
|
||||
}
|
||||
|
||||
// Build is one done build of a module, as its key holds it, with when it ended.
|
||||
@@ -123,81 +108,39 @@ type Build struct {
|
||||
Manifest string
|
||||
}
|
||||
|
||||
// Builds is every done build of the followed view, by key, read from memory, never from the bus (design 47 H, H-A-250
|
||||
// (2), H-A-255): each with the end known for it, zero where none is known yet (withEnds gives it). Before the view's first
|
||||
// view is taken in there is none to judge from, and it says so, so no pass sweeps against an empty keep set.
|
||||
// Builds is every done build the builder's keys hold, each with its end.
|
||||
func (h *Holder) Builds() ([]Build, error) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if !h.taken {
|
||||
return nil, fmt.Errorf("%w: they are not taken in yet, their first view has not come", ErrBuildsUnread)
|
||||
keys, err := h.Bus.Keys(BuildsRead)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the builder's builds: %w", err)
|
||||
}
|
||||
out := make([]Build, 0, len(h.done))
|
||||
for _, b := range h.done {
|
||||
b.At = h.ended[b.Key]
|
||||
out = append(out, b)
|
||||
var out []Build
|
||||
for _, k := range keys {
|
||||
module, fp, err := builds.ParseBuilderBuildKey(k)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
raw, found, err := h.Bus.Get(BuildsRead, k)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the builder's build %s: %w", k, err)
|
||||
}
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
b, err := builds.DecodeBuilderBuild(raw)
|
||||
if err != nil || b.State != builds.BuildDone {
|
||||
continue
|
||||
}
|
||||
at, err := h.endOf(k)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, Build{Module: module, FP: fp, Key: k, At: at, Artifacts: b.Artifacts, Manifest: b.Manifest})
|
||||
}
|
||||
sort.Slice(out, func(a, b int) bool { return out[a].Key < out[b].Key })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Heard takes one change of the builder's builds into the followed view: a done value is kept, anything else drops the
|
||||
// key. Its end is not read here: withEnds takes it from the builder's log at the next pass (H-A-258); a key written done
|
||||
// anew forgets the end it had. While a watch's handover is gathered (Watching), every change goes into it, current or
|
||||
// not, so nothing heard before TakenIn is lost by the swap (H-A-257).
|
||||
func (h *Holder) Heard(c stdio.StateChange) {
|
||||
module, fp, err := builds.ParseBuilderBuildKey(c.Key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var b builds.BuilderBuild
|
||||
if c.Op == stdio.OpPut {
|
||||
b, err = builds.DecodeBuilderBuild(c.Value)
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if !c.Current {
|
||||
delete(h.ended, c.Key) // written anew or gone: an end it had is no longer its
|
||||
}
|
||||
if h.next == nil && h.done == nil {
|
||||
h.done = map[string]Build{}
|
||||
}
|
||||
into := h.done
|
||||
if h.next != nil {
|
||||
into = h.next
|
||||
}
|
||||
if c.Op != stdio.OpPut || err != nil || b.State != builds.BuildDone {
|
||||
delete(into, c.Key)
|
||||
return
|
||||
}
|
||||
into[c.Key] = Build{Module: module, FP: fp, Key: c.Key, Artifacts: b.Artifacts, Manifest: b.Manifest}
|
||||
}
|
||||
|
||||
// Watching says a watch of the builds is asked, first or again after an error: its handover of current values is
|
||||
// gathered apart and replaces the view whole at TakenIn, so a key deleted while no watch ran leaves it (H-A-257).
|
||||
func (h *Holder) Watching() {
|
||||
h.mu.Lock()
|
||||
h.next = map[string]Build{}
|
||||
h.mu.Unlock()
|
||||
}
|
||||
|
||||
// TakenIn says the watch's handover is in: every current value was Heard. It replaces the view whole, and forgets the
|
||||
// end of every key it no longer holds.
|
||||
func (h *Holder) TakenIn() {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if h.next != nil {
|
||||
h.done, h.next = h.next, nil
|
||||
for k := range h.ended {
|
||||
if _, kept := h.done[k]; !kept {
|
||||
delete(h.ended, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
h.taken = true
|
||||
}
|
||||
|
||||
// endOf is when a done key was last written done, read from its history and kept: for a build the builder's log lacks.
|
||||
// endOf is when a done key was last written done, read once from its history: a done key is not written again.
|
||||
func (h *Holder) endOf(key string) (time.Time, error) {
|
||||
h.mu.Lock()
|
||||
at, known := h.ended[key]
|
||||
@@ -377,8 +320,6 @@ type judged struct {
|
||||
mirrors map[string]*Ref
|
||||
// mirrorsHeld says why no copy is eligible now, when one is not: a kept build whose bases the log does not name.
|
||||
mirrorsHeld string
|
||||
// unread is why the registry seats' list could not be read, nil when it was (or none is read).
|
||||
unread error
|
||||
}
|
||||
|
||||
func (h *Holder) judge(ctx context.Context) (*judged, error) {
|
||||
@@ -386,15 +327,6 @@ func (h *Holder) judge(ctx context.Context) (*judged, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// The builder's log, read once: what each build stood on, and each build's end (H-A-258).
|
||||
against, ends, logErr := h.logged()
|
||||
if logErr != nil {
|
||||
h.logf("the builder's log could not be read, so every copy is kept as unrecorded and each end is read from its history: %v", logErr)
|
||||
against, ends = nil, nil
|
||||
}
|
||||
if builds, err = h.withEnds(builds, ends); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wants, err := h.Wants()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -403,10 +335,15 @@ func (h *Holder) judge(ctx context.Context) (*judged, error) {
|
||||
if h.Listed != nil {
|
||||
if j.view, err = h.Listed(ctx); err != nil {
|
||||
h.logf("the store's listing could not be read, so nothing is said gone: %v", err)
|
||||
j.view, j.unread = nil, err
|
||||
j.view = nil
|
||||
}
|
||||
}
|
||||
if j.view != nil {
|
||||
against, err := h.against()
|
||||
if err != nil {
|
||||
h.logf("the builder's log could not be read, so every copy is kept as unrecorded: %v", err)
|
||||
against = nil
|
||||
}
|
||||
j.mirrors, j.mirrorsHeld = Mirrors(j.view, builds, keptBuilds(builds, wants), against)
|
||||
for r, m := range j.mirrors {
|
||||
if len(m.Builds) > 0 {
|
||||
@@ -417,47 +354,13 @@ func (h *Holder) judge(ctx context.Context) (*judged, error) {
|
||||
return j, nil
|
||||
}
|
||||
|
||||
// withEnds gives each build its end: the one known, else the builder's log's build-ended entry, else its key's history,
|
||||
// each kept once read (H-A-258). A done key whose end can be read from neither stays unread, and then nothing is judged:
|
||||
// never from a view missing a build.
|
||||
func (h *Holder) withEnds(built []Build, ends map[string]time.Time) ([]Build, error) {
|
||||
var unread []string
|
||||
var why error
|
||||
for i := range built {
|
||||
if !built[i].At.IsZero() {
|
||||
continue
|
||||
}
|
||||
if at, logged := ends[built[i].Key]; logged {
|
||||
h.mu.Lock()
|
||||
if h.ended == nil {
|
||||
h.ended = map[string]time.Time{}
|
||||
}
|
||||
h.ended[built[i].Key] = at
|
||||
h.mu.Unlock()
|
||||
built[i].At = at
|
||||
continue
|
||||
}
|
||||
at, err := h.endOf(built[i].Key)
|
||||
if err != nil {
|
||||
unread, why = append(unread, built[i].Key), err
|
||||
continue
|
||||
}
|
||||
built[i].At = at
|
||||
}
|
||||
if len(unread) > 0 {
|
||||
return nil, fmt.Errorf("%w: the end of %d done builds could not be read (%s first), so nothing is judged: %v", ErrBuildsUnread, len(unread), unread[0], why)
|
||||
}
|
||||
return built, nil
|
||||
}
|
||||
|
||||
// logged is what each build the builder's log says ended stood on, by build key (the bases' references), and when it
|
||||
// ended: one read of the log.
|
||||
func (h *Holder) logged() (map[string][]string, map[string]time.Time, error) {
|
||||
// against is what each build the builder's log says ended stood on, by build key: the bases' references.
|
||||
func (h *Holder) against() (map[string][]string, error) {
|
||||
entries, err := h.Bus.LogSince(builds.BuildLogRead, h.now().Add(-builds.BuilderLogDays*24*time.Hour))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
out, ends := map[string][]string{}, map[string]time.Time{}
|
||||
out := map[string][]string{}
|
||||
for _, raw := range entries {
|
||||
e, err := builds.DecodeBuildLogEntry(raw.Value)
|
||||
if err != nil || e.Event != builds.BuildEndedEvent {
|
||||
@@ -468,11 +371,8 @@ func (h *Holder) logged() (map[string][]string, map[string]time.Time, error) {
|
||||
continue
|
||||
}
|
||||
out[key] = e.Against
|
||||
if e.At.After(ends[key]) {
|
||||
ends[key] = e.At
|
||||
}
|
||||
}
|
||||
return out, ends, nil
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Mirrors judges each copy in the store's mirror repositories (upstream/<host>/<path>, <module>/on-<argument>; ADR
|
||||
@@ -614,11 +514,6 @@ func (h *Holder) Sweep(ctx context.Context, s Sweeping) (*CollectAnswer, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return h.sweepFrom(ctx, j, s)
|
||||
}
|
||||
|
||||
// sweepFrom is a sweep from a judgement already read; the caller holds h.sweep.
|
||||
func (h *Holder) sweepFrom(ctx context.Context, j *judged, s Sweeping) (*CollectAnswer, error) {
|
||||
a := &CollectAnswer{DryRun: s.DryRun, WouldLetGo: []string{}, LetGo: []string{}}
|
||||
var keptArchives, eligible []string
|
||||
for _, r := range j.sorted() {
|
||||
@@ -763,73 +658,15 @@ func namedBy(b Build, r string) string {
|
||||
return r
|
||||
}
|
||||
|
||||
// Wanted marks the pass wanted (design 47 H, H-A-250 (1)): a finished build or a want change asks it, by naming what
|
||||
// asked. With no pass running one starts; a pass running when it is marked runs once more when it ends, however many
|
||||
// events marked it, never once per event.
|
||||
func (h *Holder) Wanted(by string) {
|
||||
if h.mark(by) {
|
||||
go h.passes(by)
|
||||
}
|
||||
}
|
||||
|
||||
// mark sets the pending mark, and says whether no pass ran, so the caller starts one.
|
||||
func (h *Holder) mark(by string) bool {
|
||||
h.pmu.Lock()
|
||||
defer h.pmu.Unlock()
|
||||
if h.running {
|
||||
h.wanted, h.wantedBy = true, by
|
||||
return false
|
||||
}
|
||||
h.running = true
|
||||
return true
|
||||
}
|
||||
|
||||
// passes runs the pass, then once more for as long as an event marked it during the one that ran.
|
||||
func (h *Holder) passes(by string) {
|
||||
for {
|
||||
h.Pass(context.Background(), by)
|
||||
h.pmu.Lock()
|
||||
if !h.wanted {
|
||||
h.running = false
|
||||
h.pmu.Unlock()
|
||||
return
|
||||
}
|
||||
h.wanted, by = false, h.wantedBy
|
||||
h.pmu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// Pass is one pass of the seat: the keep set judged from one read of the builds, the wants and the store's list, then
|
||||
// the sweep and S25's observation from that one judgement. A list the registry seats cannot answer ends the pass with
|
||||
// the finding artifact-list-unread, asked again only at the next event (H-A-250 (3)); a list read again clears it.
|
||||
func (h *Holder) Pass(ctx context.Context, by string) {
|
||||
h.sweep.Lock()
|
||||
j, err := h.judge(ctx)
|
||||
// AfterBuild is the sweep at a build's end.
|
||||
func (h *Holder) AfterBuild(key string) {
|
||||
a, err := h.Sweep(context.Background(), Sweeping{Most: MostPerSweep, By: "after the build " + key})
|
||||
if err != nil {
|
||||
h.sweep.Unlock()
|
||||
h.logf("the pass %s: %v", by, err)
|
||||
if errors.Is(err, ErrBuildsUnread) {
|
||||
h.unreadFinding(BuildsUnread, err) // H-A-260
|
||||
}
|
||||
h.logf("the sweep after %s: %v", key, err)
|
||||
return
|
||||
}
|
||||
h.readAgain(BuildsUnread)
|
||||
if j.unread != nil {
|
||||
h.sweep.Unlock()
|
||||
h.unreadFinding(ListUnread, j.unread)
|
||||
return
|
||||
}
|
||||
h.readAgain(ListUnread)
|
||||
a, err := h.sweepFrom(ctx, j, Sweeping{Most: MostPerSweep, By: by})
|
||||
h.sweep.Unlock()
|
||||
switch {
|
||||
case err != nil:
|
||||
h.logf("the sweep %s: %v", by, err)
|
||||
case len(a.LetGo) > 0 || a.Stopped != "" || a.Missing > 0:
|
||||
h.logf("the sweep %s let go of %d (%d left, %d missing): %s", by, len(a.LetGo), a.Left, a.Missing, a.Stopped)
|
||||
}
|
||||
if err := h.observeFrom(ctx, j); err != nil {
|
||||
h.logf("image-missing could not be observed: %v", err)
|
||||
if len(a.LetGo) > 0 || a.Stopped != "" || a.Missing > 0 {
|
||||
h.logf("the sweep after %s let go of %d (%d left, %d missing): %s", key, len(a.LetGo), a.Left, a.Missing, a.Stopped)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1020,16 +857,10 @@ func inStoreAt(image, address string) string {
|
||||
// want's machine, written once and cleared once it no longer holds. Nothing is said without a listing. What stands is what
|
||||
// the held writer holds (design 47 G A-32, A-38), so a restart neither drops nor rewrites it.
|
||||
func (h *Holder) Observe(ctx context.Context) error {
|
||||
j, err := h.judge(ctx)
|
||||
now, err := h.observed(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return h.observeFrom(ctx, j)
|
||||
}
|
||||
|
||||
// observeFrom is Observe from a judgement already read.
|
||||
func (h *Holder) observeFrom(ctx context.Context, j *judged) error {
|
||||
now := h.observed(j)
|
||||
if now == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -1068,9 +899,13 @@ type Findings interface {
|
||||
}
|
||||
|
||||
// observed is S25's findings now by key, nil without a listing.
|
||||
func (h *Holder) observed(j *judged) map[string]meshconditions.Finding {
|
||||
func (h *Holder) observed(ctx context.Context) (map[string]meshconditions.Finding, error) {
|
||||
j, err := h.judge(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if j.view == nil {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
address := ""
|
||||
if d, err := h.Door(); err == nil {
|
||||
@@ -1109,7 +944,7 @@ func (h *Holder) observed(j *judged) map[string]meshconditions.Finding {
|
||||
break
|
||||
}
|
||||
}
|
||||
return now
|
||||
return now, nil
|
||||
}
|
||||
|
||||
// imageMissing is S25's finding, in the controller's words (cmd/mesh-controller/image_missing.go watchMissingImages); its id
|
||||
|
||||
@@ -11,7 +11,6 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -201,15 +200,6 @@ func TestTheSeatSweepsAtABuildsEndOnARealBusWithNoLease(t *testing.T) {
|
||||
}
|
||||
h := &Holder{Bus: findingsAt{realBus: bus}, Findings: findingsThrough{findingsAt{realBus: bus}}, Door: mem.Door, Listed: mem.Listed, Seats: ask, Now: time.Now, Logf: t.Logf}
|
||||
h.Acts = meshhandactsclient.Recorder{Ask: ask}
|
||||
// The followed view of the builds: the six on the bus handed over as the watch's first view does.
|
||||
h.Watching()
|
||||
if keys, err := bus.Keys(BuildsRead); err == nil {
|
||||
for _, k := range keys {
|
||||
raw, _, _ := bus.Get(BuildsRead, k)
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: k, Op: stdio.OpPut, Value: raw, Current: true})
|
||||
}
|
||||
}
|
||||
h.TakenIn()
|
||||
|
||||
// A build's end heard from the builds bucket, as follow hands it over.
|
||||
kv, _ := bus.kv(BuildsRead)
|
||||
@@ -225,8 +215,7 @@ func TestTheSeatSweepsAtABuildsEndOnARealBusWithNoLease(t *testing.T) {
|
||||
}
|
||||
select {
|
||||
case e := <-w.Updates():
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: e.Key(), Op: stdio.OpPut, Value: e.Value()})
|
||||
h.Pass(ctx, "after the build "+e.Key())
|
||||
h.AfterBuild(e.Key())
|
||||
case <-ctx.Done():
|
||||
t.Fatal("the build's end was not heard")
|
||||
}
|
||||
@@ -245,82 +234,26 @@ func TestTheSeatSweepsAtABuildsEndOnARealBusWithNoLease(t *testing.T) {
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
func() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(acts) != 3 || !strings.Contains(acts[0], archiveRef("letta", 2)) || !strings.Contains(acts[1], imageRef("letta", 2)) ||
|
||||
!strings.Contains(acts[2], mirror(1)) {
|
||||
t.Fatalf("the hand acts recorded are %v; the store was asked %v", acts, door.asked)
|
||||
}
|
||||
if len(gone) != 1 || !strings.Contains(gone[0], `"fingerprint":"`+fp(2)+`"`) {
|
||||
t.Fatalf("artifacts-gone said %v", gone)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(acts) != 3 || !strings.Contains(acts[0], archiveRef("letta", 2)) || !strings.Contains(acts[1], imageRef("letta", 2)) ||
|
||||
!strings.Contains(acts[2], mirror(1)) {
|
||||
t.Fatalf("the hand acts recorded are %v; the store was asked %v", acts, door.asked)
|
||||
}
|
||||
if len(gone) != 1 || !strings.Contains(gone[0], `"fingerprint":"`+fp(2)+`"`) {
|
||||
t.Fatalf("artifacts-gone said %v", gone)
|
||||
}
|
||||
|
||||
// S25 through the real findings bucket: letta's wanted image missing from the store is a finding there.
|
||||
view, _ := mem.Listed(ctx)
|
||||
repo, digest, _ := splitReference(imageRef("letta", 1))
|
||||
delete(view.L.Repos[repo].Revisions, digest)
|
||||
if err := h.Observe(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if keys, _ := bus.Keys("artifact-store-tools." + FindingsState); len(keys) != 1 || keys[0] != "module.letta.ace.image-missing" {
|
||||
t.Fatalf("the findings are %v", keys)
|
||||
}
|
||||
}()
|
||||
|
||||
// Design 47 H2 §813 (7): a pass reads oci-artifact-registry's list page by page, each at most 768 KB, over 20,000
|
||||
// artifacts, so its view holds every one.
|
||||
t.Run("a pass reads 20,000 artifacts page by page", func(t *testing.T) {
|
||||
const many, pageBytes = 20000, 768 << 10
|
||||
repos := make([]map[string]any, many)
|
||||
for i := range repos {
|
||||
repos[i] = map[string]any{"name": fmt.Sprintf("m%05d/bundle", i), "manifests": []map[string]any{{
|
||||
"digest": fmt.Sprintf("sha256:%064x", i), "artifact": fmt.Sprintf("sha256:%064x", many+i), "size": 100}}}
|
||||
}
|
||||
pages := 0
|
||||
sub, err := nc.Subscribe("mesh.seat.oci-artifact-registry.tool.list.paged", func(m *nats.Msg) {
|
||||
var in struct{ Next string }
|
||||
_ = json.Unmarshal(m.Data, &in)
|
||||
page, size, next := []map[string]any{}, 0, ""
|
||||
for _, r := range repos {
|
||||
if r["name"].(string) <= in.Next {
|
||||
continue
|
||||
}
|
||||
raw, _ := json.Marshal(r)
|
||||
if size+len(raw)+1 > pageBytes-1024 { // its comma, and room for the answer's other fields
|
||||
next = page[len(page)-1]["name"].(string)
|
||||
break
|
||||
}
|
||||
size += len(raw) + 1
|
||||
page = append(page, r)
|
||||
}
|
||||
pages++
|
||||
out, _ := json.Marshal(map[string]any{"repositories": page, "next": next, "capped": next != "", "total": many})
|
||||
_ = m.Respond(out)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = sub.Unsubscribe() }()
|
||||
store := &Store{Ask: func(key string, body any) (json.RawMessage, error) {
|
||||
if key != "seat:oci-artifact-registry.list" {
|
||||
return ask(key, body)
|
||||
}
|
||||
raw, _ := json.Marshal(body)
|
||||
m, err := nc.Request("mesh.seat.oci-artifact-registry.tool.list.paged", raw, 5*time.Second)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(m.Data) > pageBytes {
|
||||
t.Errorf("a page of %d bytes", len(m.Data))
|
||||
}
|
||||
return m.Data, nil
|
||||
}}
|
||||
got, err := store.ListOf("oci-artifact-registry", "")
|
||||
if err != nil || len(got) != many || got[many-1].Name != "m19999/bundle" || pages < 2 {
|
||||
t.Fatalf("read %d repositories in %d pages: %v", len(got), pages, err)
|
||||
}
|
||||
})
|
||||
// S25 through the real findings bucket: letta's wanted image missing from the store is a finding there.
|
||||
view, _ := mem.Listed(ctx)
|
||||
repo, digest, _ := splitReference(imageRef("letta", 1))
|
||||
delete(view.L.Repos[repo].Revisions, digest)
|
||||
if err := h.Observe(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if keys, _ := bus.Keys("artifact-store-tools." + FindingsState); len(keys) != 1 || keys[0] != "module.letta.ace.image-missing" {
|
||||
t.Fatalf("the findings are %v", keys)
|
||||
}
|
||||
}
|
||||
|
||||
// findingsAt reads and writes the holder's own findings in its bucket, artifact-store-tools_findings.
|
||||
|
||||
@@ -34,17 +34,6 @@ type memBus struct {
|
||||
history map[string][]stdio.StateRevision
|
||||
log []stdio.LogEntry
|
||||
published []string
|
||||
// historyErr fails the history read of a key.
|
||||
historyErr map[string]error
|
||||
// calls counts each read by "<verb> <state>": a pass reads nothing of the builds, judged from the followed view.
|
||||
calls map[string]int
|
||||
}
|
||||
|
||||
func (b *memBus) count(verb, state string) {
|
||||
if b.calls == nil {
|
||||
b.calls = map[string]int{}
|
||||
}
|
||||
b.calls[verb+" "+state]++
|
||||
}
|
||||
|
||||
func (b *memBus) LogSince(read string, at time.Time) ([]stdio.LogEntry, error) {
|
||||
@@ -60,7 +49,6 @@ func (b *memBus) LogSince(read string, at time.Time) ([]stdio.LogEntry, error) {
|
||||
func (b *memBus) Keys(state string) ([]string, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.count("keys", state)
|
||||
var out []string
|
||||
for k := range b.states[state] {
|
||||
out = append(out, k)
|
||||
@@ -72,18 +60,11 @@ func (b *memBus) Keys(state string) ([]string, error) {
|
||||
func (b *memBus) Get(state, key string) ([]byte, bool, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.count("get", state)
|
||||
v, ok := b.states[state][key]
|
||||
return v, ok, nil
|
||||
}
|
||||
|
||||
func (b *memBus) History(state, key string) ([]stdio.StateRevision, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.count("history", state)
|
||||
if err := b.historyErr[key]; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.history[state+" "+key], nil
|
||||
}
|
||||
|
||||
@@ -248,7 +229,7 @@ func aMesh(t *testing.T) (*Holder, *memBus, *memDoor, *[]string) {
|
||||
want, _ := json.Marshal(meshwants.WantsByWantNodeModuleValue{V: 1, Fp: fp(1), Self: true})
|
||||
_ = b.Put(WantsRead, "want.ace.letta", json.RawMessage(want))
|
||||
var acts []string
|
||||
h := &Holder{Logf: t.Logf, Bus: b, Findings: findingsThrough{b}, Door: func() (Door, error) { return door, nil }, Seats: door.seat,
|
||||
h := &Holder{Bus: b, Findings: findingsThrough{b}, Door: func() (Door, error) { return door, nil }, Seats: door.seat,
|
||||
Listed: func(context.Context) (*View, error) { return &View{L: listing}, nil },
|
||||
Now: func() time.Time { return day.Add(48 * time.Hour) },
|
||||
Acts: meshhandactsclient.Recorder{Ask: func(key string, body any) (json.RawMessage, error) {
|
||||
@@ -256,49 +237,9 @@ func aMesh(t *testing.T) (*Holder, *memBus, *memDoor, *[]string) {
|
||||
acts = append(acts, key+" "+string(raw))
|
||||
return json.RawMessage(`{}`), nil
|
||||
}}}
|
||||
takeIn(h, b)
|
||||
return h, b, door, &acts
|
||||
}
|
||||
|
||||
// takeIn hands the builds over as the watch's first view does, then forgets the reads that took: a pass's are counted.
|
||||
func takeIn(h *Holder, b *memBus) {
|
||||
handOver(h, b)
|
||||
h.TakenIn()
|
||||
b.calls = nil
|
||||
}
|
||||
|
||||
// handOver is a watch's handover of every current value, not yet taken in.
|
||||
func handOver(h *Holder, b *memBus) {
|
||||
h.Watching()
|
||||
keys, _ := b.Keys(BuildsRead)
|
||||
for _, k := range keys {
|
||||
raw, _, _ := b.Get(BuildsRead, k)
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: k, Op: stdio.OpPut, Value: raw, Current: true})
|
||||
}
|
||||
}
|
||||
|
||||
// keyOf is letta's n-th build's key.
|
||||
func keyOf(n int) string { k, _ := builds.BuilderBuildKey("letta", fp(n)); return k }
|
||||
|
||||
// viewKeys are the keys the followed view holds.
|
||||
func viewKeys(t *testing.T, h *Holder) []string {
|
||||
t.Helper()
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
var out []string
|
||||
for k := range h.done {
|
||||
out = append(out, k)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// putBuild writes a build's key and hands it over as the watch's update does.
|
||||
func putBuild(h *Holder, b *memBus, key string, raw []byte) {
|
||||
_ = b.Put(BuildsRead, key, json.RawMessage(raw))
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: key, Op: stdio.OpPut, Value: raw})
|
||||
}
|
||||
|
||||
// ADR 0571 clause 2, 1126/A4/AC2: the keep set from the store's listing, the wants and the five newest builds; with no
|
||||
// lease key, a build's end and a confirmed collect let go of the eligible within the bound, leave an index for a
|
||||
// confirmed collect, record each deletion as a hand act and say artifacts-gone.
|
||||
@@ -371,7 +312,7 @@ func TestTheSeatKeepsTheFiveNewestAndTheWantedAndSweepsWithNoLease(t *testing.T)
|
||||
raw, _ := builds.EncodeBuilderBuild(builds.BuilderBuild{State: builds.BuildDone, Manifest: strings.Repeat("d", 64), Artifacts: []builds.BuiltArtifact{
|
||||
{Name: "image", Kind: "image", Digest: digestOf(imageRef("letta", n)), Reference: artifactref.SeatReference(reg.Seat, "letta", "image", digestOf(imageRef("letta", n)))},
|
||||
{Name: "files", Kind: "archive", Digest: digestOf(archiveRef("letta", n)), Reference: artifactref.SeatReference(ociseat.Seat, "letta", "files", digestOf(archiveRef("letta", n)))}}})
|
||||
putBuild(h, b, key, raw)
|
||||
_ = b.Put(BuildsRead, key, json.RawMessage(raw))
|
||||
}
|
||||
}
|
||||
if c.refuse != "" {
|
||||
@@ -382,7 +323,7 @@ func TestTheSeatKeepsTheFiveNewestAndTheWantedAndSweepsWithNoLease(t *testing.T)
|
||||
}
|
||||
switch c.how {
|
||||
case "build":
|
||||
h.Pass(context.Background(), "after the build letta.x")
|
||||
h.AfterBuild("letta.x")
|
||||
var gone []string
|
||||
for _, x := range door.asked {
|
||||
if r, ok := strings.CutPrefix(x, "let go "); ok {
|
||||
@@ -479,7 +420,6 @@ func TestAWantedImageTheStoreLacksIsAFindingAndImagesNamesWantsAndHaves(t *testi
|
||||
view.L.Repos[repo].Revisions[digest] = true
|
||||
// A restart: a holder made afresh holds no finding of its own, and clears what the writer holds (design 47 G A-32, A-38).
|
||||
h = &Holder{Bus: h.Bus, Findings: h.Findings, Door: h.Door, Listed: h.Listed, Seats: h.Seats, Now: h.Now, Acts: h.Acts, Logf: h.Logf}
|
||||
takeIn(h, b)
|
||||
if err := h.Observe(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -542,154 +482,3 @@ func (f findingsThrough) Held() []meshconditions.Finding {
|
||||
func (f findingsThrough) Clear(_ context.Context, scope, id, kind, _ string) error {
|
||||
return f.bus.Delete(FindingsState, views.FindingKey(scope, id, kind))
|
||||
}
|
||||
|
||||
// writesCounted counts the findings written through the test's writer.
|
||||
type writesCounted struct {
|
||||
findingsThrough
|
||||
writes *int
|
||||
}
|
||||
|
||||
func (w writesCounted) Write(ctx context.Context, f meshconditions.Finding) error {
|
||||
*w.writes++
|
||||
return w.findingsThrough.Write(ctx, f)
|
||||
}
|
||||
|
||||
// Design 47 H, H-A-250: the starts fold into one pending pass, a pass reads the builds in one page, and a list the
|
||||
// registry seats cannot answer ends the pass with artifact-list-unread, written once and asked again only at an event.
|
||||
func TestOnePendingPassReadsTheBuildsOnceAndEndsOnAnUnreadList(t *testing.T) {
|
||||
t.Run("three events during a pass run exactly one more", func(t *testing.T) {
|
||||
h, _, _, _ := aMesh(t)
|
||||
listed, inner := 0, h.Listed
|
||||
h.Listed = func(ctx context.Context) (*View, error) {
|
||||
listed++
|
||||
if listed == 1 {
|
||||
for _, by := range []string{"after the build a", "after the build b", "a want changed"} {
|
||||
h.Wanted(by) // a pass runs: each only marks it
|
||||
}
|
||||
}
|
||||
return inner(ctx)
|
||||
}
|
||||
if !h.mark("after the build letta.x") {
|
||||
t.Fatal("no pass ran, and the mark did not start one")
|
||||
}
|
||||
h.passes("after the build letta.x")
|
||||
if listed != 2 {
|
||||
t.Fatalf("three events during a pass ran %d passes, want 2", listed)
|
||||
}
|
||||
if h.running || h.wanted {
|
||||
t.Fatalf("after the passes running is %v and wanted %v", h.running, h.wanted)
|
||||
}
|
||||
})
|
||||
t.Run("a pass reads nothing of the builds and their ends come from the log", func(t *testing.T) {
|
||||
h, b, door, _ := aMesh(t)
|
||||
handOver(h, b) // a fresh take-in, counted: no history read for a build the log holds (H-A-258)
|
||||
h.TakenIn()
|
||||
h.Pass(context.Background(), "after the build letta.x")
|
||||
if n := b.calls["keys "+BuildsRead] - 1 + b.calls["get "+BuildsRead] - 7 + b.calls["history "+BuildsRead]; n != 0 {
|
||||
t.Fatalf("a take-in and a pass read the builds by %v", b.calls)
|
||||
}
|
||||
if !slices.Contains(door.asked, "let go "+imageRef("letta", 2)) {
|
||||
t.Fatalf("the pass swept nothing: %v", door.asked)
|
||||
}
|
||||
// Only a build the log lacks is read from its history, once.
|
||||
h, b, _, _ = aMesh(t)
|
||||
b.log = append(b.log[:2:2], b.log[3:]...) // letta's 3rd
|
||||
h.Pass(context.Background(), "a")
|
||||
h.Pass(context.Background(), "b")
|
||||
if b.calls["history "+BuildsRead] != 1 {
|
||||
t.Fatalf("two passes read the builds' history %d times, want once for the build the log lacks", b.calls["history "+BuildsRead])
|
||||
}
|
||||
})
|
||||
t.Run("no pass judges before the first view, and says so", func(t *testing.T) {
|
||||
h, b, door, _ := aMesh(t)
|
||||
h.taken = false
|
||||
h.Pass(context.Background(), "after the build letta.x")
|
||||
_, found, _ := b.Get(FindingsState, views.FindingKey("module", Module, BuildsUnread))
|
||||
if len(door.asked) != 0 || !found {
|
||||
t.Fatalf("a pass before the builds were taken in asked %v; artifact-builds-unread is written: %v", door.asked, found)
|
||||
}
|
||||
})
|
||||
t.Run("an unread end holds every pass until it is read", func(t *testing.T) {
|
||||
h, b, door, _ := aMesh(t)
|
||||
writes := 0
|
||||
h.Findings = writesCounted{findingsThrough{b}, &writes}
|
||||
b.log = append(b.log[:2:2], b.log[3:]...) // letta's 3rd: the log lacks it, and its history fails
|
||||
b.historyErr = map[string]error{keyOf(3): errors.New("no responders")}
|
||||
h.Pass(context.Background(), "a")
|
||||
h.Pass(context.Background(), "b")
|
||||
key := views.FindingKey("module", Module, BuildsUnread)
|
||||
if _, found, _ := b.Get(FindingsState, key); !found || writes != 1 || len(door.asked) != 0 {
|
||||
t.Fatalf("with an end unread the passes asked %v; the finding is written %v, %d times", door.asked, found, writes)
|
||||
}
|
||||
b.historyErr = nil
|
||||
h.Pass(context.Background(), "c")
|
||||
if _, found, _ := b.Get(FindingsState, key); found || !slices.Contains(door.asked, "let go "+imageRef("letta", 2)) {
|
||||
t.Fatalf("once the end was read the pass asked %v and left the finding: %v", door.asked, found)
|
||||
}
|
||||
})
|
||||
t.Run("a change heard during the handover is in the view it gives", func(t *testing.T) {
|
||||
h, b, _, _ := aMesh(t)
|
||||
handOver(h, b)
|
||||
raw, _, _ := b.Get(BuildsRead, keyOf(1))
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: keyOf(8), Op: stdio.OpPut, Value: raw}) // after the handover, before TakenIn
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: keyOf(3), Op: stdio.OpDelete})
|
||||
h.TakenIn()
|
||||
got := viewKeys(t, h)
|
||||
if !slices.Contains(got, keyOf(8)) || slices.Contains(got, keyOf(3)) {
|
||||
t.Fatalf("the view after the handover holds %v", got)
|
||||
}
|
||||
})
|
||||
t.Run("a key deleted or no longer done leaves the view; one written done again takes its new end", func(t *testing.T) {
|
||||
h, b, _, _ := aMesh(t)
|
||||
h.Pass(context.Background(), "a") // every end known
|
||||
building, _ := builds.EncodeBuilderBuild(builds.BuilderBuild{State: "building"})
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: keyOf(3), Op: stdio.OpDelete})
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: keyOf(4), Op: stdio.OpPut, Value: building})
|
||||
if got := viewKeys(t, h); slices.Contains(got, keyOf(3)) || slices.Contains(got, keyOf(4)) {
|
||||
t.Fatalf("the view holds %v", got)
|
||||
}
|
||||
later := b.log[len(b.log)-1].At.Add(time.Hour)
|
||||
e := builds.BuildLogEntry{Event: builds.BuildEndedEvent, Module: "letta", FP: fp(5), Repository: "novox/x", Commit: "c", Node: "ace", At: later, Outcome: "done"}
|
||||
entry, _ := builds.EncodeBuildLogEntry(e)
|
||||
b.log = append(b.log, stdio.LogEntry{Sequence: 99, Key: "letta", Value: entry, At: later})
|
||||
raw, _, _ := b.Get(BuildsRead, keyOf(5))
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: keyOf(5), Op: stdio.OpPut, Value: raw})
|
||||
h.Pass(context.Background(), "b")
|
||||
if got, _ := h.Builds(); !slices.ContainsFunc(got, func(x Build) bool { return x.Key == keyOf(5) && x.At.Equal(later) }) {
|
||||
t.Fatalf("letta's 5th written done again does not end at %v: %+v", later, got)
|
||||
}
|
||||
})
|
||||
t.Run("a watch asked again replaces the view whole", func(t *testing.T) {
|
||||
h, b, _, _ := aMesh(t)
|
||||
gone, _ := builds.BuilderBuildKey("letta", fp(3))
|
||||
_ = b.Delete(BuildsRead, gone) // deleted while no watch ran: no change of it is heard
|
||||
takeIn(h, b)
|
||||
got, err := h.Builds()
|
||||
if err != nil || len(got) != 6 || slices.ContainsFunc(got, func(x Build) bool { return x.Key == gone }) {
|
||||
t.Fatalf("after the watch was asked again the view holds %d builds (%v), %s among them", len(got), err, gone)
|
||||
}
|
||||
})
|
||||
t.Run("a list unread ends the pass with its finding once", func(t *testing.T) {
|
||||
h, b, door, _ := aMesh(t)
|
||||
writes, listed, inner := 0, 0, h.Listed
|
||||
h.Findings = writesCounted{findingsThrough{b}, &writes}
|
||||
h.Listed = func(context.Context) (*View, error) {
|
||||
listed++
|
||||
return nil, errors.New("the answer exceeds the bus's one megabyte")
|
||||
}
|
||||
h.Pass(context.Background(), "after the build a")
|
||||
h.Pass(context.Background(), "a want changed")
|
||||
key := views.FindingKey("module", Module, ListUnread)
|
||||
if raw, found, _ := b.Get(FindingsState, key); !found || writes != 1 || !strings.Contains(string(raw), "one megabyte") {
|
||||
t.Fatalf("the finding is %s (found %v), written %d times", raw, found, writes)
|
||||
}
|
||||
if listed != 2 || len(door.asked) != 0 || len(b.published) != 0 {
|
||||
t.Fatalf("two passes listed %d times, asked %v and said %v", listed, door.asked, b.published)
|
||||
}
|
||||
h.Listed = inner
|
||||
h.Pass(context.Background(), "after the build b")
|
||||
if _, found, _ := b.Get(FindingsState, key); found || len(door.asked) == 0 {
|
||||
t.Fatalf("a pass that read the list left the finding (%v) or swept nothing (%v)", found, door.asked)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -79,46 +79,20 @@ type Store struct {
|
||||
Ask func(key string, body any) (json.RawMessage, error)
|
||||
}
|
||||
|
||||
// ListOf asks seat's list, of one repository or, with name empty, of every one. oci-artifact-registry's list is read page by
|
||||
// page from its cursor until it answers none (design 47 H2 §813 (7), §779); container-image-registry's answers whole.
|
||||
// ListOf asks seat's list, of one repository or, with name empty, of every one.
|
||||
func (s *Store) ListOf(seat, name string) ([]reg.Repository, error) {
|
||||
if s == nil || s.Ask == nil {
|
||||
return nil, fmt.Errorf("this process reaches no registry seat")
|
||||
}
|
||||
if seat != ociseat.Seat {
|
||||
raw, err := s.Ask("seat:"+seat+"."+reg.VerbList, reg.ListInput{Name: name})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s's list: %w", seat, err)
|
||||
}
|
||||
var a reg.ListAnswer
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return nil, fmt.Errorf("%s's answer to list: %w", seat, err)
|
||||
}
|
||||
return a.Repositories, nil
|
||||
raw, err := s.Ask("seat:"+seat+"."+reg.VerbList, reg.ListInput{Name: name})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s's list: %w", seat, err)
|
||||
}
|
||||
var out []reg.Repository
|
||||
seen := map[string]bool{}
|
||||
for in := (ociseat.ListInput{Name: name}); ; {
|
||||
raw, err := s.Ask("seat:"+seat+"."+reg.VerbList, in)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s's list: %w", seat, err)
|
||||
}
|
||||
var a struct {
|
||||
Repositories []reg.Repository `json:"repositories"`
|
||||
Next string `json:"next"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return nil, fmt.Errorf("%s's answer to list: %w", seat, err)
|
||||
}
|
||||
out = append(out, a.Repositories...)
|
||||
if a.Next == "" {
|
||||
return out, nil
|
||||
}
|
||||
if seen[a.Next] {
|
||||
return nil, fmt.Errorf("%s's list answered the cursor %q twice", seat, a.Next)
|
||||
}
|
||||
seen[a.Next], in.Next = true, a.Next
|
||||
var a reg.ListAnswer
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return nil, fmt.Errorf("%s's answer to list: %w", seat, err)
|
||||
}
|
||||
return a.Repositories, nil
|
||||
}
|
||||
|
||||
// View reads the store now, through both seats.
|
||||
|
||||
@@ -32,7 +32,7 @@ func main() {
|
||||
h.Acts = meshhandactsclient.Recorder{Ask: stdio.Ask}
|
||||
// The module's one held writer (design 47 G §689, A-16, A-29, A-32): the holder's findings and the reader's go through it.
|
||||
w, err := views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{
|
||||
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{ImageMissing, SweepRefused, ListUnread, BuildsUnread}})
|
||||
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{ImageMissing, SweepRefused}})
|
||||
if err != nil {
|
||||
logf("%v", err)
|
||||
os.Exit(1)
|
||||
|
||||
@@ -92,20 +92,7 @@ func (f *fakeStore) ask(t *testing.T) func(string, any) (json.RawMessage, error)
|
||||
repos = append(repos, reg.Repository{Name: name, Manifests: ms})
|
||||
}
|
||||
}
|
||||
if seat != "oci-artifact-registry" {
|
||||
return json.Marshal(reg.ListAnswer{Repositories: repos})
|
||||
}
|
||||
// oci-artifact-registry pages (design 47 H2 §813 (7)): one repository a page, after the one its cursor names.
|
||||
var page struct{ Next string }
|
||||
_ = json.Unmarshal(raw, &page)
|
||||
for len(repos) > 0 && repos[0].Name <= page.Next {
|
||||
repos = repos[1:]
|
||||
}
|
||||
next := ""
|
||||
if len(repos) > 1 {
|
||||
repos, next = repos[:1], repos[0].Name
|
||||
}
|
||||
return json.Marshal(map[string]any{"repositories": repos, "next": next, "capped": next != ""})
|
||||
return json.Marshal(reg.ListAnswer{Repositories: repos})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,9 +27,6 @@ import (
|
||||
// SweepRefused is the sweep's finding: a seat refused a hold or a remove the sweep asked.
|
||||
const SweepRefused = "sweep-refused"
|
||||
|
||||
// ListUnread is the pass's finding: the registry seats' list could not be read, so the pass ended (design 47 H, H-A-250 (3)).
|
||||
const ListUnread = "artifact-list-unread"
|
||||
|
||||
// canonical is a kept reference in the older form, `artifact-store://<path>`, whichever form it came in; another is as
|
||||
// it was.
|
||||
func canonical(reference string) string {
|
||||
@@ -143,53 +140,6 @@ func (h *Holder) sweepClean() {
|
||||
}
|
||||
}
|
||||
|
||||
// BuildsUnread is the pass's finding: the builder's builds could not be judged, so the pass ended (design 47 H, H-A-260).
|
||||
const BuildsUnread = "artifact-builds-unread"
|
||||
|
||||
// unreadFinding writes a pass's finding of what it could not read (ListUnread, BuildsUnread) once: a pass that fails the
|
||||
// same way while the writer holds it writes nothing more. Nothing asks again until the next event marks a pass.
|
||||
func (h *Holder) unreadFinding(kind string, err error) {
|
||||
for _, was := range h.Findings.Held() {
|
||||
if was.Kind == kind {
|
||||
return
|
||||
}
|
||||
}
|
||||
at := h.now().UTC()
|
||||
f := meshconditions.Finding{Scope: "module", ID: keys.EscapeName(Module), Kind: kind,
|
||||
Subject: meshconditions.Subject{Scope: "module", ID: keys.EscapeName(Module)},
|
||||
Severity: meshconditions.FindingSeverityWarning, Resolver: "operator",
|
||||
Summary: "the artifact sweep stopped: the registry seats' list could not be read (the evidence names why)",
|
||||
Headline: "The artifact sweep cannot read the store's list",
|
||||
Explanation: "The sweep judges what to keep against the store's list, which it asks the registry seats. The list " +
|
||||
"could not be read, so the sweep removed nothing and observed nothing; what it kept stays kept.",
|
||||
Needs: "read the registry seat holder's journal for why its list failed; the next finished build or want change asks again.",
|
||||
Evidence: []meshconditions.Evidence{{At: at, Said: err.Error()}},
|
||||
Source: "CAT-AS1", Observer: Module, At: at}
|
||||
if kind == BuildsUnread {
|
||||
f.Summary = "the artifact sweep stopped: the builder's builds could not be judged (the evidence names why)"
|
||||
f.Headline = "The artifact sweep cannot judge the builds"
|
||||
f.Explanation = "The sweep keeps each module's newest builds and every wanted one, judged from the builder's builds " +
|
||||
"and when each ended. Those could not all be read, so the sweep removed nothing and observed nothing; what " +
|
||||
"it kept stays kept."
|
||||
f.Needs = "read this module's journal for which build's end is unread; the next finished build or want change asks again."
|
||||
}
|
||||
if werr := h.Findings.Write(context.Background(), f); werr != nil {
|
||||
h.logf("the pass's finding %s (%v) could not be written: %v", kind, err, werr)
|
||||
}
|
||||
}
|
||||
|
||||
// readAgain clears a pass's finding of kind, when the held writer holds it, once a pass read what it could not.
|
||||
func (h *Holder) readAgain(kind string) {
|
||||
for _, was := range h.Findings.Held() {
|
||||
if was.Kind != kind {
|
||||
continue
|
||||
}
|
||||
if err := h.Findings.Clear(context.Background(), was.Scope, was.ID, was.Kind, "a pass read it again"); err != nil {
|
||||
h.logf("the pass's finding %s could not be cleared: %v", kind, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// keptWhy is why a kept reference is held, as the seat's hold takes it.
|
||||
func keptWhy(r *Ref) string {
|
||||
if r == nil || len(r.Why) == 0 {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "artifact-store-tools",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"packages": {
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds": "0.1"
|
||||
},
|
||||
@@ -112,10 +111,7 @@
|
||||
{
|
||||
"name": "findings",
|
||||
"storage": "key-value",
|
||||
"template": "state",
|
||||
"options": {
|
||||
"max-mib": 8
|
||||
}
|
||||
"template": "state"
|
||||
},
|
||||
{
|
||||
"name": "finding-events",
|
||||
@@ -128,7 +124,7 @@
|
||||
"findings-stated.*"
|
||||
],
|
||||
"max-age-days": 7,
|
||||
"max-mib": 8
|
||||
"max-mib": 16
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "asus-zephyrus-g14",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"moved": true,
|
||||
"templates": "templates",
|
||||
"capabilities": [
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "audit-logger",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"slug": "audit",
|
||||
"consumes": [
|
||||
"**"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "avahi",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "baserow",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"container-runtime"
|
||||
],
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// blueman's shape (novox/hq ADR 0208, ADR 0210): one official package, no seat, the X display on its
|
||||
@@ -15,16 +14,40 @@ import (
|
||||
// bluetooth module's (bluez, its utilities, its daemon), and the Go bundle serving exactly the listed
|
||||
// blueman_ tools.
|
||||
|
||||
func TestItInstallsTheAppletAndNothingElse(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
type manifest struct {
|
||||
Module string `json:"module"`
|
||||
Version string `json:"version"`
|
||||
Capabilities []string `json:"capabilities"`
|
||||
Requires []string `json:"requires"`
|
||||
Tools []string `json:"tools"`
|
||||
Resources []map[string]any `json:"resources"`
|
||||
Claims []any `json:"claims"`
|
||||
Seats []any `json:"seats"`
|
||||
Shell []any `json:"shell"`
|
||||
Contributions []any `json:"contributions"`
|
||||
Environment any `json:"environment"`
|
||||
Build struct {
|
||||
Artifacts []map[string]any `json:"artifacts"`
|
||||
} `json:"build"`
|
||||
}
|
||||
|
||||
func readManifest(t *testing.T) (manifest, string) {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
dec := json.NewDecoder(strings.NewReader(string(raw)))
|
||||
dec.DisallowUnknownFields()
|
||||
var m manifest
|
||||
if err := dec.Decode(&m); err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
return m, string(raw)
|
||||
}
|
||||
|
||||
func TestItInstallsTheAppletAndNothingElse(t *testing.T) {
|
||||
m, _ := readManifest(t)
|
||||
if m.Module != "blueman" || !reflect.DeepEqual(m.Requires, []string{"x11-display"}) ||
|
||||
!reflect.DeepEqual(m.Capabilities, []string{"package-manager"}) {
|
||||
t.Fatalf("%+v", m)
|
||||
@@ -32,22 +55,13 @@ func TestItInstallsTheAppletAndNothingElse(t *testing.T) {
|
||||
if len(m.Resources) != 1 || m.Resources[0]["type"] != "package" || m.Resources[0]["package"] != packageFor {
|
||||
t.Fatalf("resources: %v", m.Resources)
|
||||
}
|
||||
if m.Claims != nil || m.DefinesSeats != nil || m.Environment != nil {
|
||||
if m.Claims != nil || m.Seats != nil || m.Environment != nil {
|
||||
t.Fatal("it holds no seat and sets no environment")
|
||||
}
|
||||
}
|
||||
|
||||
func TestItAddsNoSecondStartAndDeclaresNothingOfTheBluetoothModule(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
raw := string(manifestJSON)
|
||||
m, raw := readManifest(t)
|
||||
// The package ships its XDG autostart entry, which the session's dex runs: an xinitrc slot or a
|
||||
// window-manager exec would start it twice.
|
||||
if m.Shell != nil || m.Contributions != nil {
|
||||
@@ -61,16 +75,7 @@ func TestItAddsNoSecondStartAndDeclaresNothingOfTheBluetoothModule(t *testing.T)
|
||||
}
|
||||
|
||||
func TestTheToolsAgreeWithTheManifest(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
raw := string(manifestJSON)
|
||||
m, raw := readManifest(t)
|
||||
served := map[string]bool{}
|
||||
for _, tool := range tools() {
|
||||
served[tool.Name] = true
|
||||
@@ -91,8 +96,8 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) {
|
||||
t.Fatalf("%v", m.Build.Artifacts)
|
||||
}
|
||||
b := m.Build.Artifacts[0]
|
||||
if b.Kind != "bundle" || b.Language != "go" || b.System != "arch" ||
|
||||
b.From != "cmd/blueman-tools" || b.Binary != "blueman-tools" {
|
||||
if b["kind"] != "bundle" || b["language"] != "go" || b["system"] != "arch" ||
|
||||
b["from"] != "cmd/blueman-tools" || b["binary"] != "blueman-tools" {
|
||||
t.Errorf("the Go tools bundle: %v", b)
|
||||
}
|
||||
s := strings.ToLower(raw)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "blueman",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "bluetooth",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "brother",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"requires": [
|
||||
{
|
||||
"name": "s3-bucket",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "buildah",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"user-namespaces"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "ca-trust",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"slug": "catrust",
|
||||
"capabilities": [
|
||||
"service-manager"
|
||||
|
||||
@@ -28,7 +28,8 @@ And, for where the agent runs as an account of its own (hq ADR 0266):
|
||||
rule and can read no secret the mesh placed for another account. A way found is this module's condition,
|
||||
and the controller's self-check says it urgently.
|
||||
- `/usr/local/bin/claude-agent`, root's, `0755`: starts the agent as that account — `claude` itself when run
|
||||
as it, otherwise `sudo -iu <account> claude` through the operator's own sudo.
|
||||
as it, otherwise `sudo -iu <account> claude` through the operator's own sudo, carrying the guard's
|
||||
override only when it is set in the operator's shell.
|
||||
- `/etc/claude-code/agent-account`, root's, `0644`: the name of that account, the machine fact
|
||||
`agent-account`, for the shell to read — the controller fills no machine fact into shell code.
|
||||
- **`claude` in the operator's interactive zsh is `claude-agent`** where the machine names an agent account:
|
||||
@@ -47,7 +48,7 @@ whenever the node's tool runtime collects the module's tools:
|
||||
| `managed-mcp.json` | the tool servers every session loads: the mesh's console as `mesh` (with a `timeout` of 660000 ms, mesh_wait's longest wait and a minute, so the agent's one-minute default does not cut a wait, issue 1871), and the servers set in this module's `mcp_servers` setting. **Exclusive**: a server not listed here does not load — not one added with `claude mcp add`, not a project's `.mcp.json`, not a plugin's |
|
||||
| `managed-settings.json` | the keys set in this module's `managed_settings` setting — at the controller's terminal alone (hq issue 340) — under the mesh's own keys: the repositories' attribution convention, the claude.ai connectors kept beside the managed servers, the key-helper while the node holds an API-key licence, git's credential file while the node holds the forge's read credential (below), and the two that name the `nox-mesh` marketplace and enable its plugin, and `CLAUDE_CODE_MCP_TOOL_IDLE_TIMEOUT` (660000) in `env` beside the operator's own, the mesh's key winning |
|
||||
| `CLAUDE.md` | how a session on this mesh works, this node's name and role, the conventions, the **"instead of" table** — then the instruction sections registered for every node and for this one |
|
||||
| `marketplace/` | the `nox-mesh` plugin (hq ADR 0216): the skills, subagents, commands and output styles registered for every node and for this one, offered in a session as `nox-mesh:<name>` — and this module's binary (`guard/guard`, its path kept so the home-fs helper's path does not move) run by the mesh's own SessionStart and Stop hooks, the plugin's only hooks (hq issue 340, ADR 0489). No PreToolUse hook: the guard on the agent's shell is removed (the operator, 2026-11-20). Replaced whole, staged beside and swapped in |
|
||||
| `marketplace/` | the `nox-mesh` plugin (hq ADR 0216): the skills, subagents, commands and output styles registered for every node and for this one, offered in a session as `nox-mesh:<name>` — and the **guard on the agent's shell** (`guard/`), the mesh's own hook and the plugin's only one (hq issue 340). Replaced whole, staged beside and swapped in |
|
||||
|
||||
Under the operator's home: `~/.claude/.credentials.json`, only when the licence manager hands this node a
|
||||
subscription token; and what is registered at the **home** scope for this node — a skill, subagent,
|
||||
@@ -125,15 +126,31 @@ The agent kept reaching for `ssh <machine> journalctl` while the service manager
|
||||
answered this is not placed) — at start, every ten minutes and on `claude_code_render`, keeps it in its
|
||||
state (`mesh-tools.json`), and renders it into `CLAUDE.md` as one row per seat or module: `<node>/<seat>.` and
|
||||
each verb with the commands it replaces. Generated, never written by hand: a verb that gains `replaces` is in
|
||||
the next render. Ordered by the machines' seats, the modules, the
|
||||
the next render. Ordered by what the guard here refused most, then the machines' seats, the modules, the
|
||||
mesh's seats; at most sixteen rows, the rest one `mesh_search` away.
|
||||
The rule under the table (never `ssh` to a mesh machine, never edit `/etc/hosts` or set `HOSTALIASES` for a
|
||||
mesh name) is the session's own: nothing on the agent's shell enforces it since the guard was removed (the
|
||||
operator, 2026-11-20).
|
||||
- **The guard.** A PreToolUse hook on every tool, judging `Bash`, `Edit`, `Write`, `MultiEdit` and `NotebookEdit` and letting the rest through (but see the account rule below): this module's own
|
||||
binary (`claude-code guard`), copied root's into the plugin with what it judges with (`guard.json`: the
|
||||
machines by name, domain and address, and the replaced commands), run by its path under `/etc/claude-code` so
|
||||
the session cannot change it. It refuses `ssh`, `scp`, `sftp`, `rsync`, `mosh` and `autossh` to a mesh machine
|
||||
(any `*.internal` name, a machine's name or a name under its domains, one of its addresses, as `ssh -G` reads
|
||||
the destination; a jump through one too), writing `/etc/hosts` or `/etc/resolv.conf`, and `HOSTALIASES` —
|
||||
naming the verb that does the job on that machine when one says it replaces the command, and otherwise that a
|
||||
missing tool is created in the module that owns it, never worked around. **Stated, not silent:** an ssh login
|
||||
as `git` is the forge's account, which runs nothing but git, and passes; a git remote is never an ssh command
|
||||
line anyway.
|
||||
- **The operator's override**: `MESH_GUARD_OVERRIDE=<why>`, exported in the operator's own shell before the
|
||||
session starts. It is read from the session's environment as the kernel kept it at exec
|
||||
(`/proc/<pid>/environ` of the agent's process), so nothing a session does — a command's `export`, a
|
||||
settings `env` key — can set it, and a command naming it is refused outright. Every override and every
|
||||
refusal is a line in the module's `guard.log`; an override that cannot be recorded is not honoured.
|
||||
`claude_code_guard` shows the rules, the data and the record.
|
||||
|
||||
It is a guard against the habit, not a sandbox: a command written to hide what it runs can hide it, and the
|
||||
record is how a habit that found a way round is seen.
|
||||
|
||||
## Tools
|
||||
|
||||
`claude_code_status`, `claude_code_render`, `claude_code_pull` (asks the vault for this node's
|
||||
`claude_code_status`, `claude_code_render`, `claude_code_guard`, `claude_code_pull` (asks the vault for this node's
|
||||
licence token now), `claude_code_grant` (for the licence manager), `claude_code_mcp_list`, `claude_code_mcp_unregister` (this node by default; `nodes: "all"` or a list for
|
||||
more).
|
||||
|
||||
@@ -322,6 +339,14 @@ applied only while the agent's account holds it — and what this module placed
|
||||
goes: the access-token-only credentials file (one holding a refresh token is a login the person made, and
|
||||
stays, said), the home-scope items unless changed by hand, the key kept in the state.
|
||||
|
||||
**The guard there refuses every tool call of a session that does not run as the agent's account** —
|
||||
shell, file edits, reads and the mesh's tools alike — and no override lifts it: the operator's account may become root, and an
|
||||
agent started in it by habit must do nothing. Its record goes to the machine's journal
|
||||
(`journalctl -t mesh-claude-code-guard`), which the session's account may add to and not change;
|
||||
`claude_code_guard` reads it there, keeping only the lines the journal says the guard's own binary wrote. That
|
||||
is not proof: the account can run the guard's binary itself and add lines in the guard's words. What it
|
||||
cannot do is remove one.
|
||||
|
||||
**An API key is taken only from the agent's own home.** `claude_code_add_api_key` reads a file and removes it,
|
||||
and the runtime runs as the operator: a path anywhere would hand whoever calls it the runtime's credential or
|
||||
the operator's keys. So the file must lie under the home of the account the agent runs as, reached through no
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package main
|
||||
|
||||
// Where the agent runs as an account of its own (novox/hq ADR 0266): the home touched through the helper, the
|
||||
// licence that follows the account, and what goes from the operator's home.
|
||||
// licence that follows the account, what goes from the operator's home, and the guard that refuses a session
|
||||
// running as anyone else.
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -331,6 +333,82 @@ func TestNothingGoesWhereTheAgentRunsAsTheOperator(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- the guard --------------------------------------------------------------------------------------------
|
||||
|
||||
func TestASessionNotRunningAsTheAgentsAccountDoesNothing(t *testing.T) {
|
||||
g := Guard{Data: GuardData{Node: "anchor", Agent: "agent"}}
|
||||
echo := HookInput{ToolName: "Bash", ToolInput: map[string]any{"command": "echo hello"}}
|
||||
v := g.JudgeAs("operator", echo)
|
||||
if !v.Refuse || v.Rule != "account" || v.Overridable || !strings.Contains(v.Message, "claude-agent") {
|
||||
t.Fatalf("a session as the operator is refused whatever it runs, with no override: %+v", v)
|
||||
}
|
||||
if v := g.JudgeAs("operator", HookInput{ToolName: "Write", ToolInput: map[string]any{"file_path": "/tmp/x"}}); !v.Refuse {
|
||||
t.Fatal("a file edit too")
|
||||
}
|
||||
if v := g.JudgeAs("agent", echo); v.Refuse {
|
||||
t.Fatalf("the agent's own account runs as before: %+v", v)
|
||||
}
|
||||
if v := (Guard{Data: GuardData{Node: "laptop"}}).JudgeAs("operator", echo); v.Refuse {
|
||||
t.Fatal("where agents run as the operator, the operator's session runs as before")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhereTheAgentHasItsOwnAccountTheRecordIsTheJournal(t *testing.T) {
|
||||
p, _ := node(t, "anchor")
|
||||
if d := GuardDataOf(p, nil); d.Agent != "" || strings.HasPrefix(d.Log, JournalRecord) {
|
||||
t.Fatalf("as the operator: the module's own file; got %+v", d)
|
||||
}
|
||||
p.Agent, p.Operator = "agent", "operator"
|
||||
d := GuardDataOf(p, nil)
|
||||
if d.Agent != "agent" || d.Log != JournalRecord+GuardJournalID {
|
||||
t.Fatalf("as an account of its own: the journal, and the account named; got %+v", d)
|
||||
}
|
||||
|
||||
socket := filepath.Join(t.TempDir(), "journal")
|
||||
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: socket, Net: "unixgram"})
|
||||
if err != nil {
|
||||
t.Skipf("no unix datagram socket here: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
was := journalSocket
|
||||
journalSocket = socket
|
||||
t.Cleanup(func() { journalSocket = was })
|
||||
if err := RecordGuard(d.Log, GuardEntry{Decision: "refused", Rule: "account", Tool: "Bash", Command: "a\nb"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
buf := make([]byte, 4096)
|
||||
n, _, err := conn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lines := strings.Split(strings.TrimSuffix(string(buf[:n]), "\n"), "\n")
|
||||
if len(lines) != 3 || lines[0] != "SYSLOG_IDENTIFIER="+GuardJournalID || !strings.HasPrefix(lines[2], "MESSAGE={") {
|
||||
t.Fatalf("one line per field, the entry as one line of JSON: %q", lines)
|
||||
}
|
||||
|
||||
readWas := readJournal
|
||||
t.Cleanup(func() { readJournal = readWas })
|
||||
readJournal = func(id string, last int) ([]byte, error) {
|
||||
if id != GuardJournalID {
|
||||
t.Fatalf("read %s", id)
|
||||
}
|
||||
msg, _ := json.Marshal(strings.TrimPrefix(lines[2], "MESSAGE="))
|
||||
forged, _ := json.Marshal(`{"decision":"overridden","rule":"ssh","tool":"Bash","command":"forged"}`)
|
||||
return []byte(`{"MESSAGE":` + string(msg) + `,"_EXE":"` + HelperPath() + `"}` + "\n" +
|
||||
`{"MESSAGE":` + string(forged) + `,"_EXE":"/usr/bin/logger"}` + "\n"), nil
|
||||
}
|
||||
if got := ReadGuardLog(d.Log, 10); len(got) != 1 || got[0].Rule != "account" || got[0].Command != "a\nb" {
|
||||
t.Fatalf("the record reads back from the journal: %+v", got)
|
||||
}
|
||||
if err := RecordGuard(JournalRecord+"x", GuardEntry{}); err != nil && !strings.Contains(err.Error(), "journal") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
journalSocket = filepath.Join(t.TempDir(), "absent")
|
||||
if err := RecordGuard(d.Log, GuardEntry{}); err == nil {
|
||||
t.Fatal("a journal that cannot be reached is an error, so an override is not honoured unrecorded")
|
||||
}
|
||||
}
|
||||
|
||||
// ---- an API key is taken only from the agent's own home ---------------------------------------------------
|
||||
|
||||
func TestAnAPIKeyFileOutsideTheAgentsHomeIsRefusedAndKept(t *testing.T) {
|
||||
@@ -361,6 +439,21 @@ func TestAnAPIKeyFileOutsideTheAgentsHomeIsRefusedAndKept(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheGuardIsAskedAboutEveryToolAndRefusesEachForAnotherAccount(t *testing.T) {
|
||||
if GuardMatcher != "*" {
|
||||
t.Fatalf("the guard is asked about every tool, got %q", GuardMatcher)
|
||||
}
|
||||
g := Guard{Data: GuardData{Node: "anchor", Agent: "agent"}}
|
||||
for _, tool := range []string{"Read", "Grep", "WebFetch", "mcp__mesh__mesh_call", "Task"} {
|
||||
if v := g.JudgeAs("operator", HookInput{ToolName: tool}); !v.Refuse {
|
||||
t.Fatalf("%s as another account is refused", tool)
|
||||
}
|
||||
if v := g.JudgeAs("agent", HookInput{ToolName: tool}); v.Refuse {
|
||||
t.Fatalf("%s as the agent's account passes", tool)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAMoveWritesTheAccountFileBesideTheToken(t *testing.T) {
|
||||
p, w := node(t, "anchor")
|
||||
writeFile(t, p.binding(), `{"licence":"personal","kind":"subscription","generation":4}`)
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
)
|
||||
|
||||
@@ -359,128 +358,18 @@ func TestAnAppliedBindingSaysWhyAndWho(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ADR 0206, ADR 0209, design 39 §6 "the latest login wins": a login in the file that was never handed to the manager
|
||||
// is newer than every lease, made while the tool runner was stopped (the start's first view) or while it runs (a
|
||||
// binding or the vault's change heard before the manager asked claude_code_grant). No path writes over it; once
|
||||
// claude_code_grant handed it, its successor replaces it and leaves no refresh token, and a file holding no login is
|
||||
// restored as before.
|
||||
func TestALoginNotHandedIsNeverWrittenOverAndItsSuccessorReplacesIt(t *testing.T) {
|
||||
login := `{"claudeAiOauth":{"accessToken":"at-login","refreshToken":"rt-login","expiresAt":` +
|
||||
strings.TrimSpace(string(mustJSON(now+7_200_000))) + `,"refreshTokenExpiresAt":9000},"other":1}`
|
||||
lease := `{"claudeAiOauth":{"accessToken":"at-lease","expiresAt":1},"other":1}`
|
||||
for _, c := range []struct {
|
||||
name, file string
|
||||
handed bool
|
||||
offeredRTE int64 // the refresh token expiry the grant offered carries, 0 for none
|
||||
offeredExp int64 // its access expiry, 0 for an hour from now
|
||||
heard func(p Paths, ask Ask, w WriteManaged) (string, error)
|
||||
want string // the access token the file holds after
|
||||
account string // the email address the agent's account file names, "" for none
|
||||
reason string // what the journal line says, "" for nothing
|
||||
}{
|
||||
{"the start's first view, a login made while stopped", login, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return FirstBinding(p, ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"binding.changed heard while it runs, before the grant was asked", login, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"the vault's change heard while it runs, before the grant was asked", login, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnSecretChanged(p, []byte(`{"name":"licence.personal.access","generation":5}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"a login handed, met by its refresh: same lineage, later access expiry (H-A-139)", login, true, 9000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-new", "", ""},
|
||||
{"a login handed, met by a newer lineage (H-A-138)", login, true, 200_000_000, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-new", "", ""},
|
||||
{"a login handed, met by the same lineage with an earlier access expiry (H-A-139)", login, true, 9000, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"a login handed, met by the same lineage with an equal access expiry (H-A-139)", login, true, 9000, now + 7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"a login handed, met by an older lineage (H-A-138)", login, true, -100_000_000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"a login handed, met by a lease with no lineage mark (H-A-138)", login, true, 0, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"not handed, the same account, a lease that succeeds it: the latest login wins (H-A-140)", login, false, 9000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-new", "personal", "login superseded"},
|
||||
{"not handed, the same account, a lease older than it (H-A-140)", login, false, 9000, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "personal", ""},
|
||||
{"not handed, another account, a newer lease (H-A-140)", login, false, 200_000_000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "someone@example.org", ""},
|
||||
{"a file holding the lease, older than the one handed", lease, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return FirstBinding(p, ask, w)
|
||||
}, "at-new", "", ""},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
p, w := node(t, "laptop")
|
||||
writeFile(t, p.binding(), `{"licence":"personal","key":"personal","kind":"subscription","generation":2}`+"\n")
|
||||
writeFile(t, p.credentials(), c.file)
|
||||
if c.account != "" {
|
||||
writeFile(t, p.account(), `{"oauthAccount":{"accountUuid":"u-1","emailAddress":"`+c.account+`"}}`)
|
||||
}
|
||||
if c.handed {
|
||||
manager, _ := GenerateKeyPair()
|
||||
if a, err := GrantFor(p, manager.PublicKey); err != nil || a.Sealed == nil {
|
||||
t.Fatalf("the grant: %+v %v", a, err)
|
||||
}
|
||||
}
|
||||
var asked []string
|
||||
current := seat(t, "personal", "at-new", 9, &asked)
|
||||
ask := func(address string, args any) (json.RawMessage, error) {
|
||||
if address == SeatVerb("bindings") {
|
||||
return json.RawMessage(`[{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}]`), nil
|
||||
}
|
||||
if address == VaultGet && (c.offeredRTE != 0 || c.offeredExp != 0) {
|
||||
exp := c.offeredExp
|
||||
if exp == 0 {
|
||||
exp = now + 3_600_000
|
||||
}
|
||||
g := Grant{AccessToken: "at-new", ExpiresAt: exp}
|
||||
if c.offeredRTE != 0 {
|
||||
g.RefreshTokenExpiresAt = &c.offeredRTE
|
||||
}
|
||||
gj, _ := json.Marshal(g)
|
||||
box, err := Seal(string(gj), args.(map[string]any)["public_key"].(string))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return json.Marshal(map[string]any{"name": args.(map[string]any)["name"], "generation": 1, "sealed": box})
|
||||
}
|
||||
return current(address, args)
|
||||
}
|
||||
before, _ := os.ReadFile(p.credentials())
|
||||
line, err := c.heard(p, ask, writer(w))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(line, c.reason) {
|
||||
t.Fatalf("the line %s lacks %q", line, c.reason)
|
||||
}
|
||||
got := creds(t, p)
|
||||
if got["accessToken"] != c.want {
|
||||
t.Fatalf("the file holds %v, want %s", got["accessToken"], c.want)
|
||||
}
|
||||
after, _ := os.ReadFile(p.credentials())
|
||||
if c.want == "at-login" {
|
||||
if string(after) != string(before) || !HoldingsOf(p).Refresh.Present {
|
||||
t.Fatalf("a login not handed was changed: %s", after)
|
||||
}
|
||||
return
|
||||
}
|
||||
// Restored: no refresh token survives (design 14, design 36 §5), the CLI's other keys stay.
|
||||
var whole map[string]any
|
||||
_ = json.Unmarshal(after, &whole)
|
||||
if got["refreshToken"] != nil || HoldingsOf(p).Refresh.Present || whole["other"] == nil {
|
||||
t.Fatalf("restored %s", after)
|
||||
}
|
||||
})
|
||||
func TestTheTokenANodeIsHandedReplacesALoginsGrantAndLeavesNoRefreshToken(t *testing.T) {
|
||||
p, w := node(t, "laptop")
|
||||
writeFile(t, p.credentials(), `{"claudeAiOauth":{"accessToken":"at-old","refreshToken":"rt-spent","expiresAt":`+
|
||||
strings.TrimSpace(string(mustJSON(now+7_200_000)))+`}}`)
|
||||
var asked []string
|
||||
out, err := Pull(p, seat(t, "personal", "at-new", 1, &asked), writer(w))
|
||||
if err != nil || out["applied"] != true {
|
||||
t.Fatalf("%v %v", out, err)
|
||||
}
|
||||
c := creds(t, p)
|
||||
if c["accessToken"] != "at-new" || c["refreshToken"] != nil || HoldingsOf(p).Refresh.Present {
|
||||
t.Fatalf("%v", c)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -729,10 +618,7 @@ func TestTheFirstViewOfTheBindingIsApplied(t *testing.T) {
|
||||
Reads []string
|
||||
}
|
||||
_ = json.Unmarshal(raw, &m)
|
||||
// The manager says each change as binding.changed.<consumer> (design 47 G A-134), and the bus consumer this module's
|
||||
// consumes is composed into hands it only what an entry names, so an entry naming binding.changed alone hears none.
|
||||
hears := slices.ContainsFunc(m.Consumes, func(c string) bool { return stdio.TopicMatches(c, BindingChanged+".g14") })
|
||||
if !hears || !slices.Contains(m.Consumes, SecretChanged) ||
|
||||
if !slices.Contains(m.Consumes, BindingChanged) || !slices.Contains(m.Consumes, SecretChanged) ||
|
||||
!slices.Contains(m.Invokes, SeatVerb("bindings")) || !slices.Contains(m.Invokes, VaultGet) || len(m.Reads) != 0 {
|
||||
t.Fatalf("the manifest consumes %v, invokes %v, reads %v", m.Consumes, m.Invokes, m.Reads)
|
||||
}
|
||||
|
||||
@@ -472,27 +472,40 @@ type PluginFile struct {
|
||||
From string `json:",omitempty"`
|
||||
}
|
||||
|
||||
// GuardDir is where this module's binary lives in the plugin, run by the SessionStart and Stop hooks and as the
|
||||
// home-fs helper (novox/hq ADR 0266). Outside hooks/, so no registered hook's name can be it. Its path is kept so the
|
||||
// helper's does not move; the guard on the agent's shell that once ran from it is removed (the operator,
|
||||
// 2026-11-20).
|
||||
// GuardDir is where the guard on the agent's shell lives in the plugin (novox/hq ADR 0245): its binary and
|
||||
// what it is given. Outside hooks/, so no registered hook's name can be it.
|
||||
const GuardDir = "guard"
|
||||
|
||||
// HookFiles are what the plugin carries to run its hooks: the binary to copy and the waits directory the
|
||||
// SessionStart and Stop hooks are given (novox/hq ADR 0489, issues 1744, 1743). Empty WaitsDir places no hook.
|
||||
type HookFiles struct {
|
||||
// GuardFiles are the guard as the plugin carries it: the binary to copy, what it judges with, and — the same
|
||||
// binary again — the waits directory the SessionStart and Stop hooks are given (novox/hq ADR 0489, issues
|
||||
// 1744, 1743). Empty WaitsDir places no such hook.
|
||||
type GuardFiles struct {
|
||||
Exe string
|
||||
Data GuardData
|
||||
WaitsDir string
|
||||
}
|
||||
|
||||
// SessionStartCommand is the plugin's SessionStart hook: this module's binary, told the session's open
|
||||
// GuardMatcher is what the guard is asked about: every tool. Its own rules judge the shell and the agent's file
|
||||
// edits and let every other tool through; the rule of the account agents run as (novox/hq ADR 0266) refuses
|
||||
// every tool of a session running as another, reading and the mesh's tools included, so that session can do
|
||||
// nothing at all.
|
||||
const GuardMatcher = "*"
|
||||
|
||||
// GuardCommand is the hook's command line: by its path under the managed directory, root's — never the
|
||||
// copy the agent may keep of a plugin, which the session could change.
|
||||
func GuardCommand() string {
|
||||
dir := filepath.Join(ManagedDir, MarketplaceDir, "plugins", Plugin, GuardDir)
|
||||
return `"` + filepath.Join(dir, "guard") + `" guard "` + filepath.Join(dir, "guard.json") + `"`
|
||||
}
|
||||
|
||||
// SessionStartCommand is the plugin's SessionStart hook: the guard's own binary, told the session's open
|
||||
// waits live under waitsDir (novox/hq ADR 0489 §3, issue 1744).
|
||||
func SessionStartCommand(waitsDir string) string {
|
||||
dir := filepath.Join(ManagedDir, MarketplaceDir, "plugins", Plugin, GuardDir)
|
||||
return `"` + filepath.Join(dir, "guard") + `" session-start "` + waitsDir + `"`
|
||||
}
|
||||
|
||||
// StopCommand is the plugin's Stop hook: this module's binary, refusing to end the turn while a wait under
|
||||
// StopCommand is the plugin's Stop hook: the guard's own binary, refusing to end the turn while a wait under
|
||||
// waitsDir is open and unwatched, or past its bound and untold (novox/hq ADR 0489 §3-§6, issue 1743).
|
||||
func StopCommand(waitsDir string) string {
|
||||
dir := filepath.Join(ManagedDir, MarketplaceDir, "plugins", Plugin, GuardDir)
|
||||
@@ -503,9 +516,9 @@ func StopCommand(waitsDir string) string {
|
||||
// over a mesh item of the same kind and name: the node's wins.
|
||||
func Marketplace(c Config) map[string]PluginFile { return MarketplaceWith(c, nil) }
|
||||
|
||||
// MarketplaceWith is the marketplace with the binary the plugin's hooks run, when there is one to place, and its
|
||||
// SessionStart and Stop hooks. No PreToolUse hook: the plugin judges no tool call.
|
||||
func MarketplaceWith(c Config, guard *HookFiles) map[string]PluginFile {
|
||||
// MarketplaceWith is the marketplace with the guard on the agent's shell, when there is one to place: its
|
||||
// hook first, before any registered one.
|
||||
func MarketplaceWith(c Config, guard *GuardFiles) map[string]PluginFile {
|
||||
root := "plugins/" + Plugin + "/"
|
||||
out := map[string]PluginFile{
|
||||
".claude-plugin/marketplace.json": {Content: jsonFile(map[string]any{
|
||||
@@ -534,6 +547,9 @@ func MarketplaceWith(c Config, guard *HookFiles) map[string]PluginFile {
|
||||
hooks := map[string][]any{}
|
||||
if guard != nil && guard.Exe != "" {
|
||||
out[root+GuardDir+"/guard"] = PluginFile{From: guard.Exe, Executable: true}
|
||||
out[root+GuardDir+"/guard.json"] = PluginFile{Content: jsonFile(guard.Data)}
|
||||
hooks["PreToolUse"] = append(hooks["PreToolUse"], map[string]any{"matcher": GuardMatcher,
|
||||
"hooks": []any{map[string]any{"type": "command", "command": GuardCommand(), "timeout": 15}}})
|
||||
if guard.WaitsDir != "" {
|
||||
// A session's wait list (novox/hq ADR 0489): the SessionStart hook names the session's id and its open
|
||||
// waits (issue 1744); the Stop hook keeps the turn open while one is unwatched or untold (issue 1743).
|
||||
@@ -558,7 +574,7 @@ func MarketplaceWith(c Config, guard *HookFiles) map[string]PluginFile {
|
||||
out[root+"output-styles/"+it.Name+".md"] = PluginFile{Content: it.Files[it.Name+".md"]}
|
||||
}
|
||||
// A hook is never rendered from a registration (novox/hq issue 340): the plugin's hooks are the mesh's own
|
||||
// SessionStart and Stop hooks alone, and any other is the operator's, in the managed settings set at the controller's terminal.
|
||||
// guard alone, and any other is the operator's, in the managed settings set at the controller's terminal.
|
||||
}
|
||||
if len(hooks) > 0 {
|
||||
out[root+"hooks/hooks.json"] = PluginFile{Content: jsonFile(map[string]any{"hooks": hooks})}
|
||||
|
||||
@@ -281,7 +281,7 @@ func TestTheManifestListsEveryToolServed(t *testing.T) {
|
||||
// notice goes out on the operator channel's notify (design 47 G GR8).
|
||||
if !slices.Equal(m.Invokes, []string{"seat:mesh-hand-acts.warranted", "seat:mesh-hand-acts.hand-act", "seat:mesh-catalog.seat-definitions", RevisionVerb, RegistrationsVerb, SourceVerb, AssignmentsTool, SeatVerb("bindings"), SeatVerb("current"), VaultGet, "seat:operator-channel.notify"}) ||
|
||||
len(m.Reads) != 0 ||
|
||||
!slices.Equal(m.Consumes, []string{"mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", "mesh-delivery.source-moved", "mesh-delivery-assigner.views.assignments", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", "mesh-conditions.findings-asked", HoldingsAsked, BindingChanged + ".*", SecretChanged}) {
|
||||
!slices.Equal(m.Consumes, []string{"mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", "mesh-delivery.source-moved", "mesh-delivery-assigner.views.assignments", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", "mesh-conditions.findings-asked", HoldingsAsked, BindingChanged, SecretChanged}) {
|
||||
t.Errorf("invokes = %v, reads = %v, consumes = %v", m.Invokes, m.Reads, m.Consumes)
|
||||
}
|
||||
if src, _ := os.ReadFile("proposals.go"); !strings.Contains(string(src), `"seat:mesh-hand-acts.warranted"`) ||
|
||||
|
||||
@@ -122,36 +122,6 @@ func HoldsLogin(c Credentials) bool {
|
||||
return rt != ""
|
||||
}
|
||||
|
||||
// LoginKept is the reason a grant is not written over a login (H-A-138).
|
||||
const LoginKept = "login kept"
|
||||
|
||||
// Succeeds says the offered grant replaces the login the file holds (H-A-138, H-A-139): its lineage is newer
|
||||
// (its refresh token's expiry later than the login's, beyond one generation), or it is of the same lineage
|
||||
// (sameGeneration) and its access token expires later than the login's, which only the manager's refresh of
|
||||
// that login yields. No lineage mark, an older lineage, or the same lineage with no later access expiry is the
|
||||
// older lease.
|
||||
func Succeeds(login Credentials, offered Grant) bool {
|
||||
if offered.RefreshTokenExpiresAt == nil {
|
||||
return false
|
||||
}
|
||||
held := GrantOf(login)
|
||||
if held == nil || held.RefreshTokenExpiresAt == nil {
|
||||
return true
|
||||
}
|
||||
if sameGeneration(held.RefreshTokenExpiresAt, offered.RefreshTokenExpiresAt) {
|
||||
return offered.ExpiresAt > held.ExpiresAt
|
||||
}
|
||||
return *offered.RefreshTokenExpiresAt > *held.RefreshTokenExpiresAt
|
||||
}
|
||||
|
||||
// LoginNotHanded says the file holds a login that was never handed to the manager: handed is the fingerprint
|
||||
// of the refresh token claude_code_grant last sealed, "" when none was. Such a login is newer than anything the
|
||||
// manager holds, so no grant may overwrite it before the manager adopts it (ADR 0206, ADR 0209).
|
||||
func LoginNotHanded(c Credentials, handed string) bool {
|
||||
rt := RefreshTokenOf(c)
|
||||
return rt != "" && Fingerprint(rt) != handed
|
||||
}
|
||||
|
||||
// RefreshTokenOf is the refresh token a login left, or "".
|
||||
func RefreshTokenOf(c Credentials) string {
|
||||
rt, _ := c.oauth()["refreshToken"].(string)
|
||||
|
||||
@@ -0,0 +1,811 @@
|
||||
package main
|
||||
|
||||
// The guard on the agent's shell (novox/hq ADR 0245): a PreToolUse hook every session on a machine of the
|
||||
// mesh runs before a shell command or a file edit, delivered in the module's plugin.
|
||||
//
|
||||
// It refuses three work-arounds, each with the mesh tool that does the job when one says it replaces the
|
||||
// command, and otherwise with the rule that a missing tool is created, never worked around:
|
||||
//
|
||||
// 1. **ssh to a mesh machine** — `ssh`, `scp`, `sftp`, `rsync`, `mosh` or `autossh` to a machine's name, a
|
||||
// name under its domains, any `*.internal` name, or one of its addresses; a jump through one too. The
|
||||
// destination is read as ssh itself reads it (`ssh -G`), so an alias in ~/.ssh/config is no way round.
|
||||
// **Stated, not silent**: an ssh login as `git` is the forge's account, which runs git and nothing
|
||||
// else — so `git push` over ssh, and `ssh -T git@<forge>`, reach no machine and pass. A git remote is
|
||||
// never an ssh command line anyway.
|
||||
// 2. **writing /etc/hosts or /etc/resolv.conf** — by redirect, tee, sed -i, an editor, cp/mv/install onto
|
||||
// it, or the agent's own Edit and Write tools: a mesh name is the mesh's resolvers' (ADR 0194), and a
|
||||
// machine's hosts file is its node-hostname seat's.
|
||||
// 3. **HOSTALIASES**, named anywhere in a command line.
|
||||
//
|
||||
// And it refuses any command that names the operator's override, so the agent never sets it (guard_run.go).
|
||||
//
|
||||
// The matching is on the command line as a shell would split it — quotes, separators, `$(…)`, `bash -c` —
|
||||
// and wrappers such as sudo, env and timeout are looked through. It is a guard against the habit of reaching
|
||||
// for ssh, not a sandbox: a command built to hide what it runs can hide it, and the record of what was
|
||||
// refused (the module's `guard.log`, read by `claude_code_guard`) is how a habit that found a way round is seen.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GuardData is what the hook is given: this machine, the mesh's machines, what replaces what, and where
|
||||
// its record is kept. Written beside the hook on every render, root's, so the session cannot change it.
|
||||
type GuardData struct {
|
||||
Node string `json:"node"`
|
||||
Machines []Machine `json:"machines"`
|
||||
Replacements []Replacement `json:"replacements"`
|
||||
Log string `json:"log"`
|
||||
// Agent is the account every session here must run as, where the machine names an agent account of its
|
||||
// own (novox/hq ADR 0266); empty where agents run as the operator.
|
||||
Agent string `json:"agent,omitempty"`
|
||||
}
|
||||
|
||||
// HookInput is what the agent hands a PreToolUse hook.
|
||||
type HookInput struct {
|
||||
ToolName string `json:"tool_name"`
|
||||
ToolInput map[string]any `json:"tool_input"`
|
||||
Cwd string `json:"cwd"`
|
||||
SessionID string `json:"session_id"`
|
||||
}
|
||||
|
||||
// Verdict is the guard's answer.
|
||||
type Verdict struct {
|
||||
Refuse bool `json:"refuse"`
|
||||
Rule string `json:"rule,omitempty"` // ssh | hosts-file | hostaliases | override-named | account
|
||||
Machine string `json:"machine,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
// Overridable is false for a refusal no override lifts: the agent naming the override itself.
|
||||
Overridable bool `json:"-"`
|
||||
// Calls are the tools the refusal named, by address without a machine: what the instructions' table
|
||||
// puts first, the tools the agent most reached round.
|
||||
Calls []string `json:"calls,omitempty"`
|
||||
}
|
||||
|
||||
// OverrideVar is the operator's override: set in the operator's own shell before the session starts,
|
||||
// with why as its value. Read from the session's own environment as it was started, never from what a
|
||||
// command or a setting put there (guard_run.go).
|
||||
const OverrideVar = "MESH_GUARD_OVERRIDE"
|
||||
|
||||
// ForgeUser is the forge's ssh account: it runs git, never a shell (stated in ADR 0245).
|
||||
const ForgeUser = "git"
|
||||
|
||||
// protectedFiles are the files a session never writes for a mesh name.
|
||||
var protectedFiles = map[string]bool{"/etc/hosts": true, "/etc/resolv.conf": true}
|
||||
|
||||
// SSHView is how ssh itself reads a destination with these options: the host it connects to, the user,
|
||||
// and the jumps. Empty host when ssh could not say.
|
||||
type SSHView func(options []string, destination string) (host, user string, jumps []string)
|
||||
|
||||
// Guard judges what a session is about to do.
|
||||
type Guard struct {
|
||||
Data GuardData
|
||||
// SSH reads a destination as ssh does (`ssh -G`); nil reads it from the command line alone.
|
||||
SSH SSHView
|
||||
// Resolve is a name's addresses, for a name that is none of the mesh's but may point at a machine.
|
||||
Resolve func(string) []string
|
||||
}
|
||||
|
||||
// JudgeAs is the verdict on one tool call of a session running as an account. **On a machine whose agents run
|
||||
// as an account of their own, a session running as any other does nothing** (novox/hq ADR 0266): the
|
||||
// operator's account there may become root without a person, and an agent started in it by habit would carry
|
||||
// that into every command. Refused whatever the command, and no override lifts it — the operator's own shell is
|
||||
// how the operator acts as themselves.
|
||||
func (g Guard) JudgeAs(account string, in HookInput) Verdict {
|
||||
if g.Data.Agent != "" && account != g.Data.Agent {
|
||||
return Verdict{Refuse: true, Rule: "account", Message: fmt.Sprintf("Refused: on %s agents run as %s, an "+
|
||||
"account that cannot become root without a person, and this session runs as %s (novox/hq ADR 0266). "+
|
||||
"No tool it calls here is let through. End it, and start the agent with `claude-agent`, which runs it as "+
|
||||
"%s; what needs the operator's own rights is the operator's to run in their own shell.",
|
||||
orThisMachine(g.Data.Node), g.Data.Agent, account, g.Data.Agent)}
|
||||
}
|
||||
return g.Judge(in)
|
||||
}
|
||||
|
||||
func orThisMachine(node string) string {
|
||||
if node == "" {
|
||||
return "this machine"
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
// Judge is the verdict on one tool call.
|
||||
func (g Guard) Judge(in HookInput) Verdict {
|
||||
switch in.ToolName {
|
||||
case "Bash":
|
||||
command, _ := in.ToolInput["command"].(string)
|
||||
return g.judgeLine(command, 0)
|
||||
case "Edit", "Write", "MultiEdit", "NotebookEdit":
|
||||
for _, key := range []string{"file_path", "notebook_path"} {
|
||||
if p, _ := in.ToolInput[key].(string); p != "" && protectedFiles[filepath.Clean(p)] {
|
||||
return g.hostsFile(filepath.Clean(p), in.ToolName+" "+filepath.Clean(p))
|
||||
}
|
||||
}
|
||||
}
|
||||
return Verdict{}
|
||||
}
|
||||
|
||||
func (g Guard) judgeLine(line string, depth int) Verdict {
|
||||
if depth > 4 {
|
||||
return Verdict{}
|
||||
}
|
||||
if strings.Contains(line, OverrideVar) {
|
||||
return Verdict{Refuse: true, Rule: "override-named", Message: fmt.Sprintf(
|
||||
"Refused by the mesh's guard (novox/hq ADR 0245): this command names %s, the operator's override. "+
|
||||
"It is the operator's alone, set in their own shell before a session starts; a session never sets, "+
|
||||
"reads or passes it.", OverrideVar)}
|
||||
}
|
||||
if strings.Contains(line, "HOSTALIASES") {
|
||||
return g.refuseLocal("hostaliases", "HOSTALIASES", "setting HOSTALIASES")
|
||||
}
|
||||
for _, words := range simpleCommands(line) {
|
||||
if v := g.judgeCommand(words, depth); v.Refuse {
|
||||
return v
|
||||
}
|
||||
// What a word runs in its own shell: `$(…)` and backticks inside a quoted word.
|
||||
for _, w := range words {
|
||||
for _, inner := range substitutions(w) {
|
||||
if v := g.judgeLine(inner, depth+1); v.Refuse {
|
||||
return v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Verdict{}
|
||||
}
|
||||
|
||||
// wrappers run the command after them; each with the options that take a value.
|
||||
var wrappers = map[string]string{
|
||||
"sudo": "ugCDhpTrt", "doas": "uC", "env": "uSC", "timeout": "sk", "nohup": "", "nice": "n", "ionice": "cnpP",
|
||||
"command": "", "exec": "a", "time": "fo", "stdbuf": "ioe", "xargs": "aEdIiLlnPs", "setsid": "", "unbuffer": "",
|
||||
"chronic": "", "flock": "wE", "torsocks": "", "proxychains": "f", "proxychains4": "f", "caffeinate": "",
|
||||
}
|
||||
|
||||
// shells run their -c argument as a command line.
|
||||
var shells = map[string]bool{"sh": true, "bash": true, "zsh": true, "dash": true, "ksh": true, "fish": true, "eval": true}
|
||||
|
||||
func (g Guard) judgeCommand(words []string, depth int) Verdict {
|
||||
words = unwrap(words)
|
||||
if len(words) == 0 {
|
||||
return Verdict{}
|
||||
}
|
||||
name := path.Base(words[0])
|
||||
args := words[1:]
|
||||
if shells[name] {
|
||||
if name == "eval" {
|
||||
return g.judgeLine(strings.Join(args, " "), depth+1)
|
||||
}
|
||||
for i, a := range args {
|
||||
if strings.HasPrefix(a, "-") && !strings.HasPrefix(a, "--") && strings.Contains(a, "c") && i+1 < len(args) {
|
||||
return g.judgeLine(args[i+1], depth+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
switch name {
|
||||
case "ssh", "autossh", "mosh", "slogin":
|
||||
return g.judgeSSH(name, args, strings.Join(words, " "))
|
||||
case "scp", "sftp", "rsync":
|
||||
return g.judgeCopy(name, args, strings.Join(words, " "))
|
||||
}
|
||||
if target := writesProtected(name, args); target != "" {
|
||||
return g.hostsFile(target, strings.Join(words, " "))
|
||||
}
|
||||
return Verdict{}
|
||||
}
|
||||
|
||||
// unwrap takes off what only runs the command: assignments before it, and wrappers with their options.
|
||||
func unwrap(words []string) []string {
|
||||
for len(words) > 0 {
|
||||
w := words[0]
|
||||
switch {
|
||||
case w == "!" || w == "{" || w == "}" || w == "then" || w == "do" || w == "else" || w == "if" || w == "while" ||
|
||||
w == "until" || w == "elif":
|
||||
words = words[1:]
|
||||
case isAssignment(w):
|
||||
words = words[1:]
|
||||
default:
|
||||
opts, wrapper := wrappers[path.Base(w)]
|
||||
if !wrapper {
|
||||
return words
|
||||
}
|
||||
base := path.Base(w)
|
||||
words = words[1:]
|
||||
for len(words) > 0 {
|
||||
a := words[0]
|
||||
if a == "--" {
|
||||
words = words[1:]
|
||||
break
|
||||
}
|
||||
if base == "env" && isAssignment(a) {
|
||||
words = words[1:]
|
||||
continue
|
||||
}
|
||||
if !strings.HasPrefix(a, "-") || a == "-" {
|
||||
break
|
||||
}
|
||||
words = words[1:]
|
||||
if strings.HasPrefix(a, "--") {
|
||||
continue
|
||||
}
|
||||
if f := a[len(a)-1:]; strings.Contains(opts, f) && len(words) > 0 {
|
||||
words = words[1:]
|
||||
}
|
||||
}
|
||||
// timeout's duration, nice's adjustment as a word: a number before the command.
|
||||
if (base == "timeout") && len(words) > 0 {
|
||||
words = words[1:]
|
||||
}
|
||||
}
|
||||
}
|
||||
return words
|
||||
}
|
||||
|
||||
func isAssignment(w string) bool {
|
||||
i := strings.Index(w, "=")
|
||||
if i <= 0 {
|
||||
return false
|
||||
}
|
||||
for _, r := range w[:i] {
|
||||
if !(r == '_' || r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' || r >= '0' && r <= '9') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// sshOptionsWithValue are ssh's single-letter options that take a value.
|
||||
const sshOptionsWithValue = "BbcDEeFIiJLlmOoPpQRSWw"
|
||||
|
||||
func (g Guard) judgeSSH(name string, args []string, line string) Verdict {
|
||||
var options, jumps []string
|
||||
user := ""
|
||||
dest, rest := "", []string(nil)
|
||||
for i := 0; i < len(args); i++ {
|
||||
a := args[i]
|
||||
if a == "--" {
|
||||
if i+1 < len(args) {
|
||||
dest, rest = args[i+1], args[i+2:]
|
||||
}
|
||||
break
|
||||
}
|
||||
if name == "autossh" && strings.HasPrefix(a, "-M") {
|
||||
if a == "-M" {
|
||||
i++ // autossh's own: the monitoring port
|
||||
}
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(a, "-") && len(a) > 1 {
|
||||
options = append(options, a)
|
||||
if name == "mosh" || strings.HasPrefix(a, "--") {
|
||||
continue // mosh's own options are long, each with its value after `=`
|
||||
}
|
||||
for j := 1; j < len(a); j++ {
|
||||
if !strings.ContainsRune(sshOptionsWithValue, rune(a[j])) {
|
||||
continue
|
||||
}
|
||||
value := a[j+1:]
|
||||
if value == "" && i+1 < len(args) {
|
||||
i++
|
||||
value = args[i]
|
||||
options = append(options, value)
|
||||
}
|
||||
switch a[j] {
|
||||
case 'J':
|
||||
jumps = append(jumps, strings.Split(value, ",")...)
|
||||
case 'l':
|
||||
user = value
|
||||
case 'o':
|
||||
k, v, _ := strings.Cut(value, "=")
|
||||
switch strings.ToLower(strings.TrimSpace(k)) {
|
||||
case "proxyjump":
|
||||
jumps = append(jumps, strings.Split(v, ",")...)
|
||||
case "user":
|
||||
user = strings.TrimSpace(v)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
continue
|
||||
}
|
||||
dest, rest = a, args[i+1:]
|
||||
break
|
||||
}
|
||||
if dest == "" {
|
||||
return Verdict{}
|
||||
}
|
||||
host, destUser := hostOf(dest)
|
||||
if destUser != "" {
|
||||
user = destUser
|
||||
}
|
||||
if g.SSH != nil && name != "mosh" {
|
||||
if h, u, j := g.SSH(options, dest); h != "" {
|
||||
host, user = h, u
|
||||
jumps = append(jumps, j...)
|
||||
}
|
||||
}
|
||||
remote := strings.Join(rest, " ")
|
||||
for _, jump := range jumps {
|
||||
jh, ju := hostOf(jump)
|
||||
if machine, ok := g.meshHost(jh); ok && ju != ForgeUser {
|
||||
return g.refuseSSH(machine, jh, remote, line)
|
||||
}
|
||||
}
|
||||
if user == ForgeUser {
|
||||
return Verdict{} // the forge's account: git and nothing else (stated in ADR 0245)
|
||||
}
|
||||
if machine, ok := g.meshHost(host); ok {
|
||||
return g.refuseSSH(machine, host, remote, line)
|
||||
}
|
||||
return Verdict{}
|
||||
}
|
||||
|
||||
// judgeCopy reads scp's, sftp's and rsync's remote paths, `[user@]host:path` or `scp://[user@]host/…`.
|
||||
func (g Guard) judgeCopy(name string, args []string, line string) Verdict {
|
||||
for i := 0; i < len(args); i++ {
|
||||
a := args[i]
|
||||
if strings.HasPrefix(a, "-") {
|
||||
if name != "rsync" && len(a) == 2 && strings.ContainsRune("cFiJloPSXBRD", rune(a[1])) {
|
||||
i++
|
||||
}
|
||||
if name == "rsync" && (a == "-e" || a == "--rsh") {
|
||||
i++
|
||||
}
|
||||
continue
|
||||
}
|
||||
var host, user string
|
||||
if strings.Contains(a, "://") {
|
||||
host, user = hostOf(a)
|
||||
} else if colon := strings.Index(a, ":"); colon > 0 && !strings.Contains(a[:colon], "/") {
|
||||
host, user = hostOf(a[:colon])
|
||||
} else if name == "sftp" {
|
||||
host, user = hostOf(a)
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
if user == ForgeUser {
|
||||
continue
|
||||
}
|
||||
if g.SSH != nil {
|
||||
if h, u, _ := g.SSH(nil, host); h != "" {
|
||||
host = h
|
||||
if u == ForgeUser && user == "" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
if machine, ok := g.meshHost(host); ok {
|
||||
return g.refuseSSH(machine, host, "", line)
|
||||
}
|
||||
}
|
||||
return Verdict{}
|
||||
}
|
||||
|
||||
// hostOf reads `[user@]host[:port]`, `ssh://[user@]host[:port]/…` or `[v6]`.
|
||||
func hostOf(dest string) (host, user string) {
|
||||
if strings.Contains(dest, "://") {
|
||||
if u, err := url.Parse(dest); err == nil {
|
||||
return strings.ToLower(u.Hostname()), u.User.Username()
|
||||
}
|
||||
}
|
||||
if at := strings.LastIndex(dest, "@"); at >= 0 {
|
||||
user, dest = dest[:at], dest[at+1:]
|
||||
}
|
||||
if strings.HasPrefix(dest, "[") {
|
||||
if end := strings.Index(dest, "]"); end > 0 {
|
||||
return strings.ToLower(dest[1:end]), user
|
||||
}
|
||||
}
|
||||
if h, _, err := net.SplitHostPort(dest); err == nil && strings.Count(dest, ":") == 1 {
|
||||
dest = h
|
||||
}
|
||||
return strings.TrimSuffix(strings.ToLower(dest), "."), user
|
||||
}
|
||||
|
||||
// meshHost says whether a host is one of the mesh's machines, and which when it can tell.
|
||||
func (g Guard) meshHost(host string) (string, bool) {
|
||||
host = strings.TrimSuffix(strings.ToLower(strings.Trim(host, "[]")), ".")
|
||||
if host == "" {
|
||||
return "", false
|
||||
}
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
for _, m := range g.Data.Machines {
|
||||
for _, a := range m.Addresses {
|
||||
if b := net.ParseIP(a); b != nil && b.Equal(ip) {
|
||||
return m.Name, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
for _, m := range g.Data.Machines {
|
||||
if host == strings.ToLower(m.Name) {
|
||||
return m.Name, true
|
||||
}
|
||||
for _, d := range m.Domains {
|
||||
if host == d || strings.HasSuffix(host, "."+d) {
|
||||
return m.Name, true
|
||||
}
|
||||
}
|
||||
}
|
||||
if strings.HasSuffix(host, InternalSuffix) {
|
||||
return "", true
|
||||
}
|
||||
if g.Resolve != nil && !strings.Contains(host, "/") {
|
||||
for _, a := range g.Resolve(host) {
|
||||
if m, ok := g.meshHost(a); ok && net.ParseIP(a) != nil {
|
||||
return m, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// writesProtected is the protected file a command writes, or "".
|
||||
func writesProtected(name string, args []string) string {
|
||||
protected := func(a string) string {
|
||||
a = strings.Trim(a, `"'`)
|
||||
if strings.HasPrefix(a, "of=") {
|
||||
a = a[3:]
|
||||
}
|
||||
if protectedFiles[filepath.Clean(a)] {
|
||||
return filepath.Clean(a)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
// A redirect onto it, from any command.
|
||||
for i, a := range args {
|
||||
if (a == ">" || a == ">>") && i+1 < len(args) {
|
||||
if p := protected(args[i+1]); p != "" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
}
|
||||
operands := func() []string {
|
||||
var out []string
|
||||
for i := 0; i < len(args); i++ {
|
||||
if args[i] == ">" || args[i] == ">>" || args[i] == "<" {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
out = append(out, args[i])
|
||||
}
|
||||
return out
|
||||
}()
|
||||
switch name {
|
||||
case "tee", "vi", "vim", "nvim", "nano", "emacs", "ed", "ex", "micro", "hx", "helix", "kak", "gedit", "code",
|
||||
"truncate", "chattr", "chmod", "chown", "rm", "unlink", "shred", "dd", "sponge", "visudo", "vipw":
|
||||
for _, a := range operands {
|
||||
if p := protected(a); p != "" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
case "sed", "perl", "ruby", "awk", "gawk":
|
||||
inPlace := false
|
||||
for _, a := range operands {
|
||||
switch {
|
||||
case name == "awk" || name == "gawk":
|
||||
inPlace = inPlace || a == "inplace"
|
||||
case strings.HasPrefix(a, "--in-place"):
|
||||
inPlace = true
|
||||
case strings.HasPrefix(a, "-") && !strings.HasPrefix(a, "--") && strings.Contains(a, "i"):
|
||||
inPlace = true
|
||||
}
|
||||
}
|
||||
if inPlace {
|
||||
for _, a := range operands {
|
||||
if p := protected(a); p != "" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
}
|
||||
case "cp", "mv", "install", "ln", "rsync":
|
||||
var files []string
|
||||
for _, a := range operands {
|
||||
if !strings.HasPrefix(a, "-") {
|
||||
files = append(files, a)
|
||||
}
|
||||
}
|
||||
if len(files) > 1 {
|
||||
if p := protected(files[len(files)-1]); p != "" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// replacementsFor are the mesh's tools that replace a command, the most specific first, each address once.
|
||||
func (g Guard) replacementsFor(command string) []string {
|
||||
have := commandWordsOf(command)
|
||||
type found struct {
|
||||
address string
|
||||
weight int
|
||||
replace string
|
||||
}
|
||||
var all []found
|
||||
for _, r := range g.Data.Replacements {
|
||||
best := 0
|
||||
which := ""
|
||||
for _, c := range r.Replaces {
|
||||
want := commandWordsOf(c)
|
||||
if inOrderWords(want, have) && len(want) > best {
|
||||
best, which = len(want), c
|
||||
}
|
||||
}
|
||||
if best > 0 {
|
||||
all = append(all, found{r.Address, best, which})
|
||||
}
|
||||
}
|
||||
sort.SliceStable(all, func(i, j int) bool { return all[i].weight > all[j].weight })
|
||||
var out []string
|
||||
for _, f := range all {
|
||||
out = append(out, f.address+"\x00"+f.replace)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// callFor is how an address is called for a machine.
|
||||
func (g Guard) callFor(address, machine string) string {
|
||||
for _, r := range g.Data.Replacements {
|
||||
if r.Address != address || !r.NodeScoped {
|
||||
continue
|
||||
}
|
||||
on := machine
|
||||
if !r.Seat && len(r.On) > 0 && !containsString(r.On, machine) {
|
||||
on = r.On[0]
|
||||
}
|
||||
if on == "" {
|
||||
on = "<node>"
|
||||
}
|
||||
return on + "/" + address
|
||||
}
|
||||
return address
|
||||
}
|
||||
|
||||
func containsString(xs []string, s string) bool {
|
||||
for _, x := range xs {
|
||||
if x == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// exact is the replacement that names this exact command, or "".
|
||||
func (g Guard) exact(command string) string {
|
||||
for _, r := range g.Data.Replacements {
|
||||
for _, c := range r.Replaces {
|
||||
if c == command {
|
||||
return r.Address
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
const missingTool = "No mesh tool says it replaces this. Do not work around it: a missing tool is created in the module " +
|
||||
"that owns the thing, on the seat it occupies — say what is missing to the operator. First make sure with " +
|
||||
"`mesh_search` and the command's own words."
|
||||
|
||||
func (g Guard) refuseSSH(machine, host, remote, line string) Verdict {
|
||||
who := machine
|
||||
if who == "" {
|
||||
who = host + " (a mesh name)"
|
||||
}
|
||||
var b strings.Builder
|
||||
var named []string
|
||||
fmt.Fprintf(&b, "Refused by the mesh's guard (novox/hq ADR 0245): `%s` reaches %s round the mesh's tools.\n", cut(line, 160), who)
|
||||
switch {
|
||||
case strings.TrimSpace(remote) == "":
|
||||
target := machine
|
||||
if target == "" {
|
||||
target = "<machine>"
|
||||
}
|
||||
fmt.Fprintf(&b, "What a machine serves is `mesh_machine %s`; `mesh_search` with what you meant to run finds its tool.\n", target)
|
||||
default:
|
||||
calls := g.replacementsFor(remote)
|
||||
if len(calls) == 0 {
|
||||
b.WriteString(missingTool + "\n")
|
||||
break
|
||||
}
|
||||
b.WriteString("Call instead, through `mesh_call`:\n")
|
||||
for i, c := range calls {
|
||||
if i == 3 {
|
||||
break
|
||||
}
|
||||
address, replaces, _ := strings.Cut(c, "\x00")
|
||||
named = append(named, address)
|
||||
fmt.Fprintf(&b, " %s — replaces `%s`\n", g.callFor(address, machine), replaces)
|
||||
}
|
||||
}
|
||||
return Verdict{Refuse: true, Rule: "ssh", Machine: machine, Message: strings.TrimRight(b.String(), "\n"), Overridable: true,
|
||||
Calls: named}
|
||||
}
|
||||
|
||||
func (g Guard) refuseLocal(rule, replaced, what string) Verdict {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "Refused by the mesh's guard (novox/hq ADR 0245): %s is a work-around for a mesh name. "+
|
||||
"A mesh name is the mesh's resolvers' to answer, and a machine's own lines in /etc/hosts are its node-hostname seat's.\n", what)
|
||||
var named []string
|
||||
if address := g.exact(replaced); address != "" {
|
||||
named = append(named, address)
|
||||
fmt.Fprintf(&b, "Call instead, through `mesh_call`: %s — replaces `%s`", g.callFor(address, g.Data.Node), replaced)
|
||||
} else {
|
||||
b.WriteString(missingTool)
|
||||
}
|
||||
return Verdict{Refuse: true, Rule: rule, Machine: g.Data.Node, Message: b.String(), Overridable: true, Calls: named}
|
||||
}
|
||||
|
||||
func (g Guard) hostsFile(file, line string) Verdict {
|
||||
replaced := "edit " + file
|
||||
return g.refuseLocal("hosts-file", replaced, fmt.Sprintf("writing %s (`%s`)", file, cut(line, 120)))
|
||||
}
|
||||
|
||||
func cut(s string, n int) string {
|
||||
s = strings.Join(strings.Fields(s), " ")
|
||||
if r := []rune(s); len(r) > n {
|
||||
return string(r[:n-1]) + "…"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// commandWordsOf are a command line's words as matched against a replaced command (the mesh MCP server's search rule):
|
||||
// lower-cased, a program by path reduced to its name, sudo left out.
|
||||
func commandWordsOf(line string) []string {
|
||||
var out []string
|
||||
for i, w := range strings.Fields(strings.ToLower(line)) {
|
||||
if i == 0 || strings.HasPrefix(w, "/") {
|
||||
w = path.Base(w)
|
||||
}
|
||||
if w == "sudo" {
|
||||
continue
|
||||
}
|
||||
out = append(out, w)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func inOrderWords(want, have []string) bool {
|
||||
i := 0
|
||||
for _, h := range have {
|
||||
if i < len(want) && h == want[i] {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return len(want) > 0 && i == len(want)
|
||||
}
|
||||
|
||||
// ---- the shell's words ------------------------------------------------------------------------------
|
||||
|
||||
// simpleCommands splits a command line as a shell would into its simple commands' words: quotes
|
||||
// removed, `;`, `&`, `|`, `&&`, `||`, newlines, parentheses and backticks between commands, `$(` opening
|
||||
// one; `>` and `>>` kept as words of their own, before what they redirect to.
|
||||
func simpleCommands(line string) [][]string {
|
||||
var out [][]string
|
||||
var words []string
|
||||
var cur strings.Builder
|
||||
inWord := false
|
||||
flushWord := func() {
|
||||
if inWord {
|
||||
words = append(words, cur.String())
|
||||
cur.Reset()
|
||||
inWord = false
|
||||
}
|
||||
}
|
||||
flushCommand := func() {
|
||||
flushWord()
|
||||
if len(words) > 0 {
|
||||
out = append(out, words)
|
||||
}
|
||||
words = nil
|
||||
}
|
||||
r := []rune(line)
|
||||
for i := 0; i < len(r); i++ {
|
||||
c := r[i]
|
||||
switch {
|
||||
case c == '\\' && i+1 < len(r):
|
||||
i++
|
||||
if r[i] != '\n' {
|
||||
cur.WriteRune(r[i])
|
||||
inWord = true
|
||||
}
|
||||
case c == '\'':
|
||||
inWord = true
|
||||
for i++; i < len(r) && r[i] != '\''; i++ {
|
||||
cur.WriteRune(r[i])
|
||||
}
|
||||
case c == '"':
|
||||
inWord = true
|
||||
for i++; i < len(r) && r[i] != '"'; i++ {
|
||||
if r[i] == '\\' && i+1 < len(r) && strings.ContainsRune("\"\\$`", r[i+1]) {
|
||||
i++
|
||||
}
|
||||
cur.WriteRune(r[i])
|
||||
}
|
||||
case c == '$' && i+1 < len(r) && r[i+1] == '(':
|
||||
flushCommand()
|
||||
i++
|
||||
case c == ';' || c == '&' || c == '|' || c == '\n' || c == '(' || c == ')' || c == '`':
|
||||
flushCommand()
|
||||
case c == '>' || c == '<':
|
||||
// A file descriptor before it (`2>`) is not a word.
|
||||
if inWord && isDigits(cur.String()) {
|
||||
cur.Reset()
|
||||
inWord = false
|
||||
}
|
||||
flushWord()
|
||||
op := string(c)
|
||||
if c == '>' && i+1 < len(r) && r[i+1] == '>' {
|
||||
op = ">>"
|
||||
i++
|
||||
}
|
||||
if i+1 < len(r) && (r[i+1] == '&' || r[i+1] == '|') {
|
||||
i++
|
||||
}
|
||||
words = append(words, op)
|
||||
case c == ' ' || c == '\t':
|
||||
flushWord()
|
||||
case c == '#' && !inWord:
|
||||
for i < len(r) && r[i] != '\n' {
|
||||
i++
|
||||
}
|
||||
flushCommand()
|
||||
default:
|
||||
cur.WriteRune(c)
|
||||
inWord = true
|
||||
}
|
||||
}
|
||||
flushCommand()
|
||||
return out
|
||||
}
|
||||
|
||||
func isDigits(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
for _, r := range s {
|
||||
if r < '0' || r > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// substitutions are the command lines a quoted word runs in a shell of its own: `$(…)` and `…`.
|
||||
func substitutions(word string) []string {
|
||||
var out []string
|
||||
for {
|
||||
i := strings.Index(word, "$(")
|
||||
if i < 0 {
|
||||
break
|
||||
}
|
||||
rest := word[i+2:]
|
||||
depth, end := 1, len(rest)
|
||||
for j, c := range rest {
|
||||
if c == '(' {
|
||||
depth++
|
||||
} else if c == ')' {
|
||||
depth--
|
||||
if depth == 0 {
|
||||
end = j
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
out = append(out, rest[:end])
|
||||
if end >= len(rest) {
|
||||
break
|
||||
}
|
||||
word = rest[end+1:]
|
||||
}
|
||||
parts := strings.Split(word, "`")
|
||||
for i := 1; i < len(parts); i += 2 {
|
||||
out = append(out, parts[i])
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
package main
|
||||
|
||||
// The guard as the hook runs it (novox/hq ADR 0245): `claude-code guard <guard.json>`, the tool call on
|
||||
// standard input, exit 2 with the reason on standard error to refuse — the agent reads it — and 0 to let it be.
|
||||
//
|
||||
// **The operator's override.** `MESH_GUARD_OVERRIDE=<why>` lets one session through a refusal — never the
|
||||
// refusal of a command naming the override itself — and only:
|
||||
//
|
||||
// - **from the session's environment as it was started**: read from /proc/<pid>/environ of the agent's own
|
||||
// process, which is fixed at exec. A command's `export`, an `env` key in a settings file, a variable the
|
||||
// session sets for its tools — none of them reaches it. A nested session the agent starts with the
|
||||
// variable is refused before it starts: the command names the override;
|
||||
// - **recorded**: every override is a line in the module's guard.log — or, where the agent runs as an account
|
||||
// of its own, in the machine's journal (novox/hq ADR 0266) — with why, the command and the machine; an
|
||||
// override that cannot be recorded is not honoured. Refusals are recorded the same way.
|
||||
//
|
||||
// A guard that fails — a fault in this code — exits 1, which the agent shows and does not block on: a guard
|
||||
// that refused every command would stop the session the operator needs to repair it.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// procRoot and parentPID are where the session's process is found; a test points them at its own.
|
||||
var (
|
||||
procRoot = "/proc"
|
||||
parentPID = os.Getppid
|
||||
)
|
||||
|
||||
// GuardLogMax is the size the guard's record grows to before it is kept once as guard.log.1.
|
||||
const GuardLogMax = 1 << 20
|
||||
|
||||
// GuardEntry is one line of the guard's record.
|
||||
type GuardEntry struct {
|
||||
At string `json:"at"`
|
||||
Node string `json:"node,omitempty"`
|
||||
Decision string `json:"decision"` // refused | overridden
|
||||
Rule string `json:"rule"`
|
||||
Machine string `json:"machine,omitempty"`
|
||||
Tool string `json:"tool"`
|
||||
Command string `json:"command"`
|
||||
Why string `json:"why,omitempty"`
|
||||
// Tools are the tools the refusal named instead.
|
||||
Tools []string `json:"tools,omitempty"`
|
||||
Session string `json:"session,omitempty"`
|
||||
}
|
||||
|
||||
// JournalRecord is the guard's record where the agent runs as an account of its own (novox/hq ADR 0266): the
|
||||
// session's account cannot enter the module's state, and must not be able to rewrite what it was refused, so
|
||||
// each line goes to the machine's journal, which the account may add to and not change. The record's
|
||||
// location is then this prefix and the journal's identifier.
|
||||
const JournalRecord = "journal:"
|
||||
|
||||
// GuardJournalID is the identifier the guard's lines carry in the journal.
|
||||
const GuardJournalID = "mesh-claude-code-guard"
|
||||
|
||||
// journalSocket is where the journal takes a line; a test points it at its own.
|
||||
var journalSocket = "/run/systemd/journal/socket"
|
||||
|
||||
// RecordGuard appends one line to the record.
|
||||
func RecordGuard(path string, e GuardEntry) error {
|
||||
if path == "" {
|
||||
return fmt.Errorf("no record is kept here")
|
||||
}
|
||||
if id, ok := strings.CutPrefix(path, JournalRecord); ok {
|
||||
return recordInJournal(id, e)
|
||||
}
|
||||
if info, err := os.Stat(path); err == nil && info.Size() > GuardLogMax {
|
||||
_ = os.Rename(path, path+".1")
|
||||
}
|
||||
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
raw, _ := json.Marshal(e)
|
||||
if _, err := f.Write(append(raw, '\n')); err != nil {
|
||||
f.Close()
|
||||
return err
|
||||
}
|
||||
return f.Close()
|
||||
}
|
||||
|
||||
// recordInJournal says one line to the journal by its native protocol: the entry as JSON in MESSAGE, one line,
|
||||
// so nothing in a command can add a field of its own.
|
||||
func recordInJournal(id string, e GuardEntry) error {
|
||||
raw, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conn, err := net.Dial("unixgram", journalSocket)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the journal cannot be reached: %w", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
_, err = conn.Write([]byte("SYSLOG_IDENTIFIER=" + id + "\nPRIORITY=5\nMESSAGE=" + string(raw) + "\n"))
|
||||
return err
|
||||
}
|
||||
|
||||
// readJournal is the journal's last lines of the guard's record, read as root through the runtime account's
|
||||
// sudo where this process is not root: what other accounts said is not the runtime account's to read. As JSON,
|
||||
// with the field the journal itself sets for the program that wrote the line (_EXE), which no writer chooses —
|
||||
// and matched on it in the query itself, so lines another program wrote under the guard's identifier cannot
|
||||
// push the guard's own past the last ones read.
|
||||
var readJournal = func(id string, last int) ([]byte, error) {
|
||||
args := []string{"journalctl", "-q", "--no-pager", "-o", "json", "--output-fields=MESSAGE,_EXE", "-t", id,
|
||||
"_EXE=" + HelperPath(), "-n", strconv.Itoa(last)}
|
||||
if os.Geteuid() != 0 {
|
||||
args = append([]string{"sudo", "-n"}, args...)
|
||||
}
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Env = append(os.Environ(), "LC_ALL=C", "LANG=C")
|
||||
return cmd.Output()
|
||||
}
|
||||
|
||||
// guardsOwnLines keeps, of the journal's JSON lines, the messages the guard's own binary wrote (novox/hq ADR
|
||||
// 0266). The agent's account may add to the journal under any identifier — `logger -t` — and the journal
|
||||
// says which program wrote each line; a line another program wrote under the guard's identifier is not the
|
||||
// guard's record. The agent can still run the guard's binary itself, so the record is the guard's words, not
|
||||
// proof that each refusal happened in a session: what it cannot do is remove one.
|
||||
func guardsOwnLines(raw []byte, exe string) []byte {
|
||||
var out []byte
|
||||
for _, line := range bytes.Split(raw, []byte("\n")) {
|
||||
var f struct {
|
||||
Message any `json:"MESSAGE"`
|
||||
Exe string `json:"_EXE"`
|
||||
}
|
||||
if json.Unmarshal(line, &f) != nil || f.Exe != exe {
|
||||
continue
|
||||
}
|
||||
if m, ok := f.Message.(string); ok {
|
||||
out = append(append(out, m...), '\n')
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ReadGuardLog is the record's last lines, newest last.
|
||||
func ReadGuardLog(path string, last int) []GuardEntry {
|
||||
read := os.ReadFile
|
||||
if id, ok := strings.CutPrefix(path, JournalRecord); ok {
|
||||
read = func(string) ([]byte, error) {
|
||||
raw, err := readJournal(id, last)
|
||||
return guardsOwnLines(raw, HelperPath()), err
|
||||
}
|
||||
}
|
||||
raw, err := read(path)
|
||||
if err != nil {
|
||||
return []GuardEntry{}
|
||||
}
|
||||
lines := strings.Split(strings.TrimSpace(string(raw)), "\n")
|
||||
if len(lines) > last {
|
||||
lines = lines[len(lines)-last:]
|
||||
}
|
||||
out := []GuardEntry{}
|
||||
for _, l := range lines {
|
||||
var e GuardEntry
|
||||
if json.Unmarshal([]byte(l), &e) == nil {
|
||||
out = append(out, e)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// sshDashG reads a destination as ssh does: `ssh -G` prints the configuration it would use and connects
|
||||
// to nothing, so an alias, a HostName and a ProxyJump in ~/.ssh/config are what is judged.
|
||||
func sshDashG(options []string, destination string) (host, user string, jumps []string) {
|
||||
if _, err := exec.LookPath("ssh"); err != nil {
|
||||
return "", "", nil
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
args := append([]string{"-G"}, options...)
|
||||
args = append(args, "--", destination)
|
||||
out, err := exec.CommandContext(ctx, "ssh", args...).Output()
|
||||
if err != nil {
|
||||
return "", "", nil
|
||||
}
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
k, v, ok := strings.Cut(strings.TrimSpace(line), " ")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch k {
|
||||
case "hostname":
|
||||
host = strings.ToLower(v)
|
||||
case "user":
|
||||
user = v
|
||||
case "proxyjump":
|
||||
if v != "none" {
|
||||
jumps = append(jumps, strings.Split(v, ",")...)
|
||||
}
|
||||
}
|
||||
}
|
||||
return host, user, jumps
|
||||
}
|
||||
|
||||
func quickLookup(name string) []string {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
addrs, _ := net.DefaultResolver.LookupHost(ctx, name)
|
||||
return addrs
|
||||
}
|
||||
|
||||
// sessionEnvironment is the environment the agent's own process was started with: the nearest ancestor
|
||||
// that is the agent, read from what the kernel kept at its exec.
|
||||
func sessionEnvironment(proc string, pid int) map[string]string {
|
||||
for i := 0; i < 32 && pid > 1; i++ {
|
||||
cmdline, _ := os.ReadFile(filepath.Join(proc, strconv.Itoa(pid), "cmdline"))
|
||||
if isAgent(cmdline) {
|
||||
raw, err := os.ReadFile(filepath.Join(proc, strconv.Itoa(pid), "environ"))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
env := map[string]string{}
|
||||
for _, kv := range bytes.Split(raw, []byte{0}) {
|
||||
if k, v, ok := strings.Cut(string(kv), "="); ok {
|
||||
env[k] = v
|
||||
}
|
||||
}
|
||||
return env
|
||||
}
|
||||
stat, err := os.ReadFile(filepath.Join(proc, strconv.Itoa(pid), "stat"))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
// The parent is the fourth field, after the command's name in parentheses (which may hold spaces).
|
||||
closing := bytes.LastIndexByte(stat, ')')
|
||||
fields := strings.Fields(string(stat[closing+1:]))
|
||||
if len(fields) < 2 {
|
||||
return nil
|
||||
}
|
||||
pid, _ = strconv.Atoi(fields[1])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// isAgent says whether a process's command line is the agent's: its native binary, or its package run by node.
|
||||
func isAgent(cmdline []byte) bool {
|
||||
args := strings.Split(strings.TrimRight(string(cmdline), "\x00"), "\x00")
|
||||
if len(args) == 0 || args[0] == "" {
|
||||
return false
|
||||
}
|
||||
if filepath.Base(args[0]) == "claude" {
|
||||
return true
|
||||
}
|
||||
if b := filepath.Base(args[0]); (b == "node" || b == "bun") && len(args) > 1 {
|
||||
return filepath.Base(args[1]) == "claude" || strings.Contains(args[1], "@anthropic-ai/claude-code")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// currentAccount is the account the session runs as; a test sets its own.
|
||||
var currentAccount = func() string {
|
||||
if u, err := user.Current(); err == nil {
|
||||
return u.Username
|
||||
}
|
||||
return "uid " + strconv.Itoa(os.Getuid())
|
||||
}
|
||||
|
||||
// runGuard is the hook: the tool call in, the verdict out as the exit code.
|
||||
func runGuard(dataPath string, stdin io.Reader, stderr io.Writer) (code int) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Fprintf(stderr, "the mesh's guard failed and judged nothing: %v\n", r)
|
||||
code = 1
|
||||
}
|
||||
}()
|
||||
var in HookInput
|
||||
if err := json.NewDecoder(stdin).Decode(&in); err != nil {
|
||||
fmt.Fprintf(stderr, "the mesh's guard could not read the tool call: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
var data GuardData
|
||||
if raw, err := os.ReadFile(dataPath); err == nil {
|
||||
_ = json.Unmarshal(raw, &data)
|
||||
}
|
||||
g := Guard{Data: data, SSH: sshDashG, Resolve: quickLookup}
|
||||
v := g.JudgeAs(currentAccount(), in)
|
||||
if !v.Refuse {
|
||||
return 0
|
||||
}
|
||||
command, _ := in.ToolInput["command"].(string)
|
||||
if command == "" {
|
||||
command, _ = in.ToolInput["file_path"].(string)
|
||||
}
|
||||
entry := GuardEntry{At: time.Now().UTC().Format(time.RFC3339), Node: data.Node, Decision: "refused", Rule: v.Rule,
|
||||
Machine: v.Machine, Tool: in.ToolName, Command: cut(command, 400), Session: in.SessionID, Tools: v.Calls}
|
||||
if v.Overridable {
|
||||
if why := strings.TrimSpace(sessionEnvironment(procRoot, parentPID())[OverrideVar]); why != "" {
|
||||
entry.Decision, entry.Why = "overridden", cut(why, 200)
|
||||
if err := RecordGuard(data.Log, entry); err == nil {
|
||||
return 0
|
||||
} else {
|
||||
v.Message += fmt.Sprintf("\nThe operator's override is set, and could not be recorded (%v): an override "+
|
||||
"that is not recorded is not honoured.", err)
|
||||
entry.Decision, entry.Why = "refused", ""
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = RecordGuard(data.Log, entry)
|
||||
fmt.Fprintln(stderr, v.Message)
|
||||
return 2
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
||||
)
|
||||
|
||||
// meshForTheGuard is a mesh of two machines and the verbs that replace what an agent runs over ssh, as the
|
||||
// seats' contract and the modules' manifests say them.
|
||||
func meshForTheGuard() GuardData {
|
||||
defined := []seatdef.Definition{
|
||||
{Seat: "mesh-module-assigner", Scope: "mesh", Verbs: []seatdef.Verb{
|
||||
{Name: "assignments", Description: "Read assignments: one node's key.", Replaces: []string{"hostnamectl", "uptime"}}}},
|
||||
{Seat: "node-service-manager", Scope: "node", Verbs: []seatdef.Verb{
|
||||
{Name: "status", Description: "One unit as the service manager sees it now: its states.", Replaces: []string{"systemctl status", "systemctl is-active"}},
|
||||
{Name: "restart", Description: "Restart one unit.", Replaces: []string{"systemctl restart"}},
|
||||
{Name: "journal", Description: "The last lines of one unit's journal.", Replaces: []string{"journalctl"}}}},
|
||||
{Seat: "node-hostname", Scope: "node", Verbs: []seatdef.Verb{
|
||||
{Name: "add", Description: "Add one address and its names to the operator's lines of /etc/hosts.", Replaces: []string{"edit /etc/hosts", "HOSTALIASES"}}}},
|
||||
}
|
||||
modules := []ModuleTools{{Module: "docker", On: []string{"anchor"}, Replaces: map[string][]string{"docker_logs": {"docker logs"}, "docker_list": {"docker ps"}}},
|
||||
{Module: "systemd", On: []string{"anchor", "laptop"}}}
|
||||
return GuardData{Node: "laptop", Replacements: ReplacementsOf(defined, modules), Machines: []Machine{
|
||||
{Name: "anchor", Domains: []string{"anchor" + InternalSuffix, "anchor.example"}, Addresses: []string{"192.0.2.10", "10.10.0.1"}},
|
||||
{Name: "laptop", Domains: []string{"laptop" + InternalSuffix}, Addresses: []string{"10.10.0.4"}},
|
||||
}}
|
||||
}
|
||||
|
||||
// noSSH reads a destination from the command line alone; aliases reads one alias as ssh's configuration would.
|
||||
func aliases(options []string, destination string) (string, string, []string) {
|
||||
host, user := hostOf(destination)
|
||||
switch host {
|
||||
case "a":
|
||||
return "anchor.example", "operator", nil
|
||||
case "via-anchor":
|
||||
return "203.0.113.5", "operator", []string{"anchor"}
|
||||
case "forge":
|
||||
return "git.anchor" + InternalSuffix, "git", nil
|
||||
}
|
||||
if user == "" {
|
||||
user = "operator"
|
||||
}
|
||||
for i, o := range options {
|
||||
if o == "-l" && i+1 < len(options) {
|
||||
user = options[i+1]
|
||||
}
|
||||
}
|
||||
return host, user, nil
|
||||
}
|
||||
|
||||
func guard() Guard {
|
||||
return Guard{Data: meshForTheGuard(), SSH: aliases, Resolve: func(name string) []string {
|
||||
if name == "nas.lan" {
|
||||
return []string{"192.0.2.10"}
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
|
||||
func bash(command string) HookInput {
|
||||
return HookInput{ToolName: "Bash", ToolInput: map[string]any{"command": command}}
|
||||
}
|
||||
|
||||
// **ssh to a mesh machine is refused**, by every name and address it has and however the shell spells it,
|
||||
// and the refusal names the verb that does the job on that machine (novox/hq ADR 0245).
|
||||
func TestSSHToAMeshMachineIsRefusedNamingTheTool(t *testing.T) {
|
||||
g := guard()
|
||||
for command, want := range map[string]string{
|
||||
"ssh anchor journalctl -u mesh-config -n 50": "anchor/node-service-manager.journal",
|
||||
"ssh anchor 'sudo journalctl -fu sshd'": "anchor/node-service-manager.journal",
|
||||
"ssh -p 22 root@anchor.example systemctl status nats": "anchor/node-service-manager.status",
|
||||
"ssh anchor.internal systemctl restart nats": "anchor/node-service-manager.restart",
|
||||
"ssh -o StrictHostKeyChecking=no 10.10.0.1 docker ps -a": "anchor/docker.docker_list",
|
||||
"ssh anchor docker logs --tail 100 nats": "anchor/docker.docker_logs",
|
||||
"ssh laptop docker logs x": "anchor/docker.docker_logs",
|
||||
"ssh a uptime": "mesh-module-assigner.assignments",
|
||||
"cd /tmp && ssh anchor journalctl": "anchor/node-service-manager.journal",
|
||||
"sudo -u root ssh anchor journalctl": "anchor/node-service-manager.journal",
|
||||
"timeout 10 ssh anchor journalctl": "anchor/node-service-manager.journal",
|
||||
"env LANG=C ssh anchor journalctl": "anchor/node-service-manager.journal",
|
||||
"bash -c 'ssh anchor journalctl -u x'": "anchor/node-service-manager.journal",
|
||||
`echo "$(ssh anchor journalctl -n 5)" | tail`: "anchor/node-service-manager.journal",
|
||||
"for n in anchor laptop; do ssh $n uptime; done; ssh anchor uptime": "mesh-module-assigner.assignments",
|
||||
"ssh -J anchor 203.0.113.9 journalctl": "anchor/node-service-manager.journal",
|
||||
"ssh via-anchor journalctl": "anchor/node-service-manager.journal",
|
||||
"ssh nas.lan journalctl": "anchor/node-service-manager.journal",
|
||||
"ssh ssh://anchor:2222 journalctl": "anchor/node-service-manager.journal",
|
||||
} {
|
||||
v := g.Judge(bash(command))
|
||||
if !v.Refuse || v.Rule != "ssh" {
|
||||
t.Errorf("%q was not refused: %+v", command, v)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(v.Message, want) {
|
||||
t.Errorf("%q: the refusal does not name %s:\n%s", command, want, v.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// What no tool replaces is refused all the same, saying a tool is created, never worked around; a shell
|
||||
// on a machine is pointed at what the machine serves; copying a file off one is refused too.
|
||||
func TestSSHWithNoToolForItSaysCreateOne(t *testing.T) {
|
||||
g := guard()
|
||||
for command, want := range map[string]string{
|
||||
"ssh anchor cat /var/lib/thing/state.json": "a missing tool is created in the module",
|
||||
"ssh anchor": "mesh_machine anchor",
|
||||
"ssh build.internal ls": "a missing tool is created",
|
||||
"scp anchor:/etc/nats/nats.conf /tmp/": "mesh_machine anchor",
|
||||
"rsync -av root@10.10.0.4:/srv/ ./srv/": "mesh_machine laptop",
|
||||
"sftp anchor.example": "mesh_machine anchor",
|
||||
"mosh anchor": "mesh_machine anchor",
|
||||
"autossh -M 0 -N -L 5432:localhost:5432 anchor": "mesh_machine anchor",
|
||||
} {
|
||||
v := g.Judge(bash(command))
|
||||
if !v.Refuse {
|
||||
t.Errorf("%q was not refused", command)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(v.Message, want) {
|
||||
t.Errorf("%q: want %q in:\n%s", command, want, v.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// **What is not a work-around passes**: git over ssh to the forge above all, and ssh beyond the mesh, and
|
||||
// every command that merely mentions ssh or a machine.
|
||||
func TestWhatIsNotAWorkAroundPasses(t *testing.T) {
|
||||
g := guard()
|
||||
for _, command := range []string{
|
||||
"git push -u origin feat/x",
|
||||
"git clone ssh://git@git.anchor.internal:222/novox/hq.git",
|
||||
"git fetch ssh://git@git.anchor.internal:222/novox/hq.git main",
|
||||
`GIT_SSH_COMMAND="ssh -i ~/.ssh/forge -o IdentitiesOnly=yes" git push origin HEAD`,
|
||||
"git -c core.sshCommand='ssh -p 222' pull",
|
||||
"ssh -T git@git.anchor.internal -p 222",
|
||||
"ssh forge",
|
||||
"scp git@git.anchor.internal:novox/hq.git .",
|
||||
"ssh github.com",
|
||||
"ssh -T git@github.com",
|
||||
"ssh user@203.0.113.7 uptime",
|
||||
"ssh -V",
|
||||
"ssh-keygen -t ed25519 -f ~/.ssh/x",
|
||||
"ssh-add -l",
|
||||
"man ssh",
|
||||
"grep -r 'ssh anchor' docs/",
|
||||
`git commit -m "Stop running ssh anchor journalctl"`,
|
||||
"echo anchor && journalctl --user -n 5",
|
||||
"cat /etc/hosts",
|
||||
"getent hosts anchor.internal",
|
||||
"cp /etc/hosts /tmp/hosts.copy",
|
||||
"sed -n 1,5p /etc/hosts",
|
||||
"curl -s http://anchor.internal:8080/health",
|
||||
"go test ./...",
|
||||
} {
|
||||
if v := g.Judge(bash(command)); v.Refuse {
|
||||
t.Errorf("%q was refused:\n%s", command, v.Message)
|
||||
}
|
||||
}
|
||||
if v := g.Judge(HookInput{ToolName: "Read", ToolInput: map[string]any{"file_path": "/etc/hosts"}}); v.Refuse {
|
||||
t.Errorf("reading /etc/hosts was refused")
|
||||
}
|
||||
if v := g.Judge(HookInput{ToolName: "Write", ToolInput: map[string]any{"file_path": "/tmp/hosts"}}); v.Refuse {
|
||||
t.Errorf("writing a file named hosts elsewhere was refused")
|
||||
}
|
||||
}
|
||||
|
||||
// **The local work-arounds for a mesh name are refused** with the machine's own hosts verb: writing
|
||||
// /etc/hosts by any means, the agent's own Edit and Write included, and HOSTALIASES.
|
||||
func TestTheLocalWorkAroundsForAMeshNameAreRefused(t *testing.T) {
|
||||
g := guard()
|
||||
for _, command := range []string{
|
||||
"echo '10.10.0.1 anchor' | sudo tee -a /etc/hosts",
|
||||
"sudo sh -c 'echo 10.10.0.1 anchor >> /etc/hosts'",
|
||||
"echo x >>/etc/hosts",
|
||||
"sudo sed -i 's/old/new/' /etc/hosts",
|
||||
"sudo sed -Ei.bak 's/a/b/' /etc/hosts",
|
||||
"sudo vim /etc/hosts",
|
||||
"sudo cp /tmp/hosts /etc/hosts",
|
||||
"sudo install -m 644 hosts /etc/hosts",
|
||||
"HOSTALIASES=~/.hosts curl http://anchor/",
|
||||
"export HOSTALIASES=/tmp/aliases",
|
||||
} {
|
||||
v := g.Judge(bash(command))
|
||||
if !v.Refuse {
|
||||
t.Errorf("%q was not refused", command)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(v.Message, "laptop/node-hostname.add") {
|
||||
t.Errorf("%q: the refusal does not name this machine's hosts verb:\n%s", command, v.Message)
|
||||
}
|
||||
}
|
||||
for _, tool := range []string{"Edit", "Write", "MultiEdit"} {
|
||||
v := g.Judge(HookInput{ToolName: tool, ToolInput: map[string]any{"file_path": "/etc/../etc/hosts"}})
|
||||
if !v.Refuse || !strings.Contains(v.Message, "node-hostname.add") {
|
||||
t.Errorf("%s of /etc/hosts was not refused with the verb: %+v", tool, v)
|
||||
}
|
||||
}
|
||||
// The resolver file has no verb that replaces writing it: the refusal says to create one.
|
||||
v := g.Judge(bash("echo nameserver 192.0.2.53 | sudo tee /etc/resolv.conf"))
|
||||
if !v.Refuse || !strings.Contains(v.Message, "a missing tool is created") {
|
||||
t.Errorf("writing the resolver file: %+v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// The agent never names the operator's override, and no override lifts that refusal.
|
||||
func TestACommandNamingTheOverrideIsRefused(t *testing.T) {
|
||||
g := guard()
|
||||
for _, command := range []string{
|
||||
"export " + OverrideVar + "=because",
|
||||
OverrideVar + "=x claude -p 'ssh anchor uptime'",
|
||||
"env " + OverrideVar + "=1 bash",
|
||||
} {
|
||||
v := g.Judge(bash(command))
|
||||
if !v.Refuse || v.Rule != "override-named" || v.Overridable {
|
||||
t.Errorf("%q: %+v", command, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Without the mesh's answer — a controller not asked yet — the mesh's own names are still refused.
|
||||
func TestWithoutTheMeshsAnswerItsNamesAreStillRefused(t *testing.T) {
|
||||
g := Guard{}
|
||||
if v := g.Judge(bash("ssh anchor.internal journalctl")); !v.Refuse || !strings.Contains(v.Message, "a missing tool is created") {
|
||||
t.Errorf("an internal name without data: %+v", v)
|
||||
}
|
||||
if v := g.Judge(bash("echo x | sudo tee -a /etc/hosts")); !v.Refuse {
|
||||
t.Errorf("the hosts file without data: %+v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// fakeProc lays out a process tree: the agent started with env, a shell under it, the hook under that.
|
||||
func fakeProc(t *testing.T, agentEnv []string) (root string, hookParent int) {
|
||||
t.Helper()
|
||||
root = t.TempDir()
|
||||
write := func(pid, ppid int, cmdline []string, env []string) {
|
||||
dir := filepath.Join(root, strconv.Itoa(pid))
|
||||
_ = os.MkdirAll(dir, 0o755)
|
||||
_ = os.WriteFile(filepath.Join(dir, "cmdline"), []byte(strings.Join(cmdline, "\x00")+"\x00"), 0o644)
|
||||
_ = os.WriteFile(filepath.Join(dir, "environ"), []byte(strings.Join(env, "\x00")), 0o644)
|
||||
_ = os.WriteFile(filepath.Join(dir, "stat"), []byte(strconv.Itoa(pid)+" (some (odd) name) S "+strconv.Itoa(ppid)+" 1 1"), 0o644)
|
||||
}
|
||||
write(100, 1, []string{"/opt/claude-code/bin/claude", "--resume"}, agentEnv)
|
||||
write(200, 100, []string{"/bin/sh", "-c", "hook"}, []string{OverrideVar + "=set-by-the-session"})
|
||||
return root, 200
|
||||
}
|
||||
|
||||
// **The override is the operator's, from the session as it was started, and recorded** — and a value a
|
||||
// command or a setting put in the session's later environment is not it.
|
||||
func TestTheOverrideIsTheOperatorsAndRecorded(t *testing.T) {
|
||||
data := meshForTheGuard()
|
||||
data.Log = filepath.Join(t.TempDir(), "guard.log")
|
||||
path := filepath.Join(t.TempDir(), "guard.json")
|
||||
raw, _ := json.Marshal(data)
|
||||
_ = os.WriteFile(path, raw, 0o644)
|
||||
was, wasPID := procRoot, parentPID
|
||||
t.Cleanup(func() { procRoot, parentPID = was, wasPID })
|
||||
run := func(command string) (int, string) {
|
||||
var stderr bytes.Buffer
|
||||
in, _ := json.Marshal(bash(command))
|
||||
return runGuard(path, bytes.NewReader(in), &stderr), stderr.String()
|
||||
}
|
||||
|
||||
// Not set where the session started: the later shell's value is not the operator's.
|
||||
root, hook := fakeProc(t, []string{"HOME=/home/operator"})
|
||||
procRoot, parentPID = root, func() int { return hook }
|
||||
if code, says := run("ssh anchor journalctl"); code != 2 || !strings.Contains(says, "anchor/node-service-manager.journal") {
|
||||
t.Fatalf("refused with %d: %s", code, says)
|
||||
}
|
||||
// Set by the operator before the session: let through, with why on record.
|
||||
root, hook = fakeProc(t, []string{"HOME=/home/operator", OverrideVar + "=reading the broker's log by hand while the systemd module is down"})
|
||||
procRoot, parentPID = root, func() int { return hook }
|
||||
if code, says := run("ssh anchor journalctl -u nats"); code != 0 {
|
||||
t.Fatalf("the operator's override was not honoured: %d %s", code, says)
|
||||
}
|
||||
// Never for a command naming the override itself.
|
||||
if code, _ := run("export " + OverrideVar + "=x"); code != 2 {
|
||||
t.Fatalf("a command naming the override passed under it: %d", code)
|
||||
}
|
||||
record := ReadGuardLog(data.Log, 10)
|
||||
if len(record) != 3 || record[0].Decision != "refused" || record[1].Decision != "overridden" ||
|
||||
!strings.Contains(record[1].Why, "systemd module is down") || record[1].Machine != "anchor" || record[2].Rule != "override-named" {
|
||||
t.Fatalf("the record: %+v", record)
|
||||
}
|
||||
// An override that cannot be recorded is not honoured.
|
||||
data.Log = filepath.Join(t.TempDir(), "no", "such", "dir", "guard.log")
|
||||
raw, _ = json.Marshal(data)
|
||||
_ = os.WriteFile(path, raw, 0o644)
|
||||
if code, says := run("ssh anchor journalctl"); code != 2 || !strings.Contains(says, "could not be recorded") {
|
||||
t.Fatalf("an override not recorded was honoured: %d %s", code, says)
|
||||
}
|
||||
}
|
||||
|
||||
// What passes is not recorded, and a guard that cannot read the call says so and blocks nothing.
|
||||
func TestTheHookExitsAsTheAgentReadsIt(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "guard.json")
|
||||
_ = os.WriteFile(path, []byte(`{}`), 0o644)
|
||||
var stderr bytes.Buffer
|
||||
in, _ := json.Marshal(bash("git push"))
|
||||
if code := runGuard(path, bytes.NewReader(in), &stderr); code != 0 || stderr.Len() != 0 {
|
||||
t.Errorf("git push: %d %q", code, stderr.String())
|
||||
}
|
||||
if code := runGuard(path, strings.NewReader("not json"), &stderr); code != 1 {
|
||||
t.Errorf("an unreadable call: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheAgentsProcessIsKnown(t *testing.T) {
|
||||
for cmdline, want := range map[string]bool{
|
||||
"/opt/claude-code/bin/claude\x00--resume\x00": true,
|
||||
"claude\x00": true,
|
||||
"node\x00/usr/lib/node_modules/@anthropic-ai/claude-code/cli.js\x00": true,
|
||||
"/usr/bin/node\x00/home/x/.local/bin/claude\x00": true,
|
||||
"/bin/zsh\x00": false,
|
||||
"node\x00server.js\x00": false,
|
||||
"/usr/bin/claude-code-tools\x00": false,
|
||||
} {
|
||||
if isAgent([]byte(cmdline)) != want {
|
||||
t.Errorf("%q: want %v", cmdline, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func FilesFor(p Paths) *Files {
|
||||
return &Files{Home: filepath.Clean(p.Home), Account: p.Agent, Helper: &Helper{Account: p.Agent, Exe: HelperPath()}}
|
||||
}
|
||||
|
||||
// HelperPath is the helper's binary: the plugin's copy of this module's binary in the plugin, root's and
|
||||
// HelperPath is the helper's binary: the guard's copy of this module's binary in the plugin, root's and
|
||||
// readable by every account, so the agent's account runs exactly what the mesh placed.
|
||||
func HelperPath() string {
|
||||
return filepath.Join(ManagedDir, MarketplaceDir, "plugins", Plugin, GuardDir, "guard")
|
||||
|
||||
@@ -4,10 +4,12 @@ package main
|
||||
// address, not this shell command — generated on every render from what each seat verb and module tool
|
||||
// says it replaces, so it cannot drift from the tools the mesh has.
|
||||
//
|
||||
// Short: a row per seat or module, the machines' seats first.
|
||||
// Short: a row per seat or module, its top what this machine's agent reached round most — the tools the
|
||||
// guard named most often in its record.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -29,15 +31,17 @@ func code(s string) string { return "`" + strings.ReplaceAll(s, "`", "'") + "`"
|
||||
|
||||
// insteadGroup is one row: a seat or a module, and its verbs that replace a command.
|
||||
type insteadGroup struct {
|
||||
prefix string // `<node>/node-service-manager.` or `mesh-controller.`
|
||||
verbs []Replacement
|
||||
prefix string // `<node>/node-service-manager.` or `mesh-controller.`
|
||||
verbs []Replacement
|
||||
refused int
|
||||
}
|
||||
|
||||
// InsteadOf is the section, or "" when the mesh said nothing replaces anything — an older controller,
|
||||
// or one not yet asked.
|
||||
//
|
||||
// One row per seat or module, every verb of it that replaces a command in the row, so a verb is never cut
|
||||
// for being late in its seat's list; the machines' seats first, the modules' tools and the mesh's own seats in the records' order.
|
||||
// for being late in its seat's list; the rows the guard here named most come first, then the machines'
|
||||
// seats, the modules' tools and the mesh's own seats in the records' order.
|
||||
func InsteadOf(m *MeshTools) string {
|
||||
if m == nil || len(m.Replacements) == 0 {
|
||||
return ""
|
||||
@@ -60,8 +64,13 @@ func InsteadOf(m *MeshTools) string {
|
||||
groups = append(groups, g)
|
||||
}
|
||||
g.verbs = append(g.verbs, r)
|
||||
g.refused += m.Refused[r.Address]
|
||||
}
|
||||
}
|
||||
sort.SliceStable(groups, func(i, j int) bool { return groups[i].refused > groups[j].refused })
|
||||
for _, g := range groups {
|
||||
sort.SliceStable(g.verbs, func(i, j int) bool { return m.Refused[g.verbs[i].Address] > m.Refused[g.verbs[j].Address] })
|
||||
}
|
||||
more := 0
|
||||
if len(groups) > InsteadOfRows {
|
||||
more = len(groups) - InsteadOfRows
|
||||
@@ -88,7 +97,7 @@ func InsteadOf(m *MeshTools) string {
|
||||
fmt.Fprintf(&b, "\n%d more seats and modules: `mesh_search` with the command finds the tool for it.\n", more)
|
||||
}
|
||||
b.WriteString("\n- **Never `ssh` to a mesh machine**, and never edit `/etc/hosts` or set `HOSTALIASES` for a mesh\n" +
|
||||
" name: call the tool the table names instead. Where no tool covers what you\n" +
|
||||
" name: the guard on this session's shell refuses it and names the tool. Where no tool covers what you\n" +
|
||||
" need, none is worked around: say so, and the tool is created in the module that owns it, on its seat.\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -99,10 +98,16 @@ func aCatalog() fakeCatalog {
|
||||
return f
|
||||
}
|
||||
|
||||
func resolving(name string) []string {
|
||||
return map[string][]string{"anchor": {"10.10.0.1"}, "anchor.internal": {"10.10.0.1"}, "anchor.example": {"192.0.2.10"},
|
||||
"laptop": {"127.0.0.1", "10.10.0.4"}}[name]
|
||||
}
|
||||
|
||||
// Issue 1126 B13: what the mesh says of its tools and machines is read from the bus whole, never asked of the
|
||||
// controller: the seats' verbs and the placed modules' own tools.
|
||||
// controller: the seats' verbs, the placed modules' own tools, every machine with its domains and addresses — a
|
||||
// loopback address never one of them.
|
||||
func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
m, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats())
|
||||
m, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats(), resolving)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -113,21 +118,12 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
if got := strings.Join(addresses, ","); got != "mesh-module-assigner.assignments,node-service-manager.status,node-service-manager.journal,docker.docker_logs" {
|
||||
t.Errorf("replacements: %s", got)
|
||||
}
|
||||
t.Run("the again tool answers every caller", func(t *testing.T) {
|
||||
// H-A-66 (design 47 H §752): no holder keeps a callers list; the seat list's tools answer every caller.
|
||||
files, _ := filepath.Glob("*.go")
|
||||
for _, f := range files {
|
||||
if strings.HasSuffix(f, "_test.go") {
|
||||
continue
|
||||
}
|
||||
raw, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, gone := range []string{"AnyPerson", "seatListAgainCallers", "seatlist.Refusal"} {
|
||||
if strings.Contains(string(raw), gone) {
|
||||
t.Errorf("%s still names %s", f, gone)
|
||||
}
|
||||
t.Run("the again tool answers the operator and the healers only", func(t *testing.T) {
|
||||
// An operator's session through the mesh MCP server is proved as its machine's tool runner (issue 627, issue 1966).
|
||||
for caller, refused := range map[string]bool{"person.jochen": false, "shanks.node-tools": false, "shanks.mesh-healers": false,
|
||||
"shanks.mesh-config": true} {
|
||||
if err := seatlist.Refusal("claude_code_seat_list_again", seatListAgainCallers, caller); (err != nil) != refused {
|
||||
t.Errorf("%s: refused=%v, err %v", caller, refused, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -148,7 +144,7 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatalf("the seat list was not read: %+v", seatList.Status())
|
||||
}
|
||||
got, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), definedSeats())
|
||||
got, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), definedSeats(), resolving)
|
||||
var addrs []string
|
||||
for _, r := range got.Replacements {
|
||||
addrs = append(addrs, r.Address)
|
||||
@@ -158,6 +154,10 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
t.Fatalf("replacements from the bus: %v, %v", addrs, err)
|
||||
}
|
||||
})
|
||||
if len(m.Machines) != 2 || strings.Join(m.Machines[0].Domains, ",") != "anchor.internal,anchor.example" ||
|
||||
strings.Join(m.Machines[0].Addresses, ",") != "10.10.0.1,192.0.2.10" || strings.Join(m.Machines[1].Addresses, ",") != "10.10.0.4" {
|
||||
t.Errorf("machines: %+v", m.Machines)
|
||||
}
|
||||
if m.Replacements[3].Does != "logs" || !m.Replacements[2].NodeScoped || m.Replacements[0].NodeScoped {
|
||||
t.Errorf("the rows: %+v", m.Replacements)
|
||||
}
|
||||
@@ -166,7 +166,7 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
// **The table is generated, not written** (novox/hq ADR 0245): what the records say a verb replaces is a row,
|
||||
// the machines' seats first, and nothing is rendered where the records say nothing.
|
||||
func TestTheInsteadOfTableIsGeneratedFromTheRecords(t *testing.T) {
|
||||
m, _ := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats())
|
||||
m, _ := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats(), resolving)
|
||||
table := InsteadOf(&m)
|
||||
for _, want := range []string{
|
||||
"| `<node>/node-service-manager.` | `status` — `systemctl status`, `systemctl is-active` · `journal` — `journalctl` |",
|
||||
@@ -182,11 +182,14 @@ func TestTheInsteadOfTableIsGeneratedFromTheRecords(t *testing.T) {
|
||||
strings.Index(table, "docker.") > strings.Index(table, "`mesh-module-assigner.`") {
|
||||
t.Errorf("not the machines' seats, then modules, then the mesh's:\n%s", table)
|
||||
}
|
||||
// No guard on the session's shell: the rule is the session's own, and the text claims no guard (the operator,
|
||||
// 2026-11-20).
|
||||
if strings.Contains(table, "guard") || !strings.Contains(table, "call the tool the table names instead") {
|
||||
t.Errorf("the rule names a guard, or not the tool to call:\n%s", table)
|
||||
// What the guard here named most comes first: its row, and in its row its verb.
|
||||
m.Refused = map[string]int{"node-service-manager.journal": 3, "mesh-module-assigner.assignments": 1}
|
||||
ranked := InsteadOf(&m)
|
||||
if !strings.Contains(ranked, "| `<node>/node-service-manager.` | `journal` — `journalctl` · `status`") ||
|
||||
strings.Index(ranked, "`mesh-module-assigner.`") > strings.Index(ranked, "`<node>/docker.`") {
|
||||
t.Errorf("not ranked by the guard's record:\n%s", ranked)
|
||||
}
|
||||
m.Refused = nil
|
||||
if InsteadOf(nil) != "" || InsteadOf(&MeshTools{}) != "" {
|
||||
t.Error("a table from nothing")
|
||||
}
|
||||
@@ -213,17 +216,21 @@ func TestTheInsteadOfTableIsGeneratedFromTheRecords(t *testing.T) {
|
||||
// Asked again with the same answer, nothing is written and nothing renders.
|
||||
func TestTheMeshsToolsAreKeptOnlyWhenTheyChange(t *testing.T) {
|
||||
p := Paths{State: t.TempDir()}
|
||||
m, _ := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats())
|
||||
m, _ := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats(), resolving)
|
||||
if !KeepMeshTools(p, m) {
|
||||
t.Fatal("the first answer was not kept")
|
||||
}
|
||||
if KeepMeshTools(p, m) {
|
||||
t.Fatal("the same answer was kept again")
|
||||
}
|
||||
m.Replacements = m.Replacements[:1]
|
||||
if !KeepMeshTools(p, m) || len(ReadMeshTools(p).Replacements) != 1 {
|
||||
m.Machines = m.Machines[:1]
|
||||
if !KeepMeshTools(p, m) || len(ReadMeshTools(p).Machines) != 1 {
|
||||
t.Fatal("a changed answer was not kept")
|
||||
}
|
||||
d := GuardDataOf(Paths{State: p.State, Node: "laptop"}, ReadMeshTools(p))
|
||||
if d.Node != "laptop" || len(d.Replacements) != 4 || !strings.HasSuffix(d.Log, "guard.log") {
|
||||
t.Errorf("the guard's data: %+v", d)
|
||||
}
|
||||
}
|
||||
|
||||
// Where a module runs is read from the seat mesh-module-assigner's assignments (issue 1126 B13), never the controller's answer.
|
||||
@@ -260,20 +267,20 @@ func TestTheMeshIsReadFromTheOwnersFirstViews(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
want, err := ReadMeshToolsFrom(bus, aCatalog(), someSeats())
|
||||
want, err := ReadMeshToolsFrom(bus, aCatalog(), someSeats(), resolving)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := ReadMeshToolsFrom(viewStates{r}, aCatalog(), someSeats())
|
||||
got, err := ReadMeshToolsFrom(viewStates{r}, aCatalog(), someSeats(), resolving)
|
||||
if err != nil || !reflect.DeepEqual(got, want) || len(got.Replacements) == 0 {
|
||||
t.Fatalf("from the views: %+v, %v\nfrom the state: %+v", got, err, want)
|
||||
}
|
||||
// A reader not yet answered says so, and reads nothing.
|
||||
// A catalog half with no first view says so, and reads no module as absent.
|
||||
if _, err := ReadMeshToolsFrom(bus, fakeCatalog{notYet: errors.New("the catalog half has no first view of main yet")}, someSeats()); err == nil || !strings.Contains(err.Error(), "no first view") {
|
||||
if _, err := ReadMeshToolsFrom(bus, fakeCatalog{notYet: errors.New("the catalog half has no first view of main yet")}, someSeats(), resolving); err == nil || !strings.Contains(err.Error(), "no first view") {
|
||||
t.Fatalf("an unanswered catalog half: %v", err)
|
||||
}
|
||||
if _, err := ReadMeshToolsFrom(viewStates{newMeshViews()}, aCatalog(), someSeats()); err == nil || !strings.Contains(err.Error(), "no first view") {
|
||||
if _, err := ReadMeshToolsFrom(viewStates{newMeshViews()}, aCatalog(), someSeats(), resolving); err == nil || !strings.Contains(err.Error(), "no first view") {
|
||||
t.Fatalf("an unanswered first view: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -281,7 +288,7 @@ func TestTheMeshIsReadFromTheOwnersFirstViews(t *testing.T) {
|
||||
// Issue 1126 B13 AC1 (ADR 0590): over the seats the mesh defines, the rendered instructions, their table among them,
|
||||
// name no address of the controller, which is not placed: each job is named by the seat that owns it now.
|
||||
func TestTheRenderedInstructionsNameNoControllerAddress(t *testing.T) {
|
||||
m, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats())
|
||||
m, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats(), resolving)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -339,10 +339,10 @@ func tools(p Paths, servers ServerState, view *ServerView, config ConfigState, c
|
||||
Description: "Claude Code on this machine as the mesh configured it: the licence it holds and when its token expires, what it reports holding, the managed files, the MCP servers registered here. Fingerprints only, never a token.",
|
||||
Run: func(map[string]any) (any, error) { return status(p), nil }},
|
||||
{Name: "claude_code_render",
|
||||
Description: "Write Claude Code's managed directory now, from the mesh's facts, this module's settings and the servers registered here — after reading the bus again for what each tool replaces (the instructions' table).",
|
||||
Description: "Write Claude Code's managed directory now, from the mesh's facts, this module's settings and the servers registered here — after reading the bus again for what each tool replaces and which machines the mesh has (the instructions' table and the shell's guard).",
|
||||
Run: func(map[string]any) (any, error) {
|
||||
answer := map[string]any{}
|
||||
if m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats()); err != nil {
|
||||
if m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats(), lookup); err != nil {
|
||||
answer["asked"] = "the bus could not be read in full (" + err.Error() + "); what was kept is rendered"
|
||||
} else {
|
||||
KeepMeshTools(p, m)
|
||||
@@ -351,6 +351,16 @@ func tools(p Paths, servers ServerState, view *ServerView, config ConfigState, c
|
||||
answer["rendered"] = out
|
||||
return answer, err
|
||||
}},
|
||||
{Name: "claude_code_guard",
|
||||
Description: "The guard on the agent's shell on this machine (novox/hq ADR 0245): what it refuses — ssh to a mesh machine, writing /etc/hosts or /etc/resolv.conf, HOSTALIASES — the machines and the replaced commands it judges with, the \"instead of\" table rendered into the agent's instructions, and its record of what it refused and what the operator's override let through, newest last.",
|
||||
Input: map[string]any{"last": map[string]any{"type": "integer", "description": "how many lines of the record (default 50)"}},
|
||||
Run: func(a map[string]any) (any, error) {
|
||||
last := 50
|
||||
if n, ok := a["last"].(float64); ok && n > 0 {
|
||||
last = int(n)
|
||||
}
|
||||
return GuardStatus(p, last), nil
|
||||
}},
|
||||
{Name: "claude_code_pull",
|
||||
Description: "Ask the licence manager which licence this node holds and the vault for its access token now, and apply it, rather than waiting for its binding or the token to change.",
|
||||
Run: func(map[string]any) (any, error) { return Pull(p, ask, writeManaged) }},
|
||||
@@ -442,6 +452,10 @@ func persist(what string, attempt func() error, done func(refusals int)) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// The guard on the agent's shell (novox/hq ADR 0245): this binary, run by the agent's hook.
|
||||
if len(os.Args) == 3 && os.Args[1] == "guard" {
|
||||
os.Exit(runGuard(os.Args[2], os.Stdin, os.Stderr))
|
||||
}
|
||||
// The plugin's SessionStart hook (novox/hq ADR 0489 §1, issue 1744): names the session's id and its open waits.
|
||||
if len(os.Args) == 3 && os.Args[1] == "session-start" {
|
||||
os.Exit(runSessionStart(os.Args[2], os.Stdin, os.Stdout))
|
||||
@@ -720,12 +734,17 @@ func refusals(n int) string {
|
||||
return fmt.Sprintf(" (after %d refusal(s))", n)
|
||||
}
|
||||
|
||||
// seatListTools are the seat list reader's two tools: what the list holds and what it waits for, and a read now, each
|
||||
// answering every caller (design 47 H §752, H-A-66). The reader starts once the runtime serves, after the tools are
|
||||
// listed, so each tool finds it when it is called.
|
||||
// seatListAgainCallers may read the seat list again: every operator's session and the healers (ADR 0636 §7). A call through
|
||||
// the mesh MCP server is proved only as its machine's tool runner (<node>.node-tools, issue 627), so node-tools stands for
|
||||
// an operator's session until a session is proved (issue 1966).
|
||||
var seatListAgainCallers = []string{stdio.AnyPerson, "node-tools", "mesh-healers"}
|
||||
|
||||
// seatListTools are the seat list reader's two tools: what the list holds and what it waits for, and a read now. The
|
||||
// reader starts once the runtime serves, after the tools are listed, so each tool finds it when it is called.
|
||||
func seatListTools() []stdio.Tool {
|
||||
names := []string{"claude_code_seat_list", "claude_code_seat_list_again"}
|
||||
shape := seatlist.Tools(&seatlist.Reader{}, names[0], names[1])
|
||||
callers := seatListAgainCallers
|
||||
shape := seatlist.Tools(&seatlist.Reader{}, names[0], names[1], callers)
|
||||
for i := range shape {
|
||||
run := i
|
||||
shape[i].Run = func(args map[string]any) (any, error) {
|
||||
@@ -735,7 +754,7 @@ func seatListTools() []stdio.Tool {
|
||||
if r == nil {
|
||||
return nil, errors.New("the seat list reader has not started yet: the runtime has not served this module")
|
||||
}
|
||||
return seatlist.Tools(r, names[0], names[1])[run].Run(args)
|
||||
return seatlist.Tools(r, names[0], names[1], callers)[run].Run(args)
|
||||
}
|
||||
}
|
||||
return shape
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
package main
|
||||
|
||||
// What the mesh says about its own tools, as the agent's instructions need it (novox/hq ADR 0245): every seat verb
|
||||
// and module tool that says what shell command it replaces.
|
||||
// What the mesh says about its own tools and machines, as the agent's instructions and the guard on its
|
||||
// shell need it (novox/hq ADR 0245): every seat verb and module tool that says what shell command it
|
||||
// replaces, and the mesh's machines by every name and address a session could reach one by.
|
||||
//
|
||||
// Read from the bus at start and whenever a followed state changes (issue 1126 B13; the controller that answered this before is
|
||||
// Read from the bus at start and every few minutes (issue 1126 B13; the controller that answered this before is
|
||||
// not placed, novox/hq ADR 0590): the seats' verbs from the seats' contract (mesh-contracts), the modules'
|
||||
// own tools from the manifests the indexer keeps (`mesh-delivery-indexer.modules` and `.manifests`), and where
|
||||
// each module runs from the seat mesh-module-assigner's `assignments`. Kept in the module's state so
|
||||
// own tools from the manifests the indexer keeps (`mesh-delivery-indexer.modules` and `.manifests`), and the
|
||||
// machines and where each module runs from the seat mesh-module-assigner's `assignments`. Kept in the module's state so
|
||||
// a render never waits for the bus, and rendered whenever it changes. Nothing here is written by hand: a verb
|
||||
// that gains a `replaces` is in the next render's table.
|
||||
// that gains a `replaces` is in the next render's table and the guard's next refusal.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
@@ -42,13 +45,39 @@ type Replacement struct {
|
||||
On []string `json:"on,omitempty"`
|
||||
}
|
||||
|
||||
// Machine is one of the mesh's machines and the names and addresses it is reached by.
|
||||
type Machine struct {
|
||||
Name string `json:"name"`
|
||||
Domains []string `json:"domains,omitempty"`
|
||||
Addresses []string `json:"addresses,omitempty"`
|
||||
}
|
||||
|
||||
// MeshTools is what the bus said, as kept in the module's state.
|
||||
type MeshTools struct {
|
||||
Replacements []Replacement `json:"replacements"`
|
||||
Machines []Machine `json:"machines"`
|
||||
// Asked is when the bus was last read whole.
|
||||
Asked string `json:"asked,omitempty"`
|
||||
// Refused is how often the guard here named each tool instead of a work-around, from its record: the
|
||||
// table's order. Never kept — read from the record at each render.
|
||||
Refused map[string]int `json:"-"`
|
||||
}
|
||||
|
||||
// RefusedCounts are how often the guard's record named each tool, over its last lines.
|
||||
func RefusedCounts(log string) map[string]int {
|
||||
out := map[string]int{}
|
||||
for _, e := range ReadGuardLog(log, 2000) {
|
||||
for _, t := range e.Tools {
|
||||
out[t]++
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// InternalSuffix is the mesh's own names' domain: every machine and service is `<name>.internal`
|
||||
// (novox/hq ADR 0194), and a session reaches none of them by ssh.
|
||||
const InternalSuffix = ".internal"
|
||||
|
||||
func (p Paths) meshTools() string { return filepath.Join(p.State, "mesh-tools.json") }
|
||||
|
||||
// ReadMeshTools is what was kept, or nil.
|
||||
@@ -205,8 +234,17 @@ func ReplacementsOf(defined []seatdef.Definition, modules []ModuleTools) []Repla
|
||||
return out
|
||||
}
|
||||
|
||||
// ReadMeshToolsFrom reads the bus now: the seats' verbs and the placed modules' own tools. A part that cannot be read is left out and said in the error; what was read is kept.
|
||||
func ReadMeshToolsFrom(st States, cat CatalogHalf, defined []seatdef.Definition) (MeshTools, error) {
|
||||
// lookup resolves one name, briefly: a name that does not resolve here is no address.
|
||||
func lookup(name string) []string {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
addrs, _ := net.DefaultResolver.LookupHost(ctx, name)
|
||||
return addrs
|
||||
}
|
||||
|
||||
// ReadMeshToolsFrom reads the bus now: the seats' verbs, the placed modules' own tools, and every node assignments names
|
||||
// with its domains and addresses. A part that cannot be read is left out and said in the error; what was read is kept.
|
||||
func ReadMeshToolsFrom(st States, cat CatalogHalf, defined []seatdef.Definition, resolve func(string) []string) (MeshTools, error) {
|
||||
var m MeshTools
|
||||
placed, err := assignments(st)
|
||||
if err != nil {
|
||||
@@ -215,6 +253,24 @@ func ReadMeshToolsFrom(st States, cat CatalogHalf, defined []seatdef.Definition)
|
||||
}
|
||||
modules, merr := modulesTools(cat, runsOn(placed))
|
||||
m.Replacements = ReplacementsOf(defined, modules)
|
||||
for node, p := range placed {
|
||||
machine := Machine{Name: node, Domains: []string{node + InternalSuffix}}
|
||||
if d := strings.ToLower(strings.TrimSpace(p.Decided.PublicDomain)); d != "" {
|
||||
machine.Domains = append(machine.Domains, d)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, name := range append([]string{node}, machine.Domains...) {
|
||||
for _, a := range resolve(name) {
|
||||
if !seen[a] && !net.ParseIP(a).IsLoopback() {
|
||||
seen[a] = true
|
||||
machine.Addresses = append(machine.Addresses, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(machine.Addresses)
|
||||
m.Machines = append(m.Machines, machine)
|
||||
}
|
||||
sort.Slice(m.Machines, func(i, j int) bool { return m.Machines[i].Name < m.Machines[j].Name })
|
||||
return m, merr
|
||||
}
|
||||
|
||||
@@ -239,9 +295,22 @@ func KeepMeshTools(p Paths, m MeshTools) bool {
|
||||
return os.Rename(tmp, p.meshTools()) == nil
|
||||
}
|
||||
|
||||
// hookFiles is what the plugin's hooks run: this module's own executable and the waits directory. None where the
|
||||
// GuardDataOf is what the guard judges with on this machine.
|
||||
func GuardDataOf(p Paths, m *MeshTools) GuardData {
|
||||
d := GuardData{Node: p.Node, Log: filepath.Join(p.State, "guard.log"), Machines: []Machine{}, Replacements: []Replacement{}}
|
||||
if p.Confined() {
|
||||
// The session's account cannot write the module's state, and must not rewrite its own record (ADR 0266).
|
||||
d.Agent, d.Log = p.Agent, JournalRecord+GuardJournalID
|
||||
}
|
||||
if m != nil {
|
||||
d.Machines, d.Replacements = m.Machines, m.Replacements
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// guardFiles is the guard to place: this module's own executable and what it judges with. None where the
|
||||
// executable cannot be named — then no hook is written, rather than one that cannot run.
|
||||
func hookFiles(p Paths) *HookFiles {
|
||||
func guardFiles(p Paths, m *MeshTools) *GuardFiles {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -249,13 +318,18 @@ func hookFiles(p Paths) *HookFiles {
|
||||
if resolved, err := filepath.EvalSymlinks(exe); err == nil {
|
||||
exe = resolved
|
||||
}
|
||||
return &HookFiles{Exe: exe, WaitsDir: WaitsDirPath(p)}
|
||||
return &GuardFiles{Exe: exe, Data: GuardDataOf(p, m), WaitsDir: WaitsDirPath(p)}
|
||||
}
|
||||
|
||||
// MeshToolsEvery is how often the machines' addresses are resolved again; the tools and the assignments are read again
|
||||
// when their followed states change (a views event), not on this timer.
|
||||
const MeshToolsEvery = 10 * time.Minute
|
||||
|
||||
// MeshToolsFirst is how long the first read waits for the first views to be answered.
|
||||
const MeshToolsFirst = 2 * time.Minute
|
||||
|
||||
// refreshMeshTools reads the pictures once the first views are answered, and again whenever one changes, and renders when what it said changed.
|
||||
// refreshMeshTools reads the pictures once the first views are answered, and again whenever one changes (and the
|
||||
// machines' addresses every few minutes), and renders when what it said changed.
|
||||
func refreshMeshTools(p Paths) {
|
||||
select {
|
||||
case <-meshViews.Ready():
|
||||
@@ -264,26 +338,60 @@ func refreshMeshTools(p Paths) {
|
||||
}
|
||||
startSeatList(seatlist.StdioBus())
|
||||
for {
|
||||
m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats())
|
||||
m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats(), lookup)
|
||||
if err != nil {
|
||||
say("reading what each tool replaces: %v", err)
|
||||
say("reading what each tool replaces and which machines the mesh has: %v", err)
|
||||
}
|
||||
if (err == nil || len(m.Replacements) > 0) && KeepMeshTools(p, m) {
|
||||
if _, err := RenderNow(p, writeManaged); err != nil {
|
||||
say("rendering what the bus said of the tools: %v", err)
|
||||
} else {
|
||||
say("the mesh's tools changed: %d replace a command; rendered", len(m.Replacements))
|
||||
say("the mesh's tools changed: %d replace a command, %d machines; rendered", len(m.Replacements), len(m.Machines))
|
||||
}
|
||||
}
|
||||
<-meshChanged
|
||||
time.Sleep(2 * time.Second) // a burst of events is one read
|
||||
select {
|
||||
case <-meshChanged:
|
||||
default:
|
||||
time.Sleep(2 * time.Second) // a burst of events is one read
|
||||
select {
|
||||
case <-meshChanged:
|
||||
default:
|
||||
}
|
||||
case <-time.After(MeshToolsEvery):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GuardStatus is what claude_code_guard answers.
|
||||
func GuardStatus(p Paths, last int) map[string]any {
|
||||
m := ReadMeshTools(p)
|
||||
d := GuardDataOf(p, m)
|
||||
asked := "never: the bus has not been read whole yet"
|
||||
if m != nil {
|
||||
asked = m.Asked
|
||||
}
|
||||
return map[string]any{
|
||||
"refuses": []string{
|
||||
"ssh, scp, sftp, rsync, mosh or autossh to a mesh machine: its name, a name under its domains, any *" + InternalSuffix +
|
||||
" name or one of its addresses, read as ssh reads it (ssh -G); a jump through one too. An ssh login as `" +
|
||||
ForgeUser + "` is the forge's account and passes",
|
||||
"writing /etc/hosts or /etc/resolv.conf, by the shell or the agent's Edit and Write",
|
||||
"HOSTALIASES, named anywhere in a command",
|
||||
"any command naming " + OverrideVar,
|
||||
"where the machine names an agent account of its own (novox/hq ADR 0266): every command and file edit of a " +
|
||||
"session that does not run as it, whatever it is, and no override lifts it",
|
||||
},
|
||||
"agent_account": map[bool]string{true: p.Agent, false: "none of its own: agents run as the operator's account"}[p.Confined()],
|
||||
"override": OverrideVar + "=<why>, set in the operator's own shell before the session starts; read from the session's " +
|
||||
"environment as it was started, recorded with why, and not honoured when it cannot be recorded",
|
||||
"hook": GuardCommand(),
|
||||
"asked": asked,
|
||||
"machines": d.Machines,
|
||||
"replacements": d.Replacements,
|
||||
"instead_of": InsteadOf(m),
|
||||
"record": ReadGuardLog(d.Log, last),
|
||||
}
|
||||
}
|
||||
|
||||
// seatList is the seat list this module holds: the definitions of the seats, read from the seat mesh-catalog and
|
||||
// followed on its events (novox/hq ADR 0635 §1, ADR 0636), the foundation's bootstrapped ones until it answers.
|
||||
var seatList *seatlist.Reader
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -85,8 +84,8 @@ func PathsFrom(env func(string) string) (Paths, bool) {
|
||||
Agent: env("MESH_AGENT_ACCOUNT"), Operator: env("MESH_OPERATOR_ACCOUNT"),
|
||||
AgentHome: env("MESH_AGENT_HOME"), OperatorHome: env("MESH_OPERATOR_HOME")}
|
||||
if (p.Home == "") != (p.Agent == "") {
|
||||
// Half of the agent's account is not "the operator's": taken so, the agent's
|
||||
// home would never be written, and nothing would say so. Not launched, said (main).
|
||||
// Half of the agent's account is not "the operator's": taken so, the agent's account rule of the guard
|
||||
// would be off and its home never written, and nothing would say so. Not launched, said (main).
|
||||
return p, false
|
||||
}
|
||||
if p.Home == "" {
|
||||
@@ -144,7 +143,6 @@ func HomeScopeRefused(p Paths, target string) string {
|
||||
func (p Paths) credentials() string { return filepath.Join(p.Home, ".claude", ".credentials.json") }
|
||||
func (p Paths) account() string { return filepath.Join(p.Home, ".claude.json") }
|
||||
func (p Paths) binding() string { return filepath.Join(p.State, "licence.json") }
|
||||
func (p Paths) handed() string { return filepath.Join(p.State, "login-handed") }
|
||||
func (p Paths) apiKey() string { return filepath.Join(p.keyDir(), "api-key") }
|
||||
func (p Paths) helper() string { return filepath.Join(p.keyDir(), "api-key-helper") }
|
||||
|
||||
@@ -258,8 +256,11 @@ func RenderNow(p Paths, write WriteManaged) ([]string, error) {
|
||||
config.Approved = ApprovedHere(approved, p.Node)
|
||||
}
|
||||
mesh := ReadMeshTools(p)
|
||||
if mesh != nil {
|
||||
mesh.Refused = RefusedCounts(GuardDataOf(p, mesh).Log)
|
||||
}
|
||||
files := RenderWithMesh(facts, settings, binding, p.helperCommand(), Registered(p), config, mesh)
|
||||
tree, _ := json.Marshal(MarketplaceWith(config, hookFiles(p)))
|
||||
tree, _ := json.Marshal(MarketplaceWith(config, guardFiles(p, mesh)))
|
||||
files[MarketplaceDir+"/"] = string(tree)
|
||||
names := make([]string, 0, len(files))
|
||||
for n := range files {
|
||||
@@ -417,26 +418,9 @@ func GrantFor(p Paths, managerPublicKey string) (GrantAnswer, error) {
|
||||
if err != nil {
|
||||
return GrantAnswer{}, err
|
||||
}
|
||||
// Which login was handed: only its successor may replace it (ADR 0206, ADR 0209). The fingerprint, never
|
||||
// the token.
|
||||
if err := fsys.MkdirAll(p.State, 0o700); err != nil {
|
||||
return GrantAnswer{}, err
|
||||
}
|
||||
if err := fsys.WriteFile(p.handed(), []byte(Fingerprint(rt)+"\n"), 0o600); err != nil {
|
||||
return GrantAnswer{}, err
|
||||
}
|
||||
return GrantAnswer{Sealed: &box, Identity: ReadIdentity(p.account()), Fingerprint: Fingerprint(rt)}, nil
|
||||
}
|
||||
|
||||
// readHanded is the fingerprint of the login last handed to the manager, or "".
|
||||
func readHanded(p Paths) string {
|
||||
raw, err := fsys.ReadFile(p.handed())
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(string(raw))
|
||||
}
|
||||
|
||||
// Pull asks the seat which licence this node holds, then the vault for that licence's access secret, sealed to this
|
||||
// module's key, and applies it (§19).
|
||||
func Pull(p Paths, ask Ask, write WriteManaged) (map[string]any, error) {
|
||||
@@ -625,37 +609,9 @@ func Apply(p Paths, c Current, sealed SealedBox, write WriteManaged) (map[string
|
||||
return nil, err
|
||||
}
|
||||
local := ReadCredentials(p.credentials())
|
||||
// A login made here that the manager has not adopted yet stays whole: the grant handed is the old
|
||||
// lineage's, and writing it would spend the person's login unseen (ADR 0206, ADR 0209). The manager
|
||||
// asks claude_code_grant for it on this node's report; its successor then comes as a newer generation.
|
||||
// This return skips the binding's generation below on purpose: later binding events pull again until
|
||||
// the manager adopts the login.
|
||||
// The latest login wins (design 39 §6): a lease of the same account that succeeds the login (H-A-139) —
|
||||
// the manager adopted a later login to that account from another node — replaces it, said as "login
|
||||
// superseded"; a login to another account than the lease's licence is always kept (H-A-140, hq
|
||||
// design/47-H1-answers-59 b75d1386).
|
||||
superseded := false
|
||||
if LoginNotHanded(local, readHanded(p)) {
|
||||
id := ReadIdentity(p.account())
|
||||
if id == nil || id.EmailAddress == "" || id.EmailAddress != c.Licence || !Succeeds(local, g) {
|
||||
log.Printf("login kept: the login in the credentials file was not handed to the manager yet (licence %s)", c.Licence)
|
||||
return map[string]any{"applied": false, "licence": c.Licence, "reason": LoginKept}, nil
|
||||
}
|
||||
superseded = true
|
||||
log.Printf("login superseded: a later login to the account of licence %s was adopted by the manager (generation %d)", c.Licence, c.Generation)
|
||||
}
|
||||
// A login handed to the manager is replaced only by a grant of a newer lineage (its refresh token's
|
||||
// expiry later than the login's, or the same lineage with a later access expiry, as the manager's refresh yields it): a lease older than the login is never
|
||||
// written, so the manager's failed refresh is a finding on the manager, never a lost login here
|
||||
// (H-A-138, hq design/47-H1-answers-59 175ed320; H-A-139, e359911e).
|
||||
if HoldsLogin(local) && !Succeeds(local, g) {
|
||||
log.Printf("login kept: the grant offered for licence %s does not succeed the login in the credentials file", c.Licence)
|
||||
return map[string]any{"applied": false, "licence": c.Licence, "reason": LoginKept}, nil
|
||||
}
|
||||
// A login waiting here was handed to the manager first (ADR 0206): what comes back is its successor,
|
||||
// and the refresh token in the file is the one the manager just spent.
|
||||
d := DecideApply(GrantOf(local), g, switched || HoldsLogin(local))
|
||||
if d.Apply && superseded {
|
||||
out["reason"] = "login superseded"
|
||||
}
|
||||
if d.Apply {
|
||||
next := WithGrant(local, g)
|
||||
if switched {
|
||||
|
||||
@@ -114,7 +114,7 @@ func TestSessionStartRejectsAnUnsafeSessionID(t *testing.T) {
|
||||
}
|
||||
|
||||
// 1744/AC4 is checked together with 1743/AC7 in terminal_only_test.go's
|
||||
// TestThePluginsOnlyHooksAreTheSessionStartHookAndTheStopHook.
|
||||
// TestThePluginsOnlyHooksAreTheGuardTheSessionStartHookAndTheStopHook.
|
||||
|
||||
// ADR 0490 §2: every session start lists every name on this node with an open wait, from the node-wide
|
||||
// index, with the exact adopt call to make if it is yours — read directly, never by calling back into this
|
||||
|
||||
@@ -320,9 +320,9 @@ func TestWaitBlocksStopAdvisesMeshWaitOnItsEventOtherwiseAWatcher(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
// 1743/AC7's rendering half (hooks.json holds the Stop entry) is checked
|
||||
// 1743/AC7's rendering half (hooks.json holds the guard's PreToolUse entry and the Stop entry) is checked
|
||||
// together with 1744/AC4 in terminal_only_test.go's
|
||||
// TestThePluginsOnlyHooksAreTheSessionStartHookAndTheStopHook. claude_code_hook_register still
|
||||
// TestThePluginsOnlyHooksAreTheGuardTheSessionStartHookAndTheStopHook. claude_code_hook_register still
|
||||
// refuses a Stop hook (or any other) at every scope.
|
||||
func TestHookRegisterStillRefusesEveryHook(t *testing.T) {
|
||||
p, _ := node(t, "laptop")
|
||||
|
||||
@@ -261,13 +261,12 @@ func TestTheOperatorsManagedSettingsAreWhatSessionsObey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The plugin's only hooks are the session-start hook and the Stop hook (novox/hq ADR 0489) — no guard on the
|
||||
// shell (the operator, 2026-11-20), and never a hook a registration named, whatever the configuration given holds
|
||||
// (novox/hq issue 340).
|
||||
func TestThePluginsOnlyHooksAreTheSessionStartHookAndTheStopHook(t *testing.T) {
|
||||
// The plugin's only hooks are the guard, the session-start hook and the Stop hook (novox/hq ADR 0245, ADR
|
||||
// 0489) — never a hook a registration named, whatever the configuration given holds (novox/hq issue 340).
|
||||
func TestThePluginsOnlyHooksAreTheGuardTheSessionStartHookAndTheStopHook(t *testing.T) {
|
||||
c := Config{Mesh: []Item{{Kind: KindHook, Name: "planted", Scope: ScopeMesh, Event: "PreToolUse", Matcher: "Bash",
|
||||
Command: "${HOOK_DIR}/x.sh", Files: map[string]string{"x.sh": "#!/bin/sh\n"}}}}
|
||||
plugin := MarketplaceWith(c, &HookFiles{Exe: "/bin/true", WaitsDir: "/home/agent/.claude/nox-mesh/waits"})
|
||||
plugin := MarketplaceWith(c, &GuardFiles{Exe: "/bin/true", WaitsDir: "/home/agent/.claude/nox-mesh/waits"})
|
||||
root := "plugins/" + Plugin + "/"
|
||||
var hooks struct {
|
||||
Hooks map[string][]struct {
|
||||
@@ -280,14 +279,9 @@ func TestThePluginsOnlyHooksAreTheSessionStartHookAndTheStopHook(t *testing.T) {
|
||||
if err := json.Unmarshal([]byte(plugin[root+"hooks/hooks.json"].Content), &hooks); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(hooks.Hooks) != 2 || hooks.Hooks["PreToolUse"] != nil {
|
||||
t.Fatalf("a hook beside the session-start and Stop hooks: %s", plugin[root+"hooks/hooks.json"].Content)
|
||||
}
|
||||
if plugin[root+GuardDir+"/guard"].From != "/bin/true" {
|
||||
t.Fatalf("the binary the hooks run is not placed: %+v", plugin[root+GuardDir+"/guard"])
|
||||
}
|
||||
if _, ok := plugin[root+GuardDir+"/guard.json"]; ok {
|
||||
t.Fatal("the guard's data is placed")
|
||||
if len(hooks.Hooks) != 3 || len(hooks.Hooks["PreToolUse"]) != 1 || hooks.Hooks["PreToolUse"][0].Matcher != GuardMatcher ||
|
||||
hooks.Hooks["PreToolUse"][0].Hooks[0].Command != GuardCommand() {
|
||||
t.Fatalf("the guard's hook: %s", plugin[root+"hooks/hooks.json"].Content)
|
||||
}
|
||||
if len(hooks.Hooks["SessionStart"]) != 1 || hooks.Hooks["SessionStart"][0].Matcher != "startup|resume|clear|compact" ||
|
||||
hooks.Hooks["SessionStart"][0].Hooks[0].Command != SessionStartCommand("/home/agent/.claude/nox-mesh/waits") {
|
||||
|
||||
@@ -20,7 +20,7 @@ package main
|
||||
// Every file lives under the agent's home (the manifest's `agent-home` resource), which this process —
|
||||
// launched as the operator's account (novox/hq ADR 0193) — cannot itself write when the node names an agent
|
||||
// account of its own (novox/hq ADR 0266): every write below goes through `fsys` (homefs.go), which already
|
||||
// carries such a write through the sudo helper this module's binary serves, exactly as grant.go's
|
||||
// carries such a write through the sudo helper the guard's binary also serves, exactly as grant.go's
|
||||
// WriteCredentials does for the agent's own credentials file. Where the node has no agent account of its
|
||||
// own, fsys is the plain filesystem and the write reaches the operator's own home directly.
|
||||
|
||||
|
||||
@@ -5,5 +5,9 @@ agent='${machine:agent-account}'
|
||||
if [ "$(id -un)" = "$agent" ]; then
|
||||
exec claude "$@"
|
||||
fi
|
||||
# Another account, the operator's, becomes the agent's through its own sudo: the person is the authority.
|
||||
# Another account, the operator's, becomes the agent's through its own sudo: the person is the authority. The
|
||||
# operator's override of the guard travels only when it is set in this shell, as it would reach claude.
|
||||
if [ -n "$MESH_GUARD_OVERRIDE" ]; then
|
||||
exec sudo -iu "$agent" env MESH_GUARD_OVERRIDE="$MESH_GUARD_OVERRIDE" claude "$@"
|
||||
fi
|
||||
exec sudo -iu "$agent" claude "$@"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "claude-code",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"moved": true,
|
||||
"slug": "agent",
|
||||
"capabilities": [
|
||||
@@ -24,7 +23,7 @@
|
||||
"findings-stated.*"
|
||||
],
|
||||
"max-age-days": 7,
|
||||
"max-mib": 8
|
||||
"max-mib": 16
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -64,7 +63,7 @@
|
||||
"mesh-catalog.seat-definition-removed",
|
||||
"mesh-conditions.findings-asked",
|
||||
"claude-licence-manager.holdings.asked",
|
||||
"claude-licence-manager.binding.changed.*",
|
||||
"claude-licence-manager.binding.changed",
|
||||
"mesh-vault.secret-changed"
|
||||
],
|
||||
"state": [
|
||||
@@ -83,6 +82,7 @@
|
||||
"tools": [
|
||||
"claude_code_status",
|
||||
"claude_code_render",
|
||||
"claude_code_guard",
|
||||
"claude_code_pull",
|
||||
"claude_code_grant",
|
||||
"claude_code_add_api_key",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "claude-licence-manager",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"slug": "licmgr",
|
||||
"runs-as": "claude-licence-manager",
|
||||
"requires": [
|
||||
|
||||
@@ -1,19 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in
|
||||
// every desktop module that carries it.
|
||||
|
||||
type manifest struct {
|
||||
Module string `json:"module"`
|
||||
Version string `json:"version"`
|
||||
Moved bool `json:"moved"`
|
||||
Items []item `json:"items"`
|
||||
Capabilities []string `json:"capabilities"`
|
||||
Requires []string `json:"requires"`
|
||||
Claims []claim `json:"claims"`
|
||||
Seats []any `json:"seats"`
|
||||
Tools []string `json:"tools"`
|
||||
Environment *environment `json:"environment"`
|
||||
Shell []shellCode `json:"shell"`
|
||||
Resources []map[string]any `json:"resources"`
|
||||
// Lines for other modules' seats (novox/hq ADR 0212): the window manager's, here.
|
||||
Contributions []any `json:"contributions"`
|
||||
Build struct {
|
||||
Artifacts []map[string]any `json:"artifacts"`
|
||||
} `json:"build"`
|
||||
}
|
||||
|
||||
type claim struct {
|
||||
Name string `json:"name"`
|
||||
Scope string `json:"scope"`
|
||||
Serves []string `json:"serves"`
|
||||
}
|
||||
|
||||
type environment struct {
|
||||
Variables map[string]string `json:"variables"`
|
||||
Path []map[string]any `json:"path"`
|
||||
}
|
||||
|
||||
type shellCode struct {
|
||||
For string `json:"for"`
|
||||
Slot string `json:"slot"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
// An item of a collection (novox/hq ADR 0349 clause 13): its text is the template file beside the manifest.
|
||||
type item struct {
|
||||
Collection string `json:"collection"`
|
||||
Name string `json:"name"`
|
||||
Attributes map[string]string `json:"attributes"`
|
||||
Template string `json:"template"`
|
||||
}
|
||||
|
||||
// shell is the code the module adds to a shell's or the session's start, read from the templates of its items of
|
||||
// the collections shell:<file>:<slot>, in the order they are declared.
|
||||
func shell(t *testing.T, m catalogue.Manifest) []catalogue.ShellCode {
|
||||
func (m manifest) shell(t *testing.T) []shellCode {
|
||||
t.Helper()
|
||||
var out []catalogue.ShellCode
|
||||
var out []shellCode
|
||||
for _, it := range m.Items {
|
||||
parts := strings.Split(it.Collection, ":")
|
||||
if len(parts) != 3 || parts[0] != "shell" {
|
||||
@@ -23,15 +70,15 @@ func shell(t *testing.T, m catalogue.Manifest) []catalogue.ShellCode {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out = append(out, catalogue.ShellCode{For: parts[1], Slot: parts[2], Code: string(text)})
|
||||
out = append(out, shellCode{For: parts[1], Slot: parts[2], Code: string(text)})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// environmentOf is the variables the module sets, read from the templates of its items of the environment.
|
||||
func environmentOf(t *testing.T, m catalogue.Manifest) *catalogue.Environment {
|
||||
func (m manifest) environmentOf(t *testing.T) *environment {
|
||||
t.Helper()
|
||||
var env *catalogue.Environment
|
||||
var env *environment
|
||||
for _, it := range m.Items {
|
||||
if it.Collection != "environment" || it.Attributes["kind"] != "variable" {
|
||||
continue
|
||||
@@ -41,14 +88,30 @@ func environmentOf(t *testing.T, m catalogue.Manifest) *catalogue.Environment {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if env == nil {
|
||||
env = &catalogue.Environment{Variables: map[string]string{}}
|
||||
env = &environment{Variables: map[string]string{}}
|
||||
}
|
||||
env.Variables[it.Attributes["name"]] = strings.TrimSuffix(string(text), "\n")
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any {
|
||||
func readManifest(t *testing.T) manifest {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dec := json.NewDecoder(strings.NewReader(string(raw)))
|
||||
dec.DisallowUnknownFields()
|
||||
var m manifest
|
||||
if err := dec.Decode(&m); err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
m.Environment = m.environmentOf(t)
|
||||
return m
|
||||
}
|
||||
|
||||
func (m manifest) resource(t *testing.T, id string) map[string]any {
|
||||
t.Helper()
|
||||
for _, r := range m.Resources {
|
||||
if r["id"] == id {
|
||||
@@ -59,7 +122,7 @@ func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any {
|
||||
return nil
|
||||
}
|
||||
|
||||
func packages(m catalogue.Manifest) (present, absent []string) {
|
||||
func (m manifest) packages() (present, absent []string) {
|
||||
for _, r := range m.Resources {
|
||||
if r["type"] == "package" {
|
||||
if r["absent"] == true {
|
||||
@@ -75,9 +138,9 @@ func packages(m catalogue.Manifest) (present, absent []string) {
|
||||
// fromTemplate checks that a file resource takes its text from its template file in the module's
|
||||
// repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the
|
||||
// template is there. The readable file in the repository is the one place the machine's text is written.
|
||||
func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) {
|
||||
func (m manifest) fromTemplate(t *testing.T, id, template string) {
|
||||
t.Helper()
|
||||
r := resource(t, m, id)
|
||||
r := m.resource(t, id)
|
||||
if r["type"] != "file" {
|
||||
t.Fatalf("%s is a %v, not a file", id, r["type"])
|
||||
}
|
||||
@@ -97,7 +160,7 @@ func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) {
|
||||
|
||||
// everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs
|
||||
// files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2).
|
||||
func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) {
|
||||
func (m manifest) everyFileIsATemplate(t *testing.T) {
|
||||
t.Helper()
|
||||
for _, r := range m.Resources {
|
||||
if _, inline := r["content"]; inline {
|
||||
@@ -106,10 +169,10 @@ func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) {
|
||||
}
|
||||
packs := 0
|
||||
for _, a := range m.Build.Artifacts {
|
||||
if a.Kind == "archive" && a.From == "files" {
|
||||
if a["kind"] == "archive" && a["from"] == "files" {
|
||||
packs++
|
||||
if a.Name != "files" {
|
||||
t.Errorf("the templates archive is named %v, not files", a.Name)
|
||||
if a["name"] != "files" {
|
||||
t.Errorf("the templates archive is named %v, not files", a["name"])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,9 +182,9 @@ func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) {
|
||||
}
|
||||
|
||||
// text is the text a file resource places on the machine: its template file's.
|
||||
func templateText(t *testing.T, m catalogue.Manifest, id string) string {
|
||||
func (m manifest) text(t *testing.T, id string) string {
|
||||
t.Helper()
|
||||
name, _ := resource(t, m, id)["template"].(string)
|
||||
name, _ := m.resource(t, id)["template"].(string)
|
||||
if name == "" {
|
||||
t.Fatalf("resource %s names no template", id)
|
||||
}
|
||||
@@ -134,7 +197,7 @@ func templateText(t *testing.T, m catalogue.Manifest, id string) string {
|
||||
|
||||
// checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle
|
||||
// serves each seat verb the claims promise as <seat>.<verb>, and that the Go bundle is declared.
|
||||
func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) {
|
||||
func checkTheToolsAgree(t *testing.T, m manifest) {
|
||||
t.Helper()
|
||||
own, seat := map[string]bool{}, map[string]bool{}
|
||||
for _, tool := range tools() {
|
||||
@@ -176,14 +239,14 @@ func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) {
|
||||
t.Errorf("the bundle serves %s, which no claim promises", name)
|
||||
}
|
||||
}
|
||||
var bundle *catalogue.Artifact
|
||||
var bundle map[string]any
|
||||
for _, a := range m.Build.Artifacts {
|
||||
if a.Kind == "bundle" {
|
||||
bundle = &a
|
||||
if a["kind"] == "bundle" {
|
||||
bundle = a
|
||||
}
|
||||
}
|
||||
if bundle == nil || bundle.Language != "go" || bundle.System != "arch" ||
|
||||
bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" {
|
||||
if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" ||
|
||||
bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" {
|
||||
t.Errorf("the Go tools bundle: %v", bundle)
|
||||
}
|
||||
}
|
||||
@@ -208,7 +271,7 @@ const displaySession = "contribution:node-display-session:config"
|
||||
|
||||
// i3Lines is what the module contributes to the window manager: the text of its item of the collection
|
||||
// contribution:node-display-session:config (design 57 L3, issue 848).
|
||||
func i3Lines(m catalogue.Manifest) string {
|
||||
func (m manifest) i3Lines() string {
|
||||
var out string
|
||||
for _, it := range m.Items {
|
||||
if it.Collection == displaySession {
|
||||
@@ -224,7 +287,7 @@ func i3Lines(m catalogue.Manifest) string {
|
||||
|
||||
// i3LinesAreSource checks the window-manager lines are one item whose template is the source file, and that no
|
||||
// old-form contribution to node-display-session is left.
|
||||
func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) {
|
||||
func (m manifest) i3LinesAreSource(t *testing.T, source string) {
|
||||
t.Helper()
|
||||
if len(m.Contributions) != 0 {
|
||||
t.Fatalf("the module still holds contributions: the lines are an item of %s", displaySession)
|
||||
@@ -238,7 +301,7 @@ func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) {
|
||||
if len(templates) != 1 || templates[0] != source {
|
||||
t.Fatalf("the items of %s are %v, want the one template %s", displaySession, templates, source)
|
||||
}
|
||||
if i3Lines(m) == "" {
|
||||
if m.i3Lines() == "" {
|
||||
t.Fatalf("the template %s holds no line", source)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,15 +6,12 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// clipmenu's shape (novox/hq ADR 0208, research 026/04): it claims node-clipboard serving history
|
||||
@@ -22,44 +19,28 @@ import (
|
||||
// from the session's start, and binds its menu as an i3 drop-in through the launcher's dmenu command.
|
||||
|
||||
func TestItClaimsTheClipboardSeatServingHistoryAndCopy(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
m := readManifest(t)
|
||||
if m.Module != "clipmenu" || m.Seats != nil {
|
||||
t.Fatalf("module %q declares seats %v", m.Module, m.Seats)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
if m.Module != "clipmenu" || m.DefinesSeats != nil {
|
||||
t.Fatalf("module %q declares seats %v", m.Module, m.DefinesSeats)
|
||||
}
|
||||
if !reflect.DeepEqual(m.Claims, []catalogue.Claim{{Name: "node-clipboard", Scope: "node", Serves: []string{"history", "copy"}}}) {
|
||||
if !reflect.DeepEqual(m.Claims, []claim{{Name: "node-clipboard", Scope: "node", Serves: []string{"history", "copy"}}}) {
|
||||
t.Fatalf("claims: %+v", m.Claims)
|
||||
}
|
||||
if !reflect.DeepEqual(m.Requires, []string{"x11-display"}) {
|
||||
t.Fatalf("requires: %v", m.Requires)
|
||||
}
|
||||
present, absent := packages(m)
|
||||
present, absent := m.packages()
|
||||
if !reflect.DeepEqual(present, []string{"clipmenu"}) || !reflect.DeepEqual(absent, []string{"rofi-greenclip"}) {
|
||||
t.Fatalf("packages: %v, absent %v", present, absent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheDaemonIsTheOperatorsUserUnitEnabledAndRunning(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
m := readManifest(t)
|
||||
if len(m.shell(t)) != 0 {
|
||||
t.Fatalf("a second start from the session: %+v", m.shell(t))
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
if len(shell(t, m)) != 0 {
|
||||
t.Fatalf("a second start from the session: %+v", shell(t, m))
|
||||
}
|
||||
svc := resource(t, m, "daemon")
|
||||
svc := m.resource(t, "daemon")
|
||||
want := map[string]any{"id": "daemon", "type": "service", "unit": "clipmenud.service", "scope": "user",
|
||||
"user": "${machine:account}", "state": "running", "boot": "enabled", "reload-on": nil}
|
||||
for k, v := range want {
|
||||
@@ -72,15 +53,15 @@ func TestTheDaemonIsTheOperatorsUserUnitEnabledAndRunning(t *testing.T) {
|
||||
if h, _ := svc["health"].(map[string]any); h["kind"] != "unit" {
|
||||
t.Fatalf("daemon health = %v, want kind unit", svc["health"])
|
||||
}
|
||||
dropin := resource(t, m, "daemon-dropin")
|
||||
content := templateText(t, m, "daemon-dropin")
|
||||
dropin := m.resource(t, "daemon-dropin")
|
||||
content := m.text(t, "daemon-dropin")
|
||||
if dropin["path"] != "${machine:account-home}/.config/systemd/user/clipmenud.service.d/mesh.conf" ||
|
||||
dropin["owner"] != "${machine:account}" ||
|
||||
!strings.Contains(content, "ExecStart=/usr/local/lib/mesh-clipmenu/clipmenud-session") ||
|
||||
strings.Contains(content, "greenclip") {
|
||||
t.Fatalf("drop-in: %v", dropin)
|
||||
}
|
||||
launcher := templateText(t, m, "launcher")
|
||||
launcher := m.text(t, "launcher")
|
||||
if !strings.Contains(launcher, "DISPLAY") || !strings.Contains(launcher, "/tmp/.X11-unix") ||
|
||||
!strings.Contains(launcher, `PATH="/usr/local/lib/mesh-clipmenu:$PATH"`) || !strings.Contains(launcher, "exec clipmenud") {
|
||||
t.Fatalf("launcher: %q", launcher)
|
||||
@@ -93,7 +74,7 @@ func TestTheDaemonIsTheOperatorsUserUnitEnabledAndRunning(t *testing.T) {
|
||||
if !strings.Contains(content, "StartLimitIntervalSec=0") || !strings.Contains(content, "RestartSec=2s") {
|
||||
t.Fatalf("drop-in lacks the safety net: %q", content)
|
||||
}
|
||||
for k, v := range environmentOf(t, m).Variables {
|
||||
for k, v := range m.environmentOf(t).Variables {
|
||||
if !strings.Contains(content, "Environment="+k+"="+v) {
|
||||
t.Fatalf("the unit lacks %s=%s", k, v)
|
||||
}
|
||||
@@ -101,67 +82,34 @@ func TestTheDaemonIsTheOperatorsUserUnitEnabledAndRunning(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestItsSettingsAreEnvironmentAndItsMenuIsTheLaunchersDmenu(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
m := readManifest(t)
|
||||
if !reflect.DeepEqual(m.Environment.Variables, map[string]string{"CM_SELECTIONS": "clipboard", "CM_MAX_CLIPS": "500", "CM_HISTLENGTH": "15"}) {
|
||||
t.Fatalf("%v", m.Environment.Variables)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(environmentOf(t, m).Variables, map[string]string{"CM_SELECTIONS": "clipboard", "CM_MAX_CLIPS": "500", "CM_HISTLENGTH": "15"}) {
|
||||
t.Fatalf("%v", environmentOf(t, m).Variables)
|
||||
}
|
||||
if _, set := environmentOf(t, m).Variables["CM_LAUNCHER"]; set {
|
||||
if _, set := m.Environment.Variables["CM_LAUNCHER"]; set {
|
||||
t.Fatal("the launcher is clipmenu's default, dmenu: the seat's command")
|
||||
}
|
||||
i3LinesAreSource(t, m, "files/i3/50-clipmenu.conf")
|
||||
if c := i3Lines(m); !strings.Contains(c, "bindsym $mod+period exec --no-startup-id clipmenu") {
|
||||
m.i3LinesAreSource(t, "files/i3/50-clipmenu.conf")
|
||||
if c := m.i3Lines(); !strings.Contains(c, "bindsym $mod+period exec --no-startup-id clipmenu") {
|
||||
t.Fatalf("%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheToolsAgreeWithTheManifest(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
m := readManifest(t)
|
||||
checkTheToolsAgree(t, m)
|
||||
checkNoSecretsOrInstallationNames(t)
|
||||
}
|
||||
|
||||
// No file resource carries its text inline (novox/hq ADR 0349 §2).
|
||||
func TestEveryFileTakesItsTextFromATemplate(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
everyFileIsATemplate(t, m)
|
||||
readManifest(t).everyFileIsATemplate(t)
|
||||
}
|
||||
|
||||
// Run for real: DISPLAY from the manager is used at once; several sockets and no DISPLAY wait.
|
||||
func TestTheLauncherUsesTheManagersDisplayAndWaitsOtherwise(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
script := templateText(t, m, "launcher")
|
||||
m := readManifest(t)
|
||||
script := m.text(t, "launcher")
|
||||
dir := t.TempDir()
|
||||
file := dir + "/launcher"
|
||||
// clipmenud and the socket directory are faked: the script's own paths are rewritten.
|
||||
@@ -214,16 +162,8 @@ func TestTheLauncherWaitsQuietlyWhileAnotherClipmenudHoldsTheLock(t *testing.T)
|
||||
if _, err := exec.LookPath("flock"); err != nil {
|
||||
t.Skip("flock is missing here")
|
||||
}
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
script := templateText(t, m, "launcher")
|
||||
m := readManifest(t)
|
||||
script := m.text(t, "launcher")
|
||||
dir := t.TempDir()
|
||||
body := strings.Replace(script, "exec clipmenud", `echo "started"`, 1)
|
||||
file := dir + "/launcher"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "clipmenu",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"moved": true,
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "confluence",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"slug": "confl",
|
||||
"own-secrets": {
|
||||
"token": "${dir:state}/token"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "cups",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "dbus",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "desk-channel",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"slug": "desk",
|
||||
"runs-as": "desk-channel",
|
||||
"claims": [
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "dev-tools",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "disk-load",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"tools": [
|
||||
"disk_load",
|
||||
"disk_top",
|
||||
@@ -25,15 +24,6 @@
|
||||
"loads": [
|
||||
"disk-load"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "diskreadings",
|
||||
"kind": "package",
|
||||
"language": "go",
|
||||
"paths": [
|
||||
"diskreadings"
|
||||
],
|
||||
"series": "0.1"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -46,13 +46,8 @@ func stepErr(bound time.Duration, what string, f func() error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// boundedObjects is the incoming object store with each operation bounded and logged. Purge is the store's purge of a
|
||||
// deleted object's chunks (busstate.Objects.Purge): every delete of a chunk is followed by it, so what a delete left
|
||||
// behind is dropped and the store's kept descriptions equal its live objects (design 47 H2 §813 (8); H-A-227, H-A-366).
|
||||
type boundedObjects struct {
|
||||
parts.Objects
|
||||
Purge func(name string) error
|
||||
}
|
||||
// boundedObjects is the incoming object store with each operation bounded and logged.
|
||||
type boundedObjects struct{ parts.Objects }
|
||||
|
||||
func (b boundedObjects) Used() (uint64, error) {
|
||||
return step(stepBound, "the incoming object store's fill (objects)", b.Objects.Used)
|
||||
@@ -64,15 +59,7 @@ func (b boundedObjects) Get(name string) ([]byte, error) {
|
||||
return step(stepBound, "the get of a chunk from the incoming object store (objects)", func() ([]byte, error) { return b.Objects.Get(name) })
|
||||
}
|
||||
func (b boundedObjects) Delete(name string) error {
|
||||
err := stepErr(stepBound, "the delete of a chunk in the incoming object store (objects)", func() error { return b.Objects.Delete(name) })
|
||||
if b.Purge == nil {
|
||||
return err
|
||||
}
|
||||
perr := stepErr(stepBound, "the purge of a deleted chunk's leftovers in the incoming object store (objects)", func() error { return b.Purge(name) })
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return perr
|
||||
return stepErr(stepBound, "the delete of a chunk in the incoming object store (objects)", func() error { return b.Objects.Delete(name) })
|
||||
}
|
||||
|
||||
// boundedTransfers is the transfers state with its reads and writes bounded and logged.
|
||||
|
||||
@@ -8,16 +8,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// The incoming store's steps (design 47 H2 §819: one test function for the bounded steps).
|
||||
func TestTheBoundedSteps(t *testing.T) {
|
||||
t.Run("a hung step answers with its name", hungStepAnswersWithItsName)
|
||||
t.Run("a given-up step says when its call ended", givenUpStepSaysWhenItsCallEnded)
|
||||
t.Run("a deleted chunk is purged", deletedChunkIsPurged)
|
||||
t.Run("a finding is said off the path", findingIsSaidOffThePath)
|
||||
}
|
||||
|
||||
// A bus operation that never answers becomes an error naming its step, within the bound.
|
||||
func hungStepAnswersWithItsName(t *testing.T) {
|
||||
func TestAHungStepAnswersWithItsName(t *testing.T) {
|
||||
block := make(chan struct{})
|
||||
defer close(block)
|
||||
_, err := step(10*time.Millisecond, "the write of the idle key", func() (int, error) { <-block; return 0, nil })
|
||||
@@ -30,7 +22,7 @@ func hungStepAnswersWithItsName(t *testing.T) {
|
||||
}
|
||||
|
||||
// A step given up on is said given up by name, and the log says later when the call behind it ended and how (A-443a).
|
||||
func givenUpStepSaysWhenItsCallEnded(t *testing.T) {
|
||||
func TestAGivenUpStepSaysWhenItsCallEnded(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var lines []string
|
||||
ended := make(chan struct{})
|
||||
@@ -60,7 +52,7 @@ func givenUpStepSaysWhenItsCallEnded(t *testing.T) {
|
||||
}
|
||||
|
||||
// The finding is said off the caller's path: a writer that never answers does not hold the call that says it.
|
||||
func findingIsSaidOffThePath(t *testing.T) {
|
||||
func TestAFindingIsSaidOffThePath(t *testing.T) {
|
||||
block := make(chan struct{})
|
||||
defer close(block)
|
||||
say := boundedFinding(func(string, string, string, bool) { <-block })
|
||||
@@ -76,31 +68,3 @@ func findingIsSaidOffThePath(t *testing.T) {
|
||||
t.Fatal("the call was held by the finding")
|
||||
}
|
||||
}
|
||||
|
||||
// fakeObjects is an object store that counts deletes.
|
||||
type fakeObjects struct{ deleted []string }
|
||||
|
||||
func (f *fakeObjects) Put(string, []byte) error { return nil }
|
||||
func (f *fakeObjects) Get(string) ([]byte, error) { return nil, nil }
|
||||
func (f *fakeObjects) Delete(name string) error { f.deleted = append(f.deleted, name); return nil }
|
||||
func (f *fakeObjects) Used() (uint64, error) { return 0, nil }
|
||||
|
||||
// Every delete of a chunk purges what it left behind, so the store's kept descriptions equal its live objects (design 47
|
||||
// H2 §813 (8)); a purge's failure is said, never swallowed.
|
||||
func deletedChunkIsPurged(t *testing.T) {
|
||||
f := &fakeObjects{}
|
||||
var purged []string
|
||||
b := boundedObjects{f, func(name string) error { purged = append(purged, name); return nil }}
|
||||
for i := range 1000 {
|
||||
if err := b.Delete(fmt.Sprintf("chunk.%d", i)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if len(f.deleted) != 1000 || len(purged) != 1000 || purged[999] != "chunk.999" {
|
||||
t.Fatalf("deleted %d, purged %d", len(f.deleted), len(purged))
|
||||
}
|
||||
b.Purge = func(string) error { return fmt.Errorf("no responders") }
|
||||
if err := b.Delete("chunk.x"); err == nil || !strings.Contains(err.Error(), "no responders") {
|
||||
t.Fatalf("a failed purge answered %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,15 +40,11 @@ func partAnswer(in reg.PublishPartInput, a holder.PublishAnswer) reg.PublishPart
|
||||
return reg.PublishPartAnswer{Transfer: in.Transfer, Part: in.Part, Received: in.Sequence + 1, Complete: a.Ended}
|
||||
}
|
||||
|
||||
// versionsAnswer is `versions`: each version with the digest its reference names (holder.Verbs.VersionDigest).
|
||||
// versionsAnswer is `versions`: each version with the digest the store holds under it.
|
||||
func versionsAnswer(v *holder.Verbs, kind string, a holder.VersionsAnswer) reg.VersionsAnswer {
|
||||
out := reg.VersionsAnswer{Name: a.Name, Versions: []reg.Artifact{}}
|
||||
for _, ver := range a.Versions {
|
||||
d, err := v.VersionDigest(a.Seat, a.Name, ver)
|
||||
if err != nil {
|
||||
// The contract's answer has no field for why; the version is answered with no digest and the reason said here.
|
||||
logf("versions: %s %s on %s answers no digest: %v", a.Name, ver, a.Seat, err)
|
||||
}
|
||||
d, _ := v.Catalog.Digest(a.Name, ver)
|
||||
out.Versions = append(out.Versions, reg.Artifact{Name: a.Name, Version: ver, Digest: d, Kind: kind})
|
||||
}
|
||||
return out
|
||||
|
||||
@@ -12,12 +12,6 @@ import (
|
||||
"distribution/internal/holder"
|
||||
)
|
||||
|
||||
// twoDigests is a store whose every version's manifest digest and blob differ.
|
||||
type twoDigests struct{ holder.Catalog }
|
||||
|
||||
func (twoDigests) Digest(string, string) (string, error) { return "sha256:manifest", nil }
|
||||
func (twoDigests) Blob(string, string) (string, error) { return "sha256:blob", nil }
|
||||
|
||||
func jsonFields(v any) map[string]bool {
|
||||
out := map[string]bool{}
|
||||
t := reflect.TypeOf(v)
|
||||
@@ -36,7 +30,7 @@ func TestTheVerbsSpeakTheSeatsContract(t *testing.T) {
|
||||
reg.VerbVersions: reg.VersionsInput{}, reg.VerbRemove: reg.RemoveInput{}, reg.VerbHold: reg.HoldInput{},
|
||||
reg.VerbRelease: reg.ReleaseInput{}, reg.VerbMirror: reg.MirrorInput{}, reg.VerbTags: reg.TagsInput{}}
|
||||
seen := 0
|
||||
for _, tool := range tools(&holder.Verbs{}, kept[holder.Window]{}, nil) {
|
||||
for _, tool := range tools(&holder.Verbs{}, kept[holder.Window]{}) {
|
||||
seat, verb, ok := strings.Cut(tool.Name, ".")
|
||||
in, isVerb := inputs[verb]
|
||||
if !ok || !isVerb || (seat != holder.ImageSeat && seat != holder.ArtifactSeat) {
|
||||
@@ -65,14 +59,6 @@ func TestTheVerbsSpeakTheSeatsContract(t *testing.T) {
|
||||
if a := publishAnswer(holder.PublishAnswer{Transfer: "t", Missing: []string{"sha256:bb"}}); !reflect.DeepEqual(a.Lacks, []string{"sha256:bb"}) || a.Held {
|
||||
t.Errorf("publish answers %+v", a)
|
||||
}
|
||||
// versions answers the digest a reference names (design 47 G A-430a): the blob on the artifact seat, the manifest on the image seat.
|
||||
cat := twoDigests{}
|
||||
for _, c := range []struct{ seat, want string }{{holder.ArtifactSeat, "sha256:blob"}, {holder.ImageSeat, "sha256:manifest"}} {
|
||||
got := versionsAnswer(&holder.Verbs{Catalog: cat}, "", holder.VersionsAnswer{Seat: c.seat, Name: "m/files", Versions: []string{"0.1.7"}})
|
||||
if len(got.Versions) != 1 || got.Versions[0].Digest != c.want || got.Versions[0].Version != "0.1.7" {
|
||||
t.Errorf("versions on %s answers %+v, want digest %s", c.seat, got.Versions, c.want)
|
||||
}
|
||||
}
|
||||
if r, err := partOf(reg.PublishPartInput{Transfer: "t", Part: "sha256:bb", Data: "aGk=", Last: true}); err != nil || string(r.Data) != "hi" || r.Digest != "sha256:bb" {
|
||||
t.Errorf("publish-part read %+v, %v", r, err)
|
||||
}
|
||||
|
||||
@@ -22,10 +22,9 @@ import (
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
||||
reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
|
||||
oci "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/parts"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/busstate"
|
||||
"git.novox.be/novox/mesh-sdk/go/seatlist"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
|
||||
"distribution/internal/holder"
|
||||
@@ -244,7 +243,7 @@ func arg[T any](args map[string]any) (T, error) {
|
||||
func str(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
|
||||
|
||||
// tools are distribution's tools over v.
|
||||
func tools(v *holder.Verbs, windows kept[holder.Window], taken *asks) []stdio.Tool {
|
||||
func tools(v *holder.Verbs, windows kept[holder.Window]) []stdio.Tool {
|
||||
obj := func(props map[string]any, req ...string) map[string]any {
|
||||
return map[string]any{"type": "object", "required": req, "properties": props}
|
||||
}
|
||||
@@ -262,9 +261,7 @@ func tools(v *holder.Verbs, windows kept[holder.Window], taken *asks) []stdio.To
|
||||
caller := stdio.CallerOf(args)
|
||||
start := time.Now()
|
||||
logf("%s.publish received from %s: %s %s (%s, %d parts)", seat, caller, in.Name, in.Version, in.Kind, len(in.Parts))
|
||||
r := publishOf(in)
|
||||
a, err := taken.once(seat, stdio.AskIDOf(args), func() (holder.PublishAnswer, error) { return v.Publish(seat, caller, r) },
|
||||
func() (holder.PublishAnswer, error) { return v.Repeat(seat, caller, r) })
|
||||
a, err := v.Publish(seat, caller, publishOf(in))
|
||||
if err == nil {
|
||||
// The window's learning asks the delivery view (up to 60 s, past the runner's 30 s call bound),
|
||||
// so it never holds the answer.
|
||||
@@ -314,7 +311,15 @@ func tools(v *holder.Verbs, windows kept[holder.Window], taken *asks) []stdio.To
|
||||
}
|
||||
return reg.RemoveAnswer{Removed: got.Removed, Kept: got.Kept, Reason: got.Reason, Tags: got.Tags, Artifact: ®.Artifact{Name: in.Name, Version: in.Version, Tag: in.Tag, Digest: in.Digest}}, nil
|
||||
}},
|
||||
listTool(v, seat),
|
||||
stdio.Tool{Name: seat + "." + holder.VerbList, Description: "What the store holds of one repository, or of every repository the seat holds: each manifest's digest, whether it is an index and the manifests it names, the tags naming it, its size and whether it is held (ADR 0632 §3). Read from the store's own files; changes nothing.",
|
||||
Input: obj(map[string]any{"name": s("<module>/<artifact>; every repository when not given")}),
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
repos, err := v.List(context.Background(), seat, stdio.CallerOf(args), str(args, "name"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return reg.ListAnswer{Repositories: repos}, nil
|
||||
}},
|
||||
stdio.Tool{Name: seat + "." + holder.VerbHold, Description: "Keep one untagged bundle, archive or copied base from retention (the artifact sweep alone); says held.",
|
||||
Input: obj(map[string]any{"name": s("name"), "digest": s("sha256:…"), "reason": s("which of the sweep's rules keeps it")}, "name", "digest", "reason"),
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
@@ -483,11 +488,11 @@ func main() {
|
||||
}()
|
||||
base, _ := url.Parse("http://127.0.0.1:5001")
|
||||
store := &holder.OCI{Base: base, Upstream: &holder.Upstream{}}
|
||||
h := &holder.Holder{Logf: logf, Objects: boundedObjects{parts.BusObjects(holder.IncomingObjects), busstate.ObjectsOf(holder.IncomingObjects).Purge}, Builder: holder.Reserved{Ask: stdio.Ask}, Store: store, Say: say, Finding: boundedFinding(findings.Say),
|
||||
h := &holder.Holder{Logf: logf, Objects: boundedObjects{parts.BusObjects(holder.IncomingObjects)}, Builder: holder.Reserved{Ask: stdio.Ask}, Store: store, Say: say, Finding: boundedFinding(findings.Say),
|
||||
Transfers: boundedTransfers{parts.BusTransfers[holder.Opening](holder.TransfersState)}, Cap: boundedCap(parts.BusCap(holder.IncomingObjects)), Record: recordAct, Idle: boundedArm(parts.BusArm(holder.IdleState))}
|
||||
files := &holder.Files{Container: or(os.Getenv("MESH_REGISTRY_CONTAINER"), "mesh-registry"), Run: holder.ExecRunner, UID: os.Getuid()}
|
||||
v := &holder.Verbs{Holder: h, Catalog: store, Holds: kept[holder.Hold]{state: "holds", key: holder.Hold.Key}, Mirrors: mirrors{},
|
||||
Files: files}
|
||||
Files: files, SweepHolder: func() (string, error) { return seatlist.HolderOf(stdio.Ask, holder.SweepSeat) }}
|
||||
go followMirrors(v)
|
||||
go parts.FollowIdle(holder.IdleState, parts.IDs(h.Transfers), h.Expired, func(id string, dropped bool, err error) {
|
||||
if err != nil {
|
||||
@@ -497,13 +502,7 @@ func main() {
|
||||
logf("transfer %s had no part for %s: dropped with its parts", id, holder.TransferIdle)
|
||||
})
|
||||
windows := kept[holder.Window]{state: "windows", key: func(w holder.Window) string { return w.Name }}
|
||||
bound, err := stdio.SettingDuration(askTakenBound, askTakenDefault)
|
||||
if err != nil {
|
||||
logf("the setting %s: %v", askTakenBound, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
taken := &asks{take: stdio.State(stdio.TakenState).TakeOnce, bound: bound}
|
||||
if err := stdio.Serve("", tools(v, windows, taken)); err != nil {
|
||||
if err := stdio.Serve("", tools(v, windows)); err != nil {
|
||||
logf("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -515,41 +514,3 @@ func or(v, d string) string {
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// listTool is the seat's verb list. On oci-artifact-registry it filters by name or module while it reads and pages at 768 KB
|
||||
// by the cursor next (design 47 H2 §779, §811, §813 (7)); on container-image-registry, whose form takes no cursor, it answers
|
||||
// whole.
|
||||
func listTool(v *holder.Verbs, seat string) stdio.Tool {
|
||||
s := func(d string) map[string]any { return map[string]any{"type": "string", "description": d} }
|
||||
description := "What the store holds of one repository, or of every repository the seat holds: each manifest's digest, whether it is an index and the manifests it names, the tags naming it, its size and whether it is held (ADR 0632 §3). Read from the store's own files; changes nothing."
|
||||
props := map[string]any{"name": s("<module>/<artifact>; every repository when not given")}
|
||||
if seat == holder.ArtifactSeat {
|
||||
description += " Filtered by name or module while it reads and paged by the cursor next, at most 768 KB a page, capped saying the rest is read from next."
|
||||
props["module"] = s("only the repositories of this module, <module>/*")
|
||||
props["next"] = s("the cursor the previous page answered; absent for the first page")
|
||||
}
|
||||
return stdio.Tool{Name: seat + "." + holder.VerbList, Description: description, Input: map[string]any{"type": "object", "properties": props},
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
if seat != holder.ArtifactSeat {
|
||||
page, err := v.List(context.Background(), seat, stdio.CallerOf(args), holder.ListQuery{Name: str(args, "name")})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return reg.ListAnswer{Repositories: page.Repositories}, nil
|
||||
}
|
||||
in, err := arg[oci.ListInput](args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
page, err := v.List(context.Background(), seat, stdio.CallerOf(args), holder.ListQuery{Name: in.Name, Module: in.Module, Next: in.Next, PageBytes: holder.ListPageBytes})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := oci.ListAnswer{Form: oci.Form, Repositories: []oci.Repository{}, Capped: page.Capped, Next: page.Next, Total: int64(page.Total)}
|
||||
raw, _ := json.Marshal(page.Repositories)
|
||||
if err := json.Unmarshal(raw, &out.Repositories); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}}
|
||||
}
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"distribution/internal/holder"
|
||||
)
|
||||
|
||||
// askTakenBound is the setting the taken key of a publish ask lives (design 47 H2 §775, H-A-381 (2)): the holder never
|
||||
// receives the caller's bound, so it keeps an ask's id this long, the largest caller's bound by default.
|
||||
const askTakenBound = "ask-taken-bound"
|
||||
|
||||
// askTakenDefault is ask-taken-bound's default, the runner's ask-unanswered-bound (H-A-381 (2)).
|
||||
const askTakenDefault = 10 * time.Minute
|
||||
|
||||
// asks takes each publish ask once by its id (design 47 H2 §775; ADR 0668 §2; H-A-381): the first call with an id is
|
||||
// kept in the state taken and acts; a repeat while that act runs joins it and gets its answer; a repeat after it ended
|
||||
// answers the act's state read afresh (holder.Repeat), never acting twice. No answer is kept in taken. A call that
|
||||
// carries no ask id is no kept call, and acts as it did before.
|
||||
type asks struct {
|
||||
take func(seat, ask string, bound time.Duration) (bool, error)
|
||||
bound time.Duration
|
||||
mu sync.Mutex
|
||||
running map[string]*flight
|
||||
// joined, when set, is told each repeat that joins a running act (a test's hook).
|
||||
joined func()
|
||||
}
|
||||
|
||||
// flight is one act of a taken ask while it runs, which a repeat joins.
|
||||
type flight struct {
|
||||
done chan struct{}
|
||||
a holder.PublishAnswer
|
||||
err error
|
||||
}
|
||||
|
||||
func (k *asks) once(seat, ask string, act, repeat func() (holder.PublishAnswer, error)) (holder.PublishAnswer, error) {
|
||||
if ask == "" || k == nil || k.take == nil {
|
||||
return act()
|
||||
}
|
||||
key := seat + "." + ask
|
||||
k.mu.Lock()
|
||||
if f, ok := k.running[key]; ok {
|
||||
k.mu.Unlock()
|
||||
if k.joined != nil {
|
||||
k.joined()
|
||||
}
|
||||
<-f.done
|
||||
return f.a, f.err
|
||||
}
|
||||
if k.running == nil {
|
||||
k.running = map[string]*flight{}
|
||||
}
|
||||
f := &flight{done: make(chan struct{})}
|
||||
k.running[key] = f
|
||||
k.mu.Unlock()
|
||||
defer func() {
|
||||
k.mu.Lock()
|
||||
delete(k.running, key)
|
||||
k.mu.Unlock()
|
||||
close(f.done)
|
||||
}()
|
||||
took, err := k.take(seat, ask, k.bound)
|
||||
switch {
|
||||
case err != nil:
|
||||
f.err = fmt.Errorf("publish: the ask %s of %s cannot be taken: %w", ask, seat, err)
|
||||
case took:
|
||||
f.a, f.err = act()
|
||||
default:
|
||||
logf("%s.publish: the ask %s was taken before; it answers the act's state, acting not again", seat, ask)
|
||||
f.a, f.err = repeat()
|
||||
}
|
||||
return f.a, f.err
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"distribution/internal/holder"
|
||||
)
|
||||
|
||||
// A publish ask given twice acts once (design 47 H2 §775; H-A-381): a repeat while the act runs joins it and gets its
|
||||
// answer, a repeat after it ended answers the act's state, a call with no ask id acts as before, a take that fails is said.
|
||||
func TestAPublishAskIsTakenOnce(t *testing.T) {
|
||||
taken := map[string]bool{}
|
||||
var mu sync.Mutex
|
||||
k := &asks{bound: time.Minute, take: func(seat, ask string, bound time.Duration) (bool, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if ask == "broken" {
|
||||
return false, errors.New("no responders")
|
||||
}
|
||||
if bound != time.Minute || taken[seat+"."+ask] {
|
||||
return false, nil
|
||||
}
|
||||
taken[seat+"."+ask] = true
|
||||
return true, nil
|
||||
}}
|
||||
var acts, repeats atomic.Int32
|
||||
release, started, joined := make(chan struct{}), make(chan struct{}, 1), make(chan struct{}, 1)
|
||||
k.joined = func() { joined <- struct{}{} }
|
||||
act := func() (holder.PublishAnswer, error) {
|
||||
acts.Add(1)
|
||||
started <- struct{}{}
|
||||
<-release
|
||||
return holder.PublishAnswer{Transfer: "t", Missing: []string{"d"}}, nil
|
||||
}
|
||||
repeat := func() (holder.PublishAnswer, error) { repeats.Add(1); return holder.PublishAnswer{Transfer: "t"}, nil }
|
||||
answers := make(chan holder.PublishAnswer, 2)
|
||||
go func() { a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); answers <- a }()
|
||||
<-started
|
||||
go func() { a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); answers <- a }()
|
||||
<-joined
|
||||
close(release)
|
||||
for range 2 {
|
||||
if a := <-answers; len(a.Missing) != 1 {
|
||||
t.Errorf("an answer of the running act = %+v", a)
|
||||
}
|
||||
}
|
||||
if a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); acts.Load() != 1 || repeats.Load() != 1 || a.Transfer != "t" {
|
||||
t.Errorf("acts %d, repeats %d after a repeat of an ended act", acts.Load(), repeats.Load())
|
||||
}
|
||||
release = make(chan struct{})
|
||||
close(release)
|
||||
act2 := func() (holder.PublishAnswer, error) { acts.Add(1); return holder.PublishAnswer{}, nil }
|
||||
if _, err := k.once(holder.ArtifactSeat, "", act2, repeat); err != nil || acts.Load() != 2 {
|
||||
t.Errorf("a call with no ask id: acts %d, %v", acts.Load(), err)
|
||||
}
|
||||
if _, err := k.once(holder.ArtifactSeat, "broken", act2, repeat); err == nil || acts.Load() != 2 {
|
||||
t.Errorf("a failed take: acts %d, %v", acts.Load(), err)
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ require (
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000
|
||||
github.com/nats-io/nats.go v1.54.0
|
||||
@@ -32,6 +31,7 @@ require (
|
||||
|
||||
require (
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshhandacts v0.0.0-00010101000000-000000000000 // indirect
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry v0.0.0-00010101000000-000000000000 // indirect
|
||||
)
|
||||
|
||||
require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery v0.0.0-00010101000000-000000000000 // indirect
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
artifactref "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient/reference"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/publishconformance"
|
||||
)
|
||||
|
||||
type memObjects map[string][]byte
|
||||
@@ -62,35 +61,17 @@ func (b fakeBuilder) Recorded(string, string, string) (bool, string, error) {
|
||||
}
|
||||
|
||||
type fakeStore struct {
|
||||
has map[string]bool
|
||||
held map[string]heldAt // name@version to what the store holds there
|
||||
put []map[string][]byte
|
||||
has map[string]bool
|
||||
put []map[string][]byte
|
||||
}
|
||||
|
||||
// heldAt is what a version's tag names in the store: the manifest's digest and, for a bundle or an archive, the one layer
|
||||
// that manifest keeps, two digests as the store holds them (H-A-474).
|
||||
type heldAt struct{ manifest, layer string }
|
||||
|
||||
func (s *fakeStore) Has(_, d string) (bool, error) { return s.has[d], nil }
|
||||
func (s *fakeStore) Put(t PublishRequest, parts map[string][]byte) (string, error) {
|
||||
s.put = append(s.put, parts)
|
||||
return t.Name + "@" + t.Digest, nil
|
||||
}
|
||||
func (s *fakeStore) Held(r PublishRequest) (string, string, error) {
|
||||
at, ok := s.held[r.Name+"@"+r.Version]
|
||||
if !ok {
|
||||
return "", "", nil
|
||||
}
|
||||
d := at.layer
|
||||
if r.Kind == KindImage || r.Kind == KindUpstream {
|
||||
d = at.manifest
|
||||
}
|
||||
if d != r.Digest {
|
||||
return "", d, nil
|
||||
}
|
||||
return r.Name + "@" + d, "", nil
|
||||
}
|
||||
func (s *fakeStore) Holds(r PublishRequest) (bool, error) { return s.has[r.Digest], nil }
|
||||
func (s *fakeStore) Held(string, string, string) (string, bool, error) { return "", false, nil }
|
||||
func (s *fakeStore) Holds(r PublishRequest) (bool, error) { return s.has[r.Digest], nil }
|
||||
|
||||
type said struct {
|
||||
seat, event string
|
||||
@@ -127,9 +108,6 @@ func (c *fakeCatalog) Versions(name string) ([]string, error) {
|
||||
func (c *fakeCatalog) Digest(name, version string) (string, error) {
|
||||
return c.tags[name+":"+version], nil
|
||||
}
|
||||
func (c *fakeCatalog) Blob(name, version string) (string, error) {
|
||||
return c.tags[name+":"+version], nil
|
||||
}
|
||||
|
||||
// Remove deletes by the digest, as the store does: every tag naming the digest goes with it.
|
||||
func (c *fakeCatalog) Remove(name, version string) error {
|
||||
@@ -224,7 +202,8 @@ func newBed(builderOK bool) *bed {
|
||||
return nil
|
||||
},
|
||||
Finding: func(kind, subject, _ string, raised bool) { b.findings[kind+" "+subject] = raised }}
|
||||
b.v = &Verbs{Holder: b.h, Catalog: b.cat, Holds: memHolds{}, Mirrors: memMirrors{}}
|
||||
b.v = &Verbs{Holder: b.h, Catalog: b.cat, Holds: memHolds{}, Mirrors: memMirrors{},
|
||||
SweepHolder: func() (string, error) { return "artifact-store-tools", nil }}
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -268,25 +247,6 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
// A publish ask taken before answers the act's state read afresh and never publishes again (design 47 H2 §775;
|
||||
// H-A-381): an open transfer its lacking parts, a transfer gone with nothing held a refusal naming it.
|
||||
t.Run("a repeated ask answers the act's state", func(t *testing.T) {
|
||||
b := newBed(true)
|
||||
if _, err := b.h.Repeat(ArtifactSeat, builder, req("tr")); err == nil || !strings.Contains(err.Error(), "transfer tr is gone") {
|
||||
t.Errorf("a repeat of an act that never happened answered %v", err)
|
||||
}
|
||||
if _, err := b.h.Publish(ArtifactSeat, builder, req("tr")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
opened := len(b.h.Transfers.(memTransfers))
|
||||
if a, err := b.h.Repeat(ArtifactSeat, builder, req("tr")); err != nil || !reflect.DeepEqual(a.Missing, []string{part.Digest}) {
|
||||
t.Errorf("a repeat of an open transfer = %+v, %v", a, err)
|
||||
}
|
||||
if len(b.h.Transfers.(memTransfers)) != opened || len(b.said) != 0 {
|
||||
t.Errorf("a repeat acted: transfers %d, said %+v", len(b.h.Transfers.(memTransfers)), b.said)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("a transfer in parts, cut and resumed, hashed, numbered by the builder, published", func(t *testing.T) {
|
||||
b := newBed(true)
|
||||
a, err := b.h.Publish(ArtifactSeat, builder, req("t1"))
|
||||
@@ -353,16 +313,6 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
_, err := b.h.Publish(ArtifactSeat, builder, req("t"))
|
||||
return err
|
||||
}, "the builder did not reserve it"},
|
||||
{"a version the store holds at another digest", func(b *bed) error {
|
||||
b.store.held = map[string]heldAt{"m/program@0.1.4": {manifest: "sha256:cc", layer: "sha256:bb"}}
|
||||
r := req("t")
|
||||
r.Digest = "sha256:aa"
|
||||
_, err := b.h.Publish(ArtifactSeat, builder, r) // the reservation names no digest yet: the bed's builder says yes
|
||||
if !b.findings["publish-refused m/program@0.1.4"] || len(b.store.put) != 0 {
|
||||
return errors.New("not refused loudly, or stored")
|
||||
}
|
||||
return err
|
||||
}, "the store holds m/program@0.1.4 at digest sha256:bb, not sha256:aa"},
|
||||
{"an image on the artifact seat", func(b *bed) error {
|
||||
r := req("t")
|
||||
r.Kind = KindImage
|
||||
@@ -373,6 +323,10 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
_, err := b.v.Remove(ArtifactSeat, "novox.mesh-delivery-builder", RemoveRequest{Name: "m/program", Version: "0.1.4", Reason: "branch ended"})
|
||||
return err
|
||||
}, "has no tag"},
|
||||
{"a retention removal from another caller than the sweep", func(b *bed) error {
|
||||
_, err := b.v.Remove(ArtifactSeat, "novox.mesh-delivery-builder", RemoveRequest{Name: "m/program", Digest: part.Digest, Reason: ReasonRetention})
|
||||
return err
|
||||
}, "artifact sweep's alone"},
|
||||
{"a retention removal of a held archive", func(b *bed) error {
|
||||
_ = b.v.HoldOne(ArtifactSeat, "novox.artifact-store-tools", HoldRequest{Name: "m/a", Kind: KindArchive, Digest: part.Digest})
|
||||
_, err := b.v.Remove(ArtifactSeat, "novox.artifact-store-tools", RemoveRequest{Name: "m/a", Digest: part.Digest, Reason: ReasonRetention})
|
||||
@@ -381,6 +335,16 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
{"a hold of an image", func(b *bed) error {
|
||||
return b.v.HoldOne(ImageSeat, "novox.artifact-store-tools", HoldRequest{Name: "m/i", Kind: KindImage, Digest: part.Digest})
|
||||
}, "never held"},
|
||||
{"a retention removal by the module that held the sweep seat before", func(b *bed) error {
|
||||
b.v.SweepHolder = func() (string, error) { return "artifact-sweeper", nil } // the seat's holder as the seat says it now
|
||||
_, err := b.v.Remove(ArtifactSeat, "novox.artifact-store-tools", RemoveRequest{Name: "m/program", Digest: part.Digest, Reason: ReasonRetention})
|
||||
return err
|
||||
}, "artifact sweep's alone"},
|
||||
{"a retention removal while the sweep's holder cannot be read", func(b *bed) error {
|
||||
b.v.SweepHolder = func() (string, error) { return "", errors.New("the seat's holders are not answered") }
|
||||
_, err := b.v.Remove(ArtifactSeat, "novox.artifact-store-tools", RemoveRequest{Name: "m/program", Digest: part.Digest, Reason: ReasonRetention})
|
||||
return err
|
||||
}, "cannot be read"},
|
||||
{"a release in distribution's own name", func(b *bed) error {
|
||||
_, err := b.v.Release(ArtifactSeat, "novox.distribution", HoldRequest{Name: "m/a", Digest: part.Digest})
|
||||
return err
|
||||
@@ -396,47 +360,6 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
})
|
||||
// H-A-474: the store judges a held version like with like, through its own OCI code over a store in memory. A bundle
|
||||
// is stored as a manifest whose digest is not its layer's: the same bytes published again are already held (ended, no
|
||||
// error, nothing stored again, also on a repeated ask); other bytes are refused naming the layer the store holds. An
|
||||
// image is judged by its manifest's digest.
|
||||
t.Run("a held version is judged by the artifact's own digest", func(t *testing.T) {
|
||||
reg := newFakeRegistry()
|
||||
sts := httptest.NewServer(reg)
|
||||
defer sts.Close()
|
||||
u, _ := url.Parse(sts.URL)
|
||||
o := &OCI{Base: u}
|
||||
if _, err := o.Put(req("first"), map[string][]byte{part.Digest: blob}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m, _ := o.Digest("m/program", "0.1.4"); m == "" || m == part.Digest {
|
||||
t.Fatalf("the stored manifest's digest %q; want one, not the layer's %s", m, part.Digest)
|
||||
}
|
||||
b := newBed(true)
|
||||
b.h.Store = o
|
||||
uploads := len(reg.blobs)
|
||||
a, err := b.h.Publish(ArtifactSeat, builder, req("again"))
|
||||
if err != nil || !a.Ended || len(a.Missing) != 0 || len(b.said) != 1 || b.said[0].event != EventPublished || b.findings["publish-refused m/program@0.1.4"] {
|
||||
t.Errorf("the same bundle again = %+v, %v; said %+v; want already held, published said, nothing refused", a, err, b.said)
|
||||
}
|
||||
if a, err := b.h.Repeat(ArtifactSeat, builder, req("again")); err != nil || !a.Ended {
|
||||
t.Errorf("a repeated ask of the held bundle = %+v, %v; want ended", a, err)
|
||||
}
|
||||
other := req("other")
|
||||
other.Digest = digestOf([]byte("other bytes"))
|
||||
if _, err := b.h.Publish(ArtifactSeat, builder, other); err == nil || !strings.Contains(err.Error(), "at digest "+part.Digest+", not "+other.Digest) {
|
||||
t.Errorf("other bytes at the held version = %v; want refused naming the held layer %s", err, part.Digest)
|
||||
}
|
||||
if len(reg.blobs) != uploads {
|
||||
t.Errorf("the store took %d blobs more; want none", len(reg.blobs)-uploads)
|
||||
}
|
||||
img := []byte(`{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","layers":[]}`)
|
||||
reg.manifests["m/image:0.2.0"], reg.types["m/image:0.2.0"] = img, mediaManifest
|
||||
ir := PublishRequest{Name: "m/image", Version: "0.2.0", Kind: KindImage, Digest: digestOf(img)}
|
||||
if ref, other, err := o.Held(ir); err != nil || ref != "m/image@"+digestOf(img) || other != "" {
|
||||
t.Errorf("an image held at its manifest = %q, %q, %v; want held", ref, other, err)
|
||||
}
|
||||
})
|
||||
t.Run("only the missing layers are sent, and an idle transfer is dropped", func(t *testing.T) {
|
||||
b := newBed(true)
|
||||
other := []byte("layer two")
|
||||
@@ -524,8 +447,7 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
t.Run("the sweep's removal, hold and release", func(t *testing.T) {
|
||||
b := newBed(true)
|
||||
sweep := "novox.artifact-store-tools"
|
||||
// A removal for retention is taken whoever asks it; the seat's holders are not read (design 47 H2 §813 (7)).
|
||||
if _, err := b.v.Remove(ArtifactSeat, "novox.artifact-sweeper", RemoveRequest{Name: "m/a", Digest: part.Digest, Kind: KindArchive, Reason: ReasonRetention}); err != nil {
|
||||
if _, err := b.v.Remove(ArtifactSeat, sweep, RemoveRequest{Name: "m/a", Digest: part.Digest, Kind: KindArchive, Reason: ReasonRetention}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := b.v.HoldOne(ArtifactSeat, sweep, HoldRequest{Name: "m/b", Kind: KindArchive, Digest: part.Digest}); err != nil {
|
||||
@@ -542,7 +464,7 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
for _, s := range b.said {
|
||||
events = append(events, s.event+" "+s.body.By)
|
||||
}
|
||||
if want := []string{"removed novox.artifact-sweeper", "held " + sweep, "released person.jochen"}; !reflect.DeepEqual(events, want) {
|
||||
if want := []string{"removed " + sweep, "held " + sweep, "released person.jochen"}; !reflect.DeepEqual(events, want) {
|
||||
t.Errorf("said %v, want %v", events, want)
|
||||
}
|
||||
b.cat.tags["m/p:0.1.5-feat-x.1"] = part.Digest
|
||||
@@ -744,22 +666,6 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
})
|
||||
// Design 47 G A-490: the publish contract's no-parts form, as every publish holder is held to it.
|
||||
t.Run("the publish conformance cases", func(t *testing.T) {
|
||||
publishconformance.Run(t, func(t *testing.T) publishconformance.Bed {
|
||||
b := newBed(true)
|
||||
return publishconformance.Bed{
|
||||
Hold: func(d string) { b.store.has[d] = true },
|
||||
Publish: func(name, version, digest string) publishconformance.Outcome {
|
||||
got, err := b.h.Publish(ArtifactSeat, builder, PublishRequest{Name: name, Version: version, Kind: KindBundle, Digest: digest, Transfer: "t"})
|
||||
o := publishconformance.Outcome{Ended: got.Ended, Lacks: len(got.Missing), Err: err}
|
||||
if len(b.said) > 0 {
|
||||
o.Event, o.Reason = b.said[len(b.said)-1].event, b.said[len(b.said)-1].body.Reason
|
||||
}
|
||||
return o
|
||||
}}
|
||||
})
|
||||
})
|
||||
t.Run("a publish with a version and no parts tags the held digest at the store's size; one not held is not stored", func(t *testing.T) {
|
||||
// Design 47 G A-417, A-73, A-50: the build stored the bundle untagged; the builder's publish of its version with that
|
||||
// digest and no parts tags it and says published, nothing sent; a digest the store holds none of is refused as not
|
||||
@@ -786,23 +692,6 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
if err := json.Unmarshal(store.manifests["m/tools:0.1.4"], &man); err != nil || len(man.Layers) != 1 || man.Layers[0].Size != int64(len(blob)) || man.Layers[0].Digest != digestOf(blob) {
|
||||
t.Errorf("the version's manifest %s, want one layer of %d bytes", store.manifests["m/tools:0.1.4"], len(blob))
|
||||
}
|
||||
// `versions` answers the digest the version's reference names and its published carries (design 47 G A-430a): on the
|
||||
// artifact seat the blob, which the node-engine fetches as /blobs/<digest>, never the manifest's own digest.
|
||||
v := &Verbs{Holder: b.h, Catalog: &OCI{Base: base}}
|
||||
for _, c := range []struct{ seat, version, want string }{
|
||||
{ArtifactSeat, "0.1.4", digestOf(blob)},
|
||||
{ImageSeat, "0.1.4", digestOf(store.manifests["m/tools:0.1.4"])},
|
||||
{ArtifactSeat, "0.1.9", ""},
|
||||
} {
|
||||
if got, err := v.VersionDigest(c.seat, "m/tools", c.version); err != nil || got != c.want {
|
||||
t.Errorf("VersionDigest(%s, m/tools, %s) = %q, %v, want %q", c.seat, c.version, got, err, c.want)
|
||||
}
|
||||
}
|
||||
// A manifest that keeps no single blob answers no digest and says why.
|
||||
store.manifests["m/tools:0.2.0"] = []byte(`{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","config":{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:c"},"layers":[{"digest":"sha256:a"},{"digest":"sha256:b"}]}`)
|
||||
if got, err := v.VersionDigest(ArtifactSeat, "m/tools", "0.2.0"); got != "" || err == nil || !strings.Contains(err.Error(), "2 layers") {
|
||||
t.Errorf("VersionDigest of a manifest of two layers = %q, %v, want no digest and why", got, err)
|
||||
}
|
||||
gone := "sha256:" + strings.Repeat("e", 64)
|
||||
_, err = b.h.Publish(ArtifactSeat, "x.mesh-delivery-builder", PublishRequest{Name: "m/tools", Version: "0.1.5", Kind: KindBundle, Digest: gone, Transfer: "republish-m-0-1-5-tools"})
|
||||
if err == nil || !strings.Contains(err.Error(), "not stored: ") || !strings.Contains(err.Error(), gone) || store.manifests["m/tools:0.1.5"] != nil ||
|
||||
@@ -978,24 +867,10 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
Name string `json:"name"`
|
||||
Port int `json:"port"`
|
||||
} `json:"listens"`
|
||||
Class string `json:"class"`
|
||||
State []struct {
|
||||
Name string `json:"name"`
|
||||
MaxMiB int `json:"max-mib"`
|
||||
} `json:"state"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Design 47 H2 H-A-411 (2): a core module, and every state names its cap, 8 MiB.
|
||||
if m.Class != "core" {
|
||||
t.Errorf("class %q, want core", m.Class)
|
||||
}
|
||||
for _, st := range m.State {
|
||||
if st.MaxMiB != 8 {
|
||||
t.Errorf("the state %s names max-mib %d, want 8", st.Name, st.MaxMiB)
|
||||
}
|
||||
}
|
||||
// The reservation is asked through the seat mesh-builds, so the manifest takes no package of the builder (design 47 §381).
|
||||
if len(m.Packages) != 0 {
|
||||
t.Errorf("packages %v: want none", m.Packages)
|
||||
|
||||
@@ -9,12 +9,9 @@ package holder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
|
||||
)
|
||||
@@ -28,52 +25,27 @@ type StoreFiles interface {
|
||||
Manifests(ctx context.Context, l *Listing) (map[string]*Manifest, map[string]string)
|
||||
}
|
||||
|
||||
// ListPageBytes is the most a page of oci-artifact-registry's list answers (design 47 H2 §811): 768 KB.
|
||||
const ListPageBytes = 768 << 10
|
||||
|
||||
// ListQuery is what a list is asked: Name or Module (`<module>/*`) filter the repositories while the files are read, Next
|
||||
// is the cursor a page answered, and PageBytes the most a page answers, 0 for the whole answer at once (the seat
|
||||
// container-image-registry's list, whose form takes no cursor).
|
||||
type ListQuery struct {
|
||||
Name, Module, Next string
|
||||
PageBytes int
|
||||
}
|
||||
|
||||
// ListPage is one page of a list: its repositories in order of their names, Total the repositories the filters hold in
|
||||
// all, and Next the cursor of the next page, empty at the end; Capped says the page stopped at PageBytes.
|
||||
type ListPage struct {
|
||||
Repositories []reg.Repository
|
||||
Capped bool
|
||||
Total int
|
||||
Next string
|
||||
}
|
||||
|
||||
// List is `list` on seat: the repositories it holds, or those q's filters name; a repository the seat holds nothing of is
|
||||
// not named, and a name not held is an empty answer. A paged list continues after the repository its cursor names, in
|
||||
// order of the names (design 47 H2 §779, §811, §813 (7)). A manifest whose content cannot be read fails the answer, by
|
||||
// name: a listing missing one would read as the store not holding it.
|
||||
func (v *Verbs) List(ctx context.Context, seat, caller string, q ListQuery) (ListPage, error) {
|
||||
// List is `list` on seat: the repositories it holds, or name alone; a repository the seat holds nothing of is not named,
|
||||
// and name not held is an empty answer. A manifest whose content cannot be read fails the answer, by name: a listing
|
||||
// missing one would read as the store not holding it.
|
||||
func (v *Verbs) List(ctx context.Context, seat, caller, name string) ([]reg.Repository, error) {
|
||||
if seat != ImageSeat && seat != ArtifactSeat {
|
||||
return ListPage{}, fmt.Errorf("list: %s is no seat of distribution's", seat)
|
||||
return nil, fmt.Errorf("list: %s is no seat of distribution's", seat)
|
||||
}
|
||||
if v.Files == nil {
|
||||
return ListPage{}, errors.New("list: this process reads no store files")
|
||||
}
|
||||
after, err := listCursor(q.Next)
|
||||
if err != nil {
|
||||
return ListPage{}, err
|
||||
return nil, errors.New("list: this process reads no store files")
|
||||
}
|
||||
l, err := v.Files.List(ctx)
|
||||
if err != nil {
|
||||
return ListPage{}, fmt.Errorf("list: the store's files cannot be listed: %w", err)
|
||||
return nil, fmt.Errorf("list: the store's files cannot be listed: %w", err)
|
||||
}
|
||||
kept := &Listing{Blobs: l.Blobs, Repos: map[string]*Repo{}}
|
||||
for name, r := range l.Repos {
|
||||
if (q.Name == "" || name == q.Name) && (q.Module == "" || strings.HasPrefix(name, q.Module+"/")) {
|
||||
kept.Repos[name] = r
|
||||
if name != "" {
|
||||
r, held := l.Repos[name]
|
||||
l = &Listing{Blobs: l.Blobs, Repos: map[string]*Repo{}}
|
||||
if held {
|
||||
l.Repos[name] = r
|
||||
}
|
||||
}
|
||||
l = kept
|
||||
docs, unread := v.Files.Manifests(ctx, l)
|
||||
if len(unread) > 0 {
|
||||
keys := make([]string, 0, len(unread))
|
||||
@@ -81,11 +53,11 @@ func (v *Verbs) List(ctx context.Context, seat, caller string, q ListQuery) (Lis
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return ListPage{}, fmt.Errorf("list: %d manifests cannot be read, the first %s: %s", len(keys), keys[0], unread[keys[0]])
|
||||
return nil, fmt.Errorf("list: %d manifests cannot be read, the first %s: %s", len(keys), keys[0], unread[keys[0]])
|
||||
}
|
||||
holds, err := v.Holds.List()
|
||||
if err != nil {
|
||||
return ListPage{}, fmt.Errorf("list: the holds cannot be read: %w", err)
|
||||
return nil, fmt.Errorf("list: the holds cannot be read: %w", err)
|
||||
}
|
||||
held := map[string]bool{}
|
||||
for _, h := range holds {
|
||||
@@ -130,37 +102,5 @@ func (v *Verbs) List(ctx context.Context, seat, caller string, q ListQuery) (Lis
|
||||
out = append(out, reg.Repository{Name: repo, Manifests: manifests})
|
||||
}
|
||||
}
|
||||
page := ListPage{Repositories: []reg.Repository{}, Total: len(out)}
|
||||
size := 0
|
||||
for _, r := range out {
|
||||
if r.Name <= after {
|
||||
continue
|
||||
}
|
||||
if q.PageBytes > 0 {
|
||||
raw, _ := json.Marshal(r)
|
||||
if size+len(raw) > q.PageBytes {
|
||||
if len(page.Repositories) == 0 {
|
||||
return ListPage{}, fmt.Errorf("list: the repository %s alone is over a page's %d bytes (%d)", r.Name, q.PageBytes, len(raw))
|
||||
}
|
||||
page.Capped = true
|
||||
page.Next = base64.RawURLEncoding.EncodeToString([]byte(page.Repositories[len(page.Repositories)-1].Name))
|
||||
break
|
||||
}
|
||||
size += len(raw)
|
||||
}
|
||||
page.Repositories = append(page.Repositories, r)
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
// listCursor is the repository a page's cursor continues after, "" for the first page; a cursor no page answered is refused.
|
||||
func listCursor(next string) (string, error) {
|
||||
if next == "" {
|
||||
return "", nil
|
||||
}
|
||||
name, err := base64.RawURLEncoding.DecodeString(next)
|
||||
if err != nil || len(name) == 0 {
|
||||
return "", fmt.Errorf("list: %q is no cursor a page of list answered", next)
|
||||
}
|
||||
return string(name), nil
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package holder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -133,8 +132,7 @@ func TestListAnswersTheStoreThroughTheSeat(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
page, err := b.v.List(ctx, ImageSeat, "mesh-anyone", ListQuery{})
|
||||
images := page.Repositories
|
||||
images, err := b.v.List(ctx, ImageSeat, "mesh-anyone", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -157,8 +155,7 @@ func TestListAnswersTheStoreThroughTheSeat(t *testing.T) {
|
||||
if !reflect.DeepEqual(images, want) {
|
||||
t.Errorf("the image seat lists %+v\nwant %+v", images, want)
|
||||
}
|
||||
page, err = b.v.List(ctx, ArtifactSeat, "mesh-anyone", ListQuery{})
|
||||
artifacts := page.Repositories
|
||||
artifacts, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -169,42 +166,16 @@ func TestListAnswersTheStoreThroughTheSeat(t *testing.T) {
|
||||
if !reflect.DeepEqual(artifacts, wantA) {
|
||||
t.Errorf("the artifact seat lists %+v\nwant %+v", artifacts, wantA)
|
||||
}
|
||||
if one, err := b.v.List(ctx, ImageSeat, "mesh-anyone", ListQuery{Name: "app/tools"}); err != nil || len(one.Repositories) != 0 {
|
||||
if one, err := b.v.List(ctx, ImageSeat, "mesh-anyone", "app/tools"); err != nil || len(one) != 0 {
|
||||
t.Errorf("the image seat's list of a bundle's repository: %+v, %v", one, err)
|
||||
}
|
||||
if one, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", ListQuery{Name: "mesh/facts"}); err != nil || len(one.Repositories) != 1 || one.Repositories[0].Name != "mesh/facts" {
|
||||
if one, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", "mesh/facts"); err != nil || len(one) != 1 || one[0].Name != "mesh/facts" {
|
||||
t.Errorf("one repository: %+v, %v", one, err)
|
||||
}
|
||||
// Design 47 H2 §813 (7), §811, §779: the artifact seat's list filters by module while it reads, and pages by its cursor in
|
||||
// order of the names, each page within its bytes, saying capped and the total the filters hold.
|
||||
if mod, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", ListQuery{Module: "app", PageBytes: ListPageBytes}); err != nil ||
|
||||
!reflect.DeepEqual(mod, ListPage{Repositories: wantA[:1], Total: 1}) {
|
||||
t.Errorf("the module app's repositories: %+v, %v", mod, err)
|
||||
}
|
||||
first, _ := json.Marshal(wantA[0])
|
||||
second, _ := json.Marshal(wantA[1])
|
||||
var paged []reg.Repository
|
||||
q := ListQuery{PageBytes: max(len(first), len(second))} // one repository a page
|
||||
for pages := 0; ; pages++ {
|
||||
p, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", q)
|
||||
if err != nil || p.Total != 2 || p.Capped != (p.Next != "") || pages > 2 {
|
||||
t.Fatalf("page %d: %+v, %v", pages, p, err)
|
||||
}
|
||||
paged = append(paged, p.Repositories...)
|
||||
if q.Next = p.Next; q.Next == "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(paged, wantA) {
|
||||
t.Errorf("the pages hold %+v, want %+v", paged, wantA)
|
||||
}
|
||||
if _, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", ListQuery{Next: "!"}); err == nil {
|
||||
t.Error("a cursor no page answered was taken")
|
||||
}
|
||||
// A manifest whose content cannot be read fails the answer: a listing without it would read as the store not holding it.
|
||||
delete(f.manifests, dg("m-old"))
|
||||
b.v.Files = &Files{Container: "mesh-registry", Run: f.run(t)} // what was read is kept: a fresh reader reads again
|
||||
if _, err := b.v.List(ctx, ImageSeat, "mesh-anyone", ListQuery{}); err == nil || !strings.Contains(err.Error(), "cannot be read") {
|
||||
if _, err := b.v.List(ctx, ImageSeat, "mesh-anyone", ""); err == nil || !strings.Contains(err.Error(), "cannot be read") {
|
||||
t.Errorf("an unreadable manifest: %v", err)
|
||||
}
|
||||
// The files' listing: a repository with slashes read, a cut or stray one refused.
|
||||
|
||||
@@ -207,25 +207,16 @@ func (o *OCI) layerSize(t PublishRequest, parts map[string][]byte) (int64, error
|
||||
return resp.ContentLength, nil
|
||||
}
|
||||
|
||||
// Held is the reference of r's name at r's version when the store holds r's digest under it, compared like with like
|
||||
// (H-A-474): an image's by the manifest's digest the version's tag names, a bundle's or an archive's by the one layer
|
||||
// that manifest keeps (the blob Put stored and the reference names). When the store holds the version at another digest,
|
||||
// other names that digest of the same kind.
|
||||
func (o *OCI) Held(r PublishRequest) (string, string, error) {
|
||||
var d string
|
||||
var err error
|
||||
if r.Kind == KindImage || r.Kind == KindUpstream {
|
||||
d, err = o.Digest(r.Name, r.Version)
|
||||
} else {
|
||||
d, err = o.Blob(r.Name, r.Version)
|
||||
}
|
||||
// Held is the reference of name at version when the store holds digest under it.
|
||||
func (o *OCI) Held(name, version, digest string) (string, bool, error) {
|
||||
d, err := o.Digest(name, version)
|
||||
if err != nil || d == "" {
|
||||
return "", "", err
|
||||
return "", false, err
|
||||
}
|
||||
if d != r.Digest {
|
||||
return "", d, nil
|
||||
if d != digest {
|
||||
return "", false, nil
|
||||
}
|
||||
return r.Name + "@" + d, "", nil
|
||||
return name + "@" + d, true, nil
|
||||
}
|
||||
|
||||
// Holds is whether the store holds what r's digest names under r's name: an image's manifest by its digest, or a bundle's
|
||||
@@ -282,29 +273,6 @@ func (o *OCI) Digest(name, version string) (string, error) {
|
||||
return resp.Header.Get("Docker-Content-Digest"), nil
|
||||
}
|
||||
|
||||
// Blob is the one blob the manifest name holds at version keeps (Manifest.Holder's shape): a bundle's or an archive's,
|
||||
// what its reference names. Empty when none is held at version; an error naming why when the manifest keeps no single blob.
|
||||
func (o *OCI) Blob(name, version string) (string, error) {
|
||||
resp, b, err := o.do(http.MethodGet, "/v2/"+name+"/manifests/"+version, "", nil, mediaManifest)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return "", nil
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("the store answered %d for %s:%s", resp.StatusCode, name, version)
|
||||
}
|
||||
m, err := ParseManifest(b)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("the manifest of %s:%s cannot be read: %w", name, version, err)
|
||||
}
|
||||
if !m.Holder() {
|
||||
return "", fmt.Errorf("the manifest of %s:%s keeps %d layers under a %q configuration, not one blob", name, version, len(m.Layers), m.ConfigMedia)
|
||||
}
|
||||
return m.Layers[0], nil
|
||||
}
|
||||
|
||||
// ErrNoTagAlone is a store that cannot delete a tag alone. registry 2.8.3, the store's pinned image, is one: its
|
||||
// DeleteManifest (distribution v2.8.3 registry/handlers/manifests.go) ignores a tag reference and deletes the empty
|
||||
// digest, whose check fails, answered 400 DIGEST_INVALID; registry 3 untags alone.
|
||||
|
||||
@@ -3,14 +3,13 @@ package holder
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
||||
)
|
||||
|
||||
// Reserved is the holder's check of a publish against the builder's reservation (novox/hq design 47 §381, §478, §504): it asks the
|
||||
// seat mesh-builds' verb reserved with the publish's name and version, a package's name in its ecosystem, and accepts the publish
|
||||
// only when that version is the one reserved and an artifact of it has the publish's digest or none recorded yet (A-490j). It reads no catalog and a publish gains
|
||||
// only when that version is the one reserved and an artifact of it has the publish's digest. It reads no catalog and a publish gains
|
||||
// no field. It is the Builder the holder's Check asks.
|
||||
type Reserved struct {
|
||||
// Ask is the runtime's request and reply over the bus (mesh-sdk stdio.Ask).
|
||||
@@ -32,20 +31,12 @@ func (r Reserved) Recorded(name, version, digest string) (bool, string, error) {
|
||||
return false, "", fmt.Errorf("%s answered no reservation for %s@%s: %s", Address, name, version, raw)
|
||||
}
|
||||
if a.Version != version {
|
||||
return false, fmt.Sprintf("%s has no reservation of version %s of %s (it reserved %s)", a.Module, version, name, a.Version), nil
|
||||
return false, fmt.Sprintf("%s reserved %s for %s, not %s", a.Module, a.Version, name, version), nil
|
||||
}
|
||||
var reserved []string
|
||||
for _, art := range a.Artifacts {
|
||||
// An artifact whose build has not recorded its digest yet takes the publish: the builder records a build's digests only when
|
||||
// the build ends, while the build module publishes during it with the reserved version (design 47 G §369, A-415, A-490j,
|
||||
// H-A-387). A digest the build then records that differs from what the holder holds fails the build.
|
||||
if art.Digest == digest || art.Digest == "" {
|
||||
if art.Digest == digest {
|
||||
return true, "", nil
|
||||
}
|
||||
reserved = append(reserved, art.Digest)
|
||||
}
|
||||
if len(reserved) == 0 {
|
||||
return false, fmt.Sprintf("%s reserved %s@%s with no artifact, so no digest takes %s", a.Module, name, version, digest), nil
|
||||
}
|
||||
return false, fmt.Sprintf("%s reserved %s@%s for digest %s, not %s", a.Module, name, version, strings.Join(reserved, ", "), digest), nil
|
||||
return false, fmt.Sprintf("%s reserved %s@%s for another digest than %s", a.Module, name, version, digest), nil
|
||||
}
|
||||
|
||||
@@ -19,10 +19,8 @@ func reservedChecksAPublishAgainstTheReservation(t *testing.T) {
|
||||
}{
|
||||
{name: "the reserved version and digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:aa"}]}`, ok: true},
|
||||
{name: "another artifact of it carries the digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:aux"},{"artifact":"mesh-image:image","digest":"sha256:aa"}]}`, ok: true},
|
||||
{name: "no digest recorded yet", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image"}]}`, ok: true},
|
||||
{name: "another digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:bb"}]}`, why: "reserved registry/mesh-image@0.1.7 for digest sha256:bb, not sha256:aa"},
|
||||
{name: "no artifact reserved", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[]}`, why: "with no artifact, so no digest takes sha256:aa"},
|
||||
{name: "another version reserved", answer: `{"module":"mesh-image","version":"0.1.8","artifacts":[]}`, why: "no reservation of version 0.1.7 of registry/mesh-image (it reserved 0.1.8)"},
|
||||
{name: "another digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:bb"}]}`, why: "another digest than sha256:aa"},
|
||||
{name: "another version reserved", answer: `{"module":"mesh-image","version":"0.1.8","artifacts":[]}`, why: "reserved 0.1.8 for registry/mesh-image, not 0.1.7"},
|
||||
{name: "the builder refused the name", err: errors.New("no entry carries registry/mesh-image"), why: "no entry carries registry/mesh-image"},
|
||||
{name: "an answer that is none", answer: `{"error":"x"}`, why: "answered no reservation"},
|
||||
} {
|
||||
|
||||
@@ -19,6 +19,9 @@ const (
|
||||
OldSeat = "mesh-artifact-store"
|
||||
)
|
||||
|
||||
// SweepSeat is the seat of the artifact sweep, whose holder alone removes an untagged version for retention.
|
||||
const SweepSeat = "mesh-artifact-sweep"
|
||||
|
||||
// The verbs of the seats (design 26 *A seat's definition*; design 51 *One contract*).
|
||||
const (
|
||||
VerbPublish = "publish"
|
||||
|
||||
@@ -140,11 +140,9 @@ type Store interface {
|
||||
Has(name, digest string) (bool, error)
|
||||
// Put stores a publish's parts under its name and version and answers the reference's path (SeatReference).
|
||||
Put(r PublishRequest, parts map[string][]byte) (reference string, err error)
|
||||
// Held is the reference's path of a publish's name at its version when the store already holds its digest under it: a
|
||||
// rebuild of a held fingerprint answers it and stores nothing. The digest is compared like with like (H-A-474): an
|
||||
// image's manifest digest, a bundle's or an archive's blob digest. When the store holds the version at another digest,
|
||||
// other names that digest and reference is empty; neither is set when the store holds no such version.
|
||||
Held(r PublishRequest) (reference, other string, err error)
|
||||
// Held is the reference's path of name at version when the store already holds digest under it: a rebuild of a held
|
||||
// fingerprint answers it and stores nothing.
|
||||
Held(name, version, digest string) (reference string, ok bool, err error)
|
||||
// Holds says whether the store holds what a publish's digest names under its name: an image's manifest, or a bundle's or
|
||||
// an archive's blob. The store is the artifact store (A-50): `artifact-store://<name>/blobs/<digest>` is that blob.
|
||||
Holds(r PublishRequest) (bool, error)
|
||||
@@ -268,19 +266,15 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer,
|
||||
}
|
||||
// A rebuild of a held fingerprint answers the held version and stores nothing.
|
||||
if r.Version != "" {
|
||||
var ref, other string
|
||||
var ref string
|
||||
var ok bool
|
||||
err := h.timed("the store's read of what it holds (the program's registry)", func() (err error) {
|
||||
ref, other, err = h.Store.Held(r)
|
||||
ref, ok, err = h.Store.Held(r.Name, r.Version, r.Digest)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return PublishAnswer{}, fmt.Errorf("publish: the store cannot say whether it holds %s@%s: %w", r.Name, r.Version, err)
|
||||
} else if other != "" {
|
||||
// A second publish of a held version with another digest is refused before the reservation is asked, as the forge
|
||||
// holds the first (A-490j, H-A-393): while the reserved digest is still empty it would pass, and the store's Put
|
||||
// would move the version's tag.
|
||||
return h.refusePublish(seat, r, fmt.Sprintf("the store holds %s@%s at digest %s, not %s", r.Name, r.Version, other, r.Digest))
|
||||
} else if ref != "" {
|
||||
} else if ok {
|
||||
return PublishAnswer{Transfer: r.Transfer, Ended: true}, h.timed("the published event on the seat", func() error {
|
||||
return h.Say(seat, EventPublished, h.eventOf(seat, r, SeatReference(seat, ref), ""))
|
||||
})
|
||||
@@ -304,16 +298,6 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer,
|
||||
return answerOf(got), nil
|
||||
}
|
||||
|
||||
// refusePublish ends a publish refused before anything is stored: its finding raised, refused said on the seat, the reason
|
||||
// answered.
|
||||
func (h *Holder) refusePublish(seat string, r PublishRequest, reason string) (PublishAnswer, error) {
|
||||
h.Finding("publish-refused", r.Name+"@"+r.Version, reason, true)
|
||||
if err := h.Say(seat, EventRefused, h.eventOf(seat, r, "", reason)); err != nil {
|
||||
return PublishAnswer{}, fmt.Errorf("publish: %s, and the refused is not said: %w", reason, err)
|
||||
}
|
||||
return PublishAnswer{Transfer: r.Transfer, Ended: true}, errors.New("publish: " + reason)
|
||||
}
|
||||
|
||||
// tagHeld is a publish with a version, a digest and no parts (design 47 G A-417, A-73, A-50): what the store holds of that
|
||||
// digest under the name (the build's untagged publish, the artifact store's blob) is checked against the builder's reservation
|
||||
// and tagged with the version, and published is said; when the store holds none of it, the publish is refused as not stored,
|
||||
@@ -327,7 +311,13 @@ func (h *Holder) tagHeld(seat string, r PublishRequest) (PublishAnswer, error) {
|
||||
if err != nil {
|
||||
return PublishAnswer{}, fmt.Errorf("publish: the store cannot say whether it holds %s of %s: %w", r.Digest, r.Name, err)
|
||||
}
|
||||
refuse := func(reason string) (PublishAnswer, error) { return h.refusePublish(seat, r, reason) }
|
||||
refuse := func(reason string) (PublishAnswer, error) {
|
||||
h.Finding("publish-refused", r.Name+"@"+r.Version, reason, true)
|
||||
if err := h.Say(seat, EventRefused, h.eventOf(seat, r, "", reason)); err != nil {
|
||||
return PublishAnswer{}, fmt.Errorf("publish: %s, and the refused is not said: %w", reason, err)
|
||||
}
|
||||
return PublishAnswer{Transfer: r.Transfer, Ended: true}, errors.New("publish: " + reason)
|
||||
}
|
||||
if !held {
|
||||
return refuse(fmt.Sprintf("not stored: the store holds no %s of %s, so version %s names nothing", r.Digest, r.Name, r.Version))
|
||||
}
|
||||
@@ -407,38 +397,6 @@ func (h *Holder) DropIdle() ([]string, error) { return h.receiver().DropIdle() }
|
||||
// already gone. It answers whether it dropped the transfer.
|
||||
func (h *Holder) Expired(id string) (bool, error) { return h.receiver().Expired(id) }
|
||||
|
||||
// Repeat is `publish` given again with an ask id the holder already took and whose first act ended (design 47 H2 §775;
|
||||
// H-A-381): it never publishes twice and keeps no answer, but answers the act's state as it stands, read afresh, which is
|
||||
// what the act answers when done: a version the store holds ended, an open transfer the parts it lacks; a transfer that
|
||||
// is gone, with nothing held, says the act never happened, and is refused naming it.
|
||||
func (h *Holder) Repeat(seat, caller string, r PublishRequest) (PublishAnswer, error) {
|
||||
if err := checkPublish(seat, r); err != nil {
|
||||
return PublishAnswer{}, err
|
||||
}
|
||||
if r.Version != "" {
|
||||
var ref string
|
||||
err := h.timed("the store's read of what it holds (the program's registry)", func() (err error) {
|
||||
ref, _, err = h.Store.Held(r)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return PublishAnswer{}, fmt.Errorf("publish: the store cannot say whether it holds %s@%s: %w", r.Name, r.Version, err)
|
||||
} else if ref != "" {
|
||||
return PublishAnswer{Transfer: r.Transfer, Ended: true}, nil
|
||||
}
|
||||
}
|
||||
t, found, err := h.Transfers.Get(r.Transfer)
|
||||
switch {
|
||||
case err != nil:
|
||||
return PublishAnswer{}, fmt.Errorf("publish: the transfer %s of a repeated ask cannot be read: %w", r.Transfer, err)
|
||||
case !found:
|
||||
return PublishAnswer{}, fmt.Errorf("publish: refused: the ask was taken before, and transfer %s is gone with nothing of %s held: the act did not happen", r.Transfer, r.Name)
|
||||
case t.Opener != caller:
|
||||
return PublishAnswer{}, fmt.Errorf("publish: refused for %s: transfer %s was opened by another caller", caller, r.Transfer)
|
||||
}
|
||||
return PublishAnswer{Transfer: t.ID, Missing: t.Lacks()}, nil
|
||||
}
|
||||
|
||||
func checkPublish(seat string, r PublishRequest) error {
|
||||
switch {
|
||||
case !slices.Contains(Seats, seat):
|
||||
|
||||
@@ -19,9 +19,6 @@ type Catalog interface {
|
||||
Versions(name string) ([]string, error)
|
||||
// Digest is the digest name holds at version, empty when none.
|
||||
Digest(name, version string) (string, error)
|
||||
// Blob is the one blob the manifest name holds at version keeps, for a bundle or an archive: the digest its reference
|
||||
// names and its published event carries. Empty when none is held; an error when the manifest keeps no single blob.
|
||||
Blob(name, version string) (string, error)
|
||||
// Remove deletes name at version by its manifest's digest, every tag naming that digest with it; the store's
|
||||
// collector frees its blobs.
|
||||
Remove(name, version string) error
|
||||
@@ -88,6 +85,9 @@ type Verbs struct {
|
||||
Mirrors Mirrors
|
||||
// Files are the store's own files, which list reads (ADR 0632 §3).
|
||||
Files StoreFiles
|
||||
// SweepHolder is the module holding the seat `mesh-artifact-sweep`, as the seat's holders say it now (design 47 G §7, §428);
|
||||
// a removal for retention is that module's alone.
|
||||
SweepHolder func() (string, error)
|
||||
}
|
||||
|
||||
// VersionsAnswer is `versions`: the versions the seat holds of name.
|
||||
@@ -112,16 +112,6 @@ func (v *Verbs) Versions(seat, caller, name string) (VersionsAnswer, error) {
|
||||
return VersionsAnswer{Seat: seat, Name: name, Versions: got}, nil
|
||||
}
|
||||
|
||||
// VersionDigest is the digest `versions` answers for name at version on seat: the one an artifact's reference names and
|
||||
// its event `published` carries (the contract's Artifact), so a node-engine fetches the bytes by it (design 47 G A-430a):
|
||||
// on the artifact seat a bundle's or an archive's blob, on the image seat the image's manifest. Empty when none is held.
|
||||
func (v *Verbs) VersionDigest(seat, name, version string) (string, error) {
|
||||
if seat == ArtifactSeat {
|
||||
return v.Catalog.Blob(name, version)
|
||||
}
|
||||
return v.Catalog.Digest(name, version)
|
||||
}
|
||||
|
||||
// RemoveRequest is `remove`: a tagged version, or with the reason retention an untagged one by its digest.
|
||||
type RemoveRequest struct {
|
||||
Name string `json:"name"`
|
||||
@@ -147,7 +137,7 @@ type Removal struct {
|
||||
const ReasonSharedDigest = reg.RemoveAnswerReasonSharedDigest
|
||||
|
||||
// Remove is `remove`: the builder removes a tagged version; the artifact sweep's holder an untagged bundle, archive or
|
||||
// copied base with the reason retention, which a hold keeps, its caller not judged (design 47 H2 §813 (7)). Every removal says `removed` with its reason.
|
||||
// copied base with the reason retention, which a hold keeps. Every removal says `removed` with its reason.
|
||||
func (v *Verbs) Remove(seat, caller string, r RemoveRequest) (Removal, error) {
|
||||
if strings.TrimSpace(r.Name) == "" || strings.TrimSpace(r.Reason) == "" {
|
||||
return Removal{}, errors.New("remove: a name and a reason are required")
|
||||
@@ -155,8 +145,13 @@ func (v *Verbs) Remove(seat, caller string, r RemoveRequest) (Removal, error) {
|
||||
ev := Event{Name: r.Name, Version: r.Version, Kind: r.Kind, Digest: r.Digest, Holder: Self, Reason: r.Reason, By: caller}
|
||||
switch {
|
||||
case r.Reason == ReasonRetention:
|
||||
// The removal for retention the holder of mesh-artifact-sweep asks; its caller is not judged, as every account may call
|
||||
// every verb (design 47 H2 §813 (7)).
|
||||
sweep, err := v.SweepHolder()
|
||||
if err != nil {
|
||||
return Removal{}, fmt.Errorf("remove: the holder of %s cannot be read, so nothing is removed for retention: %w", SweepSeat, err)
|
||||
}
|
||||
if CallerModule(caller) != sweep {
|
||||
return Removal{}, fmt.Errorf("remove: refused for %s: a removal for retention is the artifact sweep's alone", caller)
|
||||
}
|
||||
if r.Digest == "" {
|
||||
return Removal{}, errors.New("remove: a removal for retention names the digest")
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "distribution",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"provides": [
|
||||
{
|
||||
"name": "artifact-store",
|
||||
@@ -250,44 +249,14 @@
|
||||
}
|
||||
],
|
||||
"state": [
|
||||
{
|
||||
"name": "transfers",
|
||||
"max-mib": 8
|
||||
},
|
||||
"transfers",
|
||||
{
|
||||
"name": "idle",
|
||||
"key-ttl": true,
|
||||
"max-mib": 8
|
||||
"key-ttl": true
|
||||
},
|
||||
{
|
||||
"name": "holds",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "windows",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "mirrors",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "findings",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "taken",
|
||||
"key-ttl": true,
|
||||
"max-mib": 8
|
||||
}
|
||||
],
|
||||
"config": {
|
||||
"ask-taken-bound": {
|
||||
"type": "string",
|
||||
"description": "How long the registry keeps the id of a publish ask it took, as a duration such as 10m (design 47 H2 §775): a publish given again with that id within it acts not again but answers the transfer as it stands. The largest caller's bound, the pipeline runner's ask-unanswered-bound, by default.",
|
||||
"trusted": false,
|
||||
"runtime": true,
|
||||
"default": "10m"
|
||||
}
|
||||
}
|
||||
"holds",
|
||||
"windows",
|
||||
"mirrors",
|
||||
"findings"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "dmenu",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "dnsmasq",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"upgrade": {
|
||||
"policy": "record",
|
||||
"why": "the mesh's resolver: a build that breaks it can stop a machine resolving the bus, and then neither the gate's rollback nor a push reaches it (hq ADR 0236, issue 260)"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "docker-compose",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "docker",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager",
|
||||
|
||||
@@ -1,15 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any {
|
||||
// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in
|
||||
// every desktop module that carries it.
|
||||
|
||||
type manifest struct {
|
||||
Module string `json:"module"`
|
||||
Version string `json:"version"`
|
||||
Moved bool `json:"moved"`
|
||||
Capabilities []string `json:"capabilities"`
|
||||
Requires []string `json:"requires"`
|
||||
Claims []claim `json:"claims"`
|
||||
Seats []any `json:"seats"`
|
||||
Tools []string `json:"tools"`
|
||||
Config map[string]config `json:"config"`
|
||||
Emits []string `json:"emits"`
|
||||
Items []item `json:"items"`
|
||||
Contributions []any `json:"contributions"`
|
||||
Environment *environment `json:"environment"`
|
||||
Shell []shellCode `json:"shell"`
|
||||
Resources []map[string]any `json:"resources"`
|
||||
Build struct {
|
||||
Artifacts []map[string]any `json:"artifacts"`
|
||||
} `json:"build"`
|
||||
}
|
||||
|
||||
// config is one config value as the definition declares it (novox/hq ADR 0348 §4).
|
||||
type config struct {
|
||||
Type string `json:"type"`
|
||||
Description string `json:"description"`
|
||||
Enum []any `json:"enum"`
|
||||
Minimum *float64 `json:"minimum"`
|
||||
Maximum *float64 `json:"maximum"`
|
||||
Pattern string `json:"pattern"`
|
||||
Trusted bool `json:"trusted"`
|
||||
Predefined string `json:"predefined"`
|
||||
Default any `json:"default"`
|
||||
Required bool `json:"required"`
|
||||
Runtime bool `json:"runtime"`
|
||||
}
|
||||
|
||||
type claim struct {
|
||||
Name string `json:"name"`
|
||||
Scope string `json:"scope"`
|
||||
Serves []string `json:"serves"`
|
||||
}
|
||||
|
||||
type environment struct {
|
||||
Variables map[string]string `json:"variables"`
|
||||
Path []map[string]any `json:"path"`
|
||||
}
|
||||
|
||||
type shellCode struct {
|
||||
For string `json:"for"`
|
||||
Slot string `json:"slot"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
func readManifest(t *testing.T) manifest {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dec := json.NewDecoder(strings.NewReader(string(raw)))
|
||||
dec.DisallowUnknownFields()
|
||||
var m manifest
|
||||
if err := dec.Decode(&m); err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (m manifest) resource(t *testing.T, id string) map[string]any {
|
||||
t.Helper()
|
||||
for _, r := range m.Resources {
|
||||
if r["id"] == id {
|
||||
@@ -20,7 +90,7 @@ func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any {
|
||||
return nil
|
||||
}
|
||||
|
||||
func packages(m catalogue.Manifest) (present, absent []string) {
|
||||
func (m manifest) packages() (present, absent []string) {
|
||||
for _, r := range m.Resources {
|
||||
if r["type"] == "package" {
|
||||
if r["absent"] == true {
|
||||
@@ -36,9 +106,9 @@ func packages(m catalogue.Manifest) (present, absent []string) {
|
||||
// fromTemplate checks that a file resource takes its text from its template file in the module's
|
||||
// repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the
|
||||
// template is there. The readable file in the repository is the one place the machine's text is written.
|
||||
func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) {
|
||||
func (m manifest) fromTemplate(t *testing.T, id, template string) {
|
||||
t.Helper()
|
||||
r := resource(t, m, id)
|
||||
r := m.resource(t, id)
|
||||
if r["type"] != "file" {
|
||||
t.Fatalf("%s is a %v, not a file", id, r["type"])
|
||||
}
|
||||
@@ -58,7 +128,7 @@ func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) {
|
||||
|
||||
// everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs
|
||||
// files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2).
|
||||
func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) {
|
||||
func (m manifest) everyFileIsATemplate(t *testing.T) {
|
||||
t.Helper()
|
||||
for _, r := range m.Resources {
|
||||
if _, inline := r["content"]; inline {
|
||||
@@ -67,10 +137,10 @@ func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) {
|
||||
}
|
||||
packs := 0
|
||||
for _, a := range m.Build.Artifacts {
|
||||
if a.Kind == "archive" && a.From == "files" {
|
||||
if a["kind"] == "archive" && a["from"] == "files" {
|
||||
packs++
|
||||
if a.Name != "files" {
|
||||
t.Errorf("the templates archive is named %v, not files", a.Name)
|
||||
if a["name"] != "files" {
|
||||
t.Errorf("the templates archive is named %v, not files", a["name"])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,9 +150,9 @@ func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) {
|
||||
}
|
||||
|
||||
// text is the text a file resource places on the machine: its template file's.
|
||||
func templateText(t *testing.T, m catalogue.Manifest, id string) string {
|
||||
func (m manifest) text(t *testing.T, id string) string {
|
||||
t.Helper()
|
||||
name, _ := resource(t, m, id)["template"].(string)
|
||||
name, _ := m.resource(t, id)["template"].(string)
|
||||
if name == "" {
|
||||
t.Fatalf("resource %s names no template", id)
|
||||
}
|
||||
@@ -95,7 +165,7 @@ func templateText(t *testing.T, m catalogue.Manifest, id string) string {
|
||||
|
||||
// checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle
|
||||
// serves each seat verb the claims promise as <seat>.<verb>, and that the Go bundle is declared.
|
||||
func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) {
|
||||
func checkTheToolsAgree(t *testing.T, m manifest) {
|
||||
t.Helper()
|
||||
own, seat := map[string]bool{}, map[string]bool{}
|
||||
for _, tool := range tools() {
|
||||
@@ -137,14 +207,14 @@ func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) {
|
||||
t.Errorf("the bundle serves %s, which no claim promises", name)
|
||||
}
|
||||
}
|
||||
var bundle *catalogue.Artifact
|
||||
var bundle map[string]any
|
||||
for _, a := range m.Build.Artifacts {
|
||||
if a.Kind == "bundle" {
|
||||
bundle = &a
|
||||
if a["kind"] == "bundle" {
|
||||
bundle = a
|
||||
}
|
||||
}
|
||||
if bundle == nil || bundle.Language != "go" || bundle.System != "arch" ||
|
||||
bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" {
|
||||
if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" ||
|
||||
bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" {
|
||||
t.Errorf("the Go tools bundle: %v", bundle)
|
||||
}
|
||||
}
|
||||
@@ -169,7 +239,7 @@ const displaySession = "contribution:node-display-session:config"
|
||||
|
||||
// i3Lines is what the module contributes to the window manager: the text of its item of the collection
|
||||
// contribution:node-display-session:config (design 57 L3, issue 848).
|
||||
func i3Lines(m catalogue.Manifest) string {
|
||||
func (m manifest) i3Lines() string {
|
||||
var out string
|
||||
for _, it := range m.Items {
|
||||
if it.Collection == displaySession {
|
||||
@@ -185,7 +255,7 @@ func i3Lines(m catalogue.Manifest) string {
|
||||
|
||||
// i3LinesAreSource checks the window-manager lines are one item whose template is the source file, and that no
|
||||
// old-form contribution to node-display-session is left.
|
||||
func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) {
|
||||
func (m manifest) i3LinesAreSource(t *testing.T, source string) {
|
||||
t.Helper()
|
||||
if len(m.Contributions) != 0 {
|
||||
t.Fatalf("the module still holds contributions: the lines are an item of %s", displaySession)
|
||||
@@ -199,7 +269,14 @@ func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) {
|
||||
if len(templates) != 1 || templates[0] != source {
|
||||
t.Fatalf("the items of %s are %v, want the one template %s", displaySession, templates, source)
|
||||
}
|
||||
if i3Lines(m) == "" {
|
||||
if m.i3Lines() == "" {
|
||||
t.Fatalf("the template %s holds no line", source)
|
||||
}
|
||||
}
|
||||
|
||||
type item struct {
|
||||
Collection string `json:"collection"`
|
||||
Name string `json:"name"`
|
||||
Attributes map[string]string `json:"attributes"`
|
||||
Template string `json:"template"`
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// dunst's shape (novox/hq ADR 0208): it claims node-notifier serving send and history, owns its
|
||||
@@ -16,44 +12,28 @@ import (
|
||||
// activation, so it requires no display of its own.
|
||||
|
||||
func TestItClaimsTheNotifierSeatServingSendAndHistory(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
m := readManifest(t)
|
||||
if m.Module != "dunst" || m.Seats != nil {
|
||||
t.Fatalf("module %q declares seats %v", m.Module, m.Seats)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
if m.Module != "dunst" || m.DefinesSeats != nil {
|
||||
t.Fatalf("module %q declares seats %v", m.Module, m.DefinesSeats)
|
||||
}
|
||||
if !reflect.DeepEqual(m.Claims, []catalogue.Claim{{Name: "node-notifier", Scope: "node", Serves: []string{"send", "history"}}}) {
|
||||
if !reflect.DeepEqual(m.Claims, []claim{{Name: "node-notifier", Scope: "node", Serves: []string{"send", "history"}}}) {
|
||||
t.Fatalf("claims: %+v", m.Claims)
|
||||
}
|
||||
// rofi, the context menu the dunstrc calls by name, is the rofi module's package: two modules on one
|
||||
// machine may not declare the same package, and the rofi module holds node-launcher on every
|
||||
// machine dunst serves.
|
||||
if present, absent := packages(m); !reflect.DeepEqual(present, []string{"dunst", "libnotify"}) || absent != nil {
|
||||
if present, absent := m.packages(); !reflect.DeepEqual(present, []string{"dunst", "libnotify"}) || absent != nil {
|
||||
t.Fatalf("packages: %v, absent %v", present, absent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestItOwnsItsFileAndTheDropInDirectory(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
fromTemplate(t, m, "configuration", "files/dunstrc")
|
||||
if p := resource(t, m, "configuration")["path"]; p != "${machine:account-home}/.config/dunst/dunstrc" {
|
||||
m := readManifest(t)
|
||||
m.fromTemplate(t, "configuration", "files/dunstrc")
|
||||
if p := m.resource(t, "configuration")["path"]; p != "${machine:account-home}/.config/dunst/dunstrc" {
|
||||
t.Fatalf("path: %v", p)
|
||||
}
|
||||
if d := resource(t, m, "dropins"); d["type"] != "directory" || d["path"] != "${machine:account-home}/.config/dunst/dunstrc.d" {
|
||||
if d := m.resource(t, "dropins"); d["type"] != "directory" || d["path"] != "${machine:account-home}/.config/dunst/dunstrc.d" {
|
||||
t.Fatalf("drop-ins: %v", d)
|
||||
}
|
||||
for _, r := range m.Resources {
|
||||
@@ -64,16 +44,8 @@ func TestItOwnsItsFileAndTheDropInDirectory(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTheFileIsTheDecidedOneAndCallsRofi(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
c := templateText(t, m, "configuration")
|
||||
m := readManifest(t)
|
||||
c := m.text(t, "configuration")
|
||||
for _, want := range []string{"origin = top-right", "transparency = 0", "corner_radius = 0", "font = Inter ${config:font-size}",
|
||||
"width = ${config:width}", "dmenu = rofi -dmenu -no-custom -p dunst"} {
|
||||
if !strings.Contains(c, " "+want+"\n") {
|
||||
@@ -94,16 +66,8 @@ func TestTheFileIsTheDecidedOneAndCallsRofi(t *testing.T) {
|
||||
// of them (dunst holds it open until the menu answers, and an Escape leaves it shown), and one without
|
||||
// actions closes. Every other click is as it was.
|
||||
func TestATapChoosesTheNotificationsAction(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
c := templateText(t, m, "configuration")
|
||||
m := readManifest(t)
|
||||
c := m.text(t, "configuration")
|
||||
for _, want := range []string{"mouse_left_click = do_action, close_current",
|
||||
"mouse_middle_click = do_action, close_current", "mouse_right_click = close_all"} {
|
||||
if !strings.Contains(c, " "+want+"\n") {
|
||||
@@ -118,17 +82,9 @@ func TestATapChoosesTheNotificationsAction(t *testing.T) {
|
||||
// The key that chooses from the keyboard is a line i3 places from this contribution (novox/hq ADR
|
||||
// 0212): a binding only, never a start, so the bus stays the only thing that starts dunst.
|
||||
func TestTheMenuKeyIsABindingI3Places(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
i3LinesAreSource(t, m, "files/i3/50-dunst.conf")
|
||||
lines := i3Lines(m)
|
||||
m := readManifest(t)
|
||||
m.i3LinesAreSource(t, "files/i3/50-dunst.conf")
|
||||
lines := m.i3Lines()
|
||||
if !strings.Contains(lines, "bindsym $mod+n exec --no-startup-id dunstctl context\n") {
|
||||
t.Fatalf("no key for the context menu: %s", lines)
|
||||
}
|
||||
@@ -144,31 +100,23 @@ func TestTheMenuKeyIsABindingI3Places(t *testing.T) {
|
||||
// predefined font-size (ADR 0348 §2), so a machine with a denser screen sets its own and every other
|
||||
// machine's file is unchanged. Neither is trusted: the file says "trusted": false.
|
||||
func TestTheFontSizeAndWidthAreConfigValuesWithTodaysDefaults(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
m := readManifest(t)
|
||||
want := map[string]float64{"font-size": 10, "width": 250}
|
||||
if len(m.Settings) != len(want) {
|
||||
t.Fatalf("config: %+v", m.Settings)
|
||||
if len(m.Config) != len(want) {
|
||||
t.Fatalf("config: %+v", m.Config)
|
||||
}
|
||||
for key, value := range want {
|
||||
s, ok := m.Settings[key]
|
||||
s, ok := m.Config[key]
|
||||
if !ok || s.Type != "number" || s.Default != value || s.Required || s.Trusted ||
|
||||
strings.TrimSpace(s.Description) == "" || s.Minimum == nil || s.Maximum == nil {
|
||||
t.Errorf("%s: %+v, want an untrusted number defaulting to %v, with a description and bounds", key, s, value)
|
||||
}
|
||||
}
|
||||
if p := m.Settings["font-size"].Predefined; p != "font-size" {
|
||||
if p := m.Config["font-size"].Predefined; p != "font-size" {
|
||||
t.Errorf("the font size falls back to %q, want the predefined font-size", p)
|
||||
}
|
||||
// Filled with the defaults, the file is the one every machine had before it took config values.
|
||||
c := templateText(t, m, "configuration")
|
||||
c := m.text(t, "configuration")
|
||||
c = strings.ReplaceAll(c, "${config:font-size}", "10")
|
||||
c = strings.ReplaceAll(c, "${config:width}", "250")
|
||||
for _, want := range []string{"font = Inter 10", "width = 250"} {
|
||||
@@ -182,15 +130,7 @@ func TestTheFontSizeAndWidthAreConfigValuesWithTodaysDefaults(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNothingStartsItButTheBus(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
m := readManifest(t)
|
||||
if m.Shell != nil {
|
||||
t.Fatalf("a session start: %+v", m.Shell)
|
||||
}
|
||||
@@ -207,50 +147,25 @@ func TestNothingStartsItButTheBus(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTheToolsAgreeWithTheManifest(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
m := readManifest(t)
|
||||
checkTheToolsAgree(t, m)
|
||||
checkNoSecretsOrInstallationNames(t)
|
||||
}
|
||||
|
||||
// No file resource carries its text inline (novox/hq ADR 0349 §2).
|
||||
func TestEveryFileTakesItsTextFromATemplate(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
everyFileIsATemplate(t, m)
|
||||
readManifest(t).everyFileIsATemplate(t)
|
||||
}
|
||||
|
||||
// dunst moves first (design 57 P6, issue 672): marked moved, its dunstrc is filled on the node by the provisioner's
|
||||
// client from its template, and reloaded as the file declares.
|
||||
func TestItIsHandedToTheProvisioner(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
m := readManifest(t)
|
||||
if !m.Moved {
|
||||
t.Fatal("dunst is not marked moved")
|
||||
}
|
||||
fromTemplate(t, m, "configuration", "files/dunstrc")
|
||||
if on := resource(t, m, "notifier")["reload-on"]; !reflect.DeepEqual(on, []any{"configuration"}) {
|
||||
m.fromTemplate(t, "configuration", "files/dunstrc")
|
||||
if on := m.resource(t, "notifier")["reload-on"]; !reflect.DeepEqual(on, []any{"configuration"}) {
|
||||
t.Fatalf("the notifier is not reloaded on its dunstrc: %v", on)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// The running dunst reads its file again when the file changes (novox/hq ADR 0262: a setting the
|
||||
@@ -85,16 +83,8 @@ func TestAReloadDunstRefusesIsAFailure(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTheRunningDunstIsReloadedWhenItsFileChanges(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
s := resource(t, m, "notifier")
|
||||
m := readManifest(t)
|
||||
s := m.resource(t, "notifier")
|
||||
want := map[string]any{
|
||||
"id": "notifier", "type": "service", "unit": "dunst.service",
|
||||
"scope": "user", "user": "${machine:account}",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "dunst",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"moved": true,
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "fail2ban",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"capabilities": [
|
||||
"firewall"
|
||||
],
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// feh's Go tools bundle (novox/hq ADR 0188, ADR 0193, ADR 0208): the wallpaper's tools, served by the
|
||||
// node's runtime as the operator account.
|
||||
// Design 47 H1, proof 1: this comment is the single-module change its pipeline delivers.
|
||||
package main
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,19 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in
|
||||
// every desktop module that carries it.
|
||||
|
||||
type manifest struct {
|
||||
Module string `json:"module"`
|
||||
Version string `json:"version"`
|
||||
Moved bool `json:"moved"`
|
||||
Items []item `json:"items"`
|
||||
Capabilities []string `json:"capabilities"`
|
||||
Requires []string `json:"requires"`
|
||||
Claims []claim `json:"claims"`
|
||||
Seats []any `json:"seats"`
|
||||
Tools []string `json:"tools"`
|
||||
Environment *environment `json:"environment"`
|
||||
Shell []shellCode `json:"shell"`
|
||||
Resources []map[string]any `json:"resources"`
|
||||
// Lines for other modules' seats (novox/hq ADR 0212): the window manager's, here.
|
||||
Contributions []any `json:"contributions"`
|
||||
Build struct {
|
||||
Artifacts []map[string]any `json:"artifacts"`
|
||||
} `json:"build"`
|
||||
}
|
||||
|
||||
type claim struct {
|
||||
Name string `json:"name"`
|
||||
Scope string `json:"scope"`
|
||||
Serves []string `json:"serves"`
|
||||
}
|
||||
|
||||
type environment struct {
|
||||
Variables map[string]string `json:"variables"`
|
||||
Path []map[string]any `json:"path"`
|
||||
}
|
||||
|
||||
type shellCode struct {
|
||||
For string `json:"for"`
|
||||
Slot string `json:"slot"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
// An item of a collection (novox/hq ADR 0349 clause 13): its text is the template file beside the manifest.
|
||||
type item struct {
|
||||
Collection string `json:"collection"`
|
||||
Name string `json:"name"`
|
||||
Attributes map[string]string `json:"attributes"`
|
||||
Template string `json:"template"`
|
||||
}
|
||||
|
||||
// shell is the code the module adds to a shell's or the session's start, read from the templates of its items of
|
||||
// the collections shell:<file>:<slot>, in the order they are declared.
|
||||
func shell(t *testing.T, m catalogue.Manifest) []catalogue.ShellCode {
|
||||
func (m manifest) shell(t *testing.T) []shellCode {
|
||||
t.Helper()
|
||||
var out []catalogue.ShellCode
|
||||
var out []shellCode
|
||||
for _, it := range m.Items {
|
||||
parts := strings.Split(it.Collection, ":")
|
||||
if len(parts) != 3 || parts[0] != "shell" {
|
||||
@@ -23,15 +70,15 @@ func shell(t *testing.T, m catalogue.Manifest) []catalogue.ShellCode {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out = append(out, catalogue.ShellCode{For: parts[1], Slot: parts[2], Code: string(text)})
|
||||
out = append(out, shellCode{For: parts[1], Slot: parts[2], Code: string(text)})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// environmentOf is the variables the module sets, read from the templates of its items of the environment.
|
||||
func environmentOf(t *testing.T, m catalogue.Manifest) *catalogue.Environment {
|
||||
func (m manifest) environmentOf(t *testing.T) *environment {
|
||||
t.Helper()
|
||||
var env *catalogue.Environment
|
||||
var env *environment
|
||||
for _, it := range m.Items {
|
||||
if it.Collection != "environment" || it.Attributes["kind"] != "variable" {
|
||||
continue
|
||||
@@ -41,14 +88,30 @@ func environmentOf(t *testing.T, m catalogue.Manifest) *catalogue.Environment {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if env == nil {
|
||||
env = &catalogue.Environment{Variables: map[string]string{}}
|
||||
env = &environment{Variables: map[string]string{}}
|
||||
}
|
||||
env.Variables[it.Attributes["name"]] = strings.TrimSuffix(string(text), "\n")
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any {
|
||||
func readManifest(t *testing.T) manifest {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dec := json.NewDecoder(strings.NewReader(string(raw)))
|
||||
dec.DisallowUnknownFields()
|
||||
var m manifest
|
||||
if err := dec.Decode(&m); err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
m.Environment = m.environmentOf(t)
|
||||
return m
|
||||
}
|
||||
|
||||
func (m manifest) resource(t *testing.T, id string) map[string]any {
|
||||
t.Helper()
|
||||
for _, r := range m.Resources {
|
||||
if r["id"] == id {
|
||||
@@ -59,7 +122,7 @@ func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any {
|
||||
return nil
|
||||
}
|
||||
|
||||
func packages(m catalogue.Manifest) (present, absent []string) {
|
||||
func (m manifest) packages() (present, absent []string) {
|
||||
for _, r := range m.Resources {
|
||||
if r["type"] == "package" {
|
||||
if r["absent"] == true {
|
||||
@@ -75,9 +138,9 @@ func packages(m catalogue.Manifest) (present, absent []string) {
|
||||
// fromTemplate checks that a file resource takes its text from its template file in the module's
|
||||
// repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the
|
||||
// template is there. The readable file in the repository is the one place the machine's text is written.
|
||||
func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) {
|
||||
func (m manifest) fromTemplate(t *testing.T, id, template string) {
|
||||
t.Helper()
|
||||
r := resource(t, m, id)
|
||||
r := m.resource(t, id)
|
||||
if r["type"] != "file" {
|
||||
t.Fatalf("%s is a %v, not a file", id, r["type"])
|
||||
}
|
||||
@@ -97,7 +160,7 @@ func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) {
|
||||
|
||||
// everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs
|
||||
// files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2).
|
||||
func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) {
|
||||
func (m manifest) everyFileIsATemplate(t *testing.T) {
|
||||
t.Helper()
|
||||
for _, r := range m.Resources {
|
||||
if _, inline := r["content"]; inline {
|
||||
@@ -106,10 +169,10 @@ func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) {
|
||||
}
|
||||
packs := 0
|
||||
for _, a := range m.Build.Artifacts {
|
||||
if a.Kind == "archive" && a.From == "files" {
|
||||
if a["kind"] == "archive" && a["from"] == "files" {
|
||||
packs++
|
||||
if a.Name != "files" {
|
||||
t.Errorf("the templates archive is named %v, not files", a.Name)
|
||||
if a["name"] != "files" {
|
||||
t.Errorf("the templates archive is named %v, not files", a["name"])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,9 +182,9 @@ func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) {
|
||||
}
|
||||
|
||||
// text is the text a file resource places on the machine: its template file's.
|
||||
func templateText(t *testing.T, m catalogue.Manifest, id string) string {
|
||||
func (m manifest) text(t *testing.T, id string) string {
|
||||
t.Helper()
|
||||
name, _ := resource(t, m, id)["template"].(string)
|
||||
name, _ := m.resource(t, id)["template"].(string)
|
||||
if name == "" {
|
||||
t.Fatalf("resource %s names no template", id)
|
||||
}
|
||||
@@ -134,7 +197,7 @@ func templateText(t *testing.T, m catalogue.Manifest, id string) string {
|
||||
|
||||
// checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle
|
||||
// serves each seat verb the claims promise as <seat>.<verb>, and that the Go bundle is declared.
|
||||
func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) {
|
||||
func checkTheToolsAgree(t *testing.T, m manifest) {
|
||||
t.Helper()
|
||||
own, seat := map[string]bool{}, map[string]bool{}
|
||||
for _, tool := range tools() {
|
||||
@@ -176,14 +239,14 @@ func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) {
|
||||
t.Errorf("the bundle serves %s, which no claim promises", name)
|
||||
}
|
||||
}
|
||||
var bundle *catalogue.Artifact
|
||||
var bundle map[string]any
|
||||
for _, a := range m.Build.Artifacts {
|
||||
if a.Kind == "bundle" {
|
||||
bundle = &a
|
||||
if a["kind"] == "bundle" {
|
||||
bundle = a
|
||||
}
|
||||
}
|
||||
if bundle == nil || bundle.Language != "go" || bundle.System != "arch" ||
|
||||
bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" {
|
||||
if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" ||
|
||||
bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" {
|
||||
t.Errorf("the Go tools bundle: %v", bundle)
|
||||
}
|
||||
}
|
||||
@@ -208,7 +271,7 @@ const displaySession = "contribution:node-display-session:config"
|
||||
|
||||
// i3Lines is what the module contributes to the window manager: the text of its item of the collection
|
||||
// contribution:node-display-session:config (design 57 L3, issue 848).
|
||||
func i3Lines(m catalogue.Manifest) string {
|
||||
func (m manifest) i3Lines() string {
|
||||
var out string
|
||||
for _, it := range m.Items {
|
||||
if it.Collection == displaySession {
|
||||
@@ -224,7 +287,7 @@ func i3Lines(m catalogue.Manifest) string {
|
||||
|
||||
// i3LinesAreSource checks the window-manager lines are one item whose template is the source file, and that no
|
||||
// old-form contribution to node-display-session is left.
|
||||
func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) {
|
||||
func (m manifest) i3LinesAreSource(t *testing.T, source string) {
|
||||
t.Helper()
|
||||
if len(m.Contributions) != 0 {
|
||||
t.Fatalf("the module still holds contributions: the lines are an item of %s", displaySession)
|
||||
@@ -238,7 +301,7 @@ func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) {
|
||||
if len(templates) != 1 || templates[0] != source {
|
||||
t.Fatalf("the items of %s are %v, want the one template %s", displaySession, templates, source)
|
||||
}
|
||||
if i3Lines(m) == "" {
|
||||
if m.i3Lines() == "" {
|
||||
t.Fatalf("the template %s holds no line", source)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// feh's shape (novox/hq ADR 0208): no seat; it requires the X display on its own machine, carries
|
||||
@@ -15,43 +13,27 @@ import (
|
||||
// the session's start.
|
||||
|
||||
func TestItRequiresTheXDisplayAndClaimsNothing(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
if m.Module != "feh" || m.DefinesSeats != nil || m.Claims != nil {
|
||||
t.Fatalf("module %q, seats %v, claims %v", m.Module, m.DefinesSeats, m.Claims)
|
||||
m := readManifest(t)
|
||||
if m.Module != "feh" || m.Seats != nil || m.Claims != nil {
|
||||
t.Fatalf("module %q, seats %v, claims %v", m.Module, m.Seats, m.Claims)
|
||||
}
|
||||
if !reflect.DeepEqual(m.Requires, []string{"x11-display"}) {
|
||||
t.Fatalf("requires: %v", m.Requires)
|
||||
}
|
||||
if present, absent := packages(m); !reflect.DeepEqual(present, []string{"feh"}) || absent != nil {
|
||||
if present, absent := m.packages(); !reflect.DeepEqual(present, []string{"feh"}) || absent != nil {
|
||||
t.Fatalf("packages: %v, absent %v", present, absent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheWallpaperIsTheModulesOwnArchive(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
a := resource(t, m, "wallpapers")
|
||||
m := readManifest(t)
|
||||
a := m.resource(t, "wallpapers")
|
||||
if a["type"] != "archive" || a["artifact"] != "wallpapers" || a["path"] != "${machine:account-home}/.local/share/feh/wallpapers" || a["owner"] != "${machine:account}" {
|
||||
t.Fatalf("%v", a)
|
||||
}
|
||||
found := false
|
||||
for _, art := range m.Build.Artifacts {
|
||||
if art.Name == "wallpapers" && art.Kind == "archive" && art.From == "wallpaper" {
|
||||
if art["name"] == "wallpapers" && art["kind"] == "archive" && art["from"] == "wallpaper" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
@@ -65,71 +47,38 @@ func TestTheWallpaperIsTheModulesOwnArchive(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFehbgIsOwnedAndTheSessionStartRunsItOnce(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
fromTemplate(t, m, "fehbg", "files/fehbg")
|
||||
f := resource(t, m, "fehbg")
|
||||
m := readManifest(t)
|
||||
m.fromTemplate(t, "fehbg", "files/fehbg")
|
||||
f := m.resource(t, "fehbg")
|
||||
if f["path"] != "${machine:account-home}/.fehbg" || f["mode"] != "0755" {
|
||||
t.Fatalf("%v", f)
|
||||
}
|
||||
if c := templateText(t, m, "fehbg"); !strings.Contains(c, "$HOME/.local/share/feh/wallpapers/default.jpg") || strings.Contains(c, ".hal") {
|
||||
if c := m.text(t, "fehbg"); !strings.Contains(c, "$HOME/.local/share/feh/wallpapers/default.jpg") || strings.Contains(c, ".hal") {
|
||||
t.Fatalf("%s", c)
|
||||
}
|
||||
if len(shell(t, m)) != 1 || shell(t, m)[0].For != "xinitrc" || shell(t, m)[0].Slot != "normal" || strings.Count(shell(t, m)[0].Code, `"$HOME/.fehbg"`) != 1 {
|
||||
t.Fatalf("%+v", shell(t, m))
|
||||
if len(m.shell(t)) != 1 || m.shell(t)[0].For != "xinitrc" || m.shell(t)[0].Slot != "normal" || strings.Count(m.shell(t)[0].Code, `"$HOME/.fehbg"`) != 1 {
|
||||
t.Fatalf("%+v", m.shell(t))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheKeyThatRestoresTheWallpaperIsAnI3DropIn(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
i3LinesAreSource(t, m, "files/i3/50-feh.conf")
|
||||
if p := i3Lines(m); p == "" {
|
||||
m := readManifest(t)
|
||||
m.i3LinesAreSource(t, "files/i3/50-feh.conf")
|
||||
if p := m.i3Lines(); p == "" {
|
||||
t.Fatalf("path: %v", p)
|
||||
}
|
||||
if c := i3Lines(m); !strings.Contains(c, "bindsym $mod+Shift+b exec --no-startup-id ~/.fehbg\n") {
|
||||
if c := m.i3Lines(); !strings.Contains(c, "bindsym $mod+Shift+b exec --no-startup-id ~/.fehbg\n") {
|
||||
t.Fatalf("%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheToolsAgreeWithTheManifest(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
m := readManifest(t)
|
||||
checkTheToolsAgree(t, m)
|
||||
checkNoSecretsOrInstallationNames(t)
|
||||
}
|
||||
|
||||
// No file resource carries its text inline (novox/hq ADR 0349 §2).
|
||||
func TestEveryFileTakesItsTextFromATemplate(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
everyFileIsATemplate(t, m)
|
||||
readManifest(t).everyFileIsATemplate(t)
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
# The wallpaper (module feh, novox/hq ADR 0208). Owned by the mesh: replaced at every delivery. The
|
||||
# session's start runs it, and so may anything that wants the declared wallpaper back. The image is
|
||||
# the module's own, in ~/.local/share/feh/wallpapers. feh_set changes the wallpaper for a session
|
||||
# without touching this file (design 47 H1, proof 1 (second run): delivered by its pipeline, 2026-11-21).
|
||||
# without touching this file (the second single-module proof, 2026-11-17).
|
||||
feh --no-fehbg --bg-fill "$HOME/.local/share/feh/wallpapers/default.jpg"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "feh",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"moved": true,
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "flatpak",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "fonts",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// forticlient's shape (novox/hq ADR 0205, ADR 0207, ADR 0208): no package (the client is outside the
|
||||
@@ -16,17 +15,48 @@ import (
|
||||
// start), nothing of the VPN's configuration, and the Go bundle serving exactly the listed forticlient_
|
||||
// tools.
|
||||
|
||||
func TestTheToolsAgreeWithTheManifest(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
type manifest struct {
|
||||
Module string `json:"module"`
|
||||
Version string `json:"version"`
|
||||
Upgrade struct {
|
||||
Policy string `json:"policy"`
|
||||
Why string `json:"why"`
|
||||
} `json:"upgrade"`
|
||||
Capabilities []string `json:"capabilities"`
|
||||
Requires []string `json:"requires"`
|
||||
Tools []string `json:"tools"`
|
||||
Resources []map[string]any `json:"resources"`
|
||||
Claims []any `json:"claims"`
|
||||
Seats []any `json:"seats"`
|
||||
Shell []any `json:"shell"`
|
||||
Contributions []struct {
|
||||
Seat string `json:"seat"`
|
||||
Kind string `json:"kind"`
|
||||
Content string `json:"content"`
|
||||
} `json:"contributions"`
|
||||
Environment any `json:"environment"`
|
||||
Build struct {
|
||||
Artifacts []map[string]any `json:"artifacts"`
|
||||
} `json:"build"`
|
||||
}
|
||||
|
||||
func readManifest(t *testing.T) (manifest, string) {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
dec := json.NewDecoder(strings.NewReader(string(raw)))
|
||||
dec.DisallowUnknownFields()
|
||||
var m manifest
|
||||
if err := dec.Decode(&m); err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
raw := string(manifestJSON)
|
||||
return m, string(raw)
|
||||
}
|
||||
|
||||
func TestTheToolsAgreeWithTheManifest(t *testing.T) {
|
||||
m, raw := readManifest(t)
|
||||
served := map[string]bool{}
|
||||
for _, tool := range tools() {
|
||||
served[tool.Name] = true
|
||||
@@ -47,8 +77,8 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) {
|
||||
t.Fatalf("%v", m.Build.Artifacts)
|
||||
}
|
||||
b := m.Build.Artifacts[0]
|
||||
if b.Kind != "bundle" || b.Language != "go" || b.System != "arch" ||
|
||||
b.From != "cmd/forticlient-tools" || b.Binary != "forticlient-tools" {
|
||||
if b["kind"] != "bundle" || b["language"] != "go" || b["system"] != "arch" ||
|
||||
b["from"] != "cmd/forticlient-tools" || b["binary"] != "forticlient-tools" {
|
||||
t.Errorf("the Go tools bundle: %v", b)
|
||||
}
|
||||
s := strings.ToLower(raw)
|
||||
@@ -60,16 +90,7 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestItDeclaresTheServiceAndNothingOfTheConfiguration(t *testing.T) {
|
||||
manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside.
|
||||
m, err := catalogue.Decode(manifestJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("module.json: %v", err)
|
||||
}
|
||||
raw := string(manifestJSON)
|
||||
m, raw := readManifest(t)
|
||||
// split-dns (novox/hq ADR 0247): the machine's own resolver, which its adapter hands the client's
|
||||
// resolver file to. Required at the machine's reach, so the module is assigned only beside one.
|
||||
if m.Module != "forticlient" || !reflect.DeepEqual(m.Requires, []string{"x11-display", "split-dns"}) ||
|
||||
@@ -89,7 +110,7 @@ func TestItDeclaresTheServiceAndNothingOfTheConfiguration(t *testing.T) {
|
||||
if m.Upgrade.Policy != "record" || m.Upgrade.Why == "" {
|
||||
t.Error("a build of what routes the person's work names rolls out on its own")
|
||||
}
|
||||
if m.Claims != nil || m.DefinesSeats != nil || m.Environment != nil || m.Shell != nil || m.Contributions != nil {
|
||||
if m.Claims != nil || m.Seats != nil || m.Environment != nil || m.Shell != nil || m.Contributions != nil {
|
||||
t.Fatal("no seat, no environment, and no second start")
|
||||
}
|
||||
for _, never := range []string{"/etc/forticlient", "/opt/forticlient", "autostart", "\"package\"", "vpn.", "gateway", "profile"} {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"module": "forticlient",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"upgrade": {
|
||||
"policy": "record",
|
||||
"why": "its adapter routes the company's domains on the machine a person works on: a build that breaks it cuts the person off from work names until a person pushes the next (hq ADR 0236, ADR 0247)"
|
||||
|
||||
@@ -100,8 +100,7 @@ search that reaches the call's deadline stops there and answers what it found, w
|
||||
|
||||
It reaches the forge through the same HTTP client as the package-registry bundle (`internal/forgehttp`), as the
|
||||
tools' own account (novox/hq ADR 0365 §2, issue 469): the token the TypeScript bundle mints for `mesh-tools` and
|
||||
keeps in the vault as the module's own (`module/gitea/mesh-tools`, design 47 H2 §808), read through the vault seat's
|
||||
`get` at start and at each `secret-changed` naming it, never the admin login. One tool
|
||||
keeps (`MESH_GITEA_TOKEN_FILE`), sent only when its scopes line names that account, never the admin login. One tool
|
||||
call takes at most 50 seconds.
|
||||
`go test ./cmd/forge` runs it against a fake forge: the files of one commit created, updated and deleted on
|
||||
a new branch from its base; an existing branch committed on; the default branch, a protected branch and
|
||||
@@ -248,10 +247,9 @@ version already gone counts as deleted.
|
||||
|
||||
Over the forge's HTTP interface on the machine (`MESH_GITEA_URL`), never as the admin account (design 51: each
|
||||
holder keeps its write side as an own secret beside a read-only tool token). It **reads** as the tools' account,
|
||||
with the token the TypeScript bundle keeps for it in the vault (`module/gitea/mesh-tools`, scopes including
|
||||
`read:package`), and **writes** (a publish, a remove, retention's deletion) as the packages account `mesh-packages`
|
||||
(`MESH_GITEA_PACKAGES_USER`), with its own token (`module/gitea/mesh-packages`, the one scope `write:package`), both
|
||||
read through the vault seat's `get` at start and at each `secret-changed` (design 47 H2 §808).
|
||||
with the token the TypeScript bundle keeps for it (`MESH_GITEA_TOKEN_FILE`, scopes including `read:package`), and
|
||||
**writes** (a publish, a remove, retention's deletion) as the packages account `mesh-packages`
|
||||
(`MESH_GITEA_PACKAGES_USER`), with its own token (`MESH_GITEA_PACKAGES_TOKEN_FILE`, the one scope `write:package`).
|
||||
That account is plain, a member of the team `mesh-packages` that writes the organisation's packages and nothing else;
|
||||
the watcher makes it with the administrator's login and mints and keeps its token at its start (`packages-account.ts`).
|
||||
No timer looks at it after that: when the forge refuses a write (401) this bundle raises the finding
|
||||
|
||||
@@ -1,113 +0,0 @@
|
||||
// **The call is written before its first merge** (novox/hq design 47 H §766; H-A-18, H-A-47, H-A-71): before it merges
|
||||
// anything, the forge module writes the call's key `call.<call>` in its own template state `calls` (the seat git's
|
||||
// definition declares it): `by`, the bus account the tool runner proved, `at`, the members, each with its base `branch`
|
||||
// and `state` judged, merged with its commit, or refused with why, and `said`, whether its `merge-called` was said. Each
|
||||
// member is updated as it merges. A call is kept `call-retention` (a setting, default 30 days), then deleted by the forge
|
||||
// module on each new call's write, oldest first, with no timer (H-A-47).
|
||||
|
||||
import { readFileSync } from "node:fs";
|
||||
import type { State } from "@novox/mesh-sdk/state";
|
||||
import {
|
||||
callsByCallCallKey, mergeCalledSubject, parseCallsByCallCallKey, type Call, type CallMember, type MergeCalledEvent,
|
||||
} from "@novox/mesh-delivery-contracts-git";
|
||||
|
||||
/** call-retention's default, in days (§766). */
|
||||
export const CALL_RETENTION_DAYS = 30;
|
||||
|
||||
/** call-retention as the module's config file says it, in days; the default when it says none (mainrules.ts' form). */
|
||||
export function callRetentionDays(env: NodeJS.ProcessEnv = process.env): number {
|
||||
const file = env.MESH_GITEA_CONFIG_FILE;
|
||||
if (!file) return CALL_RETENTION_DAYS;
|
||||
try {
|
||||
const v = Number((JSON.parse(readFileSync(file, "utf8")) as Record<string, unknown>)["call-retention"]);
|
||||
return Number.isInteger(v) && v >= 1 ? v : CALL_RETENTION_DAYS;
|
||||
} catch { return CALL_RETENTION_DAYS; }
|
||||
}
|
||||
|
||||
/** One kept call with its id. */
|
||||
export interface KeptCall extends Call {
|
||||
call: string;
|
||||
}
|
||||
|
||||
/** The state `calls`: one key per merge call. */
|
||||
export class Calls {
|
||||
constructor(private readonly st: State<unknown>, private readonly retentionDays: () => number = () => CALL_RETENTION_DAYS,
|
||||
private readonly log: (line: string) => void = () => {}) {}
|
||||
|
||||
/** Every call kept, newest first. */
|
||||
async all(): Promise<KeptCall[]> {
|
||||
const out: KeptCall[] = [];
|
||||
for (const key of await this.st.keys()) {
|
||||
const parsed = parseCallsByCallCallKey(key);
|
||||
if (!parsed) continue;
|
||||
const e = await this.st.get(key);
|
||||
if (e?.value) out.push({ ...(e.value as Call), call: parsed.call });
|
||||
}
|
||||
return out.sort((a, b) => (a.at < b.at ? 1 : a.at > b.at ? -1 : a.call < b.call ? 1 : -1));
|
||||
}
|
||||
|
||||
/** One call, or undefined. */
|
||||
async get(call: string): Promise<KeptCall | undefined> {
|
||||
const e = await this.st.get(callsByCallCallKey(call));
|
||||
return e?.value ? { ...(e.value as Call), call } : undefined;
|
||||
}
|
||||
|
||||
/** Writes a call. A new call (none kept under its key) first deletes the calls past call-retention, oldest first. */
|
||||
async put(call: string, value: Call, now: Date = new Date()): Promise<void> {
|
||||
if (!(await this.st.get(callsByCallCallKey(call)))?.value) await this.expire(now);
|
||||
await this.st.put(callsByCallCallKey(call), value);
|
||||
}
|
||||
|
||||
/** Deletes the calls whose `at` is past call-retention before now, oldest first; answers the calls deleted. */
|
||||
async expire(now: Date): Promise<string[]> {
|
||||
const before = now.getTime() - this.retentionDays() * 86_400_000;
|
||||
const old = (await this.all()).filter((c) => (Date.parse(c.at) || 0) < before).reverse();
|
||||
for (const c of old) await this.st.delete(callsByCallCallKey(c.call));
|
||||
if (old.length) this.log(`${old.length} merge calls past ${this.retentionDays()} days deleted: ${old.map((c) => c.call).join(", ")}`);
|
||||
return old.map((c) => c.call);
|
||||
}
|
||||
}
|
||||
|
||||
/** A kept member of a call. */
|
||||
export const keptMember = (m: { repository: string; number: number; branch: string; head: string; state: CallMember["state"];
|
||||
commit?: string; why?: string; note?: string }): CallMember => ({ repository: m.repository, number: m.number, branch: m.branch,
|
||||
head: m.head, state: m.state, ...(m.commit ? { commit: m.commit } : {}), ...(m.why ? { why: m.why } : {}),
|
||||
...(m.note ? { note: m.note } : {}) });
|
||||
|
||||
/** Says a seat event on its subject, under an id the bus keeps once. */
|
||||
export type Say = (subject: string, body: unknown, id?: string) => Promise<void>;
|
||||
|
||||
/** The merge-called of a kept call (design 47 H §736, §767): its landed members, each with its base and merge commit, and its
|
||||
* refused ones; undefined for a call that landed no member, which says none. A member merged plainly, carrying the note
|
||||
* no delivery (its repository has no watched branch), starts no delivery and is left out, by the kept note (H2 H-A-471). */
|
||||
export function mergeCalledOf(c: KeptCall): MergeCalledEvent | undefined {
|
||||
const landed = c.members.filter((m) => m.state === "merged" && m.commit && !m.note);
|
||||
if (landed.length === 0) return undefined;
|
||||
// A member judged and not merged (a stop mid-call) is in neither: its head is an untaken head (§740).
|
||||
const refused = c.members.filter((m) => m.state === "refused");
|
||||
return { call: c.call, by: c.by, account: c.account, at: c.at,
|
||||
members: landed.map((m) => ({ repository: m.repository, number: m.number, branch: m.branch, head: m.head, commit: m.commit! })),
|
||||
...(refused.length ? { refused: refused.map((m) => ({ repository: m.repository, number: m.number, why: m.why ?? "not merged" })) } : {}) };
|
||||
}
|
||||
|
||||
/** Says a call's merge-called, when it landed a member; answers whether it said one. */
|
||||
export async function sayCall(c: KeptCall, say: Say): Promise<boolean> {
|
||||
const body = mergeCalledOf(c);
|
||||
if (!body) return false;
|
||||
await say(mergeCalledSubject(c.call), body, `merge-called.${c.call}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** At the start (§766): every call whose event is not said says merge-called for what landed, and is kept said, so a stop
|
||||
* between the last merge and the event loses nothing it recorded. Answers the calls said. */
|
||||
export async function sayUnsaid(calls: Calls, say: Say, log: (line: string) => void = () => {}): Promise<string[]> {
|
||||
const said: string[] = [];
|
||||
for (const c of await calls.all()) {
|
||||
if (c.said) continue;
|
||||
if (await sayCall(c, say)) said.push(c.call);
|
||||
const { call, ...value } = c;
|
||||
await calls.put(call, { ...value, said: true });
|
||||
}
|
||||
if (said.length) log(`merge-called said again at the start for ${said.join(", ")}`);
|
||||
return said;
|
||||
}
|
||||
+3
-43
@@ -182,19 +182,7 @@ export const FORGE_REQUEST_TIMEOUT_MS = 20_000;
|
||||
/** The most a tool's answer carries of one file: the bus's message bound is 1 MiB and a file travels as text (issue 1577). */
|
||||
export const MAX_FILE_BYTES = 512 * 1024;
|
||||
|
||||
/** A directory of a listing's `files` the commit does not hold: the seat git answers it with the kind not-found, the error
|
||||
* naming the directory (seat.ts failure; novox/hq design 47 H H-A-232). */
|
||||
export class DirectoryNotInTree extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "DirectoryNotInTree";
|
||||
}
|
||||
}
|
||||
|
||||
/** One request to the forge, cut at the limit and failing loud naming the request. */
|
||||
/** What a forge's answer says when its store does not answer (design 47 H2 §776). */
|
||||
const STORE_UNAVAILABLE = /database|\bdb\b|store/i;
|
||||
|
||||
async function timedFetch(url: string, init: RequestInit, label: string, timeoutMs: number): Promise<Response> {
|
||||
try {
|
||||
return await fetch(url, { ...init, signal: init.signal ? AbortSignal.any([init.signal, AbortSignal.timeout(timeoutMs)]) : AbortSignal.timeout(timeoutMs) });
|
||||
@@ -279,28 +267,6 @@ export class GiteaClient {
|
||||
return this.request<T>(path, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* The forge's own version read, unauthenticated (GET /api/v1/version): whether the forge answers at all, and whether
|
||||
* it names its store unavailable (design 47 H2 §776). What the seat git reads before it says a 401, a 502, a 503 or a
|
||||
* connection fault is the forge's refusal or the forge away; never throws.
|
||||
*/
|
||||
async forgeVersion(): Promise<{ ok: true; version: string } | { ok: false; words: string; store: boolean }> {
|
||||
const label = "GET /version";
|
||||
try {
|
||||
const res = await timedFetch(`${this.baseUrl}/api/v1/version`, { headers: { Accept: "application/json" } }, label, this.requestTimeoutMs);
|
||||
const text = await res.text();
|
||||
const store = STORE_UNAVAILABLE.test(text);
|
||||
if (!res.ok) return { ok: false, words: `the forge's version read answered ${res.status} ${text}`.trim(), store };
|
||||
let version = "";
|
||||
try { version = String(JSON.parse(text)?.version ?? ""); } catch { /* no version said */ }
|
||||
if (store) return { ok: false, words: `the forge's version read names its store unavailable: ${text}`, store };
|
||||
if (!version) return { ok: false, words: `the forge's version read named no version: ${text}`, store };
|
||||
return { ok: true, version };
|
||||
} catch (err) {
|
||||
return { ok: false, words: `the forge's version read: ${err instanceof Error ? err.message : String(err)}`, store: false };
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Repositories ----
|
||||
|
||||
/** Every repository this token can see, one page. `/user/repos` is only what the token's own
|
||||
@@ -524,17 +490,11 @@ export class GiteaClient {
|
||||
}
|
||||
|
||||
/** One page of a repository's tags, newest first, by name: what the seat git's `tags` answers. */
|
||||
async tags(owner: string, repo: string, page: number, limit: number): Promise<{ tag: string; commit: string }[]> {
|
||||
async tags(owner: string, repo: string, page: number, limit: number): Promise<string[]> {
|
||||
if (!Number.isInteger(page) || page < 1) throw new Error(`${page} is not a page, from 1`);
|
||||
const list = await this.request<any[]>(`${repoPath(owner, repo)}/tags?page=${page}&limit=${limit}`);
|
||||
if (!Array.isArray(list)) throw new Error(`the forge answered no list of the tags of ${owner}/${repo}`);
|
||||
// Each tag with the commit it names (design 47 H2 §796): a tag the forge names no commit for is no answer, never a
|
||||
// tag without its commit.
|
||||
return list.filter((t) => String(t?.name ?? "") !== "").map((t) => {
|
||||
const tag = String(t.name), commit = String(t?.commit?.sha ?? "");
|
||||
if (!/^[0-9a-f]{40}$/.test(commit)) throw new Error(`the forge named no commit for ${owner}/${repo}'s tag ${tag}`);
|
||||
return { tag, commit };
|
||||
});
|
||||
return list.map((t) => String(t?.name ?? "")).filter((n) => n !== "");
|
||||
}
|
||||
|
||||
/** The commit a tag names, or null when the repository has no such tag (the forge's 404 only). */
|
||||
@@ -674,7 +634,7 @@ export class GiteaClient {
|
||||
// is whole (a cut one failed above), so a directory absent from it is absent at the commit.
|
||||
filesOf = {};
|
||||
for (const dir of files) {
|
||||
if (dir !== "" && dirOf.get(dir) === undefined) throw new DirectoryNotInTree(`the directory ${dir} is not in ${owner}/${repo} at ${sha}`);
|
||||
if (dir !== "" && dirOf.get(dir) === undefined) throw new Error(`the directory ${dir} is not in ${owner}/${repo} at ${sha}`);
|
||||
const prefix = dir === "" ? "" : `${dir}/`;
|
||||
const blobs: Record<string, string> = {};
|
||||
for (const e of entries) {
|
||||
|
||||
@@ -1,54 +1,35 @@
|
||||
package main
|
||||
|
||||
// The commit and the reads act as the tools' own account (novox/hq ADR 0365 §2, issue 469): the token the module
|
||||
// keeps for `mesh-tools` in the vault (design 47 H2 §808), never the admin login. That account is no repository's administrator, so the forge
|
||||
// keeps for `mesh-tools`, never the admin login. That account is no repository's administrator, so the forge
|
||||
// refuses it the protection rules (403, as gitea's reqAdmin does); the commit then judges an existing branch by the
|
||||
// protection the forge says it applies, and leaves a branch not made yet to the forge's own judgement.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/seal"
|
||||
|
||||
"gitea/internal/forgehttp"
|
||||
)
|
||||
|
||||
const toolsToken = "tools-token-never-shown"
|
||||
|
||||
// vaultHolding is the vault seat's get answering account's token sealed to the key it is asked with; "" holds none.
|
||||
func vaultHolding(t *testing.T, account, token string) *forgehttp.VaultToken {
|
||||
t.Helper()
|
||||
v, err := forgehttp.NewVaultToken(account, func(address string, args any) (json.RawMessage, error) {
|
||||
a := args.(map[string]any)
|
||||
if address != forgehttp.VaultGet || a["name"] != forgehttp.VaultName(account) {
|
||||
return nil, fmt.Errorf("asked %s for %v", address, a["name"])
|
||||
}
|
||||
if token == "" {
|
||||
return nil, fmt.Errorf("the vault holds no secret named %s", a["name"])
|
||||
}
|
||||
// The value the minter writes: the token and the scopes it was minted with (H-A-439).
|
||||
value, _ := json.Marshal(map[string]any{"token": token, "scopes": []string{"write:repository", "write:issue", "read:user", "read:package"}})
|
||||
box, err := seal.Seal(string(value), a["public_key"].(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(map[string]any{"name": a["name"], "generation": 1, "sealed": box})
|
||||
})
|
||||
if err != nil {
|
||||
// asTools serves the fake forge to a bundle holding the tools' token: only that token is taken, the rules are
|
||||
// refused as to a non-administrator, and the branches named protected say so as the forge does.
|
||||
func asTools(t *testing.T, f *fakeForge, protected map[string]string, scopes string) (*Forge, *[]string) {
|
||||
dir := t.TempDir()
|
||||
file := filepath.Join(dir, "token")
|
||||
if err := os.WriteFile(file, []byte(toolsToken+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(file+".scopes", []byte(scopes+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// asTools serves the fake forge to a bundle holding the token the vault hands: only the tools' token is taken, the
|
||||
// rules are refused as to a non-administrator, and the branches named protected say so as the forge does.
|
||||
func asTools(t *testing.T, f *fakeForge, protected map[string]string, held string) (*Forge, *[]string) {
|
||||
var seen []string
|
||||
const repo = "/api/v1/repos/acme/app"
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -71,12 +52,12 @@ func asTools(t *testing.T, f *fakeForge, protected map[string]string, held strin
|
||||
f.handle(w, r)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return &Forge{Client: forgehttp.Client{URL: srv.URL, Token: vaultHolding(t, "mesh-tools", held), Account: "mesh-tools"}}, &seen
|
||||
return &Forge{Client: forgehttp.Client{URL: srv.URL, TokenFile: file, Account: "mesh-tools"}}, &seen
|
||||
}
|
||||
|
||||
func TestTheToolsAccountCommitsOnANewBranchWithItsTokenThoughTheRulesAreNotItsToRead(t *testing.T) {
|
||||
f := newFake(t)
|
||||
forge, _ := asTools(t, f, nil, toolsToken)
|
||||
forge, _ := asTools(t, f, nil, "read:user,write:issue,write:repository for mesh-tools")
|
||||
got, err := commit(context.Background(), forge, ask(t, map[string]any{"branch": "fix/365",
|
||||
"files": filesArg(map[string]any{"path": "x", "content": "x"})}))
|
||||
if err != nil {
|
||||
@@ -94,7 +75,7 @@ func TestTheToolsAccountIsRefusedAProtectedBranchTheForgeSaysItGuardsAndTheDefau
|
||||
if rule != "" {
|
||||
protected[branch] = rule
|
||||
}
|
||||
forge, _ := asTools(t, f, protected, toolsToken)
|
||||
forge, _ := asTools(t, f, protected, "read:user,write:issue,write:repository for mesh-tools")
|
||||
_, err := commit(context.Background(), forge, ask(t, map[string]any{"branch": branch,
|
||||
"files": filesArg(map[string]any{"path": "x", "content": "x"})}))
|
||||
if err == nil || !strings.Contains(err.Error(), "refused") || !strings.Contains(err.Error(), "ADR 0266") {
|
||||
@@ -109,13 +90,13 @@ func TestTheToolsAccountIsRefusedAProtectedBranchTheForgeSaysItGuardsAndTheDefau
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoTokenIsSentWhileTheVaultHoldsNoneForTheAccount(t *testing.T) {
|
||||
func TestATokenKeptForTheAdministratorIsNeverSent(t *testing.T) {
|
||||
f := newFake(t)
|
||||
// Design 47 H2 §808: the token is read through the vault seat's get; while the module has minted none, nothing goes.
|
||||
forge, seen := asTools(t, f, nil, "")
|
||||
// What the module kept before ADR 0365: the admin's token, its scopes line naming no account.
|
||||
forge, seen := asTools(t, f, nil, "read:user,write:issue,write:repository")
|
||||
_, err := commit(context.Background(), forge, ask(t, map[string]any{"branch": "fix/365",
|
||||
"files": filesArg(map[string]any{"path": "x", "content": "x"})}))
|
||||
if err == nil || !strings.Contains(err.Error(), "not kept yet") {
|
||||
if err == nil || !strings.Contains(err.Error(), "not mesh-tools's") {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
if len(*seen) != 0 {
|
||||
@@ -123,8 +104,9 @@ func TestNoTokenIsSentWhileTheVaultHoldsNoneForTheAccount(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheBundleReadsItsTokenFromTheVaultAndNeedsNoAdminLogin(t *testing.T) {
|
||||
func TestTheBundleHandedATokenFileNeedsNoAdminLogin(t *testing.T) {
|
||||
t.Setenv("MESH_GITEA_URL", "http://127.0.0.1:3000")
|
||||
t.Setenv("MESH_GITEA_TOKEN_FILE", "/state/token")
|
||||
t.Setenv("MESH_GITEA_TOOLS_USER", "")
|
||||
t.Setenv("MESH_GITEA_ADMIN_USER", "")
|
||||
t.Setenv("MESH_GITEA_ADMIN_PASSWORD_FILE", "")
|
||||
@@ -132,7 +114,7 @@ func TestTheBundleReadsItsTokenFromTheVaultAndNeedsNoAdminLogin(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.Token == nil || f.Token.Account != "mesh-tools" || f.Account != "mesh-tools" || f.User != "" || f.PasswordFile != "" {
|
||||
if f.TokenFile != "/state/token" || f.Account != "mesh-tools" || f.User != "" || f.PasswordFile != "" {
|
||||
t.Fatalf("%+v", f.Client)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,10 +21,7 @@ type Forge struct {
|
||||
|
||||
// ForgeFromEnv is the forge as the manifest hands it to this bundle.
|
||||
func ForgeFromEnv() (*Forge, error) {
|
||||
c, missing, err := forgehttp.ToolsFromEnv()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, missing := forgehttp.FromEnv()
|
||||
if len(missing) > 0 {
|
||||
return nil, fmt.Errorf("the forge cannot be reached: %s not set by the manifest", strings.Join(missing, ", "))
|
||||
}
|
||||
|
||||
@@ -691,8 +691,8 @@ func TestToolsAreTheOnesTheReadmeNamesAndTheManifestBuilds(t *testing.T) {
|
||||
// The tools' own account, never the admin login (novox/hq ADR 0365 §2); the package registry's bundle also writes
|
||||
// as the packages account (design 51: the write side an own secret beside a read-only tool token).
|
||||
wants := map[string][]string{
|
||||
"cmd/forge": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER"},
|
||||
"cmd/package-registry": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER", "MESH_GITEA_PACKAGES_USER"},
|
||||
"cmd/forge": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER", "MESH_GITEA_TOKEN_FILE"},
|
||||
"cmd/package-registry": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER", "MESH_GITEA_TOKEN_FILE", "MESH_GITEA_PACKAGES_USER", "MESH_GITEA_PACKAGES_TOKEN_FILE"},
|
||||
}
|
||||
for _, a := range m.Build.Artifacts {
|
||||
want, ok := wants[a.From]
|
||||
@@ -713,12 +713,6 @@ func TestToolsAreTheOnesTheReadmeNamesAndTheManifestBuilds(t *testing.T) {
|
||||
t.Errorf("%s is given %s: it acts as an account of its own, not the administrator", a.From, k)
|
||||
}
|
||||
}
|
||||
// Design 47 H2 §808, H-A-422 (1): the token is read through the vault seat's get, never from a file.
|
||||
for _, k := range []string{"MESH_GITEA_TOKEN_FILE", "MESH_GITEA_PACKAGES_TOKEN_FILE"} {
|
||||
if a.Env[k] != "" {
|
||||
t.Errorf("%s is given %s: its token is read from the vault", a.From, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
for from := range wants {
|
||||
t.Errorf("the manifest builds no %s bundle", from)
|
||||
|
||||
@@ -14,8 +14,6 @@ import (
|
||||
"strings"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
|
||||
"gitea/internal/forgehttp"
|
||||
)
|
||||
|
||||
// ToolNames are the tools this bundle serves. The module's manifest lists no `tools` (see the package-registry
|
||||
@@ -23,16 +21,6 @@ import (
|
||||
var ToolNames = []string{"gitea_commit", "gitea_commits", "gitea_compare", "gitea_pull_reviews", "gitea_list_dir", "gitea_search_code"}
|
||||
|
||||
func main() {
|
||||
// The tools' account's token, read from the vault at start and at each secret-changed naming it (design 47 H2 §808).
|
||||
go func() {
|
||||
account := os.Getenv("MESH_GITEA_TOOLS_USER")
|
||||
if account == "" {
|
||||
account = forgehttp.ToolsAccount
|
||||
}
|
||||
if err := forgehttp.Follow(func(f string, a ...any) { fmt.Fprintf(os.Stderr, f+"\n", a...) }, account); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "the vault's changes of the forge token are not heard:", err)
|
||||
}
|
||||
}()
|
||||
if err := stdio.Serve("", Tools(ForgeFromEnv)); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
|
||||
@@ -78,14 +78,8 @@ func (f *Forge) As(typ string) *Forge {
|
||||
|
||||
// ForgeFromEnv is the forge as the manifest hands it to this bundle.
|
||||
func ForgeFromEnv() (*Forge, error) {
|
||||
c, missing, err := forgehttp.ToolsFromEnv()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w, writeMissing, err := forgehttp.WriterFromEnv()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, missing := forgehttp.FromEnv()
|
||||
w, writeMissing := forgehttp.WriterFromEnv()
|
||||
for _, m := range writeMissing {
|
||||
if m != "MESH_GITEA_URL" {
|
||||
missing = append(missing, m)
|
||||
@@ -174,7 +168,7 @@ func (f *Forge) DistTags(ctx context.Context, name string) (map[string]string, e
|
||||
// write is the registry's write side, refused when the manifest handed none.
|
||||
func (f *Forge) write() (*forgehttp.Client, error) {
|
||||
if f.Writer == nil {
|
||||
return nil, errors.New("the package registry's write side is not handed to this bundle (the packages account's token); nothing was written")
|
||||
return nil, errors.New("the package registry's write side is not handed to this bundle (MESH_GITEA_PACKAGES_TOKEN_FILE); nothing was written")
|
||||
}
|
||||
return f.Writer, nil
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user