1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Refactor ResolveAuthConfig to remove the builder dependency on cli code.

registry.ResolveAuthConfig() only needs the AuthConfigs from the ConfigFile, so
this change passed just the AuthConfigs.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
Daniel Nephin 2015-12-11 19:11:20 -08:00
parent 8d36f097cf
commit 920ea13516
8 changed files with 25 additions and 52 deletions

View file

@ -65,7 +65,7 @@ func (cli *DockerCli) CmdPull(args ...string) error {
return err return err
} }
authConfig := registry.ResolveAuthConfig(cli.configFile, repoInfo.Index) authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "pull") requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
if isTrusted() && !ref.HasDigest() { if isTrusted() && !ref.HasDigest() {

View file

@ -44,7 +44,7 @@ func (cli *DockerCli) CmdPush(args ...string) error {
return err return err
} }
// Resolve the Auth config relevant for this server // Resolve the Auth config relevant for this server
authConfig := registry.ResolveAuthConfig(cli.configFile, repoInfo.Index) authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
// If we're not using a custom registry, we know the restrictions // If we're not using a custom registry, we know the restrictions
// applied to repository names and can warn the user in advance. // applied to repository names and can warn the user in advance.
// Custom repositories can have different rules, and we must also // Custom repositories can have different rules, and we must also

View file

@ -35,7 +35,7 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
return err return err
} }
authConfig := registry.ResolveAuthConfig(cli.configFile, indexInfo) authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, indexInfo)
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(indexInfo, "search") requestPrivilege := cli.registryAuthenticationPrivilegedFunc(indexInfo, "search")
encodedAuth, err := authConfig.EncodeToBase64() encodedAuth, err := authConfig.EncodeToBase64()

View file

@ -229,7 +229,7 @@ func (cli *DockerCli) trustedReference(ref reference.NamedTagged) (reference.Can
} }
// Resolve the Auth config relevant for this server // Resolve the Auth config relevant for this server
authConfig := registry.ResolveAuthConfig(cli.configFile, repoInfo.Index) authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
notaryRepo, err := cli.getNotaryRepository(repoInfo, authConfig) notaryRepo, err := cli.getNotaryRepository(repoInfo, authConfig)
if err != nil { if err != nil {

View file

@ -16,7 +16,7 @@ import (
) )
func (cli *DockerCli) encodeRegistryAuth(index *registry.IndexInfo) (string, error) { func (cli *DockerCli) encodeRegistryAuth(index *registry.IndexInfo) (string, error) {
authConfig := registry.ResolveAuthConfig(cli.configFile, index) authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, index)
return authConfig.EncodeToBase64() return authConfig.EncodeToBase64()
} }

View file

@ -67,7 +67,7 @@ func (d Docker) Pull(name string) (*image.Image, error) {
} }
resolvedConfig := registry.ResolveAuthConfig( resolvedConfig := registry.ResolveAuthConfig(
&cliconfig.ConfigFile{AuthConfigs: d.AuthConfigs}, d.AuthConfigs,
repoInfo.Index, repoInfo.Index,
) )
pullRegistryAuth = &resolvedConfig pullRegistryAuth = &resolvedConfig

View file

@ -221,10 +221,10 @@ func tryV2TokenAuthLogin(authConfig *cliconfig.AuthConfig, params map[string]str
} }
// ResolveAuthConfig matches an auth configuration to a server address or a URL // ResolveAuthConfig matches an auth configuration to a server address or a URL
func ResolveAuthConfig(config *cliconfig.ConfigFile, index *IndexInfo) cliconfig.AuthConfig { func ResolveAuthConfig(authConfigs map[string]cliconfig.AuthConfig, index *IndexInfo) cliconfig.AuthConfig {
configKey := index.GetAuthConfigKey() configKey := index.GetAuthConfigKey()
// First try the happy case // First try the happy case
if c, found := config.AuthConfigs[configKey]; found || index.Official { if c, found := authConfigs[configKey]; found || index.Official {
return c return c
} }
@ -243,7 +243,7 @@ func ResolveAuthConfig(config *cliconfig.ConfigFile, index *IndexInfo) cliconfig
// Maybe they have a legacy config file, we will iterate the keys converting // Maybe they have a legacy config file, we will iterate the keys converting
// them to the new format and testing // them to the new format and testing
for registry, ac := range config.AuthConfigs { for registry, ac := range authConfigs {
if configKey == convertToHostname(registry) { if configKey == convertToHostname(registry) {
return ac return ac
} }

View file

@ -1,9 +1,6 @@
package registry package registry
import ( import (
"io/ioutil"
"os"
"path/filepath"
"testing" "testing"
"github.com/docker/docker/cliconfig" "github.com/docker/docker/cliconfig"
@ -29,38 +26,23 @@ func TestEncodeAuth(t *testing.T) {
} }
} }
func setupTempConfigFile() (*cliconfig.ConfigFile, error) { func buildAuthConfigs() map[string]cliconfig.AuthConfig {
root, err := ioutil.TempDir("", "docker-test-auth") authConfigs := map[string]cliconfig.AuthConfig{}
if err != nil {
return nil, err
}
root = filepath.Join(root, cliconfig.ConfigFileName)
configFile := cliconfig.NewConfigFile(root)
for _, registry := range []string{"testIndex", IndexServer} { for _, registry := range []string{"testIndex", IndexServer} {
configFile.AuthConfigs[registry] = cliconfig.AuthConfig{ authConfigs[registry] = cliconfig.AuthConfig{
Username: "docker-user", Username: "docker-user",
Password: "docker-pass", Password: "docker-pass",
Email: "docker@docker.io", Email: "docker@docker.io",
} }
} }
return configFile, nil return authConfigs
} }
func TestSameAuthDataPostSave(t *testing.T) { func TestSameAuthDataPostSave(t *testing.T) {
configFile, err := setupTempConfigFile() authConfigs := buildAuthConfigs()
if err != nil { authConfig := authConfigs["testIndex"]
t.Fatal(err)
}
defer os.RemoveAll(configFile.Filename())
err = configFile.Save()
if err != nil {
t.Fatal(err)
}
authConfig := configFile.AuthConfigs["testIndex"]
if authConfig.Username != "docker-user" { if authConfig.Username != "docker-user" {
t.Fail() t.Fail()
} }
@ -76,13 +58,8 @@ func TestSameAuthDataPostSave(t *testing.T) {
} }
func TestResolveAuthConfigIndexServer(t *testing.T) { func TestResolveAuthConfigIndexServer(t *testing.T) {
configFile, err := setupTempConfigFile() authConfigs := buildAuthConfigs()
if err != nil { indexConfig := authConfigs[IndexServer]
t.Fatal(err)
}
defer os.RemoveAll(configFile.Filename())
indexConfig := configFile.AuthConfigs[IndexServer]
officialIndex := &IndexInfo{ officialIndex := &IndexInfo{
Official: true, Official: true,
@ -91,19 +68,15 @@ func TestResolveAuthConfigIndexServer(t *testing.T) {
Official: false, Official: false,
} }
resolved := ResolveAuthConfig(configFile, officialIndex) resolved := ResolveAuthConfig(authConfigs, officialIndex)
assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer") assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")
resolved = ResolveAuthConfig(configFile, privateIndex) resolved = ResolveAuthConfig(authConfigs, privateIndex)
assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer") assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
} }
func TestResolveAuthConfigFullURL(t *testing.T) { func TestResolveAuthConfigFullURL(t *testing.T) {
configFile, err := setupTempConfigFile() authConfigs := buildAuthConfigs()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(configFile.Filename())
registryAuth := cliconfig.AuthConfig{ registryAuth := cliconfig.AuthConfig{
Username: "foo-user", Username: "foo-user",
@ -120,7 +93,7 @@ func TestResolveAuthConfigFullURL(t *testing.T) {
Password: "baz-pass", Password: "baz-pass",
Email: "baz@example.com", Email: "baz@example.com",
} }
configFile.AuthConfigs[IndexServer] = officialAuth authConfigs[IndexServer] = officialAuth
expectedAuths := map[string]cliconfig.AuthConfig{ expectedAuths := map[string]cliconfig.AuthConfig{
"registry.example.com": registryAuth, "registry.example.com": registryAuth,
@ -158,13 +131,13 @@ func TestResolveAuthConfigFullURL(t *testing.T) {
Name: configKey, Name: configKey,
} }
for _, registry := range registries { for _, registry := range registries {
configFile.AuthConfigs[registry] = configured authConfigs[registry] = configured
resolved := ResolveAuthConfig(configFile, index) resolved := ResolveAuthConfig(authConfigs, index)
if resolved.Email != configured.Email { if resolved.Email != configured.Email {
t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email) t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
} }
delete(configFile.AuthConfigs, registry) delete(authConfigs, registry)
resolved = ResolveAuthConfig(configFile, index) resolved = ResolveAuthConfig(authConfigs, index)
if resolved.Email == configured.Email { if resolved.Email == configured.Email {
t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email) t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email)
} }