Kaydet (Commit) 53beba75 authored tarafından Joffrey F's avatar Joffrey F

Merge pull request #427 from abanna/adding_multiple_dockercfg_support

adding the ability to login with different dockercfg files.
......@@ -117,14 +117,20 @@ def encode_full_header(auth):
return encode_header({'configs': auth})
def load_config(root=None):
"""Loads authentication data from a Docker configuration file in the given
root directory."""
def load_config(config_path=None):
"""
Loads authentication data from a Docker configuration file in the given
root directory or if config_path is passed use given path.
"""
conf = {}
data = None
config_file = os.path.join(root or os.environ.get('HOME', '.'),
DOCKER_CONFIG_FILENAME)
config_file = config_path or os.path.join(os.environ.get('HOME', '.'),
DOCKER_CONFIG_FILENAME)
# if config path doesn't exist return empty config
if not os.path.exists(config_file):
return {}
# First try as JSON
try:
......
......@@ -715,10 +715,14 @@ class Client(requests.Session):
self._raise_for_status(res)
def login(self, username, password=None, email=None, registry=None,
reauth=False, insecure_registry=False):
reauth=False, insecure_registry=False, dockercfg_path=None):
# If we don't have any auth data so far, try reloading the config file
# one more time in case anything showed up in there.
if not self._auth_configs:
# If dockercfg_path is passed check to see if the config file exists,
# if so load that config.
if dockercfg_path and os.path.exists(dockercfg_path):
self._auth_configs = auth.load_config(dockercfg_path)
elif not self._auth_configs:
self._auth_configs = auth.load_config()
registry = auth.expand_registry_url(registry, insecure_registry) \
......
......@@ -29,6 +29,7 @@ import threading
import time
import unittest
import warnings
import random
import docker
import requests
......@@ -2004,12 +2005,13 @@ class DockerClientTest(Cleanup, unittest.TestCase):
def test_load_config(self):
folder = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, folder)
f = open(os.path.join(folder, '.dockercfg'), 'w')
dockercfg_path = os.path.join(folder, '.dockercfg')
f = open(dockercfg_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(folder)
cfg = docker.auth.load_config(dockercfg_path)
self.assertTrue(docker.auth.INDEX_URL in cfg)
self.assertNotEqual(cfg[docker.auth.INDEX_URL], None)
cfg = cfg[docker.auth.INDEX_URL]
......@@ -2018,6 +2020,34 @@ class DockerClientTest(Cleanup, unittest.TestCase):
self.assertEqual(cfg['email'], 'sakuya@scarlet.net')
self.assertEqual(cfg.get('auth'), None)
def test_load_config_with_random_name(self):
folder = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, folder)
dockercfg_path = os.path.join(folder,
'.{0}.dockercfg'.format(
random.randrange(100000)))
registry = 'https://your.private.registry.io'
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
config = {
registry: {
'auth': '{0}'.format(auth_),
'email': 'sakuya@scarlet.net'
}
}
with open(dockercfg_path, 'w') as f:
f.write(json.dumps(config))
cfg = docker.auth.load_config(dockercfg_path)
self.assertTrue(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)
def test_tar_with_excludes(self):
base = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, base)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment