durability 3a: pin fail-closed box key changes
Replace the withdrawn reseal witness with fresh-backend sweep and wake checks. Pin unreadable env and trio conflict refusals, and prove no mint or overwrite of the escrow sealed to the old key.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+119
−25
internal/activator/escrow_test.go+119 −25
diff --git a/internal/activator/escrow_test.go b/internal/activator/escrow_test.go| index 3f81b2c47..023859e7f 100644 |
| --- a/internal/activator/escrow_test.go |
| +++ b/internal/activator/escrow_test.go |
| @@ -1100,31 +1100,125 @@ func TestAFleetBoxSealsToItsOwnKey(t *testing.T) { |
| } |
| } |
| -func TestABoxThatChangesKeyResealsItsEscrow(t *testing.T) { |
| - ctx := context.Background() |
| - b, rt := escrowBackend(t, escrowHost) |
| - keyA, keyB := testEscrowKey(t), testEscrowKey(t) |
| - b.EscrowKey = keyA |
| - writeEnv(t, b, rt, envA) |
| - mustPublish(t, b, rt) |
| - sealA := sealerOf(t, b, rt) |
| - before := loadManifest(t, b, rt) |
| - prefix := b.filesPrefix(rt.Host) |
| - if plain, err := hostfiles.ReadSealed(ctx, b.FilesStore, prefix, before, hostfiles.SealedEnvItem, nil, sealA); err != nil || string(plain) != envA { |
| - t.Fatalf("initial escrow with A = %q, %v", plain, err) |
| - } |
| - b.EscrowKey = keyB |
| - sealB := sealerOf(t, b, rt) |
| - rep := b.EscrowSweep(ctx, []registry.Route{rt}) |
| - if rep.Published != 1 || len(rep.Failing) != 0 { |
| - t.Errorf("key-change sweep = %+v, want one publish", rep) |
| - } |
| - after := loadManifest(t, b, rt) |
| - if plain, err := hostfiles.ReadSealed(ctx, b.FilesStore, prefix, after, hostfiles.SealedEnvItem, nil, sealB); err != nil || string(plain) != envA { |
| - t.Errorf("escrow after key change with B = %q, %v", plain, err) |
| - } |
| - if _, err := hostfiles.ReadSealed(ctx, b.FilesStore, prefix, after, hostfiles.SealedEnvItem, nil, sealA); err == nil { |
| - t.Error("key A still opens the escrow after the sweep") |
| +// A key change in host.env takes effect after a restart. The new process |
| +// cannot check the old escrow's write-once secrets, so it never reseals |
| +// them. Even with all local files present, the launch refuses to proceed |
| +// without checking the escrow. The operator must put the old key back |
| +// and settle any conflict marker before trying again. |
| +func TestABoxThatChangesKeyFailsClosed(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + missingEnv, trio bool |
| + }{ |
| + {name: "local-env-present"}, |
| + {name: "env-missing", missingEnv: true}, |
| + {name: "all-local-files-present", trio: true}, |
| + {name: "env-missing-trio-present", missingEnv: true, trio: true}, |
| + } { |
| + missingEnv := tc.missingEnv |
| + t.Run(tc.name, func(t *testing.T) { |
| + ctx := context.Background() |
| + s := escrowService(t, escrowHost) |
| + b, rt := s.b, s.rt |
| + keyA, keyB := testEscrowKey(t), testEscrowKey(t) |
| + b.EscrowKey = keyA |
| + writeEnv(t, b, rt, envA) |
| + trio := map[string]string{} |
| + if tc.trio { |
| + trio = map[string]string{"instance.key": "ik-a", "report.token": "rt-a"} |
| + } |
| + writeFiles(t, b, rt, trio) |
| + holdLease(t, b, rt) |
| + mustPublish(t, b, rt) |
| + sealA := sealerOf(t, b, rt) |
| + before := prefixObjects(t, b, rt) |
| + ledger, err := os.ReadFile(escrowLedgerPath(b.DataDir, rt.Host)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if missingEnv { |
| + if err := os.Remove(b.pathsFor(rt).envFile); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + |
| + // Keep the bucket, ledger and local files, but none of A's |
| + // in-memory escrow or publisher state, as on an agent restart. |
| + nb := restartEscrow(t, b) |
| + nb.EscrowKey = keyB |
| + cs := &countingStore{Store: nb.FilesStore} |
| + nb.FilesStore = cs |
| + s.b = nb |
| + s.wire(nb) |
| + s.a = New(Config{Bucket: nb.Cfg.Bucket, Idle: time.Hour}, nb, nb.Registry) |
| + sealB := sealerOf(t, nb, rt) |
| + release := holdLease(t, nb, rt) |
| + rep := nb.EscrowSweep(ctx, []registry.Route{rt}) |
| + if missingEnv && !tc.trio { |
| + if rep.Published != 0 || rep.Unchanged != 1 || len(rep.Failing) != 0 { |
| + t.Errorf("sweep with no local secrets = %+v, want unchanged", rep) |
| + } |
| + } else if rep.Published != 0 || len(rep.Failing) != 1 || rep.Failing[0] != rt.Host { |
| + t.Errorf("key-change sweep = %+v, want this host refused without publication", rep) |
| + } |
| + release() |
| + |
| + _, err = s.wakeForTest(ctx) |
| + wantErr := errEscrowUnreadable |
| + if missingEnv { |
| + wantErr = ErrEscrowNotReady |
| + } |
| + if tc.trio { |
| + // The trio digest is keyed: B differs from A's digest even |
| + // for the same plaintext. The sweep leaves a conflict marker, |
| + // and the wake refuses on that marker before restore or mint. |
| + marker := escrowConflictPath(nb.DataDir, rt.Host) |
| + if err == nil || !strings.Contains(err.Error(), "ALERT: ") || !strings.Contains(err.Error(), marker) || !exists(marker) { |
| + t.Errorf("wake with trio after key change = %v; marker present %v", err, exists(marker)) |
| + } |
| + } else if !errors.Is(err, wantErr) { |
| + t.Errorf("wake after key change = %v, want %v", err, wantErr) |
| + } |
| + if spawns(nb) != 0 { |
| + t.Errorf("wake spawned %d processes", spawns(nb)) |
| + } |
| + if got := envFileBytes(t, nb, rt); missingEnv && exists(nb.pathsFor(rt).envFile) || !missingEnv && got != envA { |
| + t.Errorf("wake changed or minted the env: %q (missing before wake: %v)", got, missingEnv) |
| + } |
| + for item, want := range trio { |
| + if got := restoredSecret(t, filepath.Join(filesDirOf(nb, rt), item)); got != want { |
| + t.Errorf("local %s = %q, want unchanged %q", item, got, want) |
| + } |
| + } |
| + if cs.puts.Load() != 0 { |
| + t.Errorf("new-key sweep/wake made %d PUTs", cs.puts.Load()) |
| + } |
| + after := prefixObjects(t, nb, rt) |
| + if len(after) != len(before) { |
| + t.Errorf("bucket object count changed: %d -> %d", len(before), len(after)) |
| + } |
| + for key, body := range before { |
| + if !bytes.Equal(after[key], body) { |
| + t.Errorf("bucket object %s changed", key) |
| + } |
| + } |
| + if got, err := os.ReadFile(escrowLedgerPath(nb.DataDir, rt.Host)); err != nil || !bytes.Equal(got, ledger) { |
| + t.Errorf("ledger changed: %v", err) |
| + } |
| + m := loadManifest(t, nb, rt) |
| + sealed := map[string]string{hostfiles.SealedEnvItem: envA} |
| + for item, plain := range trio { |
| + sealed[item+".sealed"] = plain |
| + } |
| + for item, want := range sealed { |
| + if plain, err := hostfiles.ReadSealed(ctx, nb.FilesStore, nb.filesPrefix(rt.Host), m, item, nil, sealA); err != nil || string(plain) != want { |
| + t.Errorf("escrow %s with A = %q, %v", item, plain, err) |
| + } |
| + if _, err := hostfiles.ReadSealed(ctx, nb.FilesStore, nb.filesPrefix(rt.Host), m, item, nil, sealB); err == nil { |
| + t.Errorf("key B opened %s", item) |
| + } |
| + } |
| + }) |
| } |
| } |