From c025b292b5157a60a22abd38ef2154a8330cc565 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 3 Aug 2016 13:09:12 -0400 Subject: [PATCH] Cleanup --- builtin/credential/approle/path_role.go | 2 +- helper/strutil/strutil.go | 16 ++++++++-------- helper/strutil/strutil_test.go | 8 ++++---- physical/consul.go | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/builtin/credential/approle/path_role.go b/builtin/credential/approle/path_role.go index d496d9bcd..171fa8c55 100644 --- a/builtin/credential/approle/path_role.go +++ b/builtin/credential/approle/path_role.go @@ -1452,7 +1452,7 @@ func (b *backend) handleRoleSecretIDCommon(req *logical.Request, data *framework Metadata: make(map[string]string), } - if _, err = strutil.ParseArbitraryKeyValues(data.Get("metadata").(string), secretIDStorage.Metadata); err != nil { + if err = strutil.ParseArbitraryKeyValues(data.Get("metadata").(string), secretIDStorage.Metadata); err != nil { return logical.ErrorResponse(fmt.Sprintf("failed to parse metadata: %v", err)), nil } diff --git a/helper/strutil/strutil.go b/helper/strutil/strutil.go index bd09e21d8..0d537143c 100644 --- a/helper/strutil/strutil.go +++ b/helper/strutil/strutil.go @@ -32,7 +32,7 @@ func StrListSubset(super, sub []string) bool { // Parses a comma separated list of strings into a slice of strings. // The return slice will be sorted and will not contain duplicate or // empty items. The values will be converted to lower case. -func ParseStrings(input string) []string { +func ParseDedupAndSortStrings(input string) []string { input = strings.TrimSpace(input) var parsed []string if input == "" { @@ -45,7 +45,7 @@ func ParseStrings(input string) []string { // Parses a comma separated list of `=` tuples into a // map[string]string. func ParseKeyValues(input string, out map[string]string) error { - keyValues := ParseStrings(input) + keyValues := ParseDedupAndSortStrings(input) if len(keyValues) == 0 { return nil } @@ -72,13 +72,13 @@ func ParseKeyValues(input string, out map[string]string) error { // // Input will be parsed into the output paramater, which should // be a non-nil map[string]string. -func ParseArbitraryKeyValues(input string, out map[string]string) (string, error) { +func ParseArbitraryKeyValues(input string, out map[string]string) error { input = strings.TrimSpace(input) if input == "" { - return "", nil + return nil } if out == nil { - return "", fmt.Errorf("'out' is nil") + return fmt.Errorf("'out' is nil") } // Try to base64 decode the input. If successful, consider the decoded @@ -95,18 +95,18 @@ func ParseArbitraryKeyValues(input string, out map[string]string) (string, error // If JSON unmarshalling fails, consider that the input was // supplied as a comma separated string of 'key=value' pairs. if err = ParseKeyValues(input, out); err != nil { - return "", fmt.Errorf("failed to parse the input: %v", err) + return fmt.Errorf("failed to parse the input: %v", err) } } // Validate the parsed input for key, value := range out { if key != "" && value == "" { - return "", fmt.Errorf("invalid value for key '%s'", key) + return fmt.Errorf("invalid value for key '%s'", key) } } - return input, nil + return nil } // Removes duplicate and empty elements from a slice of strings. diff --git a/helper/strutil/strutil_test.go b/helper/strutil/strutil_test.go index 5e8f07f43..f5ceb8ce7 100644 --- a/helper/strutil/strutil_test.go +++ b/helper/strutil/strutil_test.go @@ -128,7 +128,7 @@ func TestStrutil_ParseArbitraryKeyValues(t *testing.T) { // Test = as comma separated string input = "key1=value1,key2=value2" - _, err = ParseArbitraryKeyValues(input, actual) + err = ParseArbitraryKeyValues(input, actual) if err != nil { t.Fatal(err) } @@ -141,7 +141,7 @@ func TestStrutil_ParseArbitraryKeyValues(t *testing.T) { // Test = as base64 encoded comma separated string input = base64.StdEncoding.EncodeToString([]byte(input)) - _, err = ParseArbitraryKeyValues(input, actual) + err = ParseArbitraryKeyValues(input, actual) if err != nil { t.Fatal(err) } @@ -154,7 +154,7 @@ func TestStrutil_ParseArbitraryKeyValues(t *testing.T) { // Test JSON encoded = tuples input = `{"key1":"value1", "key2":"value2"}` - _, err = ParseArbitraryKeyValues(input, actual) + err = ParseArbitraryKeyValues(input, actual) if err != nil { t.Fatal(err) } @@ -167,7 +167,7 @@ func TestStrutil_ParseArbitraryKeyValues(t *testing.T) { // Test base64 encoded JSON string of = tuples input = base64.StdEncoding.EncodeToString([]byte(input)) - _, err = ParseArbitraryKeyValues(input, actual) + err = ParseArbitraryKeyValues(input, actual) if err != nil { t.Fatal(err) } diff --git a/physical/consul.go b/physical/consul.go index 5f88a3194..f939dcfab 100644 --- a/physical/consul.go +++ b/physical/consul.go @@ -184,7 +184,7 @@ func newConsulBackend(conf map[string]string, logger *log.Logger) (Backend, erro kv: client.KV(), permitPool: NewPermitPool(maxParInt), serviceName: service, - serviceTags: strutil.ParseStrings(tags), + serviceTags: strutil.ParseDedupAndSortStrings(tags), checkTimeout: checkTimeout, disableRegistration: disableRegistration, }