2013-12-11 22:04:44 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2013-12-24 20:43:34 +00:00
|
|
|
"fmt"
|
2018-10-19 16:04:07 +00:00
|
|
|
"sort"
|
2014-02-20 23:16:26 +00:00
|
|
|
"time"
|
2014-12-01 04:05:15 +00:00
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
2020-11-13 02:12:12 +00:00
|
|
|
"github.com/armon/go-metrics/prometheus"
|
2021-04-08 22:58:15 +00:00
|
|
|
bexpr "github.com/hashicorp/go-bexpr"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
|
2017-08-23 14:52:48 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-05-15 20:10:36 +00:00
|
|
|
"github.com/hashicorp/consul/ipaddr"
|
2016-06-06 20:19:31 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2013-12-11 22:04:44 +00:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:12:12 +00:00
|
|
|
var CatalogCounters = []prometheus.CounterDefinition{
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"catalog", "service", "query"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Increments for each catalog query for the given service.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"catalog", "connect", "query"},
|
2020-11-23 21:06:30 +00:00
|
|
|
Help: "Increments for each connect-based catalog query for the given service.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"catalog", "service", "query-tag"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Increments for each catalog query for the given service with the given tag.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"catalog", "connect", "query-tag"},
|
2020-11-23 21:06:30 +00:00
|
|
|
Help: "Increments for each connect-based catalog query for the given service with the given tag.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"catalog", "service", "query-tags"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Increments for each catalog query for the given service with the given tags.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"catalog", "connect", "query-tags"},
|
2020-11-23 21:06:30 +00:00
|
|
|
Help: "Increments for each connect-based catalog query for the given service with the given tags.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"catalog", "service", "not-found"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Increments for each catalog query where the given service could not be found.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"catalog", "connect", "not-found"},
|
2020-11-23 21:06:30 +00:00
|
|
|
Help: "Increments for each connect-based catalog query where the given service could not be found.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var CatalogSummaries = []prometheus.SummaryDefinition{
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"catalog", "deregister"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Measures the time it takes to complete a catalog deregister operation.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"catalog", "register"},
|
2020-11-16 19:02:11 +00:00
|
|
|
Help: "Measures the time it takes to complete a catalog register operation.",
|
2020-11-13 02:12:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// Catalog endpoint is used to manipulate the service catalog
|
|
|
|
type Catalog struct {
|
2020-06-12 02:05:07 +00:00
|
|
|
srv *Server
|
|
|
|
logger hclog.Logger
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 10:29:54 +00:00
|
|
|
// nodePreApply does the verification of a node before it is applied to Raft.
|
|
|
|
func nodePreApply(nodeName, nodeID string) error {
|
|
|
|
if nodeName == "" {
|
|
|
|
return fmt.Errorf("Must provide node")
|
|
|
|
}
|
|
|
|
if nodeID != "" {
|
|
|
|
if _, err := uuid.ParseUUID(nodeID); err != nil {
|
|
|
|
return fmt.Errorf("Bad node ID: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-04 22:18:51 +00:00
|
|
|
func servicePreApply(service *structs.NodeService, authz acl.Authorizer, authzCtxFill func(*acl.AuthorizerContext)) error {
|
2018-12-12 10:29:54 +00:00
|
|
|
// Validate the service. This is in addition to the below since
|
|
|
|
// the above just hasn't been moved over yet. We should move it over
|
|
|
|
// in time.
|
|
|
|
if err := service.Validate(); err != nil {
|
2013-12-11 22:04:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-12 10:29:54 +00:00
|
|
|
// If no service id, but service name, use default
|
|
|
|
if service.ID == "" && service.Service != "" {
|
|
|
|
service.ID = service.Service
|
2017-11-01 21:25:46 +00:00
|
|
|
}
|
2018-12-12 10:29:54 +00:00
|
|
|
|
|
|
|
// Verify ServiceName provided if ID.
|
|
|
|
if service.ID != "" && service.Service == "" {
|
|
|
|
return fmt.Errorf("Must provide service name with ID")
|
2013-12-24 20:43:34 +00:00
|
|
|
}
|
2018-12-12 10:29:54 +00:00
|
|
|
|
|
|
|
// Check the service address here and in the agent endpoint
|
|
|
|
// since service registration isn't synchronous.
|
|
|
|
if ipaddr.IsAny(service.Address) {
|
|
|
|
return fmt.Errorf("Invalid service address")
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2021-08-04 22:18:51 +00:00
|
|
|
authzCtxFill(&authzContext)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2018-12-12 10:29:54 +00:00
|
|
|
// Apply the ACL policy if any. The 'consul' service is excluded
|
|
|
|
// since it is managed automatically internally (that behavior
|
|
|
|
// is going away after version 0.8). We check this same policy
|
|
|
|
// later if version 0.8 is enabled, so we can eventually just
|
|
|
|
// delete this and do all the ACL checks down there.
|
|
|
|
if service.Service != structs.ConsulServiceName {
|
2021-07-30 18:28:19 +00:00
|
|
|
if authz.ServiceWrite(service.Service, &authzContext) != acl.Allow {
|
2018-12-12 10:29:54 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2017-01-18 22:26:42 +00:00
|
|
|
}
|
2018-06-27 13:07:22 +00:00
|
|
|
}
|
2013-12-24 20:43:34 +00:00
|
|
|
|
2018-12-12 10:29:54 +00:00
|
|
|
// Proxies must have write permission on their destination
|
|
|
|
if service.Kind == structs.ServiceKindConnectProxy {
|
2021-07-30 18:28:19 +00:00
|
|
|
if authz.ServiceWrite(service.Proxy.DestinationServiceName, &authzContext) != acl.Allow {
|
2018-12-12 10:29:54 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-03 07:11:48 +00:00
|
|
|
// checkPreApply does the verification of a check before it is applied to Raft.
|
|
|
|
func checkPreApply(check *structs.HealthCheck) {
|
|
|
|
if check.CheckID == "" && check.Name != "" {
|
|
|
|
check.CheckID = types.CheckID(check.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// Register is used register that a node is providing a given service.
|
2013-12-19 20:03:57 +00:00
|
|
|
func (c *Catalog) Register(args *structs.RegisterRequest, reply *struct{}) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Catalog.Register", args, reply); done {
|
2013-12-11 22:04:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSince([]string{"catalog", "register"}, time.Now())
|
2013-12-11 22:04:44 +00:00
|
|
|
|
2016-12-08 01:58:23 +00:00
|
|
|
// Fetch the ACL token, if any.
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.srv.validateEnterpriseRequest(args.GetEnterpriseMeta(), true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This needs to happen before the other preapply checks as it will fixup some of the
|
|
|
|
// internal enterprise metas on the services and checks
|
|
|
|
state := c.srv.fsm.State()
|
|
|
|
entMeta, err := state.ValidateRegisterRequest(args)
|
2016-12-08 01:58:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-12 10:29:54 +00:00
|
|
|
// Verify the args.
|
|
|
|
if err := nodePreApply(args.Node, string(args.ID)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if args.Address == "" && !args.SkipNodeUpdate {
|
|
|
|
return fmt.Errorf("Must provide address if SkipNodeUpdate is not set")
|
|
|
|
}
|
|
|
|
|
2016-12-09 00:01:01 +00:00
|
|
|
// Handle a service registration.
|
2014-01-08 21:39:40 +00:00
|
|
|
if args.Service != nil {
|
2021-08-04 22:18:51 +00:00
|
|
|
if err := servicePreApply(args.Service, authz, args.Service.FillAuthzContext); err != nil {
|
2018-03-09 06:13:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-01-06 22:18:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 00:01:01 +00:00
|
|
|
// Move the old format single check into the slice, and fixup IDs.
|
2014-01-08 21:39:40 +00:00
|
|
|
if args.Check != nil {
|
2015-01-14 01:52:17 +00:00
|
|
|
args.Checks = append(args.Checks, args.Check)
|
|
|
|
args.Check = nil
|
|
|
|
}
|
|
|
|
for _, check := range args.Checks {
|
|
|
|
if check.Node == "" {
|
|
|
|
check.Node = args.Node
|
2014-01-08 21:39:40 +00:00
|
|
|
}
|
2018-12-03 07:11:48 +00:00
|
|
|
checkPreApply(check)
|
2019-10-17 18:33:11 +00:00
|
|
|
|
|
|
|
// Populate check type for cases when a check is registered in the catalog directly
|
|
|
|
// and not via anti-entropy
|
|
|
|
if check.Type == "" {
|
|
|
|
chkType := check.CheckType()
|
|
|
|
check.Type = chkType.Type()
|
|
|
|
}
|
2014-01-06 22:18:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 00:01:01 +00:00
|
|
|
// Check the complete register request against the given ACL policy.
|
2021-07-30 18:55:35 +00:00
|
|
|
_, ns, err := state.NodeServices(nil, args.Node, entMeta)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Node lookup failed: %v", err)
|
|
|
|
}
|
|
|
|
if err := vetRegisterWithACL(authz, args, ns); err != nil {
|
|
|
|
return err
|
2016-12-09 00:01:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = c.srv.raftApply(structs.RegisterRequestType, args)
|
|
|
|
return err
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 16:42:22 +00:00
|
|
|
// vetRegisterWithACL applies the given ACL's policy to the catalog update and
|
|
|
|
// determines if it is allowed. Since the catalog register request is so
|
|
|
|
// dynamic, this is a pretty complex algorithm and was worth breaking out of the
|
|
|
|
// endpoint. The NodeServices record for the node must be supplied, and can be
|
|
|
|
// nil.
|
|
|
|
//
|
|
|
|
// This is a bit racy because we have to check the state store outside of a
|
|
|
|
// transaction. It's the best we can do because we don't want to flow ACL
|
|
|
|
// checking down there. The node information doesn't change in practice, so this
|
|
|
|
// will be fine. If we expose ways to change node addresses in a later version,
|
|
|
|
// then we should split the catalog API at the node and service level so we can
|
|
|
|
// address this race better (even then it would be super rare, and would at
|
|
|
|
// worst let a service update revert a recent node update, so it doesn't open up
|
|
|
|
// too much abuse).
|
|
|
|
func vetRegisterWithACL(
|
2021-08-04 22:36:48 +00:00
|
|
|
authz acl.Authorizer,
|
2021-07-30 16:42:22 +00:00
|
|
|
subj *structs.RegisterRequest,
|
|
|
|
ns *structs.NodeServices,
|
|
|
|
) error {
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
subj.FillAuthzContext(&authzContext)
|
|
|
|
|
|
|
|
// Vet the node info. This allows service updates to re-post the required
|
|
|
|
// node info for each request without having to have node "write"
|
|
|
|
// privileges.
|
|
|
|
needsNode := ns == nil || subj.ChangesNode(ns.Node)
|
|
|
|
|
2021-08-04 22:36:48 +00:00
|
|
|
if needsNode && authz.NodeWrite(subj.Node, &authzContext) != acl.Allow {
|
2021-07-30 16:42:22 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vet the service change. This includes making sure they can register
|
|
|
|
// the given service, and that we can write to any existing service that
|
|
|
|
// is being modified by id (if any).
|
|
|
|
if subj.Service != nil {
|
2021-08-04 22:36:48 +00:00
|
|
|
if authz.ServiceWrite(subj.Service.Service, &authzContext) != acl.Allow {
|
2021-07-30 16:42:22 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
if ns != nil {
|
|
|
|
other, ok := ns.Services[subj.Service.ID]
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
// This is effectively a delete, so we DO NOT apply the
|
|
|
|
// sentinel scope to the service we are overwriting, just
|
|
|
|
// the regular ACL policy.
|
|
|
|
var secondaryCtx acl.AuthorizerContext
|
|
|
|
other.FillAuthzContext(&secondaryCtx)
|
|
|
|
|
2021-08-04 22:36:48 +00:00
|
|
|
if authz.ServiceWrite(other.Service, &secondaryCtx) != acl.Allow {
|
2021-07-30 16:42:22 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the member was flattened before we got there. This
|
|
|
|
// keeps us from having to verify this check as well.
|
|
|
|
if subj.Check != nil {
|
|
|
|
return fmt.Errorf("check member must be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vet the checks. Node-level checks require node write, and
|
|
|
|
// service-level checks require service write.
|
|
|
|
for _, check := range subj.Checks {
|
|
|
|
// Make sure that the node matches - we don't allow you to mix
|
|
|
|
// checks from other nodes because we'd have to pull a bunch
|
|
|
|
// more state store data to check this. If ACLs are enabled then
|
|
|
|
// we simply require them to match in a given request. There's a
|
|
|
|
// note in state_store.go to ban this down there in Consul 0.8,
|
|
|
|
// but it's good to leave this here because it's required for
|
|
|
|
// correctness wrt. ACLs.
|
|
|
|
if check.Node != subj.Node {
|
|
|
|
return fmt.Errorf("Node '%s' for check '%s' doesn't match register request node '%s'",
|
|
|
|
check.Node, check.CheckID, subj.Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node-level check.
|
|
|
|
if check.ServiceID == "" {
|
2021-08-04 22:36:48 +00:00
|
|
|
if authz.NodeWrite(subj.Node, &authzContext) != acl.Allow {
|
2021-07-30 16:42:22 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service-level check, check the common case where it
|
|
|
|
// matches the service part of this request, which has
|
|
|
|
// already been vetted above, and might be being registered
|
|
|
|
// along with its checks.
|
|
|
|
if subj.Service != nil && subj.Service.ID == check.ServiceID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service-level check for some other service. Make sure they've
|
|
|
|
// got write permissions for that service.
|
|
|
|
if ns == nil {
|
|
|
|
return fmt.Errorf("Unknown service '%s' for check '%s'", check.ServiceID, check.CheckID)
|
|
|
|
}
|
|
|
|
|
|
|
|
other, ok := ns.Services[check.ServiceID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Unknown service '%s' for check '%s'", check.ServiceID, check.CheckID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are only adding a check here, so we don't add the scope,
|
|
|
|
// since the sentinel policy doesn't apply to adding checks at
|
|
|
|
// this time.
|
|
|
|
var secondaryCtx acl.AuthorizerContext
|
|
|
|
other.FillAuthzContext(&secondaryCtx)
|
|
|
|
|
2021-08-04 22:36:48 +00:00
|
|
|
if authz.ServiceWrite(other.Service, &secondaryCtx) != acl.Allow {
|
2021-07-30 16:42:22 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// Deregister is used to remove a service registration for a given node.
|
2013-12-19 20:03:57 +00:00
|
|
|
func (c *Catalog) Deregister(args *structs.DeregisterRequest, reply *struct{}) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Catalog.Deregister", args, reply); done {
|
2013-12-11 23:34:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-10-04 23:43:27 +00:00
|
|
|
defer metrics.MeasureSince([]string{"catalog", "deregister"}, time.Now())
|
2013-12-11 23:34:10 +00:00
|
|
|
|
2013-12-24 20:43:34 +00:00
|
|
|
// Verify the args
|
|
|
|
if args.Node == "" {
|
|
|
|
return fmt.Errorf("Must provide node")
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:15:44 +00:00
|
|
|
// Fetch the ACL token, if any.
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
2016-12-10 03:15:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:15:44 +00:00
|
|
|
// Check the complete deregister request against the given ACL policy.
|
2021-07-30 18:55:35 +00:00
|
|
|
state := c.srv.fsm.State()
|
2016-12-10 03:15:44 +00:00
|
|
|
|
2021-07-30 18:55:35 +00:00
|
|
|
var ns *structs.NodeService
|
|
|
|
if args.ServiceID != "" {
|
|
|
|
_, ns, err = state.NodeService(args.Node, args.ServiceID, &args.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Service lookup failed: %v", err)
|
2016-12-10 03:15:44 +00:00
|
|
|
}
|
2021-07-30 18:55:35 +00:00
|
|
|
}
|
2016-12-10 03:15:44 +00:00
|
|
|
|
2021-07-30 18:55:35 +00:00
|
|
|
var nc *structs.HealthCheck
|
|
|
|
if args.CheckID != "" {
|
|
|
|
_, nc, err = state.NodeCheck(args.Node, args.CheckID, &args.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Check lookup failed: %v", err)
|
2016-12-10 03:15:44 +00:00
|
|
|
}
|
2021-07-30 18:55:35 +00:00
|
|
|
}
|
2017-09-14 19:31:01 +00:00
|
|
|
|
2021-07-30 18:55:35 +00:00
|
|
|
if err := vetDeregisterWithACL(authz, args, ns, nc); err != nil {
|
|
|
|
return err
|
2016-12-10 03:15:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = c.srv.raftApply(structs.DeregisterRequestType, args)
|
|
|
|
return err
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
2013-12-12 18:35:50 +00:00
|
|
|
|
2021-07-30 16:42:22 +00:00
|
|
|
// vetDeregisterWithACL applies the given ACL's policy to the catalog update and
|
|
|
|
// determines if it is allowed. Since the catalog deregister request is so
|
|
|
|
// dynamic, this is a pretty complex algorithm and was worth breaking out of the
|
|
|
|
// endpoint. The NodeService for the referenced service must be supplied, and can
|
|
|
|
// be nil; similar for the HealthCheck for the referenced health check.
|
|
|
|
func vetDeregisterWithACL(
|
2021-08-04 22:36:48 +00:00
|
|
|
authz acl.Authorizer,
|
2021-07-30 16:42:22 +00:00
|
|
|
subj *structs.DeregisterRequest,
|
|
|
|
ns *structs.NodeService,
|
|
|
|
nc *structs.HealthCheck,
|
|
|
|
) error {
|
|
|
|
// We don't apply sentinel in this path, since at this time sentinel
|
|
|
|
// only applies to create and update operations.
|
|
|
|
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
// fill with the defaults for use with the NodeWrite check
|
|
|
|
subj.FillAuthzContext(&authzContext)
|
|
|
|
|
|
|
|
// Allow service deregistration if the token has write permission for the node.
|
|
|
|
// This accounts for cases where the agent no longer has a token with write permission
|
|
|
|
// on the service to deregister it.
|
2021-08-04 22:36:48 +00:00
|
|
|
if authz.NodeWrite(subj.Node, &authzContext) == acl.Allow {
|
2021-07-30 16:42:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This order must match the code in applyDeregister() in
|
|
|
|
// fsm/commands_oss.go since it also evaluates things in this order,
|
|
|
|
// and will ignore fields based on this precedence. This lets us also
|
|
|
|
// ignore them from an ACL perspective.
|
|
|
|
if subj.ServiceID != "" {
|
|
|
|
if ns == nil {
|
|
|
|
return fmt.Errorf("Unknown service '%s'", subj.ServiceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
ns.FillAuthzContext(&authzContext)
|
|
|
|
|
2021-08-04 22:36:48 +00:00
|
|
|
if authz.ServiceWrite(ns.Service, &authzContext) != acl.Allow {
|
2021-07-30 16:42:22 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
} else if subj.CheckID != "" {
|
|
|
|
if nc == nil {
|
|
|
|
return fmt.Errorf("Unknown check '%s'", subj.CheckID)
|
|
|
|
}
|
|
|
|
|
|
|
|
nc.FillAuthzContext(&authzContext)
|
|
|
|
|
|
|
|
if nc.ServiceID != "" {
|
2021-08-04 22:36:48 +00:00
|
|
|
if authz.ServiceWrite(nc.ServiceName, &authzContext) != acl.Allow {
|
2021-07-30 16:42:22 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-04 22:36:48 +00:00
|
|
|
if authz.NodeWrite(subj.Node, &authzContext) != acl.Allow {
|
2021-07-30 16:42:22 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Since NodeWrite is not given - otherwise the earlier check
|
|
|
|
// would've returned already - we can deny here.
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-12 18:35:50 +00:00
|
|
|
// ListDatacenters is used to query for the list of known datacenters
|
2019-06-24 18:11:34 +00:00
|
|
|
func (c *Catalog) ListDatacenters(args *structs.DatacentersRequest, reply *[]string) error {
|
2017-03-14 05:56:24 +00:00
|
|
|
dcs, err := c.srv.router.GetDatacentersByDistance()
|
2015-11-07 06:14:45 +00:00
|
|
|
if err != nil {
|
2015-07-24 19:53:50 +00:00
|
|
|
return err
|
2015-07-02 22:36:59 +00:00
|
|
|
}
|
2014-06-06 21:12:40 +00:00
|
|
|
|
2018-03-26 19:21:06 +00:00
|
|
|
if len(dcs) == 0 { // no WAN federation, so return the local data center name
|
|
|
|
dcs = []string{c.srv.config.Datacenter}
|
|
|
|
}
|
|
|
|
|
2013-12-12 18:35:50 +00:00
|
|
|
*reply = dcs
|
|
|
|
return nil
|
|
|
|
}
|
2013-12-12 18:48:36 +00:00
|
|
|
|
|
|
|
// ListNodes is used to query the nodes in a DC
|
2014-02-05 19:00:43 +00:00
|
|
|
func (c *Catalog) ListNodes(args *structs.DCSpecificRequest, reply *structs.IndexedNodes) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Catalog.ListNodes", args, reply); done {
|
2013-12-12 18:48:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.Nodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-20 07:36:50 +00:00
|
|
|
return c.srv.blockingQuery(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2017-01-06 01:21:56 +00:00
|
|
|
var err error
|
2017-01-11 19:41:12 +00:00
|
|
|
if len(args.NodeMetaFilters) > 0 {
|
2021-08-19 20:09:42 +00:00
|
|
|
reply.Index, reply.Nodes, err = state.NodesByMeta(ws, args.NodeMetaFilters, &args.EnterpriseMeta)
|
2017-01-06 01:21:56 +00:00
|
|
|
} else {
|
2021-08-19 20:09:42 +00:00
|
|
|
reply.Index, reply.Nodes, err = state.Nodes(ws, &args.EnterpriseMeta)
|
2017-01-06 01:21:56 +00:00
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-30 18:11:43 +00:00
|
|
|
if isUnmodified(args.QueryOptions, reply.Index) {
|
|
|
|
reply.QueryMeta.NotModified = true
|
|
|
|
reply.Nodes = nil
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
if err := c.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-16 16:00:15 +00:00
|
|
|
|
|
|
|
raw, err := filter.Execute(reply.Nodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Nodes = raw.(structs.Nodes)
|
|
|
|
|
2015-07-02 22:36:59 +00:00
|
|
|
return c.srv.sortNodesByDistanceFrom(args.Source, reply.Nodes)
|
2014-02-05 19:00:43 +00:00
|
|
|
})
|
2013-12-12 18:48:36 +00:00
|
|
|
}
|
2013-12-12 19:07:14 +00:00
|
|
|
|
2020-06-30 18:11:43 +00:00
|
|
|
func isUnmodified(opts structs.QueryOptions, index uint64) bool {
|
|
|
|
return opts.AllowNotModifiedResponse && opts.MinQueryIndex > 0 && opts.MinQueryIndex == index
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:07:14 +00:00
|
|
|
// ListServices is used to query the services in a DC
|
2014-02-05 19:00:43 +00:00
|
|
|
func (c *Catalog) ListServices(args *structs.DCSpecificRequest, reply *structs.IndexedServices) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Catalog.ListServices", args, reply); done {
|
2019-12-10 02:26:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
|
|
|
if err != nil {
|
2013-12-12 19:07:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2020-07-13 18:39:57 +00:00
|
|
|
// Set reply enterprise metadata after resolving and validating the token so
|
|
|
|
// that we can properly infer metadata from the token.
|
|
|
|
reply.EnterpriseMeta = args.EnterpriseMeta
|
|
|
|
|
2017-01-20 07:36:50 +00:00
|
|
|
return c.srv.blockingQuery(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2017-01-09 19:21:49 +00:00
|
|
|
var err error
|
2017-01-11 19:41:12 +00:00
|
|
|
if len(args.NodeMetaFilters) > 0 {
|
2020-06-30 18:11:43 +00:00
|
|
|
reply.Index, reply.Services, err = state.ServicesByNodeMeta(ws, args.NodeMetaFilters, &args.EnterpriseMeta)
|
2017-01-09 19:21:49 +00:00
|
|
|
} else {
|
2020-06-30 18:11:43 +00:00
|
|
|
reply.Index, reply.Services, err = state.Services(ws, &args.EnterpriseMeta)
|
2017-01-09 19:21:49 +00:00
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-30 18:11:43 +00:00
|
|
|
if isUnmodified(args.QueryOptions, reply.Index) {
|
|
|
|
reply.Services = nil
|
|
|
|
reply.QueryMeta.NotModified = true
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
|
2021-07-30 21:08:58 +00:00
|
|
|
c.srv.filterACLWithAuthorizer(authz, reply)
|
|
|
|
return nil
|
2014-02-05 19:00:43 +00:00
|
|
|
})
|
2013-12-12 19:07:14 +00:00
|
|
|
}
|
2013-12-12 19:37:19 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func (c *Catalog) ServiceList(args *structs.DCSpecificRequest, reply *structs.IndexedServiceList) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Catalog.ServiceList", args, reply); done {
|
2020-01-24 15:04:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2021-03-14 04:44:24 +00:00
|
|
|
index, services, err := state.ServiceList(ws, nil, &args.EnterpriseMeta)
|
2020-01-24 15:04:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Services = index, services
|
2021-07-30 21:08:58 +00:00
|
|
|
c.srv.filterACLWithAuthorizer(authz, reply)
|
|
|
|
return nil
|
2020-01-24 15:04:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:37:19 +00:00
|
|
|
// ServiceNodes returns all the nodes registered as part of a service
|
2014-02-05 19:10:10 +00:00
|
|
|
func (c *Catalog) ServiceNodes(args *structs.ServiceSpecificRequest, reply *structs.IndexedServiceNodes) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Catalog.ServiceNodes", args, reply); done {
|
2013-12-12 19:37:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:43:34 +00:00
|
|
|
// Verify the arguments
|
2018-05-03 20:54:14 +00:00
|
|
|
if args.ServiceName == "" && args.ServiceAddress == "" {
|
2013-12-24 20:43:34 +00:00
|
|
|
return fmt.Errorf("Must provide service name")
|
|
|
|
}
|
|
|
|
|
2018-03-09 16:34:55 +00:00
|
|
|
// Determine the function we'll call
|
|
|
|
var f func(memdb.WatchSet, *state.Store) (uint64, structs.ServiceNodes, error)
|
|
|
|
switch {
|
|
|
|
case args.Connect:
|
|
|
|
f = func(ws memdb.WatchSet, s *state.Store) (uint64, structs.ServiceNodes, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return s.ConnectServiceNodes(ws, args.ServiceName, &args.EnterpriseMeta)
|
2018-03-09 16:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
f = func(ws memdb.WatchSet, s *state.Store) (uint64, structs.ServiceNodes, error) {
|
|
|
|
if args.ServiceAddress != "" {
|
2019-12-10 02:26:41 +00:00
|
|
|
return s.ServiceAddressNodes(ws, args.ServiceAddress, &args.EnterpriseMeta)
|
2018-03-09 16:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if args.TagFilter {
|
2018-11-13 14:44:36 +00:00
|
|
|
tags := args.ServiceTags
|
2019-01-07 21:30:47 +00:00
|
|
|
// DEPRECATED (singular-service-tag) - remove this when backwards RPC compat
|
|
|
|
// with 1.2.x is not required.
|
|
|
|
// Agents < v1.3.0 populate the ServiceTag field. In this case,
|
2018-11-13 14:44:36 +00:00
|
|
|
// use ServiceTag instead of the ServiceTags field.
|
|
|
|
if args.ServiceTag != "" {
|
|
|
|
tags = []string{args.ServiceTag}
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
return s.ServiceTagNodes(ws, args.ServiceName, tags, &args.EnterpriseMeta)
|
2018-03-09 16:34:55 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
return s.ServiceNodes(ws, args.ServiceName, &args.EnterpriseMeta)
|
2018-03-09 16:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-11 16:31:39 +00:00
|
|
|
// If we're doing a connect query, we need read access to the service
|
|
|
|
// we're trying to find proxies for, so check that.
|
|
|
|
if args.Connect {
|
2021-07-30 18:28:19 +00:00
|
|
|
if authz.ServiceRead(args.ServiceName, &authzContext) != acl.Allow {
|
2018-03-11 16:31:39 +00:00
|
|
|
// Just return nil, which will return an empty response (tested)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.ServiceNodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.srv.blockingQuery(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2018-03-09 16:34:55 +00:00
|
|
|
index, services, err := f(ws, state)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-02-05 19:10:10 +00:00
|
|
|
}
|
2018-03-09 16:34:55 +00:00
|
|
|
|
2015-10-12 07:42:09 +00:00
|
|
|
reply.Index, reply.ServiceNodes = index, services
|
2017-01-14 01:08:43 +00:00
|
|
|
if len(args.NodeMetaFilters) > 0 {
|
|
|
|
var filtered structs.ServiceNodes
|
|
|
|
for _, service := range services {
|
|
|
|
if structs.SatisfiesMetaFilters(service.NodeMeta, args.NodeMetaFilters) {
|
|
|
|
filtered = append(filtered, service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reply.ServiceNodes = filtered
|
|
|
|
}
|
2019-04-16 16:00:15 +00:00
|
|
|
|
2015-06-30 21:25:40 +00:00
|
|
|
if err := c.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-16 16:00:15 +00:00
|
|
|
|
|
|
|
// This is safe to do even when the filter is nil - its just a no-op then
|
|
|
|
raw, err := filter.Execute(reply.ServiceNodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.ServiceNodes = raw.(structs.ServiceNodes)
|
|
|
|
|
2015-07-02 22:36:59 +00:00
|
|
|
return c.srv.sortNodesByDistanceFrom(args.Source, reply.ServiceNodes)
|
2014-02-05 19:10:10 +00:00
|
|
|
})
|
2014-04-04 21:55:44 +00:00
|
|
|
|
|
|
|
// Provide some metrics
|
|
|
|
if err == nil {
|
2018-03-09 16:34:55 +00:00
|
|
|
// For metrics, we separate Connect-based lookups from non-Connect
|
|
|
|
key := "service"
|
|
|
|
if args.Connect {
|
|
|
|
key = "connect"
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.IncrCounterWithLabels([]string{"catalog", key, "query"}, 1,
|
2017-10-04 23:43:27 +00:00
|
|
|
[]metrics.Label{{Name: "service", Value: args.ServiceName}})
|
2019-01-07 21:30:47 +00:00
|
|
|
// DEPRECATED (singular-service-tag) - remove this when backwards RPC compat
|
|
|
|
// with 1.2.x is not required.
|
2014-04-04 21:55:44 +00:00
|
|
|
if args.ServiceTag != "" {
|
2018-03-09 16:34:55 +00:00
|
|
|
metrics.IncrCounterWithLabels([]string{"catalog", key, "query-tag"}, 1,
|
2017-10-04 23:43:27 +00:00
|
|
|
[]metrics.Label{{Name: "service", Value: args.ServiceName}, {Name: "tag", Value: args.ServiceTag}})
|
2014-04-04 21:55:44 +00:00
|
|
|
}
|
2018-10-11 11:50:05 +00:00
|
|
|
if len(args.ServiceTags) > 0 {
|
2018-10-19 16:04:07 +00:00
|
|
|
// Sort tags so that the metric is the same even if the request
|
|
|
|
// tags are in a different order
|
|
|
|
sort.Strings(args.ServiceTags)
|
|
|
|
|
2018-10-11 11:50:05 +00:00
|
|
|
// Build metric labels
|
|
|
|
labels := []metrics.Label{{Name: "service", Value: args.ServiceName}}
|
|
|
|
for _, tag := range args.ServiceTags {
|
|
|
|
labels = append(labels, metrics.Label{Name: "tag", Value: tag})
|
|
|
|
}
|
|
|
|
metrics.IncrCounterWithLabels([]string{"catalog", key, "query-tags"}, 1, labels)
|
|
|
|
}
|
2014-04-04 21:55:44 +00:00
|
|
|
if len(reply.ServiceNodes) == 0 {
|
2018-03-09 16:34:55 +00:00
|
|
|
metrics.IncrCounterWithLabels([]string{"catalog", key, "not-found"}, 1,
|
2017-10-04 23:43:27 +00:00
|
|
|
[]metrics.Label{{Name: "service", Value: args.ServiceName}})
|
2014-04-04 21:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 16:34:55 +00:00
|
|
|
|
2014-04-04 21:55:44 +00:00
|
|
|
return err
|
2013-12-12 19:37:19 +00:00
|
|
|
}
|
2013-12-12 19:46:25 +00:00
|
|
|
|
|
|
|
// NodeServices returns all the services registered as part of a node
|
2014-02-05 19:10:10 +00:00
|
|
|
func (c *Catalog) NodeServices(args *structs.NodeSpecificRequest, reply *structs.IndexedNodeServices) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Catalog.NodeServices", args, reply); done {
|
2013-12-12 19:46:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:43:34 +00:00
|
|
|
// Verify the arguments
|
|
|
|
if args.Node == "" {
|
|
|
|
return fmt.Errorf("Must provide node")
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
var filterType map[string]*structs.NodeService
|
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, filterType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
_, err = c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-20 07:36:50 +00:00
|
|
|
return c.srv.blockingQuery(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2019-12-10 02:26:41 +00:00
|
|
|
index, services, err := state.NodeServices(ws, args.Node, &args.EnterpriseMeta)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-10 05:04:00 +00:00
|
|
|
|
2015-10-12 07:42:09 +00:00
|
|
|
reply.Index, reply.NodeServices = index, services
|
2019-04-16 16:00:15 +00:00
|
|
|
if err := c.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if reply.NodeServices != nil {
|
|
|
|
raw, err := filter.Execute(reply.NodeServices.Services)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.NodeServices.Services = raw.(map[string]*structs.NodeService)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-02-05 19:10:10 +00:00
|
|
|
})
|
2013-12-12 19:46:25 +00:00
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
func (c *Catalog) NodeServiceList(args *structs.NodeSpecificRequest, reply *structs.IndexedNodeServiceList) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Catalog.NodeServiceList", args, reply); done {
|
2019-12-10 02:26:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the arguments
|
|
|
|
if args.Node == "" {
|
|
|
|
return fmt.Errorf("Must provide node")
|
|
|
|
}
|
|
|
|
|
2020-01-24 14:27:25 +00:00
|
|
|
var filterType []*structs.NodeService
|
2019-12-10 02:26:41 +00:00
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, filterType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
_, err = c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, services, err := state.NodeServiceList(ws, args.Node, &args.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.srv.filterACL(args.Token, &services); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index = index
|
|
|
|
if services != nil {
|
|
|
|
reply.NodeServices = *services
|
|
|
|
|
|
|
|
raw, err := filter.Execute(reply.NodeServices.Services)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.NodeServices.Services = raw.([]*structs.NodeService)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2020-06-12 02:05:07 +00:00
|
|
|
|
|
|
|
func (c *Catalog) GatewayServices(args *structs.ServiceSpecificRequest, reply *structs.IndexedGatewayServices) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Catalog.GatewayServices", args, reply); done {
|
2020-06-12 02:05:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-30 18:28:19 +00:00
|
|
|
if authz.ServiceRead(args.ServiceName, &authzContext) != acl.Allow {
|
2020-06-12 02:05:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
var index uint64
|
|
|
|
var services structs.GatewayServices
|
|
|
|
|
|
|
|
supportedGateways := []string{structs.IngressGateway, structs.TerminatingGateway}
|
|
|
|
var found bool
|
|
|
|
for _, kind := range supportedGateways {
|
|
|
|
// We only use this call to validate the RPC call, don't add the watch set
|
|
|
|
_, entry, err := state.ConfigEntry(nil, kind, args.ServiceName, &args.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if entry != nil {
|
|
|
|
found = true
|
2020-06-12 14:57:41 +00:00
|
|
|
break
|
2020-06-12 02:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We log a warning here to indicate that there is a potential
|
|
|
|
// misconfiguration. We explicitly do NOT return an error because this
|
|
|
|
// can occur in the course of normal operation by deleting a
|
|
|
|
// configuration entry or starting the proxy before registering the
|
|
|
|
// config entry.
|
|
|
|
if !found {
|
|
|
|
c.logger.Warn("no terminating-gateway or ingress-gateway associated with this gateway",
|
|
|
|
"gateway", args.ServiceName,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
index, services, err = state.GatewayServices(ws, args.ServiceName, &args.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.srv.filterACL(args.Token, &services); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Services = index, services
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2021-12-02 23:42:47 +00:00
|
|
|
|
|
|
|
func (c *Catalog) VirtualIPForService(args *structs.ServiceSpecificRequest, reply *string) error {
|
|
|
|
if done, err := c.srv.ForwardRPC("Catalog.VirtualIPForService", args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if authz.ServiceRead(args.ServiceName, &authzContext) != acl.Allow {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
state := c.srv.fsm.State()
|
|
|
|
*reply, err = state.VirtualIPForService(structs.NewServiceName(args.ServiceName, &args.EnterpriseMeta))
|
|
|
|
return err
|
|
|
|
}
|