forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_access_controller.go
309 lines (283 loc) · 10.9 KB
/
template_access_controller.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
package template
import (
"context"
"fmt"
"strings"
"k8s.io/apimachinery/pkg/runtime"
"github.com/rancher/types/apis/management.cattle.io/v3"
rrbacv1 "github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
"github.com/rancher/types/config"
log "github.com/sirupsen/logrus"
"k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/tools/cache"
)
const (
catalogTypeLabel = "catalog.cattle.io/catalog_type"
templateNameLabel = "catalog.cattle.io/template_name"
clusterRoleSuffix = "-use-templates-templateversions"
clusterRoleBindingSuffix = "-templates-templateversions-crb"
templateRule = "templates"
templateVersionRule = "templateversions"
)
// RBACTemplateManager controller manages access to the templates and templateversions of global, cluster and project level catalogs, via ClusterRoles and ClusterRoleBindings.
// A clusterRole is created for each cluster, and it includes all templates and templateversions of all cluster-level catalogs created within this cluster as rules
// These templates/templateversions are added via resourceNames
type RBACTemplateManager struct {
templateLister v3.TemplateLister
templateVersionLister v3.TemplateVersionLister
clusterCatalogLister v3.ClusterCatalogLister
clusterLister v3.ClusterLister
crtbLister v3.ClusterRoleTemplateBindingLister
projectCatalogLister v3.ProjectCatalogLister
projectLister v3.ProjectLister
prtbLister v3.ProjectRoleTemplateBindingLister
crbClient rrbacv1.ClusterRoleBindingInterface
crbLister rrbacv1.ClusterRoleBindingLister
clusterRoleClient rrbacv1.ClusterRoleInterface
clusterRoleLister rrbacv1.ClusterRoleLister
globalRoleClient v3.GlobalRoleInterface
}
func Register(ctx context.Context, management *config.ManagementContext) {
t := RBACTemplateManager{
templateLister: management.Management.Templates("").Controller().Lister(),
templateVersionLister: management.Management.TemplateVersions("").Controller().Lister(),
clusterCatalogLister: management.Management.ClusterCatalogs("").Controller().Lister(),
clusterLister: management.Management.Clusters("").Controller().Lister(),
crtbLister: management.Management.ClusterRoleTemplateBindings("").Controller().Lister(),
projectCatalogLister: management.Management.ProjectCatalogs("").Controller().Lister(),
prtbLister: management.Management.ProjectRoleTemplateBindings("").Controller().Lister(),
projectLister: management.Management.Projects("").Controller().Lister(),
crbClient: management.RBAC.ClusterRoleBindings(""),
crbLister: management.RBAC.ClusterRoleBindings("").Controller().Lister(),
clusterRoleClient: management.RBAC.ClusterRoles(""),
clusterRoleLister: management.RBAC.ClusterRoles("").Controller().Lister(),
globalRoleClient: management.Management.GlobalRoles(""),
}
management.Management.Catalogs("").Controller().AddHandler(ctx, "createGlobalTemplateBinding", t.syncForGlobalCatalog)
management.Management.ClusterCatalogs("").Controller().AddHandler(ctx, "createClusterTemplateBinding", t.syncForClusterCatalog)
management.Management.ProjectCatalogs("").Controller().AddHandler(ctx, "createProjectTemplateBinding", t.syncForProjectCatalog)
management.Management.ClusterRoleTemplateBindings("").Controller().AddHandler(ctx, "addUserToClusterCatalog", t.syncCRBT)
management.Management.ProjectRoleTemplateBindings("").Controller().AddHandler(ctx, "addUserToProjectCatalog", t.syncPRTB)
management.Management.Clusters("").Controller().AddHandler(ctx, "syncClusters", t.syncCluster)
management.Management.Projects("").Controller().AddHandler(ctx, "syncProjects", t.syncProject)
}
func buildSubjectFromRTB(binding interface{}) (v1.Subject, bool, error) {
var userName, groupPrincipalName, groupName, name, kind string
if rtb, ok := binding.(*v3.ProjectRoleTemplateBinding); ok {
userName = rtb.UserName
groupPrincipalName = rtb.GroupPrincipalName
groupName = rtb.GroupName
// only create subjects to add to the projectcatalog's templates bindings, if the roletemplate on the current rtb should have access to project catalogs
if !rolesWithAccessToProjectCatalogTemplates[rtb.RoleTemplateName] {
return v1.Subject{}, false, nil
}
} else if rtb, ok := binding.(*v3.ClusterRoleTemplateBinding); ok {
userName = rtb.UserName
groupPrincipalName = rtb.GroupPrincipalName
groupName = rtb.GroupName
// only create subjects to add to the clustercatalog's templates bindings, if the roletemplate on the current rtb should have access to cluster catalogs
if !rolesWithAccessToClusterCatalogTemplates[rtb.RoleTemplateName] {
return v1.Subject{}, false, nil
}
} else {
return v1.Subject{}, false, fmt.Errorf("unrecognized roleTemplateBinding type: %v", binding)
}
if userName != "" {
name = userName
kind = "User"
}
if groupPrincipalName != "" {
if name != "" {
return v1.Subject{}, false, fmt.Errorf("roletemplatebinding has more than one subject fields set: %v", binding)
}
name = groupPrincipalName
kind = "Group"
}
if groupName != "" {
if name != "" {
return v1.Subject{}, false, fmt.Errorf("roletemplatebinding has more than one subject fields set: %v", binding)
}
name = groupName
kind = "Group"
}
if name == "" {
return v1.Subject{}, false, fmt.Errorf("roletemplatebinding doesn't have any subject fields set: %v", binding)
}
return v1.Subject{
Kind: kind,
Name: name,
}, true, nil
}
func (tm *RBACTemplateManager) getTemplateAndTemplateVersions(r *labels.Requirement) (tempArray []string, tempVersionArray []string, err error) {
templates, err := tm.templateLister.List("", labels.NewSelector().Add(*r))
if len(templates) == 0 {
return []string{}, []string{}, nil
}
if err != nil {
return []string{}, []string{}, nil
}
tempArray = []string{}
for _, t := range templates {
tempArray = append(tempArray, t.Name)
}
//get template versions for these templates
selector := labels.NewSelector()
newSelector := labels.NewSelector()
for _, t := range templates {
r, err := labels.NewRequirement(templateNameLabel, selection.Equals, []string{t.Name})
if err != nil {
return []string{}, []string{}, nil
}
selector = selector.Add(*r)
templateVersions, err := tm.templateVersionLister.List("", selector)
for _, tv := range templateVersions {
tempVersionArray = append(tempVersionArray, tv.Name)
}
selector = newSelector
}
return tempArray, tempVersionArray, err
}
func (tm *RBACTemplateManager) syncForClusterCatalog(key string, obj *v3.ClusterCatalog) (runtime.Object, error) {
if obj == nil || obj.DeletionTimestamp != nil {
// cluster catalog deleted, reconcile this cluster
clusterName, _, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return nil, err
}
return nil, tm.reconcileClusterCatalog(clusterName)
}
return nil, tm.reconcileClusterCatalog(obj.ClusterName)
}
func (tm *RBACTemplateManager) syncForProjectCatalog(key string, obj *v3.ProjectCatalog) (runtime.Object, error) {
if obj == nil || obj.DeletionTimestamp != nil {
// project catalog deleted, reconcile this project
projectName, _, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return nil, err
}
// find cluster this project belongs to
clusters, err := tm.clusterLister.List("", labels.NewSelector())
for _, c := range clusters {
projects, err := tm.projectLister.List(c.Name, labels.NewSelector())
if err != nil {
return nil, err
}
for _, p := range projects {
if p.Name == projectName {
if err = tm.reconcileProjectCatalog(projectName, c.Name); err != nil {
return nil, err
}
}
}
}
return nil, nil
}
split := strings.SplitN(obj.ProjectName, ":", 2)
if len(split) != 2 {
return nil, nil
}
clusterName, projectName := split[0], split[1]
return nil, tm.reconcileProjectCatalog(projectName, clusterName)
}
func (tm *RBACTemplateManager) syncCRBT(key string, obj *v3.ClusterRoleTemplateBinding) (runtime.Object, error) {
var clusterName string
var err error
if obj == nil || obj.DeletionTimestamp != nil {
// crtb is being removed, remove the user of this crtb from any CRBs for the templates
if key == "" {
return nil, nil
}
clusterName, _, err = cache.SplitMetaNamespaceKey(key)
if err != nil {
return nil, err
}
} else {
clusterName = obj.ClusterName
}
return nil, tm.reconcileClusterCatalog(clusterName)
}
func (tm *RBACTemplateManager) syncPRTB(key string, obj *v3.ProjectRoleTemplateBinding) (runtime.Object, error) {
var clusterName, projectName string
if obj == nil || obj.DeletionTimestamp != nil {
// reconcileClusterCatalog all clusters as it's impossible to find cluster that this PRTB belongs to,
// we need to remove this user from CRB for templates of the project too
clusters, err := tm.clusterLister.List("", labels.NewSelector())
if err != nil {
return nil, err
}
for _, c := range clusters {
if err = tm.reconcileClusterCatalog(c.Name); err != nil {
return nil, err
}
}
if key == "" {
return nil, nil
}
projectName, _, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return nil, err
}
for _, c := range clusters {
projects, err := tm.projectLister.List(c.Name, labels.NewSelector())
if err != nil {
return nil, err
}
for _, p := range projects {
if p.Name == projectName {
if err = tm.reconcileProjectCatalog(projectName, c.Name); err != nil {
return nil, err
}
}
}
}
} else {
pName := strings.SplitN(obj.ProjectName, ":", 2)
if len(pName) != 2 {
log.Errorf("Project name incorrect")
return nil, fmt.Errorf("project name incorrect")
}
clusterName, projectName = pName[0], pName[1]
if err := tm.reconcileClusterCatalog(clusterName); err != nil {
return nil, err
}
return nil, tm.reconcileProjectCatalog(projectName, clusterName)
}
return nil, nil
}
func (tm *RBACTemplateManager) syncCluster(key string, obj *v3.Cluster) (runtime.Object, error) {
if key == "" && obj == nil {
return nil, nil
}
return nil, tm.reconcileClusterCatalog(key)
}
func (tm *RBACTemplateManager) syncProject(key string, obj *v3.Project) (runtime.Object, error) {
if key == "" && obj == nil {
return nil, nil
}
split := strings.SplitN(key, "/", 2)
if len(split) != 2 {
return nil, nil
}
clusterName, projectName := split[0], split[1]
return nil, tm.reconcileProjectCatalog(projectName, clusterName)
}
func (tm *RBACTemplateManager) updateCRB(crb *v1.ClusterRoleBinding, subjects []v1.Subject) error {
crbToUpdate := crb.DeepCopy()
crbToUpdate.Subjects = subjects
_, err := tm.crbClient.Update(crbToUpdate)
if err != nil {
return err
}
return nil
}
func (tm *RBACTemplateManager) updateClusterRole(cRole *v1.ClusterRole, rules []v1.PolicyRule) error {
cRoleToUpdate := cRole.DeepCopy()
cRoleToUpdate.Rules = rules
_, err := tm.clusterRoleClient.Update(cRoleToUpdate)
if err != nil {
return err
}
return nil
}