fix(controlplane): a kind with no stored value leaves the file's value in force

The fix-closure review found the previous pass moved the failure instead of
ending it. A seed the resource validator refuses leaves the kind ABSENT, and
RuntimeConfig treated an absent kind as fatal, so archied and the Gateway
refused to start with "control-plane resource not found" -- a database-named
error that editing config.toml cannot clear. Reproduced before the fix: every
kind in the new table failed that way.

Two changes, one on each side of the seam:

- Client.RuntimeConfig (and RuntimeChatConfig) now treat a kind with no stored
  value as "the file document's value stays in effect", which is what the
  skip-and-report comment already claimed. The version map records no version
  for such a kind, so nothing is reported as applied. A value the file layer
  also refuses still fails closed, at configuration.Validate, with a message
  naming the file's own field -- and the file is the fix.

- Which repository lists are valid can no longer differ between the layers.
  configuration.ValidateRepositories is now the single rule set (owner and name
  present, no duplicates, compilable test glob) and the repository-policies
  resource validator delegates to it. The file layer had no duplicate rule
  while the resource layer did, so a duplicate [[repos]] entry passed the loader
  and then vanished into a skipped seed.

Also, from the same review: the scheduling validator's "0s" carve-out is gone,
so a zero poll interval is refused at write time as configuration.Validate
refuses it (there is no defaulting behind a stored value); applyOverlayFile's
doc now says which overlay forms fold (a conf.d extra does not, and nothing
reads cfg.Extra); and plugin-settings' no-op validator is a named function with
the reason it accepts everything.
This commit is contained in:
sam committed 2026-09-21 22:12:32 +10:00
1 parent 042a86ce4a
commit 67ebeb8d52
9 files changed
+288 -95

No files matched your search

@@ -8,6 +8,8 @@ import (
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/samcharles93/archie-core/internal/app/controlplane"
"github.com/samcharles93/archie-core/internal/config"
@@ -18,7 +20,9 @@ import (
// controlPlaneStub answers Query from a fixed resource map. queryErr, when
// set, fails every Query so a test can drive the unreachable-State-Store
// path.
// path. A kind the map does not carry is answered the way the server answers
// one with no stored value -- codes.NotFound (controlplane.mapError) -- so a
// test can drive the absent-resource state a skipped seed leaves behind.
type controlPlaneStub struct {
values map[string]any
queryErr error
@@ -32,8 +36,12 @@ func (c *controlPlaneStub) Query(_ context.Context, request *pb.QueryRequest, _
if c.queryErr != nil {
return nil, c.queryErr
}
value, err := json.Marshal(c.values[request.Kind])
return &pb.QueryResponse{Resource: &pb.Resource{Kind: request.Kind, Version: 3, ValueJson: value}}, err
value, ok := c.values[request.Kind]
if !ok {
return nil, status.Error(codes.NotFound, "resource not found")
}
encoded, err := json.Marshal(value)
return &pb.QueryResponse{Resource: &pb.Resource{Kind: request.Kind, Version: 3, ValueJson: encoded}}, err
}
func (*controlPlaneStub) History(context.Context, *pb.HistoryRequest, ...grpc.CallOption) (*pb.HistoryResponse, error) {
@@ -67,11 +75,15 @@ func databaseOwnedResources() map[string]any {
// fileConfig is what the loader produces from config.toml alone: the layer a
// SIGHUP reload re-resolves, holding none of the database's values. It is a
// config the daemon would run, so it passes configuration.Validate -- the live
// apply path runs that same check over the snapshot it is about to publish.
// apply path runs that same check over the snapshot it is about to publish --
// and every field validation reads is filled in the way the loader's defaults
// fill it, so a kind with no stored value can fall back to this document (see
// TestRuntimeConfigKeepsTheFileValueWhenAKindHasNoStoredResource).
func fileConfig() config.Config {
return config.Config{
BotUser: "widget",
Forge: config.Forge{Type: "github"},
Dispatch: config.Dispatch{Trigger: "assignee"},
Providers: map[string]config.Provider{"file": {Class: "openai"}},
Models: map[string]string{"builder": "file/model"},
Repos: []config.Repo{{Owner: "acme", Name: "from-file"}},
@@ -81,7 +93,6 @@ func fileConfig() config.Config {
PluginDir: "/file/plugins",
Containers: config.ContainerConfig{Image: "archie:from-file", PullPolicy: "missing"},
Budgets: config.Budgets{MaxSteps: 1, WallClock: config.Duration(time.Minute)},
Dispatch: config.Dispatch{Trigger: "assignee"},
}
}
@@ -7,10 +7,12 @@ import (
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/app/controlplane"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
)
@@ -66,21 +68,29 @@ func TestLoadConfigAcceptsStaleDatabaseOwnedValues(t *testing.T) {
}
}
// staleBootGrace is how long one boot in
// TestStateStoreBootsWithStaleDatabaseOwnedValues may run before the test cancels
// it: short, because the assertion does not depend on it -- a refusal arrives as
// an error either before the cancel (first select) or after it (second select,
// where a non-Canceled error fails the test).
//
// staleShutdownGrace bounds the shutdown, and is deliberately not the 5s the
// single-boot regression test uses: this table runs five boots back to back on a
// host that may be busy, and the select returns the moment the process does, so
// the bound costs nothing when shutdown is quick and only decides how long a
// failure takes to report.
const (
staleBootGrace = 250 * time.Millisecond
staleShutdownGrace = 30 * time.Second
)
// TestStateStoreBootsWithStaleDatabaseOwnedValues is the process-level half of
// the item, and the layer that actually decides it: archie-state-store seeds
// every control-plane resource from the same file config on a fresh database
// (state_store.go, control.ImportConfig), and that seed is what runs the
// resource's own validator. A seed that does not validate is skipped and
// reported, not fatal -- the value stays file-owned, archied refuses to run with
// it, and the operator's fix in config.toml is what clears it.
// staleBootGrace is how long one boot in
// TestStateStoreBootsWithStaleDatabaseOwnedValues may run before the test
// cancels it. It can be short because the assertion does not depend on it: a
// refusal arrives as an error either before the cancel (first select) or after
// it (second select, where a non-Canceled error fails the test). The wait only
// gives the serving path a chance to prove it did not return immediately.
const staleBootGrace = 250 * time.Millisecond
// reported, not fatal -- the kind stays absent, the file's value is the one in
// effect, and the operator's fix in config.toml is what clears it.
func TestStateStoreBootsWithStaleDatabaseOwnedValues(t *testing.T) {
for _, tt := range staleDatabaseOwnedSettings {
t.Run(tt.name, func(t *testing.T) {
@@ -116,13 +126,108 @@ func TestStateStoreBootsWithStaleDatabaseOwnedValues(t *testing.T) {
if err != nil && !errors.Is(err, context.Canceled) {
t.Fatalf("RunStateStore returned %v, want nil or context.Canceled", err)
}
case <-time.After(shutdownGrace):
case <-time.After(staleShutdownGrace):
t.Fatal("RunStateStore did not return within the shutdown grace after cancellation")
}
})
}
}
// TestRuntimeConfigKeepsTheFileValueWhenAKindHasNoStoredResource is the other
// end of a seed the control plane refused: the kind stays ABSENT rather than
// being stored (ImportConfig skips it), and boot must then fall back to the
// file document's value. Treating an absent kind as fatal is how a config the
// file layer blesses left the daemon unbootable with a database-named error,
// and left editing config.toml unable to fix it.
func TestRuntimeConfigKeepsTheFileValueWhenAKindHasNoStoredResource(t *testing.T) {
tests := []struct {
name string
kind string
check func(t *testing.T, cfg config.Config)
}{
{
name: "repository policies",
kind: controlplane.RepositoryPoliciesKind,
check: func(t *testing.T, cfg config.Config) {
t.Helper()
if len(cfg.Repos) != 1 || cfg.Repos[0].Name != "from-file" {
t.Errorf("Repos = %+v, want the file's from-file", cfg.Repos)
}
},
},
{
name: "scheduling policy",
kind: controlplane.SchedulingPolicyKind,
check: func(t *testing.T, cfg config.Config) {
t.Helper()
if cfg.PollInterval != config.Duration(time.Minute) {
t.Errorf("PollInterval = %v, want the file's 1m", cfg.PollInterval)
}
},
},
{
name: "container runtime policies",
kind: controlplane.ContainerRuntimePoliciesKind,
check: func(t *testing.T, cfg config.Config) {
t.Helper()
if cfg.Containers.Image != "archie:from-file" {
t.Errorf("Containers.Image = %q, want the file's archie:from-file", cfg.Containers.Image)
}
},
},
{
name: "providers",
kind: controlplane.ProviderSettingsKind,
check: func(t *testing.T, cfg config.Config) {
t.Helper()
if got := cfg.Providers["file"].Class; got != "openai" {
t.Errorf("Providers[file].Class = %q, want the file's openai", got)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resources := databaseOwnedResources()
delete(resources, tt.kind)
b := newReloadBoot(t, &controlPlaneStub{values: resources})
if err := b.loadRuntimeConfig(t.Context()); err != nil {
t.Fatalf("loadRuntimeConfig: %v (a kind with no stored value must leave the file's value in effect)", err)
}
tt.check(t, b.cfgHolder.Get())
})
}
}
// TestDuplicateRepositoriesFailOnTheFilesOwnMessage is the end of the path the
// fix-closure review found. A duplicate [[repos]] entry is refused by the
// repository-policies validator, so the kind is never seeded and stays absent --
// and boot must then fail on the FILE's copy of the value, naming the duplicate,
// with the file as the fix. It used to fail on "control-plane resource not
// found": a database-named error that editing config.toml could not clear.
func TestDuplicateRepositoriesFailOnTheFilesOwnMessage(t *testing.T) {
resources := databaseOwnedResources()
delete(resources, controlplane.RepositoryPoliciesKind)
b := newReloadBoot(t, &controlPlaneStub{values: resources})
b.cfg.Repos = []config.Repo{{Owner: "acme", Name: "app"}, {Owner: "acme", Name: "app"}}
err := b.loadRuntimeConfig(t.Context())
if err == nil {
t.Fatal("loadRuntimeConfig accepted a duplicate repository list")
}
if !errors.Is(err, configuration.ErrInvalidInput) {
t.Errorf("loadRuntimeConfig = %v, want it to wrap configuration.ErrInvalidInput", err)
}
if !strings.Contains(err.Error(), "duplicates") {
t.Errorf("loadRuntimeConfig = %v, want the duplicate named the way configuration.Validate names it", err)
}
if strings.Contains(err.Error(), "not found") {
t.Errorf("loadRuntimeConfig = %v, want no absent-resource error: the file's value is the one in effect", 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
+24 -11
View File
@@ -110,17 +110,7 @@ func RunStateStore(ctx context.Context, options StateStoreOptions) error {
if err != nil {
return fmt.Errorf("import control-plane resources: %w", err)
}
for _, skip := range skipped {
// Reported, not fatal: a stale TOML value in a setting the database owns
// must not stop this process from starting
// (docs/prds/runtime-control-plane.md, "Bootstrap, migration, and
// recovery"), and the resource stays absent, so the file's value is
// still the one in effect. Whether it may RUN is decided where it is
// used: boot.runtimeConfig validates the effective document and refuses
// to start archied or the Gateway with it.
b.log.Error("control-plane resource not seeded; the file config's value stays in effect",
"kind", skip.Kind, "err", skip.Err)
}
b.reportUnseededResources(skipped)
if err := migrateLegacySchedules(ctx, control, b.cfg.DBPath, versions[controlplane.SchedulesKind]); err != nil {
return fmt.Errorf("migrate schedules: %w", err)
}
@@ -153,6 +143,29 @@ func RunStateStore(ctx context.Context, options StateStoreOptions) error {
return serveStateStore(ctx, listener, deps, opts)
}
// reportUnseededResources logs every kind ImportConfig could not seed.
//
// Reported, not fatal: a stale TOML value in a setting the database owns must
// not stop this process from starting (docs/prds/runtime-control-plane.md,
// "Bootstrap, migration, and recovery"). The kind stays ABSENT rather than
// half-written, so the file document's value is the one in effect for every
// reader -- the layering leaves a kind with no stored value alone
// (controlplane.resourceReader) -- and the operator's fix is still the file.
// Whether the value may RUN is decided where it is used: boot.runtimeConfig
// validates the effective document and refuses to start archied or the Gateway
// with one it cannot run. The seed is retried on this process's next start, so
// a corrected config.toml takes effect when archie-state-store restarts.
//
// It is a method rather than a loop in RunStateStore because the boot sequence
// is at its complexity budget: the branch belongs to the report's own policy,
// not to the sequence that triggers it.
func (b *boot) reportUnseededResources(skipped []controlplane.SeedSkip) {
for _, skip := range skipped {
b.log.Error("control-plane resource not seeded; the kind stays absent and the file's value is the one in effect",
"kind", skip.Kind, "err", skip.Err)
}
}
// openStateStoreControlPlane builds the State Store's control plane server: the
// validating side of the workflow step vocabulary, registered here at the
// composition root before the first definition is read or replaced. The same
@@ -41,33 +41,27 @@ func operationalDefinitions() []Definition {
{Kind: ToolSettingsKind, Title: "Tool and MCP settings", ApplyMode: "restart-required", Schema: objectSchema, Seed: seedTools, Validate: validateTools},
{Kind: PluginSettingsKind, Title: "Plugin settings", ApplyMode: "restart-required", Schema: objectSchema, Seed: func(cfg config.Config) any {
return pluginSettings{cfg.PluginDir, cfg.ModuleDir, cfg.SecretEngineDir, cfg.SkillsDir}
}, Validate: func(input []byte) error { return validateAs(input, func(pluginSettings) error { return nil }) }},
}, Validate: validatePluginSettings},
{Kind: ContainerRuntimePoliciesKind, Title: "Container runtime policies", ApplyMode: "restart-required", Schema: objectSchema, Seed: func(cfg config.Config) any { return cfg.Containers }, Validate: validateContainers},
}
}
// validatePluginSettings accepts every value: the four directories are free-form
// operator paths with no cross-field rule and no rule the configuration package
// applies either (configuration.Validate has no say over them), so there is
// nothing yet for this validator to enforce. It stays a real function rather than
// an inline no-op so the place to add a rule is obvious when one exists -- e.g.
// "a path that is absolute" or "a skills dir under the plugin dir".
func validatePluginSettings(input []byte) error {
return validateAs(input, func(pluginSettings) error { return nil })
}
func validateRepositories(input []byte) error {
return validateAs(input, func(repos []config.Repo) error {
seen := make(map[string]struct{}, len(repos))
for _, repo := range repos {
if strings.TrimSpace(repo.Owner) == "" || strings.TrimSpace(repo.Name) == "" {
return fmt.Errorf("repository owner and name are required")
}
if _, exists := seen[repo.FullName()]; exists {
return fmt.Errorf("duplicate repository %q", repo.FullName())
}
seen[repo.FullName()] = struct{}{}
// The repository-policies resource replaces the file's list, so the
// rules that judge the file's list have to hold here too. They come
// from the configuration package rather than being restated: a
// resource that accepts what configuration.Validate rejects becomes a
// stored value the operator cannot clear by editing config.toml.
if err := configuration.ValidateTestGlob(repo.ResolvedTestGlob()); err != nil {
return err
}
}
return nil
})
// The rules live in the configuration package and are shared with the file
// document's own validation (configuration.ValidateRepositories): which
// repository lists are valid cannot differ between the file that seeds this
// resource and the resource that replaces it.
return validateAs(input, configuration.ValidateRepositories)
}
func validateScheduling(input []byte) error {
@@ -75,13 +69,14 @@ func validateScheduling(input []byte) error {
if policy.MaxRetries < 0 {
return fmt.Errorf("max_retries must not be negative")
}
if policy.PollInterval != "0s" {
interval, err := time.ParseDuration(policy.PollInterval)
if err != nil || interval < 0 {
return fmt.Errorf("poll_interval must be a non-negative duration")
}
// A positive interval, not a non-negative one: the file layer's rule is
// the same, and there is no defaulting behind a stored value -- a stored
// "0s" reaches cfg.PollInterval as zero and stops archied starting.
interval, err := time.ParseDuration(policy.PollInterval)
if err != nil || interval <= 0 {
return fmt.Errorf("poll_interval must be a positive duration")
}
// Same reason as the repository glob above: RuntimeConfig replaces
// Same reason as the repository rules above: RuntimeConfig replaces
// cfg.Dispatch from this resource, and the daemon refuses to start with a
// trigger it does not know.
if !configuration.DispatchTriggerValid(policy.Dispatch.Trigger) {
+4 -2
View File
@@ -72,8 +72,10 @@ type SeedSkip struct {
// from the file config, and reports the version of every kind it left alone.
//
// A seed the resource's own validator refuses is SKIPPED and returned in skipped,
// not fatal. The setting it would have carried stays file-owned, so the value in
// effect is still the file's, and the fix is still the file's.
// not fatal. Nothing is written for that kind, so it stays ABSENT and the file
// document's value stays the one in effect (Client.RuntimeConfig leaves an absent
// kind alone), with the fix still in the file. The seed is retried on the next
// start of this process, so correcting config.toml re-seeds it.
// docs/prds/runtime-control-plane.md, "Bootstrap, migration, and recovery":
// after migration, settings in TOML are ignored and cannot block State Store
// startup -- and this import runs on the State Store's startup path. Fail closed
+53 -23
View File
@@ -13,12 +13,21 @@ import (
)
// resourceReader is the one read the layering performs: a resource kind's value
// and the version it came from. The gRPC client and the in-process server both
// provide it, so "what the database covers in the running config" has exactly
// one implementation. The offline validate runs the same code as boot, which is
// the only way its verdict can be boot's verdict.
// and the version it came from, or nothing at all when the store holds no value
// for that kind. The gRPC client and the in-process server both provide it, so
// "what the database covers in the running config" has exactly one
// implementation. The offline validate runs the same code as boot, which is the
// only way its verdict can be boot's verdict.
//
// found is false only for the reader that dials a live State Store: a kind with
// no stored value leaves the file document's value in effect instead of failing
// the process, which is the state a seed ImportConfig refused leaves behind
// (controlplane.Server.ImportConfig skips it) and the state of a database the
// migration has not reached yet. storeReader never reports absent -- it answers
// with the seed the State Store would write -- so the offline verdict stays
// boot's.
type resourceReader interface {
query(ctx context.Context, kind string, decode func([]byte) error) (int64, error)
query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error)
}
// RuntimeConfig applies restart-scoped database resources over bootstrap
@@ -67,27 +76,28 @@ type storeReader struct {
seeds map[string][]byte
}
func (r storeReader) query(ctx context.Context, kind string, decode func([]byte) error) (int64, error) {
func (r storeReader) query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
resource, err := r.resources.Resource(ctx, kind)
if errors.Is(err, store.ErrResourceNotFound) {
seed, ok := r.seeds[kind]
if !ok {
return 0, fmt.Errorf("read %s: %w", kind, store.ErrResourceNotFound)
return 0, false, fmt.Errorf("read %s: %w", kind, store.ErrResourceNotFound)
}
if err := decode(seed); err != nil {
return 0, fmt.Errorf("decode %s seed: %w", kind, err)
return 0, false, fmt.Errorf("decode %s seed: %w", kind, err)
}
// No version: nothing has been stored for this kind, so nothing has
// been applied either.
return 0, nil
// been applied either. It is still a value to layer in -- the seed --
// which is why this reader never reports the kind absent.
return 0, true, nil
}
if err != nil {
return 0, fmt.Errorf("read %s: %w", kind, err)
return 0, false, fmt.Errorf("read %s: %w", kind, err)
}
if err := decode(resource.Value); err != nil {
return 0, fmt.Errorf("decode %s: %w", kind, err)
return 0, false, fmt.Errorf("decode %s: %w", kind, err)
}
return resource.Version, nil
return resource.Version, true, nil
}
func runtimeConfigFrom(ctx context.Context, reader resourceReader, base config.Config) (config.Config, map[string]int64, error) {
@@ -171,7 +181,7 @@ func runtimeToolConfigFrom(ctx context.Context, reader resourceReader, versions
func runtimeChatConfigFrom(ctx context.Context, reader resourceReader, base config.ChatConfig) (config.ChatConfig, int64, error) {
out := base
version, err := reader.query(ctx, ChannelSettingsKind, func(value []byte) error {
version, found, err := reader.query(ctx, ChannelSettingsKind, func(value []byte) error {
var settings channelSettings
if err := json.Unmarshal(value, &settings); err != nil {
return err
@@ -186,17 +196,31 @@ func runtimeChatConfigFrom(ctx context.Context, reader resourceReader, base conf
}
return nil
})
return out, version, err
if err != nil {
return out, 0, err
}
if !found {
return out, 0, nil
}
return out, version, nil
}
// layerResource decodes a resource and records the version it came from, so the
// layering ends up holding the version of every kind it applied.
//
// A kind with no stored value is not an error and records no version: the file
// document's value stays in effect. That is the state a seed the resource
// validator refused leaves behind (controlplane.Server.ImportConfig skips it),
// and failing here instead would stop the process with a database-named error
// that editing config.toml cannot clear.
func layerResource(ctx context.Context, reader resourceReader, versions map[string]int64, kind string, decode func([]byte) error) error {
version, err := reader.query(ctx, kind, decode)
version, found, err := reader.query(ctx, kind, decode)
if err != nil {
return err
}
versions[kind] = version
if found {
versions[kind] = version
}
return nil
}
@@ -205,17 +229,23 @@ func layerResourceJSON(ctx context.Context, reader resourceReader, versions map[
}
// query returns the version of the resource it decoded, so callers that layer
// a resource in can report the version they applied.
func (c *Client) query(ctx context.Context, kind string, decode func([]byte) error) (int64, error) {
// a resource in can report the version they applied. A kind the store holds no
// value for is reported as not found rather than as an error: the caller leaves
// the file document's value in effect (see resourceReader).
func (c *Client) query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: kind})
if err != nil {
return 0, clientError(err)
mapped := clientError(err)
if errors.Is(mapped, ErrNotFound) {
return 0, false, nil
}
return 0, false, mapped
}
if response.Resource == nil {
return 0, fmt.Errorf("%s resource missing", kind)
return 0, false, fmt.Errorf("%s resource missing", kind)
}
if err := decode(response.Resource.ValueJson); err != nil {
return 0, fmt.Errorf("decode %s: %w", kind, err)
return 0, false, fmt.Errorf("decode %s: %w", kind, err)
}
return response.Resource.Version, nil
return response.Resource.Version, true, nil
}
@@ -48,6 +48,17 @@ func TestResourceValidatorsRejectWhatEffectiveValidationRejects(t *testing.T) {
mutate: func(cfg *config.Config) { cfg.PollInterval = config.Duration(-5 * time.Second) },
wantErr: true,
},
{
// A zero interval is what applyDefaults replaces before validation
// runs, so the file layer only ever sees it as a value to reject. The
// resource has no defaulting behind it: a stored "0s" would become
// cfg.PollInterval = 0 and stop archied starting.
name: "zero poll interval",
kind: SchedulingPolicyKind,
value: map[string]any{"poll_interval": "0s", "max_retries": 0, "dispatch": map[string]any{"trigger": "assignee"}},
mutate: func(cfg *config.Config) { cfg.PollInterval = 0 },
wantErr: true,
},
{
name: "valid repository policies",
kind: RepositoryPoliciesKind,
@@ -62,6 +73,18 @@ func TestResourceValidatorsRejectWhatEffectiveValidationRejects(t *testing.T) {
mutate: func(cfg *config.Config) { cfg.Repos = []config.Repo{{Owner: "acme", Name: "app", TestGlob: "["}} },
wantErr: true,
},
{
// Both layers have to call a duplicate list invalid. When only the
// resource did, a config the file layer blessed left the daemon
// unbootable on a database-named error.
name: "duplicate repository",
kind: RepositoryPoliciesKind,
value: []config.Repo{{Owner: "acme", Name: "app"}, {Owner: "acme", Name: "app"}},
mutate: func(cfg *config.Config) {
cfg.Repos = []config.Repo{{Owner: "acme", Name: "app"}, {Owner: "acme", Name: "app"}}
},
wantErr: true,
},
{
name: "valid container runtime policies",
kind: ContainerRuntimePoliciesKind,
@@ -35,11 +35,14 @@ func ApplyOverlayValues(cfg *config.Config, overrides map[string]any) error {
// file's keys that nothing consumes. target is whatever the overlay addresses:
// the whole config for a main file, one sub-struct for a feature file.
//
// Both overlay forms go through it -- the single overlay file, and every file an
// overlay directory contributes -- so neither form can lose the fields of a
// map-valued entry it only partly addresses. Decoding an overlay into target
// directly replaces such an entry wholesale, which is how an overlay naming only
// services.state.target cleared the target_token the base file set.
// Both processed overlay forms go through it -- the single overlay file, and an
// overlay directory's main and feature files -- so neither can lose the fields of
// a map-valued entry it only partly addresses. A conf.d/*.yaml extra in an
// overlay directory does not: decodeExtra stores a whole file under
// cfg.Extra[name] and nothing reads that map, so there is no entry to fold into.
// Decoding an overlay into target directly replaces such an entry wholesale,
// which is how an overlay naming only services.state.target cleared the
// target_token the base file set.
func applyOverlayFile(path string, target any) ([]string, error) {
mapping, err := decodeFileMapping(path)
if err != nil {
@@ -353,34 +353,45 @@ func validateSingleIdentity(cfg *config.Config) error {
// (docs/architecture/configuration.md's IdentityConfig note).
func validateRepositoryContents(cfg *config.Config) error {
if len(cfg.Identities) == 0 {
return validateRepos(cfg.Repos)
return ValidateRepositories(cfg.Repos)
}
for i, id := range cfg.Identities {
if err := validateRepos(id.Repos); err != nil {
if err := ValidateRepositories(id.Repos); err != nil {
return fmt.Errorf("identities[%d]: %w", i, err)
}
}
return nil
}
func validateRepos(repos []config.Repo) error {
// ValidateRepositories judges a repository list: owner and name present, no
// duplicate entries, and a test glob the gate can compile. It is exported
// because the repository-policies resource validator (internal/app/controlplane)
// judges the same list -- that resource is seeded from these and replaces them
// wholesale, so the two layers must not disagree about which lists are valid, in
// either direction: a list one layer accepts and the other refuses is a config
// that boots file-side and stores nowhere, or a stored value the daemon then
// refuses to start with.
func ValidateRepositories(repos []config.Repo) error {
seen := make(map[string]struct{}, len(repos))
for i, r := range repos {
if r.Owner == "" || r.Name == "" {
return fmt.Errorf("%w: repos[%d] needs owner and name", ErrInvalidInput, i)
}
if err := ValidateTestGlob(r.ResolvedTestGlob()); err != nil {
if _, exists := seen[r.FullName()]; exists {
return fmt.Errorf("%w: repos[%d] duplicates %q", ErrInvalidInput, i, r.FullName())
}
seen[r.FullName()] = struct{}{}
if err := validateTestGlob(r.ResolvedTestGlob()); err != nil {
return fmt.Errorf("%w: repos[%d] %w", ErrInvalidInput, i, err)
}
}
return nil
}
// ValidateTestGlob reports whether glob is a pattern the test-protection gate can
// compile. It is exported because the repository-policies resource validator
// (internal/app/controlplane) judges the same field: that resource is seeded from
// the file's repositories and replaces them wholesale, and a pattern the gate
// cannot compile is a value that must not be storable either.
func ValidateTestGlob(glob string) error {
// validateTestGlob reports whether glob is a pattern the test-protection gate can
// compile. It has one caller on each side through ValidateRepositories, so the
// rule has one definition rather than a copy per layer.
func validateTestGlob(glob string) error {
if glob == "" {
return nil
}