Browse code

Default the auth config domain to the target image domain

When server address is not provided with the auth configuration,
use the domain from the image provided with the auth.

Signed-off-by: Derek McGowan <derek@mcg.dev>

Derek McGowan authored on 2023/11/07 10:02:18
Showing 3 changed files
... ...
@@ -67,7 +67,7 @@ func (i *ImageService) pullTag(ctx context.Context, ref reference.NamedTagged, p
67 67
 		opts = append(opts, containerd.WithPlatform(platforms.Format(*platform)))
68 68
 	}
69 69
 
70
-	resolver, _ := i.newResolverFromAuthConfig(ctx, authConfig)
70
+	resolver, _ := i.newResolverFromAuthConfig(ctx, authConfig, ref)
71 71
 	opts = append(opts, containerd.WithResolver(resolver))
72 72
 
73 73
 	old, err := i.resolveDescriptor(ctx, ref.String())
... ...
@@ -102,7 +102,7 @@ func (i *ImageService) pushRef(ctx context.Context, targetRef reference.Named, m
102 102
 	target := img.Target
103 103
 	store := i.client.ContentStore()
104 104
 
105
-	resolver, tracker := i.newResolverFromAuthConfig(ctx, authConfig)
105
+	resolver, tracker := i.newResolverFromAuthConfig(ctx, authConfig, targetRef)
106 106
 	pp := pushProgress{Tracker: tracker}
107 107
 	jobsQueue := newJobs()
108 108
 	finishProgress := jobsQueue.showProgress(ctx, out, combinedProgress([]progressUpdater{
... ...
@@ -11,16 +11,17 @@ import (
11 11
 	"github.com/containerd/containerd/remotes/docker"
12 12
 	"github.com/containerd/containerd/version"
13 13
 	"github.com/containerd/log"
14
+	"github.com/distribution/reference"
14 15
 	registrytypes "github.com/docker/docker/api/types/registry"
15 16
 	"github.com/docker/docker/dockerversion"
16 17
 	"github.com/docker/docker/pkg/useragent"
17 18
 	"github.com/docker/docker/registry"
18 19
 )
19 20
 
20
-func (i *ImageService) newResolverFromAuthConfig(ctx context.Context, authConfig *registrytypes.AuthConfig) (remotes.Resolver, docker.StatusTracker) {
21
+func (i *ImageService) newResolverFromAuthConfig(ctx context.Context, authConfig *registrytypes.AuthConfig, ref reference.Named) (remotes.Resolver, docker.StatusTracker) {
21 22
 	tracker := docker.NewInMemoryTracker()
22 23
 
23
-	hosts := hostsWrapper(i.registryHosts, authConfig, i.registryService)
24
+	hosts := hostsWrapper(i.registryHosts, authConfig, ref, i.registryService)
24 25
 	headers := http.Header{}
25 26
 	headers.Set("User-Agent", dockerversion.DockerUserAgent(ctx, useragent.VersionInfo{Name: "containerd-client", Version: version.Version}, useragent.VersionInfo{Name: "storage-driver", Version: i.snapshotter}))
26 27
 
... ...
@@ -31,10 +32,10 @@ func (i *ImageService) newResolverFromAuthConfig(ctx context.Context, authConfig
31 31
 	}), tracker
32 32
 }
33 33
 
34
-func hostsWrapper(hostsFn docker.RegistryHosts, optAuthConfig *registrytypes.AuthConfig, regService registryResolver) docker.RegistryHosts {
34
+func hostsWrapper(hostsFn docker.RegistryHosts, optAuthConfig *registrytypes.AuthConfig, ref reference.Named, regService registryResolver) docker.RegistryHosts {
35 35
 	var authorizer docker.Authorizer
36 36
 	if optAuthConfig != nil {
37
-		authorizer = authorizerFromAuthConfig(*optAuthConfig)
37
+		authorizer = authorizerFromAuthConfig(*optAuthConfig, ref)
38 38
 	}
39 39
 
40 40
 	return func(n string) ([]docker.RegistryHost, error) {
... ...
@@ -56,9 +57,12 @@ func hostsWrapper(hostsFn docker.RegistryHosts, optAuthConfig *registrytypes.Aut
56 56
 	}
57 57
 }
58 58
 
59
-func authorizerFromAuthConfig(authConfig registrytypes.AuthConfig) docker.Authorizer {
59
+func authorizerFromAuthConfig(authConfig registrytypes.AuthConfig, ref reference.Named) docker.Authorizer {
60 60
 	cfgHost := registry.ConvertToHostname(authConfig.ServerAddress)
61
-	if cfgHost == "" || cfgHost == registry.IndexHostname {
61
+	if cfgHost == "" {
62
+		cfgHost = reference.Domain(ref)
63
+	}
64
+	if cfgHost == registry.IndexHostname || cfgHost == registry.IndexName {
62 65
 		cfgHost = registry.DefaultRegistryHost
63 66
 	}
64 67