2019-04-26 17:49:28 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/go-bexpr"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2019-04-26 17:49:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/authmethod"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
|
2020-05-12 01:59:29 +00:00
|
|
|
// register these as a builtin auth method
|
2022-03-31 15:18:48 +00:00
|
|
|
_ "github.com/hashicorp/consul/agent/consul/authmethod/awsauth"
|
2019-04-26 17:49:28 +00:00
|
|
|
_ "github.com/hashicorp/consul/agent/consul/authmethod/kubeauth"
|
2020-05-12 01:59:29 +00:00
|
|
|
_ "github.com/hashicorp/consul/agent/consul/authmethod/ssoauth"
|
2019-04-26 17:49:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type authMethodValidatorEntry struct {
|
|
|
|
Validator authmethod.Validator
|
|
|
|
ModifyIndex uint64 // the raft index when this last changed
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadAuthMethodValidator returns an authmethod.Validator for the given auth
|
|
|
|
// method configuration. If the cache is up to date as-of the provided index
|
|
|
|
// then the cached version is returned, otherwise a new validator is created
|
|
|
|
// and cached.
|
|
|
|
func (s *Server) loadAuthMethodValidator(idx uint64, method *structs.ACLAuthMethod) (authmethod.Validator, error) {
|
2019-10-24 18:38:09 +00:00
|
|
|
if prevIdx, v, ok := s.aclAuthMethodValidators.GetValidator(method); ok && idx <= prevIdx {
|
2019-04-26 17:49:28 +00:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 20:55:26 +00:00
|
|
|
v, err := authmethod.NewValidator(s.logger, method)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("auth method validator for %q could not be initialized: %v", method.Name, err)
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
v = s.aclAuthMethodValidators.PutValidatorIfNewer(method, v, idx)
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 16:54:27 +00:00
|
|
|
type aclBindings struct {
|
|
|
|
roles []structs.ACLTokenRoleLink
|
|
|
|
serviceIdentities []*structs.ACLServiceIdentity
|
|
|
|
nodeIdentities []*structs.ACLNodeIdentity
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// evaluateRoleBindings evaluates all current binding rules associated with the
|
|
|
|
// given auth method against the verified data returned from the authentication
|
|
|
|
// process.
|
|
|
|
//
|
|
|
|
// A list of role links and service identities are returned.
|
|
|
|
func (s *Server) evaluateRoleBindings(
|
|
|
|
validator authmethod.Validator,
|
2020-05-01 22:35:28 +00:00
|
|
|
verifiedIdentity *authmethod.Identity,
|
2022-04-05 21:10:06 +00:00
|
|
|
methodMeta *acl.EnterpriseMeta,
|
|
|
|
targetMeta *acl.EnterpriseMeta,
|
2020-06-16 16:54:27 +00:00
|
|
|
) (*aclBindings, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
// Only fetch rules that are relevant for this method.
|
2020-01-14 15:09:29 +00:00
|
|
|
_, rules, err := s.fsm.State().ACLBindingRuleList(nil, validator.Name(), methodMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
2020-06-16 16:54:27 +00:00
|
|
|
return nil, err
|
2019-04-26 17:49:28 +00:00
|
|
|
} else if len(rules) == 0 {
|
2020-06-16 16:54:27 +00:00
|
|
|
return nil, nil
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find all binding rules that match the provided fields.
|
|
|
|
var matchingRules []*structs.ACLBindingRule
|
|
|
|
for _, rule := range rules {
|
2020-05-01 22:35:28 +00:00
|
|
|
if doesSelectorMatch(rule.Selector, verifiedIdentity.SelectableFields) {
|
2019-04-26 17:49:28 +00:00
|
|
|
matchingRules = append(matchingRules, rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(matchingRules) == 0 {
|
2020-06-16 16:54:27 +00:00
|
|
|
return nil, nil
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For all matching rules compute the attributes of a token.
|
2020-06-16 16:54:27 +00:00
|
|
|
var bindings aclBindings
|
2019-04-26 17:49:28 +00:00
|
|
|
for _, rule := range matchingRules {
|
2020-05-01 22:35:28 +00:00
|
|
|
bindName, valid, err := computeBindingRuleBindName(rule.BindType, rule.BindName, verifiedIdentity.ProjectedVars)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
2020-06-16 16:54:27 +00:00
|
|
|
return nil, fmt.Errorf("cannot compute %q bind name for bind target: %v", rule.BindType, err)
|
2019-04-26 17:49:28 +00:00
|
|
|
} else if !valid {
|
2020-06-16 16:54:27 +00:00
|
|
|
return nil, fmt.Errorf("computed %q bind name for bind target is invalid: %q", rule.BindType, bindName)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch rule.BindType {
|
|
|
|
case structs.BindingRuleBindTypeService:
|
2020-06-16 16:54:27 +00:00
|
|
|
bindings.serviceIdentities = append(bindings.serviceIdentities, &structs.ACLServiceIdentity{
|
2019-04-26 17:49:28 +00:00
|
|
|
ServiceName: bindName,
|
|
|
|
})
|
|
|
|
|
2020-06-16 16:54:27 +00:00
|
|
|
case structs.BindingRuleBindTypeNode:
|
|
|
|
bindings.nodeIdentities = append(bindings.nodeIdentities, &structs.ACLNodeIdentity{
|
|
|
|
NodeName: bindName,
|
|
|
|
Datacenter: s.config.Datacenter,
|
|
|
|
})
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
case structs.BindingRuleBindTypeRole:
|
2020-01-14 15:09:29 +00:00
|
|
|
_, role, err := s.fsm.State().ACLRoleGetByName(nil, bindName, targetMeta)
|
2019-05-03 19:22:44 +00:00
|
|
|
if err != nil {
|
2020-06-16 16:54:27 +00:00
|
|
|
return nil, err
|
2019-05-03 19:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if role != nil {
|
2020-06-16 16:54:27 +00:00
|
|
|
bindings.roles = append(bindings.roles, structs.ACLTokenRoleLink{
|
2019-05-03 19:22:44 +00:00
|
|
|
ID: role.ID,
|
|
|
|
})
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// skip unknown bind type; don't grant privileges
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 16:54:27 +00:00
|
|
|
return &bindings, nil
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 22:35:28 +00:00
|
|
|
// doesSelectorMatch checks that a single selector matches the provided vars.
|
|
|
|
func doesSelectorMatch(selector string, selectableVars interface{}) bool {
|
|
|
|
if selector == "" {
|
2019-04-26 17:49:28 +00:00
|
|
|
return true // catch-all
|
|
|
|
}
|
|
|
|
|
2020-05-01 22:35:28 +00:00
|
|
|
eval, err := bexpr.CreateEvaluatorForType(selector, nil, selectableVars)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return false // fails to match if selector is invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := eval.Evaluate(selectableVars)
|
|
|
|
if err != nil {
|
|
|
|
return false // fails to match if evaluation fails
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|