Direct-bucket secrets set refuses an account that owns a fleet and any registered fleet key (Task 8)
directSealRefusal lists accounts/, keeps fleet owner records, and refuses when the target account owns a fleet, when the terminal's key fingerprint is in any record's secrets_key_fps, or when the bucket cannot be listed or read (FSK-C48, C49, C50). Direct mode then seals with NewSealer and saves Vars carrying SealedTo; the seal guard allowlists runSecretsSet for NewSealer only.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
appconfig_cli.go+66 −13appconfig_cli_fleet_test.go+219 −0approved_fleet_secrets_copy_test.go+1 −1internal/console/sealkey_guard_test.go+4 −2
Intent
<task-notification> <task-id>ae916787ec6397887</task-id> <tool-use-id>toolu_0193puaW8Dxqt5g7gvWRC8Qu</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/2d812478-ae68-44f2-8075-b2330abd5939/tasks/ae916787ec6397887.output</output-file> <status>completed</status> <summary>Agent "Implement Task 7: moves and env fork" 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 Commits: 924d47f1, then 97ae289c as the second commit (nothing amended). The `packed-refs.lock` hook error printed again, but the commit exists and the tree is clean. Change: in `staleForTarget`, an absent `sealed_to` is no longer stale when the target is the platform, and it stays stale for a fleet. A non-empty `sealed_to` that is not the deployment key's fingerprint is still stale for the platform. Tests: `TestPlaceBackToPlatformAcceptsLegacyValues` failed first (`stale for the platform = [LEGACY], <nil>, want none`) and now passes. It checks both directions: the value is not listed moving to the platform and is listed moving to a fleet (place refused with FSK-C47). The Task 7 tests, fleet-secrets, sealkey, environment and guard tests pass, and build and vet are clean. Concerns: none. The earlier concern about absent `sealed_to` being stale for the platform no longer applies; the fix note is appended to the report. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/design-fleet-secrets-key/.superpowers/sdd/2026-10-29-fleet-secrets-key/task-7-report.md</result> <usage><subagent_tokens>157434</subagent_tokens><tool_uses>19</tool_uses><duration_ms>892466</duration_ms></usage> </task-notification>
Attributed to this commit: 22.8 g CO₂e (2,905,804 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-fleet-secrets-key, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/appconfig_cli.go b/appconfig_cli.go| index d27556599..e5ccb6432 100644 |
| --- a/appconfig_cli.go |
| +++ b/appconfig_cli.go |
| @@ -6,11 +6,14 @@ package main |
| import ( |
| "context" |
| "crypto/ecdh" |
| + "encoding/json" |
| + "errors" |
| "flag" |
| "fmt" |
| "io" |
| "log" |
| "os" |
| + "slices" |
| "sort" |
| "strconv" |
| "strings" |
| @@ -306,14 +309,10 @@ func saveEnvVars(ctx context.Context, s release.Store, acct, app, environment st |
| return appconfig.SaveEnvironment(ctx, s, acct, app, environment, sets, nil) |
| } |
| -// saveSecrets writes already-sealed secrets to the default bundle when |
| -// 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 { |
| - sets := make(map[string]appconfig.Var, len(sealed)) |
| - for k, v := range sealed { |
| - sets[k] = appconfig.Var{Kind: "secret", Envelope: v} |
| - } |
| +// saveSecrets writes already-sealed secrets (each Var carries SealedTo) to |
| +// the default bundle when environment=="", or to the named environment's own |
| +// bundle otherwise, mirroring saveEnvVars (see its comment). |
| +func saveSecrets(ctx context.Context, s release.Store, acct, app, environment string, sets map[string]appconfig.Var) error { |
| if environment == "" { |
| return appconfig.SetSecret(ctx, s, acct, app, sets) |
| } |
| @@ -1008,18 +1007,24 @@ func runSecretsSet(args []string) { |
| } |
| pub := secretsPubKey() |
| acct := acctOf() |
| + store := openStore() |
| + // Direct mode cannot ask a console which key the app needs, so it |
| + // refuses wherever its key could be a fleet's (spec §5.3, I1) before |
| + // anything is sealed. |
| + if err := directSealRefusal(context.Background(), store, acct, pub); err != nil { |
| + log.Fatal(strings.ReplaceAll(err.Error(), "<app>", *app)) |
| + } |
| // Direct-bucket mode has no console to ask, so the deployment's switch |
| // comes from the operator's environment under the console's own name. |
| - appconfig.SetBoundSealing(secretsEnvelopeV2Env()) |
| - sealed := make(map[string]string, len(kv)) |
| + sealer := appconfig.NewSealer(pub, appconfig.Binding{Account: acct, App: *app}, secretsEnvelopeV2Env()) |
| + sealed := make(map[string]appconfig.Var, len(kv)) |
| for k, v := range kv { |
| - env, err := appconfig.Seal(pub, appconfig.Binding{Account: acct, App: *app}, []byte(v)) |
| + sv, err := sealer.Seal(appconfig.Binding{Account: acct, App: *app}, []byte(v)) |
| if err != nil { |
| log.Fatalf("seal %s: %v", k, err) |
| } |
| - sealed[k] = env |
| + sealed[k] = sv |
| } |
| - store := openStore() |
| if err := saveSecrets(context.Background(), store, acct, *app, *environment, sealed); err != nil { |
| log.Fatal(err) |
| } |
| @@ -1027,6 +1032,54 @@ func runSecretsSet(args []string) { |
| printRestartNotice(os.Stdout, *app, localRestartAnswer(context.Background(), store, *regPath, acctOf(), *app, *environment, mapKeys(kv))) |
| } |
| +// directSealRefusal is direct-bucket `secrets set`'s check that sealing to |
| +// pub for acct is safe (spec §5.3, I1). Nothing in this mode can say which |
| +// fleet's key an app needs, so it refuses when acct owns a fleet at all, and |
| +// refuses when pub is any key any fleet in the bucket has ever registered |
| +// (the owner record's secrets_key_fps is append-only), whoever owns it. It |
| +// refuses too when it cannot list the bucket or read a record: a check that |
| +// did not run is not a pass. The refusal for an owning account carries the |
| +// literal "<app>", which the caller fills in. |
| +func directSealRefusal(ctx context.Context, st release.Store, acct string, pub *ecdh.PublicKey) error { |
| + keys, err := st.List(ctx, "accounts/") |
| + if err != nil { |
| + return errors.New(directRefuseNoCheck) |
| + } |
| + var records []string |
| + for _, key := range keys { |
| + p := strings.Split(key, "/") |
| + if len(p) != 4 || p[0] != "accounts" || p[2] != "fleets" || p[1] == "" || !strings.HasSuffix(p[3], ".json") { |
| + continue |
| + } |
| + if p[1] == acct { |
| + return errors.New(directRefuseOwnsFleet) |
| + } |
| + records = append(records, key) |
| + } |
| + fp := appconfig.KeyFingerprint(pub) |
| + for _, key := range records { |
| + body, _, err := st.Get(ctx, key) |
| + if err != nil { |
| + return errors.New(directRefuseNoCheck) |
| + } |
| + var rec release.FleetOwner |
| + if err := json.Unmarshal(body, &rec); err != nil { |
| + return errors.New(directRefuseNoCheck) |
| + } |
| + if slices.Contains(rec.SecretsKeyFPs, fp) { |
| + return errors.New(directRefuseFleetKey) |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// Fleet secrets key copy rows FSK-C48 to FSK-C50. |
| +const ( |
| + directRefuseOwnsFleet = "this account owns a fleet, and a direct write cannot tell which key <app> needs. unset CARLOS_DEPLOYMENT_BUCKET and CARLOS_DEPLOYMENT_DIR in your terminal and run carlos secrets set through a signed-in console" |
| + directRefuseFleetKey = "the CARLOS_SECRETS_PUBKEY in your terminal is a fleet's key, and a direct write must not seal to one. set it to the deployment's public key and run the command again" |
| + directRefuseNoCheck = "cannot check this bucket for fleet keys, so nothing was set. check your access to the bucket and run the command again" |
| +) |
| + |
| func runSecretsUnset(args []string) { |
| fs := flag.NewFlagSet("secrets unset", flag.ExitOnError) |
| app := appFlag(fs, "") |
diff --git a/appconfig_cli_fleet_test.go b/appconfig_cli_fleet_test.go| new file mode 100644 |
| index 000000000..12528443e |
| --- /dev/null |
| +++ b/appconfig_cli_fleet_test.go |
| @@ -0,0 +1,219 @@ |
| +package main |
| + |
| +import ( |
| + "context" |
| + "crypto/ecdh" |
| + "encoding/json" |
| + "errors" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/appconfig" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// The approved rows FSK-C48 to FSK-C50, spelled out so a change to the |
| +// constants in appconfig_cli.go cannot pass unnoticed. |
| +const ( |
| + wantDirectRefuseOwnsFleet = "this account owns a fleet, and a direct write cannot tell which key <app> needs. unset CARLOS_DEPLOYMENT_BUCKET and CARLOS_DEPLOYMENT_DIR in your terminal and run carlos secrets set through a signed-in console" |
| + wantDirectRefuseFleetKey = "the CARLOS_SECRETS_PUBKEY in your terminal is a fleet's key, and a direct write must not seal to one. set it to the deployment's public key and run the command again" |
| + wantDirectRefuseNoCheck = "cannot check this bucket for fleet keys, so nothing was set. check your access to the bucket and run the command again" |
| +) |
| + |
| +// Direct-bucket mode has no console to say which key an app needs, so it |
| +// refuses wherever sealing could reach a fleet's key (fleet secrets key spec |
| +// §5.3, I1). |
| + |
| +func directFleetRecord(t *testing.T, st release.Store, acct, fleet string, fps ...string) { |
| + t.Helper() |
| + body, err := json.Marshal(release.FleetOwner{Fleet: fleet, Account: acct, SecretsKeyFPs: fps}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := st.Put(context.Background(), release.FleetOwnerKey(acct, fleet), body, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func directKey(t *testing.T) (pubB64, fp string) { |
| + t.Helper() |
| + _, pubB64, err := appconfig.GenerateKey() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + fp, err = appconfig.PublicFingerprint(pubB64) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return pubB64, fp |
| +} |
| + |
| +func directPub(t *testing.T, b64 string) *ecdh.PublicKey { |
| + t.Helper() |
| + k, err := appconfig.ParsePublicKey(b64, "test") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return k |
| +} |
| + |
| +// listFailStore is a store whose listing fails. |
| +type listFailStore struct{ release.Store } |
| + |
| +func (listFailStore) List(context.Context, string) ([]string, error) { |
| + return nil, errors.New("access denied") |
| +} |
| + |
| +// readFailStore lists fine and fails every read. |
| +type readFailStore struct{ release.Store } |
| + |
| +func (readFailStore) Get(context.Context, string) ([]byte, string, error) { |
| + return nil, "", errors.New("access denied") |
| +} |
| + |
| +func TestDirectSecretsSetRefusesAnAccountWithAFleet(t *testing.T) { |
| + st := release.NewDirStore(t.TempDir()) |
| + directFleetRecord(t, st, "bab", "home") |
| + b64, _ := directKey(t) |
| + err := directSealRefusal(context.Background(), st, "bab", directPub(t, b64)) |
| + if err == nil || err.Error() != wantDirectRefuseOwnsFleet { |
| + t.Fatalf("err = %v, want %q", err, wantDirectRefuseOwnsFleet) |
| + } |
| +} |
| + |
| +func TestDirectSecretsSetRefusesAnotherFleetsKey(t *testing.T) { |
| + st := release.NewDirStore(t.TempDir()) |
| + b64, fp := directKey(t) |
| + // Account xyz's fleet carries the terminal's key; the target account |
| + // bab owns no fleet. I1: the terminal key still must not be sealed to. |
| + directFleetRecord(t, st, "xyz", "away", fp) |
| + err := directSealRefusal(context.Background(), st, "bab", directPub(t, b64)) |
| + if err == nil || err.Error() != wantDirectRefuseFleetKey { |
| + t.Fatalf("err = %v, want %q", err, wantDirectRefuseFleetKey) |
| + } |
| +} |
| + |
| +func TestDirectSecretsSetRefusesOnListError(t *testing.T) { |
| + st := release.NewDirStore(t.TempDir()) |
| + b64, _ := directKey(t) |
| + err := directSealRefusal(context.Background(), listFailStore{st}, "bab", directPub(t, b64)) |
| + if err == nil || err.Error() != wantDirectRefuseNoCheck { |
| + t.Fatalf("list error: err = %v, want %q", err, wantDirectRefuseNoCheck) |
| + } |
| + // A record that lists but cannot be read is the same refusal. |
| + directFleetRecord(t, st, "xyz", "away") |
| + err = directSealRefusal(context.Background(), readFailStore{st}, "bab", directPub(t, b64)) |
| + if err == nil || err.Error() != wantDirectRefuseNoCheck { |
| + t.Fatalf("read error: err = %v, want %q", err, wantDirectRefuseNoCheck) |
| + } |
| + // So is one that does not parse. |
| + bad := release.NewDirStore(t.TempDir()) |
| + if _, err := bad.Put(context.Background(), release.FleetOwnerKey("xyz", "away"), []byte("{not json"), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + err = directSealRefusal(context.Background(), bad, "bab", directPub(t, b64)) |
| + if err == nil || err.Error() != wantDirectRefuseNoCheck { |
| + t.Fatalf("malformed record: err = %v, want %q", err, wantDirectRefuseNoCheck) |
| + } |
| +} |
| + |
| +func TestDirectSecretsSetRefusesAnyListedFingerprint(t *testing.T) { |
| + st := release.NewDirStore(t.TempDir()) |
| + oldB64, oldFP := directKey(t) |
| + newB64, newFP := directKey(t) |
| + directFleetRecord(t, st, "xyz", "away", oldFP, newFP) |
| + for name, b64 := range map[string]string{"old": oldB64, "new": newB64} { |
| + err := directSealRefusal(context.Background(), st, "bab", directPub(t, b64)) |
| + if err == nil || err.Error() != wantDirectRefuseFleetKey { |
| + t.Errorf("%s fingerprint: err = %v, want %q", name, err, wantDirectRefuseFleetKey) |
| + } |
| + } |
| + // A key no fleet ever carried passes. |
| + otherB64, _ := directKey(t) |
| + if err := directSealRefusal(context.Background(), st, "bab", directPub(t, otherB64)); err != nil { |
| + t.Errorf("an unlisted key was refused: %v", err) |
| + } |
| +} |
| + |
| +// directSetEnv points the CLI at a DirStore with the given key as the |
| +// terminal's CARLOS_SECRETS_PUBKEY. |
| +func directSetEnv(t *testing.T, dir, pubB64 string) { |
| + t.Helper() |
| + useTempCredentials(t) |
| + clearDeploymentEnv(t) |
| + t.Setenv("CARLOS_DEPLOYMENT_DIR", dir) |
| + t.Setenv("CARLOS_SECRETS_PUBKEY", pubB64) |
| + t.Setenv("CONSOLE_SECRETS_ENVELOPE_V2", "1") |
| +} |
| + |
| +func TestDirectSecretsSetPlatformAppStillWorks(t *testing.T) { |
| + dir := t.TempDir() |
| + privB64, pubB64, err := appconfig.GenerateKey() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + directSetEnv(t, dir, pubB64) |
| + // Another account owns a fleet with some other key: no bearing. |
| + _, otherFP := directKey(t) |
| + directFleetRecord(t, release.NewDirStore(dir), "xyz", "away", otherFP) |
| + |
| + runCapturingRealStdout(t, func() { |
| + runSecretsSet([]string{"-account-id", "bab", "-app", "shop", "API_KEY=hunter2"}) |
| + }) |
| + m, err := appconfig.Get(context.Background(), release.NewDirStore(dir), "bab", "shop") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + v := m.Vars["API_KEY"] |
| + fp, _ := appconfig.PublicFingerprint(pubB64) |
| + if v.SealedTo != fp { |
| + t.Fatalf("sealed_to = %q, want the terminal key's %s", v.SealedTo, fp) |
| + } |
| + if !appconfig.EnvelopeBound(v.Envelope) { |
| + t.Fatal("direct mode with the v2 switch on sealed v1") |
| + } |
| + priv, _ := appconfig.ParsePrivateKey(privB64, "test") |
| + if got, err := appconfig.Open(priv, appconfig.Binding{Account: "bab", App: "shop"}, v.Envelope); err != nil || string(got) != "hunter2" { |
| + t.Fatalf("open = %q %v", got, err) |
| + } |
| +} |
| + |
| +// TestDirectSecretsSetRefusalWritesNothing runs the real command (a refusal |
| +// is log.Fatal, so in a child process) and checks it names the fix and leaves |
| +// the app's record alone. |
| +func TestDirectSecretsSetRefusalWritesNothing(t *testing.T) { |
| + if dir := os.Getenv("DIRECT_REFUSAL_DIR"); dir != "" { |
| + runSecretsSet([]string{"-account-id", "bab", "-app", "shop", "API_KEY=hunter2"}) |
| + return |
| + } |
| + dir := t.TempDir() |
| + _, pubB64, err := appconfig.GenerateKey() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + directFleetRecord(t, release.NewDirStore(dir), "bab", "home") |
| + cmd := exec.Command(os.Args[0], "-test.run=^TestDirectSecretsSetRefusalWritesNothing$") |
| + cmd.Env = append(os.Environ(), |
| + "DIRECT_REFUSAL_DIR="+dir, |
| + "CARLOS_DEPLOYMENT_DIR="+dir, |
| + "CARLOS_DEPLOYMENT_BUCKET=", |
| + "CARLOS_CONSOLE=", |
| + "CARLOS_SERVICE_TOKEN=", |
| + "CARLOS_SECRETS_PUBKEY="+pubB64, |
| + "HOME="+t.TempDir(), |
| + "CARLOS_CREDENTIALS="+filepath.Join(t.TempDir(), "credentials"), |
| + ) |
| + out, err := cmd.CombinedOutput() |
| + if err == nil { |
| + t.Fatalf("the command succeeded:\n%s", out) |
| + } |
| + if !strings.Contains(string(out), strings.ReplaceAll(wantDirectRefuseOwnsFleet, "<app>", "shop")) { |
| + t.Fatalf("output = %q, want the refusal naming shop", out) |
| + } |
| + if m, err := appconfig.Get(context.Background(), release.NewDirStore(dir), "bab", "shop"); err == nil && len(m.Vars) != 0 { |
| + t.Fatalf("a refused set wrote the app's record: %+v", m) |
| + } |
| +} |
diff --git a/approved_fleet_secrets_copy_test.go b/approved_fleet_secrets_copy_test.go| index 582fd1fdb..28f9c88a3 100644 |
| --- a/approved_fleet_secrets_copy_test.go |
| +++ b/approved_fleet_secrets_copy_test.go |
| @@ -69,7 +69,7 @@ var fleetSecretsCopyBuilt = []string{ |
| "FSK-C33", "FSK-C34", "FSK-C35", "FSK-C36", |
| "FSK-C37", "FSK-C38", "FSK-C39", "FSK-C40", |
| "FSK-C41", "FSK-C42", "FSK-C43", "FSK-C44", |
| - "FSK-C45", "FSK-C46", "FSK-C47", |
| + "FSK-C45", "FSK-C46", "FSK-C47", "FSK-C48", "FSK-C49", "FSK-C50", |
| } |
| func mustParseFleetSecretsRecord(t *testing.T) []copyRow { |
diff --git a/internal/console/sealkey_guard_test.go b/internal/console/sealkey_guard_test.go| index 9e0559d12..120dd6310 100644 |
| --- a/internal/console/sealkey_guard_test.go |
| +++ b/internal/console/sealkey_guard_test.go |
| @@ -87,8 +87,10 @@ var sealGuardAllowed = map[string][]string{ |
| "(*" + sgConsole + ".Server).configCheckSealedTo": {sgChooseSealKey}, |
| // The CLI, sealing to the key the console's config GET served. |
| sgModule + ".sealSecretAPI": {sgSealBound, sgSealUnbound}, |
| - // The CLI's direct-bucket mode (Task 8 adds its key checks). |
| - sgModule + ".runSecretsSet": {sgSeal}, |
| + // The CLI's direct-bucket mode: it builds one Sealer for the key in the |
| + // operator's terminal, after directSealRefusal has refused a fleet's key |
| + // and any account that owns a fleet. |
| + sgModule + ".runSecretsSet": {sgNewSealer}, |
| // A platform move's archive, sealed to the deployment key on the box |
| // (fleet instances are not moved). |
| sgArchiveSeal: {sgSealBound}, |