Add appconfig.Sealer and Var.SealedTo
Sealer seals to one key for one binding and records the key's fingerprint in Var.SealedTo. SetSecret now takes map[string]Var. scanSecretHash clears SealedTo so callback secrets already delivered are not re-sent.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
appconfig_cli.go+3 −3internal/appconfig/appconfig.go+3 −0internal/appconfig/configack_test.go+1 −1internal/appconfig/crypto_bind_test.go+1 −1internal/appconfig/generation_test.go+1 −1internal/appconfig/owned_test.go+1 −1internal/appconfig/sealer.go+67 −0internal/appconfig/sealer_test.go+84 −0internal/appconfig/store.go+7 −5internal/appconfig/store_test.go+1 −1internal/appconfig/sync_test.go+1 −1internal/console/scankeysync.go+3 −0internal/console/scankeysync_hash_test.go+30 −0internal/host/replicahomekeys_test.go+1 −1
Intent
<task-notification> <task-id>aa0a9c630d08afd07</task-id> <tool-use-id>toolu_019Su6VGxg2dDjzhcDBMWfhz</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/2d812478-ae68-44f2-8075-b2330abd5939/tasks/aa0a9c630d08afd07.output</output-file> <status>completed</status> <summary>Agent "Implement Task 0: copy deck" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE_WITH_CONCERNS Commit: 3ab89eea "Fleet secrets key Task 0: copy deck (FSK-C01 to C70, all pending) and its gate" (the usual packed-refs.lock hook error printed; `git log -1` confirms the commit). Tests: `go test . -run 'TestFleetSecretsCopy' -count=1` passes all 6 gate tests. Deck: 70 rows, all `pending`, grouped by task (3: 11, 4: 25, 5: 7, 6: 3, 7: 1, 8: 3, 9: 5, 10: 15). `fleetSecretsCopyBuilt` is empty. I ran it with the sandbox off because the Go cache is read-only inside it. Two gate mutations (a pending id in the built list, a dropped inventory id) failed as they should. Concerns, detail in the report: - **Same-key no-op:** Task 3's handler returns before computing `stale`, so re-running the same key gives no list. `fleets ls` shows only the STALE count. The no-op answer should carry `stale`. - **Keyless fleet in the CLI:** the existing `secretsPubKeyAPI` refusal tells the user to set `CARLOS_SECRETS_PUBKEY`, which is wrong for a keyless fleet. FSK-C43 replaces it, but the CLI cannot know the fleet name from the GET. - **FSK-C04:** "another fleet already uses this key" tells the caller that some fleet holds that public key. It names neither fleet nor account. - **FSK-C52:** the `cannot_open` text doesn't fit a platform instance holding a fleet-key value. - **FSK-C01:** a fragment, because the shared owner helper builds "only an account owner can ...". It has no next-step sentence. - **Wording:** I used "sealed" (the existing CLI word), `carlos email rotate` (not `mail rotate`), and a full stop where spec §5.7 has a semicolon. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/design-fleet-secrets-key/.superpowers/sdd/2026-10-29-fleet-secrets-key/task-0-report.md</result> <usage><subagent_tokens>171967</subagent_tokens><tool_uses>28</tool_uses><duration_ms>483030</duration_ms></usage> </task-notification>
<task-notification> <task-id>b21b0wa1j</task-id> <tool-use-id>toolu_019fMztCpiw5aAQgBod6AAPL</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/2d812478-ae68-44f2-8075-b2330abd5939/tasks/b21b0wa1j.output</output-file> <status>completed</status> <summary>Background command "Ledger rulings; run Luna pass 1" completed (exit code 0)</summary> </task-notification>
diff --git a/appconfig_cli.go b/appconfig_cli.go| index e4203a2ff..6956ab259 100644 |
| --- a/appconfig_cli.go |
| +++ b/appconfig_cli.go |
| @@ -310,13 +310,13 @@ func saveEnvVars(ctx context.Context, s release.Store, acct, app, environment st |
| // environment=="", or to the named environment's own bundle otherwise — |
| // mirrors saveEnvVars; see its comment. |
| func saveSecrets(ctx context.Context, s release.Store, acct, app, environment string, sealed map[string]string) error { |
| - if environment == "" { |
| - return appconfig.SetSecret(ctx, s, acct, app, sealed) |
| - } |
| sets := make(map[string]appconfig.Var, len(sealed)) |
| for k, v := range sealed { |
| sets[k] = appconfig.Var{Kind: "secret", Envelope: v} |
| } |
| + if environment == "" { |
| + return appconfig.SetSecret(ctx, s, acct, app, sets) |
| + } |
| return appconfig.SaveEnvironment(ctx, s, acct, app, environment, sets, nil) |
| } |
diff --git a/internal/appconfig/appconfig.go b/internal/appconfig/appconfig.go| index da196cb5c..e5fb0bb29 100644 |
| --- a/internal/appconfig/appconfig.go |
| +++ b/internal/appconfig/appconfig.go |
| @@ -157,10 +157,13 @@ func ValidSwapConcurrency(n int) bool { return n >= 0 } |
| // Var is one config var or secret. Kind selects which of Value/Envelope is |
| // populated: "env" vars are plaintext (Value); "secret" vars are sealed |
| // client-side before upload (Envelope, base64 — see crypto.go's Seal). |
| +// SealedTo is the KeyFingerprint of the key a secret was sealed to; empty for |
| +// env vars and for secrets sealed before the field existed. |
| type Var struct { |
| Kind string `json:"kind"` |
| Value string `json:"value,omitempty"` |
| Envelope string `json:"envelope,omitempty"` |
| + SealedTo string `json:"sealed_to,omitempty"` |
| } |
| // Key is the bucket key of app's config manifest, account-scoped by |
diff --git a/internal/appconfig/configack_test.go b/internal/appconfig/configack_test.go| index f354f695c..f15cb4f5a 100644 |
| --- a/internal/appconfig/configack_test.go |
| +++ b/internal/appconfig/configack_test.go |
| @@ -49,7 +49,7 @@ func sealRelayConfig(t *testing.T, ctx context.Context, s release.Store, pub *ec |
| if err != nil { |
| t.Fatalf("Seal: %v", err) |
| } |
| - if err := SetSecret(ctx, s, acct, app, map[string]string{"CARLOS_RELAY": envelope}); err != nil { |
| + if err := SetSecret(ctx, s, acct, app, map[string]Var{"CARLOS_RELAY": {Kind: "secret", Envelope: envelope}}); err != nil { |
| t.Fatalf("SetSecret: %v", err) |
| } |
| return suffixes |
diff --git a/internal/appconfig/crypto_bind_test.go b/internal/appconfig/crypto_bind_test.go| index be490cbe3..db15fabd2 100644 |
| --- a/internal/appconfig/crypto_bind_test.go |
| +++ b/internal/appconfig/crypto_bind_test.go |
| @@ -136,7 +136,7 @@ func TestSyncRefusesAnEnvelopeSealedForAnotherAccount(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| - if err := SetSecret(ctx, s, "bbb", "blog", map[string]string{"TOKEN": env}); err != nil { |
| + if err := SetSecret(ctx, s, "bbb", "blog", map[string]Var{"TOKEN": {Kind: "secret", Envelope: env}}); err != nil { |
| t.Fatal(err) |
| } |
| if err := reg.Upsert(registry.Route{ |
diff --git a/internal/appconfig/generation_test.go b/internal/appconfig/generation_test.go| index c5f51aa4c..889cd8ecb 100644 |
| --- a/internal/appconfig/generation_test.go |
| +++ b/internal/appconfig/generation_test.go |
| @@ -146,7 +146,7 @@ func TestWritesBumpGeneration(t *testing.T) { |
| return SetEnv(ctx, s, "bab", "keymail", map[string]string{"LOG_LEVEL": "debug"}) |
| }}, |
| {"SetSecret", func() error { |
| - return SetSecret(ctx, s, "bab", "keymail", map[string]string{"SES_SECRET": envelope}) |
| + return SetSecret(ctx, s, "bab", "keymail", map[string]Var{"SES_SECRET": {Kind: "secret", Envelope: envelope}}) |
| }}, |
| {"Unset", func() error { |
| _, err := Unset(ctx, s, "bab", "keymail", []string{"LOG_LEVEL"}) |
diff --git a/internal/appconfig/owned_test.go b/internal/appconfig/owned_test.go| index a13fc0243..6bb182a2c 100644 |
| --- a/internal/appconfig/owned_test.go |
| +++ b/internal/appconfig/owned_test.go |
| @@ -40,7 +40,7 @@ func ownedWorld(t *testing.T) (context.Context, release.Store, string) { |
| if err := SetEnv(ctx, s, "bab", "docs", map[string]string{"OWNER_CANARY": "owner-env-canary", "SHARED": "default"}); err != nil { |
| t.Fatal(err) |
| } |
| - if err := SetSecret(ctx, s, "bab", "docs", map[string]string{"OWNER_SECRET": env}); err != nil { |
| + if err := SetSecret(ctx, s, "bab", "docs", map[string]Var{"OWNER_SECRET": {Kind: "secret", Envelope: env}}); err != nil { |
| t.Fatal(err) |
| } |
| if _, err := ApplyEnvironmentOwned(ctx, s, "bab", "docs", "team", "alta", |
diff --git a/internal/appconfig/sealer.go b/internal/appconfig/sealer.go| new file mode 100644 |
| index 000000000..9b6f68183 |
| --- /dev/null |
| +++ b/internal/appconfig/sealer.go |
| @@ -0,0 +1,67 @@ |
| +package appconfig |
| + |
| +import ( |
| + "crypto/ecdh" |
| + "encoding/base64" |
| + "errors" |
| +) |
| + |
| +// ErrSealerBinding is what Sealer.Seal returns when asked to seal for a |
| +// binding other than the one the sealer was built for. |
| +var ErrSealerBinding = errors.New("sealer: binding does not match the sealer's own") |
| + |
| +// Sealer seals secrets to one public key for one (account, app) binding, and |
| +// records which key it used. Every seal in the platform goes through it, so a |
| +// stored secret always names the key it was sealed to (Var.SealedTo). Whether |
| +// it writes a bound (v2) or unbound (v1) envelope is fixed at construction, |
| +// not read from the deployment-wide switch. |
| +type Sealer struct { |
| + pub *ecdh.PublicKey |
| + fp string |
| + binding Binding |
| + bound bool |
| +} |
| + |
| +// NewSealer returns a Sealer for pub and b. bound selects a v2 envelope bound |
| +// to b (true) or a v1 envelope (false). |
| +func NewSealer(pub *ecdh.PublicKey, b Binding, bound bool) Sealer { |
| + return Sealer{pub: pub, fp: KeyFingerprint(pub), binding: b, bound: bound} |
| +} |
| + |
| +// Fingerprint is KeyFingerprint of the sealer's public key. |
| +func (s Sealer) Fingerprint() string { return s.fp } |
| + |
| +// Binding is the (account, app) the sealer seals for. |
| +func (s Sealer) Binding() Binding { return s.binding } |
| + |
| +// Bound reports whether the sealer writes v2 (bound) envelopes. |
| +func (s Sealer) Bound() bool { return s.bound } |
| + |
| +// PublicKeyB64 is the base64 of the public point: what the config GET serves |
| +// as secrets_pubkey. |
| +func (s Sealer) PublicKeyB64() string { |
| + return base64.StdEncoding.EncodeToString(s.pub.Bytes()) |
| +} |
| + |
| +// Seal encrypts plaintext as a secret Var for b, which must equal the |
| +// sealer's own binding. The Var carries the key's fingerprint in SealedTo. |
| +func (s Sealer) Seal(b Binding, plaintext []byte) (Var, error) { |
| + if b != s.binding { |
| + return Var{}, ErrSealerBinding |
| + } |
| + var ( |
| + env string |
| + err error |
| + ) |
| + if s.bound { |
| + env, err = SealBound(s.pub, b, plaintext) |
| + } else { |
| + if _, err = b.aad(); err == nil { |
| + env, err = SealUnbound(s.pub, plaintext) |
| + } |
| + } |
| + if err != nil { |
| + return Var{}, err |
| + } |
| + return Var{Kind: "secret", Envelope: env, SealedTo: s.fp}, nil |
| +} |
diff --git a/internal/appconfig/sealer_test.go b/internal/appconfig/sealer_test.go| new file mode 100644 |
| index 000000000..558f6445b |
| --- /dev/null |
| +++ b/internal/appconfig/sealer_test.go |
| @@ -0,0 +1,84 @@ |
| +package appconfig |
| + |
| +import ( |
| + "encoding/base64" |
| + "errors" |
| + "testing" |
| +) |
| + |
| +func TestSealerSealsToItsKeyAndRecordsTheFingerprint(t *testing.T) { |
| + priv, pub := bindKeys(t) |
| + b := Binding{Account: "bab", App: "shop"} |
| + for _, bound := range []bool{true, false} { |
| + s := NewSealer(pub, b, bound) |
| + v, err := s.Seal(b, []byte("s3kr1t")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if v.Kind != "secret" || v.Envelope == "" || v.Value != "" { |
| + t.Fatalf("bound=%v: var = %+v", bound, v) |
| + } |
| + if v.SealedTo != KeyFingerprint(pub) || v.SealedTo != s.Fingerprint() { |
| + t.Fatalf("bound=%v: SealedTo = %q, want %q", bound, v.SealedTo, KeyFingerprint(pub)) |
| + } |
| + got, err := Open(priv, b, v.Envelope) |
| + if err != nil || string(got) != "s3kr1t" { |
| + t.Fatalf("bound=%v: Open = %q, %v", bound, got, err) |
| + } |
| + } |
| +} |
| + |
| +func TestSealerAccessors(t *testing.T) { |
| + _, pub := bindKeys(t) |
| + b := Binding{Account: "bab", App: "shop"} |
| + s := NewSealer(pub, b, true) |
| + if s.Binding() != b || !s.Bound() || s.Fingerprint() != KeyFingerprint(pub) { |
| + t.Fatalf("accessors: %+v %v %q", s.Binding(), s.Bound(), s.Fingerprint()) |
| + } |
| + if want := base64.StdEncoding.EncodeToString(pub.Bytes()); s.PublicKeyB64() != want { |
| + t.Fatalf("PublicKeyB64 = %q, want %q", s.PublicKeyB64(), want) |
| + } |
| + if NewSealer(pub, b, false).Bound() { |
| + t.Fatal("unbound sealer reports Bound") |
| + } |
| +} |
| + |
| +func TestSealerRefusesAnotherBinding(t *testing.T) { |
| + _, pub := bindKeys(t) |
| + s := NewSealer(pub, Binding{Account: "bab", App: "shop"}, true) |
| + v, err := s.Seal(Binding{Account: "bab", App: "other"}, []byte("x")) |
| + if !errors.Is(err, ErrSealerBinding) { |
| + t.Fatalf("err = %v, want ErrSealerBinding", err) |
| + } |
| + if v != (Var{}) { |
| + t.Fatalf("refused seal returned %+v", v) |
| + } |
| +} |
| + |
| +// A sealer's bound flag is fixed at construction: the deployment-wide switch |
| +// (SetBoundSealing) does not change what it writes. |
| +func TestSealerBoundIgnoresTheDeploymentSwitch(t *testing.T) { |
| + _, pub := bindKeys(t) |
| + b := Binding{Account: "bab", App: "shop"} |
| + prev := BoundSealing() |
| + SetBoundSealing(false) |
| + t.Cleanup(func() { SetBoundSealing(prev) }) |
| + |
| + prefix := func(bound bool) string { |
| + v, err := NewSealer(pub, b, bound).Seal(b, []byte("x")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + raw, err := base64.StdEncoding.DecodeString(v.Envelope) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return string(raw[:4]) |
| + } |
| + if got := prefix(true); got != "CSB2" { |
| + t.Fatalf("bound sealer prefix = %q, want CSB2", got) |
| + } |
| + if got := prefix(false); got == "CSB2" { |
| + t.Fatalf("unbound sealer wrote a v2 envelope") |
| + } |
| +} |
diff --git a/internal/appconfig/store.go b/internal/appconfig/store.go| index ccf8c9573..7993b0b85 100644 |
| --- a/internal/appconfig/store.go |
| +++ b/internal/appconfig/store.go |
| @@ -235,12 +235,14 @@ func claimRawSetEnv(ctx context.Context, s release.Store, acct, app string, kv m |
| } |
| // SetSecret writes one or more secrets. sealed values must already be |
| -// Seal-produced envelopes — this function stores them opaquely and never |
| -// sees plaintext. |
| -func claimRawSetSecret(ctx context.Context, s release.Store, acct, app string, sealed map[string]string) error { |
| +// Seal-produced envelopes (Sealer.Seal's Vars, or a Var built around an |
| +// envelope sealed elsewhere) — this function stores them opaquely and never |
| +// sees plaintext. Kind is forced to "secret". |
| +func claimRawSetSecret(ctx context.Context, s release.Store, acct, app string, sealed map[string]Var) error { |
| sets := make(map[string]Var, len(sealed)) |
| for k, v := range sealed { |
| - sets[k] = Var{Kind: "secret", Envelope: v} |
| + v.Kind = "secret" |
| + sets[k] = v |
| } |
| _, err := apply(ctx, s, acct, app, DefaultBundle, sets, nil) |
| return err |
| @@ -342,7 +344,7 @@ func SetExecDeliveryAt(ctx context.Context, s release.Store, acct, app, envName, |
| }) |
| return |
| } |
| -func SetSecret(ctx context.Context, s release.Store, acct, app string, sealed map[string]string) (claimResult0 error) { |
| +func SetSecret(ctx context.Context, s release.Store, acct, app string, sealed map[string]Var) (claimResult0 error) { |
| claimResult0 = release.WithAppMutation(ctx, s, acct, app, "appconfig.SetSecret", func(claimContext context.Context) error { |
| claimResult0 = claimRawSetSecret(claimContext, s, acct, app, sealed) |
| return claimResult0 |
diff --git a/internal/appconfig/store_test.go b/internal/appconfig/store_test.go| index 55d2583e4..161be3f7f 100644 |
| --- a/internal/appconfig/store_test.go |
| +++ b/internal/appconfig/store_test.go |
| @@ -104,7 +104,7 @@ func TestUnsetOfAnAbsentKeyOnAnExistingBundleIsANoopSuccess(t *testing.T) { |
| func TestSetSecretStoresEnvelopeOnly(t *testing.T) { |
| s := release.NewDirStore(t.TempDir()) |
| ctx := context.Background() |
| - if err := SetSecret(ctx, s, "bab", "keymail", map[string]string{"SES_SECRET": "opaque-envelope-bytes"}); err != nil { |
| + if err := SetSecret(ctx, s, "bab", "keymail", map[string]Var{"SES_SECRET": {Kind: "secret", Envelope: "opaque-envelope-bytes"}}); err != nil { |
| t.Fatalf("SetSecret: %v", err) |
| } |
| m, err := Get(ctx, s, "bab", "keymail") |
diff --git a/internal/appconfig/sync_test.go b/internal/appconfig/sync_test.go| index c91af7b74..273132b2b 100644 |
| --- a/internal/appconfig/sync_test.go |
| +++ b/internal/appconfig/sync_test.go |
| @@ -44,7 +44,7 @@ func TestSyncWritesFileAndRestartsOnChange(t *testing.T) { |
| if err := SetEnv(ctx, s, "bab", "keymail", map[string]string{"LOG_LEVEL": "debug"}); err != nil { |
| t.Fatalf("SetEnv: %v", err) |
| } |
| - if err := SetSecret(ctx, s, "bab", "keymail", map[string]string{"SES_SECRET": envelope}); err != nil { |
| + if err := SetSecret(ctx, s, "bab", "keymail", map[string]Var{"SES_SECRET": {Kind: "secret", Envelope: envelope}}); err != nil { |
| t.Fatalf("SetSecret: %v", err) |
| } |
| if err := reg.Upsert(registry.Route{ |
diff --git a/internal/console/scankeysync.go b/internal/console/scankeysync.go| index 9b6f6f6ce..97e4b656c 100644 |
| --- a/internal/console/scankeysync.go |
| +++ b/internal/console/scankeysync.go |
| @@ -17,6 +17,9 @@ func scanSecretHash(v appconfig.Var) string { |
| if v.Kind != "secret" || v.Envelope == "" { |
| return "" |
| } |
| + // SealedTo says which key sealed the value, not what was delivered: left |
| + // out, a Var without it hashes as it did before the field existed. |
| + v.SealedTo = "" |
| raw, _ := json.Marshal(v) |
| digest := sha256.Sum256(raw) |
| return hex.EncodeToString(digest[:]) |
diff --git a/internal/console/scankeysync_hash_test.go b/internal/console/scankeysync_hash_test.go| new file mode 100644 |
| index 000000000..85b5214ac |
| --- /dev/null |
| +++ b/internal/console/scankeysync_hash_test.go |
| @@ -0,0 +1,30 @@ |
| +package console |
| + |
| +import ( |
| + "crypto/sha256" |
| + "encoding/hex" |
| + "encoding/json" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/appconfig" |
| +) |
| + |
| +func TestScanSecretHashIgnoresSealedTo(t *testing.T) { |
| + a := appconfig.Var{Kind: "secret", Envelope: "ZW52ZWxvcGU="} |
| + b := a |
| + b.SealedTo = "sha256:abc" |
| + if scanSecretHash(a) == "" || scanSecretHash(a) != scanSecretHash(b) { |
| + t.Fatalf("hash differs with SealedTo: %q vs %q", scanSecretHash(a), scanSecretHash(b)) |
| + } |
| + // A var with no SealedTo hashes as it did before the field existed, so |
| + // deliveries already recorded are not re-sent. |
| + old, _ := json.Marshal(struct { |
| + Kind string `json:"kind"` |
| + Value string `json:"value,omitempty"` |
| + Envelope string `json:"envelope,omitempty"` |
| + }{Kind: a.Kind, Envelope: a.Envelope}) |
| + sum := sha256.Sum256(old) |
| + if got := scanSecretHash(a); got != hex.EncodeToString(sum[:]) { |
| + t.Fatalf("hash = %q, want the pre-SealedTo hash %q", got, hex.EncodeToString(sum[:])) |
| + } |
| +} |
diff --git a/internal/host/replicahomekeys_test.go b/internal/host/replicahomekeys_test.go| index c3823dc9b..c719b92e9 100644 |
| --- a/internal/host/replicahomekeys_test.go |
| +++ b/internal/host/replicahomekeys_test.go |
| @@ -131,7 +131,7 @@ func TestConfiguredKeyReadsTheBucketNow(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| - if err := appconfig.SetSecret(ctx, s, "acct", "shop", map[string]string{"SEALED": envelope}); err != nil { |
| + if err := appconfig.SetSecret(ctx, s, "acct", "shop", map[string]appconfig.Var{"SEALED": {Kind: "secret", Envelope: envelope}}); err != nil { |
| t.Fatal(err) |
| } |
| if _, err := configuredKey(func(string) string { return "" }, open)(ctx, rt); err == nil { |