forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
authenticate.go
143 lines (119 loc) · 3.87 KB
/
authenticate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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"
"k8s.io/apiserver/pkg/authentication/user"
)
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(""),
userAttributeLister: mgmtCtx.Management.UserAttributes("").Controller().Lister(),
userAttributes: mgmtCtx.Management.UserAttributes(""),
}
}
type tokenAuthenticator struct {
ctx context.Context
tokenIndexer cache.Indexer
tokenClient v3.TokenInterface
userAttributes v3.UserAttributeInterface
userAttributeLister v3.UserAttributeLister
}
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
}
attribs, err := a.userAttributeLister.Get("", token.UserID)
if err != nil && !apierrors.IsNotFound(err) {
return false, "", []string{}, err
}
var groups []string
hitProvider := false
if attribs != nil {
for provider, gps := range attribs.GroupPrincipals {
if provider == token.AuthProvider {
hitProvider = true
}
for _, principal := range gps.Items {
name := strings.TrimPrefix(principal.Name, "local://")
groups = append(groups, name)
}
}
}
// fallback to legacy token.GroupPrincipals
if !hitProvider {
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)
}
}
groups = append(groups, user.AllAuthenticated)
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
}