2015-03-15 23:39:49 +00:00
|
|
|
package framework
|
2015-03-14 04:11:19 +00:00
|
|
|
|
|
|
|
import (
|
2016-07-06 22:54:18 +00:00
|
|
|
"encoding/json"
|
2017-07-17 18:39:58 +00:00
|
|
|
"errors"
|
2015-03-14 04:11:19 +00:00
|
|
|
"fmt"
|
2017-07-17 18:39:58 +00:00
|
|
|
"regexp"
|
2017-11-07 16:11:49 +00:00
|
|
|
"strings"
|
2015-03-14 04:11:19 +00:00
|
|
|
|
2017-03-07 16:21:22 +00:00
|
|
|
"github.com/hashicorp/vault/helper/parseutil"
|
2017-04-18 20:02:31 +00:00
|
|
|
"github.com/hashicorp/vault/helper/strutil"
|
2015-03-14 04:11:19 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FieldData is the structure passed to the callback to handle a path
|
|
|
|
// containing the populated parameters for fields. This should be used
|
|
|
|
// instead of the raw (*vault.Request).Data to access data in a type-safe
|
|
|
|
// way.
|
|
|
|
type FieldData struct {
|
|
|
|
Raw map[string]interface{}
|
|
|
|
Schema map[string]*FieldSchema
|
|
|
|
}
|
|
|
|
|
2017-07-17 20:44:47 +00:00
|
|
|
// Validate cycles through raw data and validate conversions in
|
2015-08-11 16:34:14 +00:00
|
|
|
// the schema, so we don't get an error/panic later when
|
|
|
|
// trying to get data out. Data not in the schema is not
|
|
|
|
// an error at this point, so we don't worry about it.
|
|
|
|
func (d *FieldData) Validate() error {
|
|
|
|
for field, value := range d.Raw {
|
|
|
|
|
|
|
|
schema, ok := d.Schema[field]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch schema.Type {
|
2017-07-17 20:44:47 +00:00
|
|
|
case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeString,
|
2017-11-07 16:11:49 +00:00
|
|
|
TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice,
|
2018-03-02 23:01:13 +00:00
|
|
|
TypeKVPairs, TypeCommaIntSlice:
|
2015-08-11 16:34:14 +00:00
|
|
|
_, _, err := d.getPrimitive(field, schema)
|
|
|
|
if err != nil {
|
2015-09-01 22:29:30 +00:00
|
|
|
return fmt.Errorf("Error converting input %v for field %s: %s", value, field, err)
|
2015-08-11 16:34:14 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown field type %s for field %s",
|
2015-09-01 22:29:30 +00:00
|
|
|
schema.Type, field)
|
2015-08-11 16:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-14 04:11:19 +00:00
|
|
|
// Get gets the value for the given field. If the key is an invalid field,
|
|
|
|
// FieldData will panic. If you want a safer version of this method, use
|
|
|
|
// GetOk. If the field k is not set, the default value (if set) will be
|
|
|
|
// returned, otherwise the zero value will be returned.
|
|
|
|
func (d *FieldData) Get(k string) interface{} {
|
|
|
|
schema, ok := d.Schema[k]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("field %s not in the schema", k))
|
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := d.GetOk(k)
|
|
|
|
if !ok {
|
2015-03-14 04:15:20 +00:00
|
|
|
value = schema.DefaultOrZero()
|
2015-03-14 04:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:42:01 +00:00
|
|
|
// GetDefaultOrZero gets the default value set on the schema for the given
|
|
|
|
// field. If there is no default value set, the zero value of the type
|
|
|
|
// will be returned.
|
|
|
|
func (d *FieldData) GetDefaultOrZero(k string) interface{} {
|
|
|
|
schema, ok := d.Schema[k]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("field %s not in the schema", k))
|
|
|
|
}
|
|
|
|
|
|
|
|
return schema.DefaultOrZero()
|
|
|
|
}
|
|
|
|
|
2015-03-14 04:11:19 +00:00
|
|
|
// GetOk gets the value for the given field. The second return value
|
|
|
|
// will be false if the key is invalid or the key is not set at all.
|
|
|
|
func (d *FieldData) GetOk(k string) (interface{}, bool) {
|
|
|
|
schema, ok := d.Schema[k]
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok, err := d.GetOkErr(k)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("error reading %s: %s", k, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok && result == nil {
|
2015-03-14 04:15:20 +00:00
|
|
|
result = schema.DefaultOrZero()
|
2015-03-14 04:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOkErr is the most conservative of all the Get methods. It returns
|
|
|
|
// whether key is set or not, but also an error value. The error value is
|
|
|
|
// non-nil if the field doesn't exist or there was an error parsing the
|
|
|
|
// field value.
|
|
|
|
func (d *FieldData) GetOkErr(k string) (interface{}, bool, error) {
|
|
|
|
schema, ok := d.Schema[k]
|
|
|
|
if !ok {
|
|
|
|
return nil, false, fmt.Errorf("unknown field: %s", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch schema.Type {
|
2017-04-18 20:02:31 +00:00
|
|
|
case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeString,
|
2017-11-07 16:11:49 +00:00
|
|
|
TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice,
|
2018-03-02 23:01:13 +00:00
|
|
|
TypeKVPairs, TypeCommaIntSlice:
|
2015-03-14 04:11:19 +00:00
|
|
|
return d.getPrimitive(k, schema)
|
|
|
|
default:
|
|
|
|
return nil, false,
|
|
|
|
fmt.Errorf("unknown field type %s for field %s", schema.Type, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 23:01:13 +00:00
|
|
|
func (d *FieldData) getPrimitive(k string, schema *FieldSchema) (interface{}, bool, error) {
|
2015-03-14 04:11:19 +00:00
|
|
|
raw, ok := d.Raw[k]
|
|
|
|
if !ok {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch schema.Type {
|
|
|
|
case TypeBool:
|
|
|
|
var result bool
|
|
|
|
if err := mapstructure.WeakDecode(raw, &result); err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
return result, true, nil
|
2015-09-21 13:39:37 +00:00
|
|
|
|
2015-03-14 04:11:19 +00:00
|
|
|
case TypeInt:
|
|
|
|
var result int
|
|
|
|
if err := mapstructure.WeakDecode(raw, &result); err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
return result, true, nil
|
2015-09-21 13:39:37 +00:00
|
|
|
|
2015-03-14 04:11:19 +00:00
|
|
|
case TypeString:
|
|
|
|
var result string
|
|
|
|
if err := mapstructure.WeakDecode(raw, &result); err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
2015-03-31 23:06:15 +00:00
|
|
|
return result, true, nil
|
2015-09-21 13:39:37 +00:00
|
|
|
|
2017-07-17 18:39:58 +00:00
|
|
|
case TypeNameString:
|
|
|
|
var result string
|
|
|
|
if err := mapstructure.WeakDecode(raw, &result); err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
matched, err := regexp.MatchString("^\\w(([\\w-.]+)?\\w)?$", result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
if !matched {
|
|
|
|
return nil, true, errors.New("field does not match the formatting rules")
|
|
|
|
}
|
|
|
|
return result, true, nil
|
|
|
|
|
2015-03-31 23:06:15 +00:00
|
|
|
case TypeMap:
|
|
|
|
var result map[string]interface{}
|
|
|
|
if err := mapstructure.WeakDecode(raw, &result); err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
2015-03-14 04:11:19 +00:00
|
|
|
return result, true, nil
|
2015-06-17 22:56:26 +00:00
|
|
|
|
|
|
|
case TypeDurationSecond:
|
|
|
|
var result int
|
|
|
|
switch inp := raw.(type) {
|
2015-07-08 22:55:52 +00:00
|
|
|
case nil:
|
2015-09-21 13:39:37 +00:00
|
|
|
return nil, false, nil
|
2015-06-17 22:56:26 +00:00
|
|
|
case int:
|
|
|
|
result = inp
|
2017-08-02 22:12:02 +00:00
|
|
|
case int32:
|
|
|
|
result = int(inp)
|
|
|
|
case int64:
|
|
|
|
result = int(inp)
|
|
|
|
case uint:
|
|
|
|
result = int(inp)
|
|
|
|
case uint32:
|
|
|
|
result = int(inp)
|
|
|
|
case uint64:
|
|
|
|
result = int(inp)
|
2015-06-17 22:56:26 +00:00
|
|
|
case float32:
|
|
|
|
result = int(inp)
|
|
|
|
case float64:
|
|
|
|
result = int(inp)
|
|
|
|
case string:
|
2017-03-07 16:21:22 +00:00
|
|
|
dur, err := parseutil.ParseDurationSecond(inp)
|
2016-07-11 17:46:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, true, err
|
2015-06-17 22:56:26 +00:00
|
|
|
}
|
2016-07-11 18:19:35 +00:00
|
|
|
result = int(dur.Seconds())
|
2016-07-06 22:54:18 +00:00
|
|
|
case json.Number:
|
|
|
|
valInt64, err := inp.Int64()
|
|
|
|
if err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
result = int(valInt64)
|
2015-06-17 22:56:26 +00:00
|
|
|
default:
|
|
|
|
return nil, false, fmt.Errorf("invalid input '%v'", raw)
|
|
|
|
}
|
|
|
|
return result, true, nil
|
|
|
|
|
2018-03-02 23:01:13 +00:00
|
|
|
case TypeCommaIntSlice:
|
|
|
|
var result []int
|
|
|
|
config := &mapstructure.DecoderConfig{
|
|
|
|
Result: &result,
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
DecodeHook: mapstructure.StringToSliceHookFunc(","),
|
|
|
|
}
|
|
|
|
decoder, err := mapstructure.NewDecoder(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
if err := decoder.Decode(raw); err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
return result, true, nil
|
|
|
|
|
2017-04-18 20:02:31 +00:00
|
|
|
case TypeSlice:
|
|
|
|
var result []interface{}
|
|
|
|
if err := mapstructure.WeakDecode(raw, &result); err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
return result, true, nil
|
|
|
|
|
|
|
|
case TypeStringSlice:
|
|
|
|
var result []string
|
|
|
|
if err := mapstructure.WeakDecode(raw, &result); err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
return strutil.TrimStrings(result), true, nil
|
|
|
|
|
|
|
|
case TypeCommaStringSlice:
|
2018-02-16 22:19:34 +00:00
|
|
|
res, err := parseutil.ParseCommaStringSlice(raw)
|
2017-04-18 20:02:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2018-02-16 22:19:34 +00:00
|
|
|
return res, true, nil
|
2017-04-18 20:02:31 +00:00
|
|
|
|
2017-11-07 16:11:49 +00:00
|
|
|
case TypeKVPairs:
|
|
|
|
// First try to parse this as a map
|
|
|
|
var mapResult map[string]string
|
|
|
|
if err := mapstructure.WeakDecode(raw, &mapResult); err == nil {
|
|
|
|
return mapResult, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If map parse fails, parse as a string list of = delimited pairs
|
|
|
|
var listResult []string
|
|
|
|
if err := mapstructure.WeakDecode(raw, &listResult); err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make(map[string]string, len(listResult))
|
|
|
|
for _, keyPair := range listResult {
|
|
|
|
keyPairSlice := strings.SplitN(keyPair, "=", 2)
|
|
|
|
if len(keyPairSlice) != 2 || keyPairSlice[0] == "" {
|
|
|
|
return nil, false, fmt.Errorf("invalid key pair %q", keyPair)
|
|
|
|
}
|
|
|
|
result[keyPairSlice[0]] = keyPairSlice[1]
|
|
|
|
}
|
|
|
|
return result, true, nil
|
|
|
|
|
2015-03-14 04:11:19 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown type: %s", schema.Type))
|
|
|
|
}
|
|
|
|
}
|