Refactor ResolveAuthConfig to remove the builder dependency on cli code
| ... | ... |
@@ -65,7 +65,7 @@ func (cli *DockerCli) CmdPull(args ...string) error {
|
| 65 | 65 |
return err |
| 66 | 66 |
} |
| 67 | 67 |
|
| 68 |
- authConfig := registry.ResolveAuthConfig(cli.configFile, repoInfo.Index) |
|
| 68 |
+ authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index) |
|
| 69 | 69 |
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "pull") |
| 70 | 70 |
|
| 71 | 71 |
if isTrusted() && !ref.HasDigest() {
|
| ... | ... |
@@ -44,7 +44,7 @@ func (cli *DockerCli) CmdPush(args ...string) error {
|
| 44 | 44 |
return err |
| 45 | 45 |
} |
| 46 | 46 |
// Resolve the Auth config relevant for this server |
| 47 |
- authConfig := registry.ResolveAuthConfig(cli.configFile, repoInfo.Index) |
|
| 47 |
+ authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index) |
|
| 48 | 48 |
// If we're not using a custom registry, we know the restrictions |
| 49 | 49 |
// applied to repository names and can warn the user in advance. |
| 50 | 50 |
// Custom repositories can have different rules, and we must also |
| ... | ... |
@@ -35,7 +35,7 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
|
| 35 | 35 |
return err |
| 36 | 36 |
} |
| 37 | 37 |
|
| 38 |
- authConfig := registry.ResolveAuthConfig(cli.configFile, indexInfo) |
|
| 38 |
+ authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, indexInfo) |
|
| 39 | 39 |
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(indexInfo, "search") |
| 40 | 40 |
|
| 41 | 41 |
encodedAuth, err := authConfig.EncodeToBase64() |
| ... | ... |
@@ -229,7 +229,7 @@ func (cli *DockerCli) trustedReference(ref reference.NamedTagged) (reference.Can |
| 229 | 229 |
} |
| 230 | 230 |
|
| 231 | 231 |
// Resolve the Auth config relevant for this server |
| 232 |
- authConfig := registry.ResolveAuthConfig(cli.configFile, repoInfo.Index) |
|
| 232 |
+ authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index) |
|
| 233 | 233 |
|
| 234 | 234 |
notaryRepo, err := cli.getNotaryRepository(repoInfo, authConfig) |
| 235 | 235 |
if err != nil {
|
| ... | ... |
@@ -16,7 +16,7 @@ import ( |
| 16 | 16 |
) |
| 17 | 17 |
|
| 18 | 18 |
func (cli *DockerCli) encodeRegistryAuth(index *registry.IndexInfo) (string, error) {
|
| 19 |
- authConfig := registry.ResolveAuthConfig(cli.configFile, index) |
|
| 19 |
+ authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, index) |
|
| 20 | 20 |
return authConfig.EncodeToBase64() |
| 21 | 21 |
} |
| 22 | 22 |
|
| ... | ... |
@@ -221,10 +221,10 @@ func tryV2TokenAuthLogin(authConfig *cliconfig.AuthConfig, params map[string]str |
| 221 | 221 |
} |
| 222 | 222 |
|
| 223 | 223 |
// ResolveAuthConfig matches an auth configuration to a server address or a URL |
| 224 |
-func ResolveAuthConfig(config *cliconfig.ConfigFile, index *IndexInfo) cliconfig.AuthConfig {
|
|
| 224 |
+func ResolveAuthConfig(authConfigs map[string]cliconfig.AuthConfig, index *IndexInfo) cliconfig.AuthConfig {
|
|
| 225 | 225 |
configKey := index.GetAuthConfigKey() |
| 226 | 226 |
// First try the happy case |
| 227 |
- if c, found := config.AuthConfigs[configKey]; found || index.Official {
|
|
| 227 |
+ if c, found := authConfigs[configKey]; found || index.Official {
|
|
| 228 | 228 |
return c |
| 229 | 229 |
} |
| 230 | 230 |
|
| ... | ... |
@@ -243,7 +243,7 @@ func ResolveAuthConfig(config *cliconfig.ConfigFile, index *IndexInfo) cliconfig |
| 243 | 243 |
|
| 244 | 244 |
// Maybe they have a legacy config file, we will iterate the keys converting |
| 245 | 245 |
// them to the new format and testing |
| 246 |
- for registry, ac := range config.AuthConfigs {
|
|
| 246 |
+ for registry, ac := range authConfigs {
|
|
| 247 | 247 |
if configKey == convertToHostname(registry) {
|
| 248 | 248 |
return ac |
| 249 | 249 |
} |
| ... | ... |
@@ -1,9 +1,6 @@ |
| 1 | 1 |
package registry |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "io/ioutil" |
|
| 5 |
- "os" |
|
| 6 |
- "path/filepath" |
|
| 7 | 4 |
"testing" |
| 8 | 5 |
|
| 9 | 6 |
"github.com/docker/docker/cliconfig" |
| ... | ... |
@@ -29,38 +26,23 @@ func TestEncodeAuth(t *testing.T) {
|
| 29 | 29 |
} |
| 30 | 30 |
} |
| 31 | 31 |
|
| 32 |
-func setupTempConfigFile() (*cliconfig.ConfigFile, error) {
|
|
| 33 |
- root, err := ioutil.TempDir("", "docker-test-auth")
|
|
| 34 |
- if err != nil {
|
|
| 35 |
- return nil, err |
|
| 36 |
- } |
|
| 37 |
- root = filepath.Join(root, cliconfig.ConfigFileName) |
|
| 38 |
- configFile := cliconfig.NewConfigFile(root) |
|
| 32 |
+func buildAuthConfigs() map[string]cliconfig.AuthConfig {
|
|
| 33 |
+ authConfigs := map[string]cliconfig.AuthConfig{}
|
|
| 39 | 34 |
|
| 40 | 35 |
for _, registry := range []string{"testIndex", IndexServer} {
|
| 41 |
- configFile.AuthConfigs[registry] = cliconfig.AuthConfig{
|
|
| 36 |
+ authConfigs[registry] = cliconfig.AuthConfig{
|
|
| 42 | 37 |
Username: "docker-user", |
| 43 | 38 |
Password: "docker-pass", |
| 44 | 39 |
Email: "docker@docker.io", |
| 45 | 40 |
} |
| 46 | 41 |
} |
| 47 | 42 |
|
| 48 |
- return configFile, nil |
|
| 43 |
+ return authConfigs |
|
| 49 | 44 |
} |
| 50 | 45 |
|
| 51 | 46 |
func TestSameAuthDataPostSave(t *testing.T) {
|
| 52 |
- configFile, err := setupTempConfigFile() |
|
| 53 |
- if err != nil {
|
|
| 54 |
- t.Fatal(err) |
|
| 55 |
- } |
|
| 56 |
- defer os.RemoveAll(configFile.Filename()) |
|
| 57 |
- |
|
| 58 |
- err = configFile.Save() |
|
| 59 |
- if err != nil {
|
|
| 60 |
- t.Fatal(err) |
|
| 61 |
- } |
|
| 62 |
- |
|
| 63 |
- authConfig := configFile.AuthConfigs["testIndex"] |
|
| 47 |
+ authConfigs := buildAuthConfigs() |
|
| 48 |
+ authConfig := authConfigs["testIndex"] |
|
| 64 | 49 |
if authConfig.Username != "docker-user" {
|
| 65 | 50 |
t.Fail() |
| 66 | 51 |
} |
| ... | ... |
@@ -76,13 +58,8 @@ func TestSameAuthDataPostSave(t *testing.T) {
|
| 76 | 76 |
} |
| 77 | 77 |
|
| 78 | 78 |
func TestResolveAuthConfigIndexServer(t *testing.T) {
|
| 79 |
- configFile, err := setupTempConfigFile() |
|
| 80 |
- if err != nil {
|
|
| 81 |
- t.Fatal(err) |
|
| 82 |
- } |
|
| 83 |
- defer os.RemoveAll(configFile.Filename()) |
|
| 84 |
- |
|
| 85 |
- indexConfig := configFile.AuthConfigs[IndexServer] |
|
| 79 |
+ authConfigs := buildAuthConfigs() |
|
| 80 |
+ indexConfig := authConfigs[IndexServer] |
|
| 86 | 81 |
|
| 87 | 82 |
officialIndex := &IndexInfo{
|
| 88 | 83 |
Official: true, |
| ... | ... |
@@ -91,19 +68,15 @@ func TestResolveAuthConfigIndexServer(t *testing.T) {
|
| 91 | 91 |
Official: false, |
| 92 | 92 |
} |
| 93 | 93 |
|
| 94 |
- resolved := ResolveAuthConfig(configFile, officialIndex) |
|
| 94 |
+ resolved := ResolveAuthConfig(authConfigs, officialIndex) |
|
| 95 | 95 |
assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer") |
| 96 | 96 |
|
| 97 |
- resolved = ResolveAuthConfig(configFile, privateIndex) |
|
| 97 |
+ resolved = ResolveAuthConfig(authConfigs, privateIndex) |
|
| 98 | 98 |
assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer") |
| 99 | 99 |
} |
| 100 | 100 |
|
| 101 | 101 |
func TestResolveAuthConfigFullURL(t *testing.T) {
|
| 102 |
- configFile, err := setupTempConfigFile() |
|
| 103 |
- if err != nil {
|
|
| 104 |
- t.Fatal(err) |
|
| 105 |
- } |
|
| 106 |
- defer os.RemoveAll(configFile.Filename()) |
|
| 102 |
+ authConfigs := buildAuthConfigs() |
|
| 107 | 103 |
|
| 108 | 104 |
registryAuth := cliconfig.AuthConfig{
|
| 109 | 105 |
Username: "foo-user", |
| ... | ... |
@@ -120,7 +93,7 @@ func TestResolveAuthConfigFullURL(t *testing.T) {
|
| 120 | 120 |
Password: "baz-pass", |
| 121 | 121 |
Email: "baz@example.com", |
| 122 | 122 |
} |
| 123 |
- configFile.AuthConfigs[IndexServer] = officialAuth |
|
| 123 |
+ authConfigs[IndexServer] = officialAuth |
|
| 124 | 124 |
|
| 125 | 125 |
expectedAuths := map[string]cliconfig.AuthConfig{
|
| 126 | 126 |
"registry.example.com": registryAuth, |
| ... | ... |
@@ -158,13 +131,13 @@ func TestResolveAuthConfigFullURL(t *testing.T) {
|
| 158 | 158 |
Name: configKey, |
| 159 | 159 |
} |
| 160 | 160 |
for _, registry := range registries {
|
| 161 |
- configFile.AuthConfigs[registry] = configured |
|
| 162 |
- resolved := ResolveAuthConfig(configFile, index) |
|
| 161 |
+ authConfigs[registry] = configured |
|
| 162 |
+ resolved := ResolveAuthConfig(authConfigs, index) |
|
| 163 | 163 |
if resolved.Email != configured.Email {
|
| 164 | 164 |
t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
|
| 165 | 165 |
} |
| 166 |
- delete(configFile.AuthConfigs, registry) |
|
| 167 |
- resolved = ResolveAuthConfig(configFile, index) |
|
| 166 |
+ delete(authConfigs, registry) |
|
| 167 |
+ resolved = ResolveAuthConfig(authConfigs, index) |
|
| 168 | 168 |
if resolved.Email == configured.Email {
|
| 169 | 169 |
t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email)
|
| 170 | 170 |
} |