forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usermanager.go
618 lines (540 loc) · 16.3 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package common
import (
"crypto/sha256"
"encoding/base32"
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
"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"
rbacv1 "github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
"github.com/rancher/types/config"
"github.com/rancher/types/user"
"github.com/sirupsen/logrus"
k8srbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
apitypes "k8s.io/apimachinery/pkg/types"
"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"
grbByUserIndex = "auth.management.cattle.io/grbByUser"
roleTemplatesRequired = "authz.management.cattle.io/creator-role-bindings"
)
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
}
grbInformer := scaledContext.Management.GlobalRoleBindings("").Controller().Informer()
grbIndexers := map[string]cache.IndexFunc{
grbByUserIndex: grbByUser,
}
if err := grbInformer.AddIndexers(grbIndexers); 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(""),
globalRoleLister: scaledContext.Management.GlobalRoles("").Controller().Lister(),
grbIndexer: grbInformer.GetIndexer(),
clusterRoleLister: scaledContext.RBAC.ClusterRoles("").Controller().Lister(),
clusterRoleBindingLister: scaledContext.RBAC.ClusterRoleBindings("").Controller().Lister(),
rbacClient: scaledContext.RBAC,
}, nil
}
type userManager struct {
users v3.UserInterface
globalRoleBindings v3.GlobalRoleBindingInterface
globalRoleLister v3.GlobalRoleLister
grbIndexer cache.Indexer
userIndexer cache.Indexer
crtbIndexer cache.Indexer
prtbIndexer cache.Indexer
tokenLister v3.TokenLister
tokens v3.TokenInterface
clusterRoleLister rbacv1.ClusterRoleLister
clusterRoleBindingLister rbacv1.ClusterRoleBindingLister
rbacClient rbacv1.Interface
}
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")
}
return m.SetPrincipalOnCurrentUserByUserID(userID, principal)
}
func (m *userManager) SetPrincipalOnCurrentUserByUserID(userID string, principal v3.Principal) (*v3.User, error) {
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 apierrors.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 {
if !apierrors.IsAlreadyExists(err) {
return "", err
}
token, err = m.tokens.Get(tokenName, v1.GetOptions{})
if err != nil {
return "", err
}
} else {
token = createdToken
}
}
return token.Name + ":" + token.Token, nil
}
func (m *userManager) EnsureUser(principalName, displayName string) (*v3.User, error) {
var user *v3.User
var err error
var labelSet labels.Set
// First check the local cache
user, err = m.checkCache(principalName)
if err != nil {
return nil, err
}
if user == nil {
// Not in cache, query API by label
user, labelSet, err = m.checkLabels(principalName)
if err != nil {
return nil, err
}
}
if user != nil {
// If the user does not have the annotation it indicates the user was created
// through the UI or from a previous rancher version so don't add the
// default bindings.
if _, ok := user.Annotations[roleTemplatesRequired]; !ok {
return user, nil
}
if v3.UserConditionInitialRolesPopulated.IsTrue(user) {
// The users global role bindings were already created. They can differ
// from what is in the annotation if they were updated manually.
return user, nil
}
} else {
// User doesn't exist, create user
logrus.Infof("Creating user for principal %v", principalName)
// Create a hash of the principalName to use as the name for the user,
// this lets k8s tell us if there are duplicate users with the same name
// thus avoiding a race.
hasher := sha256.New()
hasher.Write([]byte(principalName))
sha := base32.StdEncoding.WithPadding(-1).EncodeToString(hasher.Sum(nil))[:10]
annotations, err := m.createUsersRoleAnnotation()
if err != nil {
return nil, err
}
user = &v3.User{
ObjectMeta: v1.ObjectMeta{
Name: "u-" + strings.ToLower(sha),
Labels: labelSet,
Annotations: annotations,
},
DisplayName: displayName,
PrincipalIDs: []string{principalName},
}
user, err = m.users.Create(user)
if err != nil {
return nil, err
}
err = m.CreateNewUserClusterRoleBinding(user.Name, user.UID)
if err != nil {
return nil, err
}
}
logrus.Infof("Creating globalRoleBindings for %v", user.Name)
err = m.createUsersBindings(user)
if err != nil {
return nil, err
}
return user, nil
}
func (m *userManager) CreateNewUserClusterRoleBinding(userName string, userUID apitypes.UID) error {
roleName := userName + "-view"
bindingName := "grb-" + roleName
ownerReference := v1.OwnerReference{
APIVersion: "management.cattle.io/v3",
Kind: "User",
Name: userName,
UID: userUID,
}
cr, err := m.clusterRoleLister.Get("", roleName)
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
// ClusterRole doesn't exist yet, create it.
rule := k8srbacv1.PolicyRule{
Verbs: []string{"get"},
APIGroups: []string{"management.cattle.io"},
Resources: []string{"users"},
ResourceNames: []string{userName},
}
role := &k8srbacv1.ClusterRole{
ObjectMeta: v1.ObjectMeta{
Name: roleName,
OwnerReferences: []v1.OwnerReference{ownerReference},
},
Rules: []k8srbacv1.PolicyRule{rule},
}
cr, err = m.rbacClient.ClusterRoles("").Create(role)
if err != nil {
return err
}
}
_, err = m.clusterRoleBindingLister.Get("", bindingName)
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
// ClusterRoleBinding doesn't exit yet, create it.
crb := &k8srbacv1.ClusterRoleBinding{
ObjectMeta: v1.ObjectMeta{
Name: bindingName,
OwnerReferences: []v1.OwnerReference{ownerReference},
},
Subjects: []k8srbacv1.Subject{
k8srbacv1.Subject{
Kind: "User",
Name: userName,
},
},
RoleRef: k8srbacv1.RoleRef{
Kind: "ClusterRole",
Name: cr.Name,
},
}
_, err = m.rbacClient.ClusterRoleBindings("").Create(crb)
if err != nil {
return err
}
}
return nil
}
func (m *userManager) createUsersBindings(user *v3.User) error {
roleMap := make(map[string][]string)
err := json.Unmarshal([]byte(user.Annotations[roleTemplatesRequired]), &roleMap)
if err != nil {
return err
}
// Collect the users existing globalRoleBindings
var existingGRB []string
grbs, err := m.grbIndexer.ByIndex(grbByUserIndex, user.Name)
if err != nil {
return err
}
for _, grb := range grbs {
binding, ok := grb.(*v3.GlobalRoleBinding)
if !ok {
continue
}
existingGRB = append(existingGRB, binding.GlobalRoleName)
}
var createdRoles []string
for _, role := range roleMap["required"] {
if !slice.ContainsString(existingGRB, role) {
_, err := m.globalRoleBindings.Create(&v3.GlobalRoleBinding{
ObjectMeta: v1.ObjectMeta{
GenerateName: "grb-",
},
UserName: user.Name,
GlobalRoleName: role,
})
if err != nil {
return err
}
}
createdRoles = append(createdRoles, role)
}
roleMap["created"] = createdRoles
d, err := json.Marshal(roleMap)
if err != nil {
return err
}
rtr := string(d)
sleepTime := 100
// The user needs updated so keep trying if there is a conflict
for i := 0; i <= 3; i++ {
user, err = m.users.Get(user.Name, v1.GetOptions{})
if err != nil {
return err
}
user.Annotations[roleTemplatesRequired] = rtr
if reflect.DeepEqual(roleMap["required"], createdRoles) {
v3.UserConditionInitialRolesPopulated.True(user)
}
_, err = m.users.Update(user)
if err != nil {
if apierrors.IsConflict(err) {
// Conflict on the user, sleep and try again
time.Sleep(time.Duration(sleepTime) * time.Millisecond)
sleepTime *= 2
continue
}
return err
}
break
}
return nil
}
func (m *userManager) createUsersRoleAnnotation() (map[string]string, error) {
roleMap := make(map[string][]string)
roles, err := m.globalRoleLister.List("", labels.NewSelector())
if err != nil {
return nil, err
}
for _, gr := range roles {
if gr.NewUserDefault {
roleMap["required"] = append(roleMap["required"], gr.Name)
}
}
d, err := json.Marshal(roleMap)
if err != nil {
return nil, err
}
annotations := make(map[string]string)
annotations[roleTemplatesRequired] = string(d)
return annotations, 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 grbByUser(obj interface{}) ([]string, error) {
grb, ok := obj.(*v3.GlobalRoleBinding)
if !ok {
return []string{}, nil
}
return []string{grb.UserName}, 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
}