diff --git a/CHANGELOG.md b/CHANGELOG.md index a90fd937b..5c1a06274 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **`notion.token` is kept out of the Dolt database** + ([#6676](https://github.com/gastownhall/beads/issues/6676)). It was missing + from the yaml-only key list that holds the other tracker secrets, so + `bd config set notion.token` — the command `bd notion status` recommends — + wrote it to the database, whose contents `bd dolt push` sends to the remote. + It is now written to `config.yaml` like `github.token`, `gitlab.token` and + the other tracker secrets, and refused on a git-tracked `config.yaml` with a + pointer to `NOTION_TOKEN`. A token an older bd already stored in the database + is still read, after `config.yaml` and before `NOTION_TOKEN`, so existing + setups keep authenticating. `bd config unset notion.token` removes both + copies — the `config.yaml` entry and any row an older bd left in the database + — and reports which of them it actually removed; that cleanup covers every + yaml-only secret key, not just this one. The database half is best effort and + says so: it is skipped only where reaching the row would mean creating a + database (the command never creates one), so it still runs for a server-mode + or proxied workspace whose database is simply not on local disk, and it is + attempted even when the `config.yaml` edit fails, so a workspace whose only + copy is the stored row still has a remover. `bd notion status` now reports the + auth source as `database_legacy` rather than `config_token` while the stored + row is the one authenticating, so an affected workspace can identify itself. + A token that was already pushed should still be rotated: deleting the row + locally does not unpublish it from remotes that already have a copy. + - **`bd -C dir prime` now describes the target workspace instead of the launch directory** ([#5509](https://github.com/gastownhall/beads/issues/5509)). `-C` resolves `BEADS_DIR` but never changes directory, so prime's cwd-relative @@ -76,6 +99,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `bd mol burn`, `bd duplicates --merge`, and the wisp writes — closes, updates, deletes and demote-to-wisp. + - **`bd list --watch --format` is refused instead of silently dropping the format** ([#6277](https://github.com/gastownhall/beads/issues/6277)). `--watch` always re-renders the pretty listing, so on the direct route a diff --git a/cmd/bd/config.go b/cmd/bd/config.go index b2adf7426..3982deef1 100644 --- a/cmd/bd/config.go +++ b/cmd/bd/config.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "os" "os/exec" @@ -309,6 +310,182 @@ func openWorkspaceConfig(directRequirement string) (issueops.WorkspaceConfig, er return store.WorkspaceConfig() } +// secretRowCleanup is what the best-effort database half of `bd config unset` +// achieved, kept apart from the config.yaml half because the two removals are +// independent. The YAML edit is the command's contract; the row is a credential +// the database replicated to every remote it was pushed to, so "I deleted it", +// "it is still there and I could not delete it" and "I could not look" are +// three different things to tell the user, and only the middle one is certain. +type secretRowCleanup struct { + present bool // a row was there when we read it + removed bool + err error +} + +// describe writes the database half of the report. It says nothing at all for +// the overwhelmingly common workspace that never leaked: `config unset` is +// otherwise a one-line command and this is a footnote about a key's history. +func (c secretRowCleanup) describe(key, location string) { + switch { + case c.removed: + fmt.Printf("Also removed a stored %s row from the database, left by a bd that predates the key moving to %s.\n", key, location) + fmt.Printf("⚠ Rotate this credential: the row was replicated to every Dolt remote this workspace pushed to, and deleting it here does not unpublish it.\n") + case c.err != nil && c.present: + // The one case that is not a maybe: the read found the row and the + // delete failed, so bd knows a live credential is still in the table. + fmt.Fprintf(os.Stderr, "⚠ A stored %s row is still in the database: removing it failed (%v).\n", key, c.err) + fmt.Fprintf(os.Stderr, "⚠ Rotate this credential: the row was replicated to every Dolt remote this workspace pushed to.\n") + case c.err != nil: + fmt.Fprintf(os.Stderr, "⚠ Could not check the database for a leaked %s row (%v). An older bd may have stored one there; re-run this command with the database reachable.\n", key, c.err) + } +} + +// jsonFields carries the same three-way distinction to machine consumers. +// database_row_removed is always present for a secret key so a script can tell +// "nothing leaked" from "the field is missing because bd is older". +func (c secretRowCleanup) jsonFields(payload map[string]interface{}) { + payload["database_row_removed"] = c.removed + switch { + case c.err != nil && c.present: + payload["database_row_present"] = true + payload["database_delete_failed"] = c.err.Error() + case c.err != nil: + payload["database_unreachable"] = c.err.Error() + } +} + +// rowNote is the database half folded into the error the YAML half returns, so +// a failed `config unset` still tells the user where the credential stands. It +// goes on stderr with that error rather than to stdout, which `--json` owns. +func (c secretRowCleanup) rowNote(key string) string { + switch { + case c.removed: + return fmt.Sprintf("\nA stored %s row WAS removed from the database; rotate that credential — it was replicated to every Dolt remote this workspace pushed to.", key) + case c.err != nil && c.present: + return fmt.Sprintf("\nA stored %s row is still in the database: removing it failed (%v).", key, c.err) + case c.err != nil: + return fmt.Sprintf("\nThe database could not be checked for a leaked %s row (%v).", key, c.err) + } + return "" +} + +// mayProvisionDatabase reports whether reaching the store from here could have +// to CREATE a database, which is the only thing unsetLeakedSecretRow's skip +// exists to avoid. Each arm that returns false is a route on which the database +// is already there, so consulting it provisions nothing: +// +// - the proxied route opens no local storage at all — proxiedWorkspaceConfig +// reaches an already-connected provider; +// - a store that is already open and tracked is ensureStoreActive's own +// no-op arm: it returns before newDoltStoreFromConfig ever runs. This is +// the state PersistentPreRun leaves a connected server-mode workspace in, +// and it is the only thing that tells the metadata-less server shape — +// `dolt.mode: server` or BEADS_DOLT_SERVER_HOST with no local database +// directory, routed deliberately at main.go, GH#3545 — apart from a +// workspace that genuinely has no database. beads.FindDatabasePath answers +// on metadata.json plus embeddeddolt/ and dolt/, so for that shape it +// reports "" while the row itself sits on the shared server, replicated to +// every remote the workspace ever pushed to. Skipping there would strand +// the leaked credential in the population that holds the most copies of it. +// +// Only when neither of those holds does the on-disk answer decide, and there +// the open really is open-OR-CREATE. +func mayProvisionDatabase() bool { + if usesProxiedServer() { + return false + } + lockStore() + active := isStoreActive() && getStore() != nil + unlockStore() + if active { + return false + } + return beads.FindDatabasePath() == "" +} + +// unsetLeakedSecretRow deletes a yaml-only SECRET key's leftover row from the +// config table, reporting what it found and what it removed. +// +// WHY THE YAML UNSET IS NOT THE WHOLE JOB. A key becomes yaml-only at some +// point in bd's history, not at the beginning: `notion.token` moved in GH#6676, +// and every workspace configured before the move still has the value in the +// config table — a table whose contents `bd dolt push` replicates to every +// remote. `config unset`'s yaml-only branch returns before the store, so +// without this the only bd-side remover of that row would be gone for embedded +// workspaces, which are exactly the population the move protects. The row would +// keep authenticating (ResolveAuth reads it as the upgrade path) while the user +// who just ran `bd config unset` believes they are clean. +// +// IT IS BEST EFFORT, AND ONLY FOR SECRETS. The caller has already removed the +// value from config.yaml, which is the command's contract; a workspace with no +// reachable store must not be told that succeeded work failed. Non-secret +// yaml-only keys are skipped rather than merely unreported, so the ordinary +// `bd config unset routing.mode` keeps costing no database open at all — this +// pays the store-open only where a leaked credential could be sitting. +// +// The predicates, not a list of key names: `IsYamlOnlyKey` matches whole +// prefixes (`ai.`, `sync.`, `federation.`, ...), so the affected set is +// open-ended and grows whenever a tracker adds a credential. Measured at the +// time of writing, YamlOnlyKeys alone contributes seven — ado.pat, +// github.token, gitlab.token, jira.api_token, linear.api_key, +// linear.oauth_client_secret, notion.token — plus whatever the prefixes cover. +// +// IT NEVER PROVISIONS ONE. `config unset ` is deliberately +// classified as runnable with no store (configCommandCanRunWithoutStore in +// main.go, GH#536 / bd-934 / bd-3rw), and the direct open below is +// open-OR-CREATE (ensureDirectMode → ensureStoreActive → +// newDoltStoreFromConfig), so without a gate editing a YAML file would +// materialize a database in a workspace that never had one and flip it out of +// its db-less mode for every later command. mayProvisionDatabase is that gate, +// and it asks about the store before the disk: "no local database directory" is +// not the same question as "no database", and the workspaces where the two +// answers differ are the ones whose row traveled furthest. +func unsetLeakedSecretRow(key string) secretRowCleanup { + // Gate BEFORE opening the store, not inside removeStoredSecretRow: this is + // what keeps `bd config unset routing.mode` from paying a database open it + // has no use for. + if !config.IsSecretKey(key) { + return secretRowCleanup{} + } + if mayProvisionDatabase() { + return secretRowCleanup{} + } + settings, err := openWorkspaceConfig("config unset requires direct database access") + if err != nil { + return secretRowCleanup{err: err} + } + present, removed, err := removeStoredSecretRow(rootCtx, settings, key) + if removed { + noteDirectConfigWrite() + } + return secretRowCleanup{present: present, removed: removed, err: err} +} + +// removeStoredSecretRow deletes key's row, reporting whether one was there and +// whether it is gone. Those are two bits, not one: a failed delete on a row the +// read found is the single outcome that means "a live credential is still in +// the table bd pushes", and collapsing it into the error alone renders it as an +// unreachable database — the one case that needs no hedging. +// +// It READS BEFORE DELETING because UnsetSetting deliberately has no "removed" +// flag and deleting an absent key is a success — so a blind delete could not +// tell the caller whether anything had leaked, and the overwhelmingly common +// case (a workspace that never stored the key) has to stay silent rather than +// announce a removal that did not happen. +func removeStoredSecretRow(ctx context.Context, settings issueops.WorkspaceConfig, key string) (present bool, removed bool, err error) { + stored, err := settings.GetSetting(ctx, issueops.GetSettingRequest{Key: key}) + if err != nil { + return false, false, err + } + if strings.TrimSpace(stored.Value) == "" { + return false, false, nil + } + if _, err := settings.UnsetSetting(ctx, issueops.UnsetSettingRequest{Key: key}); err != nil { + return true, false, err + } + return true, true, nil +} + // noteDirectConfigWrite marks the invocation as having written, which is what // the auto-commit epilogue in main.go keys on. // @@ -600,6 +777,53 @@ func showConfigYAMLOverrides(dbConfig map[string]string) { fmt.Println("\nTip: Run 'bd config show' for all effective config with provenance.") } +// runConfigUnsetYamlOnly removes a yaml-only key from config.yaml and, for a +// secret key, the row a bd that predates the key's move to YAML may have left +// in the config table. +// +// THE TWO REMOVALS ARE INDEPENDENT, AND SO IS THEIR REPORTING. Running the row +// cleanup only after a successful YAML edit would skip it for exactly the +// workspaces the cleanup exists for: one whose config.yaml is missing, or whose +// notion.token sits in a shape the unset refuses, gets an error about +// config.yaml and keeps the credential in the table `bd dolt push` replicates — +// while `bd notion status` names this very command as the remover. So the +// cleanup runs either way, and a failed YAML unset carries the row's fate in +// its error rather than dropping it. +func runConfigUnsetYamlOnly(key string) error { + location := "config.yaml" + var unsetErr error + if config.IsUserGlobalKey(key) { + unsetErr = config.UnsetUserYamlConfig(key) + location = config.UserConfigYamlDisplayPath() + } else { + unsetErr = config.UnsetYamlConfig(key) + } + + cleanup := unsetLeakedSecretRow(key) + + if unsetErr != nil { + return HandleError("unsetting config: %v%s", unsetErr, cleanup.rowNote(key)) + } + + if jsonOutput { + payload := map[string]interface{}{ + "key": key, + "location": location, + } + if config.IsSecretKey(key) { + cleanup.jsonFields(payload) + } + if err := outputJSON(payload); err != nil { + return err + } + } else { + fmt.Printf("Unset %s (in %s)\n", key, location) + cleanup.describe(key, location) + } + printConfigSideEffects(checkConfigUnsetSideEffects(key)) + return nil +} + var configUnsetCmd = &cobra.Command{ Use: "unset ", Short: "Delete a configuration value", @@ -617,30 +841,7 @@ var configUnsetCmd = &cobra.Command{ key := args[0] if config.IsYamlOnlyKey(key) { - location := "config.yaml" - var unsetErr error - if config.IsUserGlobalKey(key) { - unsetErr = config.UnsetUserYamlConfig(key) - location = config.UserConfigYamlDisplayPath() - } else { - unsetErr = config.UnsetYamlConfig(key) - } - if unsetErr != nil { - return HandleError("unsetting config: %v", unsetErr) - } - - if jsonOutput { - if err := outputJSON(map[string]interface{}{ - "key": key, - "location": location, - }); err != nil { - return err - } - } else { - fmt.Printf("Unset %s (in %s)\n", key, location) - } - printConfigSideEffects(checkConfigUnsetSideEffects(key)) - return nil + return runConfigUnsetYamlOnly(key) } if key == "beads.role" { diff --git a/cmd/bd/config_leaked_secret_test.go b/cmd/bd/config_leaked_secret_test.go new file mode 100644 index 000000000..fb4418271 --- /dev/null +++ b/cmd/bd/config_leaked_secret_test.go @@ -0,0 +1,633 @@ +package main + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "reflect" + "sort" + "strings" + "testing" + + "github.com/spf13/cobra" + "github.com/steveyegge/beads/internal/config" + "github.com/steveyegge/beads/internal/notion" + "github.com/steveyegge/beads/internal/storage" + "github.com/steveyegge/beads/issueops" +) + +// recordingWorkspaceConfig is the config plane reduced to the two verbs +// removeStoredSecretRow uses, so a test can assert what it did NOT call. +type recordingWorkspaceConfig struct { + issueops.WorkspaceConfig + + stored map[string]string + getErr error + unsetErr error + + gets []string + unsets []string +} + +func (c *recordingWorkspaceConfig) GetSetting(_ context.Context, req issueops.GetSettingRequest) (issueops.SettingResult, error) { + c.gets = append(c.gets, req.Key) + if c.getErr != nil { + return issueops.SettingResult{}, c.getErr + } + return issueops.SettingResult{Key: req.Key, Value: c.stored[req.Key]}, nil +} + +func (c *recordingWorkspaceConfig) UnsetSetting(_ context.Context, req issueops.UnsetSettingRequest) (issueops.UnsetSettingResult, error) { + c.unsets = append(c.unsets, req.Key) + if c.unsetErr != nil { + return issueops.UnsetSettingResult{}, c.unsetErr + } + delete(c.stored, req.Key) + return issueops.UnsetSettingResult{Key: req.Key}, nil +} + +// TestRemoveStoredSecretRowDeletesTheLeakedRow is the GH#6676 regression: a +// workspace configured before notion.token became yaml-only has the token in +// the config table, which `bd dolt push` replicated. Moving the key to +// config.yaml made `bd config unset` return before the store, which left that +// row with no remover at all in embedded mode. +func TestRemoveStoredSecretRowDeletesTheLeakedRow(t *testing.T) { + settings := &recordingWorkspaceConfig{stored: map[string]string{"notion.token": "secret-ntn-abc"}} + + present, removed, err := removeStoredSecretRow(context.Background(), settings, "notion.token") + if err != nil { + t.Fatalf("removeStoredSecretRow: %v", err) + } + if !present { + t.Error("present = false, want true — the row was there to read") + } + if !removed { + t.Error("removed = false, want true — the caller reports nothing to the user when this is false, so the leaked row would be deleted silently or not at all") + } + if got := settings.unsets; len(got) != 1 || got[0] != "notion.token" { + t.Errorf("unset calls = %v, want exactly [notion.token]", got) + } + if _, still := settings.stored["notion.token"]; still { + t.Error("the row survived the removal") + } +} + +// TestRemoveStoredSecretRowIsSilentWhenNothingLeaked pins the read-before-delete. +// Deleting an absent key is a success that reports no miss, so without the read +// every `bd config unset notion.token` would claim it had cleaned up a database +// row — on the overwhelmingly common workspace that never had one. +func TestRemoveStoredSecretRowIsSilentWhenNothingLeaked(t *testing.T) { + for name, stored := range map[string]map[string]string{ + "no row at all": {}, + "row stored empty": {"notion.token": ""}, + "row of whitespace": {"notion.token": " \n"}, + } { + t.Run(name, func(t *testing.T) { + settings := &recordingWorkspaceConfig{stored: stored} + + present, removed, err := removeStoredSecretRow(context.Background(), settings, "notion.token") + if err != nil { + t.Fatalf("removeStoredSecretRow: %v", err) + } + if present { + t.Error("present = true, want false — nothing usable was stored, so nothing is still in the database") + } + if removed { + t.Error("removed = true, want false — this would tell the user a credential was deleted from the database when none was there") + } + if len(settings.unsets) != 0 { + t.Errorf("unset calls = %v, want none: the row was already absent", settings.unsets) + } + }) + } +} + +// TestRemoveStoredSecretRowPropagatesFailures keeps a failed cleanup +// distinguishable from a clean workspace. Both report removed=false, so only +// the error tells the caller to warn instead of staying quiet — reporting +// success here is the exact harm GH#6676 is about: the user believes the +// secret is gone while it is still in the pushed database. +// +// The two failures are NOT the same warning. A failed read means bd does not +// know whether anything leaked; a failed delete means it does, and the +// credential is still in the table `bd dolt push` replicates. That is what the +// `present` bit carries out to the caller. +func TestRemoveStoredSecretRowPropagatesFailures(t *testing.T) { + readFailed := errors.New("store unreachable") + deleteFailed := errors.New("delete rejected") + + t.Run("read fails", func(t *testing.T) { + settings := &recordingWorkspaceConfig{stored: map[string]string{}, getErr: readFailed} + + present, removed, err := removeStoredSecretRow(context.Background(), settings, "notion.token") + if !errors.Is(err, readFailed) { + t.Fatalf("err = %v, want %v", err, readFailed) + } + if present { + t.Error("present = true after a failed read — the read is what would have told us, so this claims knowledge bd does not have") + } + if removed { + t.Error("removed = true after a failed read") + } + if len(settings.unsets) != 0 { + t.Errorf("deleted %v despite not knowing what was there", settings.unsets) + } + }) + + t.Run("delete fails", func(t *testing.T) { + settings := &recordingWorkspaceConfig{ + stored: map[string]string{"notion.token": "secret-ntn-abc"}, + unsetErr: deleteFailed, + } + + present, removed, err := removeStoredSecretRow(context.Background(), settings, "notion.token") + if !errors.Is(err, deleteFailed) { + t.Fatalf("err = %v, want %v", err, deleteFailed) + } + if !present { + t.Error("present = false, want true — the read found the row, so this is the certain case, not an inconclusive lookup") + } + if removed { + t.Error("removed = true, want false — the row is still in the database") + } + }) +} + +// TestYamlOnlySecretKeysAreTheWholeClass pins the population the unset branch +// cleans up. The fix is deliberately keyed on the IsYamlOnlyKey/IsSecretKey +// pair rather than on "notion.token", because every tracker credential reached +// yaml-only status the same way and would otherwise each need their own fix. +// +// IsYamlOnlyKey also matches whole prefixes (`ai.`, `sync.`, `federation.`, +// ...), so this list is a floor, not a census. +func TestYamlOnlySecretKeysAreTheWholeClass(t *testing.T) { + for _, key := range []string{ + "notion.token", + "github.token", + "gitlab.token", + "jira.api_token", + "linear.api_key", + "linear.oauth_client_secret", + "ado.pat", + "ai.api_key", + } { + if !config.IsYamlOnlyKey(key) { + t.Errorf("%s: IsYamlOnlyKey = false, so config unset never reaches the branch this fix lives in", key) + } + if !config.IsSecretKey(key) { + t.Errorf("%s: IsSecretKey = false, so its leaked database row would be left behind", key) + } + } + + // The other side of the gate: an ordinary yaml-only key must not drag a + // database open into a command that has no row to clean up. + for _, key := range []string{"routing.mode", "backup.enabled", "no-db"} { + if !config.IsYamlOnlyKey(key) { + t.Errorf("%s: expected a yaml-only key for this half of the test", key) + } + if config.IsSecretKey(key) { + t.Errorf("%s: IsSecretKey = true, which would open the store on an ordinary unset", key) + } + } +} + +// TestNotionStatusWarnsOnlyForTheLeakedSource is the user-visible half of the +// same GH#6676 gap. The distinct auth source is only worth having if something +// acts on it: this is the one place a workspace is told that the token it is +// authenticating with sits in a table `bd dolt push` replicates. +// +// The negative cases matter as much as the positive one — warning a workspace +// whose token is in config.yaml or the environment would train users to ignore +// the line that matters. +func TestNotionStatusWarnsOnlyForTheLeakedSource(t *testing.T) { + for _, tc := range []struct { + name string + source notion.AuthSource + wantWarn bool + }{ + {"legacy database row", notion.AuthSourceDatabaseLegacy, true}, + {"config.yaml", notion.AuthSourceConfigToken, false}, + {"environment", notion.AuthSourceEnv, false}, + } { + t.Run(tc.name, func(t *testing.T) { + var out bytes.Buffer + cmd := &cobra.Command{} + cmd.SetOut(&out) + + renderNotionStatus(cmd, + ¬ion.ResolvedAuth{Token: "secret-ntn-abcdef", Source: tc.source}, + notionConfig{DataSourceID: "ds-1"}, + ¬ion.StatusResponse{}) + + got := out.String() + if warned := strings.Contains(got, "bd config unset notion.token"); warned != tc.wantWarn { + t.Errorf("rotation warning present = %v, want %v; output:\n%s", warned, tc.wantWarn, got) + } + if !strings.Contains(got, "Auth source: "+string(tc.source)) { + t.Errorf("auth source line missing for %q; output:\n%s", tc.source, got) + } + if strings.Contains(got, "secret-ntn-abcdef") { + t.Errorf("status printed the raw token; output:\n%s", got) + } + }) + } +} + +// leakedRowStore is the production store reduced to the one accessor the +// cleanup reaches through. Its nil embedded storage makes any other call panic, +// so these tests cannot silently exercise more of the store than the seam. +type leakedRowStore struct { + storage.DoltStorage + settings *recordingWorkspaceConfig +} + +func (s *leakedRowStore) WorkspaceConfig() (issueops.WorkspaceConfig, error) { + return s.settings, nil +} + +// newLeakedSecretWorkspace builds the workspace shapes GH#6676 produced: a +// .beads directory that may or may not hold a config.yaml, and may or may not +// hold a database directory — which is what FindDatabasePath answers on, and +// therefore what decides whether the cleanup may open a store at all. +func newLeakedSecretWorkspace(t *testing.T, configYAML string, withDatabase bool) string { + t.Helper() + beadsDir := filepath.Join(t.TempDir(), ".beads") + if err := os.MkdirAll(beadsDir, 0o755); err != nil { + t.Fatalf("create .beads: %v", err) + } + if configYAML != "" { + if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configYAML), 0o600); err != nil { + t.Fatalf("write config.yaml: %v", err) + } + } + if withDatabase { + if err := os.MkdirAll(filepath.Join(beadsDir, "embeddeddolt"), 0o755); err != nil { + t.Fatalf("create embeddeddolt: %v", err) + } + } + t.Setenv("BEADS_DIR", beadsDir) + initConfigForTest(t) + return beadsDir +} + +// installLeakedRowSettings puts the recording config plane behind the store +// accessor and marks the store active, so openWorkspaceConfig hands it back +// without opening — or creating — anything. +func installLeakedRowSettings(t *testing.T, settings *recordingWorkspaceConfig) { + t.Helper() + saveAndRestoreGlobals(t) + store = &leakedRowStore{settings: settings} + setStoreActive(true) +} + +// runConfigUnset drives the real cobra command, which is the point: every other +// test in this file stubs in below the wiring. +func runConfigUnset(t *testing.T, key string, asJSON bool) (stdout, stderr string, err error) { + t.Helper() + savedJSON := jsonOutput + jsonOutput = asJSON + t.Cleanup(func() { jsonOutput = savedJSON }) + + stdout, stderr = captureStdio(t, func() { + err = configUnsetCmd.RunE(configUnsetCmd, []string{key}) + }) + return stdout, stderr, err +} + +func captureStdio(t *testing.T, fn func()) (string, string) { + t.Helper() + rOut, wOut, err := os.Pipe() + if err != nil { + t.Fatalf("pipe: %v", err) + } + rErr, wErr, err := os.Pipe() + if err != nil { + t.Fatalf("pipe: %v", err) + } + savedOut, savedErr := os.Stdout, os.Stderr + os.Stdout, os.Stderr = wOut, wErr + func() { + defer func() { os.Stdout, os.Stderr = savedOut, savedErr }() + fn() + }() + _ = wOut.Close() + _ = wErr.Close() + + var outBuf, errBuf bytes.Buffer + if _, err := io.Copy(&outBuf, rOut); err != nil { + t.Fatalf("read stdout: %v", err) + } + if _, err := io.Copy(&errBuf, rErr); err != nil { + t.Fatalf("read stderr: %v", err) + } + _ = rOut.Close() + _ = rErr.Close() + return outBuf.String(), errBuf.String() +} + +// snapshotBeadsStorage is the "did this command create a database" probe: every +// path under .beads with its size, so a new embeddeddolt/ directory, a gate +// lock or a grown file shows up as a difference instead of having to be +// predicted. +// +// config.yaml is excluded because editing it IS the command's contract; every +// other byte under .beads is storage that `bd config unset` has no business +// bringing into existence. removed_backend_heal_embedded_test.go has a +// `cgo`-tagged twin of this walk; this file must also compile in the +// CGO_ENABLED=0 lane, which does not see that one. +func snapshotBeadsStorage(t *testing.T, beadsDir string) []string { + t.Helper() + var entries []string + if err := filepath.Walk(beadsDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + rel, relErr := filepath.Rel(beadsDir, path) + if relErr != nil { + return relErr + } + if info.IsDir() { + entries = append(entries, rel+"/") + return nil + } + if rel == "config.yaml" { + return nil + } + entries = append(entries, fmt.Sprintf("%s (%d bytes)", rel, info.Size())) + return nil + }); err != nil { + t.Fatalf("walk %s: %v", beadsDir, err) + } + sort.Strings(entries) + return entries +} + +// TestConfigUnsetCommandCleansTheLeakedRow pins the wiring, which is the one +// thing the unit tests above cannot see: they call removeStoredSecretRow +// directly, so deleting the cleanup call in the command left them all green. +// +// The negative half is the same assertion the cleanup's comment makes about +// itself — an ordinary yaml-only key must not reach the store at all — and it +// is checked against a recorder that fails on any unexpected call. +func TestConfigUnsetCommandCleansTheLeakedRow(t *testing.T) { + t.Run("secret key: the row is read, deleted and reported", func(t *testing.T) { + beadsDir := newLeakedSecretWorkspace(t, "notion:\n token: yaml-copy\n", true) + settings := &recordingWorkspaceConfig{stored: map[string]string{"notion.token": "leaked-db-value"}} + installLeakedRowSettings(t, settings) + + stdout, _, err := runConfigUnset(t, "notion.token", false) + if err != nil { + t.Fatalf("config unset notion.token: %v", err) + } + if got := settings.gets; len(got) != 1 || got[0] != "notion.token" { + t.Errorf("GetSetting calls = %v, want exactly [notion.token]", got) + } + if got := settings.unsets; len(got) != 1 || got[0] != "notion.token" { + t.Errorf("UnsetSetting calls = %v, want exactly [notion.token] — the command never reached the cleanup", got) + } + if !strings.Contains(stdout, "Also removed a stored notion.token row") { + t.Errorf("the removal was not reported; stdout:\n%s", stdout) + } + if !strings.Contains(stdout, "Rotate this credential") { + t.Errorf("the rotation obligation was not reported; stdout:\n%s", stdout) + } + if yaml := readWorkspaceYAML(t, beadsDir); strings.Contains(yaml, "\n token: yaml-copy") { + t.Errorf("the config.yaml copy survived:\n%s", yaml) + } + }) + + t.Run("non-secret yaml-only key: the store is never reached", func(t *testing.T) { + newLeakedSecretWorkspace(t, "routing:\n mode: hybrid\n", true) + settings := &recordingWorkspaceConfig{stored: map[string]string{"notion.token": "leaked-db-value"}} + installLeakedRowSettings(t, settings) + + if _, _, err := runConfigUnset(t, "routing.mode", false); err != nil { + t.Fatalf("config unset routing.mode: %v", err) + } + if len(settings.gets)+len(settings.unsets) != 0 { + t.Errorf("an ordinary yaml-only unset touched the config plane: gets=%v unsets=%v", settings.gets, settings.unsets) + } + }) + + t.Run("--json carries the database half", func(t *testing.T) { + newLeakedSecretWorkspace(t, "notion:\n token: yaml-copy\n", true) + settings := &recordingWorkspaceConfig{stored: map[string]string{"notion.token": "leaked-db-value"}} + installLeakedRowSettings(t, settings) + + stdout, _, err := runConfigUnset(t, "notion.token", true) + if err != nil { + t.Fatalf("config unset --json: %v", err) + } + var payload map[string]interface{} + if err := json.Unmarshal([]byte(stdout), &payload); err != nil { + t.Fatalf("unmarshal %q: %v", stdout, err) + } + if payload["database_row_removed"] != true { + t.Errorf("database_row_removed = %v, want true; payload: %v", payload["database_row_removed"], payload) + } + }) +} + +// TestConfigUnsetDoesNotProvisionADatabase is the store-creation regression. +// `config unset ` is classified runnable with no store, and the +// direct open is open-OR-CREATE, so a best-effort cleanup that reaches for the +// store unconditionally turns a YAML edit into a 2.1 MB embedded database in a +// workspace that never had one. +// +// Both assertions are load-bearing, because removing the gate can fail either +// way: the open succeeds and materializes the database (caught by the directory +// snapshot), or it fails and the command reports an unreachable database +// (caught by the stderr assertion). Nothing about a workspace with no database +// warrants either. +func TestConfigUnsetDoesNotProvisionADatabase(t *testing.T) { + beadsDir := newLeakedSecretWorkspace(t, "notion:\n token: yaml-copy\n", false) + saveAndRestoreGlobals(t) + store = nil + setStoreActive(false) + + before := snapshotBeadsStorage(t, beadsDir) + + stdout, stderr, err := runConfigUnset(t, "notion.token", false) + if err != nil { + t.Fatalf("config unset notion.token: %v", err) + } + if after := snapshotBeadsStorage(t, beadsDir); !reflect.DeepEqual(before, after) { + t.Errorf("unsetting a yaml-only secret changed .beads/:\nbefore: %v\nafter: %v", before, after) + } + if strings.Contains(stderr, "Could not check the database") { + t.Errorf("the command tried to reach a database that does not exist; stderr:\n%s", stderr) + } + if !strings.Contains(stdout, "Unset notion.token") { + t.Errorf("the YAML half did not run; stdout:\n%s", stdout) + } +} + +// TestConfigUnsetCleansTheRowWithNoLocalDatabaseDirectory pins the two arms of +// the provisioning gate that the snapshot test above cannot see. It is that +// test's mirror: "no local database directory" is the state the three share, so +// the route to the database is the only thing that distinguishes them, and +// neither arm had a behavioral test — the clause a later simplification is most +// likely to read as redundant was the one clause nothing defended. +// +// Both shapes are workspaces whose database is real and remote. That is the +// population whose row `bd dolt push` replicated the furthest, so a gate that +// answers "no database here" from the local disk silently skips the cleanup +// exactly where it matters most, and exits 0 while it does. +func TestConfigUnsetCleansTheRowWithNoLocalDatabaseDirectory(t *testing.T) { + // The metadata-less server workspace: config.yaml names the server, there is + // nothing on disk to discover (FindDatabasePath answers on metadata.json, + // embeddeddolt/ and dolt/ — none of which exist here), and PersistentPreRun + // has already connected the store. gc provisions exactly this shape on every + // `gc-beads-bd start` — see metadataless_server_workspace_test.go. + t.Run("server mode, store already connected: the row is read and deleted", func(t *testing.T) { + beadsDir := newLeakedSecretWorkspace(t, "dolt:\n mode: server\n server_host: 127.0.0.1\nnotion:\n token: yaml-copy\n", false) + settings := &recordingWorkspaceConfig{stored: map[string]string{"notion.token": "leaked-db-value"}} + installLeakedRowSettings(t, settings) + + before := snapshotBeadsStorage(t, beadsDir) + + stdout, _, err := runConfigUnset(t, "notion.token", false) + if err != nil { + t.Fatalf("config unset notion.token: %v", err) + } + if got := settings.gets; len(got) != 1 || got[0] != "notion.token" { + t.Errorf("GetSetting calls = %v, want exactly [notion.token] — the cleanup was skipped for a server workspace whose row is on the server", got) + } + if got := settings.unsets; len(got) != 1 || got[0] != "notion.token" { + t.Errorf("UnsetSetting calls = %v, want exactly [notion.token] — the leaked row was left on the shared server", got) + } + if !strings.Contains(stdout, "Also removed a stored notion.token row") { + t.Errorf("the removal was not reported; stdout:\n%s", stdout) + } + // The gate's purpose still has to hold on this arm: reaching an + // already-connected store must not bring a local database into being. + if after := snapshotBeadsStorage(t, beadsDir); !reflect.DeepEqual(before, after) { + t.Errorf("cleaning the row provisioned local storage:\nbefore: %v\nafter: %v", before, after) + } + }) + + // The proxied route, which has no local database directory by construction. + // The store is deliberately left inactive so this subtest exercises the + // proxied arm alone rather than the connected-store arm above; the proxied + // provider is not initialized in-process, so "reached" shows up as the + // best-effort could-not-check report. Silence is the failure being pinned. + t.Run("proxied server: the cleanup is reached, not silently skipped", func(t *testing.T) { + newLeakedSecretWorkspace(t, "notion:\n token: yaml-copy\n", false) + saveAndRestoreGlobals(t) + store = nil + setStoreActive(false) + origProxied := proxiedServerMode + t.Cleanup(func() { proxiedServerMode = origProxied }) + proxiedServerMode = true + + stdout, stderr, err := runConfigUnset(t, "notion.token", false) + if err != nil { + t.Fatalf("config unset notion.token: %v", err) + } + if !strings.Contains(stderr, "Could not check the database") { + t.Errorf("the proxied workspace's row was never looked for; stderr:\n%s", stderr) + } + if !strings.Contains(stdout, "Unset notion.token") { + t.Errorf("the YAML half did not run; stdout:\n%s", stdout) + } + }) +} + +// TestConfigUnsetCleansTheRowWhenTheYamlUnsetFails covers the population the +// cleanup exists for and the YAML half cannot serve: a workspace whose token +// leaked into the database and whose config.yaml is gone (or holds a shape the +// unset refuses). Gating the cleanup on the YAML edit succeeding leaves the +// credential in the table `bd dolt push` replicates while telling the user to +// run `bd init` — and `bd notion status` points at this very command. +func TestConfigUnsetCleansTheRowWhenTheYamlUnsetFails(t *testing.T) { + newLeakedSecretWorkspace(t, "", true) + settings := &recordingWorkspaceConfig{stored: map[string]string{"notion.token": "leaked-db-value"}} + installLeakedRowSettings(t, settings) + + _, stderr, err := runConfigUnset(t, "notion.token", false) + if err == nil { + t.Fatal("config unset succeeded with no config.yaml, want the YAML failure to still be reported") + } + if got := settings.unsets; len(got) != 1 || got[0] != "notion.token" { + t.Errorf("UnsetSetting calls = %v, want exactly [notion.token] — the leaked row survived a failure on the other copy", got) + } + if !strings.Contains(stderr, "stored notion.token row WAS removed") { + t.Errorf("the error never mentions the database row; stderr:\n%s", stderr) + } + if !strings.Contains(stderr, "rotate that credential") { + t.Errorf("the rotation obligation was dropped on the failure path; stderr:\n%s", stderr) + } +} + +// TestConfigUnsetDistinguishesADeleteFailure keeps the one certain case out of +// the hedged one. A failed read means bd does not know whether anything leaked; +// a failed delete means it read the row and it is still there — the single +// outcome that warrants "a live credential is in the pushed table", and the one +// that was being rendered as an unreachable database. +func TestConfigUnsetDistinguishesADeleteFailure(t *testing.T) { + deleteFailed := errors.New("delete rejected") + + t.Run("human output names the surviving row", func(t *testing.T) { + newLeakedSecretWorkspace(t, "notion:\n token: yaml-copy\n", true) + settings := &recordingWorkspaceConfig{ + stored: map[string]string{"notion.token": "leaked-db-value"}, + unsetErr: deleteFailed, + } + installLeakedRowSettings(t, settings) + + _, stderr, err := runConfigUnset(t, "notion.token", false) + if err != nil { + t.Fatalf("config unset notion.token: %v", err) + } + if !strings.Contains(stderr, "still in the database") { + t.Errorf("a failed delete was not reported as a surviving row; stderr:\n%s", stderr) + } + if strings.Contains(stderr, "Could not check the database") { + t.Errorf("a failed delete was reported as an inconclusive lookup; stderr:\n%s", stderr) + } + if !strings.Contains(stderr, "Rotate this credential") { + t.Errorf("the rotation obligation was dropped; stderr:\n%s", stderr) + } + }) + + t.Run("--json separates present-but-undeleted from unreachable", func(t *testing.T) { + newLeakedSecretWorkspace(t, "notion:\n token: yaml-copy\n", true) + settings := &recordingWorkspaceConfig{ + stored: map[string]string{"notion.token": "leaked-db-value"}, + unsetErr: deleteFailed, + } + installLeakedRowSettings(t, settings) + + stdout, _, err := runConfigUnset(t, "notion.token", true) + if err != nil { + t.Fatalf("config unset --json: %v", err) + } + var payload map[string]interface{} + if err := json.Unmarshal([]byte(stdout), &payload); err != nil { + t.Fatalf("unmarshal %q: %v", stdout, err) + } + if payload["database_row_present"] != true { + t.Errorf("database_row_present = %v, want true; payload: %v", payload["database_row_present"], payload) + } + if _, claimed := payload["database_unreachable"]; claimed { + t.Errorf("a reachable database that refused the delete was reported unreachable; payload: %v", payload) + } + if got, ok := payload["database_delete_failed"].(string); !ok || !strings.Contains(got, "delete rejected") { + t.Errorf("database_delete_failed = %v, want the delete error; payload: %v", payload["database_delete_failed"], payload) + } + }) +} + +func readWorkspaceYAML(t *testing.T, beadsDir string) string { + t.Helper() + content, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml")) + if err != nil { + t.Fatalf("read config.yaml: %v", err) + } + return string(content) +} diff --git a/cmd/bd/notion.go b/cmd/bd/notion.go index 495a80892..664cd31f6 100644 --- a/cmd/bd/notion.go +++ b/cmd/bd/notion.go @@ -197,15 +197,16 @@ func resolveNotionAuth(ctx context.Context) (*notion.ResolvedAuth, error) { return notion.ResolveAuth(ctx, tempStore) } } - if token := strings.TrimSpace(os.Getenv("NOTION_TOKEN")); token != "" { - return ¬ion.ResolvedAuth{Token: token, Source: notion.AuthSourceEnv}, nil - } - return nil, nil + return notion.ResolveAuth(ctx, nil) } func validateNotionConfig(cfg notionConfig, auth *notion.ResolvedAuth) error { if auth == nil || strings.TrimSpace(auth.Token) == "" { - return fmt.Errorf("Notion authentication is not configured. Set notion.token with 'bd config set notion.token ', or export NOTION_TOKEN") + // NOTION_TOKEN leads deliberately: `bd config set notion.token` is + // refused when .beads/config.yaml is git-tracked, which is the default + // after `bd init` in a repo — so the first suggestion used to be the + // one that does not work. Matches the quickstart's ordering. + return fmt.Errorf("Notion authentication is not configured. Run 'export NOTION_TOKEN=', or 'bd config set notion.token ' if .beads/config.yaml is not tracked by git") } if cfg.DataSourceID == "" { return fmt.Errorf("notion.data_source_id is not configured. Run 'bd notion init --parent ' or 'bd notion connect --url ', or set it directly via bd config set notion.data_source_id or NOTION_DATA_SOURCE_ID") @@ -215,7 +216,11 @@ func validateNotionConfig(cfg notionConfig, auth *notion.ResolvedAuth) error { func validateNotionToken(auth *notion.ResolvedAuth) error { if auth == nil || strings.TrimSpace(auth.Token) == "" { - return fmt.Errorf("Notion authentication is not configured. Set notion.token with 'bd config set notion.token ', or export NOTION_TOKEN") + // NOTION_TOKEN leads deliberately: `bd config set notion.token` is + // refused when .beads/config.yaml is git-tracked, which is the default + // after `bd init` in a repo — so the first suggestion used to be the + // one that does not work. Matches the quickstart's ordering. + return fmt.Errorf("Notion authentication is not configured. Run 'export NOTION_TOKEN=', or 'bd config set notion.token ' if .beads/config.yaml is not tracked by git") } return nil } @@ -389,6 +394,13 @@ func renderNotionStatus(cmd *cobra.Command, auth *notion.ResolvedAuth, cfg notio if auth != nil && auth.Source != "" { _, _ = fmt.Fprintf(out, "Auth source: %s\n", auth.Source) } + // The one place a workspace learns it is the population GH#6676 tells to + // rotate. Without it the CHANGELOG's advice has no addressee: the token + // keeps working, so nothing else ever mentions where it is stored. + if auth != nil && auth.Source == notion.AuthSourceDatabaseLegacy { + _, _ = fmt.Fprintln(out, "⚠ This token is stored in the beads database, which 'bd dolt push' replicates to every remote.") + _, _ = fmt.Fprintln(out, " Rotate it, then run 'bd config unset notion.token' to delete the stored row.") + } _, _ = fmt.Fprintf(out, "Data source: %s\n", cfg.DataSourceID) if cfg.ViewURL != "" { _, _ = fmt.Fprintf(out, "View URL: %s\n", cfg.ViewURL) diff --git a/cmd/bd/notion_test.go b/cmd/bd/notion_test.go index b9dc2baab..9ea546bbd 100644 --- a/cmd/bd/notion_test.go +++ b/cmd/bd/notion_test.go @@ -14,6 +14,7 @@ import ( "testing" "github.com/spf13/cobra" + "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/notion" "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/tracker" @@ -417,7 +418,15 @@ func TestRunNotionStatusUsesHTTPClient(t *testing.T) { } } -func TestResolveNotionAuthPrefersConfigTokenOverEnv(t *testing.T) { +// TestResolveNotionAuthPrefersStoredTokenOverEnv pins the PRECEDENCE, which is +// unchanged: a token in the database still outranks NOTION_TOKEN, so a +// workspace configured before GH#6676 keeps authenticating after the upgrade. +// +// The reported SOURCE did change. Only an older bd can have put this row here, +// so it resolves as AuthSourceDatabaseLegacy rather than AuthSourceConfigToken +// — that is the signal `bd notion status` uses to tell this workspace its token +// was pushed to its remotes and needs rotating. +func TestResolveNotionAuthPrefersStoredTokenOverEnv(t *testing.T) { recorder := ¬ionConfigRecorder{reads: []notionConfigOperation{{key: "notion.token", value: "config-token"}}} installNotionRecorderStore(t, recorder, false) t.Setenv("NOTION_TOKEN", "env-token") @@ -426,12 +435,51 @@ func TestResolveNotionAuthPrefersConfigTokenOverEnv(t *testing.T) { if err != nil { t.Fatalf("resolveNotionAuth returned error: %v", err) } - if auth == nil || auth.Token != "config-token" || auth.Source != notion.AuthSourceConfigToken { + if auth == nil || auth.Token != "config-token" || auth.Source != notion.AuthSourceDatabaseLegacy { t.Fatalf("auth = %+v", auth) } assertNotionConfigCalls(t, recorder, []notionConfigCall{{key: "notion.token"}}, nil, nil) } +// TestResolveNotionAuthPrefersConfigYamlOverStore pins GH#6676: notion.token +// lives in config.yaml, so a yaml token is used without consulting the +// database, where only a token written by an older bd can be found. +func TestResolveNotionAuthPrefersConfigYamlOverStore(t *testing.T) { + recorder := ¬ionConfigRecorder{} + installNotionRecorderStore(t, recorder, false) + initConfigForTest(t) + config.Set("notion.token", "yaml-token") + t.Setenv("NOTION_TOKEN", "env-token") + + auth, err := resolveNotionAuth(context.Background()) + if err != nil { + t.Fatalf("resolveNotionAuth returned error: %v", err) + } + if auth == nil || auth.Token != "yaml-token" || auth.Source != notion.AuthSourceConfigToken { + t.Fatalf("auth = %+v", auth) + } + assertNotionConfigCalls(t, recorder, nil, nil, nil) +} + +// TestResolveNotionAuthReadsConfigYamlWithoutStore verifies the yaml token is +// found even when no store is open and no database path is known. +func TestResolveNotionAuthReadsConfigYamlWithoutStore(t *testing.T) { + saveAndRestoreGlobals(t) + initConfigForTest(t) + store = nil + dbPath = "" + config.Set("notion.token", "yaml-token") + t.Setenv("NOTION_TOKEN", "env-token") + + auth, err := resolveNotionAuth(context.Background()) + if err != nil { + t.Fatalf("resolveNotionAuth returned error: %v", err) + } + if auth == nil || auth.Token != "yaml-token" || auth.Source != notion.AuthSourceConfigToken { + t.Fatalf("auth = %+v", auth) + } +} + func TestRenderNotionSyncResultUsesPhaseStats(t *testing.T) { saveAndRestoreGlobals(t) notionSyncDryRun = true diff --git a/cmd/bd/serve_config_write_proxied_integration_test.go b/cmd/bd/serve_config_write_proxied_integration_test.go index aa4452107..321948e46 100644 --- a/cmd/bd/serve_config_write_proxied_integration_test.go +++ b/cmd/bd/serve_config_write_proxied_integration_test.go @@ -183,8 +183,11 @@ func TestProxiedServerServeSettingsWrites(t *testing.T) { // the write would protect nothing a writer does not already hold; withholding // it from every reader is what redaction actually is. t.Run("a credential key is writable and is never handed back", func(t *testing.T) { + // Not a tracker secret such as notion.token: those are yaml-only keys + // (GH#6676), which `bd config get` below reads from config.yaml rather + // than from the row this PUT writes. const secret = "not-a-real-token-9f13" - status, body := sp.putJSON(t, "/v0/beads/config/notion.token", `{"value":"`+secret+`"}`) + status, body := sp.putJSON(t, "/v0/beads/config/notes.api_token", `{"value":"`+secret+`"}`) if status != http.StatusOK { t.Fatalf("status = %d, want 200 — a credential-bearing key is writable: %v", status, body) } @@ -197,7 +200,7 @@ func TestProxiedServerServeSettingsWrites(t *testing.T) { // The read agrees, and the enumeration agrees: one rule, decided on the // key, for every operation that could carry the value. - readStatus, read, _ := sp.get(t, "/v0/beads/config/notion.token") + readStatus, read, _ := sp.get(t, "/v0/beads/config/notes.api_token") if readStatus != http.StatusOK { t.Fatalf("read back: status = %d", readStatus) } @@ -216,7 +219,7 @@ func TestProxiedServerServeSettingsWrites(t *testing.T) { // And the CLI, which holds the database, prints it: the withholding is // this SURFACE's and not a claim that the value was not stored. - out, err := bdProxiedRun(t, bd, p.dir, "config", "get", "notion.token") + out, err := bdProxiedRun(t, bd, p.dir, "config", "get", "notes.api_token") if err != nil { t.Fatalf("bd config get: %v\n%s", err, out) } diff --git a/docs/getting-started/quickstart.md b/docs/getting-started/quickstart.md index 2a607d530..814e01902 100644 --- a/docs/getting-started/quickstart.md +++ b/docs/getting-started/quickstart.md @@ -269,12 +269,16 @@ See [`bd dolt`](/cli-reference/dolt) for CLI details. For remote configuration, ## Optional: Notion sync -If you keep project issues in Notion, save an integration token first: +If you keep project issues in Notion, supply an integration token first: ```bash +export NOTION_TOKEN= +# or, if .beads/config.yaml is not tracked by git: bd config set notion.token ``` +`bd config set` never writes the token to the Dolt database, so `bd dolt push` does not send it to a remote. It writes the token to `.beads/config.yaml` instead, and refuses when that file is tracked by git, as it is after `bd init` in a git repository. + Then either create a new Beads database under a parent page or connect to an existing target: ```bash @@ -283,7 +287,7 @@ bd notion init --parent bd notion connect --url ``` -The same auth value can also come from `NOTION_TOKEN`. Directly setting `notion.data_source_id` remains available as an escape hatch for advanced setups. +Directly setting `notion.data_source_id` remains available as an escape hatch for advanced setups. Check which auth source is active and whether the target schema is ready: diff --git a/docs/reference/configuration.md b/docs/reference/configuration.md index ede764765..959618da9 100644 --- a/docs/reference/configuration.md +++ b/docs/reference/configuration.md @@ -88,10 +88,12 @@ The full namespaces routed to YAML are: Plus these individual keys: -`no-db`, `json`, `db`, `actor`, `identity`, `no-push`, `no-git-ops`, `agent.profile`, `create.require-description`, `import.auto`, `import.path`, `prime.max-memories`, `prime.max-memory-chars`, and the secret keys `github.token`, `gitlab.token`, `jira.api_token`, `ado.pat`, `linear.api_key`, `linear.oauth_client_id`, `linear.oauth_client_secret`. +`no-db`, `json`, `db`, `actor`, `identity`, `no-push`, `no-git-ops`, `agent.profile`, `create.require-description`, `import.auto`, `import.path`, `prime.max-memories`, `prime.max-memory-chars`, and the secret keys `github.token`, `gitlab.token`, `jira.api_token`, `ado.pat`, `linear.api_key`, `linear.oauth_client_id`, `linear.oauth_client_secret`, `notion.token`. Any key whose name contains `api_key`, `api-key`, `secret`, `token`, or `password` is treated as a secret: it is refused on git-tracked `config.yaml` files unless you pass `--force-git-tracked`. Prefer exporting the value as an environment variable instead (e.g. `LINEAR_API_KEY`). +`bd config unset` on one of these secret keys clears the `config.yaml` entry and also deletes any copy an older bd stored in the database, reporting which of them it removed. Keys reached YAML routing at different points in bd's history, so a workspace configured before the move can still hold the value in a table that `bd dolt push` replicates. The database half is best effort: the command never creates a database to look in, so it is skipped where reaching the row would mean provisioning one — but not for a server-mode or proxied workspace whose database is real and simply not on local disk; a database that refuses the delete is reported as a row that is still stored. Deleting it locally does not unpublish it — rotate a secret that has already been pushed. + ## Tool-Level Settings (config.yaml) | Setting | Flag | Env Var | Default | Description | @@ -415,7 +417,7 @@ federation: ## Integration Configuration -Tracker settings are project-level config under the tracker's namespace; secrets (`jira.api_token`, `linear.api_key`, `github.token`, `gitlab.token`, `ado.pat`) are YAML-routed and better supplied as environment variables. Every tracker records `.last_sync` automatically after a sync, enabling incremental syncs. +Tracker settings are project-level config under the tracker's namespace; secrets (`jira.api_token`, `linear.api_key`, `github.token`, `gitlab.token`, `ado.pat`, `notion.token`) are YAML-routed and better supplied as environment variables. Every tracker records `.last_sync` automatically after a sync, enabling incremental syncs. ### Jira diff --git a/internal/config/yaml_config.go b/internal/config/yaml_config.go index eaa960b86..9429a92ba 100644 --- a/internal/config/yaml_config.go +++ b/internal/config/yaml_config.go @@ -105,6 +105,7 @@ var YamlOnlyKeys = map[string]bool{ "jira.api_token": true, "gitlab.token": true, "ado.pat": true, + "notion.token": true, } // IsYamlOnlyKey returns true if the given key should be stored in config.yaml @@ -218,6 +219,7 @@ var secretKeyEnvVarHints = map[string]string{ //nolint:gosec // Values are envir "ai.api_key": "ANTHROPIC_API_KEY", "github.token": "GITHUB_TOKEN", "linear.api_key": "LINEAR_API_KEY", + "notion.token": "NOTION_TOKEN", } // secretKeyEnvVarHint returns a suggested environment variable name for a diff --git a/internal/config/yaml_config_test.go b/internal/config/yaml_config_test.go index 781503eee..ef413f92a 100644 --- a/internal/config/yaml_config_test.go +++ b/internal/config/yaml_config_test.go @@ -51,6 +51,7 @@ func TestIsYamlOnlyKey(t *testing.T) { // Secret keys (stored in yaml to avoid leaking via Dolt push) {"github.token", true}, {"linear.api_key", true}, + {"notion.token", true}, // GH#6676 // Non-yaml keys (should return false) {"jira.url", false}, @@ -963,6 +964,7 @@ func TestSecretKeyEnvVarHint(t *testing.T) { }{ {"linear.api_key", "LINEAR_API_KEY"}, {"github.token", "GITHUB_TOKEN"}, + {"notion.token", "NOTION_TOKEN"}, {"ai.api_key", "ANTHROPIC_API_KEY"}, {"custom.secret-token", "BD_CUSTOM_SECRET_TOKEN"}, } @@ -1003,18 +1005,23 @@ func TestCheckSecretKeyGitSafety_RefusesGitTrackedSecret(t *testing.T) { } // checkSecretGitTracked should refuse a secret key - err := checkSecretGitTracked(configPath, "linear.api_key") - if err == nil { - t.Fatal("expected error for secret key on git-tracked config, got nil") - } - if !strings.Contains(err.Error(), "refusing to write secret key") { - t.Fatalf("expected 'refusing to write' error, got: %v", err) - } - if !strings.Contains(err.Error(), "LINEAR_API_KEY") { - t.Fatalf("expected env var hint in error, got: %v", err) - } - if !strings.Contains(err.Error(), "--force-git-tracked") { - t.Fatalf("expected --force-git-tracked hint in error, got: %v", err) + for key, envVar := range map[string]string{ + "linear.api_key": "LINEAR_API_KEY", + "notion.token": "NOTION_TOKEN", // GH#6676 + } { + err := checkSecretGitTracked(configPath, key) + if err == nil { + t.Fatalf("expected error for secret key %q on git-tracked config, got nil", key) + } + if !strings.Contains(err.Error(), "refusing to write secret key") { + t.Fatalf("expected 'refusing to write' error for %q, got: %v", key, err) + } + if !strings.Contains(err.Error(), envVar) { + t.Fatalf("expected env var hint %s in error, got: %v", envVar, err) + } + if !strings.Contains(err.Error(), "--force-git-tracked") { + t.Fatalf("expected --force-git-tracked hint in error, got: %v", err) + } } } @@ -1042,7 +1049,7 @@ func TestCheckSecretKeyGitSafety_AllowsDatabaseBackedSecretKey(t *testing.T) { } // Secret-looking keys that are not YAML-backed do not write to config.yaml. - err := checkSecretGitTracked(configPath, "notion.token") + err := checkSecretGitTracked(configPath, "custom.api_token") if err != nil { t.Fatalf("expected no error for database-backed secret key, got: %v", err) } diff --git a/internal/notion/auth.go b/internal/notion/auth.go index 209eb9927..f8c3bb6c4 100644 --- a/internal/notion/auth.go +++ b/internal/notion/auth.go @@ -4,6 +4,8 @@ import ( "context" "os" "strings" + + "github.com/steveyegge/beads/internal/config" ) const configKeyToken = "notion.token" @@ -12,7 +14,18 @@ type AuthSource string const ( AuthSourceConfigToken AuthSource = "config_token" - AuthSourceEnv AuthSource = "env" + // AuthSourceDatabaseLegacy marks a token still being read out of the config + // table, where a bd from before notion.token was yaml-only put it — and from + // where `bd dolt push` replicated it to every remote this workspace pushed + // to. + // + // It is a SEPARATE source rather than another spelling of + // AuthSourceConfigToken because it is the only signal that identifies the + // population GH#6676 tells to rotate. Collapsing the two reports + // `config_token` for the safe workspace and the leaked one alike, so the + // users who must act are exactly the ones who cannot tell they must. + AuthSourceDatabaseLegacy AuthSource = "database_legacy" + AuthSourceEnv AuthSource = "env" ) type ResolvedAuth struct { @@ -25,13 +38,39 @@ type ConfigReader interface { GetConfig(ctx context.Context, key string) (string, error) } -// ResolveAuth resolves a configured Notion token before the environment fallback. +// ResolveAuth resolves the Notion token: config.yaml first, then a token left +// in the Dolt database by an older bd, then the NOTION_TOKEN environment +// variable. +// +// notion.token is a yaml-only key, so THE CLI DOOR keeps it out of the +// database: `bd config set notion.token` consults IsYamlOnlyKey before it +// reaches the store and writes config.yaml instead, and `bd config unset` +// clears the file and any row an older bd left behind. That matters because the +// config table's contents are replicated by `bd dolt push`. +// +// ONE DOOR IS STILL OPEN, and this comment deliberately does not claim +// otherwise: the routing lives in the CLI command, not at the write chokepoint, +// so PUT /v0/beads/config/notion.token — `bd serve`, and the dashboard behind +// it — still writes the row with no refusal. That hole is not Notion's; it is +// identical for github.token, jira.api_token, linear.api_key and ado.pat, which +// is why closing it belongs at the role (workapi.ValidateSettingWrite / +// SetSetting), where every tracker credential gets the rule in one change, +// rather than here. Tracked as follow-up to GH#6676. +// +// The database read below therefore remains, both as the upgrade path for a +// workspace configured before the key moved and as the reader for anything that +// door still writes. It reports AuthSourceDatabaseLegacy so `bd notion status` +// can tell that workspace to rotate. func ResolveAuth(ctx context.Context, reader ConfigReader) (*ResolvedAuth, error) { + if token := strings.TrimSpace(config.GetString(configKeyToken)); token != "" { + return &ResolvedAuth{Token: token, Source: AuthSourceConfigToken}, nil + } + if reader != nil { if token, err := reader.GetConfig(ctx, configKeyToken); err == nil && strings.TrimSpace(token) != "" { return &ResolvedAuth{ Token: strings.TrimSpace(token), - Source: AuthSourceConfigToken, + Source: AuthSourceDatabaseLegacy, }, nil } } diff --git a/internal/notion/auth_test.go b/internal/notion/auth_test.go new file mode 100644 index 000000000..4900a5835 --- /dev/null +++ b/internal/notion/auth_test.go @@ -0,0 +1,152 @@ +package notion + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/steveyegge/beads/internal/config" +) + +// mapConfigReader stands in for the Dolt config table. +type mapConfigReader map[string]string + +func (m mapConfigReader) GetConfig(_ context.Context, key string) (string, error) { + return m[key], nil +} + +// loadNotionYamlConfig points the global config at a temp workspace whose +// .beads/config.yaml holds yamlBody, isolated from the developer's own config. +func loadNotionYamlConfig(t *testing.T, yamlBody string) { + t.Helper() + tmpDir := t.TempDir() + beadsDir := filepath.Join(tmpDir, ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatalf("mkdir .beads: %v", err) + } + if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(yamlBody), 0o600); err != nil { + t.Fatalf("write config.yaml: %v", err) + } + + t.Setenv("BEADS_DIR", "") + t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1") + t.Setenv("HOME", filepath.Join(tmpDir, "home")) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, "xdg")) + t.Chdir(tmpDir) + + config.ResetForTesting() + t.Cleanup(config.ResetForTesting) + if err := config.Initialize(); err != nil { + t.Fatalf("config.Initialize: %v", err) + } +} + +// TestResolveAuth_ReadsTokenFromConfigYaml verifies that notion.token is read +// from config.yaml, where `bd config set notion.token` now writes it, ahead of +// both a legacy database value and NOTION_TOKEN (GH#6676). +func TestResolveAuth_ReadsTokenFromConfigYaml(t *testing.T) { + loadNotionYamlConfig(t, "notion.token: \"yaml-token\"\n") + t.Setenv("NOTION_TOKEN", "env-token") + + auth, err := ResolveAuth(context.Background(), mapConfigReader{configKeyToken: "db-token"}) + if err != nil { + t.Fatalf("ResolveAuth returned error: %v", err) + } + if auth == nil || auth.Token != "yaml-token" || auth.Source != AuthSourceConfigToken { + t.Fatalf("auth = %+v, want yaml-token from %q", auth, AuthSourceConfigToken) + } +} + +// TestResolveAuth_ReadsTheShapeConfigSetWrites closes the end-to-end gap the +// flat-fixture tests leave open: they assert `notion.token: x`, but that is not +// what `bd config set notion.token` puts on disk. SetYamlConfig routes through +// updateYamlKey, which hands any dotted key to updateNestedYamlKey — and that +// succeeds even on an empty file — so the bytes written are `notion:\n +// token: x`. Nothing pinned that the reader resolves the written shape, which +// is the PR's headline claim: set writes config.yaml, ResolveAuth reads it +// back. +// +// It drives the real writer rather than a second hand-written fixture on +// purpose: a fixture encodes today's shape and would keep passing if the writer +// changed, which is precisely the regression this is here to catch. +func TestResolveAuth_ReadsTheShapeConfigSetWrites(t *testing.T) { + loadNotionYamlConfig(t, "") + + // The exact call the yaml-only branch of `bd config set` makes. + if err := config.SetYamlConfig(configKeyToken, "written-token"); err != nil { + t.Fatalf("SetYamlConfig: %v", err) + } + + config.ResetForTesting() + if err := config.Initialize(); err != nil { + t.Fatalf("config.Initialize after write: %v", err) + } + + // nil reader: no database is involved, so a pass can only come from the + // file. NOTION_TOKEN is deliberately left unset — with a fallback in place + // a reader bug would still return a token and the test would not notice. + auth, err := ResolveAuth(context.Background(), nil) + if err != nil { + t.Fatalf("ResolveAuth returned error: %v", err) + } + if auth == nil || auth.Token != "written-token" || auth.Source != AuthSourceConfigToken { + t.Fatalf("auth = %+v, want written-token from %q", auth, AuthSourceConfigToken) + } + + // Non-vacuity guard, not a formatting preference: if the writer ever emits + // the flat key, this test silently becomes a duplicate of + // TestResolveAuth_ReadsTokenFromConfigYaml and stops covering the nested + // path. Failing here says the premise moved, not that the behavior broke. + onDisk, err := os.ReadFile(filepath.Join(".beads", "config.yaml")) + if err != nil { + t.Fatalf("read back config.yaml: %v", err) + } + if !strings.Contains(string(onDisk), "notion:") { + t.Errorf("config.yaml is not the nested shape this test exists to cover; writer output:\n%s", onDisk) + } +} + +// TestResolveAuth_FallsBackToLegacyDatabaseToken verifies that a token written +// to the database by an older bd still authenticates, ahead of NOTION_TOKEN. +// +// It also pins the source as AuthSourceDatabaseLegacy rather than +// AuthSourceConfigToken. That distinction is the whole reportable difference +// between a workspace that is clean and one whose token was pushed to its +// remotes, so a regression collapsing the two would silently remove the only +// signal `bd notion status` has to tell the second kind to rotate. +func TestResolveAuth_FallsBackToLegacyDatabaseToken(t *testing.T) { + loadNotionYamlConfig(t, "") + t.Setenv("NOTION_TOKEN", "env-token") + + auth, err := ResolveAuth(context.Background(), mapConfigReader{configKeyToken: "db-token"}) + if err != nil { + t.Fatalf("ResolveAuth returned error: %v", err) + } + if auth == nil || auth.Token != "db-token" || auth.Source != AuthSourceDatabaseLegacy { + t.Fatalf("auth = %+v, want db-token from %q", auth, AuthSourceDatabaseLegacy) + } +} + +// TestResolveAuth_FallsBackToEnv verifies NOTION_TOKEN is used when neither +// config.yaml nor the database holds a token, including with no reader at all. +func TestResolveAuth_FallsBackToEnv(t *testing.T) { + loadNotionYamlConfig(t, "") + t.Setenv("NOTION_TOKEN", "env-token") + + for name, reader := range map[string]ConfigReader{ + "empty reader": mapConfigReader{}, + "nil reader": nil, + } { + t.Run(name, func(t *testing.T) { + auth, err := ResolveAuth(context.Background(), reader) + if err != nil { + t.Fatalf("ResolveAuth returned error: %v", err) + } + if auth == nil || auth.Token != "env-token" || auth.Source != AuthSourceEnv { + t.Fatalf("auth = %+v, want env-token from %q", auth, AuthSourceEnv) + } + }) + } +} diff --git a/internal/notion/tracker_test.go b/internal/notion/tracker_test.go index 5cdfb26dc..5acb61262 100644 --- a/internal/notion/tracker_test.go +++ b/internal/notion/tracker_test.go @@ -599,8 +599,8 @@ func TestTrackerBatchPushDryRunSkipsLabelOrderOnlyDifference(t *testing.T) { // TestGetConfig_YamlOnlyKeyBypassesStore verifies that yaml-only keys // bypass the Dolt store entirely, falling through to config.yaml and env var. -// Notion doesn't currently have yaml-only secret keys, but this ensures the -// code path works correctly if one is added in the future. +// notion.token is resolved by ResolveAuth (see auth_test.go); this covers the +// generic getConfig branch. func TestGetConfig_YamlOnlyKeyBypassesStore(t *testing.T) { ctx := context.Background() tr := &Tracker{store: nil}