| index 57cad7bb0..eea553adb 100644 |
| --- a/internal/edge/storedoor_test.go |
| +++ b/internal/edge/storedoor_test.go |
| @@ -1199,3 +1199,239 @@ func TestStoreDoorListsOnlyItsOwnPolicyETags(t *testing.T) { |
| t.Errorf("empty etag listing = %d %q, want 200 []", w.Code, w.Body.String()) |
| } |
| } |
| + |
| +// ---- managed readiness authority (#457 phase 2, plan 2b-1 Task 6) -------- |
| + |
| +// The console's readiness authority for a managed box: the object it alone |
| +// publishes, and whose presence lets the box run workloads. The shape is |
| +// internal/console's managedReadyKey; TestStoreDoorManagedReadyKeyMatchesConsole |
| +// pins the two together. |
| +func readyKey(fleet, label string) string { |
| + return "fleets/" + fleet + "/boxes/" + label + "/ready.json" |
| +} |
| + |
| +// TestStoreDoorManagedReadyACL: no fleet box may write or delete any box's |
| +// ready object in its own fleet, its own label included, while every read and |
| +// every other own-fleet write survives. |
| +func TestStoreDoorManagedReadyACL(t *testing.T) { |
| + own, sibling := readyKey(testFleet, "box-a"), readyKey(testFleet, "box-b") |
| + foreign := readyKey("other", "box-a") |
| + rows := []struct { |
| + key string |
| + op aclOp |
| + want bool |
| + why string |
| + }{ |
| + {own, opWrite, false, "a box never asserts its own readiness"}, |
| + {own, opDelete, false, "nor deletes the console's assertion"}, |
| + {sibling, opWrite, false, "nor a sibling's: aclWrite has no label and needs none"}, |
| + {sibling, opDelete, false, "nor deletes a sibling's"}, |
| + {own, opRead, true, "the box reads its own ready object"}, |
| + {sibling, opRead, true, "own-fleet reads are unchanged"}, |
| + {foreign, opRead, false, "another fleet's ready object"}, |
| + {foreign, opWrite, false, "is out of scope for writes"}, |
| + {foreign, opDelete, false, "and deletes"}, |
| + {ownFleetKey, opWrite, true, "own heartbeat still writes"}, |
| + {ownFleetKey, opDelete, true, "and deletes"}, |
| + {"fleets/pi/bindings/app.example.json", opWrite, true, "own bindings still write"}, |
| + {"fleets/pi/boxes/box-a/ready.jsonx", opWrite, true, "a near name is not the authority key"}, |
| + {"fleets/pi/boxes/box-a/ready.json/x", opWrite, true, "nor is a sixth segment"}, |
| + {"fleets/pi/boxes/ready.json", opWrite, true, "nor a key with no label"}, |
| + {"fleets/pi/boxes/box-a/status.json", opWrite, true, "the boxes subtree is not blanket-denied"}, |
| + {"fleets/pi/boxes/box-a/../box-a/ready.json", opWrite, false, "traversal is refused by canonicalKey"}, |
| + {"fleets/pi/boxes//ready.json", opWrite, false, "an empty label is refused by canonicalKey"}, |
| + } |
| + for _, row := range rows { |
| + if got := aclAllows(row.key, row.op); got != row.want { |
| + t.Errorf("acl %s %q = %v, want %v (%s)", row.op, row.key, got, row.want, row.why) |
| + } |
| + } |
| + for _, key := range []string{own, ownFleetKey, "fleets//boxes/box-a/ready.json"} { |
| + if aclWrite(testSqid, "", key) { |
| + t.Errorf("aclWrite with no fleet allowed %q", key) |
| + } |
| + if aclRead(testSqid, "", key) { |
| + t.Errorf("aclRead with no fleet allowed %q", key) |
| + } |
| + } |
| + matches := map[string]bool{ |
| + own: true, |
| + sibling: true, |
| + "fleets/pi/boxes/box-a/ready.jsonx": false, |
| + "fleets/pi/boxes/box-a/xready.json": false, |
| + "fleets/pi/boxes/box-a/ready.json/x": false, |
| + "fleets/pi/boxes/ready.json": false, |
| + "fleets/pi/box/box-a/ready.json": false, |
| + "fleets/pix/boxes/box-a/ready.json": false, |
| + "apps/pi/boxes/box-a/ready.json": false, |
| + "x/fleets/pi/boxes/box-a/ready.json": false, |
| + "fleets/pi/boxes/box-a/b/ready.json": false, |
| + "fleets/pi/status/boxes/box-a.json": false, |
| + "fleets/pi/boxes/box-a/Ready.json": false, |
| + "fleets/pi/boxes/box-a/ready.json.tmp": false, |
| + } |
| + for key, want := range matches { |
| + if got := managedReadyObjectKey(testFleet, key); got != want { |
| + t.Errorf("managedReadyObjectKey(%q, %q) = %v, want %v", testFleet, key, got, want) |
| + } |
| + } |
| +} |
| + |
| +// TestStoreDoorManagedReadyWire drives the denial through the door: every |
| +// way of spelling a PUT or DELETE of a ready object reaches the bucket zero |
| +// times and answers the door's one refusal, while the box still reads the |
| +// exact object and its normal writes round-trip. |
| +func TestStoreDoorManagedReadyWire(t *testing.T) { |
| + d, dir := newTestDoor(t) |
| + refusal := missingInScopeShape(t, d) |
| + ownBody, siblingBody := `{"label":"box-a"}`, `{"label":"box-b"}` |
| + ownTag := seedObject(t, dir, readyKey(testFleet, "box-a"), ownBody) |
| + siblingTag := seedObject(t, dir, readyKey(testFleet, "box-b"), siblingBody) |
| + seedObject(t, dir, readyKey("other", "box-a"), `{"label":"foreign"}`) |
| + counting := &doorOpCounter{Store: dir} |
| + d.store = counting |
| + |
| + targets := []string{ |
| + "/store/o/" + readyKey(testFleet, "box-a"), |
| + "/store/o/" + readyKey(testFleet, "box-b"), |
| + "/store/o/" + readyKey(testFleet, "box-c"), |
| + "/store/o/fleets/pi/boxes/box-a%2Fready.json", |
| + "/store/o/fleets%2Fpi%2Fboxes%2Fbox-a%2Fready.json", |
| + "/store/o/fleets/pi/boxes/box-a/ready%2Ejson", |
| + "/store/o/fleets/pi/boxes/box-a/%72%65ady.json", |
| + "/store/o/fleets/pi/boxes/box-a/ready.json?x=1", |
| + "/store/o/fleets/pi/boxes//box-a/ready.json", |
| + "/store/o/fleets/pi//boxes/box-a/ready.json", |
| + "/store/o/fleets/pi/boxes/box-a//ready.json", |
| + "/store/o//fleets/pi/boxes/box-a/ready.json", |
| + "/store/o/fleets/pi/boxes/box-a/ready.json/", |
| + "/store/o/fleets/pi/boxes/x/../box-a/ready.json", |
| + "/store/o/fleets/pi/boxes/x/%2e%2e/box-a/ready.json", |
| + "/store/o/fleets/pi/boxes/box-a/./ready.json", |
| + "/store/o/fleets/pi/boxes/box-a/%2E/ready.json", |
| + "/store/o/./fleets/pi/boxes/box-a/ready.json", |
| + "/store//o/fleets/pi/boxes/box-a/ready.json", |
| + "/store/o/fleets/other/boxes/box-a/ready.json", |
| + "/store/list?prefix=fleets/pi/boxes/box-a/ready.json", |
| + "/store/dirs?prefix=fleets/pi/boxes/box-a/", |
| + "/store/etags?prefix=fleets/pi/boxes/box-a/", |
| + } |
| + preconditions := []map[string]string{ |
| + {"If-None-Match": "*"}, |
| + {"If-Match": ownTag}, |
| + {"If-Match": siblingTag}, |
| + nil, |
| + } |
| + for _, target := range targets { |
| + for _, method := range []string{"PUT", "DELETE", "POST", "PATCH"} { |
| + for _, hdr := range preconditions { |
| + h := map[string]string{"X-HTTP-Method-Override": "PUT"} |
| + for k, v := range hdr { |
| + h[k] = v |
| + } |
| + counting.ops = 0 |
| + w := doorDo(d, method, target, `{"label":"forged"}`, h) |
| + if got := shapeOf(w); got != refusal { |
| + t.Errorf("%s %s %v = %+v, want the bare refusal %+v", method, target, hdr, got, refusal) |
| + } |
| + if counting.ops != 0 { |
| + t.Errorf("%s %s %v reached the bucket %d time(s), want 0", method, target, hdr, counting.ops) |
| + } |
| + } |
| + } |
| + } |
| + for key, want := range map[string]string{readyKey(testFleet, "box-a"): ownBody, readyKey(testFleet, "box-b"): siblingBody} { |
| + if body, _, err := dir.Get(context.Background(), key); err != nil || string(body) != want { |
| + t.Errorf("%s after refused writes = %q, %v; want %q", key, body, err, want) |
| + } |
| + } |
| + if _, _, err := dir.Get(context.Background(), readyKey(testFleet, "box-c")); !errors.Is(err, os.ErrNotExist) { |
| + t.Errorf("a refused create-only PUT planted %s: %v", readyKey(testFleet, "box-c"), err) |
| + } |
| + |
| + // Reads keep the exact object. |
| + for key, want := range map[string][2]string{ |
| + readyKey(testFleet, "box-a"): {ownBody, ownTag}, |
| + readyKey(testFleet, "box-b"): {siblingBody, siblingTag}, |
| + } { |
| + w := doorDo(d, "GET", "/store/o/"+key, "", nil) |
| + if w.Code != http.StatusOK || w.Body.String() != want[0] || w.Header().Get("ETag") != want[1] { |
| + t.Errorf("GET %s = %d %q etag %q, want 200 %q etag %q", key, w.Code, w.Body.String(), w.Header().Get("ETag"), want[0], want[1]) |
| + } |
| + } |
| + if got := shapeOf(doorDo(d, "GET", "/store/o/"+readyKey("other", "box-a"), "", nil)); got != refusal { |
| + t.Errorf("GET another fleet's ready object = %+v, want the bare refusal", got) |
| + } |
| + if got := shapeOf(doorDo(d, "GET", "/store/o/fleets/pi/boxes/box-a/../box-a/ready.json", "", nil)); got != refusal { |
| + t.Errorf("GET a traversal-shaped ready key = %+v, want the bare refusal", got) |
| + } |
| + |
| + // Normal own-fleet writes round-trip, the near name and the rest of the |
| + // boxes subtree included. |
| + for _, key := range []string{ownFleetKey, "fleets/pi/bindings/app.example.json", "fleets/pi/boxes/box-a/ready.jsonx", "fleets/pi/boxes/box-a/status.json"} { |
| + w := doorDo(d, "PUT", "/store/o/"+key, "one", map[string]string{"If-None-Match": "*"}) |
| + if w.Code != http.StatusOK { |
| + t.Fatalf("create %s = %d, want 200", key, w.Code) |
| + } |
| + w = doorDo(d, "PUT", "/store/o/"+key, "two", map[string]string{"If-Match": w.Header().Get("ETag")}) |
| + if w.Code != http.StatusOK { |
| + t.Fatalf("replace %s = %d, want 200", key, w.Code) |
| + } |
| + if w = doorDo(d, "GET", "/store/o/"+key, "", nil); w.Code != http.StatusOK || w.Body.String() != "two" { |
| + t.Errorf("GET %s = %d %q, want 200 \"two\"", key, w.Code, w.Body.String()) |
| + } |
| + if w = doorDo(d, "DELETE", "/store/o/"+key, "", nil); w.Code != http.StatusNoContent { |
| + t.Errorf("DELETE %s = %d, want 204", key, w.Code) |
| + } |
| + } |
| +} |
| + |
| +// TestStoreDoorManagedReadyConsole: the console publishes through its own |
| +// direct store handle, create-only, and nothing the box can send forges the |
| +// object first or replaces or deletes it afterwards. |
| +func TestStoreDoorManagedReadyConsole(t *testing.T) { |
| + d, console := newTestDoor(t) |
| + key := readyKey(testFleet, "box-a") |
| + path := "/store/o/" + key |
| + |
| + if w := doorDo(d, "PUT", path, `{"forged":true}`, map[string]string{"If-None-Match": "*"}); w.Code != http.StatusNotFound { |
| + t.Fatalf("box create-only PUT of its ready object = %d, want 404", w.Code) |
| + } |
| + body := `{"fleet":"pi","label":"box-a","ready_at":"2026-11-08T00:00:00Z","op":"op-1"}` |
| + etag, err := console.Put(context.Background(), key, []byte(body), "") |
| + if err != nil { |
| + t.Fatalf("console create-only publication: %v", err) |
| + } |
| + |
| + w := doorDo(d, "GET", path, "", nil) |
| + if w.Code != http.StatusOK || w.Body.String() != body || w.Header().Get("ETag") != etag { |
| + t.Fatalf("box GET = %d %q etag %q, want the console's object", w.Code, w.Body.String(), w.Header().Get("ETag")) |
| + } |
| + if w := doorDo(d, "PUT", path, `{"forged":true}`, map[string]string{"If-Match": etag}); w.Code != http.StatusNotFound { |
| + t.Errorf("box replace with the fresh etag = %d, want 404", w.Code) |
| + } |
| + if w := doorDo(d, "DELETE", path, "", nil); w.Code != http.StatusNotFound { |
| + t.Errorf("box DELETE = %d, want 404", w.Code) |
| + } |
| + got, gotTag, err := console.Get(context.Background(), key) |
| + if err != nil || string(got) != body || gotTag != etag { |
| + t.Errorf("published object after box attempts = %q %q, %v; want the console's", got, gotTag, err) |
| + } |
| +} |
| + |
| +// TestStoreDoorManagedReadyKeyMatchesConsole keeps the door's matcher and the |
| +// console's publisher on one key shape: if internal/console ever moves the |
| +// ready object, the door would otherwise stop protecting it silently. |
| +func TestStoreDoorManagedReadyKeyMatchesConsole(t *testing.T) { |
| + src, err := os.ReadFile(filepath.Join("..", "console", "managedworker.go")) |
| + if err != nil { |
| + t.Fatalf("read console publisher: %v", err) |
| + } |
| + const want = `return "fleets/" + b.Fleet + "/boxes/" + b.Label + "/ready.json"` |
| + if !strings.Contains(string(src), "func managedReadyKey(b managedBox) string {\n\t"+want+"\n}") { |
| + t.Fatalf("internal/console managedReadyKey no longer reads %s; update managedReadyObjectKey to match", want) |
| + } |
| + if !managedReadyObjectKey(testFleet, readyKey(testFleet, "box-a")) { |
| + t.Fatal("the door's matcher does not recognise the console's key") |
| + } |
| +} |