github.com/outbrain/consul@v1.4.5/agent/config.go (about) 1 package agent 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 "time" 8 9 "github.com/hashicorp/consul/agent/config" 10 ) 11 12 var errInvalidHeaderFormat = errors.New("agent: invalid format of 'header' field") 13 14 func FixupCheckType(raw interface{}) error { 15 rawMap, ok := raw.(map[string]interface{}) 16 if !ok { 17 return nil 18 } 19 20 // See https://github.com/hashicorp/consul/pull/3557 why we need this 21 // and why we should get rid of it. In Consul 1.0 we also didn't map 22 // Args correctly, so we ended up exposing (and need to carry forward) 23 // ScriptArgs, see https://github.com/hashicorp/consul/issues/3587. 24 config.TranslateKeys(rawMap, map[string]string{ 25 "args": "ScriptArgs", 26 "script_args": "ScriptArgs", 27 "deregister_critical_service_after": "DeregisterCriticalServiceAfter", 28 "docker_container_id": "DockerContainerID", 29 "tls_skip_verify": "TLSSkipVerify", 30 "service_id": "ServiceID", 31 }) 32 33 parseDuration := func(v interface{}) (time.Duration, error) { 34 if v == nil { 35 return 0, nil 36 } 37 switch x := v.(type) { 38 case time.Duration: 39 return x, nil 40 case float64: 41 return time.Duration(x), nil 42 case string: 43 return time.ParseDuration(x) 44 default: 45 return 0, fmt.Errorf("invalid format") 46 } 47 } 48 49 parseHeaderMap := func(v interface{}) (map[string][]string, error) { 50 if v == nil { 51 return nil, nil 52 } 53 vm, ok := v.(map[string]interface{}) 54 if !ok { 55 return nil, errInvalidHeaderFormat 56 } 57 m := map[string][]string{} 58 for k, vv := range vm { 59 vs, ok := vv.([]interface{}) 60 if !ok { 61 return nil, errInvalidHeaderFormat 62 } 63 for _, vs := range vs { 64 s, ok := vs.(string) 65 if !ok { 66 return nil, errInvalidHeaderFormat 67 } 68 m[k] = append(m[k], s) 69 } 70 } 71 return m, nil 72 } 73 74 for k, v := range rawMap { 75 switch strings.ToLower(k) { 76 case "header": 77 h, err := parseHeaderMap(v) 78 if err != nil { 79 return fmt.Errorf("invalid %q: %s", k, err) 80 } 81 rawMap[k] = h 82 83 case "ttl", "interval", "timeout", "deregistercriticalserviceafter": 84 d, err := parseDuration(v) 85 if err != nil { 86 return fmt.Errorf("invalid %q: %v", k, err) 87 } 88 rawMap[k] = d 89 } 90 } 91 return nil 92 } 93 94 func ParseMetaPair(raw string) (string, string) { 95 pair := strings.SplitN(raw, ":", 2) 96 if len(pair) == 2 { 97 return pair[0], pair[1] 98 } 99 return pair[0], "" 100 }