2016-12-14 07:21:14 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2016-12-14 22:16:46 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2016-12-14 07:21:14 +00:00
|
|
|
)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// resolveToken is the primary interface used by ACL-checkers in the agent
|
|
|
|
// endpoints, which is the one place where we do some ACL enforcement on
|
|
|
|
// clients. Some of the enforcement is normative (e.g. self and monitor)
|
|
|
|
// and some is informative (e.g. catalog and health).
|
|
|
|
func (a *Agent) resolveToken(id string) (acl.Authorizer, error) {
|
2019-12-18 18:46:53 +00:00
|
|
|
return a.resolveTokenAndDefaultMeta(id, nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolveTokenAndDefaultMeta is used to resolve an ACL token secret to an
|
|
|
|
// acl.Authorizer and to default any enterprise specific metadata for the request.
|
|
|
|
// The defaulted metadata is then used to fill in an acl.AuthorizationContext.
|
|
|
|
func (a *Agent) resolveTokenAndDefaultMeta(id string, entMeta *structs.EnterpriseMeta, authzContext *acl.AuthorizerContext) (acl.Authorizer, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
// ACLs are disabled
|
|
|
|
if !a.delegate.ACLsEnabled() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if acl.RootAuthorizer(id) != nil {
|
|
|
|
return nil, acl.ErrRootDenied
|
|
|
|
}
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if a.tokens.IsAgentMasterToken(id) {
|
|
|
|
return a.aclMasterAuthorizer, nil
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
2019-12-18 18:46:53 +00:00
|
|
|
|
|
|
|
return a.delegate.ResolveTokenAndDefaultMeta(id, entMeta, authzContext)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2020-01-27 19:54:32 +00:00
|
|
|
// resolveIdentityFromToken is used to resolve an ACLToken's secretID to a structs.ACLIdentity
|
2020-05-13 17:00:08 +00:00
|
|
|
func (a *Agent) resolveIdentityFromToken(secretID string) (structs.ACLIdentity, error) {
|
|
|
|
return a.delegate.ResolveTokenToIdentity(secretID)
|
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 (a *Agent) aclAccessorID(secretID string) string {
|
2020-05-13 17:00:08 +00:00
|
|
|
ident, err := a.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
|
|
|
a.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()
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (a *Agent) initializeACLs() error {
|
2017-07-26 18:03:43 +00:00
|
|
|
// Build a policy for the agent master token.
|
2018-10-19 16:04:07 +00:00
|
|
|
// The builtin agent master policy allows reading any node information
|
|
|
|
// and allows writes to the agent with the node name of the running agent
|
|
|
|
// only. This used to allow a prefix match on agent names but that seems
|
|
|
|
// entirely unnecessary so it is now using an exact match.
|
2017-07-26 18:03:43 +00:00
|
|
|
policy := &acl.Policy{
|
2019-10-15 20:58:50 +00:00
|
|
|
PolicyRules: acl.PolicyRules{
|
|
|
|
Agents: []*acl.AgentRule{
|
|
|
|
&acl.AgentRule{
|
|
|
|
Node: a.config.NodeName,
|
|
|
|
Policy: acl.PolicyWrite,
|
|
|
|
},
|
2016-12-14 07:21:14 +00:00
|
|
|
},
|
2019-10-15 20:58:50 +00:00
|
|
|
NodePrefixes: []*acl.NodeRule{
|
|
|
|
&acl.NodeRule{
|
|
|
|
Name: "",
|
|
|
|
Policy: acl.PolicyRead,
|
|
|
|
},
|
2017-07-17 03:08:26 +00:00
|
|
|
},
|
2017-07-26 18:03:43 +00:00
|
|
|
},
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
master, err := acl.NewPolicyAuthorizerWithDefaults(acl.DenyAll(), []*acl.Policy{policy}, nil)
|
2017-07-26 18:03:43 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
a.aclMasterAuthorizer = master
|
|
|
|
return nil
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
// vetServiceRegister makes sure the service registration action is allowed by
|
|
|
|
// the given token.
|
|
|
|
func (a *Agent) vetServiceRegister(token string, service *structs.NodeService) error {
|
|
|
|
// Resolve the token and bail if ACLs aren't enabled.
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.resolveToken(token)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-18 18:46:53 +00:00
|
|
|
|
|
|
|
return a.vetServiceRegisterWithAuthorizer(authz, service)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) vetServiceRegisterWithAuthorizer(authz acl.Authorizer, service *structs.NodeService) error {
|
|
|
|
if authz == nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-12-10 02:26:41 +00:00
|
|
|
service.FillAuthzContext(&authzContext)
|
2016-12-14 22:16:46 +00:00
|
|
|
// Vet the service itself.
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.ServiceWrite(service.Service, &authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vet any service that might be getting overwritten.
|
2019-12-10 02:26:41 +00:00
|
|
|
if existing := a.State.Service(service.CompoundServiceID()); existing != nil {
|
|
|
|
existing.FillAuthzContext(&authzContext)
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.ServiceWrite(existing.Service, &authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 13:33:12 +00:00
|
|
|
// If the service is a proxy, ensure that it has write on the destination too
|
|
|
|
// since it can be discovered as an instance of that service.
|
|
|
|
if service.Kind == structs.ServiceKindConnectProxy {
|
2019-12-10 02:26:41 +00:00
|
|
|
service.FillAuthzContext(&authzContext)
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.ServiceWrite(service.Proxy.DestinationServiceName, &authzContext) != acl.Allow {
|
2018-09-27 13:33:12 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// vetServiceUpdate makes sure the service update action is allowed by the given
|
|
|
|
// token.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (a *Agent) vetServiceUpdate(token string, serviceID structs.ServiceID) error {
|
2016-12-14 22:16:46 +00:00
|
|
|
// Resolve the token and bail if ACLs aren't enabled.
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.resolveToken(token)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-18 18:46:53 +00:00
|
|
|
|
|
|
|
return a.vetServiceUpdateWithAuthorizer(authz, serviceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) vetServiceUpdateWithAuthorizer(authz acl.Authorizer, serviceID structs.ServiceID) error {
|
|
|
|
if authz == nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Vet any changes based on the existing services's info.
|
2019-12-10 02:26:41 +00:00
|
|
|
if existing := a.State.Service(serviceID); existing != nil {
|
|
|
|
existing.FillAuthzContext(&authzContext)
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.ServiceWrite(existing.Service, &authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Unknown service %q", serviceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// vetCheckRegister makes sure the check registration action is allowed by the
|
|
|
|
// given token.
|
|
|
|
func (a *Agent) vetCheckRegister(token string, check *structs.HealthCheck) error {
|
|
|
|
// Resolve the token and bail if ACLs aren't enabled.
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.resolveToken(token)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-18 18:46:53 +00:00
|
|
|
|
|
|
|
return a.vetCheckRegisterWithAuthorizer(authz, check)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) vetCheckRegisterWithAuthorizer(authz acl.Authorizer, check *structs.HealthCheck) error {
|
|
|
|
if authz == nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-12-10 02:26:41 +00:00
|
|
|
check.FillAuthzContext(&authzContext)
|
2016-12-14 22:16:46 +00:00
|
|
|
// Vet the check itself.
|
|
|
|
if len(check.ServiceName) > 0 {
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.ServiceWrite(check.ServiceName, &authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.NodeWrite(a.config.NodeName, &authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vet any check that might be getting overwritten.
|
2019-12-10 02:26:41 +00:00
|
|
|
if existing := a.State.Check(check.CompoundCheckID()); existing != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
if len(existing.ServiceName) > 0 {
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.ServiceWrite(existing.ServiceName, &authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.NodeWrite(a.config.NodeName, &authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// vetCheckUpdate makes sure that a check update is allowed by the given token.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (a *Agent) vetCheckUpdate(token string, checkID structs.CheckID) error {
|
2016-12-14 22:16:46 +00:00
|
|
|
// Resolve the token and bail if ACLs aren't enabled.
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.resolveToken(token)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-18 18:46:53 +00:00
|
|
|
|
|
|
|
return a.vetCheckUpdateWithAuthorizer(authz, checkID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) vetCheckUpdateWithAuthorizer(authz acl.Authorizer, checkID structs.CheckID) error {
|
|
|
|
if authz == nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-12-10 02:26:41 +00:00
|
|
|
checkID.FillAuthzContext(&authzContext)
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Vet any changes based on the existing check's info.
|
2019-12-10 02:26:41 +00:00
|
|
|
if existing := a.State.Check(checkID); existing != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
if len(existing.ServiceName) > 0 {
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.ServiceWrite(existing.ServiceName, &authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.NodeWrite(a.config.NodeName, &authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-10 02:26:41 +00:00
|
|
|
return fmt.Errorf("Unknown check %q", checkID.String())
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterMembers redacts members that the token doesn't have access to.
|
|
|
|
func (a *Agent) filterMembers(token string, members *[]serf.Member) error {
|
|
|
|
// Resolve the token and bail if ACLs aren't enabled.
|
2017-08-23 14:52:48 +00:00
|
|
|
rule, err := a.resolveToken(token)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-23 14:52:48 +00:00
|
|
|
if rule == nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-12-10 02:26:41 +00:00
|
|
|
structs.DefaultEnterpriseMeta().FillAuthzContext(&authzContext)
|
2016-12-14 22:16:46 +00:00
|
|
|
// Filter out members based on the node policy.
|
|
|
|
m := *members
|
|
|
|
for i := 0; i < len(m); i++ {
|
|
|
|
node := m[i].Name
|
2019-12-10 02:26:41 +00:00
|
|
|
if rule.NodeRead(node, &authzContext) == acl.Allow {
|
2016-12-14 22:16:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-27 19:54:32 +00:00
|
|
|
accessorID := a.aclAccessorID(token)
|
2020-01-28 23:50:41 +00:00
|
|
|
a.logger.Debug("dropping node from result due to ACLs", "node", node, "accessorID", accessorID)
|
2016-12-14 22:16:46 +00:00
|
|
|
m = append(m[:i], m[i+1:]...)
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
*members = m
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterServices redacts services that the token doesn't have access to.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (a *Agent) filterServices(token string, services *map[structs.ServiceID]*structs.NodeService) error {
|
2016-12-14 22:16:46 +00:00
|
|
|
// Resolve the token and bail if ACLs aren't enabled.
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.resolveToken(token)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-18 18:46:53 +00:00
|
|
|
|
|
|
|
return a.filterServicesWithAuthorizer(authz, services)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) filterServicesWithAuthorizer(authz acl.Authorizer, services *map[structs.ServiceID]*structs.NodeService) error {
|
|
|
|
if authz == nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-18 18:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2016-12-14 22:16:46 +00:00
|
|
|
// Filter out services based on the service policy.
|
|
|
|
for id, service := range *services {
|
2019-12-10 02:26:41 +00:00
|
|
|
service.FillAuthzContext(&authzContext)
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.ServiceRead(service.Service, &authzContext) == acl.Allow {
|
2016-12-14 22:16:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
a.logger.Debug("dropping service from result due to ACLs", "service", id.String())
|
2016-12-14 22:16:46 +00:00
|
|
|
delete(*services, id)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterChecks redacts checks that the token doesn't have access to.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (a *Agent) filterChecks(token string, checks *map[structs.CheckID]*structs.HealthCheck) error {
|
2016-12-14 22:16:46 +00:00
|
|
|
// Resolve the token and bail if ACLs aren't enabled.
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.resolveToken(token)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-18 18:46:53 +00:00
|
|
|
|
|
|
|
return a.filterChecksWithAuthorizer(authz, checks)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) filterChecksWithAuthorizer(authz acl.Authorizer, checks *map[structs.CheckID]*structs.HealthCheck) error {
|
|
|
|
if authz == nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2016-12-14 22:16:46 +00:00
|
|
|
// Filter out checks based on the node or service policy.
|
|
|
|
for id, check := range *checks {
|
|
|
|
if len(check.ServiceName) > 0 {
|
2019-12-10 02:26:41 +00:00
|
|
|
check.FillAuthzContext(&authzContext)
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.ServiceRead(check.ServiceName, &authzContext) == acl.Allow {
|
2016-12-14 22:16:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-10 02:26:41 +00:00
|
|
|
structs.DefaultEnterpriseMeta().FillAuthzContext(&authzContext)
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz.NodeRead(a.config.NodeName, &authzContext) == acl.Allow {
|
2016-12-14 22:16:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
a.logger.Debug("dropping check from result due to ACLs", "check", id.String())
|
2016-12-14 22:16:46 +00:00
|
|
|
delete(*checks, id)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|