2014-04-27 19:56:06 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2015-07-07 00:28:09 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
bexpr "github.com/hashicorp/go-bexpr"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2017-01-24 17:06:51 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2014-10-02 06:09:00 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2022-02-25 21:46:34 +00:00
|
|
|
hashstructure_v2 "github.com/mitchellh/hashstructure/v2"
|
2021-04-20 18:55:24 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2014-04-27 19:56:06 +00:00
|
|
|
)
|
|
|
|
|
2014-04-28 21:44:36 +00:00
|
|
|
// Internal endpoint is used to query the miscellaneous info that
|
2014-04-27 19:56:06 +00:00
|
|
|
// does not necessarily fit into the other systems. It is also
|
|
|
|
// used to hold undocumented APIs that users should not rely on.
|
2014-04-28 21:44:36 +00:00
|
|
|
type Internal struct {
|
2020-01-28 23:50:41 +00:00
|
|
|
srv *Server
|
|
|
|
logger hclog.Logger
|
2014-04-27 19:56:06 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 19:36:25 +00:00
|
|
|
// NodeInfo is used to retrieve information about a specific node.
|
2014-04-28 21:44:36 +00:00
|
|
|
func (m *Internal) NodeInfo(args *structs.NodeSpecificRequest,
|
2014-04-27 19:56:06 +00:00
|
|
|
reply *structs.IndexedNodeDump) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := m.srv.ForwardRPC("Internal.NodeInfo", args, reply); done {
|
2014-04-27 19:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:40:44 +00:00
|
|
|
_, err := m.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 17:06:51 +00:00
|
|
|
return m.srv.blockingQuery(
|
2015-10-13 03:48:50 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-27 19:56:06 +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, dump, err := state.NodeInfo(ws, args.Node, &args.EnterpriseMeta)
|
2015-10-13 03:48:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Dump = index, dump
|
2015-06-11 19:48:38 +00:00
|
|
|
return m.srv.filterACL(args.Token, reply)
|
2014-04-27 19:56:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:36:25 +00:00
|
|
|
// NodeDump is used to generate information about all of the nodes.
|
2014-04-28 21:44:36 +00:00
|
|
|
func (m *Internal) NodeDump(args *structs.DCSpecificRequest,
|
2014-04-27 19:56:06 +00:00
|
|
|
reply *structs.IndexedNodeDump) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := m.srv.ForwardRPC("Internal.NodeDump", args, reply); done {
|
2014-04-27 19:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:40:44 +00:00
|
|
|
_, err := m.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.Dump)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 17:06:51 +00:00
|
|
|
return m.srv.blockingQuery(
|
2015-10-13 03:48:50 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-27 19:56:06 +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, dump, err := state.NodeDump(ws, &args.EnterpriseMeta)
|
2015-10-13 03:48:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index, reply.Dump = index, dump
|
2019-04-16 16:00:15 +00:00
|
|
|
|
|
|
|
raw, err := filter.Execute(reply.Dump)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Dump = raw.(structs.NodeDump)
|
2021-12-03 23:04:24 +00:00
|
|
|
|
|
|
|
// Note: we filter the results with ACLs *after* applying the user-supplied
|
|
|
|
// bexpr filter, to ensure QueryMeta.ResultsFilteredByACLs does not include
|
|
|
|
// results that would be filtered out even if the user did have permission.
|
|
|
|
if err := m.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:21:11 +00:00
|
|
|
func (m *Internal) ServiceDump(args *structs.ServiceDumpRequest, reply *structs.IndexedNodesWithGateways) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := m.srv.ForwardRPC("Internal.ServiceDump", args, reply); done {
|
2019-04-16 16:00:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:40:44 +00:00
|
|
|
_, err := m.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.Nodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2020-07-30 16:21:11 +00:00
|
|
|
// Get, store, and filter nodes
|
|
|
|
maxIdx, nodes, err := state.ServiceDump(ws, args.ServiceKind, args.UseServiceKind, &args.EnterpriseMeta)
|
2019-04-16 16:00:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-30 16:21:11 +00:00
|
|
|
reply.Nodes = nodes
|
2019-04-16 16:00:15 +00:00
|
|
|
|
2020-07-30 16:21:11 +00:00
|
|
|
// Get, store, and filter gateway services
|
|
|
|
idx, gatewayServices, err := state.DumpGatewayServices(ws)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Gateways = gatewayServices
|
|
|
|
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
reply.Index = maxIdx
|
|
|
|
|
2021-12-03 20:56:14 +00:00
|
|
|
raw, err := filter.Execute(reply.Nodes)
|
|
|
|
if err != nil {
|
2019-04-16 16:00:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-12-03 20:56:14 +00:00
|
|
|
reply.Nodes = raw.(structs.CheckServiceNodes)
|
2019-04-16 16:00:15 +00:00
|
|
|
|
2021-12-03 20:56:14 +00:00
|
|
|
// Note: we filter the results with ACLs *after* applying the user-supplied
|
|
|
|
// bexpr filter, to ensure QueryMeta.ResultsFilteredByACLs does not include
|
|
|
|
// results that would be filtered out even if the user did have permission.
|
|
|
|
if err := m.srv.filterACL(args.Token, reply); err != nil {
|
2019-04-16 16:00:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-04-27 19:56:06 +00:00
|
|
|
})
|
|
|
|
}
|
2014-08-28 22:00:49 +00:00
|
|
|
|
2022-03-22 23:58:41 +00:00
|
|
|
func (m *Internal) CatalogOverview(args *structs.DCSpecificRequest, reply *structs.CatalogSummary) error {
|
|
|
|
if done, err := m.srv.ForwardRPC("Internal.CatalogOverview", args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
authz, err := m.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if authz.OperatorRead(nil) != acl.Allow {
|
|
|
|
return acl.PermissionDeniedByACLUnnamed(authz, nil, acl.ResourceOperator, acl.AccessRead)
|
|
|
|
}
|
|
|
|
|
|
|
|
summary := m.srv.overviewManager.GetCurrentSummary()
|
|
|
|
if summary != nil {
|
|
|
|
*reply = *summary
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 01:42:03 +00:00
|
|
|
func (m *Internal) ServiceTopology(args *structs.ServiceSpecificRequest, reply *structs.IndexedServiceTopology) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := m.srv.ForwardRPC("Internal.ServiceTopology", args, reply); done {
|
2020-09-29 01:42:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if args.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Must provide a service name")
|
|
|
|
}
|
|
|
|
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
authz, err := m.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := m.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().ServiceReadAllowed(args.ServiceName, &authzContext); err != nil {
|
|
|
|
return err
|
2020-09-29 01:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2021-07-30 18:55:35 +00:00
|
|
|
defaultAllow := authz.IntentionDefaultAllow(nil)
|
2020-10-08 00:35:34 +00:00
|
|
|
|
2020-10-08 23:31:54 +00:00
|
|
|
index, topology, err := state.ServiceTopology(ws, args.Datacenter, args.ServiceName, args.ServiceKind, defaultAllow, &args.EnterpriseMeta)
|
2020-09-29 01:42:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index = index
|
|
|
|
reply.ServiceTopology = topology
|
|
|
|
|
|
|
|
if err := m.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-15 23:50:47 +00:00
|
|
|
// IntentionUpstreams returns the upstreams of a service. Upstreams are inferred from intentions.
|
2021-03-14 04:44:24 +00:00
|
|
|
// If intentions allow a connection from the target to some candidate service, the candidate service is considered
|
|
|
|
// an upstream of the target.
|
|
|
|
func (m *Internal) IntentionUpstreams(args *structs.ServiceSpecificRequest, reply *structs.IndexedServiceList) error {
|
|
|
|
// Exit early if Connect hasn't been enabled.
|
|
|
|
if !m.srv.config.ConnectEnabled {
|
|
|
|
return ErrConnectNotEnabled
|
|
|
|
}
|
|
|
|
if args.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Must provide a service name")
|
|
|
|
}
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := m.srv.ForwardRPC("Internal.IntentionUpstreams", args, reply); done {
|
2021-03-14 04:44:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
authz, err := m.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := m.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-25 21:46:34 +00:00
|
|
|
var (
|
|
|
|
priorHash uint64
|
|
|
|
ranOnce bool
|
|
|
|
)
|
2021-03-14 04:44:24 +00:00
|
|
|
return m.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2021-07-30 18:55:35 +00:00
|
|
|
defaultDecision := authz.IntentionDefaultAllow(nil)
|
2021-03-14 04:44:24 +00:00
|
|
|
|
|
|
|
sn := structs.NewServiceName(args.ServiceName, &args.EnterpriseMeta)
|
|
|
|
index, services, err := state.IntentionTopology(ws, sn, false, defaultDecision)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Services = index, services
|
2021-07-30 21:08:58 +00:00
|
|
|
m.srv.filterACLWithAuthorizer(authz, reply)
|
2022-02-25 21:46:34 +00:00
|
|
|
|
|
|
|
// Generate a hash of the intentions content driving this response.
|
|
|
|
// Use it to determine if the response is identical to a prior
|
|
|
|
// wakeup.
|
|
|
|
newHash, err := hashstructure_v2.Hash(services, 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
|
|
|
|
}
|
|
|
|
|
2021-07-30 21:08:58 +00:00
|
|
|
return nil
|
2021-03-14 04:44:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:35:17 +00:00
|
|
|
// GatewayServiceNodes returns all the nodes for services associated with a gateway along with their gateway config
|
|
|
|
func (m *Internal) GatewayServiceDump(args *structs.ServiceSpecificRequest, reply *structs.IndexedServiceDump) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := m.srv.ForwardRPC("Internal.GatewayServiceDump", args, reply); done {
|
2020-05-11 17:35:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the arguments
|
|
|
|
if args.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Must provide gateway name")
|
|
|
|
}
|
|
|
|
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
authz, err := m.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need read access to the gateway we're trying to find services for, so check that first.
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().ServiceReadAllowed(args.ServiceName, &authzContext); err != nil {
|
|
|
|
return err
|
2020-05-11 17:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = m.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
var maxIdx uint64
|
|
|
|
idx, gatewayServices, err := state.GatewayServices(ws, args.ServiceName, &args.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over the gateway <-> serviceName mappings and fetch all service instances for each
|
|
|
|
var result structs.ServiceDump
|
|
|
|
for _, gs := range gatewayServices {
|
2020-06-12 14:57:41 +00:00
|
|
|
idx, instances, err := state.CheckServiceNodes(ws, gs.Service.Name, &gs.Service.EnterpriseMeta)
|
2020-05-11 17:35:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
for _, n := range instances {
|
|
|
|
svc := structs.ServiceInfo{
|
|
|
|
Node: n.Node,
|
|
|
|
Service: n.Service,
|
|
|
|
Checks: n.Checks,
|
|
|
|
GatewayService: gs,
|
|
|
|
}
|
|
|
|
result = append(result, &svc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we store the gateway <-> service mapping even if there are no instances of the service
|
|
|
|
if len(instances) == 0 {
|
|
|
|
svc := structs.ServiceInfo{
|
|
|
|
GatewayService: gs,
|
|
|
|
}
|
|
|
|
result = append(result, &svc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reply.Index, reply.Dump = maxIdx, result
|
|
|
|
|
|
|
|
if err := m.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-11 23:20:41 +00:00
|
|
|
// Match returns the set of intentions that match the given source/destination.
|
|
|
|
func (m *Internal) GatewayIntentions(args *structs.IntentionQueryRequest, reply *structs.IndexedIntentions) error {
|
|
|
|
// Forward if necessary
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := m.srv.ForwardRPC("Internal.GatewayIntentions", args, reply); done {
|
2020-08-11 23:20:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args.Match.Entries) > 1 {
|
|
|
|
return fmt.Errorf("Expected 1 gateway name, got %d", len(args.Match.Entries))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ACL token for the request for the checks below.
|
2022-04-05 21:10:06 +00:00
|
|
|
var entMeta acl.EnterpriseMeta
|
2020-08-11 23:20:41 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
|
|
|
|
authz, err := m.srv.ResolveTokenAndDefaultMeta(args.Token, &entMeta, &authzContext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Match.Entries[0].Namespace == "" {
|
|
|
|
args.Match.Entries[0].Namespace = entMeta.NamespaceOrDefault()
|
|
|
|
}
|
|
|
|
if err := m.srv.validateEnterpriseIntentionNamespace(args.Match.Entries[0].Namespace, true); err != nil {
|
|
|
|
return fmt.Errorf("Invalid match entry namespace %q: %v", args.Match.Entries[0].Namespace, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need read access to the gateway we're trying to find intentions for, so check that first.
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().ServiceReadAllowed(args.Match.Entries[0].Name, &authzContext); err != nil {
|
|
|
|
return err
|
2020-08-11 23:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
var maxIdx uint64
|
|
|
|
idx, gatewayServices, err := state.GatewayServices(ws, args.Match.Entries[0].Name, &entMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over the gateway <-> serviceName mappings and fetch all intentions for each
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
result := make(structs.Intentions, 0)
|
|
|
|
|
|
|
|
for _, gs := range gatewayServices {
|
|
|
|
entry := structs.IntentionMatchEntry{
|
|
|
|
Namespace: gs.Service.NamespaceOrDefault(),
|
2021-09-16 19:31:19 +00:00
|
|
|
Partition: gs.Service.PartitionOrDefault(),
|
2020-08-11 23:20:41 +00:00
|
|
|
Name: gs.Service.Name,
|
|
|
|
}
|
|
|
|
idx, intentions, err := state.IntentionMatchOne(ws, entry, structs.IntentionMatchDestination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deduplicate wildcard intentions
|
|
|
|
for _, ixn := range intentions {
|
|
|
|
if !seen[ixn.ID] {
|
|
|
|
result = append(result, ixn)
|
|
|
|
seen[ixn.ID] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Intentions = maxIdx, result
|
|
|
|
if reply.Intentions == nil {
|
|
|
|
reply.Intentions = make(structs.Intentions, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-08-28 22:00:49 +00:00
|
|
|
// EventFire is a bit of an odd endpoint, but it allows for a cross-DC RPC
|
|
|
|
// call to fire an event. The primary use case is to enable user events being
|
|
|
|
// triggered in a remote DC.
|
|
|
|
func (m *Internal) EventFire(args *structs.EventFireRequest,
|
|
|
|
reply *structs.EventFireResponse) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := m.srv.ForwardRPC("Internal.EventFire", args, reply); done {
|
2014-08-28 22:00:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-18 01:57:17 +00:00
|
|
|
// Check ACLs
|
2022-01-22 20:05:42 +00:00
|
|
|
authz, err := m.srv.ResolveTokenAndDefaultMeta(args.Token, nil, nil)
|
2015-06-18 01:57:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().EventWriteAllowed(args.Name, nil); err != nil {
|
2022-01-22 20:05:42 +00:00
|
|
|
accessorID := authz.AccessorID()
|
2020-01-28 23:50:41 +00:00
|
|
|
m.logger.Warn("user event blocked by ACLs", "event", args.Name, "accessorID", accessorID)
|
2022-03-11 02:48:27 +00:00
|
|
|
return err
|
2015-06-18 01:57:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-28 22:00:49 +00:00
|
|
|
// Set the query meta data
|
2021-12-03 17:11:26 +00:00
|
|
|
m.srv.setQueryMeta(&reply.QueryMeta, args.Token)
|
2014-08-28 22:00:49 +00:00
|
|
|
|
2015-07-14 18:38:12 +00:00
|
|
|
// Add the consul prefix to the event name
|
|
|
|
eventName := userEventName(args.Name)
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
// Fire the event on all LAN segments
|
2021-11-15 15:51:14 +00:00
|
|
|
return m.srv.LANSendUserEvent(eventName, args.Payload, false)
|
2014-08-28 22:00:49 +00:00
|
|
|
}
|
2014-09-24 23:39:14 +00:00
|
|
|
|
2014-10-09 17:25:53 +00:00
|
|
|
// KeyringOperation will query the WAN and LAN gossip keyrings of all nodes.
|
2014-10-03 00:10:54 +00:00
|
|
|
func (m *Internal) KeyringOperation(
|
2014-09-25 01:30:34 +00:00
|
|
|
args *structs.KeyringRequest,
|
|
|
|
reply *structs.KeyringResponses) error {
|
2014-09-24 23:39:14 +00:00
|
|
|
|
2020-08-11 11:35:48 +00:00
|
|
|
// Error aggressively to be clear about LocalOnly behavior
|
|
|
|
if args.LocalOnly && args.Operation != structs.KeyringList {
|
|
|
|
return fmt.Errorf("argument error: LocalOnly can only be used for List operations")
|
|
|
|
}
|
|
|
|
|
2015-07-07 00:28:09 +00:00
|
|
|
// Check ACLs
|
2022-01-23 17:31:48 +00:00
|
|
|
authz, err := m.srv.ACLResolver.ResolveToken(args.Token)
|
2015-07-07 00:28:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-02 17:07:29 +00:00
|
|
|
if err := m.srv.validateEnterpriseToken(authz.Identity()); err != nil {
|
2020-02-04 20:58:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-30 17:16:38 +00:00
|
|
|
switch args.Operation {
|
|
|
|
case structs.KeyringList:
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().KeyringReadAllowed(nil); err != nil {
|
|
|
|
return err
|
2021-07-30 17:16:38 +00:00
|
|
|
}
|
|
|
|
case structs.KeyringInstall:
|
|
|
|
fallthrough
|
|
|
|
case structs.KeyringUse:
|
|
|
|
fallthrough
|
|
|
|
case structs.KeyringRemove:
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().KeyringWriteAllowed(nil); err != nil {
|
|
|
|
return err
|
2015-07-07 00:28:09 +00:00
|
|
|
}
|
2021-07-30 17:16:38 +00:00
|
|
|
default:
|
|
|
|
panic("Invalid keyring operation")
|
2015-07-07 00:28:09 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 11:35:48 +00:00
|
|
|
if args.LocalOnly || args.Forwarded || m.srv.serfWAN == nil {
|
|
|
|
// Handle operations that are localOnly, already forwarded or
|
|
|
|
// there is no serfWAN. If any of this is the case this
|
|
|
|
// operation shouldn't go out to other dcs or WAN pool.
|
|
|
|
reply.Responses = append(reply.Responses, m.executeKeyringOpLAN(args)...)
|
|
|
|
} else {
|
|
|
|
// Handle not already forwarded, non-local operations.
|
2019-08-12 18:11:11 +00:00
|
|
|
|
2020-08-11 11:35:48 +00:00
|
|
|
// Marking this as forwarded because this is what we are about
|
|
|
|
// to do. Prevents the same message from being fowarded by
|
|
|
|
// other servers.
|
|
|
|
args.Forwarded = true
|
|
|
|
reply.Responses = append(reply.Responses, m.executeKeyringOpWAN(args))
|
|
|
|
reply.Responses = append(reply.Responses, m.executeKeyringOpLAN(args)...)
|
2019-08-12 18:11:11 +00:00
|
|
|
|
2020-08-11 11:35:48 +00:00
|
|
|
dcs := m.srv.router.GetRemoteDatacenters(m.srv.config.Datacenter)
|
|
|
|
responses, err := m.srv.keyringRPCs("Internal.KeyringOperation", args, dcs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-08-12 18:11:11 +00:00
|
|
|
}
|
2020-08-11 11:35:48 +00:00
|
|
|
reply.Add(responses)
|
2014-09-24 23:39:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:35:48 +00:00
|
|
|
func (m *Internal) executeKeyringOpLAN(args *structs.KeyringRequest) []*structs.KeyringResponse {
|
|
|
|
responses := []*structs.KeyringResponse{}
|
2021-11-15 15:51:14 +00:00
|
|
|
_ = m.srv.DoWithLANSerfs(func(poolName, poolKind string, pool *serf.Serf) error {
|
|
|
|
mgr := pool.KeyManager()
|
2020-08-11 11:35:48 +00:00
|
|
|
serfResp, err := m.executeKeyringOpMgr(mgr, args)
|
|
|
|
resp := translateKeyResponseToKeyringResponse(serfResp, m.srv.config.Datacenter, err)
|
2021-11-15 15:51:14 +00:00
|
|
|
if poolKind == PoolKindSegment {
|
|
|
|
resp.Segment = poolName
|
|
|
|
} else {
|
|
|
|
resp.Partition = poolName
|
|
|
|
}
|
2020-08-11 11:35:48 +00:00
|
|
|
responses = append(responses, &resp)
|
2021-11-15 15:51:14 +00:00
|
|
|
return nil
|
|
|
|
}, nil)
|
2020-08-11 11:35:48 +00:00
|
|
|
return responses
|
|
|
|
}
|
2014-10-05 20:59:27 +00:00
|
|
|
|
2020-08-11 11:35:48 +00:00
|
|
|
func (m *Internal) executeKeyringOpWAN(args *structs.KeyringRequest) *structs.KeyringResponse {
|
|
|
|
mgr := m.srv.KeyManagerWAN()
|
|
|
|
serfResp, err := m.executeKeyringOpMgr(mgr, args)
|
|
|
|
resp := translateKeyResponseToKeyringResponse(serfResp, m.srv.config.Datacenter, err)
|
|
|
|
resp.WAN = true
|
|
|
|
return &resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func translateKeyResponseToKeyringResponse(keyresponse *serf.KeyResponse, datacenter string, err error) structs.KeyringResponse {
|
|
|
|
resp := structs.KeyringResponse{
|
add primary keys to list keyring (#8522)
During gossip encryption key rotation it would be nice to be able to see if all nodes are using the same key. This PR adds another field to the json response from `GET v1/operator/keyring` which lists the primary keys in use per dc. That way an operator can tell when a key was successfully setup as primary key.
Based on https://github.com/hashicorp/serf/pull/611 to add primary key to list keyring output:
```json
[
{
"WAN": true,
"Datacenter": "dc2",
"Segment": "",
"Keys": {
"0OuM4oC3Os18OblWiBbZUaHA7Hk+tNs/6nhNYtaNduM=": 6,
"SINm887hKTzmMWeBNKTJReaTLX3mBEJKriDyt88Ad+g=": 6
},
"PrimaryKeys": {
"SINm887hKTzmMWeBNKTJReaTLX3mBEJKriDyt88Ad+g=": 6
},
"NumNodes": 6
},
{
"WAN": false,
"Datacenter": "dc2",
"Segment": "",
"Keys": {
"0OuM4oC3Os18OblWiBbZUaHA7Hk+tNs/6nhNYtaNduM=": 8,
"SINm887hKTzmMWeBNKTJReaTLX3mBEJKriDyt88Ad+g=": 8
},
"PrimaryKeys": {
"SINm887hKTzmMWeBNKTJReaTLX3mBEJKriDyt88Ad+g=": 8
},
"NumNodes": 8
},
{
"WAN": false,
"Datacenter": "dc1",
"Segment": "",
"Keys": {
"0OuM4oC3Os18OblWiBbZUaHA7Hk+tNs/6nhNYtaNduM=": 3,
"SINm887hKTzmMWeBNKTJReaTLX3mBEJKriDyt88Ad+g=": 8
},
"PrimaryKeys": {
"SINm887hKTzmMWeBNKTJReaTLX3mBEJKriDyt88Ad+g=": 8
},
"NumNodes": 8
}
]
```
I intentionally did not change the CLI output because I didn't find a good way of displaying this information. There are a couple of options that we could implement later:
* add a flag to show the primary keys
* add a flag to show json output
Fixes #3393.
2020-08-18 07:50:24 +00:00
|
|
|
Datacenter: datacenter,
|
|
|
|
Messages: keyresponse.Messages,
|
|
|
|
Keys: keyresponse.Keys,
|
|
|
|
PrimaryKeys: keyresponse.PrimaryKeys,
|
|
|
|
NumNodes: keyresponse.NumNodes,
|
2014-10-05 20:59:27 +00:00
|
|
|
}
|
2020-08-11 11:35:48 +00:00
|
|
|
if err != nil {
|
|
|
|
resp.Error = err.Error()
|
2014-10-05 20:59:27 +00:00
|
|
|
}
|
2020-08-11 11:35:48 +00:00
|
|
|
return resp
|
2017-08-14 14:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// executeKeyringOpMgr executes the appropriate keyring-related function based on
|
|
|
|
// the type of keyring operation in the request. It takes the KeyManager as an
|
|
|
|
// argument, so it can handle any operation for either LAN or WAN pools.
|
|
|
|
func (m *Internal) executeKeyringOpMgr(
|
|
|
|
mgr *serf.KeyManager,
|
|
|
|
args *structs.KeyringRequest,
|
2020-08-11 11:35:48 +00:00
|
|
|
) (*serf.KeyResponse, error) {
|
2017-08-14 14:36:07 +00:00
|
|
|
var serfResp *serf.KeyResponse
|
|
|
|
var err error
|
2014-10-03 00:10:54 +00:00
|
|
|
|
2017-02-02 02:42:41 +00:00
|
|
|
opts := &serf.KeyRequestOptions{RelayFactor: args.RelayFactor}
|
2014-10-03 00:10:54 +00:00
|
|
|
switch args.Operation {
|
|
|
|
case structs.KeyringList:
|
2017-02-02 02:42:41 +00:00
|
|
|
serfResp, err = mgr.ListKeysWithOptions(opts)
|
2014-10-03 00:10:54 +00:00
|
|
|
case structs.KeyringInstall:
|
2017-02-02 02:42:41 +00:00
|
|
|
serfResp, err = mgr.InstallKeyWithOptions(args.Key, opts)
|
2014-10-03 00:10:54 +00:00
|
|
|
case structs.KeyringUse:
|
2017-02-02 02:42:41 +00:00
|
|
|
serfResp, err = mgr.UseKeyWithOptions(args.Key, opts)
|
2014-10-03 00:10:54 +00:00
|
|
|
case structs.KeyringRemove:
|
2017-02-02 02:42:41 +00:00
|
|
|
serfResp, err = mgr.RemoveKeyWithOptions(args.Key, opts)
|
2014-09-24 23:39:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 11:35:48 +00:00
|
|
|
return serfResp, err
|
2014-10-02 06:09:00 +00:00
|
|
|
}
|