forked from TrueCloudLab/distribution
Merge pull request #18618 from dnephin/refactor_resolve_auth_config
Refactor ResolveAuthConfig to remove the builder dependency on cli code
This commit is contained in:
commit
4a3b23718b
2 changed files with 19 additions and 46 deletions
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue