2026-10-25 17:29:19 +01:00
|
|
|
package configcmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
|
|
|
|
coreconfig "yetano.dev/yetano/core/config"
|
|
|
|
|
"yetano.dev/yetano/core/doctor"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestShowSingleFile(t *testing.T) {
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
s := testSpec(root)
|
|
|
|
|
file := filepath.Join(root, "etc", "yamase", "yamase-test.yaml")
|
|
|
|
|
writeFile(t, file, minimalTemplate+"log-level: debug\nunread: 1\n")
|
|
|
|
|
code, out, errOut := run(t, s, "", "show")
|
|
|
|
|
if code != ExitOK {
|
|
|
|
|
t.Fatalf("show: exit %d: %s", code, errOut)
|
|
|
|
|
}
|
|
|
|
|
for _, want := range []string{
|
|
|
|
|
`log-level: "debug"`,
|
|
|
|
|
"# " + file,
|
|
|
|
|
`root: "/var/lib/yamase/queue"`,
|
|
|
|
|
"unread: 1",
|
|
|
|
|
"not used by yamase-test",
|
|
|
|
|
} {
|
|
|
|
|
if !strings.Contains(out, want) {
|
|
|
|
|
t.Errorf("show output lacks %q:\n%s", want, out)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
code, out, _ = run(t, s, "", "show", "--json")
|
|
|
|
|
var values []Value
|
|
|
|
|
if code != ExitOK || json.Unmarshal([]byte(out), &values) != nil || len(values) == 0 {
|
|
|
|
|
t.Errorf("show --json: exit %d, output %q", code, out)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fakeLayered serves two layers of one directory, as a family-wide file
|
|
|
|
|
// plus a program file.
|
|
|
|
|
func fakeLayered(dir string) *Layered {
|
|
|
|
|
shared := filepath.Join(dir, "family.yaml")
|
|
|
|
|
own := filepath.Join(dir, "own.yaml")
|
|
|
|
|
layers := func(string) ([]LayerInfo, error) {
|
|
|
|
|
return []LayerInfo{
|
|
|
|
|
{Name: own, Path: own, Source: "test", Exists: statOK(own)},
|
|
|
|
|
{Name: shared, Path: shared, Source: "test", Exists: statOK(shared)},
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
return &Layered{
|
|
|
|
|
Layers: layers,
|
|
|
|
|
Load: func(explicit string) (*Effective, error) {
|
|
|
|
|
ls, _ := layers(explicit)
|
|
|
|
|
var sources []coreconfig.Source
|
|
|
|
|
for i := len(ls) - 1; i >= 0; i-- {
|
|
|
|
|
if !ls[i].Exists {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
data, err := coreconfig.ReadFile(ls[i].Path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
sources = append(sources, coreconfig.Source{Name: ls[i].Name, Data: data})
|
|
|
|
|
}
|
|
|
|
|
if len(sources) == 0 {
|
|
|
|
|
return nil, ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
merged, origins := coreconfig.Merge(sources...)
|
|
|
|
|
return &Effective{Name: "yamase-test", Layers: ls, Merged: merged, Origins: origins}, nil
|
|
|
|
|
},
|
|
|
|
|
Validate: func(merged []byte) error {
|
|
|
|
|
var s schema
|
|
|
|
|
if err := yaml.Unmarshal(merged, &s); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if len(s.Listen) == 0 {
|
|
|
|
|
return errNoListener
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type testErr string
|
|
|
|
|
|
|
|
|
|
func (e testErr) Error() string { return string(e) }
|
|
|
|
|
|
|
|
|
|
const errNoListener = testErr("listen: at least one listener is required")
|
|
|
|
|
|
|
|
|
|
func statOK(path string) bool { return statExistsForTest(path) }
|
|
|
|
|
|
|
|
|
|
func TestLayered(t *testing.T) {
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
dir := filepath.Join(root, "cfg")
|
|
|
|
|
s := testSpec(root)
|
|
|
|
|
s.Layered = fakeLayered(dir)
|
|
|
|
|
|
|
|
|
|
if code, out, _ := run(t, s, "", "path"); code != ExitOK || !strings.Contains(out, "no configuration file found") {
|
|
|
|
|
t.Errorf("path without files: exit %d, %q", code, out)
|
|
|
|
|
}
|
|
|
|
|
if code, _, errOut := run(t, s, "", "validate"); code != ExitProblem || !strings.Contains(errOut, ErrNotFound.Error()) {
|
|
|
|
|
t.Errorf("validate without files: exit %d, %q", code, errOut)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeFile(t, filepath.Join(dir, "family.yaml"), "log-level: info\nlisten:\n - address: \":25\"\n")
|
|
|
|
|
writeFile(t, filepath.Join(dir, "own.yaml"), "log-level: debug\nlog-levle: x\n")
|
|
|
|
|
|
|
|
|
|
code, out, _ := run(t, s, "", "path")
|
|
|
|
|
if code != ExitOK || strings.Count(out, "(test, used)") != 2 {
|
|
|
|
|
t.Errorf("path: exit %d, %q", code, out)
|
|
|
|
|
}
|
|
|
|
|
// Unknown options are warnings for a layered program.
|
|
|
|
|
code, out, errOut := run(t, s, "", "validate")
|
|
|
|
|
if code != ExitOK || !strings.Contains(out, "warning: unknown option log-levle ("+filepath.Join(dir, "own.yaml")+", line 2)") || !strings.Contains(out, ": valid") {
|
|
|
|
|
t.Errorf("validate: exit %d, stdout %q, stderr %q", code, out, errOut)
|
|
|
|
|
}
|
|
|
|
|
code, out, _ = run(t, s, "", "show")
|
|
|
|
|
if code != ExitOK || !strings.Contains(out, `log-level: "debug"`) || !strings.Contains(out, "# "+filepath.Join(dir, "family.yaml")) {
|
|
|
|
|
t.Errorf("show: exit %d:\n%s", code, out)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(out, "layers, lowest priority first: "+filepath.Join(dir, "family.yaml")+" + "+filepath.Join(dir, "own.yaml")) {
|
|
|
|
|
t.Errorf("show header:\n%s", out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeFile(t, filepath.Join(dir, "family.yaml"), "log-level: info\n")
|
|
|
|
|
if code, _, errOut := run(t, s, "", "validate"); code != ExitProblem || !strings.Contains(errOut, "listen") {
|
|
|
|
|
t.Errorf("validate of an invalid merge: exit %d, %q", code, errOut)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDoctor(t *testing.T) {
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
s := testSpec(root)
|
|
|
|
|
r := doctor.Report{Program: "yamase-test"}
|
|
|
|
|
if e := Doctor(&r, s, ""); e != nil {
|
|
|
|
|
t.Error("Doctor without a file returned a configuration")
|
|
|
|
|
}
|
|
|
|
|
writeFile(t, filepath.Join(root, "etc", "yamase", "yamase-test.yaml"), minimalTemplate+"log-levle: x\n")
|
|
|
|
|
r = doctor.Report{Program: "yamase-test"}
|
|
|
|
|
e := Doctor(&r, s, "")
|
|
|
|
|
if e == nil {
|
|
|
|
|
t.Fatal("Doctor with a file returned nil")
|
|
|
|
|
}
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
r.Run([]string{"--json"}, &out, &out)
|
|
|
|
|
if !strings.Contains(out.String(), "log-levle (") || !strings.Contains(out.String(), "config file found") {
|
|
|
|
|
t.Errorf("doctor report = %s", out.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestValuesRedaction(t *testing.T) {
|
|
|
|
|
merged, origins := coreconfig.Merge(coreconfig.Source{Name: "f", Data: map[string]any{
|
|
|
|
|
"srs": map[string]any{"secrets": []any{"plain", "env:X", "base64:abc"}},
|
|
|
|
|
"yaais": map[string]any{"token": "file:/run/t", "password": "hunter2"},
|
|
|
|
|
}})
|
|
|
|
|
e := &Effective{Name: "p", Merged: merged, Origins: origins}
|
|
|
|
|
got := map[string]any{}
|
|
|
|
|
for _, v := range e.Values(nil, false) {
|
|
|
|
|
got[v.Key] = v.Value
|
|
|
|
|
}
|
|
|
|
|
want := map[string]any{
|
|
|
|
|
"srs.secrets": []any{"<redacted>", "env:X", "<redacted>"},
|
|
|
|
|
"yaais.token": "file:/run/t",
|
|
|
|
|
"yaais.password": "<redacted>",
|
|
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
|
t.Errorf("Values = %v, want %v", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 17:32:16 +01:00
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-25 17:29:19 +01:00
|
|
|
type inlineBase struct {
|
|
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestKnownKey(t *testing.T) {
|
|
|
|
|
type cfg struct {
|
|
|
|
|
Throttle struct {
|
|
|
|
|
inlineBase `yaml:",inline"`
|
|
|
|
|
Window string `yaml:"window"`
|
|
|
|
|
} `yaml:"throttle"`
|
|
|
|
|
Listen []struct {
|
|
|
|
|
Address string `yaml:"address"`
|
|
|
|
|
} `yaml:"listen"`
|
|
|
|
|
Backends map[string]struct{ Root string } `yaml:"backends"`
|
|
|
|
|
Custom selfDecoding `yaml:"custom"`
|
|
|
|
|
}
|
|
|
|
|
for key, want := range map[string]bool{
|
|
|
|
|
"throttle.enabled": true,
|
|
|
|
|
"throttle.window": true,
|
|
|
|
|
"throttle.nope": false,
|
|
|
|
|
"listen[0].address": true,
|
|
|
|
|
"listen[].address": true,
|
|
|
|
|
"backends.any.root": true,
|
|
|
|
|
"backends.any.nope": false,
|
|
|
|
|
"custom.anything": true,
|
|
|
|
|
"missing": false,
|
|
|
|
|
} {
|
|
|
|
|
if got := KnownKey(cfg{}, key); got != want {
|
|
|
|
|
t.Errorf("KnownKey(%q) = %v, want %v", key, got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func statExistsForTest(path string) bool {
|
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
return err == nil
|
|
|
|
|
}
|
2026-11-24 09:47:44 +01:00
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|