Skip to content

Commit 8393dbc

Browse files
committed
Improved TLSConfig API to be less obscure / more pythonic. Also improved / amended docs
1 parent db454f0 commit 8393dbc

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

README.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,31 +355,49 @@ http://docs.docker.com/articles/https/ first.*
355355
client = docker.Client(base_url='<https_url>', tls=True)
356356
```
357357

358+
Equivalent CLI options: `docker --tls ...`
359+
360+
If you want to use TLS but don't want to verify the server certificate
361+
(for example when testing with a self-signed certificate):
362+
363+
```python
364+
tls_config = docker.tls.TLSConfig(verify=False)
365+
client = docker.Client(base_url='<https_url>', tls=tls_config)
366+
```
367+
358368
* Authenticate server based on given CA
359369

360370
```python
361-
tls_config = docker.tls.TLSConfig(
362-
False, tls_verify=True, tls_ca_cert='/path/to/ca.pem')
371+
tls_config = docker.tls.TLSConfig(server_cacert='/path/to/ca.pem')
363372
client = docker.Client(base_url='<https_url>', tls=tls_config)
364373
```
365374

375+
Equivalent CLI options: `docker --tlsverify --tlscacert /path/to/ca.pem ...`
376+
366377
* Authenticate with client certificate, do not authenticate server
367378
based on given CA
368379

369380
```python
370381
tls_config = docker.tls.TLSConfig(
371-
True, tls_cert='/path/to/client-cert.pem',
372-
tls_key='/path/to/client-key.pem'
382+
True, client_cert=('/path/to/client-cert.pem', '/path/to/client-key.pem')
373383
)
374384
client = docker.Client(base_url='<https_url>', tls=tls_config)
375385
```
376386

387+
Equivalent CLI options:
388+
`docker --tls --tlscert /path/to/client-cert.pem
389+
--tlskey /path/to/client-key.pem ...`
390+
377391
* Authenticate with client certificate, authenticate server based on given CA
378392

379393
```python
380394
tls_config = docker.tls.TLSConfig(
381-
False, tls_cert='/path/to/client-cert.pem',
382-
tls_key='/path/to/client-key.pem', tls_ca_cert='/path/to/ca.pem'
395+
client_cert=('/path/to/client-cert.pem', '/path/to/client-key.pem'),
396+
server_cacert='/path/to/ca.pem'
383397
)
384398
client = docker.Client(base_url='<https_url>', tls=tls_config)
385399
```
400+
401+
Equivalent CLI options:
402+
`docker --tlsverify --tlscert /path/to/client-cert.pem
403+
--tlskey /path/to/client-key.pem --tlscacert /path/to/ca.pem ...`

docker/tls.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class TLSConfig(object):
99
verify = None
1010
ssl_version = None
1111

12-
def __init__(self, tls, tls_cert=None, tls_key=None, tls_verify=None,
13-
tls_ca_cert=None, ssl_version=None):
12+
def __init__(self, client_cert=None, server_cacert=None, verify=None,
13+
ssl_version=None):
1414
# Argument compatibility/mapping with
1515
# http://docs.docker.com/examples/https/
1616
# This diverges from the Docker CLI in that users can specify 'tls'
@@ -25,27 +25,35 @@ def __init__(self, tls, tls_cert=None, tls_key=None, tls_verify=None,
2525
# In either case, Alert the user when both are expected, but any are
2626
# missing.
2727

28-
if tls_cert or tls_key:
28+
if client_cert:
29+
try:
30+
tls_cert, tls_key = client_cert
31+
except ValueError:
32+
raise errors.TLSParameterError(
33+
'client_config must be a tuple of'
34+
' (client certificate, key file)'
35+
)
36+
2937
if not (tls_cert and tls_key) or (not os.path.isfile(tls_cert) or
3038
not os.path.isfile(tls_key)):
3139
raise errors.TLSParameterError(
32-
'Client certificate must provide certificate and key files'
33-
' through tls_cert and tls_key params respectively'
40+
'Path to a certificate and key files must be provided'
41+
' through the client_config param'
3442
)
3543
self.cert = (tls_cert, tls_key)
3644

3745
# Either set verify to True (public/default CA checks) or to the
3846
# path of a CA Cert file.
39-
if tls_verify is not None:
40-
if not tls_ca_cert:
41-
self.verify = tls_verify
42-
elif os.path.isfile(tls_ca_cert):
43-
if not tls_verify:
47+
if verify is not None:
48+
if not server_cacert:
49+
self.verify = verify
50+
elif os.path.isfile(server_cacert):
51+
if not verify:
4452
raise errors.TLSParameterError(
45-
'tls_verify can not be False when a CA cert is'
53+
'verify can not be False when a CA cert is'
4654
' provided.'
4755
)
48-
self.verify = tls_ca_cert
56+
self.verify = server_cacert
4957
else:
5058
raise errors.TLSParameterError(
5159
'Invalid CA certificate provided for `tls_ca_cert`.'

0 commit comments

Comments
 (0)