2019-04-07 06:38:08 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
"github.com/mitchellh/copystructure"
|
2020-11-13 02:12:12 +00:00
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
if authz != nil && !args.Entry.CanWrite(authz) {
|
2019-04-10 21:27:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
if authz != nil && !lookupEntry.CanRead(authz) {
|
2019-04-10 21:27:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index = index
|
2019-04-10 21:27:28 +00:00
|
|
|
if entry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
reply.Entry = entry
|
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
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-24 15:04:58 +00:00
|
|
|
if authz != nil && !entry.CanRead(authz) {
|
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
|
|
|
|
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 {
|
2020-01-24 15:04:58 +00:00
|
|
|
if authz != nil && !entry.CanRead(authz) {
|
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.
|
|
|
|
func (c *ConfigEntry) Delete(args *structs.ConfigEntryRequest, reply *struct{}) 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
|
|
|
|
|
|
|
if authz != nil && !args.Entry.CanWrite(authz) {
|
2019-04-10 21:27:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args.Op = structs.ConfigEntryDelete
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = c.srv.raftApply(structs.ConfigEntryRequestType, args)
|
|
|
|
return err
|
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
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
if authz != nil && authz.ServiceRead(args.Name, &authzContext) != acl.Allow {
|
2019-04-07 06:38:08 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2021-05-14 15:21:44 +00:00
|
|
|
var thisReply structs.ServiceConfigResponse
|
|
|
|
|
|
|
|
thisReply.MeshGateway.Mode = structs.MeshGatewayModeDefault
|
2021-03-17 22:45:13 +00:00
|
|
|
// TODO(freddy) Refactor this into smaller set of state store functions
|
2021-03-11 04:05:11 +00:00
|
|
|
// Pass the WatchSet to both the service and proxy config lookups. If either is updated during the
|
|
|
|
// blocking query, this function will be rerun and these state store lookups will both be current.
|
2021-03-15 22:38:01 +00:00
|
|
|
// We use the default enterprise meta to look up the global proxy defaults because they are not namespaced.
|
2021-03-31 20:21:21 +00:00
|
|
|
_, proxyEntry, err := state.ConfigEntry(ws, structs.ProxyDefaults, structs.ProxyConfigGlobal, &args.EnterpriseMeta)
|
2019-04-07 06:38:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-11 04:05:11 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
proxyConf *structs.ProxyConfigEntry
|
|
|
|
proxyConfGlobalProtocol string
|
|
|
|
ok bool
|
|
|
|
)
|
2019-04-23 06:39:02 +00:00
|
|
|
if proxyEntry != nil {
|
|
|
|
proxyConf, ok = proxyEntry.(*structs.ProxyConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid proxy config type %T", proxyEntry)
|
|
|
|
}
|
2019-05-02 13:12:21 +00:00
|
|
|
// Apply the proxy defaults to the sidecar's proxy config
|
2019-05-06 16:09:59 +00:00
|
|
|
mapCopy, err := copystructure.Copy(proxyConf.Config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to copy global proxy-defaults: %v", err)
|
|
|
|
}
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.ProxyConfig = mapCopy.(map[string]interface{})
|
|
|
|
thisReply.Mode = proxyConf.Mode
|
|
|
|
thisReply.TransparentProxy = proxyConf.TransparentProxy
|
|
|
|
thisReply.MeshGateway = proxyConf.MeshGateway
|
|
|
|
thisReply.Expose = proxyConf.Expose
|
2021-03-11 04:05:11 +00:00
|
|
|
|
|
|
|
// Extract the global protocol from proxyConf for upstream configs.
|
|
|
|
rawProtocol := proxyConf.Config["protocol"]
|
|
|
|
if rawProtocol != nil {
|
|
|
|
proxyConfGlobalProtocol, ok = rawProtocol.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid protocol type %T", rawProtocol)
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 04:05:11 +00:00
|
|
|
index, serviceEntry, err := state.ConfigEntry(ws, structs.ServiceDefaults, args.Name, &args.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.Index = index
|
2019-05-01 23:39:31 +00:00
|
|
|
|
2021-03-11 04:05:11 +00:00
|
|
|
var serviceConf *structs.ServiceConfigEntry
|
|
|
|
if serviceEntry != nil {
|
|
|
|
serviceConf, ok = serviceEntry.(*structs.ServiceConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid service config type %T", serviceEntry)
|
|
|
|
}
|
2019-09-26 02:55:52 +00:00
|
|
|
if serviceConf.Expose.Checks {
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.Expose.Checks = true
|
2019-09-26 02:55:52 +00:00
|
|
|
}
|
|
|
|
if len(serviceConf.Expose.Paths) >= 1 {
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.Expose.Paths = serviceConf.Expose.Paths
|
2019-09-26 02:55:52 +00:00
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
if serviceConf.MeshGateway.Mode != structs.MeshGatewayModeDefault {
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.MeshGateway.Mode = serviceConf.MeshGateway.Mode
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
if serviceConf.Protocol != "" {
|
2021-05-14 15:21:44 +00:00
|
|
|
if thisReply.ProxyConfig == nil {
|
|
|
|
thisReply.ProxyConfig = make(map[string]interface{})
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.ProxyConfig["protocol"] = serviceConf.Protocol
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
2021-04-12 15:35:14 +00:00
|
|
|
if serviceConf.TransparentProxy.OutboundListenerPort != 0 {
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.TransparentProxy.OutboundListenerPort = serviceConf.TransparentProxy.OutboundListenerPort
|
2021-04-12 15:35:14 +00:00
|
|
|
}
|
2021-06-09 20:34:17 +00:00
|
|
|
if serviceConf.TransparentProxy.DialedDirectly {
|
|
|
|
thisReply.TransparentProxy.DialedDirectly = serviceConf.TransparentProxy.DialedDirectly
|
|
|
|
}
|
2021-04-12 15:35:14 +00:00
|
|
|
if serviceConf.Mode != structs.ProxyModeDefault {
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.Mode = serviceConf.Mode
|
2021-03-11 06:08:41 +00:00
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
2019-05-01 23:39:31 +00:00
|
|
|
|
2021-03-11 04:05:11 +00:00
|
|
|
// First collect all upstreams into a set of seen upstreams.
|
|
|
|
// Upstreams can come from:
|
|
|
|
// - Explicitly from proxy registrations, and therefore as an argument to this RPC endpoint
|
|
|
|
// - Implicitly from centralized upstream config in service-defaults
|
|
|
|
seenUpstreams := map[structs.ServiceID]struct{}{}
|
2019-08-05 19:52:35 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
upstreamIDs := args.UpstreamIDs
|
|
|
|
legacyUpstreams := false
|
|
|
|
|
2021-03-20 04:03:17 +00:00
|
|
|
var (
|
|
|
|
noUpstreamArgs = len(upstreamIDs) == 0 && len(args.Upstreams) == 0
|
|
|
|
|
2021-04-12 15:35:14 +00:00
|
|
|
// Check the args and the resolved value. If it was exclusively set via a config entry, then args.Mode
|
|
|
|
// will never be transparent because the service config request does not use the resolved value.
|
2021-05-14 15:21:44 +00:00
|
|
|
tproxy = args.Mode == structs.ProxyModeTransparent || thisReply.Mode == structs.ProxyModeTransparent
|
2021-03-20 04:03:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The upstreams passed as arguments to this endpoint are the upstreams explicitly defined in a proxy registration.
|
2021-04-12 15:35:14 +00:00
|
|
|
// If no upstreams were passed, then we should only returned the resolved config if the proxy in transparent mode.
|
2021-03-20 04:03:17 +00:00
|
|
|
// Otherwise we would return a resolved upstream config to a proxy with no configured upstreams.
|
|
|
|
if noUpstreamArgs && !tproxy {
|
2021-05-14 15:21:44 +00:00
|
|
|
*reply = thisReply
|
2021-03-20 04:03:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-20 01:59:00 +00:00
|
|
|
// The request is considered legacy if the deprecated args.Upstream was used
|
|
|
|
if len(upstreamIDs) == 0 && len(args.Upstreams) > 0 {
|
2020-01-24 15:04:58 +00:00
|
|
|
legacyUpstreams = true
|
|
|
|
|
|
|
|
upstreamIDs = make([]structs.ServiceID, 0)
|
|
|
|
for _, upstream := range args.Upstreams {
|
2021-03-20 01:59:00 +00:00
|
|
|
// Before Consul namespaces were released, the Upstreams provided to the endpoint did not contain the namespace.
|
|
|
|
// Because of this we attach the enterprise meta of the request, which will just be the default namespace.
|
2021-03-11 04:05:11 +00:00
|
|
|
sid := structs.NewServiceID(upstream, &args.EnterpriseMeta)
|
|
|
|
upstreamIDs = append(upstreamIDs, sid)
|
2020-01-24 15:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 04:05:11 +00:00
|
|
|
// First store all upstreams that were provided in the request
|
|
|
|
for _, sid := range upstreamIDs {
|
|
|
|
if _, ok := seenUpstreams[sid]; !ok {
|
|
|
|
seenUpstreams[sid] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 19:21:44 +00:00
|
|
|
// Then store upstreams inferred from service-defaults and mapify the overrides.
|
2021-03-11 04:05:11 +00:00
|
|
|
var (
|
2021-04-15 19:21:44 +00:00
|
|
|
upstreamConfigs = make(map[structs.ServiceID]*structs.UpstreamConfig)
|
2021-03-11 04:05:11 +00:00
|
|
|
upstreamDefaults *structs.UpstreamConfig
|
2021-04-15 19:21:44 +00:00
|
|
|
// usConfigs stores the opaque config map for each upstream and is keyed on the upstream's ID.
|
|
|
|
usConfigs = make(map[structs.ServiceID]map[string]interface{})
|
2021-03-11 04:05:11 +00:00
|
|
|
)
|
2021-04-15 19:21:44 +00:00
|
|
|
if serviceConf != nil && serviceConf.UpstreamConfig != nil {
|
|
|
|
for i, override := range serviceConf.UpstreamConfig.Overrides {
|
|
|
|
if override.Name == "" {
|
|
|
|
c.logger.Warn(
|
|
|
|
"Skipping UpstreamConfig.Overrides entry without a required name field",
|
|
|
|
"entryIndex", i,
|
|
|
|
"kind", serviceConf.GetKind(),
|
|
|
|
"name", serviceConf.GetName(),
|
|
|
|
"namespace", serviceConf.GetEnterpriseMeta().NamespaceOrEmpty(),
|
|
|
|
)
|
|
|
|
continue // skip this impossible condition
|
|
|
|
}
|
|
|
|
seenUpstreams[override.ServiceID()] = struct{}{}
|
|
|
|
upstreamConfigs[override.ServiceID()] = override
|
|
|
|
}
|
|
|
|
if serviceConf.UpstreamConfig.Defaults != nil {
|
|
|
|
upstreamDefaults = serviceConf.UpstreamConfig.Defaults
|
2021-03-20 04:03:17 +00:00
|
|
|
|
|
|
|
// Store the upstream defaults under a wildcard key so that they can be applied to
|
|
|
|
// upstreams that are inferred from intentions and do not have explicit upstream configuration.
|
|
|
|
cfgMap := make(map[string]interface{})
|
|
|
|
upstreamDefaults.MergeInto(cfgMap)
|
|
|
|
|
|
|
|
wildcard := structs.NewServiceID(structs.WildcardSpecifier, structs.WildcardEnterpriseMeta())
|
|
|
|
usConfigs[wildcard] = cfgMap
|
2021-03-11 04:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 18:04:40 +00:00
|
|
|
for upstream := range seenUpstreams {
|
2021-03-11 04:05:11 +00:00
|
|
|
resolvedCfg := make(map[string]interface{})
|
|
|
|
|
|
|
|
// The protocol of an upstream is resolved in this order:
|
|
|
|
// 1. Default protocol from proxy-defaults (how all services should be addressed)
|
|
|
|
// 2. Protocol for upstream service defined in its service-defaults (how the upstream wants to be addressed)
|
2021-04-15 19:21:44 +00:00
|
|
|
// 3. Protocol defined for the upstream in the service-defaults.(upstream_config.defaults|upstream_config.overrides) of the downstream
|
2021-03-11 04:05:11 +00:00
|
|
|
// (how the downstream wants to address it)
|
|
|
|
protocol := proxyConfGlobalProtocol
|
|
|
|
|
|
|
|
_, upstreamSvcDefaults, err := state.ConfigEntry(ws, structs.ServiceDefaults, upstream.ID, &upstream.EnterpriseMeta)
|
2019-05-01 23:39:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-11 04:05:11 +00:00
|
|
|
if upstreamSvcDefaults != nil {
|
|
|
|
cfg, ok := upstreamSvcDefaults.(*structs.ServiceConfigEntry)
|
2019-05-01 23:39:31 +00:00
|
|
|
if !ok {
|
2021-03-11 04:05:11 +00:00
|
|
|
return fmt.Errorf("invalid service config type %T", upstreamSvcDefaults)
|
2019-05-01 23:39:31 +00:00
|
|
|
}
|
2021-03-11 04:05:11 +00:00
|
|
|
if cfg.Protocol != "" {
|
|
|
|
protocol = cfg.Protocol
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if protocol != "" {
|
|
|
|
resolvedCfg["protocol"] = protocol
|
2019-05-01 23:39:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 04:05:11 +00:00
|
|
|
// Merge centralized defaults for all upstreams before configuration for specific upstreams
|
|
|
|
if upstreamDefaults != nil {
|
2021-03-15 20:12:57 +00:00
|
|
|
upstreamDefaults.MergeInto(resolvedCfg)
|
2021-03-11 04:05:11 +00:00
|
|
|
}
|
2021-03-15 20:32:13 +00:00
|
|
|
|
|
|
|
// The MeshGateway value from the proxy registration overrides the one from upstream_defaults
|
|
|
|
// because it is specific to the proxy instance.
|
|
|
|
//
|
|
|
|
// The goal is to flatten the mesh gateway mode in this order:
|
|
|
|
// 0. Value from centralized upstream_defaults
|
|
|
|
// 1. Value from local proxy registration
|
2021-04-15 19:21:44 +00:00
|
|
|
// 2. Value from centralized upstream_config
|
2021-03-15 20:32:13 +00:00
|
|
|
// 3. Value from local upstream definition. This last step is done in the client's service manager.
|
|
|
|
if !args.MeshGateway.IsZero() {
|
|
|
|
resolvedCfg["mesh_gateway"] = args.MeshGateway
|
2019-08-05 19:52:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 19:21:44 +00:00
|
|
|
if upstreamConfigs[upstream] != nil {
|
|
|
|
upstreamConfigs[upstream].MergeInto(resolvedCfg)
|
2019-05-01 23:39:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 04:05:11 +00:00
|
|
|
if len(resolvedCfg) > 0 {
|
|
|
|
usConfigs[upstream] = resolvedCfg
|
2020-01-24 15:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't allocate the slices just to not fill them
|
|
|
|
if len(usConfigs) == 0 {
|
2021-05-14 15:21:44 +00:00
|
|
|
*reply = thisReply
|
2020-01-24 15:04:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if legacyUpstreams {
|
2021-03-11 04:05:11 +00:00
|
|
|
// For legacy upstreams we return a map that is only keyed on the string ID, since they precede namespaces
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.UpstreamConfigs = make(map[string]map[string]interface{})
|
2021-03-11 04:05:11 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
for us, conf := range usConfigs {
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.UpstreamConfigs[us.ID] = conf
|
2020-01-24 15:04:58 +00:00
|
|
|
}
|
2021-03-11 04:05:11 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
} else {
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.UpstreamIDConfigs = make(structs.OpaqueUpstreamConfigs, 0, len(usConfigs))
|
2020-01-24 15:04:58 +00:00
|
|
|
|
|
|
|
for us, conf := range usConfigs {
|
2021-05-14 15:21:44 +00:00
|
|
|
thisReply.UpstreamIDConfigs = append(thisReply.UpstreamIDConfigs,
|
2021-03-11 04:05:11 +00:00
|
|
|
structs.OpaqueUpstreamConfig{Upstream: us, Config: conf})
|
2019-05-01 23:39:31 +00:00
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
2021-05-14 15:21:44 +00:00
|
|
|
|
|
|
|
*reply = thisReply
|
2019-04-07 06:38:08 +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
|
|
|
|
}
|