2019-03-19 17:06:46 +00:00
|
|
|
package structs
|
|
|
|
|
2019-03-27 23:52:38 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2019-04-18 04:35:19 +00:00
|
|
|
"strconv"
|
2019-03-27 23:52:38 +00:00
|
|
|
"strings"
|
2020-10-06 18:24:05 +00:00
|
|
|
"time"
|
2019-03-28 06:56:35 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2020-09-02 19:10:25 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2019-04-18 04:35:19 +00:00
|
|
|
"github.com/mitchellh/hashstructure"
|
2019-04-29 22:08:09 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2020-12-11 21:10:00 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
"github.com/hashicorp/consul/lib"
|
|
|
|
"github.com/hashicorp/consul/lib/decode"
|
2019-03-27 23:52:38 +00:00
|
|
|
)
|
2019-03-22 16:25:37 +00:00
|
|
|
|
2019-03-19 17:06:46 +00:00
|
|
|
const (
|
2020-03-31 19:27:32 +00:00
|
|
|
ServiceDefaults string = "service-defaults"
|
|
|
|
ProxyDefaults string = "proxy-defaults"
|
|
|
|
ServiceRouter string = "service-router"
|
|
|
|
ServiceSplitter string = "service-splitter"
|
|
|
|
ServiceResolver string = "service-resolver"
|
|
|
|
IngressGateway string = "ingress-gateway"
|
|
|
|
TerminatingGateway string = "terminating-gateway"
|
2020-10-06 18:24:05 +00:00
|
|
|
ServiceIntentions string = "service-intentions"
|
2019-03-22 16:25:37 +00:00
|
|
|
|
|
|
|
ProxyConfigGlobal string = "global"
|
2019-03-27 23:52:38 +00:00
|
|
|
|
|
|
|
DefaultServiceProtocol = "tcp"
|
2019-03-19 17:06:46 +00:00
|
|
|
)
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
var AllConfigEntryKinds = []string{
|
|
|
|
ServiceDefaults,
|
|
|
|
ProxyDefaults,
|
|
|
|
ServiceRouter,
|
|
|
|
ServiceSplitter,
|
|
|
|
ServiceResolver,
|
|
|
|
IngressGateway,
|
|
|
|
TerminatingGateway,
|
|
|
|
ServiceIntentions,
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
// ConfigEntry is the interface for centralized configuration stored in Raft.
|
|
|
|
// Currently only service-defaults and proxy-defaults are supported.
|
2019-03-19 22:56:17 +00:00
|
|
|
type ConfigEntry interface {
|
|
|
|
GetKind() string
|
2019-03-19 17:06:46 +00:00
|
|
|
GetName() string
|
2019-03-19 22:56:17 +00:00
|
|
|
|
2019-03-27 23:52:38 +00:00
|
|
|
// This is called in the RPC endpoint and can apply defaults or limits.
|
2019-03-19 22:56:17 +00:00
|
|
|
Normalize() error
|
2019-03-19 17:06:46 +00:00
|
|
|
Validate() error
|
2019-03-19 22:56:17 +00:00
|
|
|
|
2019-04-23 06:55:11 +00:00
|
|
|
// CanRead and CanWrite return whether or not the given Authorizer
|
2019-04-10 21:27:28 +00:00
|
|
|
// has permission to read or write to the config entry, respectively.
|
2019-04-23 06:55:11 +00:00
|
|
|
CanRead(acl.Authorizer) bool
|
|
|
|
CanWrite(acl.Authorizer) bool
|
2019-04-10 21:27:28 +00:00
|
|
|
|
2020-09-02 19:10:25 +00:00
|
|
|
GetMeta() map[string]string
|
2020-01-24 15:04:58 +00:00
|
|
|
GetEnterpriseMeta() *EnterpriseMeta
|
2019-03-19 22:56:17 +00:00
|
|
|
GetRaftIndex() *RaftIndex
|
2019-03-19 17:06:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// UpdatableConfigEntry is the optional interface implemented by a ConfigEntry
|
|
|
|
// if it wants more control over how the update part of upsert works
|
|
|
|
// differently than a straight create. By default without this implementation
|
|
|
|
// all upsert operations are replacements.
|
|
|
|
type UpdatableConfigEntry interface {
|
|
|
|
// UpdateOver is called from the state machine when an identically named
|
|
|
|
// config entry already exists. This lets the config entry optionally
|
|
|
|
// choose to use existing information from a config entry (such as
|
|
|
|
// CreateTime) to slightly adjust how the update actually happens.
|
|
|
|
UpdateOver(prev ConfigEntry) error
|
|
|
|
ConfigEntry
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:06:46 +00:00
|
|
|
// ServiceConfiguration is the top-level struct for the configuration of a service
|
|
|
|
// across the entire cluster.
|
2019-03-19 22:56:17 +00:00
|
|
|
type ServiceConfigEntry struct {
|
2019-06-18 00:52:01 +00:00
|
|
|
Kind string
|
|
|
|
Name string
|
|
|
|
Protocol string
|
2020-05-27 18:28:28 +00:00
|
|
|
MeshGateway MeshGatewayConfig `json:",omitempty" alias:"mesh_gateway"`
|
2019-09-26 02:55:52 +00:00
|
|
|
Expose ExposeConfig `json:",omitempty"`
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2020-05-27 18:28:28 +00:00
|
|
|
ExternalSNI string `json:",omitempty" alias:"external_sni"`
|
2019-08-19 17:19:44 +00:00
|
|
|
|
2019-05-01 23:39:31 +00:00
|
|
|
// TODO(banks): enable this once we have upstreams supported too. Enabling
|
|
|
|
// sidecars actually makes no sense and adds complications when you don't
|
|
|
|
// allow upstreams to be specified centrally too.
|
|
|
|
//
|
|
|
|
// Connect ConnectConfiguration
|
2019-03-19 17:06:46 +00:00
|
|
|
|
2020-09-02 19:10:25 +00:00
|
|
|
Meta map[string]string `json:",omitempty"`
|
2020-01-24 15:04:58 +00:00
|
|
|
EnterpriseMeta `hcl:",squash" mapstructure:",squash"`
|
2019-03-19 17:06:46 +00:00
|
|
|
RaftIndex
|
|
|
|
}
|
|
|
|
|
server: config entry replication now correctly uses namespaces in comparisons (#9024)
Previously config entries sharing a kind & name but in different
namespaces could occasionally cause "stuck states" in replication
because the namespace fields were ignored during the differential
comparison phase.
Example:
Two config entries written to the primary:
kind=A,name=web,namespace=bar
kind=A,name=web,namespace=foo
Under the covers these both get saved to memdb, so they are sorted by
all 3 components (kind,name,namespace) during natural iteration. This
means that before the replication code does it's own incomplete sort,
the underlying data IS sorted by namespace ascending (bar comes before
foo).
After one pass of replication the primary and secondary datacenters have
the same set of config entries present. If
"kind=A,name=web,namespace=bar" were to be deleted, then things get
weird. Before replication the two sides look like:
primary: [
kind=A,name=web,namespace=foo
]
secondary: [
kind=A,name=web,namespace=bar
kind=A,name=web,namespace=foo
]
The differential comparison phase walks these two lists in sorted order
and first compares "kind=A,name=web,namespace=foo" vs
"kind=A,name=web,namespace=bar" and falsely determines they are the SAME
and are thus cause an update of "kind=A,name=web,namespace=foo". Then it
compares "<nothing>" with "kind=A,name=web,namespace=foo" and falsely
determines that the latter should be DELETED.
During reconciliation the deletes are processed before updates, and so
for a brief moment in the secondary "kind=A,name=web,namespace=foo" is
erroneously deleted and then immediately restored.
Unfortunately after this replication phase the final state is identical
to the initial state, so when it loops around again (rate limited) it
repeats the same set of operations indefinitely.
2020-10-23 18:41:54 +00:00
|
|
|
func (e *ServiceConfigEntry) Clone() *ServiceConfigEntry {
|
|
|
|
e2 := *e
|
|
|
|
e2.Expose = e.Expose.Clone()
|
|
|
|
return &e2
|
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
func (e *ServiceConfigEntry) GetKind() string {
|
2019-03-19 17:06:46 +00:00
|
|
|
return ServiceDefaults
|
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
func (e *ServiceConfigEntry) GetName() string {
|
2019-03-22 16:25:37 +00:00
|
|
|
if e == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
return e.Name
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:10:25 +00:00
|
|
|
func (e *ServiceConfigEntry) GetMeta() map[string]string {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.Meta
|
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
func (e *ServiceConfigEntry) Normalize() error {
|
2019-03-22 16:25:37 +00:00
|
|
|
if e == nil {
|
|
|
|
return fmt.Errorf("config entry is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Kind = ServiceDefaults
|
2019-08-19 15:44:06 +00:00
|
|
|
e.Protocol = strings.ToLower(e.Protocol)
|
2019-03-22 16:25:37 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
e.EnterpriseMeta.Normalize()
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ServiceConfigEntry) Validate() error {
|
2020-09-02 19:10:25 +00:00
|
|
|
return validateConfigEntryMeta(e.Meta)
|
2019-03-20 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func (e *ServiceConfigEntry) CanRead(authz acl.Authorizer) bool {
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
e.FillAuthzContext(&authzContext)
|
|
|
|
return authz.ServiceRead(e.Name, &authzContext) == acl.Allow
|
2019-04-10 21:27:28 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func (e *ServiceConfigEntry) CanWrite(authz acl.Authorizer) bool {
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
e.FillAuthzContext(&authzContext)
|
|
|
|
return authz.ServiceWrite(e.Name, &authzContext) == acl.Allow
|
2019-04-10 21:27:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
func (e *ServiceConfigEntry) GetRaftIndex() *RaftIndex {
|
2019-03-22 16:25:37 +00:00
|
|
|
if e == nil {
|
|
|
|
return &RaftIndex{}
|
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
return &e.RaftIndex
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func (e *ServiceConfigEntry) GetEnterpriseMeta() *EnterpriseMeta {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &e.EnterpriseMeta
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:06:46 +00:00
|
|
|
type ConnectConfiguration struct {
|
|
|
|
SidecarProxy bool
|
|
|
|
}
|
|
|
|
|
2019-03-19 22:56:17 +00:00
|
|
|
// ProxyConfigEntry is the top-level struct for global proxy configuration defaults.
|
|
|
|
type ProxyConfigEntry struct {
|
2019-06-18 00:52:01 +00:00
|
|
|
Kind string
|
|
|
|
Name string
|
|
|
|
Config map[string]interface{}
|
2020-05-27 18:28:28 +00:00
|
|
|
MeshGateway MeshGatewayConfig `json:",omitempty" alias:"mesh_gateway"`
|
2019-09-26 02:55:52 +00:00
|
|
|
Expose ExposeConfig `json:",omitempty"`
|
2019-03-19 22:56:17 +00:00
|
|
|
|
2020-09-02 19:10:25 +00:00
|
|
|
Meta map[string]string `json:",omitempty"`
|
2020-01-24 15:04:58 +00:00
|
|
|
EnterpriseMeta `hcl:",squash" mapstructure:",squash"`
|
2019-03-19 22:56:17 +00:00
|
|
|
RaftIndex
|
2019-03-19 17:06:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
func (e *ProxyConfigEntry) GetKind() string {
|
2019-03-19 17:06:46 +00:00
|
|
|
return ProxyDefaults
|
|
|
|
}
|
2019-03-19 22:56:17 +00:00
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
func (e *ProxyConfigEntry) GetName() string {
|
2019-03-22 16:25:37 +00:00
|
|
|
if e == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
return e.Name
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 19:10:25 +00:00
|
|
|
func (e *ProxyConfigEntry) GetMeta() map[string]string {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.Meta
|
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
func (e *ProxyConfigEntry) Normalize() error {
|
2019-03-22 16:25:37 +00:00
|
|
|
if e == nil {
|
|
|
|
return fmt.Errorf("config entry is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Kind = ProxyDefaults
|
2019-04-07 06:38:08 +00:00
|
|
|
e.Name = ProxyConfigGlobal
|
2019-03-22 16:25:37 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
e.EnterpriseMeta.Normalize()
|
|
|
|
|
2019-03-19 22:56:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
func (e *ProxyConfigEntry) Validate() error {
|
2019-03-22 16:25:37 +00:00
|
|
|
if e == nil {
|
|
|
|
return fmt.Errorf("config entry is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Name != ProxyConfigGlobal {
|
|
|
|
return fmt.Errorf("invalid name (%q), only %q is supported", e.Name, ProxyConfigGlobal)
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:10:25 +00:00
|
|
|
if err := validateConfigEntryMeta(e.Meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
return e.validateEnterpriseMeta()
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func (e *ProxyConfigEntry) CanRead(authz acl.Authorizer) bool {
|
2019-04-10 21:27:28 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func (e *ProxyConfigEntry) CanWrite(authz acl.Authorizer) bool {
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
e.FillAuthzContext(&authzContext)
|
|
|
|
return authz.OperatorWrite(&authzContext) == acl.Allow
|
2019-04-10 21:27:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
func (e *ProxyConfigEntry) GetRaftIndex() *RaftIndex {
|
2019-03-22 16:25:37 +00:00
|
|
|
if e == nil {
|
|
|
|
return &RaftIndex{}
|
|
|
|
}
|
|
|
|
|
2019-03-20 23:13:13 +00:00
|
|
|
return &e.RaftIndex
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func (e *ProxyConfigEntry) GetEnterpriseMeta() *EnterpriseMeta {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &e.EnterpriseMeta
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
func (e *ProxyConfigEntry) MarshalBinary() (data []byte, err error) {
|
|
|
|
// We mainly want to implement the BinaryMarshaller interface so that
|
|
|
|
// we can fixup some msgpack types to coerce them into JSON compatible
|
|
|
|
// values. No special encoding needs to be done - we just simply msgpack
|
|
|
|
// encode the struct which requires a type alias to prevent recursively
|
|
|
|
// calling this function.
|
|
|
|
|
|
|
|
type alias ProxyConfigEntry
|
|
|
|
|
|
|
|
a := alias(*e)
|
|
|
|
|
|
|
|
// bs will grow if needed but allocate enough to avoid reallocation in common
|
|
|
|
// case.
|
|
|
|
bs := make([]byte, 128)
|
2020-02-07 21:50:24 +00:00
|
|
|
enc := codec.NewEncoderBytes(&bs, MsgpackHandle)
|
2019-04-29 22:08:09 +00:00
|
|
|
err = enc.Encode(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProxyConfigEntry) UnmarshalBinary(data []byte) error {
|
|
|
|
// The goal here is to add a post-decoding operation to
|
|
|
|
// decoding of a ProxyConfigEntry. The cleanest way I could
|
|
|
|
// find to do so was to implement the BinaryMarshaller interface
|
|
|
|
// and use a type alias to do the original round of decoding,
|
|
|
|
// followed by a MapWalk of the Config to coerce everything
|
|
|
|
// into JSON compatible types.
|
|
|
|
type alias ProxyConfigEntry
|
|
|
|
|
|
|
|
var a alias
|
2020-02-07 21:50:24 +00:00
|
|
|
dec := codec.NewDecoderBytes(data, MsgpackHandle)
|
2019-04-29 22:08:09 +00:00
|
|
|
if err := dec.Decode(&a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = ProxyConfigEntry(a)
|
|
|
|
|
|
|
|
config, err := lib.MapWalk(e.Config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Config = config
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeConfigEntry can be used to decode a ConfigEntry from a raw map value.
|
|
|
|
// Currently its used in the HTTP API to decode ConfigEntry structs coming from
|
|
|
|
// JSON. Unlike some of our custom binary encodings we don't have a preamble including
|
|
|
|
// the kind so we will not have a concrete type to decode into. In those cases we must
|
|
|
|
// first decode into a map[string]interface{} and then call this function to decode
|
|
|
|
// into a concrete type.
|
2019-07-12 17:20:30 +00:00
|
|
|
//
|
|
|
|
// There is an 'api' variation of this in
|
|
|
|
// command/config/write/config_write.go:newDecodeConfigEntry
|
2019-04-29 22:08:09 +00:00
|
|
|
func DecodeConfigEntry(raw map[string]interface{}) (ConfigEntry, error) {
|
|
|
|
var entry ConfigEntry
|
|
|
|
|
|
|
|
kindVal, ok := raw["Kind"]
|
|
|
|
if !ok {
|
2019-07-12 17:20:30 +00:00
|
|
|
kindVal, ok = raw["kind"]
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Payload does not contain a kind/Kind key at the top level")
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if kindStr, ok := kindVal.(string); ok {
|
|
|
|
newEntry, err := MakeConfigEntry(kindStr, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entry = newEntry
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Kind value in payload is not a string")
|
|
|
|
}
|
|
|
|
|
2019-07-12 17:20:30 +00:00
|
|
|
var md mapstructure.Metadata
|
2019-04-29 22:08:09 +00:00
|
|
|
decodeConf := &mapstructure.DecoderConfig{
|
2020-05-27 18:42:01 +00:00
|
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
2020-05-27 17:02:22 +00:00
|
|
|
decode.HookWeakDecodeFromSlice,
|
2020-05-27 18:42:01 +00:00
|
|
|
decode.HookTranslateKeys,
|
|
|
|
mapstructure.StringToTimeDurationHookFunc(),
|
2020-10-06 18:24:05 +00:00
|
|
|
mapstructure.StringToTimeHookFunc(time.RFC3339),
|
2020-05-27 18:42:01 +00:00
|
|
|
),
|
2019-07-12 17:20:30 +00:00
|
|
|
Metadata: &md,
|
|
|
|
Result: &entry,
|
|
|
|
WeaklyTypedInput: true,
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(decodeConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-12 17:20:30 +00:00
|
|
|
if err := decoder.Decode(raw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-25 19:58:29 +00:00
|
|
|
if err := validateUnusedKeys(md.Unused); err != nil {
|
2019-07-12 17:20:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2019-03-19 22:56:17 +00:00
|
|
|
type ConfigEntryOp string
|
|
|
|
|
|
|
|
const (
|
2019-04-29 22:08:09 +00:00
|
|
|
ConfigEntryUpsert ConfigEntryOp = "upsert"
|
|
|
|
ConfigEntryUpsertCAS ConfigEntryOp = "upsert-cas"
|
|
|
|
ConfigEntryDelete ConfigEntryOp = "delete"
|
2019-03-19 22:56:17 +00:00
|
|
|
)
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
// ConfigEntryRequest is used when creating/updating/deleting a ConfigEntry.
|
2019-03-19 22:56:17 +00:00
|
|
|
type ConfigEntryRequest struct {
|
2019-04-07 06:38:08 +00:00
|
|
|
Op ConfigEntryOp
|
|
|
|
Datacenter string
|
|
|
|
Entry ConfigEntry
|
|
|
|
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigEntryRequest) RequestDatacenter() string {
|
|
|
|
return c.Datacenter
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
2019-03-28 06:56:35 +00:00
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
func (c *ConfigEntryRequest) MarshalBinary() (data []byte, err error) {
|
2019-03-28 06:56:35 +00:00
|
|
|
// bs will grow if needed but allocate enough to avoid reallocation in common
|
|
|
|
// case.
|
|
|
|
bs := make([]byte, 128)
|
2020-02-07 21:50:24 +00:00
|
|
|
enc := codec.NewEncoderBytes(&bs, MsgpackHandle)
|
2019-03-28 06:56:35 +00:00
|
|
|
// Encode kind first
|
2019-04-07 06:38:08 +00:00
|
|
|
err = enc.Encode(c.Entry.GetKind())
|
2019-03-28 06:56:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Then actual value using alias trick to avoid infinite recursion
|
|
|
|
type Alias ConfigEntryRequest
|
|
|
|
err = enc.Encode(struct {
|
|
|
|
*Alias
|
|
|
|
}{
|
2019-04-07 06:38:08 +00:00
|
|
|
Alias: (*Alias)(c),
|
2019-03-28 06:56:35 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
func (c *ConfigEntryRequest) UnmarshalBinary(data []byte) error {
|
2019-03-28 06:56:35 +00:00
|
|
|
// First decode the kind prefix
|
|
|
|
var kind string
|
2020-02-07 21:50:24 +00:00
|
|
|
dec := codec.NewDecoderBytes(data, MsgpackHandle)
|
2019-03-28 06:56:35 +00:00
|
|
|
if err := dec.Decode(&kind); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then decode the real thing with appropriate kind of ConfigEntry
|
2019-04-10 21:27:28 +00:00
|
|
|
entry, err := MakeConfigEntry(kind, "")
|
2019-04-02 22:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
c.Entry = entry
|
2019-03-28 06:56:35 +00:00
|
|
|
|
|
|
|
// Alias juggling to prevent infinite recursive calls back to this decode
|
|
|
|
// method.
|
|
|
|
type Alias ConfigEntryRequest
|
|
|
|
as := struct {
|
|
|
|
*Alias
|
|
|
|
}{
|
2019-04-07 06:38:08 +00:00
|
|
|
Alias: (*Alias)(c),
|
2019-03-28 06:56:35 +00:00
|
|
|
}
|
|
|
|
if err := dec.Decode(&as); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:27:28 +00:00
|
|
|
func MakeConfigEntry(kind, name string) (ConfigEntry, error) {
|
2019-03-28 06:56:35 +00:00
|
|
|
switch kind {
|
|
|
|
case ServiceDefaults:
|
2019-04-10 21:27:28 +00:00
|
|
|
return &ServiceConfigEntry{Name: name}, nil
|
2019-03-28 06:56:35 +00:00
|
|
|
case ProxyDefaults:
|
2019-04-10 21:27:28 +00:00
|
|
|
return &ProxyConfigEntry{Name: name}, nil
|
2019-06-27 17:37:43 +00:00
|
|
|
case ServiceRouter:
|
|
|
|
return &ServiceRouterConfigEntry{Name: name}, nil
|
|
|
|
case ServiceSplitter:
|
|
|
|
return &ServiceSplitterConfigEntry{Name: name}, nil
|
|
|
|
case ServiceResolver:
|
|
|
|
return &ServiceResolverConfigEntry{Name: name}, nil
|
2020-03-31 16:59:10 +00:00
|
|
|
case IngressGateway:
|
|
|
|
return &IngressGatewayConfigEntry{Name: name}, nil
|
2020-03-31 19:27:32 +00:00
|
|
|
case TerminatingGateway:
|
|
|
|
return &TerminatingGatewayConfigEntry{Name: name}, nil
|
2020-10-06 18:24:05 +00:00
|
|
|
case ServiceIntentions:
|
|
|
|
return &ServiceIntentionsConfigEntry{Name: name}, nil
|
2019-03-28 06:56:35 +00:00
|
|
|
default:
|
2019-04-02 22:42:12 +00:00
|
|
|
return nil, fmt.Errorf("invalid config entry kind: %s", kind)
|
2019-03-28 06:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
|
2019-04-30 22:19:19 +00:00
|
|
|
func ValidateConfigEntryKind(kind string) bool {
|
|
|
|
switch kind {
|
|
|
|
case ServiceDefaults, ProxyDefaults:
|
|
|
|
return true
|
2019-06-27 17:37:43 +00:00
|
|
|
case ServiceRouter, ServiceSplitter, ServiceResolver:
|
|
|
|
return true
|
2020-03-31 19:27:32 +00:00
|
|
|
case IngressGateway, TerminatingGateway:
|
2020-03-31 16:59:10 +00:00
|
|
|
return true
|
2020-10-06 18:24:05 +00:00
|
|
|
case ServiceIntentions:
|
|
|
|
return true
|
2019-04-30 22:19:19 +00:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
// ConfigEntryQuery is used when requesting info about a config entry.
|
|
|
|
type ConfigEntryQuery struct {
|
|
|
|
Kind string
|
|
|
|
Name string
|
|
|
|
Datacenter string
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
EnterpriseMeta `hcl:",squash" mapstructure:",squash"`
|
2019-04-07 06:38:08 +00:00
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigEntryQuery) RequestDatacenter() string {
|
|
|
|
return c.Datacenter
|
|
|
|
}
|
|
|
|
|
2019-07-02 00:45:42 +00:00
|
|
|
func (r *ConfigEntryQuery) CacheInfo() cache.RequestInfo {
|
|
|
|
info := cache.RequestInfo{
|
|
|
|
Token: r.Token,
|
|
|
|
Datacenter: r.Datacenter,
|
|
|
|
MinIndex: r.MinQueryIndex,
|
|
|
|
Timeout: r.MaxQueryTime,
|
|
|
|
MaxAge: r.MaxAge,
|
|
|
|
MustRevalidate: r.MustRevalidate,
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := hashstructure.Hash([]interface{}{
|
|
|
|
r.Kind,
|
|
|
|
r.Name,
|
|
|
|
r.Filter,
|
2020-02-11 00:26:01 +00:00
|
|
|
r.EnterpriseMeta,
|
2019-07-02 00:45:42 +00:00
|
|
|
}, nil)
|
|
|
|
if err == nil {
|
|
|
|
// If there is an error, we don't set the key. A blank key forces
|
|
|
|
// no cache for this request so the request is forwarded directly
|
|
|
|
// to the server.
|
|
|
|
info.Key = strconv.FormatUint(v, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// ConfigEntryListAllRequest is used when requesting to list all config entries
|
|
|
|
// of a set of kinds.
|
|
|
|
type ConfigEntryListAllRequest struct {
|
|
|
|
// Kinds should always be set. For backwards compatibility with versions
|
|
|
|
// prior to 1.9.0, if this is omitted or left empty it is assumed to mean
|
|
|
|
// the subset of config entry kinds that were present in 1.8.0:
|
|
|
|
//
|
|
|
|
// proxy-defaults, service-defaults, service-resolver, service-splitter,
|
|
|
|
// service-router, terminating-gateway, and ingress-gateway.
|
|
|
|
Kinds []string
|
|
|
|
Datacenter string
|
|
|
|
|
|
|
|
EnterpriseMeta `hcl:",squash" mapstructure:",squash"`
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ConfigEntryListAllRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
// ServiceConfigRequest is used when requesting the resolved configuration
|
|
|
|
// for a service.
|
|
|
|
type ServiceConfigRequest struct {
|
|
|
|
Name string
|
|
|
|
Datacenter string
|
2020-01-24 15:04:58 +00:00
|
|
|
// DEPRECATED
|
|
|
|
// Upstreams is a list of upstream service names to use for resolving the service config
|
|
|
|
// UpstreamIDs should be used instead which can encode more than just the name to
|
|
|
|
// uniquely identify a service.
|
|
|
|
Upstreams []string
|
|
|
|
UpstreamIDs []ServiceID
|
2019-04-07 06:38:08 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
EnterpriseMeta `hcl:",squash" mapstructure:",squash"`
|
2019-04-07 06:38:08 +00:00
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceConfigRequest) RequestDatacenter() string {
|
|
|
|
return s.Datacenter
|
|
|
|
}
|
|
|
|
|
2019-04-18 04:35:19 +00:00
|
|
|
func (r *ServiceConfigRequest) CacheInfo() cache.RequestInfo {
|
|
|
|
info := cache.RequestInfo{
|
|
|
|
Token: r.Token,
|
|
|
|
Datacenter: r.Datacenter,
|
|
|
|
MinIndex: r.MinQueryIndex,
|
|
|
|
Timeout: r.MaxQueryTime,
|
|
|
|
MaxAge: r.MaxAge,
|
|
|
|
MustRevalidate: r.MustRevalidate,
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:39:31 +00:00
|
|
|
// To calculate the cache key we only hash the service name and upstream set.
|
|
|
|
// We don't want ordering of the upstreams to affect the outcome so use an
|
|
|
|
// anonymous struct field with hash:set behavior. Note the order of fields in
|
|
|
|
// the slice would affect cache keys if we ever persist between agent restarts
|
|
|
|
// and change it.
|
|
|
|
v, err := hashstructure.Hash(struct {
|
2020-02-11 00:26:01 +00:00
|
|
|
Name string
|
|
|
|
EnterpriseMeta EnterpriseMeta
|
|
|
|
Upstreams []string `hash:"set"`
|
2019-05-01 23:39:31 +00:00
|
|
|
}{
|
2020-02-11 00:26:01 +00:00
|
|
|
Name: r.Name,
|
|
|
|
EnterpriseMeta: r.EnterpriseMeta,
|
|
|
|
Upstreams: r.Upstreams,
|
2019-05-01 23:39:31 +00:00
|
|
|
}, nil)
|
2019-04-18 04:35:19 +00:00
|
|
|
if err == nil {
|
|
|
|
// If there is an error, we don't set the key. A blank key forces
|
|
|
|
// no cache for this request so the request is forwarded directly
|
|
|
|
// to the server.
|
|
|
|
info.Key = strconv.FormatUint(v, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
type UpstreamConfig struct {
|
|
|
|
Upstream ServiceID
|
|
|
|
Config map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpstreamConfigs []UpstreamConfig
|
|
|
|
|
|
|
|
func (configs UpstreamConfigs) GetUpstreamConfig(sid ServiceID) (config map[string]interface{}, found bool) {
|
|
|
|
for _, usconf := range configs {
|
2020-12-11 21:10:00 +00:00
|
|
|
if usconf.Upstream.Matches(sid) {
|
2020-01-24 15:04:58 +00:00
|
|
|
return usconf.Config, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
type ServiceConfigResponse struct {
|
2020-01-24 15:04:58 +00:00
|
|
|
ProxyConfig map[string]interface{}
|
|
|
|
UpstreamConfigs map[string]map[string]interface{}
|
|
|
|
UpstreamIDConfigs UpstreamConfigs
|
|
|
|
MeshGateway MeshGatewayConfig `json:",omitempty"`
|
|
|
|
Expose ExposeConfig `json:",omitempty"`
|
2019-04-07 06:38:08 +00:00
|
|
|
QueryMeta
|
|
|
|
}
|
2019-04-29 22:08:09 +00:00
|
|
|
|
2019-08-14 14:08:46 +00:00
|
|
|
func (r *ServiceConfigResponse) Reset() {
|
|
|
|
r.ProxyConfig = nil
|
|
|
|
r.UpstreamConfigs = nil
|
|
|
|
r.MeshGateway = MeshGatewayConfig{}
|
|
|
|
}
|
|
|
|
|
2019-05-02 13:11:33 +00:00
|
|
|
// MarshalBinary writes ServiceConfigResponse as msgpack encoded. It's only here
|
|
|
|
// because we need custom decoding of the raw interface{} values.
|
|
|
|
func (r *ServiceConfigResponse) MarshalBinary() (data []byte, err error) {
|
|
|
|
// bs will grow if needed but allocate enough to avoid reallocation in common
|
|
|
|
// case.
|
|
|
|
bs := make([]byte, 128)
|
2020-02-07 21:50:24 +00:00
|
|
|
enc := codec.NewEncoderBytes(&bs, MsgpackHandle)
|
2019-05-02 13:11:33 +00:00
|
|
|
|
|
|
|
type Alias ServiceConfigResponse
|
|
|
|
|
|
|
|
if err := enc.Encode((*Alias)(r)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBinary decodes msgpack encoded ServiceConfigResponse. It used
|
|
|
|
// default msgpack encoding but fixes up the uint8 strings and other problems we
|
|
|
|
// have with encoding map[string]interface{}.
|
|
|
|
func (r *ServiceConfigResponse) UnmarshalBinary(data []byte) error {
|
2020-02-07 21:50:24 +00:00
|
|
|
dec := codec.NewDecoderBytes(data, MsgpackHandle)
|
2019-05-02 13:11:33 +00:00
|
|
|
|
|
|
|
type Alias ServiceConfigResponse
|
|
|
|
var a Alias
|
|
|
|
|
|
|
|
if err := dec.Decode(&a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*r = ServiceConfigResponse(a)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Fix strings and maps in the returned maps
|
|
|
|
r.ProxyConfig, err = lib.MapWalk(r.ProxyConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k := range r.UpstreamConfigs {
|
|
|
|
r.UpstreamConfigs[k], err = lib.MapWalk(r.UpstreamConfigs[k])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
|
|
|
|
for k := range r.UpstreamIDConfigs {
|
|
|
|
r.UpstreamIDConfigs[k].Config, err = lib.MapWalk(r.UpstreamIDConfigs[k].Config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-05-02 13:11:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
// ConfigEntryResponse returns a single ConfigEntry
|
|
|
|
type ConfigEntryResponse struct {
|
|
|
|
Entry ConfigEntry
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigEntryResponse) MarshalBinary() (data []byte, err error) {
|
|
|
|
// bs will grow if needed but allocate enough to avoid reallocation in common
|
|
|
|
// case.
|
|
|
|
bs := make([]byte, 128)
|
2020-02-07 21:50:24 +00:00
|
|
|
enc := codec.NewEncoderBytes(&bs, MsgpackHandle)
|
2019-04-29 22:08:09 +00:00
|
|
|
|
2019-05-02 19:25:29 +00:00
|
|
|
if c.Entry != nil {
|
|
|
|
if err := enc.Encode(c.Entry.GetKind()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := enc.Encode(c.Entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := enc.Encode(""); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
2019-05-02 19:25:29 +00:00
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
if err := enc.Encode(c.QueryMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigEntryResponse) UnmarshalBinary(data []byte) error {
|
2020-02-07 21:50:24 +00:00
|
|
|
dec := codec.NewDecoderBytes(data, MsgpackHandle)
|
2019-04-29 22:08:09 +00:00
|
|
|
|
|
|
|
var kind string
|
|
|
|
if err := dec.Decode(&kind); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-02 19:25:29 +00:00
|
|
|
if kind != "" {
|
|
|
|
entry, err := MakeConfigEntry(kind, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-29 22:08:09 +00:00
|
|
|
|
2019-05-02 19:25:29 +00:00
|
|
|
if err := dec.Decode(entry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Entry = entry
|
|
|
|
} else {
|
|
|
|
c.Entry = nil
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := dec.Decode(&c.QueryMeta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-01 20:23:36 +00:00
|
|
|
|
|
|
|
// ConfigEntryKindName is a value type useful for maps. You can use:
|
|
|
|
// map[ConfigEntryKindName]Payload
|
|
|
|
// instead of:
|
|
|
|
// map[string]map[string]Payload
|
|
|
|
type ConfigEntryKindName struct {
|
|
|
|
Kind string
|
|
|
|
Name string
|
2020-09-02 15:47:19 +00:00
|
|
|
EnterpriseMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfigEntryKindName(kind, name string, entMeta *EnterpriseMeta) ConfigEntryKindName {
|
|
|
|
ret := ConfigEntryKindName{
|
|
|
|
Kind: kind,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
if entMeta == nil {
|
|
|
|
entMeta = DefaultEnterpriseMeta()
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.EnterpriseMeta = *entMeta
|
|
|
|
ret.EnterpriseMeta.Normalize()
|
|
|
|
return ret
|
2019-07-01 20:23:36 +00:00
|
|
|
}
|
2020-09-02 19:10:25 +00:00
|
|
|
|
|
|
|
func validateConfigEntryMeta(meta map[string]string) error {
|
|
|
|
var err error
|
|
|
|
if len(meta) > metaMaxKeyPairs {
|
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"Meta exceeds maximum element count %d", metaMaxKeyPairs))
|
|
|
|
}
|
|
|
|
for k, v := range meta {
|
|
|
|
if len(k) > metaKeyMaxLength {
|
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"Meta key %q exceeds maximum length %d", k, metaKeyMaxLength))
|
|
|
|
}
|
|
|
|
if len(v) > metaValueMaxLength {
|
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"Meta value for key %q exceeds maximum length %d", k, metaValueMaxLength))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|