2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-09-14 14:36:06 +00:00
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
// Package authncache implements a cache of active authenticators.
|
|
|
|
package authncache
|
2020-09-14 14:36:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-09-22 14:50:34 +00:00
|
|
|
"sort"
|
2020-09-14 14:36:06 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"k8s.io/apiserver/pkg/authentication/authenticator"
|
2020-09-21 16:37:54 +00:00
|
|
|
"k8s.io/apiserver/pkg/authentication/user"
|
2020-09-14 14:36:06 +00:00
|
|
|
|
2020-10-30 14:34:43 +00:00
|
|
|
loginapi "go.pinniped.dev/generated/1.19/apis/concierge/login"
|
2020-09-14 14:36:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-10-30 19:02:21 +00:00
|
|
|
// ErrNoSuchAuthenticator is returned by Cache.AuthenticateTokenCredentialRequest() when the requested authenticator is not configured.
|
|
|
|
ErrNoSuchAuthenticator = fmt.Errorf("no such authenticator")
|
2020-09-14 14:36:06 +00:00
|
|
|
)
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
// Cache implements the authenticator.Token interface by multiplexing across a dynamic set of authenticators
|
|
|
|
// loaded from authenticator resources.
|
2020-09-14 14:36:06 +00:00
|
|
|
type Cache struct {
|
|
|
|
cache sync.Map
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
type Key struct {
|
|
|
|
APIGroup string
|
|
|
|
Kind string
|
|
|
|
Namespace string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Value interface {
|
|
|
|
authenticator.Token
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:36:06 +00:00
|
|
|
// New returns an empty cache.
|
|
|
|
func New() *Cache {
|
|
|
|
return &Cache{}
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
// Get an authenticator by key.
|
2020-09-21 16:37:54 +00:00
|
|
|
func (c *Cache) Get(key Key) Value {
|
|
|
|
res, _ := c.cache.Load(key)
|
|
|
|
if res == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return res.(Value)
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
// Store an authenticator into the cache.
|
2020-09-21 16:37:54 +00:00
|
|
|
func (c *Cache) Store(key Key, value Value) {
|
2020-09-14 14:36:06 +00:00
|
|
|
c.cache.Store(key, value)
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
// Delete an authenticator from the cache.
|
2020-09-21 16:37:54 +00:00
|
|
|
func (c *Cache) Delete(key Key) {
|
2020-09-14 14:36:06 +00:00
|
|
|
c.cache.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keys currently stored in the cache.
|
2020-09-21 16:37:54 +00:00
|
|
|
func (c *Cache) Keys() []Key {
|
|
|
|
var result []Key
|
2020-09-14 14:36:06 +00:00
|
|
|
c.cache.Range(func(key, _ interface{}) bool {
|
2020-09-21 16:37:54 +00:00
|
|
|
result = append(result, key.(Key))
|
2020-09-14 14:36:06 +00:00
|
|
|
return true
|
|
|
|
})
|
2020-09-22 14:50:34 +00:00
|
|
|
|
|
|
|
// Sort the results for consistency.
|
|
|
|
sort.Slice(result, func(i, j int) bool {
|
|
|
|
return result[i].APIGroup < result[j].APIGroup ||
|
|
|
|
result[i].Kind < result[j].Kind ||
|
|
|
|
result[i].Namespace < result[j].Namespace ||
|
|
|
|
result[i].Name < result[j].Name
|
|
|
|
})
|
2020-09-14 14:36:06 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
func (c *Cache) AuthenticateTokenCredentialRequest(ctx context.Context, req *loginapi.TokenCredentialRequest) (user.Info, error) {
|
|
|
|
// Map the incoming request to a cache key.
|
|
|
|
key := Key{
|
|
|
|
Namespace: req.Namespace,
|
2020-10-30 17:41:21 +00:00
|
|
|
Name: req.Spec.Authenticator.Name,
|
|
|
|
Kind: req.Spec.Authenticator.Kind,
|
2020-09-21 16:37:54 +00:00
|
|
|
}
|
2020-10-30 17:41:21 +00:00
|
|
|
if req.Spec.Authenticator.APIGroup != nil {
|
|
|
|
key.APIGroup = *req.Spec.Authenticator.APIGroup
|
2020-09-21 16:37:54 +00:00
|
|
|
}
|
2020-09-14 14:36:06 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
val := c.Get(key)
|
|
|
|
if val == nil {
|
2020-10-30 19:02:21 +00:00
|
|
|
return nil, ErrNoSuchAuthenticator
|
2020-09-14 14:36:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
// The incoming context could have an audience. Since we do not want to handle audiences right now, do not pass it
|
|
|
|
// through directly to the authentication webhook.
|
|
|
|
ctx = valuelessContext{ctx}
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
// Call the selected authenticator.
|
2020-09-21 16:37:54 +00:00
|
|
|
resp, authenticated, err := val.AuthenticateToken(ctx, req.Spec.Token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !authenticated {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the user.Info from the response (if it is non-nil).
|
|
|
|
var respUser user.Info
|
|
|
|
if resp != nil {
|
|
|
|
respUser = resp.User
|
|
|
|
}
|
|
|
|
return respUser, nil
|
2020-09-14 14:36:06 +00:00
|
|
|
}
|
2020-09-21 16:37:54 +00:00
|
|
|
|
|
|
|
type valuelessContext struct{ context.Context }
|
|
|
|
|
|
|
|
func (valuelessContext) Value(interface{}) interface{} { return nil }
|