37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
authenticate.go 2.99 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-02-20 12:54 . Add HA support and cluster import
package requests
import (
"context"
"net/http"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"fmt"
"strings"
"github.com/rancher/rancher/pkg/auth/tokens"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
)
type Authenticator interface {
Authenticate(req *http.Request) (authed bool, user string, groups []string, err error)
TokenFromRequest(req *http.Request) (*v3.Token, error)
}
func NewAuthenticator(ctx context.Context, mgmtCtx *config.ScaledContext) Authenticator {
tokenInformer := mgmtCtx.Management.Tokens("").Controller().Informer()
tokenInformer.AddIndexers(map[string]cache.IndexFunc{tokenKeyIndex: tokenKeyIndexer})
return &tokenAuthenticator{
ctx: ctx,
tokenIndexer: tokenInformer.GetIndexer(),
tokenClient: mgmtCtx.Management.Tokens(""),
}
}
type tokenAuthenticator struct {
ctx context.Context
tokenIndexer cache.Indexer
tokenClient v3.TokenInterface
}
const (
tokenKeyIndex = "authn.management.cattle.io/token-key-index"
)
func tokenKeyIndexer(obj interface{}) ([]string, error) {
token, ok := obj.(*v3.Token)
if !ok {
return []string{}, nil
}
return []string{token.Token}, nil
}
func (a *tokenAuthenticator) Authenticate(req *http.Request) (bool, string, []string, error) {
token, err := a.TokenFromRequest(req)
if err != nil {
return false, "", []string{}, err
}
var groups []string
for _, principal := range token.GroupPrincipals {
// TODO This is a short cut for now. Will actually need to lookup groups in future
name := strings.TrimPrefix(principal.Name, "local://")
groups = append(groups, name)
}
return true, token.UserID, groups, nil
}
func (a *tokenAuthenticator) TokenFromRequest(req *http.Request) (*v3.Token, error) {
tokenAuthValue := tokens.GetTokenAuthFromRequest(req)
if tokenAuthValue == "" {
return nil, fmt.Errorf("must authenticate")
}
tokenName, tokenKey := tokens.SplitTokenParts(tokenAuthValue)
if tokenName == "" || tokenKey == "" {
return nil, fmt.Errorf("must authenticate")
}
lookupUsingClient := false
objs, err := a.tokenIndexer.ByIndex(tokenKeyIndex, tokenKey)
if err != nil {
if apierrors.IsNotFound(err) {
lookupUsingClient = true
} else {
return nil, fmt.Errorf("failed to retrieve auth token from cache, error: %v", err)
}
} else if len(objs) == 0 {
lookupUsingClient = true
}
storedToken := &v3.Token{}
if lookupUsingClient {
storedToken, err = a.tokenClient.Get(tokenName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil, fmt.Errorf("must authenticate")
}
return nil, fmt.Errorf("failed to retrieve auth token, error: %#v", err)
}
} else {
storedToken = objs[0].(*v3.Token)
}
if storedToken.Token != tokenKey || storedToken.ObjectMeta.Name != tokenName {
return nil, fmt.Errorf("must authenticate")
}
if tokens.IsExpired(*storedToken) {
return nil, fmt.Errorf("must authenticate")
}
return storedToken, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.0-beta1

搜索帮助

0d507c66 1850385 C8b1a773 1850385