support command line parameters for tls

This commit is contained in:
Nate Jones 2015-05-21 14:33:45 -07:00
parent 56ab2b992b
commit 8053390d93
3 changed files with 27 additions and 10 deletions

5
cli.go
View file

@ -7,7 +7,10 @@ import (
) )
type GlobalOptions struct { type GlobalOptions struct {
// no options yet TLSCaCert string `long:"tlscacert" value-name:"~/.docker/ca.pem" description:"Trust certs signed only by this CA"`
TLSCert string `long:"tlscert" value-name:"~/.docker/cert.pem" description:"Path to TLS certificate file"`
TLSKey string `long:"tlskey" value-name:"~/.docker/key.pem" description:"Path to TLS key file"`
TLSVerify bool `long:"tlsverify" description:"Use TLS and verify the remote"`
} }
var globalOptions GlobalOptions var globalOptions GlobalOptions

View file

@ -53,6 +53,9 @@ func (x *ImagesCommand) Execute(args []string) error {
} else { } else {
client, err := connect() client, err := connect()
if err != nil {
return err
}
clientImages, err := client.ListImages(docker.ListImagesOptions{All: true}) clientImages, err := client.ListImages(docker.ListImagesOptions{All: true})
if err != nil { if err != nil {

29
util.go
View file

@ -1,10 +1,11 @@
package main package main
import ( import (
"github.com/fsouza/go-dockerclient" "errors"
"os" "os"
"path" "path"
"github.com/fsouza/go-dockerclient"
) )
func connect() (*docker.Client, error) { func connect() (*docker.Client, error) {
@ -17,13 +18,23 @@ func connect() (*docker.Client, error) {
var client *docker.Client var client *docker.Client
var err error var err error
if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); len(dockerCertPath) > 0 { dockerTlsVerifyEnv := os.Getenv("DOCKER_TLS_VERIFY")
cert := path.Join(dockerCertPath, "cert.pem") if dockerTlsVerifyEnv == "1" || globalOptions.TLSVerify {
key := path.Join(dockerCertPath, "key.pem") if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); len(dockerCertPath) > 0 {
ca := path.Join(dockerCertPath, "ca.pem") cert := path.Join(dockerCertPath, "cert.pem")
client, err = docker.NewTLSClient(endpoint, cert, key, ca) key := path.Join(dockerCertPath, "key.pem")
if err != nil { ca := path.Join(dockerCertPath, "ca.pem")
return nil, err client, err = docker.NewTLSClient(endpoint, cert, key, ca)
if err != nil {
return nil, err
}
} else if len(globalOptions.TLSCert) > 0 && len(globalOptions.TLSKey) > 0 && len(globalOptions.TLSCaCert) > 0 {
client, err = docker.NewTLSClient(endpoint, globalOptions.TLSCert, globalOptions.TLSKey, globalOptions.TLSCaCert)
if err != nil {
return nil, err
}
} else {
return nil, errors.New("TLS Verification requested but certs not specified")
} }
} else { } else {
client, err = docker.NewClient(endpoint) client, err = docker.NewClient(endpoint)