Make default tls host work

Signed-off-by: Lei Jitang <leijitang@huawei.com>
This commit is contained in:
Lei Jitang
2015-10-19 21:17:37 +08:00
committed by Tibor Vass
parent 42abfab3a7
commit f4dc974b2a
8 changed files with 152 additions and 63 deletions
+34
View File
@@ -1747,3 +1747,37 @@ func (s *DockerDaemonSuite) TestDaemonStartWithoutHost(c *check.C) {
}()
c.Assert(s.d.Start(), check.IsNil)
}
func (s *DockerDaemonSuite) TestDaemonStartWithDefalutTlsHost(c *check.C) {
s.d.useDefaultTLSHost = true
defer func() {
s.d.useDefaultTLSHost = false
}()
if err := s.d.Start(
"--tlsverify",
"--tlscacert", "fixtures/https/ca.pem",
"--tlscert", "fixtures/https/server-cert.pem",
"--tlskey", "fixtures/https/server-key.pem"); err != nil {
c.Fatalf("Could not start daemon: %v", err)
}
// The client with --tlsverify should also use default host localhost:2376
tmpHost := os.Getenv("DOCKER_HOST")
defer func() {
os.Setenv("DOCKER_HOST", tmpHost)
}()
os.Setenv("DOCKER_HOST", "")
out, _ := dockerCmd(
c,
"--tlsverify",
"--tlscacert", "fixtures/https/ca.pem",
"--tlscert", "fixtures/https/client-cert.pem",
"--tlskey", "fixtures/https/client-key.pem",
"version",
)
if !strings.Contains(out, "Server") {
c.Fatalf("docker version should return information of server side")
}
}
+81 -40
View File
@@ -26,7 +26,9 @@ import (
"github.com/docker/docker/pkg/httputils"
"github.com/docker/docker/pkg/integration"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/sockets"
"github.com/docker/docker/pkg/stringutils"
"github.com/docker/docker/pkg/tlsconfig"
"github.com/go-check/check"
)
@@ -37,19 +39,26 @@ type Daemon struct {
Command string
GlobalFlags []string
id string
c *check.C
logFile *os.File
folder string
root string
stdin io.WriteCloser
stdout, stderr io.ReadCloser
cmd *exec.Cmd
storageDriver string
execDriver string
wait chan error
userlandProxy bool
useDefaultHost bool
id string
c *check.C
logFile *os.File
folder string
root string
stdin io.WriteCloser
stdout, stderr io.ReadCloser
cmd *exec.Cmd
storageDriver string
execDriver string
wait chan error
userlandProxy bool
useDefaultHost bool
useDefaultTLSHost bool
}
type clientConfig struct {
transport *http.Transport
scheme string
addr string
}
// NewDaemon returns a Daemon instance to be used for testing.
@@ -92,6 +101,50 @@ func NewDaemon(c *check.C) *Daemon {
}
}
func (d *Daemon) getClientConfig() (*clientConfig, error) {
var (
transport *http.Transport
scheme string
addr string
proto string
)
if d.useDefaultTLSHost {
option := &tlsconfig.Options{
CAFile: "fixtures/https/ca.pem",
CertFile: "fixtures/https/client-cert.pem",
KeyFile: "fixtures/https/client-key.pem",
}
tlsConfig, err := tlsconfig.Client(*option)
if err != nil {
return nil, err
}
transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
addr = fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort)
scheme = "https"
proto = "tcp"
} else if d.useDefaultHost {
addr = opts.DefaultUnixSocket
proto = "unix"
scheme = "http"
transport = &http.Transport{}
} else {
addr = filepath.Join(d.folder, "docker.sock")
proto = "unix"
scheme = "http"
transport = &http.Transport{}
}
sockets.ConfigureTCPTransport(transport, proto, addr)
return &clientConfig{
transport: transport,
scheme: scheme,
addr: addr,
}, nil
}
// Start will start the daemon and return once it is ready to receive requests.
// You can specify additional daemon flags.
func (d *Daemon) Start(arg ...string) error {
@@ -106,7 +159,7 @@ func (d *Daemon) Start(arg ...string) error {
"--pidfile", fmt.Sprintf("%s/docker.pid", d.folder),
fmt.Sprintf("--userland-proxy=%t", d.userlandProxy),
)
if !d.useDefaultHost {
if !(d.useDefaultHost || d.useDefaultTLSHost) {
args = append(args, []string{"--host", d.sock()}...)
}
if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
@@ -170,27 +223,21 @@ func (d *Daemon) Start(arg ...string) error {
case <-time.After(2 * time.Second):
return fmt.Errorf("[%s] timeout: daemon does not respond", d.id)
case <-tick:
var (
c net.Conn
err error
)
if d.useDefaultHost {
c, err = net.Dial("unix", "/var/run/docker.sock")
} else {
c, err = net.Dial("unix", filepath.Join(d.folder, "docker.sock"))
}
clientConfig, err := d.getClientConfig()
if err != nil {
continue
return err
}
client := httputil.NewClientConn(c, nil)
defer client.Close()
client := &http.Client{
Transport: clientConfig.transport,
}
req, err := http.NewRequest("GET", "/_ping", nil)
if err != nil {
d.c.Fatalf("[%s] could not create new request: %v", d.id, err)
}
req.URL.Host = clientConfig.addr
req.URL.Scheme = clientConfig.scheme
resp, err := client.Do(req)
if err != nil {
continue
@@ -301,34 +348,28 @@ func (d *Daemon) Restart(arg ...string) error {
func (d *Daemon) queryRootDir() (string, error) {
// update daemon root by asking /info endpoint (to support user
// namespaced daemon with root remapped uid.gid directory)
var (
conn net.Conn
err error
)
if d.useDefaultHost {
conn, err = net.Dial("unix", "/var/run/docker.sock")
} else {
conn, err = net.Dial("unix", filepath.Join(d.folder, "docker.sock"))
}
clientConfig, err := d.getClientConfig()
if err != nil {
return "", err
}
client := httputil.NewClientConn(conn, nil)
client := &http.Client{
Transport: clientConfig.transport,
}
req, err := http.NewRequest("GET", "/info", nil)
if err != nil {
client.Close()
return "", err
}
req.Header.Set("Content-Type", "application/json")
req.URL.Host = clientConfig.addr
req.URL.Scheme = clientConfig.scheme
resp, err := client.Do(req)
if err != nil {
client.Close()
return "", err
}
body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
defer client.Close()
return resp.Body.Close()
})