Skip to content

Commit a07c836

Browse files
committed
Merge pull request #537 from aanand/tls
TLS support
2 parents 1820306 + 0f58b9f commit a07c836

16 files changed

Lines changed: 759 additions & 27 deletions

File tree

docs/cli.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ By default if there are existing containers for a service, `fig up` will stop an
101101

102102
Several environment variables can be used to configure Fig's behaviour.
103103

104+
Variables starting with `DOCKER_` are the same as those used to configure the Docker command-line client. If you're using boot2docker, `$(boot2docker shellinit)` will set them to their correct values.
105+
104106
### FIG\_PROJECT\_NAME
105107

106108
Set the project name, which is prepended to the name of every container started by Fig. Defaults to the `basename` of the current working directory.
@@ -112,3 +114,11 @@ Set the path to the `fig.yml` to use. Defaults to `fig.yml` in the current worki
112114
### DOCKER\_HOST
113115

114116
Set the URL to the docker daemon. Defaults to `unix:///var/run/docker.sock`, as with the docker client.
117+
118+
### DOCKER\_TLS\_VERIFY
119+
120+
When set to anything other than an empty string, enables TLS communication with the daemon.
121+
122+
### DOCKER\_CERT\_PATH
123+
124+
Configure the path to the `ca.pem`, `cert.pem` and `key.pem` files used for TLS verification. Defaults to `~/.docker`.

docs/install.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ Installing Fig
88

99
First, install Docker version 1.3 or greater.
1010

11-
If you're on OS X, you can use the [OS X installer](https://docs.docker.com/installation/mac/). You'll also need to set an environment variable to point at the Boot2Docker virtual machine:
11+
If you're on OS X, you can use the [OS X installer](https://docs.docker.com/installation/mac/) to install both Docker and boot2docker. Once boot2docker is running, set the environment variables that'll configure Docker and Fig to talk to it:
1212

13-
$ export DOCKER_HOST=tcp://`boot2docker ip`:2375
13+
$(boot2docker shellinit)
1414

15-
If you want this to persist across shell sessions, you can add it to your `~/.bashrc` file.
15+
To persist the environment variables across shell sessions, you can add that line to your `~/.bashrc` file.
1616

1717
There are also guides for [Ubuntu](https://docs.docker.com/installation/ubuntulinux/) and [other platforms](https://docs.docker.com/installation/) in Docker’s documentation.
1818

fig/cli/command.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import unicode_literals
22
from __future__ import absolute_import
3-
from docker import Client
4-
from requests.exceptions import ConnectionError
3+
from requests.exceptions import ConnectionError, SSLError
54
import errno
65
import logging
76
import os
@@ -12,7 +11,8 @@
1211
from ..project import Project
1312
from ..service import ConfigError
1413
from .docopt_command import DocoptCommand
15-
from .utils import docker_url, call_silently, is_mac, is_ubuntu
14+
from .utils import call_silently, is_mac, is_ubuntu
15+
from .docker_client import docker_client
1616
from . import verbose_proxy
1717
from . import errors
1818
from .. import __version__
@@ -26,6 +26,8 @@ class Command(DocoptCommand):
2626
def dispatch(self, *args, **kwargs):
2727
try:
2828
super(Command, self).dispatch(*args, **kwargs)
29+
except SSLError, e:
30+
raise errors.UserError('SSL error: %s' % e)
2931
except ConnectionError:
3032
if call_silently(['which', 'docker']) != 0:
3133
if is_mac():
@@ -49,7 +51,7 @@ def perform_command(self, options, handler, command_options):
4951
handler(project, command_options)
5052

5153
def get_client(self, verbose=False):
52-
client = Client(docker_url())
54+
client = docker_client()
5355
if verbose:
5456
version_info = six.iteritems(client.version())
5557
log.info("Fig version %s", __version__)

fig/cli/docker_client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from docker import Client
2+
from docker import tls
3+
import ssl
4+
import os
5+
6+
7+
def docker_client():
8+
"""
9+
Returns a docker-py client configured using environment variables
10+
according to the same logic as the official Docker client.
11+
"""
12+
cert_path = os.environ.get('DOCKER_CERT_PATH', '')
13+
if cert_path == '':
14+
cert_path = os.path.join(os.environ.get('HOME'), '.docker')
15+
16+
base_url = os.environ.get('DOCKER_HOST')
17+
tls_config = None
18+
19+
if os.environ.get('DOCKER_TLS_VERIFY', '') != '':
20+
parts = base_url.split('://', 1)
21+
base_url = '%s://%s' % ('https', parts[1])
22+
23+
client_cert = (os.path.join(cert_path, 'cert.pem'), os.path.join(cert_path, 'key.pem'))
24+
ca_cert = os.path.join(cert_path, 'ca.pem')
25+
26+
tls_config = tls.TLSConfig(
27+
ssl_version=ssl.PROTOCOL_TLSv1,
28+
verify=True,
29+
assert_hostname=False,
30+
client_cert=client_cert,
31+
ca_cert=ca_cert,
32+
)
33+
34+
return Client(base_url=base_url, tls=tls_config)

fig/cli/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from operator import attrgetter
88

99
from inspect import getdoc
10-
import dockerpty
10+
from fig.packages import dockerpty
1111

1212
from .. import __version__
1313
from ..project import NoSuchService, ConfigurationError

fig/cli/utils.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ def mkdir(path, permissions=0o700):
6262
return path
6363

6464

65-
def docker_url():
66-
return os.environ.get('DOCKER_HOST')
67-
68-
6965
def split_buffer(reader, separator):
7066
"""
7167
Given a generator which yields strings and a separator string,

fig/packages/__init__.py

Whitespace-only changes.

fig/packages/dockerpty/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# dockerpty.
2+
#
3+
# Copyright 2014 Chris Corbyn <chris@w3style.co.uk>
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from .pty import PseudoTerminal
18+
19+
20+
def start(client, container):
21+
"""
22+
Present the PTY of the container inside the current process.
23+
24+
This is just a wrapper for PseudoTerminal(client, container).start()
25+
"""
26+
27+
PseudoTerminal(client, container).start()

0 commit comments

Comments
 (0)