2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-08-20 18:19:43 +00:00
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
2017-06-27 21:22:57 +00:00
|
|
|
"context"
|
2014-08-20 18:19:43 +00:00
|
|
|
"fmt"
|
2014-08-21 20:09:13 +00:00
|
|
|
"io"
|
2014-08-20 20:45:34 +00:00
|
|
|
"sync"
|
2017-10-22 03:08:11 +00:00
|
|
|
"time"
|
2014-08-20 20:45:34 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2020-07-29 18:33:36 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2017-10-22 01:39:09 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2014-08-20 18:19:43 +00:00
|
|
|
)
|
|
|
|
|
2017-10-22 01:39:09 +00:00
|
|
|
const DefaultTimeout = 10 * time.Second
|
|
|
|
|
2017-04-21 00:46:29 +00:00
|
|
|
// Plan is the parsed version of a watch specification. A watch provides
|
2014-08-20 18:19:43 +00:00
|
|
|
// the details of a query, which generates a view into the Consul data store.
|
|
|
|
// This view is watched for changes and a handler is invoked to take any
|
|
|
|
// appropriate actions.
|
2017-04-21 00:46:29 +00:00
|
|
|
type Plan struct {
|
2017-10-22 01:39:09 +00:00
|
|
|
Datacenter string
|
|
|
|
Token string
|
|
|
|
Type string
|
|
|
|
HandlerType string
|
|
|
|
Exempt map[string]interface{}
|
2014-08-21 20:09:13 +00:00
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
Watcher WatcherFunc
|
|
|
|
// Handler is kept for backward compatibility but only supports watches based
|
|
|
|
// on index param. To support hash based watches, set HybridHandler instead.
|
|
|
|
Handler HandlerFunc
|
|
|
|
HybridHandler HybridHandlerFunc
|
2020-07-29 18:33:36 +00:00
|
|
|
|
|
|
|
Logger hclog.Logger
|
|
|
|
// Deprecated: use Logger
|
|
|
|
LogOutput io.Writer
|
2014-08-20 20:45:34 +00:00
|
|
|
|
2018-04-05 16:15:43 +00:00
|
|
|
address string
|
|
|
|
client *consulapi.Client
|
2018-04-25 19:41:26 +00:00
|
|
|
lastParamVal BlockingParamVal
|
2018-04-05 16:15:43 +00:00
|
|
|
lastResult interface{}
|
2014-08-20 20:45:34 +00:00
|
|
|
|
2017-06-26 20:52:03 +00:00
|
|
|
stop bool
|
|
|
|
stopCh chan struct{}
|
|
|
|
stopLock sync.Mutex
|
|
|
|
cancelFunc context.CancelFunc
|
2014-08-20 18:19:43 +00:00
|
|
|
}
|
|
|
|
|
2017-10-22 01:39:09 +00:00
|
|
|
type HttpHandlerConfig struct {
|
|
|
|
Path string `mapstructure:"path"`
|
|
|
|
Method string `mapstructure:"method"`
|
|
|
|
Timeout time.Duration `mapstructure:"-"`
|
|
|
|
TimeoutRaw string `mapstructure:"timeout"`
|
|
|
|
Header map[string][]string `mapstructure:"header"`
|
|
|
|
TLSSkipVerify bool `mapstructure:"tls_skip_verify"`
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
// BlockingParamVal is an interface representing the common operations needed for
|
2018-04-05 16:15:43 +00:00
|
|
|
// different styles of blocking. It's used to abstract the core watch plan from
|
|
|
|
// whether we are performing index-based or hash-based blocking.
|
2018-04-25 19:41:26 +00:00
|
|
|
type BlockingParamVal interface {
|
2018-04-05 16:15:43 +00:00
|
|
|
// Equal returns whether the other param value should be considered equal
|
|
|
|
// (i.e. representing no change in the watched resource). Equal must not panic
|
|
|
|
// if other is nil.
|
2018-04-25 19:41:26 +00:00
|
|
|
Equal(other BlockingParamVal) bool
|
2018-04-05 16:15:43 +00:00
|
|
|
|
|
|
|
// Next is called when deciding which value to use on the next blocking call.
|
2018-04-25 19:41:26 +00:00
|
|
|
// It assumes the BlockingParamVal value it is called on is the most recent one
|
2018-04-05 16:15:43 +00:00
|
|
|
// returned and passes the previous one which may be nil as context. This
|
2019-03-06 17:13:28 +00:00
|
|
|
// allows types to customize logic around ordering without assuming there is
|
2018-04-05 16:15:43 +00:00
|
|
|
// an order. For example WaitIndexVal can check that the index didn't go
|
|
|
|
// backwards and if it did then reset to 0. Most other cases should just
|
|
|
|
// return themselves (the most recent value) to be used in the next request.
|
2018-04-25 19:41:26 +00:00
|
|
|
Next(previous BlockingParamVal) BlockingParamVal
|
2018-04-05 16:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitIndexVal is a type representing a Consul index that implements
|
2018-04-25 19:41:26 +00:00
|
|
|
// BlockingParamVal.
|
2018-04-05 16:15:43 +00:00
|
|
|
type WaitIndexVal uint64
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
// Equal implements BlockingParamVal
|
|
|
|
func (idx WaitIndexVal) Equal(other BlockingParamVal) bool {
|
2018-04-05 16:15:43 +00:00
|
|
|
if otherIdx, ok := other.(WaitIndexVal); ok {
|
|
|
|
return idx == otherIdx
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
// Next implements BlockingParamVal
|
|
|
|
func (idx WaitIndexVal) Next(previous BlockingParamVal) BlockingParamVal {
|
2018-04-05 16:15:43 +00:00
|
|
|
if previous == nil {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
prevIdx, ok := previous.(WaitIndexVal)
|
2019-12-10 11:02:21 +00:00
|
|
|
if ok && prevIdx == idx {
|
|
|
|
// This value is the same as the previous index, reset
|
2018-04-05 16:15:43 +00:00
|
|
|
return WaitIndexVal(0)
|
|
|
|
}
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitHashVal is a type representing a Consul content hash that implements
|
2018-04-25 19:41:26 +00:00
|
|
|
// BlockingParamVal.
|
2018-04-05 16:15:43 +00:00
|
|
|
type WaitHashVal string
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
// Equal implements BlockingParamVal
|
|
|
|
func (h WaitHashVal) Equal(other BlockingParamVal) bool {
|
2018-04-05 16:15:43 +00:00
|
|
|
if otherHash, ok := other.(WaitHashVal); ok {
|
|
|
|
return h == otherHash
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
// Next implements BlockingParamVal
|
|
|
|
func (h WaitHashVal) Next(previous BlockingParamVal) BlockingParamVal {
|
2018-04-05 16:15:43 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatcherFunc is used to watch for a diff.
|
2018-04-25 19:41:26 +00:00
|
|
|
type WatcherFunc func(*Plan) (BlockingParamVal, interface{}, error)
|
2014-08-20 20:45:34 +00:00
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
// HandlerFunc is used to handle new data. It only works for index-based watches
|
|
|
|
// (which is almost all end points currently) and is kept for backwards
|
|
|
|
// compatibility until more places can make use of hash-based watches too.
|
|
|
|
type HandlerFunc func(uint64, interface{})
|
|
|
|
|
|
|
|
// HybridHandlerFunc is used to handle new data. It can support either
|
|
|
|
// index-based or hash-based watches via the BlockingParamVal.
|
|
|
|
type HybridHandlerFunc func(BlockingParamVal, interface{})
|
2014-08-20 20:45:34 +00:00
|
|
|
|
2014-08-20 18:19:43 +00:00
|
|
|
// Parse takes a watch query and compiles it into a WatchPlan or an error
|
2017-04-21 00:46:29 +00:00
|
|
|
func Parse(params map[string]interface{}) (*Plan, error) {
|
2014-08-21 18:38:44 +00:00
|
|
|
return ParseExempt(params, nil)
|
2014-08-20 23:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseExempt takes a watch query and compiles it into a WatchPlan or an error
|
|
|
|
// Any exempt parameters are stored in the Exempt map
|
2017-04-21 00:46:29 +00:00
|
|
|
func ParseExempt(params map[string]interface{}, exempt []string) (*Plan, error) {
|
|
|
|
plan := &Plan{
|
2014-08-20 20:45:34 +00:00
|
|
|
stopCh: make(chan struct{}),
|
2017-10-22 01:39:09 +00:00
|
|
|
Exempt: make(map[string]interface{}),
|
2014-08-20 20:45:34 +00:00
|
|
|
}
|
2014-08-20 18:19:43 +00:00
|
|
|
|
2014-08-20 20:45:34 +00:00
|
|
|
// Parse the generic parameters
|
|
|
|
if err := assignValue(params, "datacenter", &plan.Datacenter); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := assignValue(params, "token", &plan.Token); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-20 18:19:43 +00:00
|
|
|
if err := assignValue(params, "type", &plan.Type); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
// Ensure there is a watch type
|
2014-08-20 18:19:43 +00:00
|
|
|
if plan.Type == "" {
|
|
|
|
return nil, fmt.Errorf("Watch type must be specified")
|
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
|
2017-10-22 01:39:09 +00:00
|
|
|
// Get the specific handler
|
|
|
|
if err := assignValue(params, "handler_type", &plan.HandlerType); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch plan.HandlerType {
|
|
|
|
case "http":
|
|
|
|
if _, ok := params["http_handler_config"]; !ok {
|
|
|
|
return nil, fmt.Errorf("Handler type 'http' requires 'http_handler_config' to be set")
|
|
|
|
}
|
|
|
|
config, err := parseHttpHandlerConfig(params["http_handler_config"])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(fmt.Sprintf("Failed to parse 'http_handler_config': %v", err))
|
|
|
|
}
|
|
|
|
plan.Exempt["http_handler_config"] = config
|
|
|
|
delete(params, "http_handler_config")
|
|
|
|
|
|
|
|
case "script":
|
|
|
|
// Let the caller check for configuration in exempt parameters
|
|
|
|
}
|
|
|
|
|
2014-08-20 20:45:34 +00:00
|
|
|
// Look for a factory function
|
|
|
|
factory := watchFuncFactory[plan.Type]
|
|
|
|
if factory == nil {
|
|
|
|
return nil, fmt.Errorf("Unsupported watch type: %s", plan.Type)
|
2014-08-20 18:19:43 +00:00
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
|
|
|
|
// Get the watch func
|
|
|
|
fn, err := factory(params)
|
|
|
|
if err != nil {
|
2014-08-20 18:19:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-21 00:46:29 +00:00
|
|
|
plan.Watcher = fn
|
2014-08-20 18:19:43 +00:00
|
|
|
|
2014-08-20 23:38:15 +00:00
|
|
|
// Remove the exempt parameters
|
|
|
|
if len(exempt) > 0 {
|
|
|
|
for _, ex := range exempt {
|
|
|
|
val, ok := params[ex]
|
|
|
|
if ok {
|
|
|
|
plan.Exempt[ex] = val
|
|
|
|
delete(params, ex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 20:45:34 +00:00
|
|
|
// Ensure all parameters are consumed
|
|
|
|
if len(params) != 0 {
|
|
|
|
var bad []string
|
|
|
|
for key := range params {
|
|
|
|
bad = append(bad, key)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Invalid parameters: %v", bad)
|
|
|
|
}
|
2014-08-20 18:19:43 +00:00
|
|
|
return plan, nil
|
|
|
|
}
|
|
|
|
|
2014-08-21 18:38:44 +00:00
|
|
|
// assignValue is used to extract a value ensuring it is a string
|
|
|
|
func assignValue(params map[string]interface{}, name string, out *string) error {
|
|
|
|
if raw, ok := params[name]; ok {
|
|
|
|
val, ok := raw.(string)
|
|
|
|
if !ok {
|
2014-11-01 21:56:48 +00:00
|
|
|
return fmt.Errorf("Expecting %s to be a string", name)
|
2014-08-20 18:19:43 +00:00
|
|
|
}
|
2014-08-21 18:38:44 +00:00
|
|
|
*out = val
|
2014-08-20 18:19:43 +00:00
|
|
|
delete(params, name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-21 18:38:44 +00:00
|
|
|
// assignValueBool is used to extract a value ensuring it is a bool
|
|
|
|
func assignValueBool(params map[string]interface{}, name string, out *bool) error {
|
|
|
|
if raw, ok := params[name]; ok {
|
|
|
|
val, ok := raw.(bool)
|
|
|
|
if !ok {
|
2014-11-01 21:56:48 +00:00
|
|
|
return fmt.Errorf("Expecting %s to be a boolean", name)
|
2014-08-20 18:19:43 +00:00
|
|
|
}
|
2014-08-21 18:38:44 +00:00
|
|
|
*out = val
|
|
|
|
delete(params, name)
|
2014-08-20 18:19:43 +00:00
|
|
|
}
|
2014-08-21 18:38:44 +00:00
|
|
|
return nil
|
2014-08-20 18:19:43 +00:00
|
|
|
}
|
2017-10-22 01:39:09 +00:00
|
|
|
|
2019-04-29 19:28:01 +00:00
|
|
|
// assignValueStringSlice is used to extract a value ensuring it is either a string or a slice of strings
|
|
|
|
func assignValueStringSlice(params map[string]interface{}, name string, out *[]string) error {
|
|
|
|
if raw, ok := params[name]; ok {
|
|
|
|
var tmp []string
|
|
|
|
switch raw.(type) {
|
|
|
|
case string:
|
|
|
|
tmp = make([]string, 1, 1)
|
|
|
|
tmp[0] = raw.(string)
|
|
|
|
case []string:
|
|
|
|
l := len(raw.([]string))
|
|
|
|
tmp = make([]string, l, l)
|
|
|
|
copy(tmp, raw.([]string))
|
|
|
|
case []interface{}:
|
|
|
|
l := len(raw.([]interface{}))
|
|
|
|
tmp = make([]string, l, l)
|
|
|
|
for i, v := range raw.([]interface{}) {
|
|
|
|
if s, ok := v.(string); ok {
|
|
|
|
tmp[i] = s
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Index %d of %s expected to be string", i, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Expecting %s to be a string or []string", name)
|
|
|
|
}
|
|
|
|
*out = tmp
|
|
|
|
delete(params, name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-22 01:39:09 +00:00
|
|
|
// Parse the 'http_handler_config' parameters
|
|
|
|
func parseHttpHandlerConfig(configParams interface{}) (*HttpHandlerConfig, error) {
|
|
|
|
var config HttpHandlerConfig
|
|
|
|
if err := mapstructure.Decode(configParams, &config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Path == "" {
|
|
|
|
return nil, fmt.Errorf("Requires 'path' to be set")
|
|
|
|
}
|
|
|
|
if config.Method == "" {
|
|
|
|
config.Method = "POST"
|
|
|
|
}
|
|
|
|
if config.TimeoutRaw == "" {
|
|
|
|
config.Timeout = DefaultTimeout
|
|
|
|
} else if timeout, err := time.ParseDuration(config.TimeoutRaw); err != nil {
|
|
|
|
return nil, fmt.Errorf(fmt.Sprintf("Failed to parse timeout: %v", err))
|
|
|
|
} else {
|
|
|
|
config.Timeout = timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|