mirror of https://github.com/docker/docker-py.git
Merge pull request #2068 from docker/c6047-legacy-auth
Fix support for legacy .dockercfg auth config format
This commit is contained in:
commit
dd9cff4f03
|
@ -139,8 +139,9 @@ class ContainerApiMixin(object):
|
||||||
'changes': changes
|
'changes': changes
|
||||||
}
|
}
|
||||||
u = self._url("/commit")
|
u = self._url("/commit")
|
||||||
return self._result(self._post_json(u, data=conf, params=params),
|
return self._result(
|
||||||
json=True)
|
self._post_json(u, data=conf, params=params), json=True
|
||||||
|
)
|
||||||
|
|
||||||
def containers(self, quiet=False, all=False, trunc=False, latest=False,
|
def containers(self, quiet=False, all=False, trunc=False, latest=False,
|
||||||
since=None, before=None, limit=-1, size=False,
|
since=None, before=None, limit=-1, size=False,
|
||||||
|
|
|
@ -270,7 +270,7 @@ def load_config(config_path=None, config_dict=None):
|
||||||
"Couldn't find auth-related section ; attempting to interpret"
|
"Couldn't find auth-related section ; attempting to interpret"
|
||||||
"as auth-only file"
|
"as auth-only file"
|
||||||
)
|
)
|
||||||
return parse_auth(config_dict)
|
return {'auths': parse_auth(config_dict)}
|
||||||
|
|
||||||
|
|
||||||
def _load_legacy_config(config_file):
|
def _load_legacy_config(config_file):
|
||||||
|
@ -287,14 +287,14 @@ def _load_legacy_config(config_file):
|
||||||
)
|
)
|
||||||
|
|
||||||
username, password = decode_auth(data[0])
|
username, password = decode_auth(data[0])
|
||||||
return {
|
return {'auths': {
|
||||||
INDEX_NAME: {
|
INDEX_NAME: {
|
||||||
'username': username,
|
'username': username,
|
||||||
'password': password,
|
'password': password,
|
||||||
'email': data[1],
|
'email': data[1],
|
||||||
'serveraddress': INDEX_URL,
|
'serveraddress': INDEX_URL,
|
||||||
}
|
}
|
||||||
}
|
}}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.debug(e)
|
log.debug(e)
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
import base64
|
|
||||||
import os
|
|
||||||
import tempfile
|
|
||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
import warnings
|
import warnings
|
||||||
|
@ -24,43 +21,6 @@ class InformationTest(BaseAPIIntegrationTest):
|
||||||
assert 'Debug' in res
|
assert 'Debug' in res
|
||||||
|
|
||||||
|
|
||||||
class LoadConfigTest(BaseAPIIntegrationTest):
|
|
||||||
def test_load_legacy_config(self):
|
|
||||||
folder = tempfile.mkdtemp()
|
|
||||||
self.tmp_folders.append(folder)
|
|
||||||
cfg_path = os.path.join(folder, '.dockercfg')
|
|
||||||
f = open(cfg_path, 'w')
|
|
||||||
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
|
|
||||||
f.write('auth = {0}\n'.format(auth_))
|
|
||||||
f.write('email = sakuya@scarlet.net')
|
|
||||||
f.close()
|
|
||||||
cfg = docker.auth.load_config(cfg_path)
|
|
||||||
assert cfg[docker.auth.INDEX_NAME] is not None
|
|
||||||
cfg = cfg[docker.auth.INDEX_NAME]
|
|
||||||
assert cfg['username'] == 'sakuya'
|
|
||||||
assert cfg['password'] == 'izayoi'
|
|
||||||
assert cfg['email'] == 'sakuya@scarlet.net'
|
|
||||||
assert cfg.get('Auth') is None
|
|
||||||
|
|
||||||
def test_load_json_config(self):
|
|
||||||
folder = tempfile.mkdtemp()
|
|
||||||
self.tmp_folders.append(folder)
|
|
||||||
cfg_path = os.path.join(folder, '.dockercfg')
|
|
||||||
f = open(os.path.join(folder, '.dockercfg'), 'w')
|
|
||||||
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
|
|
||||||
email_ = 'sakuya@scarlet.net'
|
|
||||||
f.write('{{"{0}": {{"auth": "{1}", "email": "{2}"}}}}\n'.format(
|
|
||||||
docker.auth.INDEX_URL, auth_, email_))
|
|
||||||
f.close()
|
|
||||||
cfg = docker.auth.load_config(cfg_path)
|
|
||||||
assert cfg[docker.auth.INDEX_URL] is not None
|
|
||||||
cfg = cfg[docker.auth.INDEX_URL]
|
|
||||||
assert cfg['username'] == 'sakuya'
|
|
||||||
assert cfg['password'] == 'izayoi'
|
|
||||||
assert cfg['email'] == 'sakuya@scarlet.net'
|
|
||||||
assert cfg.get('Auth') is None
|
|
||||||
|
|
||||||
|
|
||||||
class AutoDetectVersionTest(unittest.TestCase):
|
class AutoDetectVersionTest(unittest.TestCase):
|
||||||
def test_client_init(self):
|
def test_client_init(self):
|
||||||
client = docker.APIClient(version='auto', **kwargs_from_env())
|
client = docker.APIClient(version='auto', **kwargs_from_env())
|
||||||
|
|
|
@ -282,22 +282,64 @@ class LoadConfigTest(unittest.TestCase):
|
||||||
cfg = auth.load_config(folder)
|
cfg = auth.load_config(folder)
|
||||||
assert cfg is not None
|
assert cfg is not None
|
||||||
|
|
||||||
def test_load_config(self):
|
def test_load_legacy_config(self):
|
||||||
folder = tempfile.mkdtemp()
|
folder = tempfile.mkdtemp()
|
||||||
self.addCleanup(shutil.rmtree, folder)
|
self.addCleanup(shutil.rmtree, folder)
|
||||||
dockercfg_path = os.path.join(folder, '.dockercfg')
|
cfg_path = os.path.join(folder, '.dockercfg')
|
||||||
with open(dockercfg_path, 'w') as f:
|
|
||||||
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
|
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
|
||||||
|
with open(cfg_path, 'w') as f:
|
||||||
f.write('auth = {0}\n'.format(auth_))
|
f.write('auth = {0}\n'.format(auth_))
|
||||||
f.write('email = sakuya@scarlet.net')
|
f.write('email = sakuya@scarlet.net')
|
||||||
cfg = auth.load_config(dockercfg_path)
|
|
||||||
assert auth.INDEX_NAME in cfg
|
cfg = auth.load_config(cfg_path)
|
||||||
assert cfg[auth.INDEX_NAME] is not None
|
assert auth.resolve_authconfig(cfg) is not None
|
||||||
cfg = cfg[auth.INDEX_NAME]
|
assert cfg['auths'][auth.INDEX_NAME] is not None
|
||||||
|
cfg = cfg['auths'][auth.INDEX_NAME]
|
||||||
assert cfg['username'] == 'sakuya'
|
assert cfg['username'] == 'sakuya'
|
||||||
assert cfg['password'] == 'izayoi'
|
assert cfg['password'] == 'izayoi'
|
||||||
assert cfg['email'] == 'sakuya@scarlet.net'
|
assert cfg['email'] == 'sakuya@scarlet.net'
|
||||||
assert cfg.get('auth') is None
|
assert cfg.get('Auth') is None
|
||||||
|
|
||||||
|
def test_load_json_config(self):
|
||||||
|
folder = tempfile.mkdtemp()
|
||||||
|
self.addCleanup(shutil.rmtree, folder)
|
||||||
|
cfg_path = os.path.join(folder, '.dockercfg')
|
||||||
|
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
|
||||||
|
email = 'sakuya@scarlet.net'
|
||||||
|
with open(cfg_path, 'w') as f:
|
||||||
|
json.dump(
|
||||||
|
{auth.INDEX_URL: {'auth': auth_, 'email': email}}, f
|
||||||
|
)
|
||||||
|
cfg = auth.load_config(cfg_path)
|
||||||
|
assert auth.resolve_authconfig(cfg) is not None
|
||||||
|
assert cfg['auths'][auth.INDEX_URL] is not None
|
||||||
|
cfg = cfg['auths'][auth.INDEX_URL]
|
||||||
|
assert cfg['username'] == 'sakuya'
|
||||||
|
assert cfg['password'] == 'izayoi'
|
||||||
|
assert cfg['email'] == email
|
||||||
|
assert cfg.get('Auth') is None
|
||||||
|
|
||||||
|
def test_load_modern_json_config(self):
|
||||||
|
folder = tempfile.mkdtemp()
|
||||||
|
self.addCleanup(shutil.rmtree, folder)
|
||||||
|
cfg_path = os.path.join(folder, 'config.json')
|
||||||
|
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
|
||||||
|
email = 'sakuya@scarlet.net'
|
||||||
|
with open(cfg_path, 'w') as f:
|
||||||
|
json.dump({
|
||||||
|
'auths': {
|
||||||
|
auth.INDEX_URL: {
|
||||||
|
'auth': auth_, 'email': email
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, f)
|
||||||
|
cfg = auth.load_config(cfg_path)
|
||||||
|
assert auth.resolve_authconfig(cfg) is not None
|
||||||
|
assert cfg['auths'][auth.INDEX_URL] is not None
|
||||||
|
cfg = cfg['auths'][auth.INDEX_URL]
|
||||||
|
assert cfg['username'] == 'sakuya'
|
||||||
|
assert cfg['password'] == 'izayoi'
|
||||||
|
assert cfg['email'] == email
|
||||||
|
|
||||||
def test_load_config_with_random_name(self):
|
def test_load_config_with_random_name(self):
|
||||||
folder = tempfile.mkdtemp()
|
folder = tempfile.mkdtemp()
|
||||||
|
@ -318,7 +360,7 @@ class LoadConfigTest(unittest.TestCase):
|
||||||
with open(dockercfg_path, 'w') as f:
|
with open(dockercfg_path, 'w') as f:
|
||||||
json.dump(config, f)
|
json.dump(config, f)
|
||||||
|
|
||||||
cfg = auth.load_config(dockercfg_path)
|
cfg = auth.load_config(dockercfg_path)['auths']
|
||||||
assert registry in cfg
|
assert registry in cfg
|
||||||
assert cfg[registry] is not None
|
assert cfg[registry] is not None
|
||||||
cfg = cfg[registry]
|
cfg = cfg[registry]
|
||||||
|
@ -345,7 +387,7 @@ class LoadConfigTest(unittest.TestCase):
|
||||||
json.dump(config, f)
|
json.dump(config, f)
|
||||||
|
|
||||||
with mock.patch.dict(os.environ, {'DOCKER_CONFIG': folder}):
|
with mock.patch.dict(os.environ, {'DOCKER_CONFIG': folder}):
|
||||||
cfg = auth.load_config(None)
|
cfg = auth.load_config(None)['auths']
|
||||||
assert registry in cfg
|
assert registry in cfg
|
||||||
assert cfg[registry] is not None
|
assert cfg[registry] is not None
|
||||||
cfg = cfg[registry]
|
cfg = cfg[registry]
|
||||||
|
@ -422,7 +464,7 @@ class LoadConfigTest(unittest.TestCase):
|
||||||
json.dump(config, f)
|
json.dump(config, f)
|
||||||
|
|
||||||
cfg = auth.load_config(dockercfg_path)
|
cfg = auth.load_config(dockercfg_path)
|
||||||
assert cfg == {}
|
assert cfg == {'auths': {}}
|
||||||
|
|
||||||
def test_load_config_invalid_auth_dict(self):
|
def test_load_config_invalid_auth_dict(self):
|
||||||
folder = tempfile.mkdtemp()
|
folder = tempfile.mkdtemp()
|
||||||
|
|
Loading…
Reference in New Issue