Skip to content

Commit a7f0814

Browse files
committed
pkg/cmd: support HTTPS Docker host
1 parent c9c6fbe commit a7f0814

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

pkg/cmd/main.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"fmt"
1111
"io"
1212
"net"
13+
"os"
1314
"os/exec"
15+
"path/filepath"
1416
"regexp"
1517
"strconv"
1618
"strings"
@@ -26,6 +28,8 @@ import (
2628
"github.com/docker/cli/cli/command"
2729
dockerconfig "github.com/docker/cli/cli/config"
2830
"github.com/docker/cli/cli/flags"
31+
"github.com/docker/docker/client"
32+
"github.com/docker/go-connections/tlsconfig"
2933
"github.com/sirupsen/logrus"
3034

3135
"github.com/adevinta/vulcan-local/pkg/checktypes"
@@ -88,7 +92,7 @@ func Run(cfg *config.Config, log *logrus.Logger) (int, error) {
8892
}
8993
}
9094

91-
cli, err := command.NewAPIClientFromFlags(flags.NewClientOptions(), dockerconfig.LoadDefaultConfigFile(io.Discard))
95+
cli, err := command.NewAPIClientFromFlags(defaultClientOptions(), dockerconfig.LoadDefaultConfigFile(io.Discard))
9296
if err != nil {
9397
return config.ErrorExitCode, fmt.Errorf("unable to get Docker client: %w", err)
9498
}
@@ -222,6 +226,30 @@ func Run(cfg *config.Config, log *logrus.Logger) (int, error) {
222226
return reportCode, nil
223227
}
224228

229+
func defaultClientOptions() *flags.ClientOptions {
230+
tlsVerify := os.Getenv(client.EnvTLSVerify) != ""
231+
232+
var tlsopts *tlsconfig.Options
233+
if tlsVerify {
234+
certPath := os.Getenv(client.EnvOverrideCertPath)
235+
if certPath == "" {
236+
certPath = dockerconfig.Dir()
237+
}
238+
tlsopts = &tlsconfig.Options{
239+
CAFile: filepath.Join(certPath, flags.DefaultCaFile),
240+
CertFile: filepath.Join(certPath, flags.DefaultCertFile),
241+
KeyFile: filepath.Join(certPath, flags.DefaultKeyFile),
242+
}
243+
}
244+
245+
opts := &flags.ClientOptions{
246+
TLS: tlsVerify,
247+
TLSVerify: tlsVerify,
248+
TLSOptions: tlsopts,
249+
}
250+
return opts
251+
}
252+
225253
func upsertEnv(envs []string, name, newValue string) []string {
226254
for i, e := range envs {
227255
if strings.HasPrefix(e, name+"=") {

0 commit comments

Comments
 (0)