Skip to content

Commit 69cd38a

Browse files
committed
initial take on adding support for tls auth with client certificates
1 parent 7e105f5 commit 69cd38a

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

docker/client.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,18 @@ def is_server_error(self):
6969

7070

7171
class Client(requests.Session):
72-
def __init__(self, base_url=None, version=DEFAULT_DOCKER_API_VERSION,
73-
timeout=DEFAULT_TIMEOUT_SECONDS):
72+
def __init__(self,
73+
base_url=None,
74+
version=DEFAULT_DOCKER_API_VERSION,
75+
timeout=DEFAULT_TIMEOUT_SECONDS,
76+
tls=False,
77+
tls_cert=None,
78+
tls_key=None):
7479
super(Client, self).__init__()
80+
if tls and not (tls_cert and tls_key):
81+
raise RuntimeError('tls_key and tls_cert are required.')
82+
if tls and not base_url.startswith('https'):
83+
raise RuntimeError('TLS: base_url has to start with https://')
7584
if base_url is None:
7685
base_url = "http+unix://var/run/docker.sock"
7786
if 'unix:///' in base_url:
@@ -87,7 +96,12 @@ def __init__(self, base_url=None, version=DEFAULT_DOCKER_API_VERSION,
8796
self._timeout = timeout
8897
self._auth_configs = auth.load_config()
8998

90-
self.mount('http+unix://', unixconn.UnixAdapter(base_url, timeout))
99+
if tls:
100+
self.cert = (tls_cert, tls_key)
101+
self.verify = False # We assume the server.crt will we self signed
102+
self.mount('https://', requests.adapters.HTTPAdapter())
103+
else:
104+
self.mount('http+unix://', unixconn.UnixAdapter(base_url, timeout))
91105

92106
def _set_request_timeout(self, kwargs):
93107
"""Prepare the kwargs for an HTTP request by inserting the timeout

0 commit comments

Comments
 (0)