-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
764 lines (687 loc) · 23.8 KB
/
manager.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
package auth
import (
"fmt"
"reflect"
"github.com/pkg/errors"
"github.com/rancher/norman/objectclient"
"github.com/rancher/norman/types/slice"
v13 "github.com/rancher/types/apis/core/v1"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
typesrbacv1 "github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
"github.com/rancher/types/config"
"github.com/rancher/types/user"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
var commonClusterAndProjectMgmtPlaneResources = map[string]bool{
"catalogtemplates": true,
"catalogtemplateversions": true,
}
func newRTBLifecycles(management *config.ManagementContext) (*prtbLifecycle, *crtbLifecycle) {
crbInformer := management.RBAC.ClusterRoleBindings("").Controller().Informer()
crbIndexers := map[string]cache.IndexFunc{
rbByRoleAndSubjectIndex: rbByRoleAndSubject,
}
crbInformer.AddIndexers(crbIndexers)
rbInformer := management.RBAC.RoleBindings("").Controller().Informer()
rbIndexers := map[string]cache.IndexFunc{
rbByOwnerIndex: rbByOwner,
rbByRoleAndSubjectIndex: rbByRoleAndSubject,
}
rbInformer.AddIndexers(rbIndexers)
prtb := &prtbLifecycle{
mgr: &manager{
mgmt: management,
projectLister: management.Management.Projects("").Controller().Lister(),
crbLister: management.RBAC.ClusterRoleBindings("").Controller().Lister(),
crLister: management.RBAC.ClusterRoles("").Controller().Lister(),
rLister: management.RBAC.Roles("").Controller().Lister(),
rbLister: management.RBAC.RoleBindings("").Controller().Lister(),
rtLister: management.Management.RoleTemplates("").Controller().Lister(),
nsLister: management.Core.Namespaces("").Controller().Lister(),
userLister: management.Management.Users("").Controller().Lister(),
rbIndexer: rbInformer.GetIndexer(),
crbIndexer: crbInformer.GetIndexer(),
userMGR: management.UserManager,
controller: ptrbMGMTController,
},
projectLister: management.Management.Projects("").Controller().Lister(),
clusterLister: management.Management.Clusters("").Controller().Lister(),
}
crtb := &crtbLifecycle{
mgr: &manager{
mgmt: management,
projectLister: management.Management.Projects("").Controller().Lister(),
crbLister: management.RBAC.ClusterRoleBindings("").Controller().Lister(),
crLister: management.RBAC.ClusterRoles("").Controller().Lister(),
rLister: management.RBAC.Roles("").Controller().Lister(),
rbLister: management.RBAC.RoleBindings("").Controller().Lister(),
rtLister: management.Management.RoleTemplates("").Controller().Lister(),
nsLister: management.Core.Namespaces("").Controller().Lister(),
userLister: management.Management.Users("").Controller().Lister(),
rbIndexer: rbInformer.GetIndexer(),
crbIndexer: crbInformer.GetIndexer(),
userMGR: management.UserManager,
controller: ctrbMGMTController,
},
clusterLister: management.Management.Clusters("").Controller().Lister(),
}
return prtb, crtb
}
type manager struct {
projectLister v3.ProjectLister
crLister typesrbacv1.ClusterRoleLister
rLister typesrbacv1.RoleLister
rbLister typesrbacv1.RoleBindingLister
crbLister typesrbacv1.ClusterRoleBindingLister
rtLister v3.RoleTemplateLister
nsLister v13.NamespaceLister
userLister v3.UserLister
rbIndexer cache.Indexer
crbIndexer cache.Indexer
mgmt *config.ManagementContext
userMGR user.Manager
controller string
}
// When a CRTB is created that gives a subject some permissions in a project or cluster, we need to create a "membership" binding
// that gives the subject access to the the cluster custom resource itself
// This is painfully similar to ensureProjectMemberBinding, but making one function that handles both is overly complex
func (m *manager) ensureClusterMembershipBinding(roleName, rtbUID string, cluster *v3.Cluster, makeOwner bool, subject v1.Subject) error {
if err := m.createClusterMembershipRole(roleName, cluster, makeOwner); err != nil {
return err
}
key := rbRoleSubjectKey(roleName, subject)
set := labels.Set(map[string]string{rtbUID: membershipBindingOwner})
crbs, err := m.crbLister.List("", set.AsSelector())
if err != nil {
return err
}
var crb *v1.ClusterRoleBinding
for _, iCRB := range crbs {
if len(iCRB.Subjects) != 1 {
iKey := rbRoleSubjectKey(iCRB.RoleRef.Name, iCRB.Subjects[0])
if iKey == key {
crb = iCRB
continue
}
}
if err := m.reconcileClusterMembershipBindingForDelete(roleName, rtbUID); err != nil {
return err
}
}
if crb != nil {
return nil
}
objs, err := m.crbIndexer.ByIndex(rbByRoleAndSubjectIndex, key)
if err != nil {
return err
}
if len(objs) == 0 {
logrus.Infof("[%v] Creating clusterRoleBinding for membership in cluster %v for subject %v", m.controller, cluster.Name, subject.Name)
_, err := m.mgmt.RBAC.ClusterRoleBindings("").Create(&v1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "clusterrolebinding-",
Labels: map[string]string{
rtbUID: membershipBindingOwner,
},
},
Subjects: []v1.Subject{subject},
RoleRef: v1.RoleRef{
Kind: "ClusterRole",
Name: roleName,
},
})
return err
}
crb, _ = objs[0].(*v1.ClusterRoleBinding)
for owner := range crb.Labels {
if rtbUID == owner {
return nil
}
}
crb = crb.DeepCopy()
if crb.Labels == nil {
crb.Labels = map[string]string{}
}
crb.Labels[rtbUID] = membershipBindingOwner
logrus.Infof("[%v] Updating clusterRoleBinding %v for cluster membership in cluster %v for subject %v", m.controller, crb.Name, cluster.Name, subject.Name)
_, err = m.mgmt.RBAC.ClusterRoleBindings("").Update(crb)
return err
}
// When a PRTB is created that gives a subject some permissions in a project or cluster, we need to create a "membership" binding
// that gives the subject access to the the project/cluster custom resource itself
func (m *manager) ensureProjectMembershipBinding(roleName, rtbUID, namespace string, project *v3.Project, makeOwner bool, subject v1.Subject) error {
if err := m.createProjectMembershipRole(roleName, namespace, project, makeOwner); err != nil {
return err
}
key := rbRoleSubjectKey(roleName, subject)
set := labels.Set(map[string]string{rtbUID: membershipBindingOwner})
rbs, err := m.rbLister.List("", set.AsSelector())
if err != nil {
return err
}
var rb *v1.RoleBinding
for _, iRB := range rbs {
if len(iRB.Subjects) != 1 {
iKey := rbRoleSubjectKey(iRB.RoleRef.Name, iRB.Subjects[0])
if iKey == key {
rb = iRB
continue
}
}
if err := m.reconcileProjectMembershipBindingForDelete(namespace, roleName, rtbUID); err != nil {
return err
}
}
if rb != nil {
return nil
}
objs, err := m.rbIndexer.ByIndex(rbByRoleAndSubjectIndex, key)
if err != nil {
return err
}
if len(objs) == 0 {
logrus.Infof("[%v] Creating roleBinding for membership in project %v for subject %v", m.controller, project.Name, subject.Name)
_, err := m.mgmt.RBAC.RoleBindings(namespace).Create(&v1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "rolebinding-",
Labels: map[string]string{
rtbUID: membershipBindingOwner,
},
},
Subjects: []v1.Subject{subject},
RoleRef: v1.RoleRef{
Kind: "Role",
Name: roleName,
},
})
return err
}
rb, _ = objs[0].(*v1.RoleBinding)
for owner := range rb.Labels {
if rtbUID == owner {
return nil
}
}
rb = rb.DeepCopy()
if rb.Labels == nil {
rb.Labels = map[string]string{}
}
rb.Labels[rtbUID] = membershipBindingOwner
logrus.Infof("[%v] Updating roleBinding %v for project membership in project %v for subject %v", m.controller, rb.Name, project.Name, subject.Name)
_, err = m.mgmt.RBAC.RoleBindings(namespace).Update(rb)
return err
}
// Creates a role that lets the bound subject see (if they are an ordinary member) the project or cluster in the mgmt api
// (or CRUD the project/cluster if they are an owner)
func (m *manager) createClusterMembershipRole(roleName string, cluster *v3.Cluster, makeOwner bool) error {
if cr, _ := m.crLister.Get("", roleName); cr == nil {
return m.createMembershipRole(clusterResource, roleName, makeOwner, cluster, m.mgmt.RBAC.ClusterRoles("").ObjectClient())
}
return nil
}
// Creates a role that lets the bound subject see (if they are an ordinary member) the project in the mgmt api
// (or CRUD the project if they are an owner)
func (m *manager) createProjectMembershipRole(roleName, namespace string, project *v3.Project, makeOwner bool) error {
if cr, _ := m.rLister.Get(namespace, roleName); cr == nil {
return m.createMembershipRole(projectResource, roleName, makeOwner, project, m.mgmt.RBAC.Roles(namespace).ObjectClient())
}
return nil
}
func (m *manager) createMembershipRole(resourceType, roleName string, makeOwner bool, ownerObject interface{}, client *objectclient.ObjectClient) error {
metaObj, err := meta.Accessor(ownerObject)
if err != nil {
return err
}
typeMeta, err := meta.TypeAccessor(ownerObject)
if err != nil {
return err
}
rules := []v1.PolicyRule{
{
APIGroups: []string{"management.cattle.io"},
Resources: []string{resourceType},
ResourceNames: []string{metaObj.GetName()},
Verbs: []string{"get"},
},
}
if makeOwner {
rules[0].Verbs = []string{"*"}
} else {
rules[0].Verbs = []string{"get"}
}
logrus.Infof("[%v] Creating clusterRole %v", m.controller, roleName)
_, err = client.Create(&v1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: roleName,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: typeMeta.GetAPIVersion(),
Kind: typeMeta.GetKind(),
Name: metaObj.GetName(),
UID: metaObj.GetUID(),
},
},
},
Rules: rules,
})
return err
}
// The CRTB has been deleted or modified, either delete or update the membership binding so that the subject
// is removed from the cluster if they should be
func (m *manager) reconcileClusterMembershipBindingForDelete(roleToKeep, rtbUID string) error {
list := func(ns string, selector labels.Selector) ([]runtime.Object, error) {
rbs, err := m.crbLister.List(ns, selector)
if err != nil {
return nil, err
}
var items []runtime.Object
for _, rb := range rbs {
items = append(items, rb.DeepCopy())
}
return items, nil
}
convert := func(i interface{}) string {
rb, _ := i.(*v1.ClusterRoleBinding)
return rb.RoleRef.Name
}
return m.reconcileMembershipBindingForDelete("", roleToKeep, rtbUID, list, convert, m.mgmt.RBAC.ClusterRoleBindings("").ObjectClient())
}
// The PRTB has been deleted, either delete or update the project membership binding so that the subject
// is removed from the project if they should be
func (m *manager) reconcileProjectMembershipBindingForDelete(namespace, roleToKeep, rtbUID string) error {
list := func(ns string, selector labels.Selector) ([]runtime.Object, error) {
rbs, err := m.rbLister.List(ns, selector)
if err != nil {
return nil, err
}
var items []runtime.Object
for _, rb := range rbs {
items = append(items, rb.DeepCopy())
}
return items, nil
}
convert := func(i interface{}) string {
rb, _ := i.(*v1.RoleBinding)
return rb.RoleRef.Name
}
return m.reconcileMembershipBindingForDelete(namespace, roleToKeep, rtbUID, list, convert, m.mgmt.RBAC.RoleBindings(namespace).ObjectClient())
}
type listFn func(ns string, selector labels.Selector) ([]runtime.Object, error)
type convertFn func(i interface{}) string
func (m *manager) reconcileMembershipBindingForDelete(namespace, roleToKeep, rtbUID string, list listFn, convert convertFn, client *objectclient.ObjectClient) error {
set := labels.Set(map[string]string{rtbUID: membershipBindingOwner})
roleBindings, err := list(namespace, set.AsSelector())
if err != nil {
return err
}
for _, rb := range roleBindings {
objMeta, err := meta.Accessor(rb)
if err != nil {
return err
}
roleName := convert(rb)
if roleName == roleToKeep {
continue
}
var otherOwners bool
for k, v := range objMeta.GetLabels() {
if k == rtbUID && v == membershipBindingOwner {
delete(objMeta.GetLabels(), k)
} else if v == membershipBindingOwner {
// Another crtb is also linked to this roleBinding so don't delete
otherOwners = true
}
}
if !otherOwners {
logrus.Infof("[%v] Deleting roleBinding %v", m.controller, objMeta.GetName())
if err := client.Delete(objMeta.GetName(), &metav1.DeleteOptions{}); err != nil {
if apierrors.IsNotFound(err) {
continue
}
return err
}
} else {
logrus.Infof("[%v] Updating owner label for roleBinding %v", m.controller, objMeta.GetName())
if _, err := client.Update(objMeta.GetName(), rb); err != nil {
return err
}
}
}
return nil
}
// Certain resources (projects, machines, prtbs, crtbs, clusterevents, etc) exist in the mangement plane but are scoped to clusters or
// projects. They need special RBAC handling because the need to be authorized just inside of the namespace that backs the project
// or cluster they belong to.
func (m *manager) grantManagementPlanePrivileges(roleTemplateName string, resources []string, subject v1.Subject, binding interface{}) error {
bindingMeta, err := meta.Accessor(binding)
if err != nil {
return err
}
bindingTypeMeta, err := meta.TypeAccessor(binding)
if err != nil {
return err
}
namespace := bindingMeta.GetNamespace()
roles, err := m.gatherAndDedupeRoles(roleTemplateName)
if err != nil {
return err
}
desiredRBs := map[string]*v1.RoleBinding{}
roleBindings := m.mgmt.RBAC.RoleBindings(namespace)
for _, role := range roles {
resourceToVerbs := map[string]map[string]bool{}
for _, resource := range resources {
verbs, err := m.checkForManagementPlaneRules(role, resource)
if err != nil {
return err
}
if len(verbs) > 0 {
resourceToVerbs[resource] = verbs
bindingName := bindingMeta.GetName() + "-" + role.Name
if _, ok := desiredRBs[bindingName]; !ok {
desiredRBs[bindingName] = &v1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: bindingName,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: bindingTypeMeta.GetAPIVersion(),
Kind: bindingTypeMeta.GetKind(),
Name: bindingMeta.GetName(),
UID: bindingMeta.GetUID(),
},
},
},
Subjects: []v1.Subject{subject},
RoleRef: v1.RoleRef{
Kind: "Role",
Name: role.Name,
},
}
}
}
}
if len(resourceToVerbs) > 0 {
if err := m.reconcileManagementPlaneRole(namespace, resourceToVerbs, role); err != nil {
return err
}
}
}
currentRBs := map[string]*v1.RoleBinding{}
current, err := m.rbIndexer.ByIndex(rbByOwnerIndex, string(bindingMeta.GetUID()))
if err != nil {
return err
}
for _, c := range current {
rb := c.(*v1.RoleBinding)
currentRBs[rb.Name] = rb
}
return m.reconcileDesiredMGMTPlaneRoleBindings(currentRBs, desiredRBs, roleBindings)
}
// grantManagementClusterScopedPrivilegesInProjectNamespace ensures that rolebindings for roles like cluster-owner (that should be able to fully
// manage all projects in a cluster) grant proper permissions to project-scoped resources. Specifically, this satisfies the use case that
// a cluster owner should be able to manage the members of all projects in their cluster
func (m *manager) grantManagementClusterScopedPrivilegesInProjectNamespace(roleTemplateName, projectNamespace string, resources []string,
subject v1.Subject, binding *v3.ClusterRoleTemplateBinding) error {
roles, err := m.gatherAndDedupeRoles(roleTemplateName)
if err != nil {
return err
}
desiredRBs := map[string]*v1.RoleBinding{}
roleBindings := m.mgmt.RBAC.RoleBindings(projectNamespace)
for _, role := range roles {
resourceToVerbs := map[string]map[string]bool{}
for _, resource := range resources {
// Adding this check, because we want cluster-owners to have access to catalogtemplates/versions of all projects, but no other cluster roles
// need to access catalogtemplates of projects they do not belong to
if !role.Administrative && commonClusterAndProjectMgmtPlaneResources[resource] {
continue
}
verbs, err := m.checkForManagementPlaneRules(role, resource)
if err != nil {
return err
}
if len(verbs) > 0 {
resourceToVerbs[resource] = verbs
bindingName := binding.Name + "-" + role.Name
if _, ok := desiredRBs[bindingName]; !ok {
desiredRBs[bindingName] = &v1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: bindingName,
Labels: map[string]string{
string(binding.UID): crtbInProjectBindingOwner,
},
},
Subjects: []v1.Subject{subject},
RoleRef: v1.RoleRef{
Kind: "Role",
Name: role.Name,
},
}
}
}
}
if len(resourceToVerbs) > 0 {
if err := m.reconcileManagementPlaneRole(projectNamespace, resourceToVerbs, role); err != nil {
return err
}
}
}
currentRBs := map[string]*v1.RoleBinding{}
set := labels.Set(map[string]string{string(binding.UID): crtbInProjectBindingOwner})
current, err := m.rbLister.List(projectNamespace, set.AsSelector())
if err != nil {
return err
}
for _, rb := range current {
currentRBs[rb.Name] = rb
}
return m.reconcileDesiredMGMTPlaneRoleBindings(currentRBs, desiredRBs, roleBindings)
}
// grantManagementProjectScopedPrivilegesInClusterNamespace ensures that project roles grant permissions to certain cluster-scoped
// resources(notifier, clusterpipelines). These resources exists in cluster namespace but need to be shared between projects.
func (m *manager) grantManagementProjectScopedPrivilegesInClusterNamespace(roleTemplateName, clusterNamespace string, resources []string,
subject v1.Subject, binding *v3.ProjectRoleTemplateBinding) error {
roles, err := m.gatherAndDedupeRoles(roleTemplateName)
if err != nil {
return err
}
desiredRBs := map[string]*v1.RoleBinding{}
roleBindings := m.mgmt.RBAC.RoleBindings(clusterNamespace)
for _, role := range roles {
resourceToVerbs := map[string]map[string]bool{}
for _, resource := range resources {
verbs, err := m.checkForManagementPlaneRules(role, resource)
if err != nil {
return err
}
if len(verbs) > 0 {
resourceToVerbs[resource] = verbs
bindingName := fmt.Sprintf("%s-%s-%s", binding.Namespace, binding.Name, role.Name)
if _, ok := desiredRBs[bindingName]; !ok {
desiredRBs[bindingName] = &v1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: bindingName,
Labels: map[string]string{
string(binding.UID): prtbInClusterBindingOwner,
},
},
Subjects: []v1.Subject{subject},
RoleRef: v1.RoleRef{
Kind: "Role",
Name: role.Name,
},
}
}
}
}
if len(resourceToVerbs) > 0 {
if err := m.reconcileManagementPlaneRole(clusterNamespace, resourceToVerbs, role); err != nil {
return err
}
}
}
currentRBs := map[string]*v1.RoleBinding{}
set := labels.Set(map[string]string{string(binding.UID): prtbInClusterBindingOwner})
current, err := m.rbLister.List(clusterNamespace, set.AsSelector())
if err != nil {
return err
}
for _, rb := range current {
currentRBs[rb.Name] = rb
}
return m.reconcileDesiredMGMTPlaneRoleBindings(currentRBs, desiredRBs, roleBindings)
}
func (m *manager) gatherAndDedupeRoles(roleTemplateName string) (map[string]*v3.RoleTemplate, error) {
rt, err := m.rtLister.Get("", roleTemplateName)
if err != nil {
return nil, err
}
allRoles := map[string]*v3.RoleTemplate{}
if err := m.gatherRoleTemplates(rt, allRoles); err != nil {
return nil, err
}
//de-dupe
roles := map[string]*v3.RoleTemplate{}
for _, role := range allRoles {
roles[role.Name] = role
}
return roles, nil
}
func (m *manager) reconcileDesiredMGMTPlaneRoleBindings(currentRBs, desiredRBs map[string]*v1.RoleBinding, roleBindings typesrbacv1.RoleBindingInterface) error {
rbsToDelete := map[string]bool{}
processed := map[string]bool{}
for _, rb := range currentRBs {
// protect against an rb being in the list more than once (shouldn't happen, but just to be safe)
if ok := processed[rb.Name]; ok {
continue
}
processed[rb.Name] = true
if _, ok := desiredRBs[rb.Name]; ok {
delete(desiredRBs, rb.Name)
} else {
rbsToDelete[rb.Name] = true
}
}
for _, rb := range desiredRBs {
logrus.Infof("[%v] Creating roleBinding for subject %v with role %v in namespace %v", m.controller, rb.Subjects[0].Name, rb.RoleRef.Name, rb.Namespace)
_, err := roleBindings.Create(rb)
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
}
for name := range rbsToDelete {
logrus.Infof("[%v] Deleting roleBinding %v", m.controller, name)
if err := roleBindings.Delete(name, &metav1.DeleteOptions{}); err != nil {
return err
}
}
return nil
}
// If the roleTemplate has rules granting access to a management plane resource, return the verbs for those rules
func (m *manager) checkForManagementPlaneRules(role *v3.RoleTemplate, managementPlaneResource string) (map[string]bool, error) {
var rules []v1.PolicyRule
if role.External {
externalRole, err := m.crLister.Get("", role.Name)
if err != nil && !apierrors.IsNotFound(err) {
// dont error if it doesnt exist
return nil, err
}
if externalRole != nil {
rules = externalRole.Rules
}
} else {
rules = role.Rules
}
verbs := map[string]bool{}
for _, rule := range rules {
if (slice.ContainsString(rule.Resources, managementPlaneResource) || slice.ContainsString(rule.Resources, "*")) && len(rule.ResourceNames) == 0 {
for _, v := range rule.Verbs {
verbs[v] = true
}
}
}
return verbs, nil
}
func (m *manager) reconcileManagementPlaneRole(namespace string, resourceToVerbs map[string]map[string]bool, rt *v3.RoleTemplate) error {
roleCli := m.mgmt.RBAC.Roles(namespace)
update := false
if role, err := m.rLister.Get(namespace, rt.Name); err == nil && role != nil {
newRole := role.DeepCopy()
for resource, newVerbs := range resourceToVerbs {
currentVerbs := map[string]bool{}
for _, rule := range role.Rules {
if slice.ContainsString(rule.Resources, resource) {
for _, v := range rule.Verbs {
currentVerbs[v] = true
}
}
}
if !reflect.DeepEqual(currentVerbs, newVerbs) {
update = true
role = role.DeepCopy()
added := false
for i, rule := range newRole.Rules {
if slice.ContainsString(rule.Resources, resource) {
newRole.Rules[i] = buildRule(resource, newVerbs)
added = true
}
}
if !added {
newRole.Rules = append(newRole.Rules, buildRule(resource, newVerbs))
}
}
}
if update {
logrus.Infof("[%v] Updating role %v in namespace %v", m.controller, newRole.Name, namespace)
_, err := roleCli.Update(newRole)
return err
}
return nil
}
var rules []v1.PolicyRule
for resource, newVerbs := range resourceToVerbs {
rules = append(rules, buildRule(resource, newVerbs))
}
logrus.Infof("[%v] Creating role %v in namespace %v", m.controller, rt.Name, namespace)
_, err := roleCli.Create(&v1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: rt.Name,
},
Rules: rules,
})
if err != nil {
return errors.Wrapf(err, "couldn't create role %v", rt.Name)
}
return nil
}
func (m *manager) gatherRoleTemplates(rt *v3.RoleTemplate, roleTemplates map[string]*v3.RoleTemplate) error {
roleTemplates[rt.Name] = rt
for _, rtName := range rt.RoleTemplateNames {
subRT, err := m.rtLister.Get("", rtName)
if err != nil {
return errors.Wrapf(err, "couldn't get RoleTemplate %s", rtName)
}
if err := m.gatherRoleTemplates(subRT, roleTemplates); err != nil {
return errors.Wrapf(err, "couldn't gather RoleTemplate %s", rtName)
}
}
return nil
}
func buildRule(resource string, verbs map[string]bool) v1.PolicyRule {
var vs []string
for v := range verbs {
vs = append(vs, v)
}
return v1.PolicyRule{
Resources: []string{resource},
Verbs: vs,
APIGroups: []string{"*"},
}
}