2013-12-20 01:14:46 +00:00
|
|
|
package agent
|
|
|
|
|
2013-12-20 23:33:13 +00:00
|
|
|
import (
|
2017-06-06 23:11:56 +00:00
|
|
|
"errors"
|
2013-12-21 00:39:32 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-01-15 22:17:40 +00:00
|
|
|
"time"
|
2017-10-10 23:40:59 +00:00
|
|
|
|
2019-04-30 22:19:19 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2017-09-25 18:40:42 +00:00
|
|
|
)
|
2014-02-03 23:15:35 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
var errInvalidHeaderFormat = errors.New("agent: invalid format of 'header' field")
|
2015-01-14 01:52:17 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
func FixupCheckType(raw interface{}) error {
|
2014-02-03 23:15:35 +00:00
|
|
|
rawMap, ok := raw.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-25 02:44:27 +00:00
|
|
|
|
2017-10-18 18:28:39 +00:00
|
|
|
// See https://github.com/hashicorp/consul/pull/3557 why we need this
|
|
|
|
// and why we should get rid of it. In Consul 1.0 we also didn't map
|
|
|
|
// Args correctly, so we ended up exposing (and need to carry forward)
|
|
|
|
// ScriptArgs, see https://github.com/hashicorp/consul/issues/3587.
|
2019-04-30 22:19:19 +00:00
|
|
|
lib.TranslateKeys(rawMap, map[string]string{
|
2017-10-18 18:28:39 +00:00
|
|
|
"args": "ScriptArgs",
|
|
|
|
"script_args": "ScriptArgs",
|
2017-10-10 23:40:59 +00:00
|
|
|
"deregister_critical_service_after": "DeregisterCriticalServiceAfter",
|
|
|
|
"docker_container_id": "DockerContainerID",
|
|
|
|
"tls_skip_verify": "TLSSkipVerify",
|
|
|
|
"service_id": "ServiceID",
|
|
|
|
})
|
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
parseDuration := func(v interface{}) (time.Duration, error) {
|
|
|
|
if v == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2017-06-08 20:34:08 +00:00
|
|
|
switch x := v.(type) {
|
|
|
|
case time.Duration:
|
|
|
|
return x, nil
|
|
|
|
case float64:
|
|
|
|
return time.Duration(x), nil
|
|
|
|
case string:
|
|
|
|
return time.ParseDuration(x)
|
|
|
|
default:
|
2017-06-06 23:11:56 +00:00
|
|
|
return 0, fmt.Errorf("invalid format")
|
|
|
|
}
|
2014-04-25 02:44:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
parseHeaderMap := func(v interface{}) (map[string][]string, error) {
|
|
|
|
if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
vm, ok := v.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, errInvalidHeaderFormat
|
|
|
|
}
|
|
|
|
m := map[string][]string{}
|
|
|
|
for k, vv := range vm {
|
|
|
|
vs, ok := vv.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, errInvalidHeaderFormat
|
|
|
|
}
|
|
|
|
for _, vs := range vs {
|
|
|
|
s, ok := vs.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errInvalidHeaderFormat
|
|
|
|
}
|
|
|
|
m[k] = append(m[k], s)
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-06 23:11:56 +00:00
|
|
|
return m, nil
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
2014-04-25 02:44:27 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
for k, v := range rawMap {
|
|
|
|
switch strings.ToLower(k) {
|
|
|
|
case "header":
|
|
|
|
h, err := parseHeaderMap(v)
|
2017-04-21 01:59:42 +00:00
|
|
|
if err != nil {
|
2017-06-06 23:11:56 +00:00
|
|
|
return fmt.Errorf("invalid %q: %s", k, err)
|
2015-02-02 08:29:42 +00:00
|
|
|
}
|
2017-06-06 23:11:56 +00:00
|
|
|
rawMap[k] = h
|
2015-02-02 08:29:42 +00:00
|
|
|
|
2017-10-10 23:40:59 +00:00
|
|
|
case "ttl", "interval", "timeout", "deregistercriticalserviceafter":
|
2017-06-06 23:11:56 +00:00
|
|
|
d, err := parseDuration(v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid %q: %v", k, err)
|
|
|
|
}
|
|
|
|
rawMap[k] = d
|
2016-08-16 07:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-03 23:15:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:04:04 +00:00
|
|
|
func ParseMetaPair(raw string) (string, string) {
|
|
|
|
pair := strings.SplitN(raw, ":", 2)
|
|
|
|
if len(pair) == 2 {
|
|
|
|
return pair[0], pair[1]
|
|
|
|
}
|
|
|
|
return pair[0], ""
|
|
|
|
}
|