-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request moby#29862 from dnephin/strip-quotes-from-args
Trim quotes from tls flag values
- Loading branch information
Showing
4 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package flags | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
cliconfig "github.com/docker/docker/cli/config" | ||
"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.Dir(), 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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package opts | ||
|
||
// QuotedString is a string that may have extra quotes around the value. The | ||
// quotes are stripped from the value. | ||
type QuotedString struct { | ||
value *string | ||
} | ||
|
||
// Set sets a new value | ||
func (s *QuotedString) Set(val string) error { | ||
*s.value = trimQuotes(val) | ||
return nil | ||
} | ||
|
||
// Type returns the type of the value | ||
func (s *QuotedString) Type() string { | ||
return "string" | ||
} | ||
|
||
func (s *QuotedString) String() string { | ||
return string(*s.value) | ||
} | ||
|
||
func trimQuotes(value string) string { | ||
lastIndex := len(value) - 1 | ||
for _, char := range []byte{'\'', '"'} { | ||
if value[0] == char && value[lastIndex] == char { | ||
return value[1:lastIndex] | ||
} | ||
} | ||
return value | ||
} | ||
|
||
// NewQuotedString returns a new quoted string option | ||
func NewQuotedString(value *string) *QuotedString { | ||
return &QuotedString{value: value} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package opts | ||
|
||
import ( | ||
"github.com/docker/docker/pkg/testutil/assert" | ||
"testing" | ||
) | ||
|
||
func TestQuotedStringSetWithQuotes(t *testing.T) { | ||
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) { | ||
value := "" | ||
qs := NewQuotedString(&value) | ||
assert.NilError(t, qs.Set("\"something'")) | ||
assert.Equal(t, qs.String(), "\"something'") | ||
} | ||
|
||
func TestQuotedStringSetWithNoQuotes(t *testing.T) { | ||
value := "" | ||
qs := NewQuotedString(&value) | ||
assert.NilError(t, qs.Set("something")) | ||
assert.Equal(t, qs.String(), "something") | ||
} |