diff --git a/docker/auth/__init__.py b/docker/auth/__init__.py index 66acdb36..d068b7fa 100644 --- a/docker/auth/__init__.py +++ b/docker/auth/__init__.py @@ -3,5 +3,5 @@ from .auth import ( encode_header, load_config, resolve_authconfig, - resolve_repository_name + resolve_repository_name, ) # flake8: noqa \ No newline at end of file diff --git a/docker/auth/auth.py b/docker/auth/auth.py index fe53c766..86939103 100644 --- a/docker/auth/auth.py +++ b/docker/auth/auth.py @@ -75,7 +75,7 @@ def resolve_authconfig(authconfig, registry=None): # Default to the public index server registry = registry or INDEX_URL - # Ff its not the index server there are three cases: + # If it's not the index server there are three cases: # # 1. this is a full config url -> it should be used as is # 2. it could be a full url, but with the wrong protocol @@ -84,7 +84,7 @@ def resolve_authconfig(authconfig, registry=None): # as there is only one auth entry which is fully qualified we need to start # parsing and matching if '/v1/' not in registry: - registry = registry + '/v1/' + registry = os.path.join(registry, 'v1/') if not registry.startswith('http:') and not registry.startswith('https:'): registry = 'https://' + registry diff --git a/tests/utils_test.py b/tests/utils_test.py index 2708302e..9b6461f4 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -8,6 +8,7 @@ from docker.utils import ( parse_repository_tag, parse_host, convert_filters, kwargs_from_env, create_host_config ) +from docker.auth import resolve_authconfig class UtilsTest(unittest.TestCase): @@ -100,6 +101,60 @@ class UtilsTest(unittest.TestCase): empty_config = create_host_config() self.assertEqual(empty_config, {}) + def test_resolve_authconfig(self): + auth_config = { + 'https://index.docker.io/v1/': {'auth': 'indexuser'}, + 'http://my.registry.net/v1/': {'auth': 'privateuser'} + } + # hostname only + self.assertEqual( + resolve_authconfig(auth_config, 'my.registry.net'), + {'auth': 'privateuser'} + ) + # no protocol + self.assertEqual( + resolve_authconfig(auth_config, 'my.registry.net/v1/'), + {'auth': 'privateuser'} + ) + # no path + self.assertEqual( + resolve_authconfig(auth_config, 'http://my.registry.net'), + {'auth': 'privateuser'} + ) + # no path, trailing slash + self.assertEqual( + resolve_authconfig(auth_config, 'http://my.registry.net/'), + {'auth': 'privateuser'} + ) + # no path, wrong secure protocol + self.assertEqual( + resolve_authconfig(auth_config, 'https://my.registry.net'), + {'auth': 'privateuser'} + ) + # no path, wrong insecure protocol + self.assertEqual( + resolve_authconfig(auth_config, 'http://index.docker.io'), + {'auth': 'indexuser'} + ) + # with path, wrong protocol + self.assertEqual( + resolve_authconfig(auth_config, 'https://my.registry.net/v1/'), + {'auth': 'privateuser'} + ) + # default registry + self.assertEqual( + resolve_authconfig(auth_config), {'auth': 'indexuser'} + ) + # default registry (explicit None) + self.assertEqual( + resolve_authconfig(auth_config, None), {'auth': 'indexuser'} + ) + # fully explicit + self.assertEqual( + resolve_authconfig(auth_config, 'http://my.registry.net/v1/'), + {'auth': 'privateuser'} + ) + if __name__ == '__main__': unittest.main()