fix(config): keep the fields a file overlay omits inside a map entry
Loader.overlayFile decoded the overlay file straight into the config the base file produced, and a decode replaces a map-valued entry wholesale: [services.state] target = ... in an overlay cleared the target_token the base file set. That is archie-core-e2e2 on the user-visible -config-overlay path -- deployments/dev.toml is the documented argument and sets [services.state] target/listen, while target_token lives in the installed config.toml. An enabled image.hosted entry lost its class and api_key_env the same way, silently turning the provider off. apply.go's foldOverrides is the fix that path was missing, so the file overlay now goes through the same fold as the map overlay: the overlay file parses to its raw mapping (decodeFileMapping), the fields of each partially addressed map entry are folded forward out of the base config, and the completed mapping is decoded over it. Loader.ApplyOverlay, ApplyOverlayValues and apply.go stay: the hazard they were written for is this one. An overlay file still reports its own unknown keys, from a separate decode into a scratch value.
This commit is contained in:
1 parent
7981ddf135
commit
a9029411ff
5 files changed
+223
-11
No files matched your search
@@ -27,16 +27,36 @@ func ApplyOverlayValues(cfg *config.Config, overrides map[string]any) error {
|
||||
if len(overrides) == 0 {
|
||||
return nil
|
||||
}
|
||||
// yaml gives a struct field the field-level treatment promised above, but a
|
||||
// map VALUE is decoded into a fresh zero value: an entry the overlay only
|
||||
// partially addresses would lose every field it does not name, so an
|
||||
// operator changing services.state.target alone would silently drop
|
||||
// target_token. Fold the fields the overlay omits forward out of the entry
|
||||
// cfg already holds, leaving the decode a complete entry to read. Doing it
|
||||
// here, once, is what makes the promise hold for every map of structs
|
||||
// (services, image.hosted, image.local, providers) instead of at each
|
||||
// field's reader.
|
||||
folded, err := foldOverrides(reflect.ValueOf(cfg), overrides)
|
||||
return applyOverlayMapping(cfg, overrides)
|
||||
}
|
||||
|
||||
// applyOverlayFile layers the overlay file at path over cfg, with the same
|
||||
// field-level precedence ApplyOverlayValues gives a map overlay. The file
|
||||
// overlay path needs it for the reason recorded on foldOverrides: decoding an
|
||||
// overlay into cfg replaces a map-valued entry wholesale, so a file naming
|
||||
// only services.state.target cleared the target_token the base file set. Both
|
||||
// overlay paths go through the fold, so neither can drift from the other.
|
||||
func applyOverlayFile(path string, cfg *config.Config) error {
|
||||
doc, err := decodeFileMapping(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return applyOverlayMapping(cfg, doc)
|
||||
}
|
||||
|
||||
// applyOverlayMapping is the decode both overlay paths share: the fields each
|
||||
// map-valued entry the overlay does not name are folded forward out of the
|
||||
// entry cfg already holds, and the completed mapping is then decoded over cfg.
|
||||
//
|
||||
// yaml gives a struct field the field-level treatment ApplyOverlayValues
|
||||
// promises, but a map VALUE is decoded into a fresh zero value: an entry the
|
||||
// overlay only partially addresses would lose every field it does not name, so
|
||||
// an operator changing services.state.target alone would silently drop
|
||||
// target_token. Folding here, once, is what makes the promise hold for every
|
||||
// map of structs (services, image.hosted, image.local, providers) instead of
|
||||
// at each field's reader.
|
||||
func applyOverlayMapping(cfg *config.Config, doc map[string]any) error {
|
||||
folded, err := foldOverrides(reflect.ValueOf(cfg), doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -27,6 +27,32 @@ func decodeConfigFileKeys(path string, target any) ([]string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// decodeFileMapping parses a configuration file into the nested mapping it
|
||||
// carries, with no typed target. An overlay needs this shape: a typed decode
|
||||
// cannot distinguish a key the file omits from a key it sets to the zero
|
||||
// value, and that distinction is what carrying the fields of a partially
|
||||
// addressed map entry forward is built on (see applyFileOverlay).
|
||||
func decodeFileMapping(path string) (map[string]any, error) {
|
||||
var doc map[string]any
|
||||
switch filepath.Ext(path) {
|
||||
case ".yaml", ".yml":
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: reading %s: %w", ErrUnreadable, path, err)
|
||||
}
|
||||
if err := yaml.Unmarshal(data, &doc); err != nil {
|
||||
return nil, fmt.Errorf("%w: parsing %s: %w", ErrUnreadable, path, err)
|
||||
}
|
||||
case ".toml":
|
||||
if _, err := toml.DecodeFile(path, &doc); err != nil {
|
||||
return nil, fmt.Errorf("%w: parsing %s: %w", ErrUnreadable, path, err)
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("%w: config file %s must end in .toml, .yaml, or .yml", ErrUnreadable, path)
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
// decodeTOMLKeys decodes a TOML file into target, additionally
|
||||
// returning the top-level dotted key paths present in the file that
|
||||
// target did not consume (toml.MetaData.Undecoded()). A single decode
|
||||
|
||||
@@ -173,7 +173,13 @@ func (l *Loader) overlayFile(basePath, overlayPath string) (*Document, error) {
|
||||
doc.Provenance.record(Origin{Path: basePath, Role: RoleMain, Layer: LayerBase})
|
||||
|
||||
if overlayPath != "" {
|
||||
cfgKeys, err = decodeConfigFileKeys(overlayPath, &doc.Config)
|
||||
// The overlay is folded over the base config rather than decoded into
|
||||
// it: a decode replaces a map-valued entry wholesale, clearing the
|
||||
// fields of that entry the overlay does not name (applyFileOverlay).
|
||||
if err := applyOverlayFile(overlayPath, &doc.Config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfgKeys, err = overlayFileKeys(overlayPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -188,6 +194,15 @@ func (l *Loader) overlayFile(basePath, overlayPath string) (*Document, error) {
|
||||
return l.finalize(doc)
|
||||
}
|
||||
|
||||
// overlayFileKeys reports the top-level keys of an overlay file that the
|
||||
// config decode does not consume. The file itself reaches the config through
|
||||
// applyOverlayFile, which needs its raw mapping; this typed decode exists only
|
||||
// for the key report, which only TOML's decoder can produce.
|
||||
func overlayFileKeys(path string) ([]string, error) {
|
||||
var scratch config.Config
|
||||
return decodeConfigFileKeys(path, &scratch)
|
||||
}
|
||||
|
||||
// unknownKeys returns the keys present in both a and b: one config file
|
||||
// feeds more than one decode target (&doc.Config and &doc.Scheduling), so
|
||||
// a key legitimately owned by one target decodes cleanly there and only
|
||||
|
||||
@@ -571,6 +571,130 @@ func TestOverlay(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveFileOverlayPreservesOmittedMapEntryFields characterizes the
|
||||
// -config-overlay path at the depth the runtime overlay is already guarded at
|
||||
// (TestApplyOverlayValuesPreservesOmittedNestedFields).
|
||||
//
|
||||
// It goes through Loader.Resolve, the production entry point, because the
|
||||
// hazard is a property of how the overlay file is applied, not of any one
|
||||
// decode helper: Loader.overlayFile decodes the overlay into the very config
|
||||
// the base file produced, and a map-valued entry is replaced wholesale, so
|
||||
// every field of that entry the overlay does not name is cleared.
|
||||
//
|
||||
// That is archie-core-e2e2 on a user-visible path. deployments/dev.toml is the
|
||||
// documented -config-overlay argument and sets [services.state] target/listen,
|
||||
// while the installed config.toml is where [services.state].target_token lives:
|
||||
// the overlay silently drops the token.
|
||||
func TestResolveFileOverlayPreservesOmittedMapEntryFields(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
base string
|
||||
overlayName string
|
||||
overlay string
|
||||
check func(t *testing.T, cfg config.Config)
|
||||
}{
|
||||
{
|
||||
// The deployment shape quoted above, with the value that is read
|
||||
// only when a client dials a non-loopback State Store.
|
||||
name: "services.state entry keeps the token it does not name",
|
||||
base: "[services.state]\ntarget = \"127.0.0.1:9090\"\ntarget_token = \"secret\"\n",
|
||||
overlay: "[services.state]\ntarget = \"10.0.0.5:9090\"\n",
|
||||
check: func(t *testing.T, cfg config.Config) {
|
||||
t.Helper()
|
||||
got := cfg.Services[config.ServiceNameState]
|
||||
if got.Target != "10.0.0.5:9090" {
|
||||
t.Errorf("services.state.target = %q, want the overlay's 10.0.0.5:9090", got.Target)
|
||||
}
|
||||
if got.TargetToken != "secret" {
|
||||
t.Errorf("services.state.target_token = %q, want the base's secret: a file overlay must not clear a field it does not name", got.TargetToken)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
// An enabled hosted provider must keep its class and key env: the
|
||||
// overlay naming only base_url would otherwise clear them and fail
|
||||
// validation after the load.
|
||||
name: "image.hosted entry keeps the fields it does not name",
|
||||
base: "[image.hosted.minimax]\nenabled = true\nclass = \"minimax\"\n" +
|
||||
"api_key_env = \"MINIMAX_API_KEY\"\n",
|
||||
overlay: "[image.hosted.minimax]\nbase_url = \"https://api.example\"\n",
|
||||
check: func(t *testing.T, cfg config.Config) {
|
||||
t.Helper()
|
||||
got := cfg.Image.Hosted["minimax"]
|
||||
want := config.ImageHostedProvider{
|
||||
Enabled: true,
|
||||
Class: "minimax",
|
||||
APIKeyEnv: "MINIMAX_API_KEY",
|
||||
BaseURL: "https://api.example",
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("image.hosted.minimax = %+v, want %+v", got, want)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "providers entry keeps the fields it does not name",
|
||||
base: "[providers.anthropic]\nclass = \"anthropic\"\n" +
|
||||
"api_key_env = \"ANTHROPIC_API_KEY\"\n",
|
||||
overlay: "[providers.anthropic]\nbase_url = \"https://proxy.example\"\n",
|
||||
check: func(t *testing.T, cfg config.Config) {
|
||||
t.Helper()
|
||||
got := cfg.Providers["anthropic"]
|
||||
want := config.Provider{
|
||||
Class: "anthropic",
|
||||
APIKeyEnv: "ANTHROPIC_API_KEY",
|
||||
BaseURL: "https://proxy.example",
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("providers.anthropic = %+v, want %+v", got, want)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
// The overlay file may be YAML (-config-overlay accepts either
|
||||
// format), and it is folded by the same code.
|
||||
name: "yaml overlay keeps the token it does not name",
|
||||
base: "[services.state]\ntarget = \"127.0.0.1:9090\"\ntarget_token = \"secret\"\n",
|
||||
overlayName: "dev.yaml",
|
||||
overlay: "services:\n state:\n target: 10.0.0.5:9090\n",
|
||||
check: func(t *testing.T, cfg config.Config) {
|
||||
t.Helper()
|
||||
got := cfg.Services[config.ServiceNameState]
|
||||
if got.Target != "10.0.0.5:9090" {
|
||||
t.Errorf("services.state.target = %q, want the overlay's 10.0.0.5:9090", got.Target)
|
||||
}
|
||||
if got.TargetToken != "secret" {
|
||||
t.Errorf("services.state.target_token = %q, want the base's secret", got.TargetToken)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
basePath := filepath.Join(dir, "config.toml")
|
||||
overlayName := tt.overlayName
|
||||
if overlayName == "" {
|
||||
overlayName = "dev.toml"
|
||||
}
|
||||
overlayPath := filepath.Join(dir, overlayName)
|
||||
if err := os.WriteFile(basePath, []byte(minimalValidConfigTOML+tt.base), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(overlayPath, []byte(tt.overlay), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
doc, err := New(nil).Resolve(basePath, overlayPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve: %v", err)
|
||||
}
|
||||
tt.check(t, doc.Config)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadBytesDoesNotExist(t *testing.T) {
|
||||
_, err := loadBytes(nil)
|
||||
if err == nil {
|
||||
|
||||
@@ -89,6 +89,33 @@ func TestUnknownSchedulingKeyIsStillReported(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestOverlayUnknownKeysAreStillReported pins that the overlay path kept its
|
||||
// drift detection when the overlay file started being applied through the
|
||||
// config fold (applyOverlayFile) instead of being decoded into the document's
|
||||
// config: the key report comes from a separate decode, so losing it would be
|
||||
// silent.
|
||||
func TestOverlayUnknownKeysAreStillReported(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
basePath := filepath.Join(dir, "config.toml")
|
||||
overlayPath := filepath.Join(dir, "dev.toml")
|
||||
if err := os.WriteFile(basePath, []byte(minimalValidConfigTOML), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// typo: concurrancy. Unknown to both decode targets, so it is a real
|
||||
// unknown key rather than Hazard 1's cross-target case.
|
||||
if err := os.WriteFile(overlayPath, []byte("[containers]\nmax_concurrancy = 4\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
doc, err := New(nil).Resolve(basePath, overlayPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve: %v (an unknown key must not fail the load)", err)
|
||||
}
|
||||
if !slices.Contains(doc.UnknownKeys, "containers.max_concurrancy") {
|
||||
t.Errorf("UnknownKeys = %v, want it to contain %q", doc.UnknownKeys, "containers.max_concurrancy")
|
||||
}
|
||||
}
|
||||
|
||||
// TestExampleConfigHasNoUnknownKeys is the contract test the design
|
||||
// promised: the checked-in template must never itself trip the detector.
|
||||
func TestExampleConfigHasNoUnknownKeys(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user