Skip to content

Commit abe32de

Browse files
committed
Trim quotes from TLS flags.
Signed-off-by: Daniel Nephin <[email protected]>
1 parent e4c1f07 commit abe32de

4 files changed

Lines changed: 67 additions & 10 deletions

File tree

cli/flags/common.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,15 @@ func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) {
5959

6060
// TODO use flag flags.String("identity"}, "i", "", "Path to libtrust key file")
6161

62-
commonOpts.TLSOptions = &tlsconfig.Options{}
62+
commonOpts.TLSOptions = &tlsconfig.Options{
63+
CAFile: filepath.Join(dockerCertPath, DefaultCaFile),
64+
CertFile: filepath.Join(dockerCertPath, DefaultCertFile),
65+
KeyFile: filepath.Join(dockerCertPath, DefaultKeyFile),
66+
}
6367
tlsOptions := commonOpts.TLSOptions
64-
flags.StringVar(&tlsOptions.CAFile, "tlscacert", filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
65-
flags.StringVar(&tlsOptions.CertFile, "tlscert", filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
66-
flags.StringVar(&tlsOptions.KeyFile, "tlskey", filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
68+
flags.Var(opts.NewQuotedString(&tlsOptions.CAFile), "tlscacert", "Trust certs signed only by this CA")
69+
flags.Var(opts.NewQuotedString(&tlsOptions.CertFile), "tlscert", "Path to TLS certificate file")
70+
flags.Var(opts.NewQuotedString(&tlsOptions.KeyFile), "tlskey", "Path to TLS key file")
6771

6872
hostOpt := opts.NewNamedListOptsRef("hosts", &commonOpts.Hosts, opts.ValidateHost)
6973
flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")

cli/flags/common_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package flags
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
cliconfig "github.com/docker/docker/cli/config"
8+
"github.com/docker/docker/pkg/testutil/assert"
9+
"github.com/spf13/pflag"
10+
)
11+
12+
func TestCommonOptionsInstallFlags(t *testing.T) {
13+
flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
14+
opts := NewCommonOptions()
15+
opts.InstallFlags(flags)
16+
17+
err := flags.Parse([]string{
18+
"--tlscacert=\"/foo/cafile\"",
19+
"--tlscert=\"/foo/cert\"",
20+
"--tlskey=\"/foo/key\"",
21+
})
22+
assert.NilError(t, err)
23+
assert.Equal(t, opts.TLSOptions.CAFile, "/foo/cafile")
24+
assert.Equal(t, opts.TLSOptions.CertFile, "/foo/cert")
25+
assert.Equal(t, opts.TLSOptions.KeyFile, "/foo/key")
26+
}
27+
28+
func defaultPath(filename string) string {
29+
return filepath.Join(cliconfig.Dir(), filename)
30+
}
31+
32+
func TestCommonOptionsInstallFlagsWithDefaults(t *testing.T) {
33+
flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
34+
opts := NewCommonOptions()
35+
opts.InstallFlags(flags)
36+
37+
err := flags.Parse([]string{})
38+
assert.NilError(t, err)
39+
assert.Equal(t, opts.TLSOptions.CAFile, defaultPath("ca.pem"))
40+
assert.Equal(t, opts.TLSOptions.CertFile, defaultPath("cert.pem"))
41+
assert.Equal(t, opts.TLSOptions.KeyFile, defaultPath("key.pem"))
42+
}

opts/quotedstring.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package opts
22

33
// QuotedString is a string that may have extra quotes around the value. The
44
// quotes are stripped from the value.
5-
type QuotedString string
5+
type QuotedString struct {
6+
value *string
7+
}
68

79
// Set sets a new value
810
func (s *QuotedString) Set(val string) error {
9-
*s = QuotedString(trimQuotes(val))
11+
*s.value = trimQuotes(val)
1012
return nil
1113
}
1214

@@ -16,7 +18,7 @@ func (s *QuotedString) Type() string {
1618
}
1719

1820
func (s *QuotedString) String() string {
19-
return string(*s)
21+
return string(*s.value)
2022
}
2123

2224
func trimQuotes(value string) string {
@@ -28,3 +30,8 @@ func trimQuotes(value string) string {
2830
}
2931
return value
3032
}
33+
34+
// NewQuotedString returns a new quoted string option
35+
func NewQuotedString(value *string) *QuotedString {
36+
return &QuotedString{value: value}
37+
}

opts/quotedstring_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@ import (
66
)
77

88
func TestQuotedStringSetWithQuotes(t *testing.T) {
9-
qs := QuotedString("")
9+
value := ""
10+
qs := NewQuotedString(&value)
1011
assert.NilError(t, qs.Set("\"something\""))
1112
assert.Equal(t, qs.String(), "something")
13+
assert.Equal(t, value, "something")
1214
}
1315

1416
func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) {
15-
qs := QuotedString("")
17+
value := ""
18+
qs := NewQuotedString(&value)
1619
assert.NilError(t, qs.Set("\"something'"))
1720
assert.Equal(t, qs.String(), "\"something'")
1821
}
1922

2023
func TestQuotedStringSetWithNoQuotes(t *testing.T) {
21-
qs := QuotedString("")
24+
value := ""
25+
qs := NewQuotedString(&value)
2226
assert.NilError(t, qs.Set("something"))
2327
assert.Equal(t, qs.String(), "something")
2428
}

0 commit comments

Comments
 (0)