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,7 +40,7 @@ func (cli *DockerCli) pullImageCustomOut(image string, out io.Writer) error {
40 40
 		return err
41 41
 	}
42 42
 
43
-	authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
43
+	authConfig := cli.resolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
44 44
 	encodedAuth, err := encodeAuthToBase64(authConfig)
45 45
 	if err != nil {
46 46
 		return err
... ...
@@ -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)
... ...
@@ -235,7 +235,7 @@ func (cli *DockerCli) trustedReference(ref reference.NamedTagged) (reference.Can
235 235
 	}
236 236
 
237 237
 	// Resolve the Auth config relevant for this server
238
-	authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
238
+	authConfig := cli.resolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
239 239
 
240 240
 	notaryRepo, err := cli.getNotaryRepository(repoInfo, authConfig)
241 241
 	if err != nil {
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	gosignal "os/signal"
11 11
 	"path/filepath"
12 12
 	"runtime"
13
+	"strings"
13 14
 	"time"
14 15
 
15 16
 	"github.com/Sirupsen/logrus"
... ...
@@ -176,3 +177,42 @@ func copyToFile(outfile string, r io.Reader) error {
176 176
 
177 177
 	return nil
178 178
 }
179
+
180
+// resolveAuthConfig is like registry.ResolveAuthConfig, but if using the
181
+// default index, it uses the default index name for the daemon's platform,
182
+// not the client's platform.
183
+func (cli *DockerCli) resolveAuthConfig(authConfigs map[string]types.AuthConfig, index *registrytypes.IndexInfo) types.AuthConfig {
184
+	configKey := index.Name
185
+	if index.Official {
186
+		configKey = cli.electAuthServer()
187
+	}
188
+
189
+	// First try the happy case
190
+	if c, found := authConfigs[configKey]; found || index.Official {
191
+		return c
192
+	}
193
+
194
+	convertToHostname := func(url string) string {
195
+		stripped := url
196
+		if strings.HasPrefix(url, "http://") {
197
+			stripped = strings.Replace(url, "http://", "", 1)
198
+		} else if strings.HasPrefix(url, "https://") {
199
+			stripped = strings.Replace(url, "https://", "", 1)
200
+		}
201
+
202
+		nameParts := strings.SplitN(stripped, "/", 2)
203
+
204
+		return nameParts[0]
205
+	}
206
+
207
+	// Maybe they have a legacy config file, we will iterate the keys converting
208
+	// them to the new format and testing
209
+	for registry, ac := range authConfigs {
210
+		if configKey == convertToHostname(registry) {
211
+			return ac
212
+		}
213
+	}
214
+
215
+	// When all else fails, return an empty auth config
216
+	return types.AuthConfig{}
217
+}