Browse code

Introduce a client-side version of resolveAuthConfig

This is similar to the version in the registry package, but uses the
daemon's default index (as opposed to the default for the client's
platform) if using the "official index".

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>

Aaron Lehmann authored on 2016/02/04 03:55:33
Showing 6 changed files
... ...
@@ -40,8 +40,8 @@ func (cli *DockerCli) pullImageCustomOut(image string, out io.Writer) error {
40 40
 		return err
41 41
 	}
42 42
 
43
-	// Resolve the Auth config relevant for this server
44
-	encodedAuth, err := cli.encodeRegistryAuth(repoInfo.Index)
43
+	authConfig := cli.resolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
44
+	encodedAuth, err := encodeAuthToBase64(authConfig)
45 45
 	if err != nil {
46 46
 		return err
47 47
 	}
... ...
@@ -54,7 +54,7 @@ func (cli *DockerCli) CmdPull(args ...string) error {
54 54
 		return err
55 55
 	}
56 56
 
57
-	authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
57
+	authConfig := cli.resolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
58 58
 	requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
59 59
 
60 60
 	if isTrusted() && !ref.HasDigest() {
... ...
@@ -42,7 +42,7 @@ func (cli *DockerCli) CmdPush(args ...string) error {
42 42
 		return err
43 43
 	}
44 44
 	// Resolve the Auth config relevant for this server
45
-	authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
45
+	authConfig := cli.resolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
46 46
 
47 47
 	requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
48 48
 	if isTrusted() {
... ...
@@ -36,7 +36,7 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
36 36
 		return err
37 37
 	}
38 38
 
39
-	authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, indexInfo)
39
+	authConfig := cli.resolveAuthConfig(cli.configFile.AuthConfigs, indexInfo)
40 40
 	requestPrivilege := cli.registryAuthenticationPrivilegedFunc(indexInfo, "search")
41 41
 
42 42
 	encodedAuth, err := encodeAuthToBase64(authConfig)
... ...
@@ -234,7 +234,7 @@ func (cli *DockerCli) trustedReference(ref reference.NamedTagged) (reference.Can
234 234
 	}
235 235
 
236 236
 	// Resolve the Auth config relevant for this server
237
-	authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
237
+	authConfig := cli.resolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
238 238
 
239 239
 	notaryRepo, err := cli.getNotaryRepository(repoInfo, authConfig)
240 240
 	if err != nil {
... ...
@@ -7,6 +7,7 @@ import (
7 7
 	"os"
8 8
 	gosignal "os/signal"
9 9
 	"runtime"
10
+	"strings"
10 11
 	"time"
11 12
 
12 13
 	"github.com/Sirupsen/logrus"
... ...
@@ -154,3 +155,42 @@ func (cli *DockerCli) getTtySize() (int, int) {
154 154
 	}
155 155
 	return int(ws.Height), int(ws.Width)
156 156
 }
157
+
158
+// resolveAuthConfig is like registry.ResolveAuthConfig, but if using the
159
+// default index, it uses the default index name for the daemon's platform,
160
+// not the client's platform.
161
+func (cli *DockerCli) resolveAuthConfig(authConfigs map[string]types.AuthConfig, index *registrytypes.IndexInfo) types.AuthConfig {
162
+	configKey := index.Name
163
+	if index.Official {
164
+		configKey = cli.electAuthServer()
165
+	}
166
+
167
+	// First try the happy case
168
+	if c, found := authConfigs[configKey]; found || index.Official {
169
+		return c
170
+	}
171
+
172
+	convertToHostname := func(url string) string {
173
+		stripped := url
174
+		if strings.HasPrefix(url, "http://") {
175
+			stripped = strings.Replace(url, "http://", "", 1)
176
+		} else if strings.HasPrefix(url, "https://") {
177
+			stripped = strings.Replace(url, "https://", "", 1)
178
+		}
179
+
180
+		nameParts := strings.SplitN(stripped, "/", 2)
181
+
182
+		return nameParts[0]
183
+	}
184
+
185
+	// Maybe they have a legacy config file, we will iterate the keys converting
186
+	// them to the new format and testing
187
+	for registry, ac := range authConfigs {
188
+		if configKey == convertToHostname(registry) {
189
+			return ac
190
+		}
191
+	}
192
+
193
+	// When all else fails, return an empty auth config
194
+	return types.AuthConfig{}
195
+}