add DockertlsConnection, a subclass of KeyCertificateConnection fix 
DockerContainerDriver connect either with http or tls pass key_file, cert_file 
through function _ex_connection_class_kwargs


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/2272c93a
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/2272c93a
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/2272c93a

Branch: refs/heads/trunk
Commit: 2272c93ad0b6947554daa6ac36ae9b237ff7a77b
Parents: 48d8063
Author: johnnyWalnut <ikaryd...@gmail.com>
Authored: Fri Jun 2 13:54:10 2017 +0200
Committer: Anthony Shaw <anthonys...@apache.org>
Committed: Fri Aug 11 14:43:39 2017 +1000

----------------------------------------------------------------------
 libcloud/container/drivers/docker.py | 68 ++++++++++++++++++++++++++++---
 1 file changed, 63 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/2272c93a/libcloud/container/drivers/docker.py
----------------------------------------------------------------------
diff --git a/libcloud/container/drivers/docker.py 
b/libcloud/container/drivers/docker.py
index 8fce78f..7b9d28b 100644
--- a/libcloud/container/drivers/docker.py
+++ b/libcloud/container/drivers/docker.py
@@ -17,6 +17,7 @@ import base64
 import datetime
 import shlex
 import re
+import os
 
 try:
     import simplejson as json
@@ -27,6 +28,7 @@ from libcloud.utils.py3 import httplib
 from libcloud.utils.py3 import b
 
 from libcloud.common.base import JsonResponse, ConnectionUserAndKey
+from libcloud.common.base import KeyCertificateConnection
 from libcloud.common.types import InvalidCredsError
 
 from libcloud.container.base import (Container, ContainerDriver,
@@ -113,6 +115,46 @@ class DockerConnection(ConnectionUserAndKey):
         return headers
 
 
+class DockertlsConnection(KeyCertificateConnection):
+
+    responseCls = DockerResponse
+
+    def __init__(self, key, secret, secure=True,
+                 host='localhost',
+                 port=4243, ca_cert='', key_file='', cert_file='', **kwargs):
+
+        super(DockertlsConnection, self).__init__(key_file=key_file,
+                                                  cert_file=cert_file,
+                                                  secure=secure, host=host,
+                                                  port=port, url=None,
+                                                  proxy_url=None,
+                                                  timeout=None, backoff=None,
+                                                  retry_delay=None)
+        if key_file:
+            keypath = os.path.expanduser(key_file)
+            is_file_path = os.path.exists(keypath) and os.path.isfile(keypath)
+            if not is_file_path:
+                raise InvalidCredsError(
+                    'You need an key PEM file to authenticate with '
+                    'Docker tls. This can be found in the server.'
+                )
+            self.key_file = key_file
+
+            certpath = os.path.expanduser(cert_file)
+            is_file_path = os.path.exists(certpath) and 
os.path.isfile(certpath)
+            if not is_file_path:
+                raise InvalidCredsError(
+                    'You need an certificate PEM file to authenticate with '
+                    'Docker tls. This can be found in the server.'
+                )
+            self.cert_file = cert_file
+
+    def add_default_headers(self, headers):
+
+        headers['Content-Type'] = 'application/json'
+        return headers
+
+
 class DockerContainerDriver(ContainerDriver):
     """
     Docker container driver class.
@@ -164,11 +206,12 @@ class DockerContainerDriver(ContainerDriver):
 
         :return: ``None``
         """
-        super(DockerContainerDriver, self).__init__(key=key, secret=secret,
-                                                    secure=secure, host=host,
-                                                    port=port,
-                                                    key_file=key_file,
-                                                    cert_file=cert_file)
+        if key_file:
+            self.connectionCls = DockertlsConnection
+            self.key_file = key_file
+            self.cert_file = cert_file
+            secure = True
+
         if host.startswith('https://'):
             secure = True
 
@@ -178,6 +221,12 @@ class DockerContainerDriver(ContainerDriver):
             if host.startswith(prefix):
                 host = host.strip(prefix)
 
+        super(DockerContainerDriver, self).__init__(key=key, secret=secret,
+                                                    secure=secure, host=host,
+                                                    port=port,
+                                                    key_file=key_file,
+                                                    cert_file=cert_file)
+
         if key_file or cert_file:
             # docker tls authentication-
             # https://docs.docker.com/articles/https/
@@ -194,9 +243,18 @@ class DockerContainerDriver(ContainerDriver):
         else:
             self.connection.secure = secure
 
+        self.connection.secure = secure
         self.connection.host = host
         self.connection.port = port
 
+    def _ex_connection_class_kwargs(self):
+        kwargs = {}
+        if hasattr(self, 'key_file'):
+            kwargs['key_file'] = self.key_file
+        if hasattr(self, 'cert_file'):
+            kwargs['cert_file'] = self.cert_file
+        return kwargs
+
     def install_image(self, path):
         """
         Install a container image from a remote path.

Reply via email to