7
7
"log"
8
8
"os"
9
9
"os/exec"
10
+ "path/filepath"
10
11
"strings"
11
12
"sync"
12
13
"time"
33
34
tlsKey string
34
35
tlsCaCert string
35
36
tlsVerify bool
37
+ tlsCertPath string
36
38
wg sync.WaitGroup
37
39
)
38
40
@@ -168,13 +170,22 @@ Environment Variables:
168
170
` )
169
171
}
170
172
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
+
171
182
func NewDockerClient (endpoint string ) (* docker.Client , error ) {
172
183
if strings .HasPrefix (endpoint , "unix:" ) {
173
184
return docker .NewClient (endpoint )
174
- } else if tlsVerify || tlsCert != "" || tlsKey != "" || tlsCaCert != "" {
185
+ } else if tlsVerify || tlsEnabled () {
175
186
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 " )
178
189
}
179
190
}
180
191
@@ -360,6 +371,11 @@ func generateFromEvents(client *docker.Client, configs ConfigFile) {
360
371
}
361
372
362
373
func initFlags () {
374
+
375
+ certPath := filepath .Join (os .Getenv ("DOCKER_CERT_PATH" ))
376
+ if certPath == "" {
377
+ certPath = filepath .Join (os .Getenv ("HOME" ), ".docker" )
378
+ }
363
379
flag .BoolVar (& version , "version" , false , "show version" )
364
380
flag .BoolVar (& watch , "watch" , false , "watch for container changes" )
365
381
flag .BoolVar (& onlyExposed , "only-exposed" , false , "only include containers with exposed ports" )
@@ -372,10 +388,11 @@ func initFlags() {
372
388
flag .Var (& configFiles , "config" , "config files with template directives. Config files will be merged if this option is specified multiple times." )
373
389
flag .IntVar (& interval , "interval" , 0 , "notify command interval (secs)" )
374
390
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
+
379
396
flag .Usage = usage
380
397
flag .Parse ()
381
398
}
0 commit comments