99e0a124cb
This PR is almost a complete rewrite of the ACL system within Consul. It brings the features more in line with other HashiCorp products. Obviously there is quite a bit left to do here but most of it is related docs, testing and finishing the last few commands in the CLI. I will update the PR description and check off the todos as I finish them over the next few days/week. Description At a high level this PR is mainly to split ACL tokens from Policies and to split the concepts of Authorization from Identities. A lot of this PR is mostly just to support CRUD operations on ACLTokens and ACLPolicies. These in and of themselves are not particularly interesting. The bigger conceptual changes are in how tokens get resolved, how backwards compatibility is handled and the separation of policy from identity which could lead the way to allowing for alternative identity providers. On the surface and with a new cluster the ACL system will look very similar to that of Nomads. Both have tokens and policies. Both have local tokens. The ACL management APIs for both are very similar. I even ripped off Nomad's ACL bootstrap resetting procedure. There are a few key differences though. Nomad requires token and policy replication where Consul only requires policy replication with token replication being opt-in. In Consul local tokens only work with token replication being enabled though. All policies in Nomad are globally applicable. In Consul all policies are stored and replicated globally but can be scoped to a subset of the datacenters. This allows for more granular access management. Unlike Nomad, Consul has legacy baggage in the form of the original ACL system. The ramifications of this are: A server running the new system must still support other clients using the legacy system. A client running the new system must be able to use the legacy RPCs when the servers in its datacenter are running the legacy system. The primary ACL DC's servers running in legacy mode needs to be a gate that keeps everything else in the entire multi-DC cluster running in legacy mode. So not only does this PR implement the new ACL system but has a legacy mode built in for when the cluster isn't ready for new ACLs. Also detecting that new ACLs can be used is automatic and requires no configuration on the part of administrators. This process is detailed more in the "Transitioning from Legacy to New ACL Mode" section below.
212 lines
6.5 KiB
Go
212 lines
6.5 KiB
Go
package consul
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/armon/go-metrics"
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
"github.com/hashicorp/go-memdb"
|
|
)
|
|
|
|
// Health endpoint is used to query the health information
|
|
type Health struct {
|
|
srv *Server
|
|
}
|
|
|
|
// ChecksInState is used to get all the checks in a given state
|
|
func (h *Health) ChecksInState(args *structs.ChecksInStateRequest,
|
|
reply *structs.IndexedHealthChecks) error {
|
|
if done, err := h.srv.forward("Health.ChecksInState", args, args, reply); done {
|
|
return err
|
|
}
|
|
|
|
return h.srv.blockingQuery(
|
|
&args.QueryOptions,
|
|
&reply.QueryMeta,
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
var index uint64
|
|
var checks structs.HealthChecks
|
|
var err error
|
|
if len(args.NodeMetaFilters) > 0 {
|
|
index, checks, err = state.ChecksInStateByNodeMeta(ws, args.State, args.NodeMetaFilters)
|
|
} else {
|
|
index, checks, err = state.ChecksInState(ws, args.State)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
reply.Index, reply.HealthChecks = index, checks
|
|
if err := h.srv.filterACL(args.Token, reply); err != nil {
|
|
return err
|
|
}
|
|
return h.srv.sortNodesByDistanceFrom(args.Source, reply.HealthChecks)
|
|
})
|
|
}
|
|
|
|
// NodeChecks is used to get all the checks for a node
|
|
func (h *Health) NodeChecks(args *structs.NodeSpecificRequest,
|
|
reply *structs.IndexedHealthChecks) error {
|
|
if done, err := h.srv.forward("Health.NodeChecks", args, args, reply); done {
|
|
return err
|
|
}
|
|
|
|
return h.srv.blockingQuery(
|
|
&args.QueryOptions,
|
|
&reply.QueryMeta,
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
index, checks, err := state.NodeChecks(ws, args.Node)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
reply.Index, reply.HealthChecks = index, checks
|
|
return h.srv.filterACL(args.Token, reply)
|
|
})
|
|
}
|
|
|
|
// ServiceChecks is used to get all the checks for a service
|
|
func (h *Health) ServiceChecks(args *structs.ServiceSpecificRequest,
|
|
reply *structs.IndexedHealthChecks) error {
|
|
// Reject if tag filtering is on
|
|
if args.TagFilter {
|
|
return fmt.Errorf("Tag filtering is not supported")
|
|
}
|
|
|
|
// Potentially forward
|
|
if done, err := h.srv.forward("Health.ServiceChecks", args, args, reply); done {
|
|
return err
|
|
}
|
|
|
|
return h.srv.blockingQuery(
|
|
&args.QueryOptions,
|
|
&reply.QueryMeta,
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
var index uint64
|
|
var checks structs.HealthChecks
|
|
var err error
|
|
if len(args.NodeMetaFilters) > 0 {
|
|
index, checks, err = state.ServiceChecksByNodeMeta(ws, args.ServiceName, args.NodeMetaFilters)
|
|
} else {
|
|
index, checks, err = state.ServiceChecks(ws, args.ServiceName)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
reply.Index, reply.HealthChecks = index, checks
|
|
if err := h.srv.filterACL(args.Token, reply); err != nil {
|
|
return err
|
|
}
|
|
return h.srv.sortNodesByDistanceFrom(args.Source, reply.HealthChecks)
|
|
})
|
|
}
|
|
|
|
// ServiceNodes returns all the nodes registered as part of a service including health info
|
|
func (h *Health) ServiceNodes(args *structs.ServiceSpecificRequest, reply *structs.IndexedCheckServiceNodes) error {
|
|
if done, err := h.srv.forward("Health.ServiceNodes", args, args, reply); done {
|
|
return err
|
|
}
|
|
|
|
// Verify the arguments
|
|
if args.ServiceName == "" {
|
|
return fmt.Errorf("Must provide service name")
|
|
}
|
|
|
|
// Determine the function we'll call
|
|
var f func(memdb.WatchSet, *state.Store, *structs.ServiceSpecificRequest) (uint64, structs.CheckServiceNodes, error)
|
|
switch {
|
|
case args.Connect:
|
|
f = h.serviceNodesConnect
|
|
case args.TagFilter:
|
|
f = h.serviceNodesTagFilter
|
|
default:
|
|
f = h.serviceNodesDefault
|
|
}
|
|
|
|
// 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 {
|
|
// Fetch the ACL token, if any.
|
|
rule, err := h.srv.ResolveToken(args.Token)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if rule != nil && !rule.ServiceRead(args.ServiceName) {
|
|
// Just return nil, which will return an empty response (tested)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
err := h.srv.blockingQuery(
|
|
&args.QueryOptions,
|
|
&reply.QueryMeta,
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
index, nodes, err := f(ws, state, args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
reply.Index, reply.Nodes = index, nodes
|
|
if len(args.NodeMetaFilters) > 0 {
|
|
reply.Nodes = nodeMetaFilter(args.NodeMetaFilters, reply.Nodes)
|
|
}
|
|
if err := h.srv.filterACL(args.Token, reply); err != nil {
|
|
return err
|
|
}
|
|
return h.srv.sortNodesByDistanceFrom(args.Source, reply.Nodes)
|
|
})
|
|
|
|
// Provide some metrics
|
|
if err == nil {
|
|
// For metrics, we separate Connect-based lookups from non-Connect
|
|
key := "service"
|
|
if args.Connect {
|
|
key = "connect"
|
|
}
|
|
|
|
metrics.IncrCounterWithLabels([]string{"health", key, "query"}, 1,
|
|
[]metrics.Label{{Name: "service", Value: args.ServiceName}})
|
|
if args.ServiceTag != "" {
|
|
metrics.IncrCounterWithLabels([]string{"health", key, "query-tag"}, 1,
|
|
[]metrics.Label{{Name: "service", Value: args.ServiceName}, {Name: "tag", Value: args.ServiceTag}})
|
|
}
|
|
if len(args.ServiceTags) > 0 {
|
|
// Sort tags so that the metric is the same even if the request
|
|
// tags are in a different order
|
|
sort.Strings(args.ServiceTags)
|
|
|
|
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{"health", key, "query-tags"}, 1, labels)
|
|
}
|
|
if len(reply.Nodes) == 0 {
|
|
metrics.IncrCounterWithLabels([]string{"health", key, "not-found"}, 1,
|
|
[]metrics.Label{{Name: "service", Value: args.ServiceName}})
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// The serviceNodes* functions below are the various lookup methods that
|
|
// can be used by the ServiceNodes endpoint.
|
|
|
|
func (h *Health) serviceNodesConnect(ws memdb.WatchSet, s *state.Store, args *structs.ServiceSpecificRequest) (uint64, structs.CheckServiceNodes, error) {
|
|
return s.CheckConnectServiceNodes(ws, args.ServiceName)
|
|
}
|
|
|
|
func (h *Health) serviceNodesTagFilter(ws memdb.WatchSet, s *state.Store, args *structs.ServiceSpecificRequest) (uint64, structs.CheckServiceNodes, error) {
|
|
// DNS service lookups populate the ServiceTag field. In this case,
|
|
// use ServiceTag instead of the ServiceTags field.
|
|
if args.ServiceTag != "" {
|
|
return s.CheckServiceTagNodes(ws, args.ServiceName, []string{args.ServiceTag})
|
|
}
|
|
return s.CheckServiceTagNodes(ws, args.ServiceName, args.ServiceTags)
|
|
}
|
|
|
|
func (h *Health) serviceNodesDefault(ws memdb.WatchSet, s *state.Store, args *structs.ServiceSpecificRequest) (uint64, structs.CheckServiceNodes, error) {
|
|
return s.CheckServiceNodes(ws, args.ServiceName)
|
|
}
|