forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
role_data.go
292 lines (244 loc) · 18.8 KB
/
role_data.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
package app
import (
"github.com/pkg/errors"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"golang.org/x/crypto/bcrypt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
var defaultAdminLabel = map[string]string{"authz.management.cattle.io/bootstrapping": "admin-user"}
func addRoles(management *config.ManagementContext) (string, error) {
rb := newRoleBuilder()
rb.addRole("Create Clusters", "clusters-create").addRule().apiGroups("management.cattle.io").resources("clusters").verbs("create").
addRule().apiGroups("management.cattle.io").resources("templates", "templateversions").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("nodedrivers").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("podsecuritypolicytemplates").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("nodetemplates").verbs("*")
rb.addRole("Manage Node Drivers", "nodedrivers-manage").addRule().apiGroups("management.cattle.io").resources("nodedrivers").verbs("*")
rb.addRole("Manage Catalogs", "catalogs-manage").addRule().apiGroups("management.cattle.io").resources("catalogs", "templates", "templateversions").verbs("*")
rb.addRole("Use Catalog Templates", "catalogs-use").addRule().apiGroups("management.cattle.io").resources("templates", "templateversions").verbs("get", "list", "watch")
rb.addRole("Manage Users", "users-manage").addRule().apiGroups("management.cattle.io").resources("users", "globalroles", "globalrolebindings").verbs("*")
rb.addRole("Manage Roles", "roles-manage").addRule().apiGroups("management.cattle.io").resources("roletemplates").verbs("*")
rb.addRole("Manage Authentication", "authn-manage").addRule().apiGroups("management.cattle.io").resources("authconfigs").verbs("get", "list", "watch", "update")
rb.addRole("Manage Settings", "settings-manage").addRule().apiGroups("management.cattle.io").resources("settings").verbs("*")
rb.addRole("Manage PodSecurityPolicy Templates", "podsecuritypolicytemplates-manage").addRule().apiGroups("management.cattle.io").resources("podsecuritypolicytemplates").verbs("*")
rb.addRole("Admin", "admin").addRule().apiGroups("*").resources("*").verbs("*").
addRule().apiGroups().nonResourceURLs("*").verbs("*")
rb.addRole("User", "user").addRule().apiGroups("management.cattle.io").resources("principals", "roletemplates").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("users").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("preferences").verbs("*").
addRule().apiGroups("management.cattle.io").resources("settings").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusters").verbs("create").
addRule().apiGroups("management.cattle.io").resources("templates", "templateversions").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("nodedrivers").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("podsecuritypolicytemplates").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("nodetemplates").verbs("*").
addRule().apiGroups("management.cattle.io").resources("sourcecodecredentials").verbs("*").
addRule().apiGroups("management.cattle.io").resources("sourcecoderepositories").verbs("*")
rb.addRole("User Base", "user-base").addRule().apiGroups("management.cattle.io").resources("principals", "roletemplates").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("users").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("preferences").verbs("*").
addRule().apiGroups("management.cattle.io").resources("settings").verbs("get", "list", "watch")
// TODO user should be dynamically authorized to only see herself
// TODO Need "self-service" for nodetemplates such that a user can create them, but only RUD their own
// TODO enable when groups are "in". they need to be self-service
if err := rb.reconcileGlobalRoles(management); err != nil {
return "", errors.Wrap(err, "problem reconciling globl roles")
}
// RoleTemplates to be used inside of clusters
rb = newRoleBuilder()
// K8s default roles
rb.addRoleTemplate("Kubernetes cluster-admin", "cluster-admin", "cluster", true, true, true)
rb.addRoleTemplate("Kubernetes admin", "admin", "project", true, true, true)
rb.addRoleTemplate("Kubernetes edit", "edit", "project", true, true, true)
rb.addRoleTemplate("Kubernetes view", "view", "project", true, true, true)
// Cluster roles
rb.addRoleTemplate("Cluster Owner", "cluster-owner", "cluster", true, false, false).
addRule().apiGroups("*").resources("*").verbs("*").
addRule().apiGroups().nonResourceURLs("*").verbs("*")
rb.addRoleTemplate("Cluster Member", "cluster-member", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("clusterroletemplatebindings").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("projects").verbs("create").
addRule().apiGroups("management.cattle.io").resources("nodes", "nodepools").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("nodes").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("persistentvolumes").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("storageclasses").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterpipelines").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterloggings").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusteralerts").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("notifiers").verbs("get", "list", "watch")
rb.addRoleTemplate("Create Projects", "projects-create", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projects").verbs("create")
rb.addRoleTemplate("View All Projects", "projects-view", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projects").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("get", "list", "watch").
addRule().apiGroups("project.cattle.io").resources("apps").verbs("get", "list", "watch").
addRule().apiGroups("").resources("namespaces").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("persistentvolumes").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("storageclasses").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("persistentvolumeclaims").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch").
setRoleTemplateNames("view")
rb.addRoleTemplate("Manage Nodes", "nodes-manage", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("nodes", "nodepools").verbs("*").
addRule().apiGroups("*").resources("nodes").verbs("*")
rb.addRoleTemplate("View Nodes", "nodes-view", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("nodes", "nodepools").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("nodes").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Storage", "storage-manage", "cluster", true, false, false).
addRule().apiGroups("*").resources("persistentvolumes").verbs("*").
addRule().apiGroups("*").resources("storageclasses").verbs("*").
addRule().apiGroups("*").resources("persistentvolumeclaims").verbs("*")
rb.addRoleTemplate("Manage Cluster Members", "clusterroletemplatebindings-manage", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("clusterroletemplatebindings").verbs("*")
rb.addRoleTemplate("View Cluster Members", "clusterroletemplatebindings-view", "cluster", true, false, false).
addRule().apiGroups("management.cattle.io").resources("clusterroletemplatebindings").verbs("get", "list", "watch")
// Project roles
rb.addRoleTemplate("Project Owner", "project-owner", "project", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("*").
addRule().apiGroups("project.cattle.io").resources("apps").verbs("*").
addRule().apiGroups("").resources("namespaces").verbs("create").
addRule().apiGroups("*").resources("persistentvolumes").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("storageclasses").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("persistentvolumeclaims").verbs("*").
addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterpipelines").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("pipelines").verbs("*").
addRule().apiGroups("management.cattle.io").resources("pipelineexecutions").verbs("*").
addRule().apiGroups("management.cattle.io").resources("pipelineexecutionlogs").verbs("*").
addRule().apiGroups("management.cattle.io").resources("notifiers").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("projectalerts").verbs("*").
addRule().apiGroups("management.cattle.io").resources("projectloggings").verbs("*").
setRoleTemplateNames("admin")
rb.addRoleTemplate("Project Member", "project-member", "project", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("get", "list", "watch").
addRule().apiGroups("project.cattle.io").resources("apps").verbs("*").
addRule().apiGroups("").resources("namespaces").verbs("create").
addRule().apiGroups("*").resources("persistentvolumes").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("storageclasses").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("persistentvolumeclaims").verbs("*").
addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterpipelines").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("pipelines").verbs("*").
addRule().apiGroups("management.cattle.io").resources("pipelineexecutions").verbs("*").
addRule().apiGroups("management.cattle.io").resources("pipelineexecutionlogs").verbs("*").
addRule().apiGroups("management.cattle.io").resources("notifiers").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("projectalerts").verbs("*").
addRule().apiGroups("management.cattle.io").resources("projectloggings").verbs("get", "list", "watch").
setRoleTemplateNames("edit")
rb.addRoleTemplate("Read-only", "read-only", "project", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("get", "list", "watch").
addRule().apiGroups("project.cattle.io").resources("apps").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("persistentvolumes").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("storageclasses").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("persistentvolumeclaims").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("clusterpipelines").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("pipelines").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("pipelineexecutions").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("pipelineexecutionlogs").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("notifiers").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("projectalerts").verbs("get", "list", "watch").
addRule().apiGroups("management.cattle.io").resources("projectloggings").verbs("get", "list", "watch").
setRoleTemplateNames("view")
rb.addRoleTemplate("Create Namespaces", "create-ns", "project", true, false, false).
addRule().apiGroups("").resources("namespaces").verbs("create")
rb.addRoleTemplate("Manage Workloads", "workloads-manage", "project", true, false, false).
addRule().apiGroups("*").resources("pods", "pods/attach", "pods/exec", "pods/portforward", "pods/proxy", "replicationcontrollers",
"replicationcontrollers/scale", "daemonsets", "deployments", "deployments/rollback", "deployments/scale", "replicasets",
"replicasets/scale", "statefulsets", "cronjobs", "jobs", "daemonsets", "deployments", "deployments/rollback", "deployments/scale",
"replicasets", "replicasets/scale", "replicationcontrollers/scale", "horizontalpodautoscalers").verbs("*").
addRule().apiGroups("*").resources("limitranges", "pods/log", "pods/status", "replicationcontrollers/status", "resourcequotas", "resourcequotas/status", "bindings").verbs("get", "list", "watch").
addRule().apiGroups("project.cattle.io").resources("apps").verbs("*")
rb.addRoleTemplate("View Workloads", "workloads-view", "project", true, false, false).
addRule().apiGroups("*").resources("pods", "pods/attach", "pods/exec", "pods/portforward", "pods/proxy", "replicationcontrollers",
"replicationcontrollers/scale", "daemonsets", "deployments", "deployments/rollback", "deployments/scale", "replicasets",
"replicasets/scale", "statefulsets", "cronjobs", "jobs", "daemonsets", "deployments", "deployments/rollback", "deployments/scale",
"replicasets", "replicasets/scale", "replicationcontrollers/scale", "horizontalpodautoscalers").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("limitranges", "pods/log", "pods/status", "replicationcontrollers/status", "resourcequotas", "resourcequotas/status", "bindings").verbs("get", "list", "watch").
addRule().apiGroups("project.cattle.io").resources("apps").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Ingress", "ingress-manage", "project", true, false, false).
addRule().apiGroups("*").resources("ingresses").verbs("*")
rb.addRoleTemplate("View Ingress", "ingress-view", "project", true, false, false).
addRule().apiGroups("*").resources("ingresses").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Services", "services-manage", "project", true, false, false).
addRule().apiGroups("*").resources("services", "endpoints").verbs("*")
rb.addRoleTemplate("View Services", "services-view", "project", true, false, false).
addRule().apiGroups("*").resources("services", "endpoints").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Secrets", "secrets-manage", "project", true, false, false).
addRule().apiGroups("*").resources("secrets").verbs("*")
rb.addRoleTemplate("View Secrets", "secrets-view", "project", true, false, false).
addRule().apiGroups("*").resources("secrets").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Config Maps", "configmaps-manage", "project", true, false, false).
addRule().apiGroups("*").resources("configmaps").verbs("*")
rb.addRoleTemplate("View Config Maps", "configmaps-view", "project", true, false, false).
addRule().apiGroups("*").resources("configmaps").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Volumes", "persistentvolumeclaims-manage", "project", true, false, false).
addRule().apiGroups("*").resources("persistentvolumes").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("storageclasses").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("persistentvolumeclaims").verbs("*")
rb.addRoleTemplate("View Volumes", "persistentvolumeclaims-view", "project", true, false, false).
addRule().apiGroups("*").resources("persistentvolumes").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("storageclasses").verbs("get", "list", "watch").
addRule().apiGroups("*").resources("persistentvolumeclaims").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Service Accounts", "serviceaccounts-manage", "project", true, false, false).
addRule().apiGroups("*").resources("serviceaccounts").verbs("*")
rb.addRoleTemplate("View Service Accounts", "serviceaccounts-view", "project", true, false, false).
addRule().apiGroups("*").resources("serviceaccounts").verbs("get", "list", "watch")
rb.addRoleTemplate("Manage Project Members", "projectroletemplatebindings-manage", "project", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("*")
rb.addRoleTemplate("View Project Members", "projectroletemplatebindings-view", "project", true, false, false).
addRule().apiGroups("management.cattle.io").resources("projectroletemplatebindings").verbs("get", "list", "watch")
// Not specific to project or cluster
// TODO When clusterevents has value, consider adding this back in
//rb.addRoleTemplate("View Events", "events-view", "", true, false, false).
// addRule().apiGroups("*").resources("events").verbs("get", "list", "watch").
// addRule().apiGroups("management.cattle.io").resources("clusterevents").verbs("get", "list", "watch")
if err := rb.reconcileRoleTemplates(management); err != nil {
return "", errors.Wrap(err, "problem reconciling role templates")
}
hash, _ := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
set := labels.Set(defaultAdminLabel)
admins, err := management.Management.Users("").List(v1.ListOptions{LabelSelector: set.String()})
if err != nil {
return "", err
}
// TODO This logic is going to be a problem in an HA setup because a race will cause more than one admin user to be created
var admin *v3.User
if len(admins.Items) == 0 {
admin, err = management.Management.Users("").Create(&v3.User{
ObjectMeta: v1.ObjectMeta{
GenerateName: "user-",
Labels: defaultAdminLabel,
},
DisplayName: "Default Admin",
Username: "admin",
Password: string(hash),
MustChangePassword: true,
})
if err != nil && !apierrors.IsAlreadyExists(err) {
return "", errors.Wrap(err, "can not ensure admin user exists")
}
} else {
admin = &admins.Items[0]
}
bindings, err := management.Management.GlobalRoleBindings("").List(v1.ListOptions{LabelSelector: set.String()})
if err != nil {
return "", err
}
if len(bindings.Items) == 0 {
management.Management.GlobalRoleBindings("").Create(
&v3.GlobalRoleBinding{
ObjectMeta: v1.ObjectMeta{
GenerateName: "globalrolebinding-",
Labels: defaultAdminLabel,
},
UserName: admin.Name,
GlobalRoleName: "admin",
})
}
return admin.Name, nil
}