Skip to content

Commit a341aa2

Browse files
committed
Merge branch 'aanand-fix-resolve-authconfig'
2 parents 5ce02b9 + 8639088 commit a341aa2

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

docker/auth/auth.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@
2626
DOCKER_CONFIG_FILENAME = '.dockercfg'
2727

2828

29-
def swap_protocol(url):
30-
if url.startswith('http://'):
31-
return url.replace('http://', 'https://', 1)
32-
if url.startswith('https://'):
33-
return url.replace('https://', 'http://', 1)
34-
return url
35-
36-
3729
def expand_registry_url(hostname, insecure=False):
3830
if hostname.startswith('http:') or hostname.startswith('https:'):
3931
return hostname
@@ -68,29 +60,27 @@ def resolve_repository_name(repo_name, insecure=False):
6860

6961

7062
def resolve_authconfig(authconfig, registry=None):
71-
"""Return the authentication data from the given auth configuration for a
72-
specific registry. We'll do our best to infer the correct URL for the
73-
registry, trying both http and https schemes. Returns an empty dictionnary
74-
if no data exists."""
63+
"""
64+
Returns the authentication data from the given auth configuration for a
65+
specific registry. As with the Docker client, legacy entries in the config
66+
with full URLs are stripped down to hostnames before checking for a match.
67+
Returns None if no match was found.
68+
"""
7569
# Default to the public index server
76-
registry = registry or INDEX_URL
77-
78-
# If it's not the index server there are three cases:
79-
#
80-
# 1. this is a full config url -> it should be used as is
81-
# 2. it could be a full url, but with the wrong protocol
82-
# 3. it can be the hostname optionally with a port
83-
#
84-
# as there is only one auth entry which is fully qualified we need to start
85-
# parsing and matching
86-
if '/v1/' not in registry:
87-
registry = os.path.join(registry, 'v1/')
88-
if not registry.startswith('http:') and not registry.startswith('https:'):
89-
registry = 'https://' + registry
70+
registry = convert_to_hostname(registry) if registry else INDEX_URL
9071

9172
if registry in authconfig:
9273
return authconfig[registry]
93-
return authconfig.get(swap_protocol(registry), None)
74+
75+
for key, config in six.iteritems(authconfig):
76+
if convert_to_hostname(key) == registry:
77+
return config
78+
79+
return None
80+
81+
82+
def convert_to_hostname(url):
83+
return url.replace('http://', '').replace('https://', '').split('/', 1)[0]
9484

9585

9686
def encode_auth(auth_info):

docker/client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,7 @@ def login(self, username, password=None, email=None, registry=None,
627627
elif not self._auth_configs:
628628
self._auth_configs = auth.load_config()
629629

630-
registry = auth.expand_registry_url(registry, insecure_registry) \
631-
if registry else auth.INDEX_URL
630+
registry = registry or auth.INDEX_URL
632631

633632
authcfg = auth.resolve_authconfig(self._auth_configs, registry)
634633
# If we found an existing auth config for this registry and username

tests/utils_test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def test_create_host_config(self):
104104
def test_resolve_authconfig(self):
105105
auth_config = {
106106
'https://index.docker.io/v1/': {'auth': 'indexuser'},
107-
'http://my.registry.net/v1/': {'auth': 'privateuser'}
107+
'my.registry.net': {'auth': 'privateuser'},
108+
'http://legacy.registry.url/v1/': {'auth': 'legacyauth'}
108109
}
109110
# hostname only
110111
self.assertEqual(
@@ -154,6 +155,15 @@ def test_resolve_authconfig(self):
154155
resolve_authconfig(auth_config, 'http://my.registry.net/v1/'),
155156
{'auth': 'privateuser'}
156157
)
158+
# legacy entry in config
159+
self.assertEqual(
160+
resolve_authconfig(auth_config, 'legacy.registry.url'),
161+
{'auth': 'legacyauth'}
162+
)
163+
# no matching entry
164+
self.assertTrue(
165+
resolve_authconfig(auth_config, 'does.not.exist') is None
166+
)
157167

158168

159169
if __name__ == '__main__':

0 commit comments

Comments
 (0)