Skip to content

Commit 637f8be

Browse files
committed
Support DOCKER_CERT_PATH and DOCKER_TLS_VERIFY env vars
Attempts to follow the same conventions that docker uses.
1 parent 35334b0 commit 637f8be

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

docker-gen.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"os"
99
"os/exec"
10+
"path/filepath"
1011
"strings"
1112
"sync"
1213
"time"
@@ -33,6 +34,7 @@ var (
3334
tlsKey string
3435
tlsCaCert string
3536
tlsVerify bool
37+
tlsCertPath string
3638
wg sync.WaitGroup
3739
)
3840

@@ -168,13 +170,22 @@ Environment Variables:
168170
`)
169171
}
170172

173+
func tlsEnabled() bool {
174+
for _, v := range []string{tlsCert, tlsCaCert, tlsKey} {
175+
if e, err := pathExists(v); e && err == nil {
176+
return true
177+
}
178+
}
179+
return false
180+
}
181+
171182
func NewDockerClient(endpoint string) (*docker.Client, error) {
172183
if strings.HasPrefix(endpoint, "unix:") {
173184
return docker.NewClient(endpoint)
174-
} else if tlsVerify || tlsCert != "" || tlsKey != "" || tlsCaCert != "" {
185+
} else if tlsVerify || tlsEnabled() {
175186
if tlsVerify {
176-
if tlsCaCert == "" {
177-
return nil, errors.New("TLS verification was requested, but no -tlscacert was provided")
187+
if e, err := pathExists(tlsCaCert); !e || err != nil {
188+
return nil, errors.New("TLS verification was requested, but CA cert does not exist")
178189
}
179190
}
180191

@@ -360,6 +371,11 @@ func generateFromEvents(client *docker.Client, configs ConfigFile) {
360371
}
361372

362373
func initFlags() {
374+
375+
certPath := filepath.Join(os.Getenv("DOCKER_CERT_PATH"))
376+
if certPath == "" {
377+
certPath = filepath.Join(os.Getenv("HOME"), ".docker")
378+
}
363379
flag.BoolVar(&version, "version", false, "show version")
364380
flag.BoolVar(&watch, "watch", false, "watch for container changes")
365381
flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports")
@@ -372,10 +388,11 @@ func initFlags() {
372388
flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.")
373389
flag.IntVar(&interval, "interval", 0, "notify command interval (secs)")
374390
flag.StringVar(&endpoint, "endpoint", "", "docker api endpoint (tcp|unix://..). Default unix:///var/run/docker.sock")
375-
flag.StringVar(&tlsCert, "tlscert", "", "path to TLS client certificate file")
376-
flag.StringVar(&tlsKey, "tlskey", "", "path to TLS client key file")
377-
flag.StringVar(&tlsCaCert, "tlscacert", "", "path to TLS CA certificate file")
378-
flag.BoolVar(&tlsVerify, "tlsverify", false, "verify docker daemon's TLS certicate")
391+
flag.StringVar(&tlsCert, "tlscert", filepath.Join(certPath, "cert.pem"), "path to TLS client certificate file")
392+
flag.StringVar(&tlsKey, "tlskey", filepath.Join(certPath, "key.pem"), "path to TLS client key file")
393+
flag.StringVar(&tlsCaCert, "tlscacert", filepath.Join(certPath, "ca.pem"), "path to TLS CA certificate file")
394+
flag.BoolVar(&tlsVerify, "tlsverify", os.Getenv("DOCKER_TLS_VERIFY") != "", "verify docker daemon's TLS certicate")
395+
379396
flag.Usage = usage
380397
flag.Parse()
381398
}

utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ func splitKeyValueSlice(in []string) map[string]string {
3838
return env
3939

4040
}
41+
42+
// pathExists returns whether the given file or directory exists or not
43+
func pathExists(path string) (bool, error) {
44+
_, err := os.Stat(path)
45+
if err == nil {
46+
return true, nil
47+
}
48+
if os.IsNotExist(err) {
49+
return false, nil
50+
}
51+
return false, err
52+
}

0 commit comments

Comments
 (0)