2019-04-07 06:38:08 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-14 20:39:12 +00:00
|
|
|
"reflect"
|
2019-04-07 06:38:08 +00:00
|
|
|
"time"
|
|
|
|
|
2021-04-15 19:21:44 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
2020-11-13 02:12:12 +00:00
|
|
|
"github.com/armon/go-metrics/prometheus"
|
2021-04-15 19:21:44 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2022-02-25 21:46:34 +00:00
|
|
|
hashstructure_v2 "github.com/mitchellh/hashstructure/v2"
|
2020-11-13 02:12:12 +00:00
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2022-09-01 14:45:07 +00:00
|
|
|
"github.com/hashicorp/consul/agent/configentry"
|
2019-04-07 06:38:08 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
2020-11-13 02:12:12 +00:00
|
|
|
var ConfigSummaries = []prometheus.SummaryDefinition{
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"config_entry", "apply"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"config_entry", "get"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"config_entry", "list"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"config_entry", "listAll"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"config_entry", "delete"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"config_entry", "resolve_service_config"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
// The ConfigEntry endpoint is used to query centralized config information
|
|
|
|
type ConfigEntry struct {
|
2021-04-15 19:21:44 +00:00
|
|
|
srv *Server
|
|
|
|
logger hclog.Logger
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply does an upsert of the given config entry.
|
2019-04-30 23:27:16 +00:00
|
|
|
func (c *ConfigEntry) Apply(args *structs.ConfigEntryRequest, reply *bool) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
if err := c.srv.validateEnterpriseRequest(args.Entry.GetEnterpriseMeta(), true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-08 16:17:40 +00:00
|
|
|
err := gateWriteToSecondary(args.Datacenter, c.srv.config.Datacenter, c.srv.config.PrimaryDatacenter, args.Entry.GetKind())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:01:13 +00:00
|
|
|
// Ensure that all config entry writes go to the primary datacenter. These will then
|
|
|
|
// be replicated to all the other datacenters.
|
|
|
|
args.Datacenter = c.srv.config.PrimaryDatacenter
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("ConfigEntry.Apply", args, reply); done {
|
2019-04-07 06:38:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "apply"}, time.Now())
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
entMeta := args.Entry.GetEnterpriseMeta()
|
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, entMeta, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if err := c.preflightCheck(args.Entry.GetKind()); err != nil {
|
2019-04-07 06:38:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
// Normalize and validate the incoming config entry as if it came from a user.
|
2020-11-13 20:42:21 +00:00
|
|
|
if err := args.Entry.Normalize(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := args.Entry.Validate(); err != nil {
|
|
|
|
return err
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 19:18:40 +00:00
|
|
|
// Log any applicable warnings about the contents of the config entry.
|
|
|
|
if warnEntry, ok := args.Entry.(structs.WarningConfigEntry); ok {
|
|
|
|
warnings := warnEntry.Warnings()
|
|
|
|
for _, warning := range warnings {
|
|
|
|
c.logger.Warn(warning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 21:45:51 +00:00
|
|
|
if err := args.Entry.CanWrite(authz); err != nil {
|
|
|
|
return err
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
if args.Op != structs.ConfigEntryUpsert && args.Op != structs.ConfigEntryUpsertCAS {
|
|
|
|
args.Op = structs.ConfigEntryUpsert
|
|
|
|
}
|
2022-02-14 20:39:12 +00:00
|
|
|
|
|
|
|
if skip, err := c.shouldSkipOperation(args); err != nil {
|
|
|
|
return err
|
|
|
|
} else if skip {
|
|
|
|
*reply = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
resp, err := c.srv.raftApply(structs.ConfigEntryRequestType, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-30 23:27:16 +00:00
|
|
|
if respBool, ok := resp.(bool); ok {
|
|
|
|
*reply = respBool
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-14 20:39:12 +00:00
|
|
|
// shouldSkipOperation returns true if the result of the operation has
|
|
|
|
// already happened and is safe to skip.
|
|
|
|
//
|
|
|
|
// It is ok if this incorrectly detects something as changed when it
|
|
|
|
// in fact has not, the important thing is that it doesn't do
|
|
|
|
// the reverse and incorrectly detect a change as a no-op.
|
|
|
|
func (c *ConfigEntry) shouldSkipOperation(args *structs.ConfigEntryRequest) (bool, error) {
|
|
|
|
state := c.srv.fsm.State()
|
|
|
|
_, currentEntry, err := state.ConfigEntry(nil, args.Entry.GetKind(), args.Entry.GetName(), args.Entry.GetEnterpriseMeta())
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error reading current config entry value: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch args.Op {
|
|
|
|
case structs.ConfigEntryUpsert, structs.ConfigEntryUpsertCAS:
|
|
|
|
return c.shouldSkipUpsertOperation(currentEntry, args.Entry)
|
|
|
|
case structs.ConfigEntryDelete, structs.ConfigEntryDeleteCAS:
|
|
|
|
return (currentEntry == nil), nil
|
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("invalid config entry operation type: %v", args.Op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigEntry) shouldSkipUpsertOperation(currentEntry, updatedEntry structs.ConfigEntry) (bool, error) {
|
|
|
|
if currentEntry == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentEntry.GetKind() != updatedEntry.GetKind() ||
|
|
|
|
currentEntry.GetName() != updatedEntry.GetName() ||
|
|
|
|
!currentEntry.GetEnterpriseMeta().IsSame(updatedEntry.GetEnterpriseMeta()) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The only reason a fully Normalized and Validated config entry may
|
|
|
|
// legitimately differ from the persisted one is due to the embedded
|
|
|
|
// RaftIndex.
|
|
|
|
//
|
|
|
|
// So, to intercept more no-op upserts we temporarily set the new config
|
|
|
|
// entry's raft index field to that of the existing data for the purposes
|
|
|
|
// of comparison, and then restore it.
|
|
|
|
var (
|
|
|
|
currentRaftIndex = currentEntry.GetRaftIndex()
|
|
|
|
userProvidedRaftIndex = updatedEntry.GetRaftIndex()
|
|
|
|
|
|
|
|
currentRaftIndexCopy = *currentRaftIndex
|
|
|
|
userProvidedRaftIndexCopy = *userProvidedRaftIndex
|
|
|
|
)
|
|
|
|
|
|
|
|
*userProvidedRaftIndex = currentRaftIndexCopy // change
|
|
|
|
same := reflect.DeepEqual(currentEntry, updatedEntry) // compare
|
|
|
|
*userProvidedRaftIndex = userProvidedRaftIndexCopy // restore
|
|
|
|
|
|
|
|
return same, nil
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
// Get returns a single config entry by Kind/Name.
|
2019-04-29 22:08:09 +00:00
|
|
|
func (c *ConfigEntry) Get(args *structs.ConfigEntryQuery, reply *structs.ConfigEntryResponse) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("ConfigEntry.Get", args, reply); done {
|
2019-04-07 06:38:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "get"}, time.Now())
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
2019-04-07 06:38:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-10 21:27:28 +00:00
|
|
|
|
|
|
|
// Create a dummy config entry to check the ACL permissions.
|
|
|
|
lookupEntry, err := structs.MakeConfigEntry(args.Kind, args.Name)
|
|
|
|
if err != nil {
|
2019-04-07 06:38:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-02-07 13:30:40 +00:00
|
|
|
lookupEntry.GetEnterpriseMeta().Merge(&args.EnterpriseMeta)
|
|
|
|
|
2022-03-11 21:45:51 +00:00
|
|
|
if err := lookupEntry.CanRead(authz); err != nil {
|
|
|
|
return err
|
2019-04-10 21:27:28 +00:00
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
|
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
index, entry, err := state.ConfigEntry(ws, args.Kind, args.Name, &args.EnterpriseMeta)
|
2019-04-07 06:38:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-18 17:13:23 +00:00
|
|
|
reply.Index, reply.Entry = index, entry
|
2019-04-10 21:27:28 +00:00
|
|
|
if entry == nil {
|
2022-01-13 00:44:09 +00:00
|
|
|
return errNotFound
|
2019-04-10 21:27:28 +00:00
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns all the config entries of the given kind. If Kind is blank,
|
|
|
|
// all existing config entries will be returned.
|
|
|
|
func (c *ConfigEntry) List(args *structs.ConfigEntryQuery, reply *structs.IndexedConfigEntries) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("ConfigEntry.List", args, reply); done {
|
2019-04-07 06:38:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "list"}, time.Now())
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
2019-04-07 06:38:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-04 21:14:21 +00:00
|
|
|
if args.Kind != "" {
|
|
|
|
if _, err := structs.MakeConfigEntry(args.Kind, ""); err != nil {
|
|
|
|
return fmt.Errorf("invalid config entry kind: %s", args.Kind)
|
|
|
|
}
|
2019-04-30 22:19:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 21:46:34 +00:00
|
|
|
var (
|
|
|
|
priorHash uint64
|
|
|
|
ranOnce bool
|
|
|
|
)
|
2019-04-07 06:38:08 +00:00
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
index, entries, err := state.ConfigEntriesByKind(ws, args.Kind, &args.EnterpriseMeta)
|
2019-04-07 06:38:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the entries returned by ACL permissions.
|
|
|
|
filteredEntries := make([]structs.ConfigEntry, 0, len(entries))
|
|
|
|
for _, entry := range entries {
|
2022-03-11 21:45:51 +00:00
|
|
|
if err := entry.CanRead(authz); err != nil {
|
|
|
|
// TODO we may wish to extract more details from this error to aid user comprehension
|
2021-12-03 17:39:47 +00:00
|
|
|
reply.QueryMeta.ResultsFilteredByACLs = true
|
2019-04-10 21:27:28 +00:00
|
|
|
continue
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
filteredEntries = append(filteredEntries, entry)
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:27:28 +00:00
|
|
|
reply.Kind = args.Kind
|
2019-04-07 06:38:08 +00:00
|
|
|
reply.Index = index
|
|
|
|
reply.Entries = filteredEntries
|
2022-02-25 21:46:34 +00:00
|
|
|
|
|
|
|
// Generate a hash of the content driving this response. Use it to
|
|
|
|
// determine if the response is identical to a prior wakeup.
|
|
|
|
newHash, err := hashstructure_v2.Hash(filteredEntries, hashstructure_v2.FormatV2, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error hashing reply for spurious wakeup suppression: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ranOnce && priorHash == newHash {
|
|
|
|
priorHash = newHash
|
|
|
|
return errNotChanged
|
|
|
|
} else {
|
|
|
|
priorHash = newHash
|
|
|
|
ranOnce = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(reply.Entries) == 0 {
|
|
|
|
return errNotFound
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
var configEntryKindsFromConsul_1_8_0 = []string{
|
|
|
|
structs.ServiceDefaults,
|
|
|
|
structs.ProxyDefaults,
|
|
|
|
structs.ServiceRouter,
|
|
|
|
structs.ServiceSplitter,
|
|
|
|
structs.ServiceResolver,
|
|
|
|
structs.IngressGateway,
|
|
|
|
structs.TerminatingGateway,
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:38:39 +00:00
|
|
|
// ListAll returns all the known configuration entries
|
2020-10-06 18:24:05 +00:00
|
|
|
func (c *ConfigEntry) ListAll(args *structs.ConfigEntryListAllRequest, reply *structs.IndexedGenericConfigEntries) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("ConfigEntry.ListAll", args, reply); done {
|
2019-04-26 17:38:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "listAll"}, time.Now())
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
2019-04-26 17:38:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if len(args.Kinds) == 0 {
|
|
|
|
args.Kinds = configEntryKindsFromConsul_1_8_0
|
|
|
|
}
|
|
|
|
|
|
|
|
kindMap := make(map[string]struct{})
|
|
|
|
for _, kind := range args.Kinds {
|
|
|
|
kindMap[kind] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:38:39 +00:00
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
index, entries, err := state.ConfigEntries(ws, &args.EnterpriseMeta)
|
2019-04-26 17:38:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Filter the entries returned by ACL permissions or by the provided kinds.
|
2019-04-26 17:38:39 +00:00
|
|
|
filteredEntries := make([]structs.ConfigEntry, 0, len(entries))
|
|
|
|
for _, entry := range entries {
|
2022-03-11 21:45:51 +00:00
|
|
|
if err := entry.CanRead(authz); err != nil {
|
|
|
|
// TODO we may wish to extract more details from this error to aid user comprehension
|
2021-12-03 17:39:47 +00:00
|
|
|
reply.QueryMeta.ResultsFilteredByACLs = true
|
2019-04-26 17:38:39 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
// Doing this filter outside of memdb isn't terribly
|
|
|
|
// performant. This kind filter is currently only used across
|
|
|
|
// version upgrades, so in the common case we are going to
|
|
|
|
// always return all of the data anyway, so it should be fine.
|
|
|
|
// If that changes at some point, then we should move this down
|
|
|
|
// into memdb.
|
|
|
|
if _, ok := kindMap[entry.GetKind()]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
2019-04-26 17:38:39 +00:00
|
|
|
filteredEntries = append(filteredEntries, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Entries = filteredEntries
|
|
|
|
reply.Index = index
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
// Delete deletes a config entry.
|
2021-11-01 16:42:01 +00:00
|
|
|
func (c *ConfigEntry) Delete(args *structs.ConfigEntryRequest, reply *structs.ConfigEntryDeleteResponse) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
if err := c.srv.validateEnterpriseRequest(args.Entry.GetEnterpriseMeta(), true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:01:13 +00:00
|
|
|
// Ensure that all config entry writes go to the primary datacenter. These will then
|
|
|
|
// be replicated to all the other datacenters.
|
|
|
|
args.Datacenter = c.srv.config.PrimaryDatacenter
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("ConfigEntry.Delete", args, reply); done {
|
2019-04-07 06:38:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "delete"}, time.Now())
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, args.Entry.GetEnterpriseMeta(), nil)
|
|
|
|
if err != nil {
|
2019-04-07 06:38:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if err := c.preflightCheck(args.Entry.GetKind()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
// Normalize the incoming entry.
|
|
|
|
if err := args.Entry.Normalize(); err != nil {
|
2019-04-07 06:38:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
|
2022-03-11 21:45:51 +00:00
|
|
|
if err := args.Entry.CanWrite(authz); err != nil {
|
|
|
|
return err
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 16:42:01 +00:00
|
|
|
// Only delete and delete-cas ops are supported. If the caller erroneously
|
|
|
|
// sent something else, we assume they meant delete.
|
|
|
|
switch args.Op {
|
|
|
|
case structs.ConfigEntryDelete, structs.ConfigEntryDeleteCAS:
|
|
|
|
default:
|
|
|
|
args.Op = structs.ConfigEntryDelete
|
|
|
|
}
|
|
|
|
|
2022-02-14 20:39:12 +00:00
|
|
|
if skip, err := c.shouldSkipOperation(args); err != nil {
|
|
|
|
return err
|
|
|
|
} else if skip {
|
|
|
|
reply.Deleted = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-01 16:42:01 +00:00
|
|
|
rsp, err := c.srv.raftApply(structs.ConfigEntryRequestType, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Op == structs.ConfigEntryDeleteCAS {
|
|
|
|
// In CAS deletions the FSM will return a boolean value indicating whether the
|
|
|
|
// operation was successful.
|
|
|
|
deleted, _ := rsp.(bool)
|
|
|
|
reply.Deleted = deleted
|
|
|
|
} else {
|
|
|
|
// For non-CAS deletions any non-error result indicates a successful deletion.
|
|
|
|
reply.Deleted = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveServiceConfig
|
|
|
|
func (c *ConfigEntry) ResolveServiceConfig(args *structs.ServiceConfigRequest, reply *structs.ServiceConfigResponse) error {
|
2020-01-24 15:04:58 +00:00
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("ConfigEntry.ResolveServiceConfig", args, reply); done {
|
2019-04-07 06:38:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "resolve_service_config"}, time.Now())
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
2019-04-07 06:38:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().ServiceReadAllowed(args.Name, &authzContext); err != nil {
|
|
|
|
return err
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 21:46:34 +00:00
|
|
|
var (
|
|
|
|
priorHash uint64
|
|
|
|
ranOnce bool
|
|
|
|
)
|
2019-04-07 06:38:08 +00:00
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2022-02-25 21:46:34 +00:00
|
|
|
// Fetch all relevant config entries.
|
|
|
|
index, entries, err := state.ReadResolvedServiceConfigEntries(
|
|
|
|
ws,
|
|
|
|
args.Name,
|
|
|
|
&args.EnterpriseMeta,
|
2023-02-03 15:51:53 +00:00
|
|
|
args.GetLocalUpstreamIDs(),
|
2022-02-25 21:46:34 +00:00
|
|
|
args.Mode,
|
|
|
|
)
|
2021-03-11 04:05:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-01 23:39:31 +00:00
|
|
|
|
2022-02-25 21:46:34 +00:00
|
|
|
// Generate a hash of the config entry content driving this
|
|
|
|
// response. Use it to determine if the response is identical to a
|
|
|
|
// prior wakeup.
|
|
|
|
newHash, err := hashstructure_v2.Hash(entries, hashstructure_v2.FormatV2, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error hashing reply for spurious wakeup suppression: %w", err)
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
2019-05-01 23:39:31 +00:00
|
|
|
|
2022-02-25 21:46:34 +00:00
|
|
|
if ranOnce && priorHash == newHash {
|
|
|
|
priorHash = newHash
|
|
|
|
reply.Index = index
|
|
|
|
// NOTE: the prior response is still alive inside of *reply, which
|
|
|
|
// is desirable
|
|
|
|
return errNotChanged
|
|
|
|
} else {
|
|
|
|
priorHash = newHash
|
|
|
|
ranOnce = true
|
|
|
|
}
|
2021-03-20 04:03:17 +00:00
|
|
|
|
2022-09-01 14:45:07 +00:00
|
|
|
thisReply, err := configentry.ComputeResolvedServiceConfig(
|
2022-02-25 21:46:34 +00:00
|
|
|
args,
|
|
|
|
entries,
|
2022-05-25 20:20:17 +00:00
|
|
|
c.logger,
|
2021-03-20 04:03:17 +00:00
|
|
|
)
|
2022-02-25 21:46:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
thisReply.Index = index
|
2021-03-20 04:03:17 +00:00
|
|
|
|
2022-02-25 21:46:34 +00:00
|
|
|
*reply = *thisReply
|
|
|
|
if entries.IsEmpty() {
|
|
|
|
// No config entries factored into this reply; it's a default.
|
|
|
|
return errNotFound
|
2021-03-20 04:03:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 21:46:34 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
|
2021-11-08 16:17:40 +00:00
|
|
|
func gateWriteToSecondary(targetDC, localDC, primaryDC, kind string) error {
|
2021-12-04 22:16:15 +00:00
|
|
|
// ExportedServices entries are gated from interactions from secondary DCs
|
2021-11-08 16:17:40 +00:00
|
|
|
// because non-default partitions cannot be created in secondaries
|
|
|
|
// and services cannot be exported to another datacenter.
|
2021-12-03 06:50:38 +00:00
|
|
|
if kind != structs.ExportedServices {
|
2021-11-08 16:17:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-08 16:26:14 +00:00
|
|
|
if localDC == "" {
|
|
|
|
// This should not happen because the datacenter is defaulted in DefaultConfig.
|
|
|
|
return fmt.Errorf("unknown local datacenter")
|
|
|
|
}
|
2021-11-08 16:17:40 +00:00
|
|
|
|
|
|
|
if primaryDC == "" {
|
|
|
|
primaryDC = localDC
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case targetDC == "" && localDC != primaryDC:
|
2021-12-03 06:50:38 +00:00
|
|
|
return fmt.Errorf("exported-services writes in secondary datacenters must target the primary datacenter explicitly.")
|
2021-11-08 16:17:40 +00:00
|
|
|
|
|
|
|
case targetDC != "" && targetDC != primaryDC:
|
2021-12-03 06:50:38 +00:00
|
|
|
return fmt.Errorf("exported-services writes must not target secondary datacenters.")
|
2021-11-08 16:17:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// preflightCheck is meant to have kind-specific system validation outside of
|
|
|
|
// content validation. The initial use case is restricting the ability to do
|
|
|
|
// writes of service-intentions until the system is finished migration.
|
|
|
|
func (c *ConfigEntry) preflightCheck(kind string) error {
|
|
|
|
switch kind {
|
|
|
|
case structs.ServiceIntentions:
|
2020-11-13 22:19:12 +00:00
|
|
|
// Exit early if Connect hasn't been enabled.
|
|
|
|
if !c.srv.config.ConnectEnabled {
|
|
|
|
return ErrConnectNotEnabled
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
usingConfigEntries, err := c.srv.fsm.State().AreIntentionsInConfigEntries()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("system metadata lookup failed: %v", err)
|
|
|
|
}
|
|
|
|
if !usingConfigEntries {
|
|
|
|
return ErrIntentionsNotUpgradedYet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|