Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9395fadbf7 | ||
|
|
8513b6db4c |
No files matched your search
@@ -374,8 +374,11 @@ 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; inside a list the same rule applies to every entry's
|
||||
fields, a Spec key naming them without the index: `admins.password`), and
|
||||
`configcmd.Doctor(report, spec, explicit)` is the config part of a
|
||||
`doctor` command (paths, layers, "config file found", unknown options).
|
||||
|
||||
|
||||
@@ -116,6 +116,14 @@ 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.
|
||||
// Fields of list entries are named without the index
|
||||
// ("admins.password" for every entry of the list admins).
|
||||
SecretKeys []string
|
||||
|
||||
// Options are passed through to core/config; tests use them to
|
||||
// inject a platform, environment, root and working directory.
|
||||
Options []coreconfig.Option
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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": "<redacted>", "database.owner_url": "file:/run/o", "database.urls": "kept",
|
||||
"smtp.url": "<redacted>", "smtp.from": "a@b", "vault.inner.key": "<redacted>",
|
||||
}
|
||||
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: "<redacted>"`) {
|
||||
t.Errorf("show: exit %d\n%s%s", code, out, errOut)
|
||||
}
|
||||
}
|
||||
|
||||
type inlineBase struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
}
|
||||
@@ -211,3 +248,53 @@ func statExistsForTest(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// TestValuesRedactionInLists: a list leaf is printed as one value, so
|
||||
// the secret fields of its entries are redacted inside it -- by default
|
||||
// name and by a Spec key without the index -- and the rest stays.
|
||||
func TestValuesRedactionInLists(t *testing.T) {
|
||||
merged, origins := coreconfig.Merge(coreconfig.Source{Name: "f", Data: map[string]any{
|
||||
"admins": []any{
|
||||
map[string]any{"username": "admin", "password": "changeme"},
|
||||
map[string]any{"username": "ops", "password": "env:OPS_PW", "tags": []any{"a"}},
|
||||
},
|
||||
"ids": []any{map[string]any{"name": "gc", "nested": map[string]any{"token": "t0k"}, "url": "pg://u:pw@h"}},
|
||||
"plain": []any{"x", "y"},
|
||||
}})
|
||||
e := &Effective{Name: "p", Merged: merged, Origins: origins, SecretKeys: []string{"ids.url"}}
|
||||
got := map[string]any{}
|
||||
for _, v := range e.Values(nil, false) {
|
||||
got[v.Key] = v.Value
|
||||
}
|
||||
want := map[string]any{
|
||||
"admins": []any{
|
||||
map[string]any{"username": "admin", "password": "<redacted>"},
|
||||
map[string]any{"username": "ops", "password": "env:OPS_PW", "tags": []any{"a"}},
|
||||
},
|
||||
"ids": []any{map[string]any{"name": "gc", "nested": map[string]any{"token": "<redacted>"}, "url": "<redacted>"}},
|
||||
"plain": []any{"x", "y"},
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Values = %v\nwant %v", got, want)
|
||||
}
|
||||
if merged["admins"].([]any)[0].(map[string]any)["password"] != "changeme" {
|
||||
t.Error("the merged configuration was changed")
|
||||
}
|
||||
for _, v := range e.Values(nil, true) {
|
||||
if v.Key == "admins" && !strings.Contains(renderValue(v.Value), "changeme") {
|
||||
t.Errorf("show-secrets: %v", v.Value)
|
||||
}
|
||||
}
|
||||
|
||||
// Through "config show" from a file, YAML and JSON.
|
||||
root := t.TempDir()
|
||||
s := testSpec(root)
|
||||
writeFile(t, filepath.Join(root, "etc", "yamase", "yamase-test.yaml"),
|
||||
minimalTemplate+"users:\n - name: a\n password: hunter2\n")
|
||||
for _, args := range [][]string{{"show"}, {"show", "--json"}} {
|
||||
code, out, errOut := run(t, s, "", args...)
|
||||
if code != ExitOK || strings.Contains(out, "hunter2") || !strings.Contains(out, "<redacted>") || !strings.Contains(out, `"name"`) {
|
||||
t.Errorf("%v: exit %d\n%s%s", args, code, out, errOut)
|
||||
}
|
||||
}
|
||||
}
|
||||
+49
-3
@@ -28,9 +28,12 @@ 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 "<redacted>" unless
|
||||
// secret, token, password, and e.SecretKeys) are replaced by "<redacted>" unless
|
||||
// showSecrets; a reference that only says where a secret is (env:,
|
||||
// file:, secret://) is shown.
|
||||
// file:, secret://) is shown. A leaf that is a list is printed as one
|
||||
// value, so the same rule is applied inside it: a field of a list entry
|
||||
// is secret by its name or by its key without the index ("admins.password"
|
||||
// for the field password of every entry of the list admins).
|
||||
func (e *Effective) Values(used func(string) bool, showSecrets bool) []Value {
|
||||
var out []Value
|
||||
walkLeaves("", e.Merged, func(key string, v any) {
|
||||
@@ -42,8 +45,12 @@ func (e *Effective) Values(used func(string) bool, showSecrets bool) []Value {
|
||||
if !u && source == DefaultOrigin {
|
||||
return
|
||||
}
|
||||
if !showSecrets && isSecretKey(key) {
|
||||
if !showSecrets {
|
||||
if e.isSecret(key) {
|
||||
v = redact(v)
|
||||
} else {
|
||||
v = e.redactWithin(key, v)
|
||||
}
|
||||
}
|
||||
out = append(out, Value{Key: key, Value: v, Source: source, Used: u})
|
||||
})
|
||||
@@ -117,6 +124,45 @@ 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
|
||||
}
|
||||
|
||||
// redactWithin redacts the secret fields of the maps inside a list (or
|
||||
// a map) leaf at key, by the same rule as isSecret. A list entry adds no
|
||||
// segment to the key. v is copied, never changed.
|
||||
func (e *Effective) redactWithin(key string, v any) any {
|
||||
switch x := v.(type) {
|
||||
case []any:
|
||||
out := make([]any, len(x))
|
||||
for i, el := range x {
|
||||
out[i] = e.redactWithin(key, el)
|
||||
}
|
||||
return out
|
||||
case map[string]any:
|
||||
out := make(map[string]any, len(x))
|
||||
for k, el := range x {
|
||||
if sub := key + "." + k; e.isSecret(sub) {
|
||||
out[k] = redact(el)
|
||||
} else {
|
||||
out[k] = e.redactWithin(sub, el)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// redact replaces secret values, keeping references that only say where
|
||||
// the secret is.
|
||||
func redact(v any) any {
|
||||
|
||||
Reference in new issue
Block a user