"config show [--json] [--show-secrets]" prints the configuration with the origin of every value (secrets redacted), for every program. Spec.Layered lets a program whose configuration is merged from several inputs (family file, a section of it, its own file -- the yamase family) run path, validate and show over all layers, with its own unknown-option rules; unknown options are warnings there. Doctor(report, spec, explicit) is the config part of a doctor command. UnknownKey carries the layer it is in; KnownKey tells whether a dotted key is an option of a config struct. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
153 lines
4.8 KiB
Go
153 lines
4.8 KiB
Go
package configcmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// UnknownKey is a key of a configuration file that no option of the
|
|
// program has: a typo, an option of another program, or one a newer
|
|
// version dropped. The program ignores it, so it silently gets the
|
|
// default instead of what the operator meant.
|
|
type UnknownKey struct {
|
|
// Key is the dotted path ("pre-accept-checks.requre-known-domain");
|
|
// entries of a list are "listen[1].tls".
|
|
Key string
|
|
Line int
|
|
// Layer names the layer the key is in, for a layered configuration
|
|
// (LayerInfo.Name); "" for a single file.
|
|
Layer string
|
|
}
|
|
|
|
func (k UnknownKey) String() string {
|
|
if k.Layer != "" {
|
|
return fmt.Sprintf("%s (%s, line %d)", k.Key, k.Layer, k.Line)
|
|
}
|
|
return fmt.Sprintf("%s (line %d)", k.Key, k.Line)
|
|
}
|
|
|
|
// UnknownKeys lists the keys of raw that schema (the program's
|
|
// configuration struct, e.g. config.Config{}) has no field for, by its
|
|
// yaml tags. It descends into struct-typed mappings, lists of structs
|
|
// and maps of structs (a map of named backends: the names are the
|
|
// operator's, their contents are options). A type with its own
|
|
// UnmarshalYAML is taken as a whole: it decides itself what it accepts.
|
|
func UnknownKeys(raw []byte, schema any) ([]UnknownKey, error) {
|
|
var doc yaml.Node
|
|
if err := yaml.Unmarshal(raw, &doc); err != nil {
|
|
return nil, fmt.Errorf("parsing: %w", err)
|
|
}
|
|
root := docMapping(&doc)
|
|
if root == nil || schema == nil {
|
|
return nil, nil
|
|
}
|
|
var out []UnknownKey
|
|
walkUnknown(nil, reflect.TypeOf(schema), root, &out)
|
|
return out, nil
|
|
}
|
|
|
|
// UnknownKeysNode is UnknownKeys for an already parsed mapping node
|
|
// (e.g. one section of a larger file): keys are reported relative to n,
|
|
// with their line in the original document.
|
|
func UnknownKeysNode(n *yaml.Node, schema any) []UnknownKey {
|
|
if n == nil || n.Kind != yaml.MappingNode || schema == nil {
|
|
return nil
|
|
}
|
|
var out []UnknownKey
|
|
walkUnknown(nil, reflect.TypeOf(schema), n, &out)
|
|
return out
|
|
}
|
|
|
|
// UnknownKeysFile is UnknownKeys for the file at path.
|
|
func UnknownKeysFile(path string, schema any) ([]UnknownKey, error) {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return UnknownKeys(raw, schema)
|
|
}
|
|
|
|
var unmarshalerType = reflect.TypeOf((*yaml.Unmarshaler)(nil)).Elem()
|
|
|
|
// opaque reports whether t decodes itself (or is no struct/map/list),
|
|
// so its contents are not checked key by key.
|
|
func opaque(t reflect.Type) bool {
|
|
if t.Implements(unmarshalerType) || reflect.PointerTo(t).Implements(unmarshalerType) {
|
|
return true
|
|
}
|
|
switch t.Kind() {
|
|
case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array:
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func walkUnknown(path []string, t reflect.Type, n *yaml.Node, out *[]UnknownKey) {
|
|
t = deref(t)
|
|
if opaque(t) {
|
|
return
|
|
}
|
|
switch {
|
|
case n.Kind == yaml.MappingNode && t.Kind() == reflect.Struct:
|
|
for i := 0; i+1 < len(n.Content); i += 2 {
|
|
k, v := n.Content[i], n.Content[i+1]
|
|
sub := append(append([]string(nil), path...), k.Value)
|
|
ft, ok := fieldType(t, k.Value)
|
|
if !ok {
|
|
*out = append(*out, UnknownKey{Key: strings.Join(sub, "."), Line: k.Line})
|
|
continue
|
|
}
|
|
walkUnknown(sub, ft, v, out)
|
|
}
|
|
case n.Kind == yaml.MappingNode && t.Kind() == reflect.Map:
|
|
for i := 0; i+1 < len(n.Content); i += 2 {
|
|
k, v := n.Content[i], n.Content[i+1]
|
|
walkUnknown(append(append([]string(nil), path...), k.Value), t.Elem(), v, out)
|
|
}
|
|
case n.Kind == yaml.SequenceNode && (t.Kind() == reflect.Slice || t.Kind() == reflect.Array):
|
|
for i, item := range n.Content {
|
|
sub := append([]string(nil), path...)
|
|
if len(sub) > 0 {
|
|
sub[len(sub)-1] = fmt.Sprintf("%s[%d]", sub[len(sub)-1], i)
|
|
}
|
|
walkUnknown(sub, t.Elem(), item, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
// WarnUnknown logs every unknown option of the configuration file at
|
|
// path. A program calls it after loading its configuration: it keeps
|
|
// running with the defaults for those keys (a typo must not take a
|
|
// server down), but the operator sees why a setting has no effect.
|
|
func WarnUnknown(log *slog.Logger, path string, schema any) {
|
|
unknown, err := UnknownKeysFile(path, schema)
|
|
if err != nil {
|
|
return // Load already reported a file it cannot parse
|
|
}
|
|
for _, k := range unknown {
|
|
log.Warn("configuration: unknown option, ignored (the default applies)", "option", k.Key, "line", k.Line, "path", path)
|
|
}
|
|
}
|
|
|
|
// UnknownCheck is doctor's check of the file at path: ok without
|
|
// unknown options, else a note listing them.
|
|
func UnknownCheck(path string, schema any) (ok bool, note string) {
|
|
unknown, err := UnknownKeysFile(path, schema)
|
|
if err != nil {
|
|
return false, err.Error()
|
|
}
|
|
if len(unknown) == 0 {
|
|
return true, ""
|
|
}
|
|
parts := make([]string, len(unknown))
|
|
for i, k := range unknown {
|
|
parts[i] = k.String()
|
|
}
|
|
return false, "ignored, the defaults apply: " + strings.Join(parts, ", ")
|
|
}
|