Skip to content

Commit ff24fad

Browse files
author
Matthew Fisher
committed
Merge pull request #2352 from johanneswuerbach/patch-6
fix(tests): Allow TLS
2 parents c5241f1 + 7a4ac38 commit ff24fad

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

tests/dockercli/dockercli.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ package dockercli
44

55
import (
66
"bufio"
7+
"crypto/tls"
8+
"log"
79
"fmt"
810
"io"
911
"net"
1012
"net/http"
1113
"net/url"
1214
"os"
15+
"path/filepath"
1316
"strings"
1417
"testing"
1518
"time"
@@ -18,6 +21,11 @@ import (
1821
"github.com/docker/docker/api/client"
1922
)
2023

24+
const (
25+
defaultKeyFile = "key.pem"
26+
defaultCertFile = "cert.pem"
27+
)
28+
2129
// CloseWrap ensures that an io.Writer is closed.
2230
func CloseWrap(args ...io.Closer) error {
2331
e := false
@@ -88,7 +96,33 @@ func NewClient() (
8896
cli *client.DockerCli, stdout *io.PipeReader, stdoutPipe *io.PipeWriter) {
8997
proto, addr, _ := DockerHost()
9098
stdout, stdoutPipe = io.Pipe()
91-
cli = client.NewDockerCli(nil, stdoutPipe, nil, nil, proto, addr, nil)
99+
100+
dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
101+
// Boot2docker use TLS per default, Jenkins not
102+
if dockerCertPath != "" {
103+
var (
104+
tlsConfig tls.Config
105+
)
106+
tlsConfig.InsecureSkipVerify = true
107+
108+
flCert := filepath.Join(dockerCertPath, defaultCertFile)
109+
flKey := filepath.Join(dockerCertPath, defaultKeyFile)
110+
111+
_, errCert := os.Stat(flCert)
112+
_, errKey := os.Stat(flKey)
113+
if errCert == nil && errKey == nil {
114+
cert, err := tls.LoadX509KeyPair(flCert, flKey)
115+
if err != nil {
116+
log.Fatalf("Couldn't load X509 key pair: %s. Key encrypted?", err)
117+
}
118+
tlsConfig.Certificates = []tls.Certificate{cert}
119+
}
120+
// Avoid fallback to SSL protocols < TLS1.0
121+
tlsConfig.MinVersion = tls.VersionTLS10
122+
cli = client.NewDockerCli(nil, stdoutPipe, nil, nil, proto, addr, &tlsConfig)
123+
} else {
124+
cli = client.NewDockerCli(nil, stdoutPipe, nil, nil, proto, addr, nil)
125+
}
92126
return
93127
}
94128

0 commit comments

Comments
 (0)