forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crtb_handler.go
167 lines (144 loc) · 5.4 KB
/
crtb_handler.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
package auth
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/runtime"
"github.com/pkg/errors"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
const (
clusterResource = "clusters"
membershipBindingOwner = "memberhsip-binding-owner"
crtbInProjectBindingOwner = "crtb-in-project-binding-owner"
prtbInClusterBindingOwner = "prtb-in-cluster-binding-owner"
rbByOwnerIndex = "auth.management.cattle.io/rb-by-owner"
rbByRoleAndSubjectIndex = "auth.management.cattle.io/crb-by-role-and-subject"
ctrbMGMTController = "mgmt-auth-crtb-controller"
)
var clusterManagmentPlaneResources = []string{"clusterroletemplatebindings", "nodes", "nodepools", "clusterevents",
"projects", "clusterregistrationtokens", "clusterloggings", "notifiers", "clusteralertrules", "clusteralertgroups",
"podsecuritypolicytemplateprojectbindings", "clustercatalogs", "clustermonitorgraphs"}
type crtbLifecycle struct {
mgr *manager
clusterLister v3.ClusterLister
}
func (c *crtbLifecycle) Create(obj *v3.ClusterRoleTemplateBinding) (runtime.Object, error) {
obj, err := c.reconcileSubject(obj)
if err != nil {
return nil, err
}
err = c.reconcilBindings(obj)
return obj, err
}
func (c *crtbLifecycle) Updated(obj *v3.ClusterRoleTemplateBinding) (runtime.Object, error) {
obj, err := c.reconcileSubject(obj)
if err != nil {
return nil, err
}
err = c.reconcilBindings(obj)
return obj, err
}
func (c *crtbLifecycle) Remove(obj *v3.ClusterRoleTemplateBinding) (runtime.Object, error) {
if err := c.mgr.reconcileClusterMembershipBindingForDelete("", string(obj.UID)); err != nil {
return nil, err
}
err := c.removeMGMTClusterScopedPrivilegesInProjectNamespace(obj)
return nil, err
}
func (c *crtbLifecycle) reconcileSubject(binding *v3.ClusterRoleTemplateBinding) (*v3.ClusterRoleTemplateBinding, error) {
if binding.GroupName != "" || binding.GroupPrincipalName != "" || (binding.UserPrincipalName != "" && binding.UserName != "") {
return binding, nil
}
if binding.UserPrincipalName != "" && binding.UserName == "" {
displayName := binding.Annotations["auth.cattle.io/principal-display-name"]
user, err := c.mgr.userMGR.EnsureUser(binding.UserPrincipalName, displayName)
if err != nil {
return binding, err
}
binding.UserName = user.Name
return binding, nil
}
if binding.UserPrincipalName == "" && binding.UserName != "" {
u, err := c.mgr.userLister.Get("", binding.UserName)
if err != nil {
return binding, err
}
for _, p := range u.PrincipalIDs {
if strings.HasSuffix(p, binding.UserName) {
binding.UserPrincipalName = p
break
}
}
return binding, nil
}
return nil, errors.Errorf("Binding %v has no subject", binding.Name)
}
// When a CRTB is created or updated, translate it into several k8s roles and bindings to actually enforce the RBAC
// Specifically:
// - ensure the subject can see the cluster in the mgmt API
// - if the subject was granted owner permissions for the clsuter, ensure they can create/update/delete the cluster
// - if the subject was granted privileges to mgmt plane resources that are scoped to the cluster, enforce those rules in the cluster's mgmt plane namespace
func (c *crtbLifecycle) reconcilBindings(binding *v3.ClusterRoleTemplateBinding) error {
if binding.UserName == "" && binding.GroupPrincipalName == "" && binding.GroupName == "" {
return nil
}
clusterName := binding.ClusterName
cluster, err := c.clusterLister.Get("", clusterName)
if err != nil {
return err
}
if cluster == nil {
return errors.Errorf("cannot create binding because cluster %v was not found", clusterName)
}
isOwnerRole := binding.RoleTemplateName == "cluster-owner"
var clusterRoleName string
if isOwnerRole {
clusterRoleName = strings.ToLower(fmt.Sprintf("%v-clusterowner", clusterName))
} else {
clusterRoleName = strings.ToLower(fmt.Sprintf("%v-clustermember", clusterName))
}
subject, err := buildSubjectFromRTB(binding)
if err != nil {
return err
}
if err := c.mgr.ensureClusterMembershipBinding(clusterRoleName, string(binding.UID), cluster, isOwnerRole, subject); err != nil {
return err
}
err = c.mgr.grantManagementPlanePrivileges(binding.RoleTemplateName, clusterManagmentPlaneResources, subject, binding)
if err != nil {
return err
}
projects, err := c.mgr.projectLister.List(binding.Namespace, labels.Everything())
if err != nil {
return err
}
for _, p := range projects {
if err := c.mgr.grantManagementClusterScopedPrivilegesInProjectNamespace(binding.RoleTemplateName, p.Name, projectManagmentPlaneResources, subject, binding); err != nil {
return err
}
}
return nil
}
func (c *crtbLifecycle) removeMGMTClusterScopedPrivilegesInProjectNamespace(binding *v3.ClusterRoleTemplateBinding) error {
projects, err := c.mgr.projectLister.List(binding.Namespace, labels.Everything())
if err != nil {
return err
}
for _, p := range projects {
set := labels.Set(map[string]string{string(binding.UID): crtbInProjectBindingOwner})
rbs, err := c.mgr.rbLister.List(p.Name, set.AsSelector())
if err != nil {
return err
}
for _, rb := range rbs {
logrus.Infof("[%v] Deleting rolebinding %v in namespace %v for crtb %v", ctrbMGMTController, rb.Name, p.Name, binding.Name)
if err := c.mgr.mgmt.RBAC.RoleBindings(p.Name).Delete(rb.Name, &v1.DeleteOptions{}); err != nil {
return err
}
}
}
return nil
}