Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
refactor(registry): use composite literals and inline helpers
Kyle Mendell00daf4441a67
diff --git a/.golangci.yml b/.golangci.ymlindex afaf2e873..79f60607d 100644--- a/.golangci.yml+++ b/.golangci.yml@@ -1,8 +1,8 @@# Slightly Modified Version of docker's golangci lint configuration to fit arcanes standards-version: '2'+version: "2"run:- go: '1.27.2'+ go: "1.27.2"build-tags:- exclude_frontend- playwright@@ -42,6 +42,7 @@ linters:- depguard- errcheck- errorlint+ - exhaustruct_v5- forbidigo- gocognit- gocritic@@ -78,20 +79,23 @@ linters:desc: use stdlib maps package- pkg: golang.org/x/exp/slicesdesc: use stdlib slices package- - pkg: 'github.com/google/uuid'+ - pkg: "github.com/google/uuid"desc: Use stdlib uuid instead.- - pkg: 'gopkg.in/yaml.v3'+ - pkg: "gopkg.in/yaml.v3"desc: Use go.yaml.in/yaml/v4 instead.+ exhaustruct_v5:+ explicit-mode: true+forbidigo:analyze-types: trueforbid:- pattern: 'context\.Background'- pkg: '^context$'- msg: 'in tests, use t.Context() instead of context.Background()'+ pkg: "^context$"+ msg: "in tests, use t.Context() instead of context.Background()"- pattern: 'context\.TODO'- pkg: '^context$'- msg: 'in tests, use t.Context() instead of context.TODO()'+ pkg: "^context$"+ msg: "in tests, use t.Context() instead of context.TODO()"gocritic:disabled-checks:@@ -142,7 +146,7 @@ linters:disabled: truesloglint:- context: 'all' # https://github.com/go-simpler/sloglint#context-only+ context: "all" # https://github.com/go-simpler/sloglint#context-onlykv-only: true # https://github.com/go-simpler/sloglint#key-value-pairs-onlystatic-msg: true # https://github.com/go-simpler/sloglint#static-messagekey-naming-case: camel@@ -153,4 +157,4 @@ linters:# The legacy edge message protocol uses deprecated tunnel.proto messages until v3 removes both.- path: pkg/libarcane/edge/linters: [staticcheck]- text: 'SA1019: .*proto/tunnel/v1'+ text: "SA1019: .*proto/tunnel/v1"diff --git a/backend/internal/registry/children/browse/service.go b/backend/internal/registry/children/browse/service.goindex d759a256a..a398a4afd 100644--- a/backend/internal/registry/children/browse/service.go+++ b/backend/internal/registry/children/browse/service.go@@ -9,6 +9,7 @@ import ("slices""strings""sync"+ "time""github.com/containerd/errdefs""github.com/getarcaneapp/arcane/types/v2/containerregistry"@@ -17,6 +18,7 @@ import ("github.com/google/go-containerregistry/pkg/v1""github.com/google/go-containerregistry/pkg/v1/remote""github.com/google/go-containerregistry/pkg/v1/remote/transport"+ "go.getarcane.app/kit/pkg""go.opentelemetry.io/otel""go.opentelemetry.io/otel/attribute""go.opentelemetry.io/otel/trace"@@ -66,22 +68,18 @@ func (s *Service) ListRepositories(ctx context.Context, id string, params paginalookupCtx, cancel := s.lookupContext(ctx)defer cancel()- target, err := s.targetInternal(lookupCtx, id)+ target, err := s.target(lookupCtx, id)if err != nil {return nil, pagination.Response{}, err}names, err := remote.Catalog(lookupCtx, target.registry, target.options...)if err != nil {- return nil, pagination.Response{}, classifyBrowseErrorInternal(err, "failed to list repositories (the registry may not support the catalog API)")+ return nil, pagination.Response{}, classifyBrowseError(err, "failed to list repositories (the registry may not support the catalog API)")}- if target.prefix != "" {- names = slices.DeleteFunc(names, func(repositoryName string) bool {- return !strings.HasPrefix(repositoryName, target.prefix+"/")- })- }-- page, response := paginateNamesInternal(names, params)+ page, response := paginateNames(slices.DeleteFunc(names, func(repositoryName string) bool {+ return target.prefix != "" && !strings.HasPrefix(repositoryName, target.prefix+"/")+ }), params)items := make([]containerregistry.Repository, 0, len(page))for _, repositoryName := range page {items = append(items, containerregistry.Repository{Name: repositoryName})@@ -89,9 +87,8 @@ func (s *Service) ListRepositories(ctx context.Context, id string, params paginareturn items, response, nil}-// ListRepositoryTags lists the tags of a repository. Manifest details are only-// fetched for the requested page, and a failure is recorded on the tag itself-// so one unreadable manifest does not hide the rest of the page.+// ListRepositoryTags lists the tags of a repository, fetching manifest details only for the requested page.+// A manifest failure is recorded on its tag so it does not hide the rest of the page.func (s *Service) ListRepositoryTags(ctx context.Context, id, repository string, params pagination.QueryParams) (listed []containerregistry.RepositoryTag, paging pagination.Response, listErr error) {ctx, span := otel.Tracer(tracing.InstrumentationName).Start(ctx, "registry.list_tags", trace.WithAttributes(attribute.String("arcane.registry.id", id),@@ -105,31 +102,27 @@ func (s *Service) ListRepositoryTags(ctx context.Context, id, repository string,lookupCtx, cancel := s.lookupContext(ctx)defer cancel()- target, err := s.targetInternal(lookupCtx, id)+ target, err := s.target(lookupCtx, id)if err != nil {return nil, pagination.Response{}, err}- repo, err := target.repositoryInternal(repository)+ repo, err := target.repository(repository)if err != nil {return nil, pagination.Response{}, err}names, err := remote.List(repo, target.options...)if err != nil {- return nil, pagination.Response{}, classifyBrowseErrorInternal(err, "failed to list tags")+ return nil, pagination.Response{}, classifyBrowseError(err, "failed to list tags")}- page, response := paginateNamesInternal(names, params)+ page, response := paginateNames(names, params)items := make([]containerregistry.RepositoryTag, len(page))group := errgroup.Group{}group.SetLimit(tagDetailsConcurrency)for i, tagName := range page {group.Go(func() error {- tag, tagDetailsErr := tagDetailsInternal(repo, target.options, tagName)- if tagDetailsErr != nil {- tag.Error = tagDetailsErr.Error()- }- items[i] = tag+ items[i] = tagDetails(repo, target.options, tagName)return nil})}@@ -154,11 +147,11 @@ func (s *Service) DeleteRepositoryTag(ctx context.Context, id, repository, tag slookupCtx, cancel := s.lookupContext(ctx)defer cancel()- target, err := s.targetInternal(lookupCtx, id)+ target, err := s.target(lookupCtx, id)if err != nil {return "", err}- repo, err := target.repositoryInternal(repository)+ repo, err := target.repository(repository)if err != nil {return "", err}@@ -169,68 +162,59 @@ func (s *Service) DeleteRepositoryTag(ctx context.Context, id, repository, tag sdescriptor, err := remote.Head(tagRef, target.options...)if err != nil {- return "", classifyBrowseErrorInternal(err, "failed to resolve tag digest")+ return "", classifyBrowseError(err, "failed to resolve tag digest")}- digest := descriptor.Digest.String()- if deleteErr := remote.Delete(repo.Digest(digest), target.options...); deleteErr != nil {- return "", classifyBrowseErrorInternal(deleteErr, "failed to delete manifest")+ if deleteErr := remote.Delete(repo.Digest(descriptor.Digest.String()), target.options...); deleteErr != nil {+ return "", classifyBrowseError(deleteErr, "failed to delete manifest")}- return digest, nil+ return descriptor.Digest.String(), nil}-// targetInternal resolves a stored registry into a registry name, its+// target resolves a stored registry into a registry name, its// namespace prefix, and the remote options carrying auth and transport.-func (s *Service) targetInternal(ctx context.Context, id string) (*target, error) {+func (s *Service) target(ctx context.Context, id string) (*target, error) {reg, credential, err := s.resolve(ctx, id)if err != nil {return nil, err}host, prefix := registryauth.SplitRegistryURL(reg.URL)- nameOptions := []name.Option{name.StrictValidation}- if reg.Insecure {- nameOptions = append(nameOptions, name.Insecure)- }- registryName, err := name.NewRegistry(host, nameOptions...)+ registryName, err := name.NewRegistry(host, kit.Ternary(reg.Insecure, []name.Option{name.StrictValidation, name.Insecure}, []name.Option{name.StrictValidation})...)if err != nil {return nil, common.Classify(common.ErrValidation, fmt.Errorf("invalid registry URL %q: %w", reg.URL, err))}- authenticator := authn.Anonymous- if credential != nil {- authenticator = authn.FromConfig(authn.AuthConfig{Username: credential.Username, Password: credential.Token})- }- options := []remote.Option{remote.WithContext(ctx), remote.WithAuth(authenticator)}- if s.transport != nil {- options = append(options, remote.WithTransport(s.transport))- }-return &target{registry: registryName,prefix: prefix,- nameOptions: nameOptions,- options: options,+ nameOptions: kit.Ternary(reg.Insecure, []name.Option{name.StrictValidation, name.Insecure}, []name.Option{name.StrictValidation}),+ options: append([]remote.Option{+ remote.WithContext(ctx),+ remote.WithAuth(kit.Ternary(credential == nil,+ authn.Anonymous,+ authn.FromConfig(authn.AuthConfig{Username: kit.FromPtr(credential).Username, Password: kit.FromPtr(credential).Token}),+ )),+ }, kit.Ternary(s.transport != nil, []remote.Option{remote.WithTransport(s.transport)}, nil)...),}, nil}-// repositoryInternal parses a repository name and keeps it inside the registry namespace.-func (t *target) repositoryInternal(repository string) (name.Repository, error) {- repository = strings.Trim(strings.TrimSpace(repository), "/")- if repository == "" {+// repository parses a repository name and keeps it inside the registry namespace.+func (t *target) repository(repository string) (name.Repository, error) {+ if strings.Trim(strings.TrimSpace(repository), "/") == "" {return name.Repository{}, common.Classify(common.ErrValidation, errors.New("repository is required"))}- if t.prefix != "" && !strings.HasPrefix(repository, t.prefix+"/") {- return name.Repository{}, common.Classify(common.ErrValidation, fmt.Errorf("repository %q is outside the registry namespace %q", repository, t.prefix))+ if t.prefix != "" && !strings.HasPrefix(strings.Trim(strings.TrimSpace(repository), "/"), t.prefix+"/") {+ return name.Repository{}, common.Classify(common.ErrValidation, fmt.Errorf("repository %q is outside the registry namespace %q", strings.Trim(strings.TrimSpace(repository), "/"), t.prefix))}- repo, err := name.NewRepository(t.registry.Name()+"/"+repository, t.nameOptions...)+ repo, err := name.NewRepository(t.registry.Name()+"/"+strings.Trim(strings.TrimSpace(repository), "/"), t.nameOptions...)if err != nil {- return name.Repository{}, common.Classify(common.ErrValidation, fmt.Errorf("invalid repository %q: %w", repository, err))+ return name.Repository{}, common.Classify(common.ErrValidation, fmt.Errorf("invalid repository %q: %w", strings.Trim(strings.TrimSpace(repository), "/"), err))}return repo, nil}-// classifyBrowseErrorInternal maps distribution API failures to API error kinds.-func classifyBrowseErrorInternal(err error, message string) error {+// classifyBrowseError maps distribution API failures to API error kinds.+func classifyBrowseError(err error, message string) error {if err == nil {return nil}@@ -267,11 +251,8 @@ func IsUnauthorizedRegistryError(err error) bool {return registryErr.StatusCode == http.StatusUnauthorized || registryErr.StatusCode == http.StatusForbidden}- // Fallback: some Docker daemon versions return plain-text errors without- // a typed wrapper. These known substrings cover Docker Hub, GHCR, and- // other common OCI registries as of Docker Engine 27.x.- errLower := strings.ToLower(err.Error())- indicators := []string{+ // Some Docker daemons return plain-text errors; these substrings cover Docker Hub, GHCR, and common OCI registries.+ return slices.ContainsFunc([]string{"unauthorized","authentication required","no basic auth credentials",@@ -281,79 +262,88 @@ func IsUnauthorizedRegistryError(err error) bool {"status 401","status: 403","status 403",- }-- for _, indicator := range indicators {- if strings.Contains(errLower, indicator) {- return true- }- }-- return false+ }, func(indicator string) bool {+ return strings.Contains(strings.ToLower(err.Error()), indicator)+ })}-// tagPlatformInternal describes one platform image with its compressed config and layer size.-func tagPlatformInternal(img v1.Image, platform *v1.Platform, digest v1.Hash) (containerregistry.TagPlatform, error) {+// tagPlatform describes one platform image with its compressed config and layer size.+func tagPlatform(img v1.Image, platform *v1.Platform, digest v1.Hash) (containerregistry.TagPlatform, error) {manifest, err := img.Manifest()if err != nil {return containerregistry.TagPlatform{}, err}- result := containerregistry.TagPlatform{Digest: digest.String(), Size: manifest.Config.Size}+ size := manifest.Config.Sizefor _, layer := range manifest.Layers {- result.Size += layer.Size- }- if platform != nil {- result.OS = platform.OS- result.Architecture = platform.Architecture- result.Variant = platform.Variant- }- return result, nil+ size += layer.Size+ }+ return containerregistry.TagPlatform{+ OS: cmp.Or(platform, &v1.Platform{}).OS,+ Architecture: cmp.Or(platform, &v1.Platform{}).Architecture,+ Variant: cmp.Or(platform, &v1.Platform{}).Variant,+ Digest: digest.String(),+ Size: size,+ }, nil}-// tagDetailsInternal resolves the manifest behind a tag. A single image yields+// tagDetails resolves the manifest behind a tag. A single image yields// one platform; an index yields one per platform image, skipping attestations.-func tagDetailsInternal(repo name.Repository, options []remote.Option, tagName string) (containerregistry.RepositoryTag, error) {- tag := containerregistry.RepositoryTag{Name: tagName, Platforms: []containerregistry.TagPlatform{}}-+func tagDetails(repo name.Repository, options []remote.Option, tagName string) containerregistry.RepositoryTag {descriptor, err := remote.Get(repo.Tag(tagName), options...)if err != nil {- return tag, err+ return containerregistry.RepositoryTag{+ Name: tagName,+ Platforms: []containerregistry.TagPlatform{},+ Error: err.Error(),+ }+ }+ tag := func(platforms []containerregistry.TagPlatform, created *time.Time, detailsErr error) containerregistry.RepositoryTag {+ var size int64+ for _, platform := range platforms {+ size += platform.Size+ }+ return containerregistry.RepositoryTag{+ Name: tagName,+ Digest: descriptor.Digest.String(),+ MediaType: string(descriptor.MediaType),+ Size: size,+ Created: created,+ Platforms: platforms,+ Error: kit.Ternary(detailsErr == nil, "", fmt.Sprint(detailsErr)),+ }}- tag.Digest = descriptor.Digest.String()- tag.MediaType = string(descriptor.MediaType)if !descriptor.MediaType.IsIndex() {img, imageErr := descriptor.Image()if imageErr != nil {- return tag, imageErr+ return tag([]containerregistry.TagPlatform{}, nil, imageErr)}config, imageErr := img.ConfigFile()if imageErr != nil {- return tag, imageErr+ return tag([]containerregistry.TagPlatform{}, nil, imageErr)}- platform, imageErr := tagPlatformInternal(img, config.Platform(), descriptor.Digest)+ platform, imageErr := tagPlatform(img, config.Platform(), descriptor.Digest)if imageErr != nil {- return tag, imageErr+ return tag([]containerregistry.TagPlatform{}, nil, imageErr)}- tag.Platforms = []containerregistry.TagPlatform{platform}- tag.Size = platform.Size- if !config.Created.IsZero() {- created := config.Created.UTC()- tag.Created = &created- }- return tag, nil+ return tag(+ []containerregistry.TagPlatform{platform},+ kit.Ternary(config.Created.IsZero(), nil, new(config.Created.UTC())),+ nil,+ )}index, err := descriptor.ImageIndex()if err != nil {- return tag, err+ return tag([]containerregistry.TagPlatform{}, nil, err)}manifest, err := index.IndexManifest()if err != nil {- return tag, err+ return tag([]containerregistry.TagPlatform{}, nil, err)}var mu sync.Mutex+ platforms := []containerregistry.TagPlatform{}group := errgroup.Group{}group.SetLimit(tagDetailsConcurrency)for _, child := range manifest.Manifests {@@ -365,40 +355,38 @@ func tagDetailsInternal(repo name.Repository, options []remote.Option, tagName sif imageErr != nil {return imageErr}- platform, imageErr := tagPlatformInternal(img, child.Platform, child.Digest)+ platform, imageErr := tagPlatform(img, child.Platform, child.Digest)if imageErr != nil {return imageErr}mu.Lock()- tag.Platforms = append(tag.Platforms, platform)- tag.Size += platform.Size+ platforms = append(platforms, platform)mu.Unlock()return nil})}if waitErr := group.Wait(); waitErr != nil {- return tag, waitErr+ return tag(platforms, nil, waitErr)}- slices.SortFunc(tag.Platforms, func(a, b containerregistry.TagPlatform) int {+ slices.SortFunc(platforms, func(a, b containerregistry.TagPlatform) int {return cmp.Or(strings.Compare(a.OS, b.OS),strings.Compare(a.Architecture, b.Architecture),strings.Compare(a.Variant, b.Variant),)})- return tag, nil+ return tag(platforms, nil, nil)}-func paginateNamesInternal(names []string, params pagination.QueryParams) ([]string, pagination.Response) {- config := pagination.Config[string]{+func paginateNames(names []string, params pagination.QueryParams) ([]string, pagination.Response) {+ result := pagination.Config[string]{SearchAccessors: []pagination.SearchAccessor[string]{func(item string) (string, error) { return item, nil },},SortBindings: []pagination.SortBinding[string]{{Key: "name", Fn: strings.Compare},},- }- result := config.SearchOrderAndPaginate(names, params)+ }.SearchOrderAndPaginate(names, params)return result.Items, pagination.BuildResponse(result.TotalCount, result.TotalAvailable, params)}diff --git a/backend/internal/registry/children/browse/service_test.go b/backend/internal/registry/children/browse/service_test.goindex 56b0ab3ac..01198ecaf 100644--- a/backend/internal/registry/children/browse/service_test.go+++ b/backend/internal/registry/children/browse/service_test.go@@ -28,9 +28,9 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/pagination")-// newTestRegistryInternal starts an in-memory OCI registry and returns its+// newTestRegistry starts an in-memory OCI registry and returns its// host as "localhost:PORT" so go-containerregistry resolves it over plain HTTP.-func newTestRegistryInternal(t *testing.T) string {+func newTestRegistry(t *testing.T) string {t.Helper()server := httptest.NewServer(registry.New(registry.Logger(log.New(io.Discard, "", 0))))t.Cleanup(server.Close)@@ -39,16 +39,16 @@ func newTestRegistryInternal(t *testing.T) string {return "localhost:" + serverURL.Port()}-// testRegistriesInternal is an in-memory registry store keyed by ID.-type testRegistriesInternal map[string]containerregistry.Credential+// testRegistries is an in-memory registry store keyed by ID.+type testRegistries map[string]containerregistry.Credential-func (r testRegistriesInternal) add(registryURL, username, token string) string {+func (r testRegistries) add(registryURL, username, token string) string {id := fmt.Sprintf("registry-%d", len(r)+1)r[id] = containerregistry.Credential{URL: registryURL, Username: username, Token: token, Enabled: true}return id}-func newTestServiceInternal(registries testRegistriesInternal) *Service {+func newTestService(registries testRegistries) *Service {return NewService(func(_ context.Context, id string) (containerregistry.ContainerRegistry, *containerregistry.Credential, error) {credential, ok := registries[id]@@ -68,87 +68,90 @@ func newTestServiceInternal(registries testRegistriesInternal) *Service {)}-func platformImageInternal(t *testing.T, platform v1.Platform, created time.Time) v1.Image {+func platformImage(t *testing.T, platform v1.Platform, created time.Time) v1.Image {t.Helper()img, err := random.Image(128, 2)require.NoError(t, err)cfg, err := img.ConfigFile()require.NoError(t, err)- cfg = cfg.DeepCopy()- cfg.OS = platform.OS- cfg.Architecture = platform.Architecture- cfg.Variant = platform.Variant- cfg.Created = v1.Time{Time: created}- img, err = mutate.ConfigFile(img, cfg)+ img, err = mutate.ConfigFile(img, &v1.ConfigFile{+ Architecture: platform.Architecture,+ Created: v1.Time{Time: created},+ History: cfg.History,+ OS: platform.OS,+ RootFS: cfg.RootFS,+ Config: cfg.Config,+ Variant: platform.Variant,+ })require.NoError(t, err)return img}-func mustParseReferenceInternal(t *testing.T, imageRef string) name.Reference {+func mustParseReference(t *testing.T, imageRef string) name.Reference {t.Helper()ref, err := name.ParseReference(imageRef)require.NoError(t, err)return ref}-func writeImageInternal(t *testing.T, imageRef string, img v1.Image, options ...remote.Option) {+func writeImage(t *testing.T, imageRef string, img v1.Image, options ...remote.Option) {t.Helper()- require.NoError(t, remote.Write(mustParseReferenceInternal(t, imageRef), img, options...))+ require.NoError(t, remote.Write(mustParseReference(t, imageRef), img, options...))}-func imageSizeInternal(t *testing.T, img v1.Image) int64 {+func imageSize(t *testing.T, img v1.Image) int64 {t.Helper()digest, err := img.Digest()require.NoError(t, err)- platform, err := tagPlatformInternal(img, nil, digest)+ platform, err := tagPlatform(img, nil, digest)require.NoError(t, err)return platform.Size}-func browseParamsInternal(search string) pagination.QueryParams {+func browseParams(search string) pagination.QueryParams {return pagination.QueryParams{Search: search,Start: 0, Limit: 20,}}-func TestService_ListRepositoriesInternal(t *testing.T) {- host := newTestRegistryInternal(t)- img := platformImageInternal(t, v1.Platform{OS: "linux", Architecture: "amd64"}, time.Now())- writeImageInternal(t, host+"/team/api:1.0", img)- writeImageInternal(t, host+"/team/web:1.0", img)- writeImageInternal(t, host+"/other/tool:1.0", img)+func TestService_ListRepositories(t *testing.T) {+ host := newTestRegistry(t)+ img := platformImage(t, v1.Platform{OS: "linux", Architecture: "amd64"}, time.Now())+ writeImage(t, host+"/team/api:1.0", img)+ writeImage(t, host+"/team/web:1.0", img)+ writeImage(t, host+"/other/tool:1.0", img)- registries := testRegistriesInternal{}- svc := newTestServiceInternal(registries)+ registries := testRegistries{}+ svc := newTestService(registries)ctx := t.Context()id := registries.add(host, "", "")- repositories, page, err := svc.ListRepositories(ctx, id, browseParamsInternal(""))+ repositories, page, err := svc.ListRepositories(ctx, id, browseParams(""))require.NoError(t, err)- assert.Equal(t, []string{"other/tool", "team/api", "team/web"}, repositoryNamesInternal(repositories))+ assert.Equal(t, []string{"other/tool", "team/api", "team/web"}, repositoryNames(repositories))assert.EqualValues(t, 3, page.TotalItems)- repositories, _, err = svc.ListRepositories(ctx, id, browseParamsInternal("web"))+ repositories, _, err = svc.ListRepositories(ctx, id, browseParams("web"))require.NoError(t, err)- assert.Equal(t, []string{"team/web"}, repositoryNamesInternal(repositories))+ assert.Equal(t, []string{"team/web"}, repositoryNames(repositories))namespacedID := registries.add("http://"+host+"/team/", "", "")- repositories, _, err = svc.ListRepositories(ctx, namespacedID, browseParamsInternal(""))+ repositories, _, err = svc.ListRepositories(ctx, namespacedID, browseParams(""))require.NoError(t, err)- assert.Equal(t, []string{"team/api", "team/web"}, repositoryNamesInternal(repositories))+ assert.Equal(t, []string{"team/api", "team/web"}, repositoryNames(repositories))}-func TestService_ListRepositoryTagsSingleImageInternal(t *testing.T) {- host := newTestRegistryInternal(t)+func TestService_ListRepositoryTagsSingleImage(t *testing.T) {+ host := newTestRegistry(t)created := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)- img := platformImageInternal(t, v1.Platform{OS: "linux", Architecture: "arm64", Variant: "v8"}, created)- writeImageInternal(t, host+"/team/api:1.0", img)- writeImageInternal(t, host+"/team/api:2.0", img)+ img := platformImage(t, v1.Platform{OS: "linux", Architecture: "arm64", Variant: "v8"}, created)+ writeImage(t, host+"/team/api:1.0", img)+ writeImage(t, host+"/team/api:2.0", img)- registries := testRegistriesInternal{}- svc := newTestServiceInternal(registries)+ registries := testRegistries{}+ svc := newTestService(registries)id := registries.add(host, "", "")tags, page, err := svc.ListRepositoryTags(t.Context(), id, "team/api", pagination.QueryParams{Start: 0, Limit: 1})@@ -158,7 +161,7 @@ func TestService_ListRepositoryTagsSingleImageInternal(t *testing.T) {digest, err := img.Digest()require.NoError(t, err)- size := imageSizeInternal(t, img)+ size := imageSize(t, img)tag := tags[0]assert.Equal(t, "1.0", tag.Name)@@ -173,12 +176,12 @@ func TestService_ListRepositoryTagsSingleImageInternal(t *testing.T) {assert.Equal(t, "v8", tag.Platforms[0].Variant)}-func TestService_ListRepositoryTagsIndexInternal(t *testing.T) {- host := newTestRegistryInternal(t)+func TestService_ListRepositoryTagsIndex(t *testing.T) {+ host := newTestRegistry(t)amd64 := v1.Platform{OS: "linux", Architecture: "amd64"}arm64 := v1.Platform{OS: "linux", Architecture: "arm64"}- amd64Image := platformImageInternal(t, amd64, time.Now())- arm64Image := platformImageInternal(t, arm64, time.Now())+ amd64Image := platformImage(t, amd64, time.Now())+ arm64Image := platformImage(t, arm64, time.Now())attestation, err := random.Image(32, 1)require.NoError(t, err)@@ -191,16 +194,16 @@ func TestService_ListRepositoryTagsIndexInternal(t *testing.T) {require.NoError(t, err)require.NoError(t, remote.WriteIndex(ref, index))- registries := testRegistriesInternal{}- svc := newTestServiceInternal(registries)+ registries := testRegistries{}+ svc := newTestService(registries)id := registries.add(host, "", "")- tags, _, err := svc.ListRepositoryTags(t.Context(), id, "team/api", browseParamsInternal(""))+ tags, _, err := svc.ListRepositoryTags(t.Context(), id, "team/api", browseParams(""))require.NoError(t, err)require.Len(t, tags, 1)- amd64Size := imageSizeInternal(t, amd64Image)- arm64Size := imageSizeInternal(t, arm64Image)+ amd64Size := imageSize(t, amd64Image)+ arm64Size := imageSize(t, arm64Image)tag := tags[0]assert.Empty(t, tag.Error)@@ -212,14 +215,14 @@ func TestService_ListRepositoryTagsIndexInternal(t *testing.T) {assert.Equal(t, "arm64", tag.Platforms[1].Architecture)}-func TestService_DeleteRepositoryTagInternal(t *testing.T) {- host := newTestRegistryInternal(t)- deleted := platformImageInternal(t, v1.Platform{OS: "linux", Architecture: "amd64"}, time.Now())- writeImageInternal(t, host+"/team/api:1.0", deleted)- writeImageInternal(t, host+"/team/api:2.0", platformImageInternal(t, v1.Platform{OS: "linux", Architecture: "amd64"}, time.Now()))+func TestService_DeleteRepositoryTag(t *testing.T) {+ host := newTestRegistry(t)+ deleted := platformImage(t, v1.Platform{OS: "linux", Architecture: "amd64"}, time.Now())+ writeImage(t, host+"/team/api:1.0", deleted)+ writeImage(t, host+"/team/api:2.0", platformImage(t, v1.Platform{OS: "linux", Architecture: "amd64"}, time.Now()))- registries := testRegistriesInternal{}- svc := newTestServiceInternal(registries)+ registries := testRegistries{}+ svc := newTestService(registries)id := registries.add(host, "", "")ctx := t.Context()@@ -229,13 +232,13 @@ func TestService_DeleteRepositoryTagInternal(t *testing.T) {require.NoError(t, err)assert.Equal(t, deletedDigest.String(), digest)- _, err = remote.Head(mustParseReferenceInternal(t, host+"/team/api@"+digest))+ _, err = remote.Head(mustParseReference(t, host+"/team/api@"+digest))require.Error(t, err)- _, err = remote.Head(mustParseReferenceInternal(t, host+"/team/api:2.0"))+ _, err = remote.Head(mustParseReference(t, host+"/team/api:2.0"))require.NoError(t, err)}-func TestService_BrowseUsesStoredCredentialsInternal(t *testing.T) {+func TestService_BrowseUsesStoredCredentials(t *testing.T) {registryHandler := registry.New(registry.Logger(log.New(io.Discard, "", 0)))server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {username, password, ok := r.BasicAuth()@@ -251,29 +254,29 @@ func TestService_BrowseUsesStoredCredentialsInternal(t *testing.T) {require.NoError(t, err)host := "localhost:" + serverURL.Port()- writeImageInternal(t, host+"/private/app:1.0", platformImageInternal(t, v1.Platform{OS: "linux", Architecture: "amd64"}, time.Now()),+ writeImage(t, host+"/private/app:1.0", platformImage(t, v1.Platform{OS: "linux", Architecture: "amd64"}, time.Now()),remote.WithAuth(&authn.Basic{Username: "robot", Password: "secret"}))- registries := testRegistriesInternal{}- svc := newTestServiceInternal(registries)+ registries := testRegistries{}+ svc := newTestService(registries)ctx := t.Context()id := registries.add(host, "robot", "secret")- repositories, _, err := svc.ListRepositories(ctx, id, browseParamsInternal(""))+ repositories, _, err := svc.ListRepositories(ctx, id, browseParams(""))require.NoError(t, err)- assert.Equal(t, []string{"private/app"}, repositoryNamesInternal(repositories))+ assert.Equal(t, []string{"private/app"}, repositoryNames(repositories))anonymousID := registries.add(host, "", "")- _, _, err = svc.ListRepositories(ctx, anonymousID, browseParamsInternal(""))+ _, _, err = svc.ListRepositories(ctx, anonymousID, browseParams(""))assert.ErrorIs(t, err, common.ErrBadRequest)}-func TestService_BrowseErrorsInternal(t *testing.T) {- host := newTestRegistryInternal(t)- writeImageInternal(t, host+"/team/api:1.0", platformImageInternal(t, v1.Platform{OS: "linux", Architecture: "amd64"}, time.Now()))+func TestService_BrowseErrors(t *testing.T) {+ host := newTestRegistry(t)+ writeImage(t, host+"/team/api:1.0", platformImage(t, v1.Platform{OS: "linux", Architecture: "amd64"}, time.Now()))- registries := testRegistriesInternal{}- svc := newTestServiceInternal(registries)+ registries := testRegistries{}+ svc := newTestService(registries)id := registries.add(host, "", "")namespacedID := registries.add("http://"+host+"/team/", "", "")@@ -285,7 +288,7 @@ func TestService_BrowseErrorsInternal(t *testing.T) {{name: "unknown registry",call: func(ctx context.Context) error {- _, _, err := svc.ListRepositories(ctx, "missing", browseParamsInternal(""))+ _, _, err := svc.ListRepositories(ctx, "missing", browseParams(""))return err},wantErr: common.ErrNotFound,@@ -293,7 +296,7 @@ func TestService_BrowseErrorsInternal(t *testing.T) {{name: "empty repository",call: func(ctx context.Context) error {- _, _, err := svc.ListRepositoryTags(ctx, id, " / ", browseParamsInternal(""))+ _, _, err := svc.ListRepositoryTags(ctx, id, " / ", browseParams(""))return err},wantErr: common.ErrValidation,@@ -301,7 +304,7 @@ func TestService_BrowseErrorsInternal(t *testing.T) {{name: "tags outside registry namespace",call: func(ctx context.Context) error {- _, _, err := svc.ListRepositoryTags(ctx, namespacedID, "other/tool", browseParamsInternal(""))+ _, _, err := svc.ListRepositoryTags(ctx, namespacedID, "other/tool", browseParams(""))return err},wantErr: common.ErrValidation,@@ -331,7 +334,7 @@ func TestService_BrowseErrorsInternal(t *testing.T) {}}-func repositoryNamesInternal(repositories []containerregistry.Repository) []string {+func repositoryNames(repositories []containerregistry.Repository) []string {names := make([]string, 0, len(repositories))for _, repository := range repositories {names = append(names, repository.Name)diff --git a/backend/internal/registry/handler.go b/backend/internal/registry/handler.goindex 96a61aa93..6f1d9f60f 100644--- a/backend/internal/registry/handler.go+++ b/backend/internal/registry/handler.go@@ -15,8 +15,6 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil")-const registryRemoteSyncFailureMessageInternal = "Failed to fan out registry sync to remote environments"-// ContainerRegistryHandler handles container registry management endpoints.type ContainerRegistryHandler struct {registryService *ContainerRegistryService@@ -63,9 +61,7 @@ func NewHandler(registryService *ContainerRegistryService, syncRemoteRegistries// ListRegistries returns a paginated list of container registries.func (h *ContainerRegistryHandler) ListRegistries(ctx context.Context, input *ListContainerRegistriesInput) (*handlerutil.Page[containerregistry.ContainerRegistry], error) {- params := handlerutil.PaginationParams(input.Start, input.Limit, input.Sort, input.Order, input.Search)-- registries, paginationResp, err := h.registryService.GetRegistriesPaginated(ctx, params)+ registries, paginationResp, err := h.registryService.GetRegistriesPaginated(ctx, handlerutil.PaginationParams(input.Start, input.Limit, input.Sort, input.Order, input.Search))if err != nil {return nil, huma.Error500InternalServerError("Failed to list registries: " + err.Error())}@@ -173,7 +169,7 @@ func (h *ContainerRegistryHandler) TestRegistry(ctx context.Context, input *Test}// ECR registries use a different auth flow: generate a temporary token via AWS API.- if reg.RegistryType == "ecr" {+ if reg.RegistryType == RegistryTypeECR {if testECRRegistryErr := h.registryService.TestECRRegistry(ctx, reg); testECRRegistryErr != nil {return nil, huma.Error400BadRequest("Registry test failed: " + testECRRegistryErr.Error())}@@ -189,10 +185,11 @@ func (h *ContainerRegistryHandler) TestRegistry(ctx context.Context, input *Testreturn nil, huma.Error400BadRequest("Registry test failed: " + testRegistryErr.Error())}- noCredentials := strings.TrimSpace(reg.Username) == "" && strings.TrimSpace(decryptedToken) == ""- msg := kit.Ternary(noCredentials, "Registry saved (no credentials to test)", "Authentication succeeded")-- return handlerutil.MessageOutput(msg, ""), nil+ return handlerutil.MessageOutput(kit.Ternary(+ strings.TrimSpace(reg.Username) == "" && strings.TrimSpace(decryptedToken) == "",+ "Registry saved (no credentials to test)",+ "Authentication succeeded",+ ), ""), nil}// SyncRegistries syncs container registries from a remote source.@@ -210,11 +207,9 @@ func (h *ContainerRegistryHandler) triggerRemoteRegistrySync(ctx context.Contextreturn}- detachedCtx := context.WithoutCancel(ctx)-go func(syncCtx context.Context, syncReason string) {if err := h.syncRemoteRegistries(syncCtx); err != nil {- slog.WarnContext(syncCtx, registryRemoteSyncFailureMessageInternal, "reason", syncReason, "error", err.Error())+ slog.WarnContext(syncCtx, "Failed to fan out registry sync to remote environments", "reason", syncReason, "error", err.Error())}- }(detachedCtx, reason)+ }(context.WithoutCancel(ctx), reason)}diff --git a/backend/internal/registry/module.go b/backend/internal/registry/module.goindex 7e73b2d20..5f0567b4c 100644--- a/backend/internal/registry/module.go+++ b/backend/internal/registry/module.go@@ -1,5 +1,3 @@-// Package registry owns registry credentials, digest inspection, pull usage,-// synchronization, and the registry HTTP surface.package registryimport (diff --git a/backend/internal/registry/service.go b/backend/internal/registry/service.goindex e38f6100a..08c05696d 100644--- a/backend/internal/registry/service.go+++ b/backend/internal/registry/service.go@@ -1,6 +1,7 @@package registryimport (+ "cmp""context""encoding/base64""encoding/json/v2"@@ -10,6 +11,7 @@ import ("net""net/http""runtime"+ "slices""strings""time"@@ -64,8 +66,14 @@ const (registryRateLimitKeyPrefix = "container_registry:rate_limits:"dockerHubRateLimitRepository = "ratelimitpreview/test"dockerHubRateLimitTag = "latest"+ ecrTokenTTL = 12 * time.Hour+ ociImageVersionLabel = "org.opencontainers.image.version"+ versionLabelCacheTTL = 3 * time.Hour)+// ErrNoVersionLabel is returned when the remote image config carries no org.opencontainers.image.version label.+var ErrNoVersionLabel = errors.New("image config has no version label")+type RegistryDaemonClient interface {RegistryLogin(ctx context.Context, options client.RegistryLoginOptions) (client.RegistryLoginResult, error)DistributionInspect(ctx context.Context, imageRef string, options client.DistributionInspectOptions) (client.DistributionInspectResult, error)@@ -79,14 +87,14 @@ type resolvedRegistryCredential struct {ServerAddress string}-type registryRateLimitCacheEntryInternal struct {+type registryRateLimitCacheEntry struct {RateLimit registry.RateLimitInfo `json:"rateLimit"`CheckedAt time.Time `json:"checkedAt"`}-type rateLimitRoundTripFuncInternal func(*http.Request) (*http.Response, error)+type rateLimitRoundTripFunc func(*http.Request) (*http.Response, error)-func (f rateLimitRoundTripFuncInternal) RoundTrip(req *http.Request) (*http.Response, error) {+func (f rateLimitRoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {return f(req)}@@ -111,65 +119,74 @@ func NewContainerRegistryService(settingsService *settings.SettingsService,distributionHTTPClients ...*http.Client,) *ContainerRegistryService {- distributionHTTPClient := &http.Client{Timeout: 30 * time.Second}- if len(distributionHTTPClients) > 0 && distributionHTTPClients[0] != nil {- distributionHTTPClient = distributionHTTPClients[0]- }+ rawHTTPClient := cmp.Or(slices.Concat(distributionHTTPClients, []*http.Client{nil})[0], &http.Client{Timeout: 30 * time.Second})// Trace a copy so the injected client keeps its raw transport.- traced := *distributionHTTPClient- traced.Transport = otelhttp.NewTransport(distributionHTTPClient.Transport)- distributionHTTPClient = &traced- service := &ContainerRegistryService{- db: db,- dockerClient: dockerClient,+ distributionHTTPClient := &http.Client{+ Transport: otelhttp.NewTransport(rawHTTPClient.Transport),+ CheckRedirect: rawHTTPClient.CheckRedirect,+ Jar: rawHTTPClient.Jar,+ Timeout: rawHTTPClient.Timeout,+ }+ var service *ContainerRegistryService+ service = &ContainerRegistryService{+ db: db,+ dockerClient: dockerClient,+ cache: hot.NewHotCache[string, string](hot.LRU, 4096).+ WithTTL(registryCacheTTL).+ WithRevalidation(registryCacheTTL, func(imageRefs []string) (map[string]string, error) {+ digests := make(map[string]string, len(imageRefs))+ var firstErr error+ for _, imageRef := range imageRefs {+ // Per-ref timeout so one slow registry cannot starve the rest of the batch.+ ctx, cancel := context.WithTimeout(context.Background(), timeouts.DefaultRegistry) //nolint:forbidigo // Cache revalidation runs independently of request cancellation.+ result, err := service.InspectImageDigest(ctx, imageRef, nil)+ cancel()+ if err != nil {+ slog.DebugContext(ctx, "registry revalidation failed for image", "imageRef", imageRef, "error", err)+ firstErr = cmp.Or(firstErr, err)+ continue+ }+ digests[imageRef] = result.Digest+ }+ // hot discards the whole map on error, so only a fully failed batch reports one (KeepOnError keeps the old digests).+ if len(digests) > 0 {+ return digests, nil+ }+ return nil, firstErr+ }).+ WithRevalidationErrorPolicy(hot.KeepOnError).+ WithJanitor().+ Build(),+ // No static loader: ImageVersionLabel uses GetWithLoaders so failed resolutions retry on the next request.+ labelCache: hot.NewHotCache[string, string](hot.LRU, 64).+ WithTTL(versionLabelCacheTTL).+ Build(),distributionHTTPClient: distributionHTTPClient,kvService: kvService,settingsService: settingsService,- }- service.browse = browse.NewService(service.browseRegistryInternal, service.tagLookupContextInternal, distributionHTTPClient.Transport)- backgroundLoader := func(imageRefs []string) (map[string]string, error) {- digests := make(map[string]string, len(imageRefs))- var firstErr error- for _, imageRef := range imageRefs {- // Per-ref timeout: a single deadline shared across the whole- // sequential batch left later refs with whatever the earlier ones- // had not already spent, so one slow registry starved the tail.- ctx, cancel := context.WithTimeout(context.Background(), timeouts.DefaultRegistry) //nolint:forbidigo // Cache revalidation runs independently of request cancellation.- result, err := service.InspectImageDigest(ctx, imageRef, nil)- cancel()- if err != nil {- slog.DebugContext(ctx, "registry revalidation failed for image", "imageRef", imageRef, "error", err)- if firstErr == nil {- firstErr = err+ browse: browse.NewService(+ func(ctx context.Context, id string) (containerregistry.ContainerRegistry, *containerregistry.Credential, error) {+ reg, err := service.GetRegistryByID(ctx, id)+ if err != nil {+ return containerregistry.ContainerRegistry{}, nil, err}- continue- }- digests[imageRef] = result.Digest- }-- // Returning an error alongside results makes hot discard the whole map,- // so report success whenever anything resolved: one unreachable registry- // used to throw away every digest already fetched, forcing the entire- // batch to be refetched on the next tick. Refs that failed are simply- // left uncached (this cache has no missing-key cache) and retried on- // next access; only a fully failed batch surfaces the error, which- // KeepOnError turns into "retain the previous digests".- if len(digests) > 0 {- return digests, nil- }- return nil, firstErr- }- service.cache = hot.NewHotCache[string, string](hot.LRU, 4096).- WithTTL(registryCacheTTL).- WithRevalidation(registryCacheTTL, backgroundLoader).- WithRevalidationErrorPolicy(hot.KeepOnError).- WithJanitor().- Build()- // No static loader: ImageVersionLabel uses GetWithLoaders so failed- // resolutions are not cached and retry on the next request.- service.labelCache = hot.NewHotCache[string, string](hot.LRU, 64).- WithTTL(versionLabelCacheTTL).- Build()+ credential, err := service.credentialForRegistry(ctx, reg)+ switch {+ case err != nil:+ return containerregistry.ContainerRegistry{}, nil, err+ case credential == nil:+ return containerregistry.ContainerRegistry{ID: reg.ID, URL: reg.URL, Username: reg.Username, Insecure: reg.Insecure, Enabled: reg.Enabled}, nil, nil+ default:+ return containerregistry.ContainerRegistry{ID: reg.ID, URL: reg.URL, Username: reg.Username, Insecure: reg.Insecure, Enabled: reg.Enabled},+ &containerregistry.Credential{URL: reg.URL, Username: credential.Username, Token: credential.Token, Enabled: true}, nil+ }+ },+ func(ctx context.Context) (context.Context, context.CancelFunc) {+ return service.tagLookupContext(ctx)+ },+ distributionHTTPClient.Transport,+ ),+ }return service}@@ -183,14 +200,16 @@ func (s *ContainerRegistryService) GetAllRegistries(ctx context.Context) ([]Contfunc (s *ContainerRegistryService) GetRegistriesPaginated(ctx context.Context, params pagination.QueryParams) ([]containerregistry.ContainerRegistry, pagination.Response, error) {var registries []ContainerRegistry- q := s.db.WithContext(ctx).Model(&ContainerRegistry{})-- q = pagination.ApplyLikeSearch(q, params.Search, "url LIKE ? OR username LIKE ? OR COALESCE(description, '') LIKE ?")-- q = pagination.ApplyBooleanFilter(q, "enabled", params.Filters["enabled"])- q = pagination.ApplyBooleanFilter(q, "insecure", params.Filters["insecure"])-- out, paginationResp, err := params.PaginateSortAndMapDB[ContainerRegistry, containerregistry.ContainerRegistry](q, ®istries)+ out, paginationResp, err := params.PaginateSortAndMapDB[ContainerRegistry, containerregistry.ContainerRegistry](+ pagination.ApplyBooleanFilter(+ pagination.ApplyBooleanFilter(+ pagination.ApplyLikeSearch(s.db.WithContext(ctx).Model(&ContainerRegistry{}), params.Search, "url LIKE ? OR username LIKE ? OR COALESCE(description, '') LIKE ?"),+ "enabled", params.Filters["enabled"],+ ),+ "insecure", params.Filters["insecure"],+ ),+ ®istries,+ )if err != nil {return nil, pagination.Response{}, fmt.Errorf("failed to list container registries: %w", err)}@@ -217,48 +236,25 @@ func (s *ContainerRegistryService) CreateRegistry(ctx context.Context, req contaif err != nil {return nil, err}- repositoryNames, err := normalizeRepositoryNamesInternal(req.RepositoryNames)+ repositoryNames, err := normalizeRepositoryNames(req.RepositoryNames)if err != nil {return nil, err}- registryRecord := &ContainerRegistry{- URL: req.URL,- Description: req.Description,- Insecure: req.Insecure != nil && *req.Insecure,- Enabled: req.Enabled == nil || *req.Enabled,- RegistryType: registryType,- RepositoryNames: repositoryNames,- CreatedAt: time.Now(),- UpdatedAt: time.Now(),- }-if registryType == RegistryTypeECR {- if endpointErr := validateECRRegistryEndpoint(registryRecord.URL, registryRecord.Insecure); endpointErr != nil {+ if endpointErr := validateECRRegistryEndpoint(req.URL, mo.PointerToOption(req.Insecure).OrElse(false)); endpointErr != nil {return nil, endpointErr}if strings.TrimSpace(req.AWSRegion) == "" {return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "awsRegion", Err: errors.New("AWS Region is required")})}- // Static keys are optional, but must be supplied as a pair. With neither,- // the default AWS credential chain (e.g. an EC2 instance profile) is used.- hasKeyID := strings.TrimSpace(req.AWSAccessKeyID) != ""- hasSecret := strings.TrimSpace(req.AWSSecretAccessKey) != ""- if hasSecret && !hasKeyID {+ // Static keys are optional but paired; with neither, the default AWS credential chain applies.+ if strings.TrimSpace(req.AWSSecretAccessKey) != "" && strings.TrimSpace(req.AWSAccessKeyID) == "" {return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "awsAccessKeyId", Err: errors.New("AWS Access Key ID is required")})}- if hasKeyID && !hasSecret {+ if strings.TrimSpace(req.AWSAccessKeyID) != "" && strings.TrimSpace(req.AWSSecretAccessKey) == "" {return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "awsSecretAccessKey", Err: errors.New("AWS Secret Access Key is required")})}- if hasSecret {- encryptedSecret, encryptErr := crypto.Encrypt(req.AWSSecretAccessKey)- if encryptErr != nil {- return nil, fmt.Errorf("failed to encrypt AWS secret access key: %w", encryptErr)- }- registryRecord.AWSSecretAccessKey = encryptedSecret- }- registryRecord.AWSAccessKeyID = req.AWSAccessKeyID- registryRecord.AWSRegion = req.AWSRegion} else {if strings.TrimSpace(req.Username) == "" {return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "username", Err: errors.New("username is required")})@@ -266,12 +262,31 @@ func (s *ContainerRegistryService) CreateRegistry(ctx context.Context, req contaif strings.TrimSpace(req.Token) == "" {return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "token", Err: errors.New("token is required")})}- encryptedToken, encryptTokenErr := crypto.Encrypt(req.Token)- if encryptTokenErr != nil {- return nil, fmt.Errorf("failed to encrypt token: %w", encryptTokenErr)- }- registryRecord.Username = req.Username- registryRecord.Token = encryptedToken+ }++ encryptedToken, err := crypto.Encrypt(kit.Ternary(registryType == RegistryTypeECR, "", req.Token))+ if err != nil {+ return nil, fmt.Errorf("failed to encrypt token: %w", err)+ }+ encryptedSecret, err := crypto.Encrypt(kit.Ternary(registryType == RegistryTypeECR && strings.TrimSpace(req.AWSSecretAccessKey) != "", req.AWSSecretAccessKey, ""))+ if err != nil {+ return nil, fmt.Errorf("failed to encrypt AWS secret access key: %w", err)+ }++ registryRecord := &ContainerRegistry{+ URL: req.URL,+ Username: kit.Ternary(registryType == RegistryTypeECR, "", req.Username),+ Token: encryptedToken,+ Description: req.Description,+ Insecure: mo.PointerToOption(req.Insecure).OrElse(false),+ Enabled: mo.PointerToOption(req.Enabled).OrElse(true),+ RegistryType: registryType,+ RepositoryNames: repositoryNames,+ AWSAccessKeyID: kit.Ternary(registryType == RegistryTypeECR, req.AWSAccessKeyID, ""),+ AWSSecretAccessKey: encryptedSecret,+ AWSRegion: kit.Ternary(registryType == RegistryTypeECR, req.AWSRegion, ""),+ CreatedAt: time.Now(),+ UpdatedAt: time.Now(),}if createRegistryErr := s.db.WithContext(ctx).Create(registryRecord).Error; createRegistryErr != nil {return nil, fmt.Errorf("failed to create registry: %w", createRegistryErr)@@ -288,144 +303,106 @@ func (s *ContainerRegistryService) UpdateRegistry(ctx context.Context, id stringreturn nil, err}- if applyRegistryTypeUpdateErr := s.applyRegistryTypeUpdateInternal(registryRecord, req.RegistryType); applyRegistryTypeUpdateErr != nil {- return nil, applyRegistryTypeUpdateErr- }-- storedCredentials := map[string]bool{"token": registryRecord.Token != ""}- updatedCredentials := map[string]bool{"token": req.Token != nil && *req.Token != ""}- if registryRecord.RegistryType == RegistryTypeECR {- storedCredentials = map[string]bool{- "awsAccessKeyId": registryRecord.AWSAccessKeyID != "",- "awsSecretAccessKey": registryRecord.AWSSecretAccessKey != "",+ if req.RegistryType != nil {+ nextType, typeErr := NormalizeRegistryType(*req.RegistryType)+ if typeErr != nil {+ return nil, typeErr}- updatedCredentials = map[string]bool{- "awsAccessKeyId": req.AWSAccessKeyID != nil && *req.AWSAccessKeyID != "",- "awsSecretAccessKey": req.AWSSecretAccessKey != nil && *req.AWSSecretAccessKey != "",+ if nextType != registryRecord.RegistryType {+ return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "registryType", Err: errors.New("registry type cannot be changed after creation")})}}- if validateCredentialTargetChangeErr := validation.ValidateCredentialTargetChange(++ if targetErr := validation.ValidateCredentialTargetChange("registry URL",registryRecord.URL,req.URL,- normalizeRegistryServerAddressInternal,- storedCredentials,- updatedCredentials,- ); validateCredentialTargetChangeErr != nil {- return nil, validateCredentialTargetChangeErr+ normalizeRegistryServerAddress,+ kit.Ternary(registryRecord.RegistryType == RegistryTypeECR,+ map[string]bool{"awsAccessKeyId": registryRecord.AWSAccessKeyID != "", "awsSecretAccessKey": registryRecord.AWSSecretAccessKey != ""},+ map[string]bool{"token": registryRecord.Token != ""},+ ),+ kit.Ternary(registryRecord.RegistryType == RegistryTypeECR,+ map[string]bool{"awsAccessKeyId": kit.FromPtr(req.AWSAccessKeyID) != "", "awsSecretAccessKey": kit.FromPtr(req.AWSSecretAccessKey) != ""},+ map[string]bool{"token": kit.FromPtr(req.Token) != ""},+ ),+ ); targetErr != nil {+ return nil, targetErr}- // Update common fields- utils.ApplyChanged(®istryRecord.URL, mo.PointerToOption(req.URL))- utils.ApplyNullable(®istryRecord.Description, mo.PointerToOption(req.Description))- utils.ApplyChanged(®istryRecord.Insecure, mo.PointerToOption(req.Insecure))- utils.ApplyChanged(®istryRecord.Enabled, mo.PointerToOption(req.Enabled))-// RepositoryNames: nil pointer means "don't touch"; empty slice means "clear".- if req.RepositoryNames != nil {- repositoryNames, normalizeRepositoryNamesErr := normalizeRepositoryNamesInternal(*req.RepositoryNames)- if normalizeRepositoryNamesErr != nil {- return nil, normalizeRepositoryNamesErr- }- registryRecord.RepositoryNames = repositoryNames+ repositoryNames, err := normalizeRepositoryNames(kit.FromPtr(req.RepositoryNames))+ if err != nil {+ return nil, err}if registryRecord.RegistryType == RegistryTypeECR {- if updateECRRegistryFieldsErr := s.updateECRRegistryFieldsInternal(registryRecord, req); updateECRRegistryFieldsErr != nil {- return nil, updateECRRegistryFieldsErr+ if endpointErr := validateECRRegistryEndpoint(mo.PointerToOption(req.URL).OrElse(registryRecord.URL), mo.PointerToOption(req.Insecure).OrElse(registryRecord.Insecure)); endpointErr != nil {+ return nil, endpointErr}- } else if updateGenericRegistryFieldsErr := s.updateGenericRegistryFieldsInternal(registryRecord, req); updateGenericRegistryFieldsErr != nil {- return nil, updateGenericRegistryFieldsErr- }-- registryRecord.UpdatedAt = time.Now()- if updateRegistryErr := s.db.WithContext(ctx).Save(registryRecord).Error; updateRegistryErr != nil {- return nil, fmt.Errorf("failed to update registry: %w", updateRegistryErr)- }- return registryRecord,- nil-}--func (s *ContainerRegistryService) applyRegistryTypeUpdateInternal(localRegistry *ContainerRegistry, registryType *string) error {- if registryType == nil {- return nil+ if kit.FromPtr(req.AWSSecretAccessKey) != "" && strings.TrimSpace(mo.PointerToOption(req.AWSAccessKeyID).OrElse(registryRecord.AWSAccessKeyID)) == "" {+ return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "awsAccessKeyId", Err: errors.New("AWS Access Key ID is required")})+ }+ if strings.TrimSpace(mo.PointerToOption(req.AWSRegion).OrElse(registryRecord.AWSRegion)) == "" {+ return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "awsRegion", Err: errors.New("AWS Region is required")})+ }+ if strings.TrimSpace(mo.PointerToOption(req.AWSAccessKeyID).OrElse(registryRecord.AWSAccessKeyID)) != "" &&+ kit.FromPtr(req.AWSSecretAccessKey) == "" && registryRecord.AWSSecretAccessKey == "" {+ return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "awsSecretAccessKey", Err: errors.New("AWS Secret Access Key is required")})+ }+ } else if strings.TrimSpace(mo.PointerToOption(req.Username).OrElse(registryRecord.Username)) == "" {+ return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "username", Err: errors.New("username is required")})}- nextType, err := NormalizeRegistryType(*registryType)+ encryptedToken, err := crypto.Encrypt(kit.Ternary(registryRecord.RegistryType == RegistryTypeECR, "", kit.FromPtr(req.Token)))if err != nil {- return err+ return nil, fmt.Errorf("failed to encrypt token: %w", err)}-- if nextType != localRegistry.RegistryType {- return common.Classify(- common.ErrValidation,- &base.FieldError{- Field: "registryType",- Err: errors.New(- "registry type cannot be changed after creation",- ),- },- )- }-- return nil-}--func (s *ContainerRegistryService) updateECRRegistryFieldsInternal(localRegistry *ContainerRegistry, req containerregistry.UpdateContainerRegistryRequest) error {- if err := validateECRRegistryEndpoint(localRegistry.URL, localRegistry.Insecure); err != nil {- return err+ encryptedSecret, err := crypto.Encrypt(kit.Ternary(+ registryRecord.RegistryType == RegistryTypeECR && strings.TrimSpace(mo.PointerToOption(req.AWSAccessKeyID).OrElse(registryRecord.AWSAccessKeyID)) != "",+ kit.FromPtr(req.AWSSecretAccessKey),+ "",+ ))+ if err != nil {+ return nil, fmt.Errorf("failed to encrypt AWS secret access key: %w", err)}- utils.ApplyChanged(&localRegistry.AWSAccessKeyID, mo.PointerToOption(req.AWSAccessKeyID))- utils.ApplyChanged(&localRegistry.AWSRegion, mo.PointerToOption(req.AWSRegion))- hasKeyID := strings.TrimSpace(localRegistry.AWSAccessKeyID) != ""- providedSecret := req.AWSSecretAccessKey != nil && *req.AWSSecretAccessKey != ""-- switch {- case !hasKeyID && providedSecret:- return common.Classify(common.ErrValidation, &base.FieldError{Field: "awsAccessKeyId", Err: errors.New("AWS Access Key ID is required")})- case !hasKeyID:+ updated := ContainerRegistry{ //exhaustruct:enforce+ BaseModel: registryRecord.BaseModel,+ URL: mo.PointerToOption(req.URL).OrElse(registryRecord.URL),+ Username: kit.Ternary(registryRecord.RegistryType == RegistryTypeECR, registryRecord.Username, mo.PointerToOption(req.Username).OrElse(registryRecord.Username)),+ Token: cmp.Or(encryptedToken, registryRecord.Token),+ Description: req.Description,+ Insecure: mo.PointerToOption(req.Insecure).OrElse(registryRecord.Insecure),+ Enabled: mo.PointerToOption(req.Enabled).OrElse(registryRecord.Enabled),+ RegistryType: registryRecord.RegistryType,+ RepositoryNames: kit.Ternary(req.RepositoryNames == nil, registryRecord.RepositoryNames, repositoryNames),+ AWSAccessKeyID: kit.Ternary(registryRecord.RegistryType == RegistryTypeECR,+ mo.PointerToOption(req.AWSAccessKeyID).OrElse(registryRecord.AWSAccessKeyID), registryRecord.AWSAccessKeyID),// No static key: the default AWS credential chain applies, so drop any stored secret.- localRegistry.AWSSecretAccessKey = ""- case providedSecret:- encryptedSecret, err := crypto.Encrypt(*req.AWSSecretAccessKey)- if err != nil {- return fmt.Errorf("failed to encrypt AWS secret access key: %w", err)- }- utils.ApplyChanged(&localRegistry.AWSSecretAccessKey, mo.Some(encryptedSecret))- }-- if strings.TrimSpace(localRegistry.AWSRegion) == "" {- return common.Classify(common.ErrValidation, &base.FieldError{Field: "awsRegion", Err: errors.New("AWS Region is required")})- }- if hasKeyID && strings.TrimSpace(localRegistry.AWSSecretAccessKey) == "" {- return common.Classify(common.ErrValidation, &base.FieldError{Field: "awsSecretAccessKey", Err: errors.New("AWS Secret Access Key is required")})- }-- if req.AWSAccessKeyID != nil || req.AWSSecretAccessKey != nil || req.AWSRegion != nil {- localRegistry.ECRToken = ""- localRegistry.ECRTokenGeneratedAt = nil- }-- return nil-}--func (s *ContainerRegistryService) updateGenericRegistryFieldsInternal(localRegistry *ContainerRegistry, req containerregistry.UpdateContainerRegistryRequest) error {- utils.ApplyChanged(&localRegistry.Username, mo.PointerToOption(req.Username))-- if req.Token != nil && *req.Token != "" {- encryptedToken, err := crypto.Encrypt(*req.Token)- if err != nil {- return fmt.Errorf("failed to encrypt token: %w", err)- }- utils.ApplyChanged(&localRegistry.Token, mo.Some(encryptedToken))- }-- if strings.TrimSpace(localRegistry.Username) == "" {- return common.Classify(common.ErrValidation, &base.FieldError{Field: "username", Err: errors.New("username is required")})+ AWSSecretAccessKey: kit.Ternary(+ registryRecord.RegistryType == RegistryTypeECR && strings.TrimSpace(mo.PointerToOption(req.AWSAccessKeyID).OrElse(registryRecord.AWSAccessKeyID)) == "",+ "",+ cmp.Or(encryptedSecret, registryRecord.AWSSecretAccessKey),+ ),+ AWSRegion: kit.Ternary(registryRecord.RegistryType == RegistryTypeECR, mo.PointerToOption(req.AWSRegion).OrElse(registryRecord.AWSRegion), registryRecord.AWSRegion),+ ECRToken: kit.Ternary(+ registryRecord.RegistryType == RegistryTypeECR && (req.AWSAccessKeyID != nil || req.AWSSecretAccessKey != nil || req.AWSRegion != nil),+ "",+ registryRecord.ECRToken,+ ),+ ECRTokenGeneratedAt: kit.Ternary(+ registryRecord.RegistryType == RegistryTypeECR && (req.AWSAccessKeyID != nil || req.AWSSecretAccessKey != nil || req.AWSRegion != nil),+ nil,+ registryRecord.ECRTokenGeneratedAt,+ ),+ CreatedAt: registryRecord.CreatedAt,+ UpdatedAt: time.Now(),+ }+ if updateRegistryErr := s.db.WithContext(ctx).Save(&updated).Error; updateRegistryErr != nil {+ return nil, fmt.Errorf("failed to update registry: %w", updateRegistryErr)}-- return nil+ return &updated, nil}func (s *ContainerRegistryService) DeleteRegistry(ctx context.Context, id string) error {@@ -460,10 +437,7 @@ func (s *ContainerRegistryService) GetEnabledRegistries(ctx context.Context) ([]}// GetRegistryAuthForImage returns X-Registry-Auth for the image's registry host.-//-// The registry-auth methods tolerate a nil receiver: callers such as build.BuildService may-// hold no registry service, and are wired in as a buildtypes.RegistryAuthProvider where a-// typed-nil pointer would otherwise satisfy the interface's nil checks and panic on use.+// A nil receiver is allowed: build.BuildService may hold no registry service behind a RegistryAuthProvider.func (s *ContainerRegistryService) GetRegistryAuthForImage(ctx context.Context, imageRef string) (string, error) {if s == nil {return "", nil@@ -477,11 +451,7 @@ func (s *ContainerRegistryService) GetRegistryAuthForImage(ctx context.Context,// GetRegistryAuthForHost returns X-Registry-Auth for a configured and enabled registry.func (s *ContainerRegistryService) GetRegistryAuthForHost(ctx context.Context, registryHost string) (string, error) {- if s == nil {- return "", nil- }- normalizedRegistryHost := registryauth.NormalizeRegistryForComparison(registryHost)- if normalizedRegistryHost == "" {+ if s == nil || registryauth.NormalizeRegistryForComparison(registryHost) == "" {return "", nil}@@ -489,11 +459,8 @@ func (s *ContainerRegistryService) GetRegistryAuthForHost(ctx context.Context, rif err != nil {return "", err}- if len(authConfigs) == 0 {- return "", nil- }- cfg, ok := authConfigs[normalizedRegistryHost]+ cfg, ok := authConfigs[registryauth.NormalizeRegistryForComparison(registryHost)]if !ok {return "", nil}@@ -512,57 +479,20 @@ func (s *ContainerRegistryService) GetAllRegistryAuthConfigs(ctx context.ContextauthConfigs := make(map[string]dockerregistry.AuthConfig, len(registries))for i := range registries {- reg := ®istries[i]- if !reg.Enabled {- continue- }-- normalizedHost := strings.TrimSpace(registryauth.NormalizeRegistryForComparison(reg.URL))- if normalizedHost == "" {+ credential, credErr := s.credentialForRegistry(ctx, ®istries[i])+ if credErr != nil {+ slog.WarnContext(ctx, "failed to resolve registry credential, skipping", "registry", registries[i].URL, "error", credErr)continue}-- serverAddress := normalizedHost- if normalizedHost == "docker.io" {- serverAddress = registryauth.NormalizeRegistryURL(reg.URL)- }- if serverAddress == "" {+ if credential == nil {continue}-- var username, token string-- if reg.RegistryType == "ecr" {- ecrUser, ecrPass, ecrErr := s.GetOrRefreshECRToken(ctx, reg)- if ecrErr != nil {- slog.WarnContext(ctx, "failed to get ECR token for auth configs", "registry", reg.URL, "error", ecrErr)- continue- }- username = ecrUser- token = ecrPass- } else {- username = strings.TrimSpace(reg.Username)- if username == "" || reg.Token == "" {- continue+ for _, key := range registryauth.LookupKeys(registries[i].URL) {+ authConfigs[key] = dockerregistry.AuthConfig{+ Username: credential.Username,+ Password: credential.Token,+ ServerAddress: credential.ServerAddress,}- decryptedToken, decryptErr := crypto.Decrypt(reg.Token)- if decryptErr != nil {- slog.WarnContext(ctx, "failed to decrypt token for registry, skipping", "registry", reg.URL, "error", decryptErr)- continue- }- token = strings.TrimSpace(decryptedToken)- if token == "" {- continue- }- }-- authConfig := dockerregistry.AuthConfig{- Username: username,- Password: token,- ServerAddress: serverAddress,- }- for _, key := range registryauth.LookupKeys(normalizedHost) {- authConfigs[key] = authConfig}}@@ -575,19 +505,16 @@ func (s *ContainerRegistryService) RecordImagePull(ctx context.Context, imageRefreturn nil}- registryHost, err := normalizePullRegistryHostInternal(imageRef)+ registryHost, err := registryauth.GetRegistryAddress(imageRef)if err != nil {- return err+ return fmt.Errorf("parse image registry for %q: %w", imageRef, err)}- if registryHost == "" {+ if registryauth.NormalizeRegistryForComparison(registryHost) == "" {return nil}- if _, incrementInt64Err := s.kvService.IncrementInt64(ctx, registryPullCountKeyInternal(registryHost), 1); incrementInt64Err != nil {- return incrementInt64Err- }-- return nil+ _, err = s.kvService.IncrementInt64(ctx, registryPullCountKeyPrefix+registryauth.NormalizeRegistryForComparison(registryHost), 1)+ return err}// GetRegistryPullUsage returns pull usage visibility for configured registries.@@ -598,149 +525,138 @@ func (s *ContainerRegistryService) GetRegistryPullUsage(ctx context.Context) (co}results := make([]containerregistry.PullUsage, 0, len(registries))- for i := range registries {- results = append(results, s.buildRegistryPullUsageInternal(ctx, registries[i]))+ for _, reg := range registries {+ results = append(results, s.buildRegistryPullUsage(ctx, reg))}return containerregistry.PullUsageResponse{Registries: results}, nil}-func (s *ContainerRegistryService) buildRegistryPullUsageInternal(ctx context.Context, reg ContainerRegistry) containerregistry.PullUsage {- registryHost := registryauth.NormalizeRegistryForComparison(reg.URL)- usage := containerregistry.PullUsage{- RegistryID: reg.ID,- Provider: registryProviderInternal(registryHost, reg.RegistryType),- Registry: registryHost,- DisplayName: registryDisplayNameInternal(registryHost, reg.RegistryType),- ObservedPulls: s.getObservedPullsInternal(ctx, registryHost),- AuthMethod: "unknown",- CheckedAt: time.Now().UTC(),- }-- if registryHost != "docker.io" || !reg.Enabled {- return usage- }-- credential, authMethod, authUsername, err := s.dockerHubCredentialForRegistryInternal(reg)- usage.AuthMethod = authMethod- usage.AuthUsername = authUsername- usage.Repository = "ratelimitpreview/test"- if err != nil {- usage.Error = err.Error()- return usage- }-- if cachedRateLimit, checkedAt, ok := s.getCachedRateLimitInternal(ctx, reg.ID); ok {- ensureRateLimitUsedInternal(cachedRateLimit)- usage.Limit = cachedRateLimit.Limit- usage.Remaining = cachedRateLimit.Remaining- usage.Used = cachedRateLimit.Used- usage.WindowSeconds = cachedRateLimit.WindowSeconds- usage.Source = cachedRateLimit.Source- usage.CheckedAt = checkedAt- return usage- }-- rateLimit, err := s.fetchDockerHubRateLimitInternal(ctx, credential)- usage.CheckedAt = time.Now().UTC()- if err != nil {- usage.Error = err.Error()- return usage- }- ensureRateLimitUsedInternal(rateLimit)-- usage.Limit = rateLimit.Limit- usage.Remaining = rateLimit.Remaining- usage.Used = rateLimit.Used- usage.WindowSeconds = rateLimit.WindowSeconds- usage.Source = rateLimit.Source- s.setCachedRateLimitInternal(ctx, reg.ID, rateLimit, usage.CheckedAt)-- return usage-}--func ensureRateLimitUsedInternal(rateLimit *registry.RateLimitInfo) {- if rateLimit == nil || rateLimit.Used != nil || rateLimit.Limit == nil || rateLimit.Remaining == nil {- return- }- rateLimit.Used = new(max(*rateLimit.Limit-*rateLimit.Remaining, 0))-}--func (s *ContainerRegistryService) getObservedPullsInternal(ctx context.Context, registryHost string) int64 {- if s.kvService == nil || registryHost == "" {- return 0- }-- value, err := s.kvService.GetInt64(ctx, registryPullCountKeyInternal(registryHost), 0)- if err != nil {- slog.WarnContext(ctx, "failed to read registry pull count", "registry", registryHost, "error", err)- return 0- }-- return value-}--func (s *ContainerRegistryService) dockerHubCredentialForRegistryInternal(reg ContainerRegistry) (*authn.AuthConfig, string, string, error) {- if reg.RegistryType != RegistryTypeGeneric {- return nil, "anonymous", "", nil- }-- username := strings.TrimSpace(reg.Username)- if username == "" || strings.TrimSpace(reg.Token) == "" {- return nil, "anonymous", "", nil- }-- token, err := crypto.Decrypt(reg.Token)- if err != nil {- return nil, "credential", username, fmt.Errorf("failed to decrypt Docker Hub credential: %w", err)- }-- token = strings.TrimSpace(token)- if token == "" {- return nil, "anonymous", "", nil- }-- return &authn.AuthConfig{- Username: username,- Password: token,- }, "credential", username, nil-}--func (s *ContainerRegistryService) fetchDockerHubRateLimitInternal(ctx context.Context, credential *authn.AuthConfig) (*registry.RateLimitInfo, error) {- return registry.FetchRegistryRateLimit(ctx, "docker.io", dockerHubRateLimitRepository, dockerHubRateLimitTag, credential, dockerHubRateLimitHTTPClientInternal(s.distributionHTTPClient))-}--func dockerHubRateLimitHTTPClientInternal(httpClient *http.Client) *http.Client {- if httpClient == nil {- return nil- }-- cloned := *httpClient- baseTransport := cloned.Transport- if baseTransport == nil {- baseTransport = http.DefaultTransport- }+func (s *ContainerRegistryService) buildRegistryPullUsage(ctx context.Context, reg ContainerRegistry) containerregistry.PullUsage {+ credential, credErr := func() (*resolvedRegistryCredential, error) {+ if registryauth.NormalizeRegistryForComparison(reg.URL) != "docker.io" || !reg.Enabled || reg.RegistryType != RegistryTypeGeneric {+ return nil, nil+ }+ return s.credentialForRegistry(ctx, ®)+ }()+ rateLimit, rateLimitErr := func() (registryRateLimitCacheEntry, error) {+ switch {+ case registryauth.NormalizeRegistryForComparison(reg.URL) != "docker.io" || !reg.Enabled:+ return registryRateLimitCacheEntry{CheckedAt: time.Now().UTC()}, nil+ case credErr != nil:+ return registryRateLimitCacheEntry{CheckedAt: time.Now().UTC()}, credErr+ }+ if cached, cachedAt, ok := s.getCachedRateLimit(ctx, reg.ID); ok {+ return registryRateLimitCacheEntry{RateLimit: *cached, CheckedAt: cachedAt}, nil+ }- cloned.Transport = rateLimitRoundTripFuncInternal(func(req *http.Request) (*http.Response, error) {- if req.Method == http.MethodGet &&- req.URL.Host == "registry-1.docker.io" &&- req.URL.Path == "/v2/"+dockerHubRateLimitRepository+"/manifests/"+dockerHubRateLimitTag {- rewritten := req.Clone(req.Context())- rewritten.Method = http.MethodHead- req = rewritten+ fetched, fetchErr := registry.FetchRegistryRateLimit(ctx, "docker.io", dockerHubRateLimitRepository, dockerHubRateLimitTag, func() *authn.AuthConfig {+ if credential == nil {+ return nil+ }+ return &authn.AuthConfig{Username: credential.Username, Password: credential.Token}+ }(), &http.Client{+ Transport: rateLimitRoundTripFunc(func(req *http.Request) (*http.Response, error) {+ if req.Method == http.MethodGet &&+ req.URL.Host == "registry-1.docker.io" &&+ req.URL.Path == "/v2/"+dockerHubRateLimitRepository+"/manifests/"+dockerHubRateLimitTag {+ rewritten := req.Clone(req.Context())+ rewritten.Method = http.MethodHead // keep: http.Request is only copyable via Clone+ return cmp.Or(s.distributionHTTPClient.Transport, http.DefaultTransport).RoundTrip(rewritten)+ }+ return cmp.Or(s.distributionHTTPClient.Transport, http.DefaultTransport).RoundTrip(req)+ }),+ CheckRedirect: s.distributionHTTPClient.CheckRedirect,+ Jar: s.distributionHTTPClient.Jar,+ Timeout: s.distributionHTTPClient.Timeout,+ })+ if fetchErr != nil {+ return registryRateLimitCacheEntry{CheckedAt: time.Now().UTC()}, fetchErr}- return baseTransport.RoundTrip(req)- })+ if s.kvService == nil {+ return registryRateLimitCacheEntry{RateLimit: *fetched, CheckedAt: time.Now().UTC()}, nil+ }+ payload, encodeErr := json.Marshal(registryRateLimitCacheEntry{RateLimit: *fetched, CheckedAt: time.Now().UTC()})+ if encodeErr == nil {+ encodeErr = s.kvService.Set(ctx, registryRateLimitKeyPrefix+reg.ID, string(payload))+ }+ if encodeErr != nil {+ slog.WarnContext(ctx, "failed to save registry rate limit cache", "registryId", reg.ID, "error", encodeErr)+ }+ return registryRateLimitCacheEntry{RateLimit: *fetched, CheckedAt: time.Now().UTC()}, nil+ }()- return &cloned+ return containerregistry.PullUsage{+ RegistryID: reg.ID,+ Provider: func() string {+ switch {+ case reg.RegistryType == RegistryTypeECR:+ return "ecr"+ case registryauth.NormalizeRegistryForComparison(reg.URL) == "docker.io":+ return "dockerhub"+ default:+ return "generic"+ }+ }(),+ Registry: registryauth.NormalizeRegistryForComparison(reg.URL),+ DisplayName: func() string {+ switch {+ case reg.RegistryType == RegistryTypeECR:+ return "Amazon ECR"+ case registryauth.NormalizeRegistryForComparison(reg.URL) == "" || registryauth.NormalizeRegistryForComparison(reg.URL) == "docker.io":+ return "Docker Hub"+ case strings.Contains(registryauth.NormalizeRegistryForComparison(reg.URL), "ghcr.io"):+ return "GitHub Container Registry"+ case strings.Contains(registryauth.NormalizeRegistryForComparison(reg.URL), "gcr.io"):+ return "Google Container Registry"+ case strings.Contains(registryauth.NormalizeRegistryForComparison(reg.URL), "quay.io"):+ return "Quay.io Registry"+ default:+ return registryauth.NormalizeRegistryForComparison(reg.URL)+ }+ }(),+ Repository: kit.Ternary(registryauth.NormalizeRegistryForComparison(reg.URL) == "docker.io" && reg.Enabled, dockerHubRateLimitRepository, ""),+ Limit: rateLimit.RateLimit.Limit,+ Remaining: rateLimit.RateLimit.Remaining,+ Used: cmp.Or(rateLimit.RateLimit.Used, kit.Ternary(+ rateLimit.RateLimit.Limit != nil && rateLimit.RateLimit.Remaining != nil,+ new(max(kit.FromPtr(rateLimit.RateLimit.Limit)-kit.FromPtr(rateLimit.RateLimit.Remaining), 0)),+ nil,+ )),+ WindowSeconds: rateLimit.RateLimit.WindowSeconds,+ ObservedPulls: func() int64 {+ if s.kvService == nil || registryauth.NormalizeRegistryForComparison(reg.URL) == "" {+ return 0+ }+ observedPulls, pullsErr := s.kvService.GetInt64(ctx, registryPullCountKeyPrefix+registryauth.NormalizeRegistryForComparison(reg.URL), 0)+ if pullsErr != nil {+ slog.WarnContext(ctx, "failed to read registry pull count", "registry", registryauth.NormalizeRegistryForComparison(reg.URL), "error", pullsErr)+ }+ return observedPulls+ }(),+ AuthMethod: func() string {+ switch {+ case registryauth.NormalizeRegistryForComparison(reg.URL) != "docker.io" || !reg.Enabled:+ return "unknown"+ case credential != nil || credErr != nil:+ return "credential"+ default:+ return "anonymous"+ }+ }(),+ AuthUsername: kit.Ternary(credential != nil || credErr != nil, strings.TrimSpace(reg.Username), ""),+ Source: rateLimit.RateLimit.Source,+ CheckedAt: rateLimit.CheckedAt,+ Error: kit.Ternary(rateLimitErr == nil, "", fmt.Sprint(rateLimitErr)),+ }}-func (s *ContainerRegistryService) getCachedRateLimitInternal(ctx context.Context, registryID string) (*registry.RateLimitInfo, time.Time, bool) {+func (s *ContainerRegistryService) getCachedRateLimit(ctx context.Context, registryID string) (*registry.RateLimitInfo, time.Time, bool) {if s.kvService == nil || registryID == "" {return nil, time.Time{}, false}- raw, ok, err := s.kvService.Get(ctx, registryRateLimitKeyInternal(registryID))+ raw, ok, err := s.kvService.Get(ctx, registryRateLimitKeyPrefix+strings.TrimSpace(registryID))if err != nil {slog.WarnContext(ctx, "failed to read registry rate limit cache", "registryId", registryID, "error", err)return nil, time.Time{}, false@@ -749,7 +665,7 @@ func (s *ContainerRegistryService) getCachedRateLimitInternal(ctx context.Contexreturn nil, time.Time{}, false}- var entry registryRateLimitCacheEntryInternal+ var entry registryRateLimitCacheEntryif unmarshalErr := json.Unmarshal([]byte(raw), &entry); unmarshalErr != nil {slog.WarnContext(ctx, "failed to parse registry rate limit cache", "registryId", registryID, "error", unmarshalErr)return nil, time.Time{}, false@@ -761,67 +677,6 @@ func (s *ContainerRegistryService) getCachedRateLimitInternal(ctx context.Contexreturn &entry.RateLimit, entry.CheckedAt, true}-func (s *ContainerRegistryService) setCachedRateLimitInternal(ctx context.Context, registryID string, rateLimit *registry.RateLimitInfo, checkedAt time.Time) {- if s.kvService == nil || registryID == "" || rateLimit == nil {- return- }-- payload, err := json.Marshal(registryRateLimitCacheEntryInternal{- RateLimit: *rateLimit,- CheckedAt: checkedAt,- })- if err != nil {- slog.WarnContext(ctx, "failed to encode registry rate limit cache", "registryId", registryID, "error", err)- return- }-- if setErr := s.kvService.Set(ctx, registryRateLimitKeyInternal(registryID), string(payload)); setErr != nil {- slog.WarnContext(ctx, "failed to save registry rate limit cache", "registryId", registryID, "error", setErr)- }-}--func normalizePullRegistryHostInternal(imageRef string) (string, error) {- registryHost, err := registryauth.GetRegistryAddress(imageRef)- if err != nil {- return "", fmt.Errorf("parse image registry for %q: %w", imageRef, err)- }-- return registryauth.NormalizeRegistryForComparison(registryHost), nil-}--func registryPullCountKeyInternal(registryHost string) string {- return registryPullCountKeyPrefix + registryauth.NormalizeRegistryForComparison(registryHost)-}--func registryRateLimitKeyInternal(registryID string) string {- return registryRateLimitKeyPrefix + strings.TrimSpace(registryID)-}--func registryProviderInternal(registryHost, registryType string) string {- if registryType == RegistryTypeECR {- return "ecr"- }- return kit.Ternary(registryHost == "docker.io", "dockerhub", "generic")-}--func registryDisplayNameInternal(registryHost, registryType string) string {- if registryType == RegistryTypeECR {- return "Amazon ECR"- }- switch {- case registryHost == "" || registryHost == "docker.io":- return "Docker Hub"- case strings.Contains(registryHost, "ghcr.io"):- return "GitHub Container Registry"- case strings.Contains(registryHost, "gcr.io"):- return "Google Container Registry"- case strings.Contains(registryHost, "quay.io"):- return "Quay.io Registry"- default:- return registryHost- }-}-func (s *ContainerRegistryService) TestRegistry(ctx context.Context, registryURL, username, token string) (testErr error) {ctx, span := otel.Tracer(tracing.InstrumentationName).Start(ctx, "registry.test_credentials", trace.WithAttributes(attribute.String("arcane.registry.host", registryauth.NormalizeRegistryForComparison(registryURL)),@@ -832,7 +687,7 @@ func (s *ContainerRegistryService) TestRegistry(ctx context.Context, registryURLreturn nil}- dockerClient, err := s.getDockerClientInternal(ctx)+ dockerClient, err := s.getDockerClient(ctx)if err != nil {return err}@@ -840,7 +695,7 @@ func (s *ContainerRegistryService) TestRegistry(ctx context.Context, registryURL_, err = dockerClient.RegistryLogin(ctx, client.RegistryLoginOptions{Username: strings.TrimSpace(username),Password: strings.TrimSpace(token),- ServerAddress: normalizeRegistryServerAddressInternal(registryURL),+ ServerAddress: normalizeRegistryServerAddress(registryURL),})if err != nil {return fmt.Errorf("registry login failed: %w", err)@@ -856,41 +711,26 @@ func (s *ContainerRegistryService) TestECRRegistry(ctx context.Context, reg *Conif err != nil {return fmt.Errorf("failed to obtain ECR token: %w", err)}-- dockerClient, err := s.getDockerClientInternal(ctx)- if err != nil {- return err- }-- _, err = dockerClient.RegistryLogin(ctx, client.RegistryLoginOptions{- Username: ecrUser,- Password: ecrPass,- ServerAddress: normalizeRegistryServerAddressInternal(reg.URL),- })- if err != nil {- return fmt.Errorf("ECR registry login failed: %w", err)- }-- return nil+ return s.TestRegistry(ctx, reg.URL, ecrUser, ecrPass)}// ImageDigest fetches the current digest for an image:tag from the registry// This is used for digest-based update detection for non-semver tagsfunc (s *ContainerRegistryService) ImageDigest(ctx context.Context, imageRef string) (string, error) {- normalizedRef, _, err := normalizeImageReferenceForDistributionInternal(imageRef)+ parts, err := refs.NormalizeReference(imageRef)if err != nil {return "", err}- digestValue, found, err := s.cache.GetWithLoaders(normalizedRef, func(_ []string) (map[string]string, error) {+ digestValue, found, err := s.cache.GetWithLoaders(parts.NormalizedRef, func(_ []string) (map[string]string, error) {loadCtx, cancel := context.WithTimeout(ctx, timeouts.DefaultRegistry)defer cancel()- result, loadErr := s.InspectImageDigest(loadCtx, normalizedRef, nil)+ result, loadErr := s.InspectImageDigest(loadCtx, parts.NormalizedRef, nil)if loadErr != nil {return nil, loadErr}- return map[string]string{normalizedRef: result.Digest}, nil+ return map[string]string{parts.NormalizedRef: result.Digest}, nil})if err != nil {return "", err@@ -914,35 +754,62 @@ func (s *ContainerRegistryService) InspectImageDigest(ctx context.Context, imagereturn nil, common.Classify(common.ErrValidation, err)}+ withDigest := func(value string, result *containerregistry.DigestResult, err error) (*containerregistry.DigestResult, error) {+ return &containerregistry.DigestResult{+ Digest: value,+ AuthMethod: result.AuthMethod,+ AuthUsername: result.AuthUsername,+ AuthRegistry: result.AuthRegistry,+ UsedCredential: result.UsedCredential,+ }, err+ }+ inspectViaDaemon := func() (*containerregistry.DigestResult, error) {+ dockerClient, clientErr := s.getDockerClient(ctx)+ if clientErr != nil {+ return nil, clientErr+ }+ return withDigest(registryOperationWithCredentials(ctx, s, parts.RegistryHost, "distribution inspect of "+parts.NormalizedRef, externalCreds,+ func(ctx context.Context, credential *resolvedRegistryCredential) (string, error) {+ authHeader, encodeErr := func() (string, error) {+ if credential == nil {+ return "", nil+ }+ return registryauth.EncodeAuthHeader(credential.Username, credential.Token, credential.ServerAddress)+ }()+ if encodeErr != nil {+ return "", fmt.Errorf("encode registry auth header for %s: %w", parts.RegistryHost, encodeErr)+ }+ inspectResult, inspectErr := dockerClient.DistributionInspect(ctx, parts.NormalizedRef, client.DistributionInspectOptions{EncodedRegistryAuth: authHeader})+ if inspectErr != nil {+ return "", inspectErr+ }+ digestValue, normalizeErr := digest.Normalize(inspectResult.Descriptor.Digest.String())+ if normalizeErr != nil {+ return "", fmt.Errorf("distribution inspect returned invalid digest for %s: %w", parts.NormalizedRef, normalizeErr)+ }+ return digestValue, nil+ }))+ }+var lastResult *containerregistry.DigestResultvar lastErr errorfallbackWarningLogged := false- bo := backoff.NewExponentialBackOff()- bo.InitialInterval = 500 * time.Millisecond- bo.MaxInterval = 10 * time.Second- bo.RandomizationFactor = 0.3-_, retryErr := backoff.Retry(ctx, func() (*containerregistry.DigestResult, error) {- result, inspectImageDigestViaDaemonErr := s.inspectImageDigestViaDaemonInternal(ctx, parts.NormalizedRef, parts.RegistryHost, externalCreds)- if inspectImageDigestViaDaemonErr == nil {- lastResult = result+ result, daemonErr := inspectViaDaemon()+ lastResult, lastErr = result, daemonErr+ if daemonErr == nil {return result, nil}-- if isRateLimitErrorInternal(inspectImageDigestViaDaemonErr) {- lastResult = result- lastErr = inspectImageDigestViaDaemonErr- slog.DebugContext(ctx, "rate limited by registry, will retry",- "registry", parts.RegistryHost,- "imageRef", parts.NormalizedRef)- return nil, inspectImageDigestViaDaemonErr+ if isRateLimitError(daemonErr) {+ slog.DebugContext(ctx, "rate limited by registry, will retry", "registry", parts.RegistryHost, "imageRef", parts.NormalizedRef)+ return nil, daemonErr}-- if !isDistributionFallbackEligibleInternal(inspectImageDigestViaDaemonErr) {- lastResult = result- lastErr = inspectImageDigestViaDaemonErr- return nil, backoff.Permanent(inspectImageDigestViaDaemonErr)+ if !registry.IsFallbackEligibleDaemonError(daemonErr) &&+ (browse.IsUnauthorizedRegistryError(daemonErr) || !slices.ContainsFunc([]string{"context deadline exceeded", "client.timeout exceeded", "i/o timeout"}, func(indicator string) bool {+ return strings.Contains(strings.ToLower(daemonErr.Error()), indicator)+ })) {+ return nil, backoff.Permanent(daemonErr)}if !fallbackWarningLogged {@@ -950,155 +817,130 @@ func (s *ContainerRegistryService) InspectImageDigest(ctx context.Context, imageslog.WarnContext(ctx, "distribution inspect unavailable, falling back to direct registry digest lookup","imageRef", parts.NormalizedRef,"registry", parts.RegistryHost,- "error", inspectImageDigestViaDaemonErr.Error())+ "error", daemonErr.Error())}- fallbackResult, fallbackErr := s.inspectImageDigestViaRegistryInternal(ctx, parts.RegistryHost, parts.Repository, parts.Tag, externalCreds)+ fallbackResult, fallbackErr := withDigest(registryOperationWithCredentials(ctx, s, parts.RegistryHost,+ "registry manifest inspect of "+parts.RegistryHost+"/"+parts.Repository+":"+parts.Tag, externalCreds,+ func(ctx context.Context, credential *resolvedRegistryCredential) (string, error) {+ return registry.FetchDigest(ctx, parts.RegistryHost, parts.Repository, parts.Tag, func() *authn.AuthConfig {+ if credential == nil {+ return nil+ }+ return &authn.AuthConfig{Username: credential.Username, Password: credential.Token}+ }(), s.distributionHTTPClient)+ }))+ lastResult, lastErr = fallbackResult, fallbackErrif fallbackErr == nil {- lastResult = fallbackResultreturn fallbackResult, nil}-- if isRateLimitErrorInternal(fallbackErr) {- lastResult = fallbackResult- lastErr = fallbackErr- slog.DebugContext(ctx, "rate limited by registry on fallback, will retry",- "registry", parts.RegistryHost,- "imageRef", parts.NormalizedRef)+ if isRateLimitError(fallbackErr) {+ slog.DebugContext(ctx, "rate limited by registry on fallback, will retry", "registry", parts.RegistryHost, "imageRef", parts.NormalizedRef)return nil, fallbackErr}- lastResult = fallbackResult- lastErr = fmt.Errorf("daemon digest lookup failed; registry fallback failed: %w", errors.Join(inspectImageDigestViaDaemonErr, fallbackErr))+ lastErr = fmt.Errorf("daemon digest lookup failed; registry fallback failed: %w", errors.Join(daemonErr, fallbackErr))return nil, backoff.Permanent(lastErr)- }, backoff.WithBackOff(bo), backoff.WithMaxTries(5))+ }, backoff.WithBackOff(&backoff.ExponentialBackOff{+ InitialInterval: 500 * time.Millisecond,+ RandomizationFactor: 0.3,+ Multiplier: backoff.DefaultMultiplier,+ MaxInterval: 10 * time.Second,+ }), backoff.WithMaxTries(5))- if retryErr != nil {- if errors.Is(retryErr, context.Canceled) {- return lastResult, retryErr- }- if errors.Is(retryErr, context.DeadlineExceeded) {- return lastResult, common.Classify(common.ErrTimeout, retryErr)- }- return lastResult, classifyDigestErrorInternal(lastErr)+ switch {+ case retryErr == nil:+ return lastResult, nil+ case errors.Is(retryErr, context.Canceled):+ return lastResult, retryErr+ case errors.Is(retryErr, context.DeadlineExceeded):+ return lastResult, common.Classify(common.ErrTimeout, retryErr)+ default:+ return lastResult, classifyDigestError(lastErr)}-- return lastResult, nil}-// classifyDigestErrorInternal tags digest lookup failures so callers can retry+// classifyDigestError tags digest lookup failures so callers can retry// transient ones. Retryable kinds are checked first because fallback errors are joined.-func classifyDigestErrorInternal(err error) error {+func classifyDigestError(err error) error {if err == nil {return nil}- errLower := strings.ToLower(err.Error())transportErr, isTransport := errors.AsType[*transport.Error](err)- var netErr net.Error- var dnsErr *net.DNSError+ _, isNetErr := errors.AsType[net.Error](err)+ dnsErr, isDNSErr := errors.AsType[*net.DNSError](err)switch {- case errors.As(err, &dnsErr) && dnsErr.IsNotFound:+ case isDNSErr && dnsErr.IsNotFound:// A registry host that does not resolve is a bad reference, not an outage worth retrying.return err- case isRateLimitErrorInternal(err), errdefs.IsUnavailable(err), errors.As(err, &netErr),- strings.Contains(errLower, "connection refused"), isTransport && transportErr.StatusCode >= http.StatusInternalServerError:+ case isRateLimitError(err), errdefs.IsUnavailable(err), isNetErr,+ strings.Contains(strings.ToLower(err.Error()), "connection refused"), isTransport && transportErr.StatusCode >= http.StatusInternalServerError:return common.Classify(common.ErrUnavailable, err)case errors.Is(err, context.DeadlineExceeded), errdefs.IsDeadlineExceeded(err):return common.Classify(common.ErrTimeout, err)case browse.IsUnauthorizedRegistryError(err):return common.Classify(common.ErrUnauthorized, err)- case errdefs.IsNotFound(err), isTransport && transportErr.StatusCode == http.StatusNotFound, strings.Contains(errLower, "manifest unknown"):+ case errdefs.IsNotFound(err), isTransport && transportErr.StatusCode == http.StatusNotFound, strings.Contains(strings.ToLower(err.Error()), "manifest unknown"):return common.Classify(common.ErrNotFound, err)default:return err}}-func (- s *ContainerRegistryService,-) inspectImageDigestViaDaemonInternal(- ctx context.Context,- normalizedRef, registryHost string,- externalCreds []containerregistry.Credential,-) (- *containerregistry.DigestResult,- error,-) {- dockerClient, err := s.getDockerClientInternal(ctx)- if err != nil {- return nil, err- }-- value, result, err := registryOperationWithCredentialsInternal(ctx, s, registryHost, "distribution inspect of "+normalizedRef, externalCreds,- func(ctx context.Context, credential *resolvedRegistryCredential) (string, error) {- return s.fetchDigestFromDaemonInternal(ctx, dockerClient, registryHost, normalizedRef, credential)- })- if result != nil {- result.Digest = value- }- return result, err-}--func (- s *ContainerRegistryService,-) inspectImageDigestViaRegistryInternal(- ctx context.Context,- registryHost, repository, tag string,- externalCreds []containerregistry.Credential,-) (- *containerregistry.DigestResult,- error,-) {- value, result, err := registryOperationWithCredentialsInternal(ctx, s, registryHost, "registry manifest inspect of "+registryHost+"/"+repository+":"+tag, externalCreds,- func(ctx context.Context, credential *resolvedRegistryCredential) (string, error) {- return s.fetchDigestFromRegistryInternal(ctx, registryHost, repository, tag, credential)- })- if result != nil {- result.Digest = value- }- return result, err-}-// Stored credentials go first: anonymous requests share a per-registry quota with every other unauthenticated client.-func registryOperationWithCredentialsInternal[- T any,-](+func registryOperationWithCredentials[T any](ctx context.Context,s *ContainerRegistryService,registryHost, operation string,externalCreds []containerregistry.Credential,- fetch func(- context.Context,- *resolvedRegistryCredential,- ) (- T,- error,- ),-) (- T,- *containerregistry.DigestResult,- error,-) {- var zero T- matchedCredentials, credErr := s.getMatchingRegistryCredentialsInternal(ctx, registryHost, externalCreds)+ fetch func(context.Context, *resolvedRegistryCredential) (T, error),+) (T, *containerregistry.DigestResult, error) {+ registries, credErr := func() ([]ContainerRegistry, error) {+ if len(externalCreds) > 0 || s == nil || s.db == nil {+ return nil, nil+ }+ return s.GetEnabledRegistries(ctx)+ }()++ var matchedCredentials []resolvedRegistryCredential+ for _, cred := range externalCreds {+ if cred.Enabled && strings.TrimSpace(cred.Username) != "" && strings.TrimSpace(cred.Token) != "" && registryauth.IsRegistryMatch(cred.URL, registryHost) {+ matchedCredentials = append(matchedCredentials, resolvedRegistryCredential{+ Username: strings.TrimSpace(cred.Username),+ Token: strings.TrimSpace(cred.Token),+ ServerAddress: normalizeRegistryServerAddress(cred.URL),+ })+ }+ }+ for i := range registries {+ if !registryauth.IsRegistryMatch(registries[i].URL, registryHost) {+ continue+ }+ credential, resolveErr := s.credentialForRegistry(ctx, ®istries[i])+ if resolveErr != nil {+ slog.WarnContext(ctx, "failed to resolve registry credential", "registry", registries[i].URL, "error", resolveErr)+ } else if credential != nil {+ matchedCredentials = append(matchedCredentials, *credential)+ }+ }var lastErr errorvar lastResult *containerregistry.DigestResultfor _, credential := range matchedCredentials {lastResult = &containerregistry.DigestResult{AuthMethod: "credential", AuthUsername: credential.Username, AuthRegistry: registryHost, UsedCredential: true}- value, err := fetch(ctx, &credential)- if err == nil {+ value, fetchErr := fetch(ctx, &credential)+ if fetchErr == nil {return value, lastResult, nil}- lastErr = err- if !browse.IsUnauthorizedRegistryError(err) {- return zero, lastResult, fmt.Errorf("%s failed with credentials: %w", operation, err)+ lastErr = fetchErr+ if !browse.IsUnauthorizedRegistryError(fetchErr) {+ return *new(T), lastResult, fmt.Errorf("%s failed with credentials: %w", operation, fetchErr)}}if lastErr != nil {// Docker Hub anonymous quotas are too small to be worth a retry; elsewhere a stale credential must not break public images.- if isDockerHubRegistryInternal(registryHost) {- return zero, lastResult, fmt.Errorf("%s failed: %w", operation, lastErr)+ if registryauth.NormalizeRegistryForComparison(registryHost) == "docker.io" {+ return *new(T), lastResult, fmt.Errorf("%s failed: %w", operation, lastErr)}slog.DebugContext(ctx, "credentialed registry lookup failed, retrying anonymously","registry", registryHost,@@ -1106,22 +948,22 @@ func registryOperationWithCredentialsInternal["error", lastErr.Error())}- result := &containerregistry.DigestResult{AuthMethod: "anonymous", AuthRegistry: registryHost}value, err := fetch(ctx, nil)- if err == nil {- return value, result, nil- }- if credErr != nil && browse.IsUnauthorizedRegistryError(err) {- return zero, result, fmt.Errorf("%s: anonymous access unauthorized; credential lookup failed: %w", operation, errors.Join(err, credErr))+ switch {+ case err == nil:+ return value, &containerregistry.DigestResult{AuthMethod: "anonymous", AuthRegistry: registryHost}, nil+ case credErr != nil && browse.IsUnauthorizedRegistryError(err):+ return *new(T), &containerregistry.DigestResult{AuthMethod: "anonymous", AuthRegistry: registryHost},+ fmt.Errorf("%s: anonymous access unauthorized; credential lookup failed: %w", operation, errors.Join(err, fmt.Errorf("failed to load enabled registries: %w", credErr)))+ default:+ return *new(T), &containerregistry.DigestResult{AuthMethod: "anonymous", AuthRegistry: registryHost}, fmt.Errorf("%s failed: %w", operation, err)}- return zero, result, fmt.Errorf("%s failed: %w", operation, err)}-func (s *ContainerRegistryService) getDockerClientInternal(ctx context.Context) (RegistryDaemonClient, error) {+func (s *ContainerRegistryService) getDockerClient(ctx context.Context) (RegistryDaemonClient, error) {if s.dockerClient == nil {return nil, errors.New("docker client unavailable")}-dockerClient, err := s.dockerClient(ctx)if err != nil {return nil, fmt.Errorf("failed to get docker client: %w", err)@@ -1129,71 +971,12 @@ func (s *ContainerRegistryService) getDockerClientInternal(ctx context.Context)if dockerClient == nil {return nil, errors.New("docker client unavailable")}-return dockerClient, nil}-func (- s *ContainerRegistryService,-) getMatchingRegistryCredentialsInternal(- ctx context.Context,- registryHost string,- externalCreds []containerregistry.Credential,-) (- []resolvedRegistryCredential,- error,-) {- if len(externalCreds) > 0 {- resolvedCredentials := make([]resolvedRegistryCredential, 0, len(externalCreds))- for _, cred := range externalCreds {- if !cred.Enabled || strings.TrimSpace(cred.Username) == "" || strings.TrimSpace(cred.Token) == "" {- continue- }- if !registryauth.IsRegistryMatch(cred.URL, registryHost) {- continue- }-- resolvedCredentials = append(resolvedCredentials, resolvedRegistryCredential{- Username: strings.TrimSpace(cred.Username),- Token: strings.TrimSpace(cred.Token),- ServerAddress: normalizeRegistryServerAddressInternal(cred.URL),- })- }- return resolvedCredentials, nil- }-- if s == nil || s.db == nil {- return nil, nil- }-- registries, err := s.GetEnabledRegistries(ctx)- if err != nil {- return nil, fmt.Errorf("failed to load enabled registries: %w", err)- }-- creds := make([]resolvedRegistryCredential, 0, len(registries))- for i := range registries {- reg := ®istries[i]- if !registryauth.IsRegistryMatch(reg.URL, registryHost) {- continue- }-- credential, credErr := s.credentialForRegistryInternal(ctx, reg)- if credErr != nil {- slog.WarnContext(ctx, "failed to resolve registry credential", "registry", reg.URL, "error", credErr)- continue- }- if credential != nil {- creds = append(creds, *credential)- }- }-- return creds, nil-}--// credentialForRegistryInternal resolves the stored credential of one registry.+// credentialForRegistry resolves the stored credential of one registry.// It returns nil when the registry is configured for anonymous access.-func (s *ContainerRegistryService) credentialForRegistryInternal(ctx context.Context, reg *ContainerRegistry) (*resolvedRegistryCredential, error) {+func (s *ContainerRegistryService) credentialForRegistry(ctx context.Context, reg *ContainerRegistry) (*resolvedRegistryCredential, error) {if reg.RegistryType == RegistryTypeECR {ecrUser, ecrPass, err := s.GetOrRefreshECRToken(ctx, reg)if err != nil {@@ -1202,392 +985,154 @@ func (s *ContainerRegistryService) credentialForRegistryInternal(ctx context.Conreturn &resolvedRegistryCredential{Username: ecrUser,Token: ecrPass,- ServerAddress: normalizeRegistryServerAddressInternal(reg.URL),+ ServerAddress: normalizeRegistryServerAddress(reg.URL),}, nil}- username := strings.TrimSpace(reg.Username)- if username == "" || reg.Token == "" {+ if strings.TrimSpace(reg.Username) == "" || reg.Token == "" {return nil, nil}-token, err := crypto.Decrypt(reg.Token)if err != nil {return nil, fmt.Errorf("failed to decrypt registry token: %w", err)}- token = strings.TrimSpace(token)- if token == "" {+ if strings.TrimSpace(token) == "" {return nil, nil}return &resolvedRegistryCredential{- Username: username,- Token: token,- ServerAddress: normalizeRegistryServerAddressInternal(reg.URL),+ Username: strings.TrimSpace(reg.Username),+ Token: strings.TrimSpace(token),+ ServerAddress: normalizeRegistryServerAddress(reg.URL),}, nil}-// browseRegistryInternal resolves a stored registry and its credential for browsing.-func (s *ContainerRegistryService) browseRegistryInternal(ctx context.Context, id string) (containerregistry.ContainerRegistry, *containerregistry.Credential, error) {- reg, err := s.GetRegistryByID(ctx, id)- if err != nil {- return containerregistry.ContainerRegistry{}, nil, err- }- credential, err := s.credentialForRegistryInternal(ctx, reg)- if err != nil {- return containerregistry.ContainerRegistry{}, nil, err- }- result := containerregistry.ContainerRegistry{ID: reg.ID, URL: reg.URL, Username: reg.Username, Insecure: reg.Insecure, Enabled: reg.Enabled}- if credential == nil {- return result, nil, nil- }- return result, &containerregistry.Credential{URL: reg.URL, Username: credential.Username, Token: credential.Token, Enabled: true}, nil-}-// SyncRegistries syncs registries from a manager to this agent instance// It creates, updates, or deletes registries to match the provided listfunc (s *ContainerRegistryService) SyncRegistries(ctx context.Context, syncItems []containerregistry.Sync) error {if err := normalization.Normalize(&syncItems); err != nil {return err}- existingMap, err := s.getExistingRegistriesMapInternal(ctx)+ existingRegistries, err := s.GetAllRegistries(ctx)if err != nil {return err}+ existingMap := make(map[string]ContainerRegistry, len(existingRegistries))+ for _, existing := range existingRegistries {+ existingMap[existing.ID] = existing+ }- syncedIDs := make(map[string]bool)-- // Process each sync itemfor _, item := range syncItems {- syncedIDs[item.ID] = true-- if processSyncItemErr := s.processSyncItemInternal(ctx, item, existingMap); processSyncItemErr != nil {- return processSyncItemErr+ registryType, typeErr := NormalizeRegistryType(item.RegistryType)+ if typeErr != nil {+ return typeErr}- }-- // Delete registries that are not in the sync list- return s.deleteUnsyncedInternal(ctx, existingMap, syncedIDs)-}--func (s *ContainerRegistryService) getExistingRegistriesMapInternal(ctx context.Context) (map[string]*ContainerRegistry, error) {- var existingRegistries []ContainerRegistry- if err := s.db.WithContext(ctx).Find(&existingRegistries).Error; err != nil {- return nil, fmt.Errorf("failed to get existing registries: %w", err)- }-- existingMap := make(map[string]*ContainerRegistry)- for i := range existingRegistries {- existingMap[existingRegistries[i].ID] = &existingRegistries[i]- }-- return existingMap, nil-}--func (s *ContainerRegistryService) processSyncItemInternal(ctx context.Context, item containerregistry.Sync, existingMap map[string]*ContainerRegistry) error {- existing, exists := existingMap[item.ID]- if exists {- return s.updateExistingRegistryInternal(ctx, item, existing)- }- return s.createNewRegistryInternal(ctx, item)-}--func (s *ContainerRegistryService) updateExistingRegistryInternal(ctx context.Context, item containerregistry.Sync, existing *ContainerRegistry) error {- needsUpdate, err := s.checkRegistryNeedsUpdateInternal(item, existing)- if err != nil {- return err- }-- if needsUpdate {- existing.UpdatedAt = time.Now()- if syncRegistryErr := s.db.WithContext(ctx).Save(existing).Error; syncRegistryErr != nil {- return fmt.Errorf("failed to update registry %s: %w", item.ID, syncRegistryErr)+ // Normalizing first gives the manager and the local copy the same representation, so only real changes are saved.+ repositoryNames, namesErr := normalizeRepositoryNames(item.RepositoryNames)+ if namesErr != nil {+ return namesErr}- }- return nil-}--func (s *ContainerRegistryService) checkRegistryNeedsUpdateInternal(item containerregistry.Sync, existing *ContainerRegistry) (bool, error) {- newType, err := NormalizeRegistryType(item.RegistryType)- if err != nil {- return false, err- }-- needsUpdate := utils.ApplyChanged(&existing.URL, mo.Some(item.URL))- needsUpdate = utils.ApplyNullable(&existing.Description, mo.PointerToOption(item.Description)) || needsUpdate- needsUpdate = utils.ApplyChanged(&existing.Insecure, mo.Some(item.Insecure)) || needsUpdate- needsUpdate = utils.ApplyChanged(&existing.Enabled, mo.Some(item.Enabled)) || needsUpdate-- // Normalizing first gives the manager and the local copy the same- // representation, so the comparison below only reports real changes.- repositoryNames, err := normalizeRepositoryNamesInternal(item.RepositoryNames)- if err != nil {- return false, err- }- needsUpdate = utils.ApplySliceChanged(&existing.RepositoryNames, mo.Some(repositoryNames)) || needsUpdate-- // Clear stale credentials when registry type changes during sync- if newType != existing.RegistryType {- if newType == RegistryTypeECR {- existing.Username = ""- existing.Token = ""- } else {- existing.AWSAccessKeyID = ""- existing.AWSSecretAccessKey = ""- existing.AWSRegion = ""- existing.ECRToken = ""- existing.ECRTokenGeneratedAt = nil+ existing, exists := existingMap[item.ID]+ credential := kit.Ternary(registryType == RegistryTypeGeneric, existing.Token, existing.AWSSecretAccessKey)+ credentialChanged, applyErr := utils.ApplyEncrypted(&credential, kit.Ternary(registryType == RegistryTypeGeneric, item.Token, item.AWSSecretAccessKey))+ if applyErr != nil {+ return fmt.Errorf("failed to apply credential for registry %s: %w", item.ID, applyErr)}- needsUpdate = true- }-- needsUpdate = utils.ApplyChanged(&existing.RegistryType, mo.Some(newType)) || needsUpdate-- if newType == RegistryTypeGeneric {- needsUpdate = utils.ApplyChanged(&existing.Username, mo.Some(item.Username)) || needsUpdate- tokenChanged, applyEncryptedErr := utils.ApplyEncrypted(&existing.Token, item.Token)- if applyEncryptedErr != nil {- return false, fmt.Errorf("failed to apply token for registry %s: %w", existing.ID, applyEncryptedErr)+ if exists && registryType == existing.RegistryType && !credentialChanged &&+ kit.Ternary(registryType == RegistryTypeGeneric,+ existing.Username == item.Username,+ existing.AWSAccessKeyID == item.AWSAccessKeyID && existing.AWSRegion == item.AWSRegion,+ ) &&+ existing.URL == item.URL &&+ mo.PointerToOption(existing.Description) == mo.PointerToOption(item.Description) &&+ existing.Insecure == item.Insecure &&+ existing.Enabled == item.Enabled &&+ slices.Equal(existing.RepositoryNames, repositoryNames) {+ continue}- return tokenChanged || needsUpdate, nil- }-- credChanged := utils.ApplyChanged(&existing.AWSAccessKeyID, mo.Some(item.AWSAccessKeyID))- credChanged = utils.ApplyChanged(&existing.AWSRegion, mo.Some(item.AWSRegion)) || credChanged-- // An empty secret means the manager has no static keys, so the agent's stored one is cleared.- secretChanged, encryptAWSSecretErr := utils.ApplyEncrypted(&existing.AWSSecretAccessKey, item.AWSSecretAccessKey)- if encryptAWSSecretErr != nil {- return false, fmt.Errorf("failed to apply AWS secret for registry %s: %w", existing.ID, encryptAWSSecretErr)- }- credChanged = secretChanged || credChanged-- // Invalidate cached ECR token when credentials change- if credChanged {- existing.ECRToken = ""- existing.ECRTokenGeneratedAt = nil- }- needsUpdate = credChanged || needsUpdate-- return needsUpdate, nil-}--func (s *ContainerRegistryService) createNewRegistryInternal(ctx context.Context, item containerregistry.Sync) error {- registryType, err := NormalizeRegistryType(item.RegistryType)- if err != nil {- return err- }- repositoryNames, err := normalizeRepositoryNamesInternal(item.RepositoryNames)- if err != nil {- return err- }- newRegistry := &ContainerRegistry{- ID: item.ID,- URL: item.URL,- Description: item.Description,- Insecure: item.Insecure,- Enabled: item.Enabled,- RegistryType: registryType,- RepositoryNames: repositoryNames,- AWSAccessKeyID: item.AWSAccessKeyID,- AWSRegion: item.AWSRegion,- CreatedAt: time.Now(),- UpdatedAt: time.Now(),+ if syncErr := kit.Ternary(exists, s.db.WithContext(ctx).Save, s.db.WithContext(ctx).Create)(&ContainerRegistry{ //exhaustruct:enforce+ BaseModel: database.BaseModel{ID: item.ID, CreatedAt: existing.BaseModel.CreatedAt, UpdatedAt: existing.BaseModel.UpdatedAt},+ URL: item.URL,+ Username: kit.Ternary(registryType == RegistryTypeGeneric, item.Username, ""),+ Token: kit.Ternary(registryType == RegistryTypeGeneric, credential, ""),+ Description: item.Description,+ Insecure: item.Insecure,+ Enabled: item.Enabled,+ RegistryType: registryType,+ RepositoryNames: repositoryNames,+ AWSAccessKeyID: kit.Ternary(registryType == RegistryTypeGeneric, "", item.AWSAccessKeyID),+ AWSSecretAccessKey: kit.Ternary(registryType == RegistryTypeGeneric, "", credential),+ AWSRegion: kit.Ternary(registryType == RegistryTypeGeneric, "", item.AWSRegion),+ ECRToken: kit.Ternary(+ registryType == RegistryTypeGeneric || credentialChanged || existing.AWSAccessKeyID != item.AWSAccessKeyID || existing.AWSRegion != item.AWSRegion,+ "",+ existing.ECRToken,+ ),+ ECRTokenGeneratedAt: kit.Ternary(+ registryType == RegistryTypeGeneric || credentialChanged || existing.AWSAccessKeyID != item.AWSAccessKeyID || existing.AWSRegion != item.AWSRegion,+ nil,+ existing.ECRTokenGeneratedAt,+ ),+ CreatedAt: cmp.Or(existing.CreatedAt, time.Now()),+ UpdatedAt: time.Now(),+ }).Error; syncErr != nil {+ return fmt.Errorf("failed to sync registry %s: %w", item.ID, syncErr)+ }}- if registryType == RegistryTypeGeneric {- newRegistry.Username = item.Username-- encryptedToken, encryptErr := crypto.Encrypt(item.Token)- if encryptErr != nil {- return fmt.Errorf("failed to encrypt token for new registry %s: %w", item.ID, encryptErr)+ for id := range existingMap {+ if slices.ContainsFunc(syncItems, func(item containerregistry.Sync) bool { return item.ID == id }) {+ continue}- newRegistry.Token = encryptedToken- } else if item.AWSSecretAccessKey != "" {- encryptedSecret, encryptNewAWSSecretErr := crypto.Encrypt(item.AWSSecretAccessKey)- if encryptNewAWSSecretErr != nil {- return fmt.Errorf("failed to encrypt AWS secret for new registry %s: %w", item.ID, encryptNewAWSSecretErr)+ if deleteErr := s.DeleteRegistry(ctx, id); deleteErr != nil {+ return deleteErr}- newRegistry.AWSSecretAccessKey = encryptedSecret- }- if createRegistryErr := s.db.WithContext(ctx).Create(newRegistry).Error; createRegistryErr != nil {- return fmt.Errorf("failed to create registry %s: %w", item.ID, createRegistryErr)}return nil}func NormalizeRegistryType(value string) (string, error) {- registryType := strings.ToLower(strings.TrimSpace(value))- if registryType == "" {+ switch cmp.Or(strings.ToLower(strings.TrimSpace(value)), RegistryTypeGeneric) {+ case RegistryTypeGeneric:return RegistryTypeGeneric, nil- }-- switch registryType {- case RegistryTypeGeneric, RegistryTypeECR:- return registryType, nil+ case RegistryTypeECR:+ return RegistryTypeECR, nildefault:- return "", common.Classify(- common.ErrValidation,- &base.FieldError{- Field: "registryType",- Err: errors.New(- "registry type must be one of: generic, ecr",- ),- },- )+ return "", common.Classify(common.ErrValidation, &base.FieldError{Field: "registryType", Err: errors.New("registry type must be one of: generic, ecr")})}}-// normalizeRepositoryNamesInternal trims, filters and deduplicates the given-// entries (preserving first-occurrence order) and validates what remains. It-// returns a non-nil slice so that GORM always serializes to a JSON array.-func normalizeRepositoryNamesInternal(raw []string) (database.StringSlice, error) {- names := kit.Unique(kit.TrimNonEmpty(raw))- result := make(database.StringSlice, 0, len(names))- for _, name := range names {- // A repository name is only a path, so pair it with placeholder domain- // and tag segments to validate it against the reference grammar.+// normalizeRepositoryNames trims, deduplicates and validates entries in order.+// It returns a non-nil slice so that GORM always serializes to a JSON array.+func normalizeRepositoryNames(raw []string) (database.StringSlice, error) {+ for _, name := range kit.Unique(kit.TrimNonEmpty(raw)) {+ // A repository name is only a path, so placeholder domain and tag segments make it a full reference.if _, err := reference.ParseNormalizedNamed("registry.invalid/" + name + "/placeholder:latest"); err != nil {return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "repositoryNames", Err: fmt.Errorf("invalid repository name %q", name)})}- result = append(result, name)- }-- return result, nil-}--func (s *ContainerRegistryService) deleteUnsyncedInternal(ctx context.Context, existingMap map[string]*ContainerRegistry, syncedIDs map[string]bool) error {- for id := range existingMap {- if !syncedIDs[id] {- if err := s.db.WithContext(ctx).Where("id = ?", id).Delete(&ContainerRegistry{}).Error; err != nil {- return fmt.Errorf("failed to delete registry %s: %w", id, err)- }- }}- return nil+ return append(database.StringSlice{}, kit.Unique(kit.TrimNonEmpty(raw))...), nil}-func normalizeImageReferenceForDistributionInternal(imageRef string) (string, string, error) {- parts, err := refs.NormalizeReference(imageRef)- if err != nil {- return "", "", err- }-- return parts.NormalizedRef, parts.RegistryHost, nil-}--func normalizeRegistryServerAddressInternal(registryURL string) string {- normalizedHost := strings.TrimSpace(registryauth.NormalizeRegistryForComparison(registryURL))- if normalizedHost == "" {- return ""- }-- if normalizedHost == "docker.io" {- return registryauth.NormalizeRegistryURL(registryURL)- }-- return normalizedHost+func normalizeRegistryServerAddress(registryURL string) string {+ return kit.Ternary(+ registryauth.NormalizeRegistryForComparison(registryURL) == "docker.io",+ registryauth.NormalizeRegistryURL(registryURL),+ registryauth.NormalizeRegistryForComparison(registryURL),+ )}-func isRateLimitErrorInternal(err error) bool {- if err == nil {- return false- }- return IsRateLimitErrorString(err.Error())+func isRateLimitError(err error) bool {+ return err != nil && IsRateLimitErrorString(err.Error())}func IsRateLimitErrorString(msg string) bool {- msgLower := strings.ToLower(msg)- indicators := []string{- "toomanyrequests",- "rate limit",- "too many requests",- "status: 429",- "status 429",- "retry-after",- }- for _, indicator := range indicators {- if strings.Contains(msgLower, indicator) {- return true- }- }- return false-}--func isDockerHubRegistryInternal(registryHost string) bool {- return registryauth.NormalizeRegistryForComparison(registryHost) == "docker.io"-}--func isDistributionFallbackEligibleInternal(err error) bool {- if err == nil {- return false- }-- if registry.IsFallbackEligibleDaemonError(err) {- return true- }-- if browse.IsUnauthorizedRegistryError(err) {- return false- }-- errLower := strings.ToLower(err.Error())- return strings.Contains(errLower, "context deadline exceeded") ||- strings.Contains(errLower, "client.timeout exceeded") ||- strings.Contains(errLower, "i/o timeout")-}--func (- s *ContainerRegistryService,-) fetchDigestFromDaemonInternal(- ctx context.Context,- dockerClient RegistryDaemonClient,- registryHost, normalizedRef string,- credential *resolvedRegistryCredential,-) (- string,- error,-) {- var inspectOptions client.DistributionInspectOptions- if credential != nil {- authHeader, err := registryauth.EncodeAuthHeader(credential.Username, credential.Token, credential.ServerAddress)- if err != nil {- return "", fmt.Errorf("encode registry auth header for %s: %w", registryHost, err)- }- inspectOptions.EncodedRegistryAuth = authHeader- }-- inspectResult, err := dockerClient.DistributionInspect(ctx, normalizedRef, inspectOptions)- if err != nil {- return "", err- }- digestValue, err := digest.Normalize(inspectResult.Descriptor.Digest.String())- if err != nil {- return "", fmt.Errorf("distribution inspect returned invalid digest for %s: %w", normalizedRef, err)- }- return digestValue, nil-}--func (s *ContainerRegistryService) fetchDigestFromRegistryInternal(ctx context.Context, registryHost, repository, tag string, credential *resolvedRegistryCredential) (string, error) {- var distributionCredential *authn.AuthConfig- if credential != nil {- distributionCredential = &authn.AuthConfig{- Username: strings.TrimSpace(credential.Username),- Password: strings.TrimSpace(credential.Token),- }- }-- return registry.FetchDigest(- ctx,- registryHost,- repository,- tag,- distributionCredential,- s.distributionHTTPClient,- )+ return slices.ContainsFunc([]string{"toomanyrequests", "rate limit", "too many requests", "status: 429", "status 429", "retry-after"}, func(indicator string) bool {+ return strings.Contains(strings.ToLower(msg), indicator)+ })}// ListImageTags discovers repository tags with the same credential precedence as@@ -1605,165 +1150,147 @@ func (s *ContainerRegistryService) ListImageTags(ctx context.Context, imageRef sif err != nil {return nil, err}- lookupCtx, cancel := s.tagLookupContextInternal(ctx)+ lookupCtx, cancel := s.tagLookupContext(ctx)defer cancel()- tags, _, err := registryOperationWithCredentialsInternal(lookupCtx, s, parts.RegistryHost, "registry tag listing of "+parts.NormalizedRef, externalCreds,+ tags, _, err := registryOperationWithCredentials(lookupCtx, s, parts.RegistryHost, "registry tag listing of "+parts.NormalizedRef, externalCreds,func(ctx context.Context, credential *resolvedRegistryCredential) ([]string, error) {- var auth *authn.AuthConfig- if credential != nil {- auth = &authn.AuthConfig{Username: credential.Username, Password: credential.Token}- }- return registry.FetchTags(ctx, parts.RegistryHost, parts.Repository, auth, s.distributionHTTPClient)+ return registry.FetchTags(ctx, parts.RegistryHost, parts.Repository, func() *authn.AuthConfig {+ if credential == nil {+ return nil+ }+ return &authn.AuthConfig{Username: credential.Username, Password: credential.Token}+ }(), s.distributionHTTPClient)})return tags, err}-// tagLookupContextInternal bounds tag and manifest walks by the configured registry tag timeout.-func (s *ContainerRegistryService) tagLookupContextInternal(ctx context.Context) (context.Context, context.CancelFunc) {- timeoutSeconds := 0- if s.settingsService != nil {- timeoutSeconds = s.settingsService.GetSettingsConfig().RegistryTagTimeout.AsInt()+// tagLookupContext bounds tag and manifest walks by the configured registry tag timeout.+func (s *ContainerRegistryService) tagLookupContext(ctx context.Context) (context.Context, context.CancelFunc) {+ if s.settingsService == nil {+ return context.WithTimeout(ctx, timeouts.DefaultRegistryTags)}- return context.WithTimeout(ctx, timeouts.GetDuration(timeoutSeconds, timeouts.DefaultRegistryTags))+ return context.WithTimeout(ctx, timeouts.GetDuration(s.settingsService.GetSettingsConfig().RegistryTagTimeout.AsInt(), timeouts.DefaultRegistryTags))}-const ecrTokenTTL = 12 * time.Hour--type ecrTokenResult struct {- username string- password string-}--// GetOrRefreshECRToken returns a valid ECR auth token (username + password) for the given-// registry. If the cached token (stored encrypted in the DB) is still within its 12-hour-// validity window it is returned directly; otherwise a new token is obtained from the AWS-// ECR API, persisted back to the DB, and returned.-// Concurrent refreshes for the same registry are deduplicated via singleflight.+// GetOrRefreshECRToken returns a valid ECR auth token, reusing the encrypted cached one within its 12-hour window.+// Otherwise it fetches a new token from AWS and persists it; concurrent refreshes per registry share one singleflight call.func (s *ContainerRegistryService) GetOrRefreshECRToken(ctx context.Context, reg *ContainerRegistry) (username, password string, err error) {if endpointErr := validateECRRegistryEndpoint(reg.URL, reg.Insecure); endpointErr != nil {return "", "", endpointErr}- // Fast path: return cached token if still valid.- if reg.ECRTokenGeneratedAt != nil && time.Since(reg.ECRTokenGeneratedAt.UTC()) < ecrTokenTTL {- if reg.ECRToken != "" {- decrypted, decErr := crypto.Decrypt(reg.ECRToken)- if decErr == nil && strings.TrimSpace(decrypted) != "" {- return "AWS", decrypted, nil- }+ if reg.ECRTokenGeneratedAt != nil && time.Since(reg.ECRTokenGeneratedAt.UTC()) < ecrTokenTTL && reg.ECRToken != "" {+ if decrypted, decErr := crypto.Decrypt(reg.ECRToken); decErr == nil && strings.TrimSpace(decrypted) != "" {+ return "AWS", decrypted, nil}}- // Slow path: deduplicate concurrent refreshes for the same registry.- result, sErr, _ := s.ecrRefreshGroup.Do(reg.ID, func() (any, error) {- // Detach from the request context so a cancelled caller doesn't- // abort the shared refresh for all waiting goroutines.- refreshCtx := context.WithoutCancel(ctx)- return s.refreshECRTokenInternal(refreshCtx, reg)- })- if sErr != nil {- return "", "", sErr- }- r, ok := result.(*ecrTokenResult)- if !ok {- return "", "", errors.New("unexpected ECR token result type")- }- return r.username, r.password, nil-}--func (s *ContainerRegistryService) refreshECRTokenInternal(ctx context.Context, reg *ContainerRegistry) (*ecrTokenResult, error) {- configOptions := []func(*config.LoadOptions) error{config.WithRegion(reg.AWSRegion)}-- // Without a static access key, the default AWS credential chain applies- // (env, shared config, ECS task role, EC2 instance profile).- if strings.TrimSpace(reg.AWSAccessKeyID) != "" {- secretKey, decErr := crypto.Decrypt(reg.AWSSecretAccessKey)+ result, refreshErr, _ := s.ecrRefreshGroup.Do(reg.ID, func() (any, error) {+ // Without a static access key, the default AWS credential chain applies (env, shared config, ECS task role, EC2 instance profile).+ secretKey, decErr := crypto.Decrypt(kit.Ternary(strings.TrimSpace(reg.AWSAccessKeyID) != "", reg.AWSSecretAccessKey, ""))if decErr != nil {return nil, fmt.Errorf("failed to decrypt AWS secret key for registry %s: %w", reg.URL, decErr)}- secretKey = strings.TrimSpace(secretKey)- if secretKey == "" {+ if strings.TrimSpace(reg.AWSAccessKeyID) != "" && strings.TrimSpace(secretKey) == "" {return nil, fmt.Errorf("AWS secret access key is empty for registry %s", reg.URL)}- configOptions = append(configOptions, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(- reg.AWSAccessKeyID,- secretKey,- "",- )))- }-- // Call AWS ECR GetAuthorizationToken.- cfg, cfgErr := config.LoadDefaultConfig(ctx, configOptions...)- if cfgErr != nil {- return nil, fmt.Errorf("failed to load AWS config for registry %s: %w", reg.URL, cfgErr)- }- ecrClient := ecr.NewFromConfig(cfg)- result, ecrErr := ecrClient.GetAuthorizationToken(ctx, &ecr.GetAuthorizationTokenInput{})- if ecrErr != nil {- return nil, fmt.Errorf("failed to get ECR authorization token for registry %s: %w", reg.URL, ecrErr)- }- if len(result.AuthorizationData) == 0 || result.AuthorizationData[0].AuthorizationToken == nil {- return nil, fmt.Errorf("ECR returned empty authorization data for registry %s", reg.URL)- }+ cfg, cfgErr := config.LoadDefaultConfig(context.WithoutCancel(ctx),+ config.WithRegion(reg.AWSRegion),+ kit.Ternary(strings.TrimSpace(reg.AWSAccessKeyID) != "",+ config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(reg.AWSAccessKeyID, strings.TrimSpace(secretKey), "")),+ func(*config.LoadOptions) error { return nil },+ ),+ )+ if cfgErr != nil {+ return nil, fmt.Errorf("failed to load AWS config for registry %s: %w", reg.URL, cfgErr)+ }+ tokenOutput, ecrErr := ecr.NewFromConfig(cfg).GetAuthorizationToken(context.WithoutCancel(ctx), &ecr.GetAuthorizationTokenInput{})+ if ecrErr != nil {+ return nil, fmt.Errorf("failed to get ECR authorization token for registry %s: %w", reg.URL, ecrErr)+ }+ if len(tokenOutput.AuthorizationData) == 0 || tokenOutput.AuthorizationData[0].AuthorizationToken == nil {+ return nil, fmt.Errorf("ECR returned empty authorization data for registry %s", reg.URL)+ }- // Decode base64 token → "AWS:<password>".- decoded, decodeErr := base64.StdEncoding.DecodeString(*result.AuthorizationData[0].AuthorizationToken)- if decodeErr != nil {- return nil, fmt.Errorf("failed to decode ECR token for registry %s: %w", reg.URL, decodeErr)- }- parts := strings.SplitN(string(decoded), ":", 2)- if len(parts) != 2 || parts[1] == "" {- return nil, fmt.Errorf("unexpected ECR token format for registry %s", reg.URL)- }- ecrPassword := parts[1]+ // The token decodes to "AWS:<password>".+ decoded, decodeErr := base64.StdEncoding.DecodeString(*tokenOutput.AuthorizationData[0].AuthorizationToken)+ if decodeErr != nil {+ return nil, fmt.Errorf("failed to decode ECR token for registry %s: %w", reg.URL, decodeErr)+ }+ _, ecrPassword, found := strings.Cut(string(decoded), ":")+ if !found || ecrPassword == "" {+ return nil, fmt.Errorf("unexpected ECR token format for registry %s", reg.URL)+ }- // Persist the new token (encrypted) and generation timestamp.- encryptedToken, encErr := crypto.Encrypt(ecrPassword)- if encErr != nil {- return nil, fmt.Errorf("failed to encrypt ECR token for registry %s: %w", reg.URL, encErr)+ encryptedToken, encErr := crypto.Encrypt(ecrPassword)+ if encErr != nil {+ return nil, fmt.Errorf("failed to encrypt ECR token for registry %s: %w", reg.URL, encErr)+ }+ *reg = ContainerRegistry{ //exhaustruct:enforce+ BaseModel: reg.BaseModel,+ URL: reg.URL,+ Username: reg.Username,+ Token: reg.Token,+ Description: reg.Description,+ Insecure: reg.Insecure,+ Enabled: reg.Enabled,+ RegistryType: reg.RegistryType,+ RepositoryNames: reg.RepositoryNames,+ AWSAccessKeyID: reg.AWSAccessKeyID,+ AWSSecretAccessKey: reg.AWSSecretAccessKey,+ AWSRegion: reg.AWSRegion,+ ECRToken: encryptedToken,+ ECRTokenGeneratedAt: new(time.Now().UTC()),+ CreatedAt: reg.CreatedAt,+ UpdatedAt: reg.UpdatedAt,+ }+ if saveErr := s.db.WithContext(context.WithoutCancel(ctx)).Model(reg).Updates(map[string]any{+ "ecr_token": reg.ECRToken,+ "ecr_token_generated_at": *reg.ECRTokenGeneratedAt,+ }).Error; saveErr != nil {+ // Non-fatal: the token is still usable for this call.+ slog.WarnContext(ctx, "failed to persist ECR token to database", "registry", reg.URL, "error", saveErr)+ }+ return ecrPassword, nil+ })+ if refreshErr != nil {+ return "", "", refreshErr}- now := time.Now().UTC()- reg.ECRToken = encryptedToken- reg.ECRTokenGeneratedAt = &now- if saveErr := s.db.WithContext(ctx).Model(reg).Updates(map[string]any{- "ecr_token": encryptedToken,- "ecr_token_generated_at": now,- }).Error; saveErr != nil {- // Non-fatal: log but continue — the token is still usable for this call.- slog.WarnContext(ctx, "failed to persist ECR token to database", "registry", reg.URL, "error", saveErr)+ ecrPassword, ok := result.(string)+ if !ok {+ return "", "", errors.New("unexpected ECR token result type")}-- return &ecrTokenResult{username: "AWS", password: ecrPassword}, nil+ return "AWS", ecrPassword, nil}-const (- ociImageVersionLabel = "org.opencontainers.image.version"- versionLabelCacheTTL = 3 * time.Hour-)--// ErrNoVersionLabel is returned when the remote image config carries no-// org.opencontainers.image.version label.-var ErrNoVersionLabel = errors.New("image config has no version label")--// ImageVersionLabel resolves the org.opencontainers.image.version label from the-// remote image config for a tag or digest reference, without pulling the image.+// ImageVersionLabel resolves the org.opencontainers.image.version label from the remote image config without pulling.// Anonymous access only — this is used for Arcane's own public GHCR images.func (s *ContainerRegistryService) ImageVersionLabel(ctx context.Context, imageRef string) (string, error) {- imageRef = strings.TrimSpace(imageRef)- if imageRef == "" {+ if strings.TrimSpace(imageRef) == "" {return "", errors.New("empty image reference")}- label, found, err := s.labelCache.GetWithLoaders(imageRef, func(_ []string) (map[string]string, error) {+ label, found, err := s.labelCache.GetWithLoaders(strings.TrimSpace(imageRef), func(_ []string) (map[string]string, error) {loadCtx, cancel := context.WithTimeout(ctx, timeouts.DefaultRegistry)defer cancel()- value, loadErr := fetchImageVersionLabelInternal(loadCtx, imageRef)- if loadErr != nil {- return nil, loadErr+ parsedRef, parseErr := name.ParseReference(strings.TrimSpace(imageRef))+ if parseErr != nil {+ return nil, fmt.Errorf("invalid image reference %q: %w", strings.TrimSpace(imageRef), parseErr)+ }+ img, fetchErr := remote.Image(parsedRef, remote.WithContext(loadCtx), remote.WithPlatform(v1.Platform{OS: "linux", Architecture: runtime.GOARCH}))+ if fetchErr != nil {+ return nil, fmt.Errorf("version label fetch failed for %s: %w", strings.TrimSpace(imageRef), fetchErr)+ }+ cfg, configErr := img.ConfigFile()+ if configErr != nil {+ return nil, fmt.Errorf("version label fetch failed for %s: %w", strings.TrimSpace(imageRef), configErr)}- return map[string]string{imageRef: value}, nil+ if cfg == nil || strings.TrimSpace(cfg.Config.Labels[ociImageVersionLabel]) == "" {+ return nil, ErrNoVersionLabel+ }+ return map[string]string{strings.TrimSpace(imageRef): strings.TrimSpace(cfg.Config.Labels[ociImageVersionLabel])}, nil})if err != nil {return "", err@@ -1773,25 +1300,3 @@ func (s *ContainerRegistryService) ImageVersionLabel(ctx context.Context, imageR}return label, nil}--func fetchImageVersionLabelInternal(ctx context.Context, imageRef string) (string, error) {- parsedRef, err := name.ParseReference(imageRef)- if err != nil {- return "", fmt.Errorf("invalid image reference %q: %w", imageRef, err)- }-- img, err := remote.Image(parsedRef,- remote.WithContext(ctx),- remote.WithPlatform(v1.Platform{OS: "linux", Architecture: runtime.GOARCH}))- if err != nil {- return "", fmt.Errorf("version label fetch failed for %s: %w", imageRef, err)- }- cfg, err := img.ConfigFile()- if err != nil {- return "", fmt.Errorf("version label fetch failed for %s: %w", imageRef, err)- }- if cfg == nil || strings.TrimSpace(cfg.Config.Labels[ociImageVersionLabel]) == "" {- return "", ErrNoVersionLabel- }- return strings.TrimSpace(cfg.Config.Labels[ociImageVersionLabel]), nil-}diff --git a/backend/internal/registry/service_test.go b/backend/internal/registry/service_test.goindex 3fb746b99..886401d3f 100644--- a/backend/internal/registry/service_test.go+++ b/backend/internal/registry/service_test.go@@ -45,7 +45,7 @@ type fakeRegistryDaemonClient struct {distributionInspectFn func(ctx context.Context, imageRef string, options client.DistributionInspectOptions) (client.DistributionInspectResult, error)}-func setupContainerRegistryTestDBInternal(t *testing.T) *database.DB {+func setupContainerRegistryTestDB(t *testing.T) *database.DB {t.Helper()db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})@@ -61,7 +61,7 @@ func setupContainerRegistryTestDBInternal(t *testing.T) *database.DB {return &database.DB{DB: db}}-func createTestPullRegistryInternal(t *testing.T, db *database.DB, localUrl, username, token string) {+func createTestPullRegistry(t *testing.T, db *database.DB, localUrl, username, token string) {t.Helper()encryptedToken, err := crypto.Encrypt(token)@@ -77,7 +77,7 @@ func createTestPullRegistryInternal(t *testing.T, db *database.DB, localUrl, userequire.NoError(t, db.WithContext(t.Context()).Create(record).Error)}-func decodeRegistryAuthInternal(t *testing.T, encoded string) dockerregistry.AuthConfig {+func decodeRegistryAuth(t *testing.T, encoded string) dockerregistry.AuthConfig {t.Helper()cfg, err := authconfig.Decode(encoded)@@ -105,10 +105,10 @@ func (f *fakeRegistryDaemonClient) DistributionInspect(ctx context.Context, imagreturn f.distributionInspectFn(ctx, imageRef, options)}-const dockerHubChallengeInternal = `Bearer realm="https://auth.docker.io/token",service="registry.docker.io"`+const dockerHubChallenge = `Bearer realm="https://auth.docker.io/token",service="registry.docker.io"`-// registryPingInternal answers the /v2/ version check every registry client sends first: a bearer challenge when set, else OK.-func registryPingInternal(w http.ResponseWriter, r *http.Request, challenge string) bool {+// registryPing answers the /v2/ version check every registry client sends first: a bearer challenge when set, else OK.+func registryPing(w http.ResponseWriter, r *http.Request, challenge string) bool {if r.URL.Path != "/v2/" {return false}@@ -121,8 +121,8 @@ func registryPingInternal(w http.ResponseWriter, r *http.Request, challenge strireturn true}-// writeManifestHeadInternal answers a manifest HEAD with the headers a client needs to trust the digest without a body.-func writeManifestHeadInternal(w http.ResponseWriter, localDigest string) {+// writeManifestHead answers a manifest HEAD with the headers a client needs to trust the digest without a body.+func writeManifestHead(w http.ResponseWriter, localDigest string) {w.Header().Set("Content-Type", "application/vnd.docker.distribution.manifest.v2+json")w.Header().Set("Content-Length", "0")w.Header().Set("Docker-Content-Digest", localDigest)@@ -133,7 +133,7 @@ func newDockerHubRateLimitTestClient(t *testing.T, handler http.HandlerFunc) *htt.Helper()server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- if registryPingInternal(w, r, dockerHubChallengeInternal) {+ if registryPing(w, r, dockerHubChallenge) {return}handler(w, r)@@ -143,20 +143,23 @@ func newDockerHubRateLimitTestClient(t *testing.T, handler http.HandlerFunc) *httargetURL, err := url.Parse(server.URL)require.NoError(t, err)- httpClient := server.Client()- baseTransport := httpClient.Transport- httpClient.Transport = roundTripFunc(func(req *http.Request) (*http.Response, error) {- if req.URL.Host == "registry-1.docker.io" || req.URL.Host == "auth.docker.io" {- rewritten := req.Clone(req.Context())- rewritten.URL.Scheme = targetURL.Scheme- rewritten.URL.Host = targetURL.Host- return baseTransport.RoundTrip(rewritten)- }-- return baseTransport.RoundTrip(req)- })+ serverClient := server.Client()+ return &http.Client{+ Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {+ if req.URL.Host == "registry-1.docker.io" || req.URL.Host == "auth.docker.io" {+ rewritten := req.Clone(req.Context())+ // keep: http.Request is only copyable via Clone+ rewritten.URL.Scheme = targetURL.Scheme+ rewritten.URL.Host = targetURL.Host+ return serverClient.Transport.RoundTrip(rewritten)+ }- return httpClient+ return serverClient.Transport.RoundTrip(req)+ }),+ CheckRedirect: serverClient.CheckRedirect,+ Jar: serverClient.Jar,+ Timeout: serverClient.Timeout,+ }}func TestNewContainerRegistryService_InitializesDistributionHTTPClient(t *testing.T) {@@ -165,9 +168,9 @@ func TestNewContainerRegistryService_InitializesDistributionHTTPClient(t *testin}func TestContainerRegistryService_GetAllRegistryAuthConfigs_NormalizesHosts(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")- createTestPullRegistryInternal(t, db, "https://GHCR.IO/", "gh-user", "gh-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")+ createTestPullRegistry(t, db, "https://GHCR.IO/", "gh-user", "gh-token")svc := NewContainerRegistryService(db, nil, nil, nil)authConfigs, err := svc.GetAllRegistryAuthConfigs(t.Context())@@ -191,10 +194,10 @@ func TestContainerRegistryService_GetAllRegistryAuthConfigs_NormalizesHosts(t *t}func TestContainerRegistryService_GetAllRegistryAuthConfigs_SkipsInvalidEntries(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "https://docker.io", " ", "docker-token")- createTestPullRegistryInternal(t, db, "https://ghcr.io", "gh-user", " ")- createTestPullRegistryInternal(t, db, "https://registry.example.com", "example-user", "example-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "https://docker.io", " ", "docker-token")+ createTestPullRegistry(t, db, "https://ghcr.io", "gh-user", " ")+ createTestPullRegistry(t, db, "https://registry.example.com", "example-user", "example-token")svc := NewContainerRegistryService(db, nil, nil, nil)authConfigs, err := svc.GetAllRegistryAuthConfigs(t.Context())@@ -212,30 +215,30 @@ func TestContainerRegistryService_GetAllRegistryAuthConfigs_SkipsInvalidEntries(}func TestContainerRegistryService_GetRegistryAuthForHost_UsesDatabaseCredentials(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")svc := NewContainerRegistryService(db, nil, nil, nil)auth, err := svc.GetRegistryAuthForHost(t.Context(), "registry-1.docker.io")require.NoError(t, err)require.NotEmpty(t, auth)- cfg := decodeRegistryAuthInternal(t, auth)+ cfg := decodeRegistryAuth(t, auth)assert.Equal(t, "docker-user", cfg.Username)assert.Equal(t, "docker-token", cfg.Password)assert.Equal(t, "https://index.docker.io/v1/", cfg.ServerAddress)}func TestContainerRegistryService_GetRegistryAuthForImage_UsesHostLookup(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "https://ghcr.io", "gh-user", "gh-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "https://ghcr.io", "gh-user", "gh-token")svc := NewContainerRegistryService(db, nil, nil, nil)auth, err := svc.GetRegistryAuthForImage(t.Context(), "ghcr.io/getarcaneapp/arcane:latest")require.NoError(t, err)require.NotEmpty(t, auth)- cfg := decodeRegistryAuthInternal(t, auth)+ cfg := decodeRegistryAuth(t, auth)assert.Equal(t, "gh-user", cfg.Username)assert.Equal(t, "gh-token", cfg.Password)assert.Equal(t, "ghcr.io", cfg.ServerAddress)@@ -259,7 +262,7 @@ func TestContainerRegistryService_NilService_RegistryAuthMethodsReturnEmpty(t *t}func TestContainerRegistryService_GetRegistryPullUsage_AnonymousDockerHubLimit(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)require.NoError(t, db.WithContext(t.Context()).Create(&ContainerRegistry{URL: "docker.io",Enabled: true,@@ -268,8 +271,7 @@ func TestContainerRegistryService_GetRegistryPullUsage_AnonymousDockerHubLimit(tvar tokenCalls int- svc := NewContainerRegistryService(db, nil, kv.NewKVService(db), nil)- svc.distributionHTTPClient = newDockerHubRateLimitTestClient(t, func(w http.ResponseWriter, r *http.Request) {+ svc := NewContainerRegistryService(db, nil, kv.NewKVService(db), nil, newDockerHubRateLimitTestClient(t, func(w http.ResponseWriter, r *http.Request) {switch {case r.URL.Path == "/token":tokenCalls++@@ -277,7 +279,7 @@ func TestContainerRegistryService_GetRegistryPullUsage_AnonymousDockerHubLimit(t_, _ = w.Write([]byte(`{"token":"anonymous-token"}`))case r.Method == http.MethodHead && r.URL.Path == "/v2/ratelimitpreview/test/manifests/latest":if r.Header.Get("Authorization") != "Bearer anonymous-token" {- w.Header().Set("WWW-Authenticate", dockerHubChallengeInternal)+ w.Header().Set("WWW-Authenticate", dockerHubChallenge)w.WriteHeader(http.StatusUnauthorized)return}@@ -289,7 +291,7 @@ func TestContainerRegistryService_GetRegistryPullUsage_AnonymousDockerHubLimit(treturn}}- })+ }))result, err := svc.GetRegistryPullUsage(t.Context())require.NoError(t, err)@@ -316,20 +318,19 @@ func TestContainerRegistryService_GetRegistryPullUsage_AnonymousDockerHubLimit(t}func TestContainerRegistryService_GetRegistryPullUsage_UsesDockerHubCredential(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")- createTestPullRegistryInternal(t, db, "https://ghcr.io", "gh-user", "gh-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")+ createTestPullRegistry(t, db, "https://ghcr.io", "gh-user", "gh-token")var tokenAuth string- svc := NewContainerRegistryService(db, nil, kv.NewKVService(db), nil)- svc.distributionHTTPClient = newDockerHubRateLimitTestClient(t, func(w http.ResponseWriter, r *http.Request) {+ svc := NewContainerRegistryService(db, nil, kv.NewKVService(db), nil, newDockerHubRateLimitTestClient(t, func(w http.ResponseWriter, r *http.Request) {switch {case r.URL.Path == "/token":tokenAuth = r.Header.Get("Authorization")_, _ = w.Write([]byte(`{"token":"credential-token"}`))case r.Method == http.MethodHead && r.URL.Path == "/v2/ratelimitpreview/test/manifests/latest":if r.Header.Get("Authorization") != "Bearer credential-token" {- w.Header().Set("WWW-Authenticate", dockerHubChallengeInternal)+ w.Header().Set("WWW-Authenticate", dockerHubChallenge)w.WriteHeader(http.StatusUnauthorized)return}@@ -341,7 +342,7 @@ func TestContainerRegistryService_GetRegistryPullUsage_UsesDockerHubCredential(treturn}}- })+ }))result, err := svc.GetRegistryPullUsage(t.Context())require.NoError(t, err)@@ -359,23 +360,22 @@ func TestContainerRegistryService_GetRegistryPullUsage_UsesDockerHubCredential(t}func TestContainerRegistryService_GetRegistryPullUsage_CredentialErrorIsNonFatal(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "https://docker.io", "docker-user", "docker-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "https://docker.io", "docker-user", "docker-token")- svc := NewContainerRegistryService(db, nil, kv.NewKVService(db), nil)- svc.distributionHTTPClient = newDockerHubRateLimitTestClient(t, func(w http.ResponseWriter, r *http.Request) {+ svc := NewContainerRegistryService(db, nil, kv.NewKVService(db), nil, newDockerHubRateLimitTestClient(t, func(w http.ResponseWriter, r *http.Request) {switch {case r.URL.Path == "/token":w.WriteHeader(http.StatusUnauthorized)case r.Method == http.MethodHead && r.URL.Path == "/v2/ratelimitpreview/test/manifests/latest":- w.Header().Set("WWW-Authenticate", dockerHubChallengeInternal)+ w.Header().Set("WWW-Authenticate", dockerHubChallenge)w.WriteHeader(http.StatusUnauthorized)default:if !assert.Failf(t, "unexpected failure", "unexpected request %s %s", r.Method, r.URL.Path) {return}}- })+ }))result, err := svc.GetRegistryPullUsage(t.Context())require.NoError(t, err)@@ -388,7 +388,7 @@ func TestContainerRegistryService_GetRegistryPullUsage_CredentialErrorIsNonFatal}func TestContainerRegistryService_RecordImagePull_IncrementsObservedRegistryCount(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)require.NoError(t, db.WithContext(t.Context()).Create(&ContainerRegistry{URL: "ghcr.io",Enabled: true,@@ -408,7 +408,7 @@ func TestContainerRegistryService_RecordImagePull_IncrementsObservedRegistryCoun}func TestContainerRegistryService_CreateRegistry_RejectsUnsupportedRegistryType(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)_, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -424,7 +424,7 @@ func TestContainerRegistryService_CreateRegistry_RejectsUnsupportedRegistryType(}func TestContainerRegistryService_CreateRegistry_RejectsEmptyUsernameForGeneric(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)_, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -441,7 +441,7 @@ func TestContainerRegistryService_CreateRegistry_RejectsEmptyUsernameForGeneric(}func TestContainerRegistryService_CreateRegistry_RejectsEmptyTokenForGeneric(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)_, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -458,7 +458,7 @@ func TestContainerRegistryService_CreateRegistry_RejectsEmptyTokenForGeneric(t *}func TestContainerRegistryService_CreateRegistry_AcceptsValidGenericCredentials(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)reg, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -472,7 +472,7 @@ func TestContainerRegistryService_CreateRegistry_AcceptsValidGenericCredentials(}func TestContainerRegistryService_UpdateRegistry_RejectsBlankingUsername(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)reg, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -494,7 +494,7 @@ func TestContainerRegistryService_UpdateRegistry_RejectsBlankingUsername(t *test}func TestContainerRegistryService_UpdateRegistry_KeepsExistingTokenWhenNotProvided(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)reg, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -524,7 +524,7 @@ func TestContainerRegistryService_UpdateRegistry_RejectsTargetChangeWhenStoredTofor _, tt := range tests {t.Run(tt.name, func(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)registryRecord, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -555,7 +555,7 @@ func TestContainerRegistryService_UpdateRegistry_RejectsTargetChangeWhenStoredTo}func TestContainerRegistryService_UpdateRegistry_AllowsPathChangeOnSameHostWithoutToken(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)registryRecord, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -575,7 +575,7 @@ func TestContainerRegistryService_UpdateRegistry_AllowsPathChangeOnSameHostWitho}func TestContainerRegistryService_UpdateRegistry_AllowsTargetChangeWhenTokenIsResupplied(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)registryRecord, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -597,8 +597,8 @@ func TestContainerRegistryService_UpdateRegistry_AllowsTargetChangeWhenTokenIsReassert.Equal(t, "new-token", decryptedToken)}-func TestContainerRegistryService_UpdateRegistryRejectsECRTargetChangeWithStoredCredentialsInternal(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+func TestContainerRegistryService_UpdateRegistryRejectsECRTargetChangeWithStoredCredentials(t *testing.T) {+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)registryRecord, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -640,7 +640,7 @@ func TestContainerRegistryService_UpdateRegistryRejectsECRTargetChangeWithStored}func TestContainerRegistryService_CreateRegistry_AcceptsECRWithoutKeysForIAMRole(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)reg, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -667,7 +667,7 @@ func TestContainerRegistryService_CreateRegistry_RejectsIncompleteECRKeyPair(t *for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)_, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -686,7 +686,7 @@ func TestContainerRegistryService_CreateRegistry_RejectsIncompleteECRKeyPair(t *}func TestContainerRegistryService_UpdateRegistry_ClearingECRKeysSwitchesToIAMRole(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)reg, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -698,10 +698,10 @@ func TestContainerRegistryService_UpdateRegistry_ClearingECRKeysSwitchesToIAMRol})require.NoError(t, err)- cachedAt := time.Now().UTC()- reg.ECRToken = "cached-token"- reg.ECRTokenGeneratedAt = &cachedAt- require.NoError(t, db.WithContext(t.Context()).Save(reg).Error)+ require.NoError(t, db.WithContext(t.Context()).Model(reg).Updates(ContainerRegistry{+ ECRToken: "cached-token",+ ECRTokenGeneratedAt: new(time.Now().UTC()),+ }).Error)updated, err := svc.UpdateRegistry(t.Context(), reg.ID, containerregistry.UpdateContainerRegistryRequest{AWSAccessKeyID: new(""),@@ -714,7 +714,7 @@ func TestContainerRegistryService_UpdateRegistry_ClearingECRKeysSwitchesToIAMRol}func TestContainerRegistryService_SyncRegistries_ClearsECRSecretWhenManagerSendsNoKeys(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)existing, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -746,7 +746,7 @@ func TestContainerRegistryService_SyncRegistries_ClearsECRSecretWhenManagerSends}func TestContainerRegistryService_UpdateRegistry_RejectsChangingRegistryType(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)reg, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -768,7 +768,7 @@ func TestContainerRegistryService_UpdateRegistry_RejectsChangingRegistryType(t *}func TestContainerRegistryService_UpdateRegistry_AllowsSameRegistryType(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)reg, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -788,8 +788,8 @@ func TestContainerRegistryService_UpdateRegistry_AllowsSameRegistryType(t *testi}func TestContainerRegistryService_SyncRegistries_ClearsGenericTokenWhenManagerSendsEmptyValue(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "https://registry.example.com", "registry-user", "old-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "https://registry.example.com", "registry-user", "old-token")var existing ContainerRegistryrequire.NoError(t, db.WithContext(t.Context()).First(&existing).Error)@@ -929,8 +929,8 @@ func TestContainerRegistryService_GetImageDigest_HonorsCallerCancellation(t *tes}func TestContainerRegistryService_InspectImageDigest_UsesStoredDockerHubCredentialsOnFirstAttempt(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")wantDigest := digest.FromString("stored-credentials").String()var calls int@@ -940,7 +940,7 @@ func TestContainerRegistryService_InspectImageDigest_UsesStoredDockerHubCredenticalls++assert.Equal(t, "docker.io/library/nginx:latest", imageRef)- authCfg := decodeRegistryAuthInternal(t, options.EncodedRegistryAuth)+ authCfg := decodeRegistryAuth(t, options.EncodedRegistryAuth)assert.Equal(t, "docker-user", authCfg.Username)assert.Equal(t, "docker-token", authCfg.Password)assert.Equal(t, "https://index.docker.io/v1/", authCfg.ServerAddress)@@ -964,8 +964,8 @@ func TestContainerRegistryService_InspectImageDigest_UsesStoredDockerHubCredenti}func TestContainerRegistryService_InspectImageDigest_UsesStoredDockerHubCredentialsForRegistryImage(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "https://index.docker.io/v1/", "docker-user", "docker-token")wantDigest := digest.FromString("stored-credentials-rate-limit").String()var calls int@@ -975,7 +975,7 @@ func TestContainerRegistryService_InspectImageDigest_UsesStoredDockerHubCredenticalls++assert.Equal(t, "docker.io/library/registry:3", imageRef)- authCfg := decodeRegistryAuthInternal(t, options.EncodedRegistryAuth)+ authCfg := decodeRegistryAuth(t, options.EncodedRegistryAuth)assert.Equal(t, "docker-user", authCfg.Username)assert.Equal(t, "docker-token", authCfg.Password)assert.Equal(t, "https://index.docker.io/v1/", authCfg.ServerAddress)@@ -999,8 +999,8 @@ func TestContainerRegistryService_InspectImageDigest_UsesStoredDockerHubCredenti}func TestContainerRegistryService_InspectImageDigest_UsesStoredCredentialsForNonDockerHubRegistry(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "ghcr.io", "ghcr-user", "ghcr-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "ghcr.io", "ghcr-user", "ghcr-token")wantDigest := digest.FromString("ghcr-stored-credentials").String()var calls int@@ -1010,7 +1010,7 @@ func TestContainerRegistryService_InspectImageDigest_UsesStoredCredentialsForNoncalls++assert.Equal(t, "ghcr.io/getarcaneapp/agent:latest", imageRef)- authCfg := decodeRegistryAuthInternal(t, options.EncodedRegistryAuth)+ authCfg := decodeRegistryAuth(t, options.EncodedRegistryAuth)assert.Equal(t, "ghcr-user", authCfg.Username)assert.Equal(t, "ghcr-token", authCfg.Password)@@ -1035,8 +1035,8 @@ func TestContainerRegistryService_InspectImageDigest_UsesStoredCredentialsForNon// A rate limited anonymous request is not an unauthorized one, so before this was// fixed a stored ghcr.io credential was never reached. See issue #2635.func TestContainerRegistryService_InspectImageDigest_UsesStoredCredentialsInsteadOfRateLimitedAnonymous(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "ghcr.io", "ghcr-user", "ghcr-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "ghcr.io", "ghcr-user", "ghcr-token")wantDigest := digest.FromString("ghcr-rate-limit").String()var anonymousCalls int@@ -1068,8 +1068,8 @@ func TestContainerRegistryService_InspectImageDigest_UsesStoredCredentialsInstea}func TestContainerRegistryService_InspectImageDigest_FallsBackToAnonymousWhenStoredCredentialRejected(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "ghcr.io", "ghcr-user", "expired-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "ghcr.io", "ghcr-user", "expired-token")wantDigest := digest.FromString("ghcr-anonymous-fallback").String()var anonymousCalls int@@ -1100,8 +1100,8 @@ func TestContainerRegistryService_InspectImageDigest_FallsBackToAnonymousWhenSto// A rate-limited credential must not fall back to anonymous: both paths share the per-registry quota.func TestContainerRegistryService_InspectImageDigest_DoesNotFallBackToAnonymousOnCredentialRateLimit(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "ghcr.io", "ghcr-user", "ghcr-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "ghcr.io", "ghcr-user", "ghcr-token")var anonymousCalls intsvc := NewContainerRegistryService(db, func(context.Context) (RegistryDaemonClient, error) {@@ -1127,8 +1127,8 @@ func TestContainerRegistryService_InspectImageDigest_DoesNotFallBackToAnonymousO// The direct-HTTP fallback must not spend an anonymous request when a stored credential resolves the digest.func TestContainerRegistryService_InspectImageDigest_FallbackUsesStoredCredentialsBeforeAnonymous(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "ghcr.io", "ghcr-user", "ghcr-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "ghcr.io", "ghcr-user", "ghcr-token")wantDigest := digest.FromString("fallback-stored-credentials").String()var anonymousCalls int@@ -1138,8 +1138,7 @@ func TestContainerRegistryService_InspectImageDigest_FallbackUsesStoredCredentiareturn client.DistributionInspectResult{}, errors.New("Error response from daemon: Not Found")},}, nil- }, nil, nil)- svc.distributionHTTPClient = &http.Client{+ }, nil, nil, &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {// The version check never carries credentials; only manifest requests count.if req.URL.Path == "/v2/" {@@ -1160,7 +1159,7 @@ func TestContainerRegistryService_InspectImageDigest_FallbackUsesStoredCredentiaBody: http.NoBody,}, nil}),- }+ })result, err := svc.InspectImageDigest(t.Context(), "ghcr.io/getarcaneapp/agent:latest", nil)require.NoError(t, err)@@ -1175,11 +1174,11 @@ func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionNowantDigest := digest.FromString("fallback-not-found").String()server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- if registryPingInternal(w, r, "") {+ if registryPing(w, r, "") {return}if r.URL.Path == "/v2/team/app/manifests/1.2.3" {- writeManifestHeadInternal(w, wantDigest)+ writeManifestHead(w, wantDigest)return}http.NotFound(w, r)@@ -1199,8 +1198,7 @@ func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionNoreturn client.DistributionInspectResult{}, errors.New("Error response from daemon: Not Found")},}, nil- }, nil, nil)- svc.distributionHTTPClient = server.Client()+ }, nil, nil, server.Client())result, err := svc.InspectImageDigest(t.Context(), serverURL.Host+"/team/app:1.2.3", nil)require.NoError(t, err)@@ -1214,11 +1212,11 @@ func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionFowantDigest := digest.FromString("fallback-forbidden").String()server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- if registryPingInternal(w, r, "") {+ if registryPing(w, r, "") {return}if r.URL.Path == "/v2/team/app/manifests/1.2.3" {- writeManifestHeadInternal(w, wantDigest)+ writeManifestHead(w, wantDigest)return}http.NotFound(w, r)@@ -1239,8 +1237,7 @@ func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionFo"ules. </body></html>")},}, nil- }, nil, nil)- svc.distributionHTTPClient = server.Client()+ }, nil, nil, server.Client())result, err := svc.InspectImageDigest(t.Context(), serverURL.Host+"/team/app:1.2.3", nil)require.NoError(t, err)@@ -1251,14 +1248,14 @@ func TestContainerRegistryService_InspectImageDigest_FallsBackWhenDistributionFo}func TestContainerRegistryService_InspectImageDigest_RetriesStoredCredentialsAfterRegistryAuth403(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)wantDigest := digest.FromString("stored-credential-fallback").String()var authHeaders []stringvar tokenURL stringhandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {challenge := `Bearer realm="` + tokenURL + `",service="registry.example.com"`- if registryPingInternal(w, r, challenge) {+ if registryPing(w, r, challenge) {return}switch r.URL.Path {@@ -1268,7 +1265,7 @@ func TestContainerRegistryService_InspectImageDigest_RetriesStoredCredentialsAftcase "Bearer credential-token":w.WriteHeader(http.StatusForbidden)case "Bearer anonymous-token":- writeManifestHeadInternal(w, wantDigest)+ writeManifestHead(w, wantDigest)default:w.Header().Set("WWW-Authenticate", challenge)w.WriteHeader(http.StatusUnauthorized)@@ -1307,7 +1304,7 @@ func TestContainerRegistryService_InspectImageDigest_RetriesStoredCredentialsAftserverURL, err := url.Parse(server.URL)require.NoError(t, err)- createTestPullRegistryInternal(t, db, server.URL, "stored-user", "stored-token")+ createTestPullRegistry(t, db, server.URL, "stored-user", "stored-token")svc := NewContainerRegistryService(db, func(context.Context) (RegistryDaemonClient, error) {return &fakeRegistryDaemonClient{@@ -1315,8 +1312,7 @@ func TestContainerRegistryService_InspectImageDigest_RetriesStoredCredentialsAftreturn client.DistributionInspectResult{}, errors.New("Error response from daemon: Not Found")},}, nil- }, nil, nil)- svc.distributionHTTPClient = server.Client()+ }, nil, nil, server.Client())result, err := svc.InspectImageDigest(t.Context(), serverURL.Host+"/team/app:1.2.3", nil)require.NoError(t, err)@@ -1357,12 +1353,11 @@ func TestContainerRegistryService_InspectImageDigest_PreservesDaemonAndFallbackEreturn client.DistributionInspectResult{}, daemonErr},}, nil- }, nil, nil)- svc.distributionHTTPClient = &http.Client{+ }, nil, nil, &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {return nil, fallbackErr}),- }+ })result, err := svc.InspectImageDigest(t.Context(), "registry.example.com/team/app:1.2.3", nil)require.Error(t, err)@@ -1372,7 +1367,7 @@ func TestContainerRegistryService_InspectImageDigest_PreservesDaemonAndFallbackE}func TestContainerRegistryService_InspectImageDigest_PreservesAnonymousUnauthorizedWhenCredentialLookupFails(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)sqlDB, err := db.DB.DB()require.NoError(t, err)require.NoError(t, sqlDB.Close())@@ -1380,7 +1375,7 @@ func TestContainerRegistryService_InspectImageDigest_PreservesAnonymousUnauthorivar tokenURL stringserver := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {challenge := `Bearer realm="` + tokenURL + `",service="registry.example.com"`- if registryPingInternal(w, r, challenge) {+ if registryPing(w, r, challenge) {return}switch r.URL.Path {@@ -1409,8 +1404,7 @@ func TestContainerRegistryService_InspectImageDigest_PreservesAnonymousUnauthorireturn client.DistributionInspectResult{}, errors.New("Error response from daemon: Not Found")},}, nil- }, nil, nil)- svc.distributionHTTPClient = server.Client()+ }, nil, nil, server.Client())result, err := svc.InspectImageDigest(t.Context(), serverURL.Host+"/team/app:1.2.3", nil)require.Error(t, err)@@ -1445,7 +1439,7 @@ func fetchRegistry(t *testing.T, db *database.DB, id string) ContainerRegistry {}func TestContainerRegistryService_CreateRegistry_NormalizesAndPersistsRepositoryNames(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)// Entries are trimmed, empties dropped and duplicates removed while@@ -1456,7 +1450,7 @@ func TestContainerRegistryService_CreateRegistry_NormalizesAndPersistsRepository}func TestContainerRegistryService_CreateRegistry_RejectsInvalidRepositoryName(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)_, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{@@ -1470,7 +1464,7 @@ func TestContainerRegistryService_CreateRegistry_RejectsInvalidRepositoryName(t}func TestContainerRegistryService_UpdateRegistry_RepositoryNamesPointerSemantics(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)+ db := setupContainerRegistryTestDB(t)svc := NewContainerRegistryService(db, nil, nil, nil)registryRecord := createRegistryWithRepositoryNames(t, svc, "team", "team/platform")@@ -1491,7 +1485,7 @@ func TestContainerRegistryService_UpdateRegistry_RepositoryNamesPointerSemanticsassert.Empty(t, fetchRegistry(t, db, registryRecord.ID).RepositoryNames)}-func TestListImageTagsCredentialsInternal(t *testing.T) {+func TestListImageTagsCredentials(t *testing.T) {for _, tt := range []struct {name stringexternal []containerregistry.Credential@@ -1514,8 +1508,8 @@ func TestListImageTagsCredentialsInternal(t *testing.T) {{name: "disabled external ignored", external: []containerregistry.Credential{{URL: "docker.io", Username: "external-user", Token: "external-token", Enabled: false}}},} {t.Run(tt.name, func(t *testing.T) {- db := setupContainerRegistryTestDBInternal(t)- createTestPullRegistryInternal(t, db, "https://index.docker.io/v1/", "stored-user", "stored-token")+ db := setupContainerRegistryTestDB(t)+ createTestPullRegistry(t, db, "https://index.docker.io/v1/", "stored-user", "stored-token")calls := 0httpClient := &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {assert.Equal(t, "registry-1.docker.io", req.URL.Host)@@ -1538,7 +1532,7 @@ func TestListImageTagsCredentialsInternal(t *testing.T) {}}-func TestListImageTagsDoesNotFallBackOnRateLimitInternal(t *testing.T) {+func TestListImageTagsDoesNotFallBackOnRateLimit(t *testing.T) {// The registry client retries 429s itself, so count credentialed and anonymous listings rather than requests.credentialed, anonymous := 0, 0svc := NewContainerRegistryService(nil, nil, nil, nil, &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {@@ -1568,7 +1562,7 @@ func TestListImageTagsDoesNotFallBackOnRateLimitInternal(t *testing.T) {assert.Zero(t, anonymous, "a rate-limited credential must not spend the anonymous quota")}-func TestListImageTagsCancellationInternal(t *testing.T) {+func TestListImageTagsCancellation(t *testing.T) {svc := NewContainerRegistryService(nil, nil, nil, nil)ctx, cancel := context.WithCancel(t.Context())cancel()@@ -1576,7 +1570,7 @@ func TestListImageTagsCancellationInternal(t *testing.T) {require.ErrorIs(t, err, context.Canceled, "error = %v", err)}-func TestListImageTagsRejectedCredentialFallbackInternal(t *testing.T) {+func TestListImageTagsRejectedCredentialFallback(t *testing.T) {basic := "Basic " + base64.StdEncoding.EncodeToString([]byte("user:token"))for _, tt := range []struct {name string@@ -1591,22 +1585,22 @@ func TestListImageTagsRejectedCredentialFallbackInternal(t *testing.T) {// The registry challenges at /v2/ and its token endpoint rejects the stored credential but serves anonymous tokens.var tokenAuth []stringsvc := NewContainerRegistryService(nil, nil, nil, nil, &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {- response := &http.Response{StatusCode: http.StatusOK, Body: http.NoBody, Header: http.Header{}}switch req.URL.Path {case "/v2/":- response.StatusCode = http.StatusUnauthorized- response.Header.Set("WWW-Authenticate", `Bearer realm="https://auth.test/token"`)+ return &http.Response{+ StatusCode: http.StatusUnauthorized,+ Header: http.Header{"Www-Authenticate": []string{`Bearer realm="https://auth.test/token"`}},+ Body: http.NoBody,+ }, nilcase "/token":tokenAuth = append(tokenAuth, req.Header.Get("Authorization"))if req.Header.Get("Authorization") != "" {- response.StatusCode = http.StatusUnauthorized- } else {- response.Body = io.NopCloser(strings.NewReader(`{"token":"anonymous-token"}`))+ return &http.Response{StatusCode: http.StatusUnauthorized, Header: http.Header{}, Body: http.NoBody}, nil}+ return &http.Response{StatusCode: http.StatusOK, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(`{"token":"anonymous-token"}`))}, nildefault:- response.Body = io.NopCloser(strings.NewReader(`{"tags":["1.0.1"]}`))+ return &http.Response{StatusCode: http.StatusOK, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(`{"tags":["1.0.1"]}`))}, nil}- return response, nil})})tags, err := svc.ListImageTags(t.Context(), tt.host+"/team/app:1.0.0", []containerregistry.Credential{{URL: tt.host, Username: "user", Token: "token", Enabled: true}})if tt.wantError {@@ -1621,9 +1615,9 @@ func TestListImageTagsRejectedCredentialFallbackInternal(t *testing.T) {}}-// newLabelTestRegistryInternal starts an in-memory OCI registry and returns its+// newLabelTestRegistry starts an in-memory OCI registry and returns its// host as "localhost:PORT" so go-containerregistry resolves it over plain HTTP.-func newLabelTestRegistryInternal(t *testing.T) string {+func newLabelTestRegistry(t *testing.T) string {t.Helper()server := httptest.NewServer(registry.New(registry.Logger(log.New(io.Discard, "", 0))))t.Cleanup(server.Close)@@ -1632,18 +1626,14 @@ func newLabelTestRegistryInternal(t *testing.T) string {return "localhost:" + serverURL.Port()}-func pushLabeledImageInternal(t *testing.T, imageRef string, imageLabels map[string]string) v1.Image {+func pushLabeledImage(t *testing.T, imageRef string, imageLabels map[string]string) v1.Image {t.Helper()img, err := random.Image(64, 1)require.NoError(t, err)if imageLabels != nil {- cfg, configFileErr := img.ConfigFile()- require.NoError(t, configFileErr)- cfg = cfg.DeepCopy()- cfg.Config.Labels = imageLabels- img, configFileErr = mutate.ConfigFile(img, cfg)- require.NoError(t, configFileErr)+ img, err = mutate.Config(img, v1.Config{Labels: imageLabels})+ require.NoError(t, err)}ref, err := name.ParseReference(imageRef)@@ -1652,10 +1642,10 @@ func pushLabeledImageInternal(t *testing.T, imageRef string, imageLabels map[strreturn img}-func TestContainerRegistryService_ImageVersionLabelInternal(t *testing.T) {- host := newLabelTestRegistryInternal(t)+func TestContainerRegistryService_ImageVersionLabel(t *testing.T) {+ host := newLabelTestRegistry(t)imageRef := host + "/getarcaneapp/arcane:next"- img := pushLabeledImageInternal(t, imageRef, map[string]string{+ img := pushLabeledImage(t, imageRef, map[string]string{ociImageVersionLabel: "v2.8.0-next.66",})@@ -1673,18 +1663,18 @@ func TestContainerRegistryService_ImageVersionLabelInternal(t *testing.T) {assert.Equal(t, "v2.8.0-next.66", label)}-func TestContainerRegistryService_ImageVersionLabelMissingLabelInternal(t *testing.T) {- host := newLabelTestRegistryInternal(t)+func TestContainerRegistryService_ImageVersionLabelMissingLabel(t *testing.T) {+ host := newLabelTestRegistry(t)imageRef := host + "/getarcaneapp/arcane:next"- pushLabeledImageInternal(t, imageRef, nil)+ pushLabeledImage(t, imageRef, nil)svc := NewContainerRegistryService(nil, nil, nil, nil)_, err := svc.ImageVersionLabel(t.Context(), imageRef)assert.ErrorIs(t, err, ErrNoVersionLabel)}-func TestContainerRegistryService_ImageVersionLabelErrorNotCachedInternal(t *testing.T) {- host := newLabelTestRegistryInternal(t)+func TestContainerRegistryService_ImageVersionLabelErrorNotCached(t *testing.T) {+ host := newLabelTestRegistry(t)imageRef := host + "/getarcaneapp/arcane:next"svc := NewContainerRegistryService(nil, nil, nil, nil)@@ -1694,7 +1684,7 @@ func TestContainerRegistryService_ImageVersionLabelErrorNotCachedInternal(t *tesrequire.Error(t, err)// ...and the failure must not be cached: after the push the same ref resolves.- pushLabeledImageInternal(t, imageRef, map[string]string{+ pushLabeledImage(t, imageRef, map[string]string{ociImageVersionLabel: "v2.8.0-next.67",})label, err := svc.ImageVersionLabel(t.Context(), imageRef)@@ -1715,14 +1705,14 @@ func TestClassifyDigestError(t *testing.T) {"manifest unknown": {errors.New("MANIFEST_UNKNOWN: manifest unknown"), common.ErrNotFound},}for name, tc := range cases {- require.ErrorIs(t, classifyDigestErrorInternal(tc.err), tc.kind, name)+ require.ErrorIs(t, classifyDigestError(tc.err), tc.kind, name)}plain := errors.New("x509: certificate signed by unknown authority")- classified := classifyDigestErrorInternal(plain)+ classified := classifyDigestError(plain)require.Equal(t, plain, classified)require.NotErrorIs(t, classified, common.ErrUnavailable)// A host that does not resolve never comes back, so it must not be retried like an outage.unknownHost := &url.Error{Op: "Get", URL: "https://invalid-registry.example.com/v2/", Err: &net.DNSError{Err: "no such host", Name: "invalid-registry.example.com", IsNotFound: true}}- require.NotErrorIs(t, classifyDigestErrorInternal(unknownHost), common.ErrUnavailable)+ require.NotErrorIs(t, classifyDigestError(unknownHost), common.ErrUnavailable)}