2013-12-19 17:14:46 -08:00
|
|
|
package agent
|
|
|
|
|
2013-12-20 15:33:13 -08:00
|
|
|
import (
|
2017-06-07 01:11:56 +02:00
|
|
|
"errors"
|
2013-12-20 16:39:32 -08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-01-15 12:17:40 -10:00
|
|
|
"time"
|
2017-10-11 01:40:59 +02:00
|
|
|
|
2019-04-30 18:19:19 -04:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2017-09-25 20:40:42 +02:00
|
|
|
)
|
2014-02-03 15:15:35 -08:00
|
|
|
|
2017-06-07 01:11:56 +02:00
|
|
|
var errInvalidHeaderFormat = errors.New("agent: invalid format of 'header' field")
|
2015-01-13 17:52:17 -08:00
|
|
|
|
2017-06-07 01:11:56 +02:00
|
|
|
func FixupCheckType(raw interface{}) error {
|
2014-02-03 15:15:35 -08:00
|
|
|
rawMap, ok := raw.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-24 19:44:27 -07:00
|
|
|
|
2017-10-18 11:28:39 -07: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 18:19:19 -04:00
|
|
|
lib.TranslateKeys(rawMap, map[string]string{
|
2017-10-18 11:28:39 -07:00
|
|
|
"args": "ScriptArgs",
|
|
|
|
"script_args": "ScriptArgs",
|
2017-10-11 01:40:59 +02:00
|
|
|
"deregister_critical_service_after": "DeregisterCriticalServiceAfter",
|
|
|
|
"docker_container_id": "DockerContainerID",
|
|
|
|
"tls_skip_verify": "TLSSkipVerify",
|
|
|
|
"service_id": "ServiceID",
|
|
|
|
})
|
|
|
|
|
2017-06-07 01:11:56 +02:00
|
|
|
parseDuration := func(v interface{}) (time.Duration, error) {
|
|
|
|
if v == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2017-06-08 22:34:08 +02: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-07 01:11:56 +02:00
|
|
|
return 0, fmt.Errorf("invalid format")
|
|
|
|
}
|
2014-04-24 19:44:27 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 01:11:56 +02: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 15:15:35 -08:00
|
|
|
}
|
|
|
|
}
|
2017-06-07 01:11:56 +02:00
|
|
|
return m, nil
|
2014-02-03 15:15:35 -08:00
|
|
|
}
|
2014-04-24 19:44:27 -07:00
|
|
|
|
2017-06-07 01:11:56 +02:00
|
|
|
for k, v := range rawMap {
|
|
|
|
switch strings.ToLower(k) {
|
|
|
|
case "header":
|
|
|
|
h, err := parseHeaderMap(v)
|
2017-04-20 18:59:42 -07:00
|
|
|
if err != nil {
|
2017-06-07 01:11:56 +02:00
|
|
|
return fmt.Errorf("invalid %q: %s", k, err)
|
2015-02-02 15:29:42 +07:00
|
|
|
}
|
2017-06-07 01:11:56 +02:00
|
|
|
rawMap[k] = h
|
2015-02-02 15:29:42 +07:00
|
|
|
|
2017-10-11 01:40:59 +02:00
|
|
|
case "ttl", "interval", "timeout", "deregistercriticalserviceafter":
|
2017-06-07 01:11:56 +02:00
|
|
|
d, err := parseDuration(v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid %q: %v", k, err)
|
|
|
|
}
|
|
|
|
rawMap[k] = d
|
2016-08-16 00:05:55 -07:00
|
|
|
}
|
|
|
|
}
|
2014-02-03 15:15:35 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-02 12:04:04 +02:00
|
|
|
func ParseMetaPair(raw string) (string, string) {
|
|
|
|
pair := strings.SplitN(raw, ":", 2)
|
|
|
|
if len(pair) == 2 {
|
|
|
|
return pair[0], pair[1]
|
|
|
|
}
|
|
|
|
return pair[0], ""
|
|
|
|
}
|