Files
archie/internal/infrastructure/configuration/decode.go
T
sam 9a45a35c27 fix(config): judge a database-owned setting at the layer that owns it
Review of the two previous commits found the split was incomplete in both
directions: the checks moved off the file load path were still enforced
anywhere the value could enter the system, and the fold reached only one of the
three overlay paths.

A stale database-owned TOML value could still stop archie-state-store. The
process seeds every control-plane resource from the same file config on a fresh
database, and the seed runs the resource's own validator; three of the five
values the loader now accepts aborted the boot there (verified by booting
RunStateStore before the fix). A seed that does not validate is now SKIPPED and
reported instead of fatal: the resource stays absent, so the file's value is the
one in effect and editing config.toml is still the fix -- the daemon and the
Gateway are where it fails closed, at boot.runtimeConfig.

The resource validators were weaker than configuration.Validate for exactly the
fields the split reclassified, so a value the file path used to refuse at load
became storable in the database, and then un-fixable by editing config.toml.
The trigger enum and the test-glob rule now come from the configuration package
rather than being restated, the container image is required there as it is in the
file, and a table test ties the two layers together value by value.

The directory form of -config-overlay kept the archie-core-e2e2 failure: it
decoded each overlay file straight into the populated config, so a map-valued
entry it only partly addressed still cleared the fields it did not name. Both
the overlay layer's main file and its feature files now fold, which is what
Loader.Dir's documented parity with Loader.Overlay has always claimed.

The overlay file's key report is derived from the apply's own view of the file as
well as the file decode's, because TOML matches keys case-insensitively and the
fold's yaml decode does not: a key only the file decode matched was consumed by
the report and dropped by the apply, a silent no-op that worked in the base file.

Tests: the state-store boot is asserted against the real process, the directory
overlay and the case-mismatched key each have one, and ImportConfig pins that a
refused seed is not stored.
2026-09-21 22:12:32 +10:00

139 lines
4.9 KiB
Go

package configuration
import (
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v3"
"github.com/samcharles93/archie-core/internal/config"
)
// decodeConfigFileKeys decodes path into target, dispatching on extension,
// additionally returning target's undecoded top-level keys for a TOML
// source (see decodeTOMLKeys). A YAML source has no equivalent hook yet
// (plan-config-drift.md step 1 note) and always reports none -- unchanged
// behaviour, not a claim of completeness.
func decodeConfigFileKeys(path string, target any) ([]string, error) {
switch filepath.Ext(path) {
case ".yaml", ".yml":
return nil, decodeYAML(path, target)
case ".toml":
return decodeTOMLKeys(path, target)
default:
return nil, fmt.Errorf("%w: config file %s must end in .toml, .yaml, or .yml", ErrUnreadable, path)
}
}
// 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 applyOverlayFile).
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
// target's undecoded set is not by itself "unknown" -- see Hazard 1 in
// .local/issue-tracker/plan-config-drift.md, one file can legitimately
// feed more than one target -- callers combine this with the other
// target's undecoded set before treating anything as a real unknown key.
func decodeTOMLKeys(path string, target any) ([]string, error) {
meta, err := toml.DecodeFile(path, target)
if err != nil {
return nil, fmt.Errorf("%w: parsing %s: %w", ErrUnreadable, path, err)
}
undecoded := meta.Undecoded()
if len(undecoded) == 0 {
return nil, nil
}
keys := make([]string, len(undecoded))
for i, k := range undecoded {
keys[i] = k.String()
}
return keys, nil
}
// decodeYAML decodes a YAML file into target.
//
// Every decode error names its file. The previous loadMainConfig returned
// bare yaml.Unmarshal and toml.DecodeFile errors, so a syntax error in one
// of several files gave no clue which one to open.
func decodeYAML(path string, target any) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("%w: reading %s: %w", ErrUnreadable, path, err)
}
if err := yaml.Unmarshal(data, target); err != nil {
return fmt.Errorf("%w: parsing %s: %w", ErrUnreadable, path, err)
}
return nil
}
// decodeFeature decodes a feature file into the part of cfg it describes.
//
// Most feature files carry top-level keys matching Config fields and decode
// into cfg directly. Memory and tools are sub-structs whose keys sit at the
// top level of their own file rather than nested under a "memory:" or
// "tools:" key, so they decode into the sub-struct.
//
// An overlay layer's feature file is folded over the base layer, for the same
// reason and with the same precedence as a main overlay file. The keys that fold
// reports are dropped here: only a main file reports unknown keys today
// (decodeMain), and a feature file reporting a second, differently-shaped set
// would be a new surface rather than a fix to this one.
func decodeFeature(cfg *config.Config, feature Feature, path string, layer Layer) error {
target := any(cfg)
switch feature {
case FeatureMemory:
target = &cfg.Memory
case FeatureTools:
target = &cfg.Tools
}
if layer == LayerOverlay {
_, err := applyOverlayFile(path, target)
return err
}
return decodeYAML(path, target)
}
// decodeExtra decodes an unrecognised conf.d/ file into cfg.Extra under name.
//
// Unlike the previous implementation this reports failures instead of
// skipping them: silently dropping a file the operator wrote, because it
// happened to be unreadable or malformed, presents as the setting simply not
// taking effect.
func decodeExtra(cfg *config.Config, name, path string) error {
var value any
if err := decodeYAML(path, &value); err != nil {
return err
}
if cfg.Extra == nil {
cfg.Extra = make(map[string]any)
}
cfg.Extra[name] = value
return nil
}