Resolve docker types conflict

Looks like the latest `github.com/docker/docker/registry.ResolveAuthConfig` expect
`github.com/docker/docker/api/types.AuthConfig` rather than
`github.com/docker/cli/cli/config/types.AuthConfig`. The two types are
identical but live in different packages.

Here, we embed `registry.ResolveAuthConfig` from upstream repo, but with
the signature we need.
This commit is contained in:
Mahmood Ali 2020-01-27 13:40:40 -05:00 committed by Yoan Blanc
parent 1d92edbbbe
commit 887292d757
No known key found for this signature in database
GPG key ID: 6058CF4574298812

View file

@ -11,8 +11,9 @@ import (
"strings"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/types"
"github.com/docker/distribution/reference"
ctypes "github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/registry"
docker "github.com/fsouza/go-dockerclient"
)
@ -116,22 +117,9 @@ func authFromDockerConfig(file string) authBackend {
return nil, err
}
authConfigs := make(map[string]ctypes.AuthConfig)
for k, c := range cfile.AuthConfigs {
authConfigs[k] = ctypes.AuthConfig{
Username: c.Username,
Password: c.Password,
Auth: c.Auth,
Email: c.Email,
ServerAddress: c.ServerAddress,
IdentityToken: c.IdentityToken,
RegistryToken: c.RegistryToken,
}
}
return firstValidAuth(repo, []authBackend{
func(string) (*docker.AuthConfiguration, error) {
dockerAuthConfig := registry.ResolveAuthConfig(authConfigs, repoInfo.Index)
dockerAuthConfig := registryResolveAuthConfig(cfile.AuthConfigs, repoInfo.Index)
auth := &docker.AuthConfiguration{
Username: dockerAuthConfig.Username,
Password: dockerAuthConfig.Password,
@ -294,3 +282,25 @@ func parseVolumeSpecLinux(volBind string) (hostPath string, containerPath string
return parts[0], parts[1], m, nil
}
// ResolveAuthConfig matches an auth configuration to a server address or a URL
// copied from https://github.com/moby/moby/blob/ca20bc4214e6a13a5f134fb0d2f67c38065283a8/registry/auth.go#L217-L235
// but with the CLI types.AuthConfig type rather than api/types
func registryResolveAuthConfig(authConfigs map[string]types.AuthConfig, index *registrytypes.IndexInfo) types.AuthConfig {
configKey := registry.GetAuthConfigKey(index)
// First try the happy case
if c, found := authConfigs[configKey]; found || index.Official {
return c
}
// Maybe they have a legacy config file, we will iterate the keys converting
// them to the new format and testing
for r, ac := range authConfigs {
if configKey == registry.ConvertToHostname(r) {
return ac
}
}
// When all else fails, return an empty auth config
return types.AuthConfig{}
}