diff --git a/.agents/skills/archie-config-and-flags/SKILL.md b/.agents/skills/archie-config-and-flags/SKILL.md index 508a4b073..7193f7afc 100644 --- a/.agents/skills/archie-config-and-flags/SKILL.md +++ b/.agents/skills/archie-config-and-flags/SKILL.md @@ -174,6 +174,7 @@ looking at before reporting one: | `max_uptime` | Default `60m` only when enabled; caps container lifetime. | | `volume_ttl` | Default `72h` only when enabled; negative rejected. | | `pull_policy` | Default `missing` when enabled. Pool code recognises `missing` and `always`; other strings silently skip pre-pull. | +| `registry_auth` | `SecretRef` (file-owned, `json:"-"`). Resolved at boot by `archied`'s `resolveRegistryAuth` and passed to `container.Config.RegistryAuth`; the pool base64url-encodes it into the `X-Registry-Auth` header (`internal/container/pool.go`'s `registryAuthHeader`). Zero ref = anonymous pull. Unresolvable/empty credential or an unencodable one warns and pulls anonymously rather than failing the pool. A half-named ref is refused by `configuration.validateContainers`. Preserved across the `container-runtime-policies` layering (the document cannot carry a secret ref), unlike `image`/`pull_policy`. | | `network` | Empty uses best-effort self-network detection. Repository overlay pins `archie-core_default`. | | `profiles` | Named agent execution environments (image or Kit); resolved per task dispatch (`ContainerConfig.Profile`). Own control-plane resource `agent-profiles` (`json:"-"` on the field so `container-runtime-policies` never carries it), `ApplyMode: live` -- a profile change applies without a restart, unlike the rest of `[containers]`. | | `credentials` | Maps a Kit `credential@1` service to a secret (`config.CredentialBinding`). Own control-plane resource `credential-bindings`, `ApplyMode: live`, `json:"-"` on the field. `kitrun.Launcher` reads it fresh from the live `config.Holder` on every `Launch`, never a value captured at boot -- see `internal/infrastructure/kitrun/launcher.go`'s `resolveCredentials`. A binding alone grants nothing; see `org`/`granted_credentials` below. | diff --git a/CLAUDE.md b/CLAUDE.md index 90c74cf35..be5ba9870 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -89,8 +89,10 @@ domain migration. reload of `nats.url` logs an error requiring restart to prevent split-brain delivery. - **Container pull policy:** `containers.pull_policy` must be `"missing"` - (default) or `"always"`. Private registries return 401 on `"always"` because - no auth is sent. Refresh explicitly via `docker compose pull agent`. + (default) or `"always"`. Private registries need `containers.registry_auth` + (a `SecretRef` resolving to a `registry.AuthConfig` JSON document); without it + the pull is anonymous and a private registry answers 401. Refresh explicitly + via `docker compose pull agent`. - **Docker Compose:** `docker-compose.yml` carries optional external NATS and the `agent` build stanza. The agent uses a `build` profile so `up -d nats` never starts it as a persistent service. diff --git a/config.example.toml b/config.example.toml index f1503fd7d..45bc3619f 100644 --- a/config.example.toml +++ b/config.example.toml @@ -442,6 +442,15 @@ target = "127.0.0.1:9090" # REQUIRED. The State Store gRPC address arch # volume_ttl = "72h" # max age for persistent repo volumes and Git clone caches # pull_policy = "missing" # "missing" or "always" # +# A private registry needs a credential the daemon sends on pull (an +# anonymous pull always answers 401 there, whatever the policy). The value is +# a Docker registry.AuthConfig JSON document -- {"username":"bot","password":"..."} +# ("identitytoken"/"registrytoken" are alternatives; "serveraddress" is +# optional and defaults to the image's registry). Unset means anonymous pulls, +# exactly as before this key existed; a reference that cannot be resolved +# warns and pulls anonymously rather than failing the daemon. +# registry_auth = { engine = "env", key = "ARCHIE_REGISTRY_AUTH" } +# # Agent profiles a workflow selects with `profile: `. A workflow that # names none runs under [containers].image with every tool. # Once the State Store holds container policies, edit profiles in the diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 6b39c5d80..555d31610 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -822,7 +822,7 @@ func (b *boot) startEmbeddedNATS(ctx context.Context) (string, string, error) { // not require Docker; repository tasks park instead of falling back to a host // model loop. func (b *boot) setupContainers(ctx context.Context) func() { - containerPool, storeBackend, closeDocker := startContainers(ctx, b.cfg, b.log) + containerPool, storeBackend, closeDocker := startContainers(ctx, b.cfg, b.secrets, b.log) b.containerPool = containerPool b.storeBackend = storeBackend return closeDocker diff --git a/internal/app/archied/main.go b/internal/app/archied/main.go index 109aba70c..a95c1f6e7 100644 --- a/internal/app/archied/main.go +++ b/internal/app/archied/main.go @@ -971,6 +971,7 @@ func safePluginInfo(p plugin.Plugin) (name, version string) { func startContainers( ctx context.Context, cfg config.Config, + secrets *secret.Registry, log *slog.Logger, ) (*container.Pool, storage.Backend, func()) { noop := func() {} @@ -991,6 +992,7 @@ func startContainers( MaxConcurrency: cfg.Containers.MaxConcurrency, MaxUptime: cfg.Containers.MaxUptime.Std(), PullPolicy: cfg.Containers.PullPolicy, + RegistryAuth: resolveRegistryAuth(cfg.Containers.RegistryAuth, secrets, log), Network: cfg.Containers.Network, DockerClient: dockerCli, // Only the embedded broker binds a discovered host gateway. The @@ -998,9 +1000,7 @@ func startContainers( RequireHostGateway: cfg.NATS.Mode == config.NATSModeEmbedded, }, log) if err != nil { - // A missing image is recoverable by hand. The daemon sends no registry - // credentials on pull (internal/container/pool.go), so a private - // registry always needs the operator's CLI to fetch it first. + // A missing image is recoverable by hand. log.Error("autonomous workflows unavailable: container pool unavailable", "err", err, "hint", "run `docker compose pull agent` (or `build agent`) so the image is present locally") return nil, storage.NewDockerBackend(dockerCli), closeDocker @@ -1016,3 +1016,31 @@ func startContainers( closeDocker() } } + +// resolveRegistryAuth resolves [containers].registry_auth into the credential +// the container pool sends on pull. It returns "" -- an anonymous pull, the +// pool's behaviour before the field existed -- whenever the credential is not +// configured or cannot be resolved, because a missing registry credential must +// not stop autonomous work: the pool only needs it for a private registry. The +// warning is what makes the 401 that a private registry will still answer +// explicable after a typo or an unset secret. A ref that names only one half is +// a config error caught by configuration.Validate, not a missing credential. +func resolveRegistryAuth(ref secret.SecretRef, secrets *secret.Registry, log *slog.Logger) string { + if ref == (secret.SecretRef{}) { + return "" + } + if secrets == nil { + log.Warn("registry credential unavailable; pulling without auth", "reason", "no secret registry", "engine", ref.Engine, "key", ref.Key) + return "" + } + value, err := secrets.Resolve(ref) + if err != nil { + log.Warn("registry credential unavailable; pulling without auth", "err", err, "engine", ref.Engine, "key", ref.Key) + return "" + } + if strings.TrimSpace(value) == "" { + log.Warn("registry credential resolved empty; pulling without auth", "engine", ref.Engine, "key", ref.Key) + return "" + } + return value +} diff --git a/internal/app/archied/main_test.go b/internal/app/archied/main_test.go index 801628929..44f9488b1 100644 --- a/internal/app/archied/main_test.go +++ b/internal/app/archied/main_test.go @@ -202,6 +202,65 @@ func TestResolveForgeBuildsClientWhenTokenResolves(t *testing.T) { } } +// resolveRegistryAuth hands the container pool the resolved +// [containers].registry_auth, and degrades to an anonymous pull (with a +// warning) whenever there is nothing resolvable to hand it. A deployment with +// no private registry must be unaffected by the field existing, and a private +// registry whose secret is missing or misnamed must still let every task that +// does not need it run -- the pool simply pulls without auth, as it always did, +// and the warning explains the 401 that follows. +func TestResolveRegistryAuthDegradesRatherThanFailing(t *testing.T) { + t.Run("unconfigured is silent", func(t *testing.T) { + rec := &recordingHandler{} + if got := resolveRegistryAuth(secret.SecretRef{}, secret.NewRegistry(), slog.New(rec)); got != "" { + t.Errorf("resolveRegistryAuth(empty) = %q, want empty", got) + } + if warns := rec.Warnings(); len(warns) != 0 { + t.Errorf("unconfigured registry auth logged %v, want nothing", warns) + } + }) + + t.Run("resolved credential reaches the pool", func(t *testing.T) { + t.Setenv("ARCHIE_TEST_REGISTRY_AUTH", `{"username":"bot","password":"s3cret"}`) + rec := &recordingHandler{} + got := resolveRegistryAuth( + secret.SecretRef{Engine: "env", Key: "ARCHIE_TEST_REGISTRY_AUTH"}, + secret.NewRegistry(), slog.New(rec)) + if want := `{"username":"bot","password":"s3cret"}`; got != want { + t.Errorf("resolveRegistryAuth = %q, want the resolved secret %q", got, want) + } + if warns := rec.Warnings(); len(warns) != 0 { + t.Errorf("a resolvable credential logged %v, want nothing", warns) + } + }) + + t.Run("unresolvable credential degrades with a warning", func(t *testing.T) { + rec := &recordingHandler{} + got := resolveRegistryAuth( + secret.SecretRef{Engine: "env", Key: "ARCHIE_TEST_REGISTRY_AUTH_DEFINITELY_UNSET"}, + secret.NewRegistry(), slog.New(rec)) + if got != "" { + t.Errorf("resolveRegistryAuth = %q, want empty for an unresolvable credential", got) + } + warns := rec.Warnings() + if len(warns) != 1 || !strings.Contains(warns[0], "registry credential unavailable") { + t.Errorf("warnings = %v, want exactly one saying the credential was unavailable", warns) + } + }) + + t.Run("no secret registry degrades instead of panicking", func(t *testing.T) { + rec := &recordingHandler{} + got := resolveRegistryAuth( + secret.SecretRef{Engine: "env", Key: "ANYTHING"}, nil, slog.New(rec)) + if got != "" { + t.Errorf("resolveRegistryAuth = %q, want empty with no secret registry", got) + } + if warns := rec.Warnings(); len(warns) != 1 { + t.Errorf("warnings = %v, want exactly one", warns) + } + }) +} + func TestResolveProviderSecretSetsPrivateRuntimeEnvironment(t *testing.T) { registry := secret.NewRegistry() registry.Register(providerSecretEngine{value: "provider-secret"}) diff --git a/internal/app/archied/reload.go b/internal/app/archied/reload.go index a194b5015..edaa23f82 100644 --- a/internal/app/archied/reload.go +++ b/internal/app/archied/reload.go @@ -157,9 +157,9 @@ var reloadableFields = map[string]bool{ // same shape one layer down: kitrun.Launcher reads Config.Get().Containers. // Credentials fresh on every Launch (CredentialBindingsKind), never a slice // captured once when the launcher was built. Everything else in -// Containers (Image, MaxConcurrency, MaxUptime, PullPolicy, Network) is -// frozen in the startup-built container pool -- the dispatchers re-read -// MaxConcurrency but the pool captures it at construction +// Containers (Image, MaxConcurrency, MaxUptime, PullPolicy, Network, +// RegistryAuth) is frozen in the startup-built container pool -- the +// dispatchers re-read MaxConcurrency but the pool captures it at construction // (container/pool.go:94,163), so a change only partially applies and must // warn requires-restart. Forge.Host is carried into TaskContext by ForTask // (display/link building only); the forge client itself is startup-built, so diff --git a/internal/app/archied/state_store_regression_test.go b/internal/app/archied/state_store_regression_test.go index 579f21e9e..fd6901745 100644 --- a/internal/app/archied/state_store_regression_test.go +++ b/internal/app/archied/state_store_regression_test.go @@ -15,6 +15,7 @@ import ( "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" + "github.com/samcharles93/archie-core/internal/secret" ) // startupGrace is how long RunStateStore is given to either refuse the config @@ -46,7 +47,7 @@ func TestExternalServicesDoNotRequireBridgeNetwork(t *testing.T) { cfg.NATS.Mode = config.NATSModeExternal cfg.Containers.Network = "host" cfg.Services = config.Services{config.ServiceNameState: {Target: "state.example.test:9090"}} - pool, _, cleanup := startContainers(t.Context(), cfg, slog.New(slog.DiscardHandler)) + pool, _, cleanup := startContainers(t.Context(), cfg, secret.NewRegistry(), slog.New(slog.DiscardHandler)) defer cleanup() if pool == nil { t.Fatal("external services on host networking must construct a worker pool") diff --git a/internal/app/controlplane/runtime_config.go b/internal/app/controlplane/runtime_config.go index e5222ae22..94032f3bf 100644 --- a/internal/app/controlplane/runtime_config.go +++ b/internal/app/controlplane/runtime_config.go @@ -213,9 +213,16 @@ func runtimeToolConfigFrom(ctx context.Context, reader controlplanerpc.ResourceR // now), and settings() never sets it, so whatever the AgentProfileKind // layering step below assigns is what's left standing regardless of // which of the two runs first. + // + // RegistryAuth is the same shape of file-owned field, with no resource of + // its own to restore it later the way CredentialBindingsKind restores + // Credentials: the document cannot carry a secret reference, so it has to + // survive this assignment or the daemon pulls anonymously again. profiles := out.Containers.Profiles + registryAuth := out.Containers.RegistryAuth out.Containers = policies.settings() out.Containers.Profiles = profiles + out.Containers.RegistryAuth = registryAuth return nil }); err != nil { return config.Config{}, nil, err diff --git a/internal/app/controlplane/runtime_config_test.go b/internal/app/controlplane/runtime_config_test.go index c05cd831d..5e9b559bd 100644 --- a/internal/app/controlplane/runtime_config_test.go +++ b/internal/app/controlplane/runtime_config_test.go @@ -660,6 +660,33 @@ func TestRuntimeConfigLayersStoredProfiles(t *testing.T) { } } +// A stored container-runtime-policies document replaces the file's [containers] +// section wholesale, so a file-owned field that document does not carry -- the +// registry credential resolved at boot -- must be preserved across the +// assignment or it is silently dropped and every private-registry pull is +// anonymous again. +func TestRuntimeConfigPreservesFileOwnedRegistryAuth(t *testing.T) { + base := config.Config{Containers: config.ContainerConfig{ + Image: "agent:1", + RegistryAuth: config.SecretRef{Engine: "env", Key: "REGISTRY_AUTH"}, + }} + + client := NewRPCClient(&runtimeConfigClient{values: map[string]any{ + SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "dispatch": map[string]any{"trigger": "assignee"}}, + ContainerRuntimePoliciesKind: map[string]any{"image": "agent:2", "pull_policy": "always"}, + }}) + got, _, err := client.RuntimeConfig(t.Context(), base) + if err != nil { + t.Fatalf("RuntimeConfig: %v", err) + } + if got.Containers.Image != "agent:2" { + t.Fatalf("image = %q, want the stored document's", got.Containers.Image) + } + if want := (config.SecretRef{Engine: "env", Key: "REGISTRY_AUTH"}); got.Containers.RegistryAuth != want { + t.Fatalf("registry_auth = %+v, want the file's %+v preserved across the container-policies layering", got.Containers.RegistryAuth, want) + } +} + // A stored CredentialBindingsKind value replaces the file's bindings // outright; no stored value at all leaves the file's in effect (the same // shape TestRuntimeConfigLayersStoredProfiles pins for AgentProfileKind, for diff --git a/internal/config/config.go b/internal/config/config.go index f469b665e..06b94009c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -897,6 +897,20 @@ type ContainerConfig struct { // seeded from this field but validated and stored (and reloaded live) // separately -- see internal/app/controlplane. Credentials []CredentialBinding `toml:"credentials" yaml:"credentials" json:"-"` + // RegistryAuth is the credential a private registry requires when the + // configured image (or a profile's) is pulled. It uses the same + // secret-reference shape as every other credential in this file; the + // resolved value is a Docker registry.AuthConfig JSON document, e.g. + // {"username":"bot","password":"..."} ("identitytoken", + // "registrytoken" and "serveraddress" are honoured when present but + // optional -- the daemon derives the host from the image reference). The + // zero value means anonymous pulls, which is what the pool has always + // done; it never fails a deployment that has no private registry. + // RegistryAuth is file-owned, not part of the control-plane JSON document + // (container-runtime-policies): it is a secret reference, resolved at boot + // and restart-required, so the pool receives the resolved value rather + // than a stored one. See internal/container's encodeRegistryAuth. + RegistryAuth SecretRef `toml:"registry_auth" yaml:"registry_auth" json:"-"` } // CredentialBinding maps one Kit credential@1 service name to an org secret. diff --git a/internal/container/pool.go b/internal/container/pool.go index f13d285d8..f450b687c 100644 --- a/internal/container/pool.go +++ b/internal/container/pool.go @@ -6,7 +6,9 @@ package container import ( "context" + "encoding/base64" "encoding/json" + "errors" "fmt" "io" "log/slog" @@ -17,6 +19,7 @@ import ( "time" "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/registry" "github.com/moby/moby/client" "github.com/samcharles93/archie-core/internal/storage" @@ -117,6 +120,11 @@ type Config struct { // (gate re-runs, human replies) during this window. PRD §1. GracePeriod time.Duration PullPolicy string + // RegistryAuth is the resolved value of [containers].registry_auth: either + // empty (anonymous pulls -- the behaviour for a deployment with no private + // registry) or a Docker registry.AuthConfig JSON document. pullImage + // encodes it into the X-Registry-Auth header the Engine API expects. + RegistryAuth string // Network is the Docker network spawned agent containers join. Empty // falls back to selfNetwork's best-effort auto-detection. Network string @@ -573,7 +581,7 @@ func (p *Pool) pullImage(ctx context.Context, ref string) error { } p.log.Info("pulling image", "image", ref) - rc, err := p.cli.ImagePull(ctx, ref, client.ImagePullOptions{}) + rc, err := p.cli.ImagePull(ctx, ref, client.ImagePullOptions{RegistryAuth: p.registryAuthHeader(ref)}) if err != nil { return fmt.Errorf("pull %s: %w", ref, err) } @@ -589,6 +597,46 @@ func (p *Pool) pullImage(ctx context.Context, ref string) error { return nil } +// registryAuthHeader renders p.cfg.RegistryAuth into the value the Engine API +// expects in ImagePullOptions.RegistryAuth (the base64url AuthConfig of the +// X-Registry-Auth header), or "" when no credential is configured. +// +// A credential that cannot be rendered degrades to an anonymous pull with a +// warning rather than failing the pull: one bad secret must not take every +// autonomous task down with it, and the warning is what makes the 401 a private +// registry will still answer explicable. A missing credential is silent -- it is +// the deployment shape that has no private registry. +func (p *Pool) registryAuthHeader(ref string) string { + if p.cfg.RegistryAuth == "" { + return "" + } + encoded, err := encodeRegistryAuth(p.cfg.RegistryAuth) + if err != nil { + p.log.Warn("registry credential unusable; pulling without auth", "image", ref, "err", err) + return "" + } + return encoded +} + +// encodeRegistryAuth encodes credential, a registry.AuthConfig JSON document, +// as the base64url JSON the Engine API documents for +// [registry.AuthHeader]. It is the shape docker login's config.json stores, +// minus the surrounding "auths" map. +func encodeRegistryAuth(credential string) (string, error) { + var auth registry.AuthConfig + if err := json.Unmarshal([]byte(credential), &auth); err != nil { + return "", fmt.Errorf("decode registry credential: %w", err) + } + if auth.Username == "" && auth.Password == "" && auth.IdentityToken == "" && auth.RegistryToken == "" { + return "", errors.New("registry credential names no username, password, identity token or registry token") + } + payload, err := json.Marshal(auth) + if err != nil { + return "", fmt.Errorf("encode registry credential: %w", err) + } + return base64.URLEncoding.EncodeToString(payload), nil +} + // selfNetwork detects the user-defined Docker network the current // process's own container is attached to, by inspecting the container // named after our hostname (Docker sets a container's hostname to its diff --git a/internal/container/pool_test.go b/internal/container/pool_test.go index c52509a1b..f6d95971c 100644 --- a/internal/container/pool_test.go +++ b/internal/container/pool_test.go @@ -1,9 +1,12 @@ package container import ( + "bytes" "context" + "encoding/base64" "encoding/json" "fmt" + "log/slog" "net/http" "net/http/httptest" "os" @@ -13,6 +16,7 @@ import ( "testing" "time" + "github.com/moby/moby/api/types/registry" "github.com/moby/moby/client" ) @@ -725,3 +729,118 @@ func TestPoolActiveReportsInFlightContainers(t *testing.T) { t.Errorf("Cap() with no MaxConcurrency = %d, want 0 (unlimited)", got) } } + +// ── registry credential on image pull ────────────────────────────────── + +// pullImage must send [containers].registry_auth to the engine as the +// base64url registry.AuthConfig the X-Registry-Auth header carries. Without +// it a private registry answers 401 no matter what pull_policy says, because +// docker login only writes a file the CLI reads. +func TestPullImageSendsConfiguredRegistryAuth(t *testing.T) { + const ref = "git.catlow.cloud/team/archie-agent:latest" + + headers := make(chan string, 1) + dockerAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "/images/create") { + headers <- r.Header.Get("X-Registry-Auth") + } + w.WriteHeader(http.StatusOK) + })) + t.Cleanup(dockerAPI.Close) + + dockerClient, err := client.New(client.WithHost(dockerAPI.URL), client.WithAPIVersion("1.55")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = dockerClient.Close() }) + + pool := &Pool{ + cli: dockerClient, + cfg: Config{ + Image: ref, + PullPolicy: "always", + RegistryAuth: `{"username":"bot","password":"s3cret"}`, + }, + log: discardLogger(), + } + if err := pool.pullImage(context.Background(), ref); err != nil { + t.Fatalf("pullImage: %v", err) + } + + header := <-headers + if header == "" { + t.Fatal("image pull sent no X-Registry-Auth header: the configured registry credential never reached ImagePullOptions") + } + raw, err := base64.URLEncoding.DecodeString(header) + if err != nil { + t.Fatalf("X-Registry-Auth %q is not base64url: %v", header, err) + } + var got registry.AuthConfig + if err := json.Unmarshal(raw, &got); err != nil { + t.Fatalf("X-Registry-Auth payload %q is not an AuthConfig: %v", raw, err) + } + if got.Username != "bot" || got.Password != "s3cret" { + t.Fatalf("X-Registry-Auth = %+v, want username bot and the configured password", got) + } +} + +// A deployment with no registry credential -- or with one that cannot be +// turned into a header -- must keep pulling anonymously exactly as it did +// before the field existed, rather than failing the pool (and with it every +// autonomous task) closed. A broken credential is logged so the 401 it will +// still produce is explicable. +func TestPullImageWithoutUsableRegistryCredentialPullsAnonymously(t *testing.T) { + const ref = "registry.example.test/team/archie-agent:latest" + + tests := []struct { + name string + credential string + wantLogSubstr string + }{ + {name: "no credential configured"}, + {name: "unparseable credential", credential: `{"username":"bot"`, wantLogSubstr: "registry credential unusable"}, + {name: "credential naming nothing", credential: `{}`, wantLogSubstr: "registry credential unusable"}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + headers := make(chan string, 1) + dockerAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "/images/create") { + headers <- r.Header.Get("X-Registry-Auth") + } + w.WriteHeader(http.StatusOK) + })) + t.Cleanup(dockerAPI.Close) + + dockerClient, err := client.New(client.WithHost(dockerAPI.URL), client.WithAPIVersion("1.55")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = dockerClient.Close() }) + + var logBuf bytes.Buffer + pool := &Pool{ + cli: dockerClient, + cfg: Config{Image: ref, PullPolicy: "always", RegistryAuth: tc.credential}, + log: slog.New(slog.NewTextHandler(&logBuf, nil)), + } + if err := pool.pullImage(context.Background(), ref); err != nil { + t.Fatalf("pullImage must degrade to an anonymous pull, got error: %v", err) + } + + if header := <-headers; header != "" { + t.Fatalf("X-Registry-Auth = %q, want none for %s", header, tc.name) + } + if tc.wantLogSubstr == "" { + if strings.Contains(logBuf.String(), "registry credential") { + t.Fatalf("log for %s should not warn about a credential it was never given: %s", tc.name, logBuf.String()) + } + return + } + if !strings.Contains(logBuf.String(), tc.wantLogSubstr) { + t.Fatalf("log = %q, want it to mention %q", logBuf.String(), tc.wantLogSubstr) + } + }) + } +} diff --git a/internal/infrastructure/configuration/validate.go b/internal/infrastructure/configuration/validate.go index f170d5713..66d748e5f 100644 --- a/internal/infrastructure/configuration/validate.go +++ b/internal/infrastructure/configuration/validate.go @@ -489,6 +489,15 @@ func validateContainers(cfg *config.Config) error { if err := cfg.Containers.ValidateCredentialBindings(); err != nil { return fmt.Errorf("%w: %w", ErrInvalidInput, err) } + // A registry credential that names only one half of the reference can only + // be a typo, and the pool cannot resolve it into the header a private + // registry needs -- the same rule resolveProviderMap applies to provider + // keys. The zero reference is deliberately accepted: anonymous pulls are + // the behaviour for a deployment with no private registry, and this field + // existing must not change that. + if ref := cfg.Containers.RegistryAuth; ref != (config.SecretRef{}) && (ref.Engine == "" || ref.Key == "") { + return fmt.Errorf("%w: containers.registry_auth must name both an engine and a key, got {engine: %q, key: %q}", ErrInvalidInput, ref.Engine, ref.Key) + } return nil } diff --git a/internal/infrastructure/configuration/validate_test.go b/internal/infrastructure/configuration/validate_test.go index 1fb2ca553..e29be3bc7 100644 --- a/internal/infrastructure/configuration/validate_test.go +++ b/internal/infrastructure/configuration/validate_test.go @@ -278,6 +278,39 @@ func TestValidateContainersSupportsBothBrokerDeployments(t *testing.T) { }) } +// TestValidateContainersRegistryAuth pins that a registry credential naming +// only half a secret reference is refused while the zero reference (anonymous +// pulls) is accepted: a half-named ref can only be a typo, and the pool cannot +// resolve it into the header a private registry needs. +func TestValidateContainersRegistryAuth(t *testing.T) { + tests := []struct { + name string + ref secret.SecretRef + wantErr bool + }{ + {name: "unset pulls anonymously"}, + {name: "both halves named", ref: secret.SecretRef{Engine: "env", Key: "REGISTRY_AUTH"}}, + {name: "engine only", ref: secret.SecretRef{Engine: "env"}, wantErr: true}, + {name: "key only", ref: secret.SecretRef{Key: "REGISTRY_AUTH"}, wantErr: true}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + cfg := minimalValidConfig() + cfg.Containers.RegistryAuth = tc.ref + err := Validate(&cfg) + if tc.wantErr { + if !errors.Is(err, ErrInvalidInput) { + t.Fatalf("Validate() = %v, want ErrInvalidInput", err) + } + return + } + if err != nil { + t.Fatalf("Validate() = %v, want nil", err) + } + }) + } +} + // TestValidateForgeIntake pins the forge.intake contract: an unset intake // resolves to poll without mutating cfg, and webhook/both require a webhook // secret and listen address.