2016-12-14 07:21:14 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-11-17 22:10:21 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
|
2016-12-14 07:21:14 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2016-12-14 07:21:14 +00:00
|
|
|
)
|
|
|
|
|
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-11-17 22:10:21 +00:00
|
|
|
ident, err := a.delegate.ResolveTokenToIdentity(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()
|
|
|
|
}
|
|
|
|
|
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.
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
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 {
|
2020-09-22 18:36:07 +00:00
|
|
|
serviceName := service.CompoundServiceName()
|
|
|
|
return acl.PermissionDenied("Missing service:write on %s", serviceName.String())
|
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 {
|
2020-09-22 18:36:07 +00:00
|
|
|
serviceName := service.CompoundServiceName()
|
|
|
|
return acl.PermissionDenied("Missing service:write on %s", serviceName.String())
|
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 {
|
2020-09-22 18:36:07 +00:00
|
|
|
return acl.PermissionDenied("Missing service:write on %s", service.Proxy.DestinationServiceName)
|
2018-09-27 13:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
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 {
|
2020-09-22 18:36:07 +00:00
|
|
|
serviceName := existing.CompoundServiceName()
|
|
|
|
return acl.PermissionDenied("Missing service:write on %s", serviceName.String())
|
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.
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
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 {
|
2020-06-10 20:47:35 +00:00
|
|
|
return acl.PermissionDenied("Missing service:write on %v", structs.ServiceIDString(check.ServiceName, &check.EnterpriseMeta))
|
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 {
|
2020-06-10 20:47:35 +00:00
|
|
|
return acl.PermissionDenied("Missing node:write on %s", a.config.NodeName)
|
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 {
|
2020-06-10 20:47:35 +00:00
|
|
|
return acl.PermissionDenied("Missing service:write on %s", structs.ServiceIDString(existing.ServiceName, &existing.EnterpriseMeta))
|
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 {
|
2020-06-10 20:47:35 +00:00
|
|
|
return acl.PermissionDenied("Missing node:write on %s", a.config.NodeName)
|
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.
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
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 {
|
2020-09-22 18:36:07 +00:00
|
|
|
return acl.PermissionDenied("Missing service:write on %s", existing.ServiceName)
|
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 {
|
2020-09-22 18:36:07 +00:00
|
|
|
return acl.PermissionDenied("Missing node:write on %s", a.config.NodeName)
|
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.
|
2021-04-14 16:39:35 +00:00
|
|
|
rule, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
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
|
2021-07-22 18:20:45 +00:00
|
|
|
structs.DefaultEnterpriseMetaInDefaultPartition().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.
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
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.
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
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 {
|
2021-07-22 18:20:45 +00:00
|
|
|
structs.DefaultEnterpriseMetaInDefaultPartition().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
|
|
|
|
}
|