This commit is contained in:
Jeff Mitchell 2016-08-03 13:09:12 -04:00
parent a7ed50dbc8
commit c025b292b5
4 changed files with 14 additions and 14 deletions

View File

@ -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
}

View File

@ -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 `<key>=<value>` 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.

View File

@ -128,7 +128,7 @@ func TestStrutil_ParseArbitraryKeyValues(t *testing.T) {
// Test <key>=<value> 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 <key>=<value> 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 <key>=<value> 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 <key>=<value> tuples
input = base64.StdEncoding.EncodeToString([]byte(input))
_, err = ParseArbitraryKeyValues(input, actual)
err = ParseArbitraryKeyValues(input, actual)
if err != nil {
t.Fatal(err)
}

View File

@ -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,
}