forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_base.go
464 lines (407 loc) · 15.6 KB
/
handler_base.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
package rbac
import (
"context"
"reflect"
"strings"
"github.com/pkg/errors"
"github.com/rancher/norman/objectclient"
"github.com/rancher/norman/types/convert"
typescorev1 "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/sirupsen/logrus"
rbacv1 "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"
"github.com/rancher/rancher/pkg/controllers/user/resourcequota"
nsutils "github.com/rancher/rancher/pkg/namespace"
pkgrbac "github.com/rancher/rancher/pkg/rbac"
)
const (
rtbOwnerLabel = "authz.cluster.cattle.io/rtb-owner"
projectIDAnnotation = "field.cattle.io/projectId"
prtbByProjectIndex = "authz.cluster.cattle.io/prtb-by-project"
prtbByProjecSubjectIndex = "authz.cluster.cattle.io/prtb-by-project-subject"
prtbByUIDIndex = "authz.cluster.cattle.io/rtb-owner"
rtbByClusterAndRoleTemplateIndex = "authz.cluster.cattle.io/rtb-by-cluster-rt"
nsByProjectIndex = "authz.cluster.cattle.io/ns-by-project"
crByNSIndex = "authz.cluster.cattle.io/cr-by-ns"
crbByRoleAndSubjectIndex = "authz.cluster.cattle.io/crb-by-role-and-subject"
)
func Register(ctx context.Context, workload *config.UserContext) {
// Add cache informer to project role template bindings
prtbInformer := workload.Management.Management.ProjectRoleTemplateBindings("").Controller().Informer()
prtbIndexers := map[string]cache.IndexFunc{
prtbByProjectIndex: prtbByProjectName,
prtbByProjecSubjectIndex: prtbByProjectAndSubject,
rtbByClusterAndRoleTemplateIndex: rtbByClusterAndRoleTemplateName,
prtbByUIDIndex: prtbByUID,
}
prtbInformer.AddIndexers(prtbIndexers)
crtbInformer := workload.Management.Management.ClusterRoleTemplateBindings("").Controller().Informer()
crtbIndexers := map[string]cache.IndexFunc{
rtbByClusterAndRoleTemplateIndex: rtbByClusterAndRoleTemplateName,
}
crtbInformer.AddIndexers(crtbIndexers)
// Index for looking up namespaces by projectID annotation
nsInformer := workload.Core.Namespaces("").Controller().Informer()
nsIndexers := map[string]cache.IndexFunc{
nsByProjectIndex: nsutils.NsByProjectID,
}
nsInformer.AddIndexers(nsIndexers)
// Get ClusterRoles by the namespaces the authorizes because they are in a project
crInformer := workload.RBAC.ClusterRoles("").Controller().Informer()
crIndexers := map[string]cache.IndexFunc{
crByNSIndex: crByNS,
}
crInformer.AddIndexers(crIndexers)
// Get ClusterRoleBindings by subject name and kind
crbInformer := workload.RBAC.ClusterRoleBindings("").Controller().Informer()
crbIndexers := map[string]cache.IndexFunc{
crbByRoleAndSubjectIndex: crbByRoleAndSubject,
}
crbInformer.AddIndexers(crbIndexers)
r := &manager{
workload: workload,
prtbIndexer: prtbInformer.GetIndexer(),
crtbIndexer: crtbInformer.GetIndexer(),
nsIndexer: nsInformer.GetIndexer(),
crIndexer: crInformer.GetIndexer(),
crbIndexer: crbInformer.GetIndexer(),
rtLister: workload.Management.Management.RoleTemplates("").Controller().Lister(),
rbLister: workload.RBAC.RoleBindings("").Controller().Lister(),
crbLister: workload.RBAC.ClusterRoleBindings("").Controller().Lister(),
crLister: workload.RBAC.ClusterRoles("").Controller().Lister(),
nsLister: workload.Core.Namespaces("").Controller().Lister(),
nsController: workload.Core.Namespaces("").Controller(),
clusterLister: workload.Management.Management.Clusters("").Controller().Lister(),
projectLister: workload.Management.Management.Projects("").Controller().Lister(),
clusterName: workload.ClusterName,
}
workload.Management.Management.Projects("").AddClusterScopedLifecycle(ctx, "project-namespace-auth", workload.ClusterName, newProjectLifecycle(r))
workload.Management.Management.ProjectRoleTemplateBindings("").AddClusterScopedLifecycle(ctx, "cluster-prtb-sync", workload.ClusterName, newPRTBLifecycle(r))
workload.RBAC.ClusterRoleBindings("").AddHandler(ctx, "legacy-crb-cleaner-sync", newLegacyCRBCleaner(r).sync)
workload.Management.Management.ClusterRoleTemplateBindings("").AddClusterScopedLifecycle(ctx, "cluster-crtb-sync", workload.ClusterName, newCRTBLifecycle(r))
workload.Management.Management.Clusters("").AddHandler(ctx, "global-admin-cluster-sync", newClusterHandler(workload))
sync := &resourcequota.SyncController{
Namespaces: workload.Core.Namespaces(""),
NamespaceLister: workload.Core.Namespaces("").Controller().Lister(),
NsIndexer: nsInformer.GetIndexer(),
ResourceQuotas: workload.Core.ResourceQuotas(""),
ResourceQuotaLister: workload.Core.ResourceQuotas("").Controller().Lister(),
LimitRange: workload.Core.LimitRanges(""),
LimitRangeLister: workload.Core.LimitRanges("").Controller().Lister(),
ProjectLister: workload.Management.Management.Projects(workload.ClusterName).Controller().Lister(),
}
workload.Core.Namespaces("").AddLifecycle(ctx, "namespace-auth", newNamespaceLifecycle(r, sync))
// This method for creating a lifecycle creates a cluster scoped handler for a non-cluster scoped resource.
// This means that when the cluster is deleted, cleanup logic in the mgmt cluster will remove the finalizer added by this
// lifecycle, but during normal operation the events received by the handler will not be filtered to match the cluster
// (because the resource -roleTemplate and globalRoleBinding- are global resources)
rti := workload.Management.Management.RoleTemplates("")
rtSync := v3.NewRoleTemplateLifecycleAdapter("cluster-roletemplate-sync_"+workload.ClusterName, true, rti, newRTLifecycle(r))
workload.Management.Management.RoleTemplates("").AddHandler(ctx, "cluster-roletemplate-sync", rtSync)
grbi := workload.Management.Management.GlobalRoleBindings("")
grbSync := v3.NewGlobalRoleBindingLifecycleAdapter("grb-sync_"+workload.ClusterName, true, grbi, newGlobalRoleBindingHandler(workload))
workload.Management.Management.GlobalRoleBindings("").AddHandler(ctx, "grb-sync", grbSync)
}
type manager struct {
workload *config.UserContext
rtLister v3.RoleTemplateLister
prtbIndexer cache.Indexer
crtbIndexer cache.Indexer
nsIndexer cache.Indexer
crIndexer cache.Indexer
crbIndexer cache.Indexer
crLister typesrbacv1.ClusterRoleLister
crbLister typesrbacv1.ClusterRoleBindingLister
rbLister typesrbacv1.RoleBindingLister
nsLister typescorev1.NamespaceLister
nsController typescorev1.NamespaceController
clusterLister v3.ClusterLister
projectLister v3.ProjectLister
clusterName string
}
func (m *manager) ensureRoles(rts map[string]*v3.RoleTemplate) error {
roleCli := m.workload.RBAC.ClusterRoles("")
for _, rt := range rts {
if rt.External {
continue
}
if role, err := m.crLister.Get("", rt.Name); err == nil && role != nil {
if reflect.DeepEqual(role.Rules, rt.Rules) {
continue
}
role = role.DeepCopy()
role.Rules = rt.Rules
logrus.Infof("Updating clusterRole %v because of rules difference with roleTemplate %v (%v).", role.Name, rt.DisplayName, rt.Name)
_, err := roleCli.Update(role)
if err != nil {
return errors.Wrapf(err, "couldn't update role %v", rt.Name)
}
continue
}
logrus.Infof("Creating clusterRole for roleTemplate %v (%v).", rt.DisplayName, rt.Name)
_, err := roleCli.Create(&rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: rt.Name,
},
Rules: rt.Rules,
})
if err != nil {
return errors.Wrapf(err, "couldn't create role %v", rt.Name)
}
}
return nil
}
func (m *manager) gatherRoles(rt *v3.RoleTemplate, roleTemplates map[string]*v3.RoleTemplate) error {
err := m.gatherRolesRecurse(rt, roleTemplates)
if err != nil {
return err
}
// clean the roles for kubeneretes: lowercase resources and verbs
for key, rt := range roleTemplates {
if rt.External {
continue
}
rt = rt.DeepCopy()
var toLowerRules []rbacv1.PolicyRule
for _, r := range rt.Rules {
rule := r.DeepCopy()
var resources []string
for _, re := range r.Resources {
resources = append(resources, strings.ToLower(re))
}
rule.Resources = resources
var verbs []string
for _, v := range r.Verbs {
verbs = append(verbs, strings.ToLower(v))
}
rule.Verbs = verbs
toLowerRules = append(toLowerRules, *rule)
}
rt.Rules = toLowerRules
roleTemplates[key] = rt
}
return nil
}
func (m *manager) gatherRolesRecurse(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.gatherRoles(subRT, roleTemplates); err != nil {
return errors.Wrapf(err, "couldn't gather RoleTemplate %s", rtName)
}
}
return nil
}
func (m *manager) ensureClusterBindings(roles map[string]*v3.RoleTemplate, binding *v3.ClusterRoleTemplateBinding) error {
create := func(objectMeta metav1.ObjectMeta, subjects []rbacv1.Subject, roleRef rbacv1.RoleRef) runtime.Object {
return &rbacv1.ClusterRoleBinding{
ObjectMeta: objectMeta,
Subjects: subjects,
RoleRef: roleRef,
}
}
list := func(ns string, selector labels.Selector) ([]interface{}, error) {
currentRBs, err := m.crbLister.List(ns, selector)
if err != nil {
return nil, err
}
var items []interface{}
for _, c := range currentRBs {
items = append(items, c)
}
return items, nil
}
convert := func(i interface{}) (string, string, []rbacv1.Subject) {
crb, _ := i.(*rbacv1.ClusterRoleBinding)
return crb.Name, crb.RoleRef.Name, crb.Subjects
}
return m.ensureBindings("", roles, binding, m.workload.RBAC.ClusterRoleBindings("").ObjectClient(), create, list, convert)
}
func (m *manager) ensureProjectRoleBindings(ns string, roles map[string]*v3.RoleTemplate, binding *v3.ProjectRoleTemplateBinding) error {
create := func(objectMeta metav1.ObjectMeta, subjects []rbacv1.Subject, roleRef rbacv1.RoleRef) runtime.Object {
return &rbacv1.RoleBinding{
ObjectMeta: objectMeta,
Subjects: subjects,
RoleRef: roleRef,
}
}
list := func(ns string, selector labels.Selector) ([]interface{}, error) {
currentRBs, err := m.rbLister.List(ns, selector)
if err != nil {
return nil, err
}
var items []interface{}
for _, c := range currentRBs {
items = append(items, c)
}
return items, nil
}
convert := func(i interface{}) (string, string, []rbacv1.Subject) {
rb, _ := i.(*rbacv1.RoleBinding)
return rb.Name, rb.RoleRef.Name, rb.Subjects
}
return m.ensureBindings(ns, roles, binding, m.workload.RBAC.RoleBindings(ns).ObjectClient(), create, list, convert)
}
type createFn func(objectMeta metav1.ObjectMeta, subjects []rbacv1.Subject, roleRef rbacv1.RoleRef) runtime.Object
type listFn func(ns string, selector labels.Selector) ([]interface{}, error)
type convertFn func(i interface{}) (string, string, []rbacv1.Subject)
func (m *manager) ensureBindings(ns string, roles map[string]*v3.RoleTemplate, binding interface{}, client *objectclient.ObjectClient,
create createFn, list listFn, convert convertFn) error {
meta, err := meta.Accessor(binding)
if err != nil {
return err
}
desiredRBs := map[string]runtime.Object{}
subject, err := pkgrbac.BuildSubjectFromRTB(binding)
if err != nil {
return err
}
for roleName := range roles {
rbKey, objectMeta, subjects, roleRef := bindingParts(roleName, string(meta.GetUID()), subject)
desiredRBs[rbKey] = create(objectMeta, subjects, roleRef)
}
set := labels.Set(map[string]string{rtbOwnerLabel: string(meta.GetUID())})
currentRBs, err := list(ns, set.AsSelector())
if err != nil {
return err
}
rbsToDelete := map[string]bool{}
processed := map[string]bool{}
for _, rb := range currentRBs {
rbName, roleName, subjects := convert(rb)
// protect against an rb being in the list more than once (shouldn't happen, but just to be safe)
if ok := processed[rbName]; ok {
continue
}
processed[rbName] = true
if len(subjects) != 1 {
rbsToDelete[rbName] = true
continue
}
crbKey := rbRoleSubjectKey(roleName, subjects[0])
if _, ok := desiredRBs[crbKey]; ok {
delete(desiredRBs, crbKey)
} else {
rbsToDelete[rbName] = true
}
}
for key, rb := range desiredRBs {
logrus.Infof("Creating roleBinding %v", key)
switch roleBinding := rb.(type) {
case *rbacv1.RoleBinding:
_, err := m.workload.RBAC.RoleBindings(ns).Create(roleBinding)
if err != nil {
return err
}
case *rbacv1.ClusterRoleBinding:
_, err := m.workload.RBAC.ClusterRoleBindings("").Create(roleBinding)
if err != nil {
return err
}
}
}
for name := range rbsToDelete {
logrus.Infof("Deleting roleBinding %v", name)
if err := client.Delete(name, &metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return err
}
}
return nil
}
func bindingParts(roleName, parentUID string, subject rbacv1.Subject) (string, metav1.ObjectMeta, []rbacv1.Subject, rbacv1.RoleRef) {
crbKey := rbRoleSubjectKey(roleName, subject)
return crbKey,
metav1.ObjectMeta{
GenerateName: "clusterrolebinding-",
Labels: map[string]string{rtbOwnerLabel: parentUID},
},
[]rbacv1.Subject{subject},
rbacv1.RoleRef{
Kind: "ClusterRole",
Name: roleName,
}
}
func prtbByProjectName(obj interface{}) ([]string, error) {
prtb, ok := obj.(*v3.ProjectRoleTemplateBinding)
if !ok {
return []string{}, nil
}
return []string{prtb.ProjectName}, nil
}
func getPRTBProjectAndSubjectKey(prtb *v3.ProjectRoleTemplateBinding) string {
var name string
if prtb.UserName != "" {
name = prtb.UserName
} else if prtb.UserPrincipalName != "" {
name = prtb.UserPrincipalName
} else if prtb.GroupName != "" {
name = prtb.GroupName
} else if prtb.GroupPrincipalName != "" {
name = prtb.GroupPrincipalName
} else if prtb.ServiceAccount != "" {
name = prtb.ServiceAccount
}
return prtb.ProjectName + "." + name
}
func prtbByProjectAndSubject(obj interface{}) ([]string, error) {
prtb, ok := obj.(*v3.ProjectRoleTemplateBinding)
if !ok {
return []string{}, nil
}
return []string{getPRTBProjectAndSubjectKey(prtb)}, nil
}
func prtbByUID(obj interface{}) ([]string, error) {
prtb, ok := obj.(*v3.ProjectRoleTemplateBinding)
if !ok {
return []string{}, nil
}
return []string{convert.ToString(prtb.UID)}, nil
}
func crbRoleSubjectKeys(roleName string, subjects []rbacv1.Subject) []string {
var keys []string
for _, s := range subjects {
keys = append(keys, rbRoleSubjectKey(roleName, s))
}
return keys
}
func rbRoleSubjectKey(roleName string, subject rbacv1.Subject) string {
return subject.Kind + " " + subject.Name + " Role " + roleName
}
func crbByRoleAndSubject(obj interface{}) ([]string, error) {
crb, ok := obj.(*rbacv1.ClusterRoleBinding)
if !ok {
return []string{}, nil
}
return crbRoleSubjectKeys(crb.RoleRef.Name, crb.Subjects), nil
}
func rtbByClusterAndRoleTemplateName(obj interface{}) ([]string, error) {
var idx string
switch rtb := obj.(type) {
case *v3.ProjectRoleTemplateBinding:
if rtb.RoleTemplateName != "" && rtb.ProjectName != "" {
parts := strings.SplitN(rtb.ProjectName, ":", 2)
if len(parts) == 2 {
idx = parts[0] + "-" + rtb.RoleTemplateName
}
}
case *v3.ClusterRoleTemplateBinding:
if rtb.RoleTemplateName != "" && rtb.ClusterName != "" {
idx = rtb.ClusterName + "-" + rtb.RoleTemplateName
}
}
if idx == "" {
return []string{}, nil
}
return []string{idx}, nil
}