2019-04-07 06:38:08 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
metrics "github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2019-07-02 03:10:51 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/discoverychain"
|
2019-04-07 06:38:08 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2019-05-06 16:09:59 +00:00
|
|
|
"github.com/mitchellh/copystructure"
|
2019-04-07 06:38:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The ConfigEntry endpoint is used to query centralized config information
|
|
|
|
type ConfigEntry struct {
|
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-04-07 06:38:08 +00:00
|
|
|
if done, err := c.srv.forward("ConfigEntry.Apply", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "apply"}, time.Now())
|
|
|
|
|
|
|
|
// Normalize and validate the incoming config entry.
|
|
|
|
if err := args.Entry.Normalize(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := args.Entry.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
rule, err := c.srv.ResolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-23 06:55:11 +00:00
|
|
|
if rule != nil && !args.Entry.CanWrite(rule) {
|
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
|
|
|
|
}
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
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 {
|
2019-04-07 06:38:08 +00:00
|
|
|
if done, err := c.srv.forward("ConfigEntry.Get", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "get"}, time.Now())
|
|
|
|
|
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
rule, err := c.srv.ResolveToken(args.Token)
|
|
|
|
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
|
|
|
|
}
|
2019-04-23 06:55:11 +00:00
|
|
|
if rule != nil && !lookupEntry.CanRead(rule) {
|
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 {
|
|
|
|
index, entry, err := state.ConfigEntry(ws, args.Kind, args.Name)
|
|
|
|
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 {
|
|
|
|
if done, err := c.srv.forward("ConfigEntry.List", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "list"}, time.Now())
|
|
|
|
|
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
rule, err := c.srv.ResolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-30 22:19:19 +00:00
|
|
|
if args.Kind != "" && !structs.ValidateConfigEntryKind(args.Kind) {
|
|
|
|
return fmt.Errorf("invalid config entry kind: %s", args.Kind)
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, entries, err := state.ConfigEntriesByKind(ws, args.Kind)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the entries returned by ACL permissions.
|
|
|
|
filteredEntries := make([]structs.ConfigEntry, 0, len(entries))
|
|
|
|
for _, entry := range entries {
|
2019-04-23 06:55:11 +00:00
|
|
|
if rule != nil && !entry.CanRead(rule) {
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:38:39 +00:00
|
|
|
// ListAll returns all the known configuration entries
|
|
|
|
func (c *ConfigEntry) ListAll(args *structs.DCSpecificRequest, reply *structs.IndexedGenericConfigEntries) error {
|
|
|
|
if done, err := c.srv.forward("ConfigEntry.ListAll", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "listAll"}, time.Now())
|
|
|
|
|
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
rule, err := c.srv.ResolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, entries, err := state.ConfigEntries(ws)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the entries returned by ACL permissions.
|
|
|
|
filteredEntries := make([]structs.ConfigEntry, 0, len(entries))
|
|
|
|
for _, entry := range entries {
|
|
|
|
if rule != nil && !entry.CanRead(rule) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
if done, err := c.srv.forward("ConfigEntry.Delete", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "delete"}, time.Now())
|
|
|
|
|
|
|
|
// Normalize the incoming entry.
|
|
|
|
if err := args.Entry.Normalize(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
rule, err := c.srv.ResolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-23 06:55:11 +00:00
|
|
|
if rule != nil && !args.Entry.CanWrite(rule) {
|
2019-04-10 21:27:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args.Op = structs.ConfigEntryDelete
|
|
|
|
resp, err := c.srv.raftApply(structs.ConfigEntryRequestType, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveServiceConfig
|
|
|
|
func (c *ConfigEntry) ResolveServiceConfig(args *structs.ServiceConfigRequest, reply *structs.ServiceConfigResponse) error {
|
|
|
|
if done, err := c.srv.forward("ConfigEntry.ResolveServiceConfig", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "resolve_service_config"}, time.Now())
|
|
|
|
|
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
rule, err := c.srv.ResolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rule != nil && !rule.ServiceRead(args.Name) {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2019-06-18 00:52:01 +00:00
|
|
|
reply.MeshGateway.Mode = structs.MeshGatewayModeDefault
|
2019-04-07 06:38:08 +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.
|
|
|
|
index, serviceEntry, err := state.ConfigEntry(ws, structs.ServiceDefaults, args.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-23 06:39:02 +00:00
|
|
|
var serviceConf *structs.ServiceConfigEntry
|
|
|
|
var ok bool
|
|
|
|
if serviceEntry != nil {
|
|
|
|
serviceConf, ok = serviceEntry.(*structs.ServiceConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid service config type %T", serviceEntry)
|
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, proxyEntry, err := state.ConfigEntry(ws, structs.ProxyDefaults, structs.ProxyConfigGlobal)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-23 06:39:02 +00:00
|
|
|
var proxyConf *structs.ProxyConfigEntry
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
reply.ProxyConfig = mapCopy.(map[string]interface{})
|
2019-06-18 00:52:01 +00:00
|
|
|
reply.MeshGateway = proxyConf.MeshGateway
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 23:39:31 +00:00
|
|
|
reply.Index = index
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
if serviceConf != nil {
|
|
|
|
if serviceConf.MeshGateway.Mode != structs.MeshGatewayModeDefault {
|
|
|
|
reply.MeshGateway.Mode = serviceConf.MeshGateway.Mode
|
|
|
|
}
|
|
|
|
if serviceConf.Protocol != "" {
|
|
|
|
if reply.ProxyConfig == nil {
|
|
|
|
reply.ProxyConfig = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
reply.ProxyConfig["protocol"] = serviceConf.Protocol
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-01 23:39:31 +00:00
|
|
|
|
|
|
|
// Apply the upstream protocols to the upstream configs
|
|
|
|
for _, upstream := range args.Upstreams {
|
|
|
|
_, upstreamEntry, err := state.ConfigEntry(ws, structs.ServiceDefaults, upstream)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var upstreamConf *structs.ServiceConfigEntry
|
|
|
|
var ok bool
|
|
|
|
if upstreamEntry != nil {
|
|
|
|
upstreamConf, ok = upstreamEntry.(*structs.ServiceConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid service config type %T", upstreamEntry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to configure if a protocol hasn't been set.
|
|
|
|
if upstreamConf == nil || upstreamConf.Protocol == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if reply.UpstreamConfigs == nil {
|
|
|
|
reply.UpstreamConfigs = make(map[string]map[string]interface{})
|
|
|
|
}
|
|
|
|
reply.UpstreamConfigs[upstream] = map[string]interface{}{
|
|
|
|
"protocol": upstreamConf.Protocol,
|
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
|
|
|
|
func (c *ConfigEntry) ReadDiscoveryChain(args *structs.DiscoveryChainRequest, reply *structs.DiscoveryChainResponse) error {
|
|
|
|
if done, err := c.srv.forward("ConfigEntry.ReadDiscoveryChain", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"config_entry", "read_discovery_chain"}, time.Now())
|
|
|
|
|
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
rule, err := c.srv.ResolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rule != nil && !rule.ServiceRead(args.Name) {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Name == "" {
|
|
|
|
return fmt.Errorf("Must provide service name")
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentNamespace = "default"
|
|
|
|
|
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, entries, err := state.ReadDiscoveryChainConfigEntries(ws, args.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we compile it into something useful.
|
|
|
|
chain, err := discoverychain.Compile(discoverychain.CompileRequest{
|
|
|
|
ServiceName: args.Name,
|
|
|
|
CurrentNamespace: currentNamespace,
|
|
|
|
CurrentDatacenter: c.srv.config.Datacenter,
|
|
|
|
InferDefaults: true,
|
|
|
|
Entries: entries,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index = index
|
|
|
|
reply.ConfigEntries = entries
|
|
|
|
reply.Chain = chain
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|