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 {
|
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 {
|
2021-08-25 18:43:11 +00:00
|
|
|
// TODO(partitions) fix this to include namespace and partition
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
func (a *Agent) vetServiceUpdateWithAuthorizer(authz acl.Authorizer, serviceID structs.ServiceID) error {
|
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 {
|
2021-07-15 21:17:49 +00:00
|
|
|
return NotFoundError{Reason: fmt.Sprintf("Unknown service %q", serviceID)}
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
func (a *Agent) vetCheckRegisterWithAuthorizer(authz acl.Authorizer, check *structs.HealthCheck) error {
|
2021-08-19 20:09:42 +00:00
|
|
|
// TODO(partitions)
|
|
|
|
|
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 {
|
2021-08-25 18:43:11 +00:00
|
|
|
return acl.PermissionDenied("Missing node:write on %s", structs.NodeNameString(a.config.NodeName, a.agentEnterpriseMeta()))
|
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 {
|
2021-08-25 18:43:11 +00:00
|
|
|
return acl.PermissionDenied("Missing node:write on %s", structs.NodeNameString(a.config.NodeName, a.agentEnterpriseMeta()))
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
func (a *Agent) vetCheckUpdateWithAuthorizer(authz acl.Authorizer, checkID structs.CheckID) error {
|
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 {
|
2021-08-25 18:43:11 +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 {
|
2021-08-25 18:43:11 +00:00
|
|
|
return acl.PermissionDenied("Missing node:write on %s", structs.NodeNameString(a.config.NodeName, a.agentEnterpriseMeta()))
|
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-08-04 21:51:19 +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:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2021-08-19 20:09:42 +00:00
|
|
|
a.agentEnterpriseMeta().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
|
2021-08-04 21:51:19 +00:00
|
|
|
if authz.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
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
func (a *Agent) filterServicesWithAuthorizer(authz acl.Authorizer, services *map[structs.ServiceID]*structs.NodeService) error {
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
func (a *Agent) filterChecksWithAuthorizer(authz acl.Authorizer, checks *map[structs.CheckID]*structs.HealthCheck) error {
|
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 {
|
2021-08-25 18:43:11 +00:00
|
|
|
check.FillAuthzContext(&authzContext)
|
2016-12-14 22:16:46 +00:00
|
|
|
if len(check.ServiceName) > 0 {
|
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-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
|
|
|
|
}
|