From 8513b6db4c9f9fe8885b523b9a2d12138d38b8e4 Mon Sep 17 00:00:00 2001 From: Bjoern Kalkbrenner Date: Sat, 21 Nov 2026 17:32:16 +0100 Subject: [PATCH] feat(configcmd): Spec.SecretKeys redacts a program's own secret options "config show" redacted only keys whose last segment is secrets, secret, token or password. A program's own secrets (database.url, oidc.client_secret, smtp.url, ...) were printed, so programs disabled show. Spec.SecretKeys names them as dotted keys (a key covers everything below it); Effective carries them to Values and Show. First consumer: yaemp and yaemp-mcp. Co-Authored-By: Claude Opus 5.5 --- README.md | 6 ++++-- configcmd/configcmd.go | 6 ++++++ configcmd/layered.go | 4 ++++ configcmd/layered_test.go | 37 +++++++++++++++++++++++++++++++++++++ configcmd/show.go | 18 ++++++++++++++++-- 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0227422..12735e0 100644 --- a/README.md +++ b/README.md @@ -374,8 +374,10 @@ report options the program ignores. Taken over from `config show [--json] [--show-secrets]` prints the configuration with the file every value comes from (`Effective.Show`; secrets -- keys -`secrets`, `secret`, `token`, `password` -- are redacted unless -`--show-secrets`, references like `env:X` stay visible), and +`secrets`, `secret`, `token`, `password`, plus the program's own +`Spec.SecretKeys` such as `database.url` (a key covers everything below +it) -- are redacted unless `--show-secrets`, references like `env:X` +stay visible), and `configcmd.Doctor(report, spec, explicit)` is the config part of a `doctor` command (paths, layers, "config file found", unknown options). diff --git a/configcmd/configcmd.go b/configcmd/configcmd.go index 041d55c..d4fcb3c 100644 --- a/configcmd/configcmd.go +++ b/configcmd/configcmd.go @@ -116,6 +116,12 @@ type Spec struct { // options (see Missing). Optional. Schema any + // SecretKeys are the program's own secret options, as dotted keys + // ("database.url", "oidc.client_secret"): "config show" redacts them + // like the keys it always treats as secret (a last segment secrets, + // secret, token or password). A key also covers everything below it. + SecretKeys []string + // Options are passed through to core/config; tests use them to // inject a platform, environment, root and working directory. Options []coreconfig.Option diff --git a/configcmd/layered.go b/configcmd/layered.go index 7345e43..9c468a1 100644 --- a/configcmd/layered.go +++ b/configcmd/layered.go @@ -73,6 +73,9 @@ type Effective struct { // Loader is the Layered loader's own value for this configuration // (e.g. its richer layer list), for its Unknown hook; nil otherwise. Loader any + // SecretKeys are redacted by Values and Show in addition to the + // default secret names (Spec.SecretKeys). + SecretKeys []string } // Used returns the layers that contributed, highest priority first. @@ -233,6 +236,7 @@ func (s Spec) show(explicit string, args []string, stdout, stderr io.Writer) int return ExitProblem } used := s.usedFunc() + e.SecretKeys = s.SecretKeys if err := e.Show(stdout, used, *asJSON, *secrets); err != nil { fmt.Fprintf(stderr, "%s config show: %v\n", s.Name, err) return ExitProblem diff --git a/configcmd/layered_test.go b/configcmd/layered_test.go index e0a143b..86d5a51 100644 --- a/configcmd/layered_test.go +++ b/configcmd/layered_test.go @@ -174,6 +174,43 @@ func TestValuesRedaction(t *testing.T) { } } +// TestValuesProgramSecrets: Spec.SecretKeys (on Effective) redacts a +// program's own secret options and everything below one; other keys and +// a key that only shares a prefix stay. +func TestValuesProgramSecrets(t *testing.T) { + merged, origins := coreconfig.Merge(coreconfig.Source{Name: "f", Data: map[string]any{ + "database": map[string]any{"url": "postgres://app:pw@db/x", "owner_url": "file:/run/o", "urls": "kept"}, + "smtp": map[string]any{"url": "smtps://u:pw@mail", "from": "a@b"}, + "vault": map[string]any{"inner": map[string]any{"key": "deep"}}, + }}) + e := &Effective{Name: "p", Merged: merged, Origins: origins, SecretKeys: []string{"database.url", "database.owner_url", "smtp.url", "vault"}} + got := map[string]any{} + for _, v := range e.Values(nil, false) { + got[v.Key] = v.Value + } + want := map[string]any{ + "database.url": "", "database.owner_url": "file:/run/o", "database.urls": "kept", + "smtp.url": "", "smtp.from": "a@b", "vault.inner.key": "", + } + if !reflect.DeepEqual(got, want) { + t.Errorf("Values = %v, want %v", got, want) + } + for _, v := range e.Values(nil, true) { + if v.Key == "smtp.url" && v.Value != "smtps://u:pw@mail" { + t.Errorf("show-secrets: %v", v.Value) + } + } + + // Through "config show": the Spec's keys reach the output. + root := t.TempDir() + s := testSpec(root) + s.SecretKeys = []string{"log-level"} + writeFile(t, filepath.Join(root, "etc", "yamase", "yamase-test.yaml"), minimalTemplate+"log-level: debug\n") + if code, out, errOut := run(t, s, "", "show"); code != ExitOK || strings.Contains(out, `"debug"`) || !strings.Contains(out, `log-level: ""`) { + t.Errorf("show: exit %d\n%s%s", code, out, errOut) + } +} + type inlineBase struct { Enabled bool `yaml:"enabled"` } diff --git a/configcmd/show.go b/configcmd/show.go index cb567c8..6a15cfa 100644 --- a/configcmd/show.go +++ b/configcmd/show.go @@ -28,7 +28,7 @@ const DefaultOrigin = "default" // Values returns every leaf of e, sorted by key, with its origin. // used reports whether the program reads a key (nil: every key); a // default it does not read is left out. Secret values (keys secrets, -// secret, token, password) are replaced by "" unless +// secret, token, password, and e.SecretKeys) are replaced by "" unless // showSecrets; a reference that only says where a secret is (env:, // file:, secret://) is shown. func (e *Effective) Values(used func(string) bool, showSecrets bool) []Value { @@ -42,7 +42,7 @@ func (e *Effective) Values(used func(string) bool, showSecrets bool) []Value { if !u && source == DefaultOrigin { return } - if !showSecrets && isSecretKey(key) { + if !showSecrets && e.isSecret(key) { v = redact(v) } out = append(out, Value{Key: key, Value: v, Source: source, Used: u}) @@ -117,6 +117,20 @@ var secretKeys = map[string]bool{"secrets": true, "secret": true, "token": true, func isSecretKey(key string) bool { return secretKeys[key[strings.LastIndex(key, ".")+1:]] } +// isSecret: a default secret name, or one of e.SecretKeys or a key below +// one. +func (e *Effective) isSecret(key string) bool { + if isSecretKey(key) { + return true + } + for _, k := range e.SecretKeys { + if key == k || strings.HasPrefix(key, k+".") { + return true + } + } + return false +} + // redact replaces secret values, keeping references that only say where // the secret is. func redact(v any) any {