From 75cc50f0ad2c0d39126f3dd1112235d338d78c4d Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Tue, 21 Jul 2015 16:50:41 +0100 Subject: [PATCH 1/2] Default to ~/.docker if DOCKER_CERT_PATH is empty Signed-off-by: Aanand Prasad --- docker/utils/utils.py | 7 +++++++ tests/utils_test.py | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 10c08de2..e5e974a2 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -314,9 +314,15 @@ def kwargs_from_env(ssl_version=None, assert_hostname=None): tls_verify = os.environ.get('DOCKER_TLS_VERIFY') params = {} + if host: params['base_url'] = (host.replace('tcp://', 'https://') if tls_verify else host) + + if tls_verify and not cert_path: + if 'HOME' in os.environ: + cert_path = os.path.join(os.environ['HOME'], '.docker') + if tls_verify and cert_path: params['tls'] = tls.TLSConfig( client_cert=(os.path.join(cert_path, 'cert.pem'), @@ -325,6 +331,7 @@ def kwargs_from_env(ssl_version=None, assert_hostname=None): verify=True, ssl_version=ssl_version, assert_hostname=assert_hostname) + return params diff --git a/tests/utils_test.py b/tests/utils_test.py index 1c8729ca..bacb0f05 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -1,5 +1,7 @@ import os import os.path +import shutil +import tempfile import unittest from docker.client import Client @@ -13,6 +15,11 @@ from docker.auth import resolve_repository_name, resolve_authconfig import base +TEST_CERT_DIR = os.path.join( + os.path.dirname(__file__), + 'testdata/certs', +) + class UtilsTest(base.BaseTestCase): longMessage = True @@ -75,11 +82,18 @@ class UtilsTest(base.BaseTestCase): for host, expected in valid_hosts.items(): self.assertEqual(parse_host(host), expected, msg=host) - def test_kwargs_from_env(self): + def test_kwargs_from_env_empty(self): + os.environ.update(DOCKER_HOST='', + DOCKER_CERT_PATH='', + DOCKER_TLS_VERIFY='') + + kwargs = kwargs_from_env() + self.assertEqual(None, kwargs.get('base_url')) + self.assertEqual(None, kwargs.get('tls')) + + def test_kwargs_from_env_tls(self): os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376', - DOCKER_CERT_PATH=os.path.join( - os.path.dirname(__file__), - 'testdata/certs'), + DOCKER_CERT_PATH=TEST_CERT_DIR, DOCKER_TLS_VERIFY='1') kwargs = kwargs_from_env(assert_hostname=False) self.assertEqual('https://192.168.59.103:2376', kwargs['base_url']) @@ -95,6 +109,24 @@ class UtilsTest(base.BaseTestCase): except TypeError as e: self.fail(e) + def test_kwargs_from_env_no_cert_path(self): + try: + temp_dir = tempfile.mkdtemp() + cert_dir = os.path.join(temp_dir, '.docker') + shutil.copytree(TEST_CERT_DIR, cert_dir) + + os.environ.update(HOME=temp_dir, + DOCKER_CERT_PATH='', + DOCKER_TLS_VERIFY='1') + + kwargs = kwargs_from_env() + self.assertIn(cert_dir, kwargs['tls'].verify) + self.assertIn(cert_dir, kwargs['tls'].cert[0]) + self.assertIn(cert_dir, kwargs['tls'].cert[1]) + finally: + if temp_dir: + shutil.rmtree(temp_dir) + def test_convert_filters(self): tests = [ ({'dangling': True}, '{"dangling": ["true"]}'), From e8af8b376dd9383a1db4185fea5705c2cb2d8add Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 26 Aug 2015 17:02:24 -0700 Subject: [PATCH 2/2] Use cross-platform home directory path --- docker/utils/utils.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index d659ca49..8dc726b3 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -114,7 +114,7 @@ def exclude_paths(root, patterns, dockerfile=None): components = p.split('/') paths += [ '/'.join(components[:end]) - for end in range(1, len(components)+1) + for end in range(1, len(components) + 1) ] return set(paths) @@ -369,8 +369,7 @@ def kwargs_from_env(ssl_version=None, assert_hostname=None): if tls_verify else host) if tls_verify and not cert_path: - if 'HOME' in os.environ: - cert_path = os.path.join(os.environ['HOME'], '.docker') + cert_path = os.path.join(os.path.expanduser('~'), '.docker') if tls_verify and cert_path: params['tls'] = tls.TLSConfig(