2018-02-28 18:04:27 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2018-02-28 23:54:48 +00:00
|
|
|
"errors"
|
2018-03-01 05:11:35 +00:00
|
|
|
"fmt"
|
2018-02-28 18:28:07 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
2018-03-04 08:39:56 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2018-05-11 05:35:47 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-02-28 18:04:27 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-01-13 20:51:40 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2020-04-07 15:48:44 +00:00
|
|
|
"github.com/hashicorp/go-bexpr"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2018-02-28 18:04:27 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
)
|
|
|
|
|
2018-02-28 23:54:48 +00:00
|
|
|
var (
|
|
|
|
// ErrIntentionNotFound is returned if the intention lookup failed.
|
|
|
|
ErrIntentionNotFound = errors.New("Intention not found")
|
|
|
|
)
|
|
|
|
|
2018-02-28 18:04:27 +00:00
|
|
|
// Intention manages the Connect intentions.
|
|
|
|
type Intention struct {
|
|
|
|
// srv is a pointer back to the server.
|
2020-01-28 23:50:41 +00:00
|
|
|
srv *Server
|
|
|
|
logger hclog.Logger
|
2018-02-28 18:04:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
func (s *Intention) checkIntentionID(id string) (bool, error) {
|
|
|
|
state := s.srv.fsm.State()
|
|
|
|
if _, ixn, err := state.IntentionGet(nil, id); err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if ixn != nil {
|
|
|
|
return false, nil
|
2019-01-22 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2018-02-28 18:28:07 +00:00
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
// prepareApplyCreate validates that the requester has permissions to create the new intention,
|
|
|
|
// generates a new uuid for the intention and generally validates that the request is well-formed
|
2020-01-27 19:54:32 +00:00
|
|
|
func (s *Intention) prepareApplyCreate(ident structs.ACLIdentity, authz acl.Authorizer, entMeta *structs.EnterpriseMeta, args *structs.IntentionRequest) error {
|
2020-01-13 20:51:40 +00:00
|
|
|
if !args.Intention.CanWrite(authz) {
|
2020-01-27 19:54:32 +00:00
|
|
|
var accessorID string
|
|
|
|
if ident != nil {
|
|
|
|
accessorID = ident.ID()
|
|
|
|
}
|
|
|
|
// todo(kit) Migrate intention access denial logging over to audit logging when we implement it
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("Intention creation denied due to ACLs", "intention", args.Intention.ID, "accessorID", accessorID)
|
2020-01-13 20:51:40 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2018-03-03 16:43:19 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 18:28:07 +00:00
|
|
|
// If no ID is provided, generate a new ID. This must be done prior to
|
|
|
|
// appending to the Raft log, because the ID is not deterministic. Once
|
|
|
|
// the entry is in the log, the state update MUST be deterministic or
|
|
|
|
// the followers will not converge.
|
2020-01-13 20:51:40 +00:00
|
|
|
if args.Intention.ID != "" {
|
|
|
|
return fmt.Errorf("ID must be empty when creating a new intention")
|
|
|
|
}
|
2018-02-28 18:28:07 +00:00
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
var err error
|
|
|
|
args.Intention.ID, err = lib.GenerateUUID(s.checkIntentionID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Set the created at
|
|
|
|
args.Intention.CreatedAt = time.Now().UTC()
|
|
|
|
args.Intention.UpdatedAt = args.Intention.CreatedAt
|
2018-03-03 16:43:19 +00:00
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
// Default source type
|
|
|
|
if args.Intention.SourceType == "" {
|
|
|
|
args.Intention.SourceType = structs.IntentionSourceConsul
|
2018-02-28 18:28:07 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
args.Intention.DefaultNamespaces(entMeta)
|
|
|
|
|
|
|
|
// Validate. We do not validate on delete since it is valid to only
|
|
|
|
// send an ID in that case.
|
|
|
|
// Set the precedence
|
|
|
|
args.Intention.UpdatePrecedence()
|
|
|
|
|
|
|
|
if err := args.Intention.Validate(); err != nil {
|
2018-03-04 08:39:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
// make sure we set the hash prior to raft application
|
2020-05-15 01:10:03 +00:00
|
|
|
args.Intention.SetHash()
|
2020-01-13 20:51:40 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareApplyUpdate validates that the requester has permissions on both the updated and existing
|
|
|
|
// intention as well as generally validating that the request is well-formed
|
2020-01-27 19:54:32 +00:00
|
|
|
func (s *Intention) prepareApplyUpdate(ident structs.ACLIdentity, authz acl.Authorizer, entMeta *structs.EnterpriseMeta, args *structs.IntentionRequest) error {
|
2020-01-13 20:51:40 +00:00
|
|
|
if !args.Intention.CanWrite(authz) {
|
2020-01-27 19:54:32 +00:00
|
|
|
var accessorID string
|
|
|
|
if ident != nil {
|
|
|
|
accessorID = ident.ID()
|
|
|
|
}
|
|
|
|
// todo(kit) Migrate intention access denial logging over to audit logging when we implement it
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("Update operation on intention denied due to ACLs", "intention", args.Intention.ID, "accessorID", accessorID)
|
2020-01-13 20:51:40 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2018-03-04 08:39:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
_, ixn, err := s.srv.fsm.State().IntentionGet(nil, args.Intention.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Intention lookup failed: %v", err)
|
|
|
|
}
|
|
|
|
if ixn == nil {
|
|
|
|
return fmt.Errorf("Cannot modify non-existent intention: '%s'", args.Intention.ID)
|
|
|
|
}
|
2018-03-04 19:35:39 +00:00
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
// Perform the ACL check that we have write to the old intention too,
|
|
|
|
// which must be true to perform any rename. This is the only ACL enforcement
|
|
|
|
// done for deletions and a secondary enforcement for updates.
|
|
|
|
if !ixn.CanWrite(authz) {
|
2020-01-27 19:54:32 +00:00
|
|
|
var accessorID string
|
|
|
|
if ident != nil {
|
|
|
|
accessorID = ident.ID()
|
|
|
|
}
|
|
|
|
// todo(kit) Migrate intention access denial logging over to audit logging when we implement it
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("Update operation on intention denied due to ACLs", "intention", args.Intention.ID, "accessorID", accessorID)
|
2020-01-13 20:51:40 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2018-03-01 05:11:35 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
// We always update the updatedat field.
|
2018-03-06 17:04:44 +00:00
|
|
|
args.Intention.UpdatedAt = time.Now().UTC()
|
2018-03-03 16:43:19 +00:00
|
|
|
|
2018-03-03 17:55:27 +00:00
|
|
|
// Default source type
|
|
|
|
if args.Intention.SourceType == "" {
|
|
|
|
args.Intention.SourceType = structs.IntentionSourceConsul
|
|
|
|
}
|
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
args.Intention.DefaultNamespaces(entMeta)
|
2018-03-03 16:51:40 +00:00
|
|
|
|
2018-03-03 18:12:05 +00:00
|
|
|
// Validate. We do not validate on delete since it is valid to only
|
|
|
|
// send an ID in that case.
|
2020-01-13 20:51:40 +00:00
|
|
|
// Set the precedence
|
|
|
|
args.Intention.UpdatePrecedence()
|
2018-06-07 04:11:37 +00:00
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
if err := args.Intention.Validate(); err != nil {
|
|
|
|
return err
|
2018-03-03 17:43:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:28:09 +00:00
|
|
|
// make sure we set the hash prior to raft application
|
2020-05-15 01:10:03 +00:00
|
|
|
args.Intention.SetHash()
|
2019-06-26 16:28:09 +00:00
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareApplyDelete ensures that the intention specified by the ID in the request exists
|
|
|
|
// and that the requester is authorized to delete it
|
2020-01-27 19:54:32 +00:00
|
|
|
func (s *Intention) prepareApplyDelete(ident structs.ACLIdentity, authz acl.Authorizer, entMeta *structs.EnterpriseMeta, args *structs.IntentionRequest) error {
|
2020-01-13 20:51:40 +00:00
|
|
|
// If this is not a create, then we have to verify the ID.
|
|
|
|
state := s.srv.fsm.State()
|
|
|
|
_, ixn, err := state.IntentionGet(nil, args.Intention.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Intention lookup failed: %v", err)
|
|
|
|
}
|
|
|
|
if ixn == nil {
|
|
|
|
return fmt.Errorf("Cannot delete non-existent intention: '%s'", args.Intention.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the ACL check that we have write to the old intention too,
|
|
|
|
// which must be true to perform any rename. This is the only ACL enforcement
|
|
|
|
// done for deletions and a secondary enforcement for updates.
|
|
|
|
if !ixn.CanWrite(authz) {
|
2020-01-27 19:54:32 +00:00
|
|
|
var accessorID string
|
|
|
|
if ident != nil {
|
|
|
|
accessorID = ident.ID()
|
|
|
|
}
|
|
|
|
// todo(kit) Migrate intention access denial logging over to audit logging when we implement it
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("Deletion operation on intention denied due to ACLs", "intention", args.Intention.ID, "accessorID", accessorID)
|
2020-01-13 20:51:40 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply creates or updates an intention in the data store.
|
|
|
|
func (s *Intention) Apply(
|
|
|
|
args *structs.IntentionRequest,
|
|
|
|
reply *string) error {
|
|
|
|
|
|
|
|
// Forward this request to the primary DC if we're a secondary that's replicating intentions.
|
|
|
|
if s.srv.intentionReplicationEnabled() {
|
|
|
|
args.Datacenter = s.srv.config.PrimaryDatacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := s.srv.forward("Intention.Apply", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"consul", "intention", "apply"}, time.Now())
|
|
|
|
defer metrics.MeasureSince([]string{"intention", "apply"}, time.Now())
|
|
|
|
|
|
|
|
// Always set a non-nil intention to avoid nil-access below
|
|
|
|
if args.Intention == nil {
|
|
|
|
args.Intention = &structs.Intention{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ACL token for the request for the checks below.
|
|
|
|
var entMeta structs.EnterpriseMeta
|
2020-01-27 19:54:32 +00:00
|
|
|
ident, authz, err := s.srv.ResolveTokenIdentityAndDefaultMeta(args.Token, &entMeta, nil)
|
2020-01-13 20:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch args.Op {
|
|
|
|
case structs.IntentionOpCreate:
|
2020-01-27 19:54:32 +00:00
|
|
|
if err := s.prepareApplyCreate(ident, authz, &entMeta, args); err != nil {
|
2020-01-13 20:51:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case structs.IntentionOpUpdate:
|
2020-01-27 19:54:32 +00:00
|
|
|
if err := s.prepareApplyUpdate(ident, authz, &entMeta, args); err != nil {
|
2020-01-13 20:51:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case structs.IntentionOpDelete:
|
2020-01-27 19:54:32 +00:00
|
|
|
if err := s.prepareApplyDelete(ident, authz, &entMeta, args); err != nil {
|
2020-01-13 20:51:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid Intention operation: %v", args.Op)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the reply which will have been filled in by one of the 3 preparedApply* funcs
|
|
|
|
*reply = args.Intention.ID
|
|
|
|
|
2018-02-28 18:28:07 +00:00
|
|
|
// Commit
|
|
|
|
resp, err := s.srv.raftApply(structs.IntentionRequestType, args)
|
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Error("Raft apply failed", "error", err)
|
2018-02-28 18:28:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-28 18:44:49 +00:00
|
|
|
// Get returns a single intention by ID.
|
|
|
|
func (s *Intention) Get(
|
|
|
|
args *structs.IntentionQueryRequest,
|
|
|
|
reply *structs.IndexedIntentions) error {
|
|
|
|
// Forward if necessary
|
|
|
|
if done, err := s.srv.forward("Intention.Get", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, ixn, err := state.IntentionGet(ws, args.IntentionID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ixn == nil {
|
2018-02-28 23:54:48 +00:00
|
|
|
return ErrIntentionNotFound
|
2018-02-28 18:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index = index
|
|
|
|
reply.Intentions = structs.Intentions{ixn}
|
|
|
|
|
2018-03-04 19:53:52 +00:00
|
|
|
// Filter
|
|
|
|
if err := s.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If ACLs prevented any responses, error
|
|
|
|
if len(reply.Intentions) == 0 {
|
2020-01-27 19:54:32 +00:00
|
|
|
accessorID := s.aclAccessorID(args.Token)
|
|
|
|
// todo(kit) Migrate intention access denial logging over to audit logging when we implement it
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("Request to get intention denied due to ACLs", "intention", args.IntentionID, "accessorID", accessorID)
|
2018-03-04 19:53:52 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
2018-02-28 18:44:49 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-02-28 18:28:07 +00:00
|
|
|
// List returns all the intentions.
|
2018-02-28 18:04:27 +00:00
|
|
|
func (s *Intention) List(
|
|
|
|
args *structs.DCSpecificRequest,
|
|
|
|
reply *structs.IndexedIntentions) error {
|
|
|
|
// Forward if necessary
|
|
|
|
if done, err := s.srv.forward("Intention.List", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-07 15:48:44 +00:00
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.Intentions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-28 18:04:27 +00:00
|
|
|
return s.srv.blockingQuery(
|
|
|
|
&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, ixns, err := state.Intentions(ws)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Intentions = index, ixns
|
2018-03-06 17:04:44 +00:00
|
|
|
if reply.Intentions == nil {
|
|
|
|
reply.Intentions = make(structs.Intentions, 0)
|
|
|
|
}
|
|
|
|
|
2020-04-07 15:48:44 +00:00
|
|
|
if err := s.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := filter.Execute(reply.Intentions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Intentions = raw.(structs.Intentions)
|
|
|
|
|
|
|
|
return nil
|
2018-02-28 18:04:27 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2018-03-02 21:40:03 +00:00
|
|
|
|
|
|
|
// Match returns the set of intentions that match the given source/destination.
|
|
|
|
func (s *Intention) Match(
|
|
|
|
args *structs.IntentionQueryRequest,
|
|
|
|
reply *structs.IndexedIntentionMatches) error {
|
|
|
|
// Forward if necessary
|
|
|
|
if done, err := s.srv.forward("Intention.Match", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-05 02:32:28 +00:00
|
|
|
// Get the ACL token for the request for the checks below.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := s.srv.ResolveToken(args.Token)
|
2018-03-05 02:32:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule != nil {
|
2020-01-13 20:51:40 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
// Go through each entry to ensure we have intention:read for the resource.
|
|
|
|
|
|
|
|
// TODO - should we do this instead of filtering the result set? This will only allow
|
|
|
|
// queries for which the token has intention:read permissions on the requested side
|
|
|
|
// of the service. Should it instead return all matches that it would be able to list.
|
|
|
|
// if so we should remove this and call filterACL instead. Based on how this is used
|
|
|
|
// its probably fine. If you have intention read on the source just do a source type
|
|
|
|
// matching, if you have it on the dest then perform a dest type match.
|
2018-03-05 02:32:28 +00:00
|
|
|
for _, entry := range args.Match.Entries {
|
2020-01-13 20:51:40 +00:00
|
|
|
entry.FillAuthzContext(&authzContext)
|
|
|
|
if prefix := entry.Name; prefix != "" && rule.IntentionRead(prefix, &authzContext) != acl.Allow {
|
2020-01-27 19:54:32 +00:00
|
|
|
accessorID := s.aclAccessorID(args.Token)
|
|
|
|
// todo(kit) Migrate intention access denial logging over to audit logging when we implement it
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("Operation on intention prefix denied due to ACLs", "prefix", prefix, "accessorID", accessorID)
|
2018-03-05 02:32:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 21:40:03 +00:00
|
|
|
|
|
|
|
return s.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, matches, err := state.IntentionMatch(ws, args.Match)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index = index
|
|
|
|
reply.Matches = matches
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2018-05-11 05:35:47 +00:00
|
|
|
|
2018-05-11 16:19:22 +00:00
|
|
|
// Check tests a source/destination and returns whether it would be allowed
|
2018-05-11 05:35:47 +00:00
|
|
|
// or denied based on the current ACL configuration.
|
2018-05-11 05:37:02 +00:00
|
|
|
//
|
|
|
|
// Note: Whenever the logic for this method is changed, you should take
|
|
|
|
// a look at the agent authorize endpoint (agent/agent_endpoint.go) since
|
|
|
|
// the logic there is similar.
|
2018-05-11 16:19:22 +00:00
|
|
|
func (s *Intention) Check(
|
2018-05-11 05:35:47 +00:00
|
|
|
args *structs.IntentionQueryRequest,
|
2018-05-11 16:19:22 +00:00
|
|
|
reply *structs.IntentionQueryCheckResponse) error {
|
2018-05-11 05:38:13 +00:00
|
|
|
// Forward maybe
|
2018-05-11 16:19:22 +00:00
|
|
|
if done, err := s.srv.forward("Intention.Check", args, args, reply); done {
|
2018-05-11 05:38:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-11 05:35:47 +00:00
|
|
|
// Get the test args, and defensively guard against nil
|
2018-05-11 16:19:22 +00:00
|
|
|
query := args.Check
|
2018-05-11 05:35:47 +00:00
|
|
|
if query == nil {
|
2018-05-11 16:19:22 +00:00
|
|
|
return errors.New("Check must be specified on args")
|
2018-05-11 05:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the URI
|
|
|
|
var uri connect.CertURI
|
|
|
|
switch query.SourceType {
|
|
|
|
case structs.IntentionSourceConsul:
|
|
|
|
uri = &connect.SpiffeIDService{
|
|
|
|
Namespace: query.SourceNS,
|
|
|
|
Service: query.SourceName,
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported SourceType: %q", query.SourceType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ACL token for the request for the checks below.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := s.srv.ResolveToken(args.Token)
|
2018-05-11 05:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-19 04:03:10 +00:00
|
|
|
// Perform the ACL check. For Check we only require ServiceRead and
|
|
|
|
// NOT IntentionRead because the Check API only returns pass/fail and
|
2020-01-13 20:51:40 +00:00
|
|
|
// returns no other information about the intentions used. We could check
|
|
|
|
// both the source and dest side but only checking dest also has the nice
|
|
|
|
// benefit of only returning a passing status if the token would be able
|
|
|
|
// to discover the dest service and connect to it.
|
2018-05-11 05:35:47 +00:00
|
|
|
if prefix, ok := query.GetACLPrefix(); ok {
|
2020-01-13 20:51:40 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
query.FillAuthzContext(&authzContext)
|
|
|
|
if rule != nil && rule.ServiceRead(prefix, &authzContext) != acl.Allow {
|
2020-01-27 19:54:32 +00:00
|
|
|
accessorID := s.aclAccessorID(args.Token)
|
|
|
|
// todo(kit) Migrate intention access denial logging over to audit logging when we implement it
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("test on intention denied due to ACLs", "prefix", prefix, "accessorID", accessorID)
|
2018-05-11 05:35:47 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the matches for this destination
|
|
|
|
state := s.srv.fsm.State()
|
|
|
|
_, matches, err := state.IntentionMatch(nil, &structs.IntentionQueryMatch{
|
|
|
|
Type: structs.IntentionMatchDestination,
|
|
|
|
Entries: []structs.IntentionMatchEntry{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2018-05-11 05:35:47 +00:00
|
|
|
Namespace: query.DestinationNS,
|
|
|
|
Name: query.DestinationName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(matches) != 1 {
|
|
|
|
// This should never happen since the documented behavior of the
|
|
|
|
// Match call is that it'll always return exactly the number of results
|
|
|
|
// as entries passed in. But we guard against misbehavior.
|
|
|
|
return errors.New("internal error loading matches")
|
|
|
|
}
|
|
|
|
|
2018-05-11 16:19:22 +00:00
|
|
|
// Check the authorization for each match
|
2018-05-11 05:35:47 +00:00
|
|
|
for _, ixn := range matches[0] {
|
|
|
|
if auth, ok := uri.Authorize(ixn); ok {
|
|
|
|
reply.Allowed = auth
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No match, we need to determine the default behavior. We do this by
|
|
|
|
// specifying the anonymous token token, which will get that behavior.
|
|
|
|
// The default behavior if ACLs are disabled is to allow connections
|
|
|
|
// to mimic the behavior of Consul itself: everything is allowed if
|
|
|
|
// ACLs are disabled.
|
|
|
|
//
|
|
|
|
// NOTE(mitchellh): This is the same behavior as the agent authorize
|
|
|
|
// endpoint. If this behavior is incorrect, we should also change it there
|
|
|
|
// which is much more important.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err = s.srv.ResolveToken("")
|
2018-05-11 05:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Allowed = true
|
|
|
|
if rule != nil {
|
2019-10-15 20:58:50 +00:00
|
|
|
reply.Allowed = rule.IntentionDefaultAllow(nil) == acl.Allow
|
2018-05-11 05:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-27 19:54:32 +00:00
|
|
|
|
|
|
|
// aclAccessorID is used to convert an ACLToken's secretID to its accessorID for non-
|
|
|
|
// critical purposes, such as logging. Therefore we interpret all errors as empty-string
|
|
|
|
// so we can safely log it without handling non-critical errors at the usage site.
|
|
|
|
func (s *Intention) aclAccessorID(secretID string) string {
|
|
|
|
_, ident, err := s.srv.ResolveIdentityFromToken(secretID)
|
2020-01-29 17:16:08 +00:00
|
|
|
if acl.IsErrNotFound(err) {
|
|
|
|
return ""
|
|
|
|
}
|
2020-01-27 19:54:32 +00:00
|
|
|
if err != nil {
|
2020-01-29 17:16:08 +00:00
|
|
|
s.logger.Debug("non-critical error resolving acl token accessor for logging", "error", err)
|
2020-01-27 19:54:32 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if ident == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ident.ID()
|
|
|
|
}
|