From 22da6ace7a755885489aafbe2003cbb2f0c253cd Mon Sep 17 00:00:00 2001 From: Alejandro Brito Monedero Date: Tue, 27 Oct 2015 09:03:28 +0100 Subject: [PATCH] Fix auth.load_config bug calling parse_auth When load_config found and auths section it didn't call parse_auth only with the auths section. Instead it called parse_auth with all the configuration. There is also a test to check this case Signed-off-by: Alejandro Brito Monedero --- docker/auth/auth.py | 2 +- tests/unit/auth_test.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docker/auth/auth.py b/docker/auth/auth.py index b02c3ed3..2ed894ee 100644 --- a/docker/auth/auth.py +++ b/docker/auth/auth.py @@ -172,7 +172,7 @@ def load_config(config_path=None): data = json.load(f) if data.get('auths'): log.debug("Found 'auths' section") - return parse_auth(data) + return parse_auth(data['auths']) else: log.debug("Couldn't find 'auths' section") f.seek(0) diff --git a/tests/unit/auth_test.py b/tests/unit/auth_test.py index 3405de6a..9f4d439b 100644 --- a/tests/unit/auth_test.py +++ b/tests/unit/auth_test.py @@ -287,3 +287,32 @@ class LoadConfigTest(base.Cleanup, base.BaseTestCase): self.assertEqual(cfg['password'], 'izayoi') self.assertEqual(cfg['email'], 'sakuya@scarlet.net') self.assertEqual(cfg.get('auth'), None) + + def test_load_config_custom_config_env_with_auths(self): + folder = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, folder) + + dockercfg_path = os.path.join(folder, 'config.json') + registry = 'https://your.private.registry.io' + auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii') + config = { + 'auths': { + registry: { + 'auth': '{0}'.format(auth_), + 'email': 'sakuya@scarlet.net' + } + } + } + + with open(dockercfg_path, 'w') as f: + json.dump(config, f) + + with mock.patch.dict(os.environ, {'DOCKER_CONFIG': folder}): + cfg = auth.load_config(None) + assert registry in cfg + self.assertNotEqual(cfg[registry], None) + cfg = cfg[registry] + self.assertEqual(cfg['username'], 'sakuya') + self.assertEqual(cfg['password'], 'izayoi') + self.assertEqual(cfg['email'], 'sakuya@scarlet.net') + self.assertEqual(cfg.get('auth'), None)