2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
package authmethod
|
|
|
|
|
|
|
|
import (
|
2020-05-01 22:35:28 +00:00
|
|
|
"context"
|
2019-04-26 17:49:28 +00:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
|
2020-05-01 20:55:26 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-04-26 17:49:28 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2022-04-05 21:10:06 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-04-26 17:49:28 +00:00
|
|
|
)
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
type Cache interface {
|
|
|
|
// GetValidator retrieves the Validator from the cache.
|
|
|
|
// It returns the modify index of struct that the validator was created from,
|
|
|
|
// the validator and a boolean indicating whether the value was found
|
|
|
|
GetValidator(method *structs.ACLAuthMethod) (uint64, Validator, bool)
|
|
|
|
|
|
|
|
// PutValidatorIfNewer inserts a new validator into the cache if the index is greater
|
|
|
|
// than the modify index of any existing entry in the cache. This method will return
|
|
|
|
// the newest validator which may or may not be the one from the method parameter
|
|
|
|
PutValidatorIfNewer(method *structs.ACLAuthMethod, validator Validator, idx uint64) Validator
|
|
|
|
|
|
|
|
// Purge removes all cached validators
|
|
|
|
Purge()
|
|
|
|
}
|
|
|
|
|
2020-05-01 20:55:26 +00:00
|
|
|
type ValidatorFactory func(logger hclog.Logger, method *structs.ACLAuthMethod) (Validator, error)
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
type Validator interface {
|
|
|
|
// Name returns the name of the auth method backing this validator.
|
|
|
|
Name() string
|
|
|
|
|
2020-05-01 22:35:28 +00:00
|
|
|
// NewIdentity creates a blank identity populated with empty values.
|
|
|
|
NewIdentity() *Identity
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// ValidateLogin takes raw user-provided auth method metadata and ensures
|
2021-07-02 16:18:46 +00:00
|
|
|
// it is reasonable, provably correct, and currently valid. Relevant identifying
|
2019-04-26 17:49:28 +00:00
|
|
|
// data is extracted and returned for immediate use by the role binding
|
|
|
|
// process.
|
|
|
|
//
|
|
|
|
// Depending upon the method, it may make sense to use these calls to
|
|
|
|
// continue to extend the life of the underlying token.
|
|
|
|
//
|
|
|
|
// Returns auth method specific metadata suitable for the Role Binding
|
2020-01-14 15:09:29 +00:00
|
|
|
// process as well as the desired enterprise meta for the token to be
|
|
|
|
// created.
|
2020-05-01 22:35:28 +00:00
|
|
|
ValidateLogin(ctx context.Context, loginToken string) (*Identity, error)
|
|
|
|
|
|
|
|
// Stop should be called to cease any background activity and free up
|
|
|
|
// resources.
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Identity struct {
|
|
|
|
// SelectableFields is the format of this Identity suitable for selection
|
|
|
|
// with a binding rule.
|
|
|
|
SelectableFields interface{}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
2020-05-01 22:35:28 +00:00
|
|
|
// ProjectedVars is the format of this Identity suitable for interpolation
|
|
|
|
// in a bind name within a binding rule.
|
|
|
|
ProjectedVars map[string]string
|
2019-04-26 17:49:28 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
*acl.EnterpriseMeta
|
2020-05-01 22:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectedVarNames returns just the keyspace of the ProjectedVars map.
|
|
|
|
func (i *Identity) ProjectedVarNames() []string {
|
|
|
|
v := make([]string, 0, len(i.ProjectedVars))
|
2020-06-16 17:19:31 +00:00
|
|
|
for k := range i.ProjectedVars {
|
2020-05-01 22:35:28 +00:00
|
|
|
v = append(v, k)
|
|
|
|
}
|
|
|
|
return v
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
typesMu sync.RWMutex
|
|
|
|
types = make(map[string]ValidatorFactory)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Register makes an auth method with the given type available for use. If
|
|
|
|
// Register is called twice with the same name or if validator is nil, it
|
|
|
|
// panics.
|
|
|
|
func Register(name string, factory ValidatorFactory) {
|
|
|
|
typesMu.Lock()
|
|
|
|
defer typesMu.Unlock()
|
|
|
|
if factory == nil {
|
|
|
|
panic("authmethod: Register factory is nil for type " + name)
|
|
|
|
}
|
|
|
|
if _, dup := types[name]; dup {
|
|
|
|
panic("authmethod: Register called twice for type " + name)
|
|
|
|
}
|
|
|
|
types[name] = factory
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsRegisteredType(typeName string) bool {
|
|
|
|
typesMu.RLock()
|
|
|
|
_, ok := types[typeName]
|
|
|
|
typesMu.RUnlock()
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
type authMethodValidatorEntry struct {
|
|
|
|
Validator Validator
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// authMethodCache is an non-thread-safe cache that maps ACLAuthMethods to their Validators
|
|
|
|
type authMethodCache struct {
|
|
|
|
entries map[string]*authMethodValidatorEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCache() Cache {
|
|
|
|
c := &authMethodCache{}
|
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *authMethodCache) init() {
|
|
|
|
c.Purge()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *authMethodCache) GetValidator(method *structs.ACLAuthMethod) (uint64, Validator, bool) {
|
|
|
|
entry, ok := c.entries[method.Name]
|
|
|
|
if ok {
|
|
|
|
return entry.ModifyIndex, entry.Validator, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *authMethodCache) PutValidatorIfNewer(method *structs.ACLAuthMethod, validator Validator, idx uint64) Validator {
|
|
|
|
prev, ok := c.entries[method.Name]
|
|
|
|
if ok {
|
|
|
|
if prev.ModifyIndex >= idx {
|
|
|
|
return prev.Validator
|
|
|
|
}
|
2020-05-01 22:35:28 +00:00
|
|
|
prev.Validator.Stop()
|
2019-10-24 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.entries[method.Name] = &authMethodValidatorEntry{
|
|
|
|
Validator: validator,
|
|
|
|
ModifyIndex: idx,
|
|
|
|
}
|
|
|
|
return validator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *authMethodCache) Purge() {
|
2020-05-01 22:35:28 +00:00
|
|
|
for _, entry := range c.entries {
|
|
|
|
entry.Validator.Stop()
|
|
|
|
}
|
2019-10-24 18:38:09 +00:00
|
|
|
c.entries = make(map[string]*authMethodValidatorEntry)
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// NewValidator instantiates a new Validator for the given auth method
|
|
|
|
// configuration. If no auth method is registered with the provided type an
|
|
|
|
// error is returned.
|
2020-05-01 20:55:26 +00:00
|
|
|
func NewValidator(logger hclog.Logger, method *structs.ACLAuthMethod) (Validator, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
typesMu.RLock()
|
|
|
|
factory, ok := types[method.Type]
|
|
|
|
typesMu.RUnlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no auth method registered with type: %s", method.Type)
|
|
|
|
}
|
|
|
|
|
2020-05-01 20:55:26 +00:00
|
|
|
logger = logger.Named("authmethod").With("type", method.Type, "name", method.Name)
|
|
|
|
|
|
|
|
return factory(logger, method)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Types returns a sorted list of the names of the registered types.
|
|
|
|
func Types() []string {
|
|
|
|
typesMu.RLock()
|
|
|
|
defer typesMu.RUnlock()
|
|
|
|
var list []string
|
|
|
|
for name := range types {
|
|
|
|
list = append(list, name)
|
|
|
|
}
|
|
|
|
sort.Strings(list)
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the config block for a auth method.
|
|
|
|
func ParseConfig(rawConfig map[string]interface{}, out interface{}) error {
|
|
|
|
decodeConf := &mapstructure.DecoderConfig{
|
|
|
|
Result: out,
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
ErrorUnused: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(decodeConf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decoder.Decode(rawConfig); err != nil {
|
|
|
|
return fmt.Errorf("error decoding config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|