diff --git a/internal/app/archied/control_plane_validation_test.go b/internal/app/archied/control_plane_validation_test.go new file mode 100644 index 000000000..1a28fbf4a --- /dev/null +++ b/internal/app/archied/control_plane_validation_test.go @@ -0,0 +1,82 @@ +package archied + +import ( + "errors" + "log/slog" + "testing" + + "github.com/samcharles93/archie-core/internal/app/controlplane" + "github.com/samcharles93/archie-core/internal/infrastructure/configuration" +) + +// TestLoadConfigAcceptsStaleDatabaseOwnedValues is archie-core-i3qm at the boot +// entry point. docs/prds/runtime-control-plane.md, "Bootstrap, migration, and +// recovery": after migration, settings in TOML are ignored and cannot block +// State Store startup. archie-state-store boots through loadConfig and then +// opens the store; it never layers a control-plane resource over the file +// document (state_store.go), so a stale value in a setting the control plane +// owns must not fail boot. Every value below is one a control-plane resource +// replaces wholesale. +func TestLoadConfigAcceptsStaleDatabaseOwnedValues(t *testing.T) { + tests := []struct { + name string + body string + }{ + { + name: "provider base_url carrying userinfo", + body: "[providers.openai]\nclass = \"openai\"\nbase_url = \"https://token@example.com/v1\"\n", + }, + { + name: "negative poll_interval", + body: "poll_interval = \"-5s\"\n", + }, + { + name: "unrecognised dispatch.trigger", + body: "[dispatch]\ntrigger = \"labels\"\n", + }, + { + name: "negative containers.volume_ttl", + body: "[containers]\nvolume_ttl = \"-1m\"\n", + }, + { + name: "malformed repos test_glob", + body: "[[repos]]\nowner = \"acme\"\nname = \"app\"\ntest_glob = \"[\"\n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + path := t.TempDir() + "/config.toml" + writeConfig(t, path, "bot_user = \"widget\"\n"+tt.body) + + b := &boot{log: slog.New(slog.DiscardHandler)} + if err := b.loadConfig(t.Context(), path, ""); err != nil { + t.Fatalf("loadConfig: %v (a stale database-owned value must not fail boot)", err) + } + }) + } +} + +// TestRuntimeConfigFailsClosedOnAnInvalidDatabaseValue is the other half of the +// same change: the checks that no longer run at load run on the effective +// document, at the one place database resources are layered in. A stored value +// that will not validate must stop archied starting rather than be published as +// the running configuration -- a state nothing can recover from in band. +func TestRuntimeConfigFailsClosedOnAnInvalidDatabaseValue(t *testing.T) { + resources := databaseOwnedResources() + resources[controlplane.ProviderSettingsKind] = map[string]any{ + "main": map[string]any{"class": "openai", "base_url": "https://token@example.com/v1"}, + } + b := newReloadBoot(t, &controlPlaneStub{values: resources}) + + err := b.loadRuntimeConfig(t.Context()) + if err == nil { + t.Fatal("loadRuntimeConfig accepted a database value that does not validate") + } + if !errors.Is(err, configuration.ErrInvalidInput) { + t.Errorf("loadRuntimeConfig = %v, want it to wrap configuration.ErrInvalidInput", err) + } + if got := b.cfgHolder.Get().Providers["main"].BaseURL; got == "https://token@example.com/v1" { + t.Error("the invalid database value was published as the running configuration") + } +} diff --git a/internal/app/archied/reload_test.go b/internal/app/archied/reload_test.go index cd4072483..0490bf3a8 100644 --- a/internal/app/archied/reload_test.go +++ b/internal/app/archied/reload_test.go @@ -30,8 +30,12 @@ func minimalConfigTOML(botUser string) string { return "bot_user = \"" + botUser + "\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n" } +// invalidConfigTOML carries a value the bootstrap document still owns, so +// resolving it fails. The settings the control plane owns are no longer judged +// at load, so using one of those here would silently turn these tests into a +// false pass. func invalidConfigTOML() string { - return "bot_user = \"widget\"\ndispatch = { trigger = \"bogus-trigger\" }\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n" + return "bot_user = \"widget\"\n[memory]\nengine = \"not-a-real-engine\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n" } func TestReloadAppliesAndPublishes(t *testing.T) { diff --git a/internal/infrastructure/configuration/loader.go b/internal/infrastructure/configuration/loader.go index 9fbb3d461..beb64085a 100644 --- a/internal/infrastructure/configuration/loader.go +++ b/internal/infrastructure/configuration/loader.go @@ -146,7 +146,7 @@ func (l *Loader) ApplyOverlay(doc *Document, overrides map[string]any) (*Documen next.Config = doc.Config.Clone() next.Provenance.Origins = append([]Origin(nil), doc.Provenance.Origins...) if len(overrides) == 0 { - return l.finalize(&next) + return l.finalize(&next, Validate) } if err := ApplyOverlayValues(&next.Config, overrides); err != nil { return nil, err @@ -155,7 +155,7 @@ func (l *Loader) ApplyOverlay(doc *Document, overrides map[string]any) (*Documen return nil, err } next.Provenance.record(Origin{Path: "config_overlay (runtime)", Role: RoleMain, Layer: LayerOverlay}) - return l.finalize(&next) + return l.finalize(&next, Validate) } func (l *Loader) overlayFile(basePath, overlayPath string) (*Document, error) { @@ -191,7 +191,7 @@ func (l *Loader) overlayFile(basePath, overlayPath string) (*Document, error) { doc.Provenance.record(Origin{Path: overlayPath, Role: RoleMain, Layer: LayerOverlay}) } - return l.finalize(doc) + return l.finalize(doc, validateBootstrap) } // overlayFileKeys reports the top-level keys of an overlay file that the @@ -290,7 +290,7 @@ func (l *Loader) Dir(baseDir, overlayDir string) (*Document, error) { } } - return l.finalize(doc) + return l.finalize(doc, validateBootstrap) } // loadDir discovers and decodes one directory into doc. The base layer must @@ -351,12 +351,19 @@ func (l *Loader) decodeMain(doc *Document, path string, isYAMLFile bool) error { } // finalize applies defaults, then validates. The order matters: validation -// judges the effective configuration, including values the operator never +// judges the defaulted configuration, including values the operator never // wrote. -func (l *Loader) finalize(doc *Document) (*Document, error) { +// +// validate is a parameter because the two kinds of caller produce different +// documents. Every file source ([Loader.File], [Loader.Overlay], [Loader.Dir]) +// produces a BOOTSTRAP document and passes validateBootstrap: the settings the +// control plane owns are layered over it later, so a stale value in one of them +// must not fail the load. [Loader.ApplyOverlay] layers overrides onto a +// document that is already effective, so it passes [Validate]. +func (l *Loader) finalize(doc *Document, validate func(*config.Config) error) (*Document, error) { doc.UnknownKeys = sortedUnique(append(doc.UnknownKeys, unregisteredServiceKeys(doc.Config.Services)...)) l.applyDefaults(&doc.Config) - if err := Validate(&doc.Config); err != nil { + if err := validate(&doc.Config); err != nil { return nil, err } l.log.Debug("configuration loaded", "sources", doc.Provenance.String()) diff --git a/internal/infrastructure/configuration/loader_test.go b/internal/infrastructure/configuration/loader_test.go index f37b003fe..b8c1a7bdc 100644 --- a/internal/infrastructure/configuration/loader_test.go +++ b/internal/infrastructure/configuration/loader_test.go @@ -236,31 +236,80 @@ func TestLoadTelegramTokenSecretRefAndLegacyFallback(t *testing.T) { } } +// TestLoadRejectsInvalidConfigEnumsAndGlobs pins which layer rejects each kind +// of invalid value. Every file source produces a BOOTSTRAP document, and the +// settings the control plane owns are layered over it later; a stale TOML value +// in one of those must not fail the load (docs/prds/runtime-control-plane.md, +// "Bootstrap, migration, and recovery"). So a check over a database-owned +// setting is asserted on the effective document instead -- the same document +// boot.runtimeConfig validates after the merge. Each case asserts both halves +// it applies to, so a check that quietly disappeared fails the case rather than +// passing it. func TestLoadRejectsInvalidConfigEnumsAndGlobs(t *testing.T) { tests := []struct { - name string - extra string + name string + body string + wantLoadErr bool }{ - {name: "dispatch trigger", extra: "\n[dispatch]\ntrigger = \"labels\"\n"}, - {name: "provider userinfo", extra: "\n[providers.openai]\nclass = \"openai\"\nbase_url = \"https://token@example.com/v1\"\n"}, - {name: "provider query secret", extra: "\n[providers.openai]\nclass = \"openai\"\nbase_url = \"https://example.com/v1?api_key=secret\"\n"}, - {name: "test glob", extra: "\n[[repos]]\nowner = \"acme\"\nname = \"app\"\ntest_glob = \"[\"\n"}, + { + name: "dispatch trigger", + body: fileConfigPrefix + "[dispatch]\ntrigger = \"labels\"\n", + }, + { + name: "provider userinfo", + body: fileConfigPrefix + "[providers.openai]\nclass = \"openai\"\nbase_url = \"https://token@example.com/v1\"\n", + }, + { + name: "provider query secret", + body: fileConfigPrefix + "[providers.openai]\nclass = \"openai\"\nbase_url = \"https://example.com/v1?api_key=secret\"\n", + }, + { + name: "test glob", + body: fileConfigPrefix + "[[repos]]\nowner = \"acme\"\nname = \"app\"\ntest_glob = \"[\"\n", + }, + { + // File-owned settings: no control-plane resource carries them, so + // the load path is the layer that has to reject them. + name: "forge intake", + body: fileConfigPrefix + "[forge]\nintake = \"not-a-real-intake\"\n", + wantLoadErr: true, + }, + { + name: "memory engine", + body: fileConfigPrefix + "[memory]\nengine = \"not-a-real-engine\"\n", + wantLoadErr: true, + }, + { + name: "negative capture retention", + body: fileConfigPrefix + "[capture]\nretention = \"-1h\"\n", + wantLoadErr: true, + }, + { + name: "image default naming no provider", + body: fileConfigPrefix + "[image]\ndefault = \"not-a-real-provider\"\n", + wantLoadErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { path := filepath.Join(t.TempDir(), "config.toml") - contents := "bot_user = \"widget\"\n" - if tt.name != "test glob" { - contents += "\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n" - } - contents += tt.extra - if err := os.WriteFile(path, []byte(contents), 0o600); err != nil { + if err := os.WriteFile(path, []byte(tt.body), 0o600); err != nil { t.Fatal(err) } - if _, err := loadFile(path); err == nil { - t.Fatal("loadFile() succeeded, want validation error") + doc, err := New(nil).File(path) + if tt.wantLoadErr { + if err == nil { + t.Fatal("File() = nil error, want the bootstrap document rejected") + } + return + } + if err != nil { + t.Fatalf("File: %v (a database-owned value must not fail the load)", err) + } + if err := Validate(&doc.Config); err == nil { + t.Fatal("Validate(effective document) = nil, want the value rejected there") } }) } @@ -487,6 +536,12 @@ func TestLoadContainerVolumeTTL(t *testing.T) { } } +// TestLoadRejectsNegativeContainerVolumeTTL: containers are a control-plane +// resource (container-runtime-policies), so a negative volume_ttl is judged on +// the effective document rather than on the file the load path reads -- a stale +// TOML value in a database-owned setting must not fail a process's startup. The +// second half of the assertion is what keeps dropping the check from passing +// this test. func TestLoadRejectsNegativeContainerVolumeTTL(t *testing.T) { path := filepath.Join(t.TempDir(), "config.toml") contents := "bot_user = \"widget\"\n" + @@ -497,8 +552,12 @@ func TestLoadRejectsNegativeContainerVolumeTTL(t *testing.T) { t.Fatal(err) } - if _, err := loadFile(path); err == nil { - t.Fatal("loadFile() accepted a negative containers.volume_ttl") + doc, err := New(nil).File(path) + if err != nil { + t.Fatalf("File: %v (a database-owned value must not fail the load)", err) + } + if err := Validate(&doc.Config); err == nil { + t.Fatal("Validate(effective document) accepted a negative containers.volume_ttl") } } @@ -695,6 +754,70 @@ func TestResolveFileOverlayPreservesOmittedMapEntryFields(t *testing.T) { } } +// TestLoadAcceptsStaleDatabaseOwnedValues is the bootstrap half of the layer +// split (archie-core-i3qm). docs/prds/runtime-control-plane.md, +// "Bootstrap, migration, and recovery": after migration, settings in TOML are +// ignored and cannot block State Store startup. archie-state-store never layers +// a database resource over its file document -- state_store.go:78 resolves the +// file and goes straight to opening the store -- so a stale value in a setting +// the control plane owns must not fail the load. +// +// Each case is a setting with a control-plane resource behind it, seeded from +// that field by controlplane.Server.ImportConfig: providers +// (provider-settings), poll_interval and dispatch (scheduling-policy), +// containers (container-runtime-policies), repositories +// (repository-policies). The second half of the assertion is what keeps this +// from being a weaker gate: the effective document, judged after the control +// plane has layered its own value in, must still reject the value. +func TestLoadAcceptsStaleDatabaseOwnedValues(t *testing.T) { + tests := []struct { + name string + body string + }{ + { + name: "providers base_url carries userinfo", + body: fileConfigPrefix + "[providers.openai]\nclass = \"openai\"\nbase_url = \"https://token@example.com/v1\"\n", + }, + { + name: "negative poll_interval", + body: fileConfigPrefix + "poll_interval = \"-5s\"\n", + }, + { + name: "unrecognised dispatch.trigger", + body: fileConfigPrefix + "[dispatch]\ntrigger = \"labels\"\n", + }, + { + name: "negative containers.volume_ttl", + body: fileConfigPrefix + "[containers]\nvolume_ttl = \"-1m\"\n", + }, + { + name: "malformed repos test_glob", + body: fileConfigPrefix + "[[repos]]\nowner = \"acme\"\nname = \"app\"\ntest_glob = \"[\"\n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + if err := os.WriteFile(path, []byte(tt.body), 0o600); err != nil { + t.Fatal(err) + } + + doc, err := New(nil).File(path) + if err != nil { + t.Fatalf("File: %v (a stale database-owned value must not fail the load)", err) + } + if err := Validate(&doc.Config); err == nil { + t.Fatal("Validate(effective document) = nil, want the control plane's own layer to reject it") + } + }) + } +} + +// fileConfigPrefix is the smallest document the load path accepts on its own, +// so each case above adds one stale setting and nothing else. +const fileConfigPrefix = "bot_user = \"widget\"\nwork_dir = \"/base/work\"\n" + func TestLoadBytesDoesNotExist(t *testing.T) { _, err := loadBytes(nil) if err == nil { diff --git a/internal/infrastructure/configuration/validate.go b/internal/infrastructure/configuration/validate.go index 3a1a82a95..61d6eef61 100644 --- a/internal/infrastructure/configuration/validate.go +++ b/internal/infrastructure/configuration/validate.go @@ -37,9 +37,23 @@ var ( memoryEngines = []string{memoryEngineBuiltin} ) -// Validate runs the same checks Loader applies before accepting a config, -// against a config.Config value already built in memory -- e.g. by archied -// setup, before it has written anything to disk. +// Validate judges a configuration a process will run with -- every check that +// can reject one -- against a config.Config value already built in memory, e.g. +// by archied setup before it has written anything to disk. It is a superset of +// what [Loader] applies to a file (validateBootstrap): a check over a setting +// the control plane owns belongs here and not there, so that a stale TOML value +// cannot fail a process's startup. +// +// It judges an EFFECTIVE document: the bootstrap file with every setting the +// control plane owns layered over it. That layering happens at exactly one +// place, boot.runtimeConfig (internal/app/archied/control_plane.go), which runs +// this afterwards; the daemon and the Gateway both go through it, and the +// readiness config probe re-runs it against the running value. A database value +// that will not validate therefore stops the process rather than starting it +// degraded. +// +// A plain file document is judged by validateBootstrap instead: see the split +// there for why the loader must not apply this to it. // // Like validate, it does not apply defaults. Several checks (dispatch.trigger, // forge.type) only pass once a default has been filled @@ -58,31 +72,34 @@ func Validate(cfg *config.Config) error { // validate reports the first problem that would stop the daemon running. // It does not modify cfg -- run applyDefaults first. func validate(cfg *config.Config) error { - if err := validateDispatch(cfg); err != nil { + if err := validateBootstrap(cfg); err != nil { return err } + return validateDatabaseOwned(cfg) +} + +// validateBootstrap reports the first problem in the settings a bootstrap +// document still owns: the file config a process reads before it can reach the +// State Store. [Loader] applies this to every file source. +// +// It deliberately omits the settings the control plane stores, because a stale +// TOML value in one of them must not be able to fail a process's startup +// (docs/prds/runtime-control-plane.md, "Bootstrap, migration, and recovery": +// after migration, settings in TOML are ignored and cannot block State Store +// startup). archie-state-store resolves its file config and opens the store +// without ever layering a database resource over it (state_store.go), so a +// check left here is a check that can still block it. Those checks live in +// validateDatabaseOwned and run on the effective document instead. +func validateBootstrap(cfg *config.Config) error { if err := validateForgeIntake(cfg); err != nil { return err } - if err := validateProviders(cfg.Providers); err != nil { - return err - } - if len(cfg.Identities) > 0 { - if err := validateIdentities(cfg.Identities); err != nil { - return err - } - } else if err := validateSingleIdentity(cfg); err != nil { - return err - } - if err := validatePollInterval(cfg); err != nil { + if err := validateIdentityStructure(cfg); err != nil { return err } if err := validateNATS(cfg); err != nil { return err } - if err := validateContainers(cfg); err != nil { - return err - } if err := validateMemory(cfg); err != nil { return err } @@ -92,6 +109,30 @@ func validate(cfg *config.Config) error { return validateCapture(cfg) } +// validateDatabaseOwned reports the first problem in a setting the control +// plane owns. Each check below judges a field that a control-plane resource is +// seeded from and replaces wholesale: providers (provider-settings), +// poll_interval and dispatch (scheduling-policy), containers +// (container-runtime-policies), and the repository lists +// (repository-policies). It runs on the effective document, after that +// replacement, which is the only point at which the value being judged is the +// value a process will use. +func validateDatabaseOwned(cfg *config.Config) error { + if err := validateDispatch(cfg); err != nil { + return err + } + if err := validateProviders(cfg.Providers); err != nil { + return err + } + if err := validatePollInterval(cfg); err != nil { + return err + } + if err := validateContainers(cfg); err != nil { + return err + } + return validateRepositoryContents(cfg) +} + // validateImage rejects an enabled hosted provider with no class or no // resolvable credential, and a Default naming a provider that is not both // present and enabled -- either would let /image silently fall back to @@ -237,6 +278,18 @@ func validateProviders(providers map[string]config.Provider) error { return nil } +// validateIdentityStructure judges the shape of the identity definitions: the +// fields a process needs to know which identities exist and which credentials +// each one commits and calls with. The repository lists inside them are judged +// by validateRepositoryContents, because repositories are a control-plane +// resource that replaces the file's list after the merge. +func validateIdentityStructure(cfg *config.Config) error { + if len(cfg.Identities) == 0 { + return validateSingleIdentity(cfg) + } + return validateIdentities(cfg.Identities) +} + func validateIdentities(identities []config.IdentityConfig) error { for i, id := range identities { if id.Name == "" { @@ -254,9 +307,6 @@ func validateIdentities(identities []config.IdentityConfig) error { if !ForgeDisabled(id.Forge.Type) && id.Forge.Token == (config.SecretRef{}) { return fmt.Errorf("%w: identities[%d].forge.token is required (each identity needs its own secret reference; unlike the top-level [forge], there is no default)", ErrInvalidInput, i) } - if err := validateRepos(id.Repos); err != nil { - return fmt.Errorf("identities[%d]: %w", i, err) - } } return nil } @@ -278,7 +328,23 @@ func validateSingleIdentity(cfg *config.Config) error { if !oneOf(cfg.Forge.Type, forgeTypes) { return fmt.Errorf("%w: forge.type %q (want %s)", ErrInvalidInput, cfg.Forge.Type, list(forgeTypes)) } - return validateRepos(cfg.Repos) + return nil +} + +// validateRepositoryContents judges the repositories themselves, whichever list +// they appear in. Repository policies are a control-plane resource seeded from +// these, so the contents are verified on the effective document -- the one the +// database's list has already replaced the file's copy in. +func validateRepositoryContents(cfg *config.Config) error { + if len(cfg.Identities) == 0 { + return validateRepos(cfg.Repos) + } + for i, id := range cfg.Identities { + if err := validateRepos(id.Repos); err != nil { + return fmt.Errorf("identities[%d]: %w", i, err) + } + } + return nil } func validateRepos(repos []config.Repo) error { diff --git a/internal/infrastructure/configuration/validate_test.go b/internal/infrastructure/configuration/validate_test.go index 412ab1957..b7176a713 100644 --- a/internal/infrastructure/configuration/validate_test.go +++ b/internal/infrastructure/configuration/validate_test.go @@ -69,6 +69,12 @@ func TestValidate_IsCallableOutsideThePackage(t *testing.T) { } } +// TestValidate_RejectsTheSameProblemsAsLoaderLoad pins the direction that still +// holds after archie-core-i3qm split the bootstrap checks from the effective +// ones: every problem the load path rejects, Validate rejects too. Validate +// additionally rejects the settings the control plane owns, which the load path +// must let through (validateBootstrap) so a stale TOML value cannot fail a +// process's startup. The name is kept from when the two check sets were equal. func TestValidate_RejectsTheSameProblemsAsLoaderLoad(t *testing.T) { tests := []struct { name string