Browse Source

Trim quotes from TLS flags.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
(cherry picked from commit abe32de6b46825300f612864e6b4c98606a5bb0e)
Signed-off-by: Victor Vieux <vieux@docker.com>
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
Daniel Nephin 8 năm trước cách đây
mục cha
commit
045e9834a5
4 tập tin đã thay đổi với 67 bổ sung10 xóa
  1. 8 4
      cli/flags/common.go
  2. 42 0
      cli/flags/common_test.go
  3. 10 3
      opts/quotedstring.go
  4. 7 3
      opts/quotedstring_test.go

+ 8 - 4
cli/flags/common.go

@@ -59,11 +59,15 @@ func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) {
 
 	// TODO use flag flags.String("identity"}, "i", "", "Path to libtrust key file")
 
-	commonOpts.TLSOptions = &tlsconfig.Options{}
+	commonOpts.TLSOptions = &tlsconfig.Options{
+		CAFile:   filepath.Join(dockerCertPath, DefaultCaFile),
+		CertFile: filepath.Join(dockerCertPath, DefaultCertFile),
+		KeyFile:  filepath.Join(dockerCertPath, DefaultKeyFile),
+	}
 	tlsOptions := commonOpts.TLSOptions
-	flags.StringVar(&tlsOptions.CAFile, "tlscacert", filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
-	flags.StringVar(&tlsOptions.CertFile, "tlscert", filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
-	flags.StringVar(&tlsOptions.KeyFile, "tlskey", filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
+	flags.Var(opts.NewQuotedString(&tlsOptions.CAFile), "tlscacert", "Trust certs signed only by this CA")
+	flags.Var(opts.NewQuotedString(&tlsOptions.CertFile), "tlscert", "Path to TLS certificate file")
+	flags.Var(opts.NewQuotedString(&tlsOptions.KeyFile), "tlskey", "Path to TLS key file")
 
 	hostOpt := opts.NewNamedListOptsRef("hosts", &commonOpts.Hosts, opts.ValidateHost)
 	flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")

+ 42 - 0
cli/flags/common_test.go

@@ -0,0 +1,42 @@
+package flags
+
+import (
+	"path/filepath"
+	"testing"
+
+	"github.com/docker/docker/cliconfig"
+	"github.com/docker/docker/pkg/testutil/assert"
+	"github.com/spf13/pflag"
+)
+
+func TestCommonOptionsInstallFlags(t *testing.T) {
+	flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
+	opts := NewCommonOptions()
+	opts.InstallFlags(flags)
+
+	err := flags.Parse([]string{
+		"--tlscacert=\"/foo/cafile\"",
+		"--tlscert=\"/foo/cert\"",
+		"--tlskey=\"/foo/key\"",
+	})
+	assert.NilError(t, err)
+	assert.Equal(t, opts.TLSOptions.CAFile, "/foo/cafile")
+	assert.Equal(t, opts.TLSOptions.CertFile, "/foo/cert")
+	assert.Equal(t, opts.TLSOptions.KeyFile, "/foo/key")
+}
+
+func defaultPath(filename string) string {
+	return filepath.Join(cliconfig.ConfigDir(), filename)
+}
+
+func TestCommonOptionsInstallFlagsWithDefaults(t *testing.T) {
+	flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
+	opts := NewCommonOptions()
+	opts.InstallFlags(flags)
+
+	err := flags.Parse([]string{})
+	assert.NilError(t, err)
+	assert.Equal(t, opts.TLSOptions.CAFile, defaultPath("ca.pem"))
+	assert.Equal(t, opts.TLSOptions.CertFile, defaultPath("cert.pem"))
+	assert.Equal(t, opts.TLSOptions.KeyFile, defaultPath("key.pem"))
+}

+ 10 - 3
opts/quotedstring.go

@@ -2,11 +2,13 @@ package opts
 
 // QuotedString is a string that may have extra quotes around the value. The
 // quotes are stripped from the value.
-type QuotedString string
+type QuotedString struct {
+	value *string
+}
 
 // Set sets a new value
 func (s *QuotedString) Set(val string) error {
-	*s = QuotedString(trimQuotes(val))
+	*s.value = trimQuotes(val)
 	return nil
 }
 
@@ -16,7 +18,7 @@ func (s *QuotedString) Type() string {
 }
 
 func (s *QuotedString) String() string {
-	return string(*s)
+	return string(*s.value)
 }
 
 func trimQuotes(value string) string {
@@ -28,3 +30,8 @@ func trimQuotes(value string) string {
 	}
 	return value
 }
+
+// NewQuotedString returns a new quoted string option
+func NewQuotedString(value *string) *QuotedString {
+	return &QuotedString{value: value}
+}

+ 7 - 3
opts/quotedstring_test.go

@@ -6,19 +6,23 @@ import (
 )
 
 func TestQuotedStringSetWithQuotes(t *testing.T) {
-	qs := QuotedString("")
+	value := ""
+	qs := NewQuotedString(&value)
 	assert.NilError(t, qs.Set("\"something\""))
 	assert.Equal(t, qs.String(), "something")
+	assert.Equal(t, value, "something")
 }
 
 func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) {
-	qs := QuotedString("")
+	value := ""
+	qs := NewQuotedString(&value)
 	assert.NilError(t, qs.Set("\"something'"))
 	assert.Equal(t, qs.String(), "\"something'")
 }
 
 func TestQuotedStringSetWithNoQuotes(t *testing.T) {
-	qs := QuotedString("")
+	value := ""
+	qs := NewQuotedString(&value)
 	assert.NilError(t, qs.Set("something"))
 	assert.Equal(t, qs.String(), "something")
 }