Skip to content

Commit 6fca166

Browse files
Add support for Docker TLS env vars
1 parent 570c255 commit 6fca166

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/docker/cli v20.10.17+incompatible
1414
github.com/docker/distribution v2.8.1+incompatible
1515
github.com/docker/docker v20.10.17+incompatible
16+
github.com/docker/go-connections v0.4.0
1617
github.com/docker/go-units v0.4.0
1718
github.com/getsentry/sentry-go v0.13.0
1819
github.com/hashicorp/go-version v1.2.0
@@ -48,7 +49,6 @@ require (
4849
github.com/davecgh/go-spew v1.1.1 // indirect
4950
github.com/docker/docker-credential-helpers v0.6.4 // indirect
5051
github.com/docker/go v1.5.1-1.0.20160303222718-d30aec9fd63c // indirect
51-
github.com/docker/go-connections v0.4.0 // indirect
5252
github.com/docker/go-metrics v0.0.1 // indirect
5353
github.com/fatih/color v1.13.0 // indirect
5454
github.com/felixge/httpsnoop v1.0.2 // indirect

pkg/cmd/build/build.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"github.com/depot/cli/pkg/config"
99
"github.com/depot/cli/pkg/project"
1010
"github.com/docker/cli/cli"
11-
"github.com/docker/cli/cli/command"
12-
cliflags "github.com/docker/cli/cli/flags"
1311
"github.com/pkg/errors"
1412
"github.com/spf13/cobra"
1513

@@ -51,13 +49,7 @@ func NewCmdBuild() *cobra.Command {
5149
return fmt.Errorf("missing API token, please run `depot login`")
5250
}
5351

54-
dockerCli, err := command.NewDockerCli()
55-
if err != nil {
56-
fmt.Fprintln(os.Stderr, err)
57-
os.Exit(1)
58-
}
59-
opts := cliflags.NewClientOptions()
60-
err = dockerCli.Initialize(opts)
52+
dockerCli, err := newDockerCLI()
6153
if err != nil {
6254
fmt.Fprintln(os.Stderr, err)
6355
os.Exit(1)

pkg/cmd/build/dockerclient.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package build
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/docker/cli/cli/command"
8+
"github.com/docker/cli/cli/config"
9+
cliflags "github.com/docker/cli/cli/flags"
10+
"github.com/docker/docker/client"
11+
"github.com/docker/go-connections/tlsconfig"
12+
)
13+
14+
// Copied from github.com/docker/cli/cli/flags/common.go
15+
16+
var (
17+
dockerCertPath = os.Getenv(client.EnvOverrideCertPath)
18+
dockerTLSVerify = os.Getenv(client.EnvTLSVerify) != ""
19+
dockerTLS = os.Getenv("DOCKER_TLS") != ""
20+
)
21+
22+
func newDockerCLI() (*command.DockerCli, error) {
23+
dockerCli, err := command.NewDockerCli()
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
// Construct options with TLS
29+
opts := cliflags.NewClientOptions()
30+
if dockerCertPath == "" {
31+
dockerCertPath = config.Dir()
32+
}
33+
34+
opts.Common.TLS = dockerTLS
35+
opts.Common.TLSVerify = dockerTLSVerify
36+
if opts.Common.TLSVerify {
37+
opts.Common.TLS = true
38+
}
39+
if opts.Common.TLS {
40+
opts.Common.TLSOptions = &tlsconfig.Options{
41+
CAFile: filepath.Join(dockerCertPath, cliflags.DefaultCaFile),
42+
CertFile: filepath.Join(dockerCertPath, cliflags.DefaultCertFile),
43+
KeyFile: filepath.Join(dockerCertPath, cliflags.DefaultKeyFile),
44+
InsecureSkipVerify: !opts.Common.TLSVerify,
45+
}
46+
// Reset CertFile and KeyFile to empty string if the respective default files were not found.
47+
if _, err := os.Stat(opts.Common.TLSOptions.CertFile); os.IsNotExist(err) {
48+
opts.Common.TLSOptions.CertFile = ""
49+
}
50+
if _, err := os.Stat(opts.Common.TLSOptions.KeyFile); os.IsNotExist(err) {
51+
opts.Common.TLSOptions.KeyFile = ""
52+
}
53+
}
54+
55+
err = dockerCli.Initialize(opts)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
return dockerCli, err
61+
}

0 commit comments

Comments
 (0)