77 "log"
88 "os"
99 "os/exec"
10+ "path/filepath"
1011 "strings"
1112 "sync"
1213 "time"
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+
171182func 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
362373func 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}
0 commit comments