forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usermanager.go
374 lines (328 loc) · 9.85 KB
/
usermanager.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package common
import (
"encoding/base32"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/slice"
"github.com/rancher/rancher/pkg/auth/tokens"
"github.com/rancher/rancher/pkg/randomtoken"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/rancher/types/user"
"github.com/sirupsen/logrus"
errors2 "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"
)
const (
userAuthHeader = "Impersonate-User"
userByPrincipalIndex = "auth.management.cattle.io/userByPrincipal"
crtbsByPrincipalAndUserIndex = "auth.management.cattle.io/crtbByPrincipalAndUser"
prtbsByPrincipalAndUserIndex = "auth.management.cattle.io/prtbByPrincipalAndUser"
)
func NewUserManager(scaledContext *config.ScaledContext) (user.Manager, error) {
userInformer := scaledContext.Management.Users("").Controller().Informer()
userIndexers := map[string]cache.IndexFunc{
userByPrincipalIndex: userByPrincipal,
}
if err := userInformer.AddIndexers(userIndexers); err != nil {
return nil, err
}
crtbInformer := scaledContext.Management.ClusterRoleTemplateBindings("").Controller().Informer()
crtbIndexers := map[string]cache.IndexFunc{
crtbsByPrincipalAndUserIndex: crtbsByPrincipalAndUser,
}
if err := crtbInformer.AddIndexers(crtbIndexers); err != nil {
return nil, err
}
prtbInformer := scaledContext.Management.ProjectRoleTemplateBindings("").Controller().Informer()
prtbIndexers := map[string]cache.IndexFunc{
prtbsByPrincipalAndUserIndex: prtbsByPrincipalAndUser,
}
if err := prtbInformer.AddIndexers(prtbIndexers); err != nil {
return nil, err
}
return &userManager{
users: scaledContext.Management.Users(""),
userIndexer: userInformer.GetIndexer(),
crtbIndexer: crtbInformer.GetIndexer(),
prtbIndexer: prtbInformer.GetIndexer(),
tokens: scaledContext.Management.Tokens(""),
tokenLister: scaledContext.Management.Tokens("").Controller().Lister(),
globalRoleBindings: scaledContext.Management.GlobalRoleBindings(""),
}, nil
}
type userManager struct {
users v3.UserInterface
globalRoleBindings v3.GlobalRoleBindingInterface
userIndexer cache.Indexer
crtbIndexer cache.Indexer
prtbIndexer cache.Indexer
tokenLister v3.TokenLister
tokens v3.TokenInterface
}
func (m *userManager) SetPrincipalOnCurrentUser(apiContext *types.APIContext, principal v3.Principal) (*v3.User, error) {
userID := m.GetUser(apiContext)
if userID == "" {
return nil, errors.New("user not provided")
}
user, err := m.users.Get(userID, v1.GetOptions{})
if err != nil {
return nil, err
}
if providerExists(user.PrincipalIDs, principal.Provider) {
var principalIDs []string
for _, id := range user.PrincipalIDs {
if !strings.Contains(id, principal.Provider) {
principalIDs = append(principalIDs, id)
}
}
user.PrincipalIDs = principalIDs
}
if !slice.ContainsString(user.PrincipalIDs, principal.Name) {
user.PrincipalIDs = append(user.PrincipalIDs, principal.Name)
logrus.Infof("Updating user %v. Adding principal", user.Name)
return m.users.Update(user)
}
return user, nil
}
func (m *userManager) GetUser(apiContext *types.APIContext) string {
return apiContext.Request.Header.Get(userAuthHeader)
}
// checkis if the supplied principal can login based on the accessMode and allowed principals
func (m *userManager) CheckAccess(accessMode string, allowedPrincipalIDs []string, userPrinc v3.Principal, groups []v3.Principal) (bool, error) {
if accessMode == "unrestricted" || accessMode == "" {
return true, nil
}
if accessMode == "required" || accessMode == "restricted" {
user, err := m.checkCache(userPrinc.Name)
if err != nil {
return false, err
}
userPrincipals := []string{userPrinc.Name}
if user != nil {
for _, p := range user.PrincipalIDs {
if userPrinc.Name != p {
userPrincipals = append(userPrincipals, p)
}
}
}
for _, p := range userPrincipals {
if slice.ContainsString(allowedPrincipalIDs, p) {
return true, nil
}
}
for _, g := range groups {
if slice.ContainsString(allowedPrincipalIDs, g.Name) {
return true, nil
}
}
if accessMode == "restricted" {
// check if any of the user's principals are in a project or cluster
var userNameAndPrincipals []string
for _, g := range groups {
userNameAndPrincipals = append(userNameAndPrincipals, g.Name)
}
if user != nil {
userNameAndPrincipals = append(userNameAndPrincipals, user.Name)
userNameAndPrincipals = append(userNameAndPrincipals, userPrincipals...)
}
return m.userExistsInClusterOrProject(userNameAndPrincipals)
}
return false, nil
}
return false, errors.Errorf("Unsupported accessMode: %v", accessMode)
}
func (m *userManager) EnsureToken(tokenName, description, userName string) (string, error) {
if strings.HasPrefix(tokenName, "token-") {
return "", errors.New("token names can't start with token-")
}
token, err := m.tokenLister.Get("", tokenName)
if errors2.IsNotFound(err) {
token, err = nil, nil
} else if err != nil {
return "", err
}
if token == nil {
key, err := randomtoken.Generate()
if err != nil {
return "", fmt.Errorf("failed to generate token key")
}
token = &v3.Token{
ObjectMeta: v1.ObjectMeta{
Name: tokenName,
Labels: map[string]string{
tokens.UserIDLabel: userName,
},
},
TTLMillis: 0,
Description: description,
UserID: userName,
AuthProvider: "local",
IsDerived: true,
Token: key,
}
logrus.Infof("Creating token for user %v", userName)
createdToken, err := m.tokens.Create(token)
if err != nil {
return "", err
}
token = createdToken
}
return token.Name + ":" + token.Token, nil
}
func (m *userManager) EnsureUser(principalName, displayName string) (*v3.User, error) {
// First check the local cache
u, err := m.checkCache(principalName)
if err != nil {
return nil, err
}
if u != nil {
return u, nil
}
// Not in cache, query API by label
u, labelSet, err := m.checkLabels(principalName)
if err != nil {
return nil, err
}
if u != nil {
return u, nil
}
// Doesn't exist, create user
user := &v3.User{
ObjectMeta: v1.ObjectMeta{
GenerateName: "user-",
Labels: labelSet,
},
DisplayName: displayName,
PrincipalIDs: []string{principalName},
}
logrus.Info("Creating user for principal %v", principalName)
created, err := m.users.Create(user)
if err != nil {
return nil, err
}
logrus.Info("Creating globalRoleBinding for %v", created.Name)
_, err = m.globalRoleBindings.Create(&v3.GlobalRoleBinding{
ObjectMeta: v1.ObjectMeta{
GenerateName: "globalrolebinding-",
},
UserName: created.Name,
GlobalRoleName: "user",
})
if err != nil {
return nil, err
}
return created, nil
}
func (m *userManager) checkCache(principalName string) (*v3.User, error) {
users, err := m.userIndexer.ByIndex(userByPrincipalIndex, principalName)
if err != nil {
return nil, err
}
if len(users) > 1 {
return nil, errors.Errorf("can't find unique user for principal %v", principalName)
}
if len(users) == 1 {
u := users[0].(*v3.User)
return u.DeepCopy(), nil
}
return nil, nil
}
func (m *userManager) userExistsInClusterOrProject(userNameAndPrincipals []string) (bool, error) {
for _, principal := range userNameAndPrincipals {
crtbs, err := m.crtbIndexer.ByIndex(crtbsByPrincipalAndUserIndex, principal)
if err != nil {
return false, err
}
if len(crtbs) > 0 {
return true, nil
}
prtbs, err := m.prtbIndexer.ByIndex(prtbsByPrincipalAndUserIndex, principal)
if err != nil {
return false, err
}
if len(prtbs) > 0 {
return true, nil
}
}
return false, nil
}
func (m *userManager) checkLabels(principalName string) (*v3.User, labels.Set, error) {
encodedPrincipalID := base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString([]byte(principalName))
if len(encodedPrincipalID) > 63 {
encodedPrincipalID = encodedPrincipalID[:63]
}
set := labels.Set(map[string]string{encodedPrincipalID: "hashed-principal-name"})
users, err := m.users.List(v1.ListOptions{LabelSelector: set.String()})
if err != nil {
return nil, nil, err
}
if len(users.Items) == 0 {
return nil, set, nil
}
var match *v3.User
for _, u := range users.Items {
if slice.ContainsString(u.PrincipalIDs, principalName) {
if match != nil {
// error out on duplicates
return nil, nil, errors.Errorf("can't find unique user for principal %v", principalName)
}
match = &u
}
}
return match, set, nil
}
func userByPrincipal(obj interface{}) ([]string, error) {
u, ok := obj.(*v3.User)
if !ok {
return []string{}, nil
}
return u.PrincipalIDs, nil
}
func crtbsByPrincipalAndUser(obj interface{}) ([]string, error) {
var principals []string
b, ok := obj.(*v3.ClusterRoleTemplateBinding)
if !ok {
return []string{}, nil
}
if b.GroupPrincipalName != "" {
principals = append(principals, b.GroupPrincipalName)
}
if b.UserPrincipalName != "" {
principals = append(principals, b.UserPrincipalName)
}
if b.UserName != "" {
principals = append(principals, b.UserName)
}
return principals, nil
}
func prtbsByPrincipalAndUser(obj interface{}) ([]string, error) {
var principals []string
b, ok := obj.(*v3.ProjectRoleTemplateBinding)
if !ok {
return []string{}, nil
}
if b.GroupPrincipalName != "" {
principals = append(principals, b.GroupPrincipalName)
}
if b.UserPrincipalName != "" {
principals = append(principals, b.UserPrincipalName)
}
if b.UserName != "" {
principals = append(principals, b.UserName)
}
return principals, nil
}
func providerExists(principalIDs []string, provider string) bool {
for _, id := range principalIDs {
splitID := strings.Split(id, ":")[0]
if strings.Contains(splitID, provider) {
return true
}
}
return false
}