forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolebuilder.go
329 lines (274 loc) · 8.93 KB
/
rolebuilder.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
package app
import (
"fmt"
"reflect"
"github.com/pkg/errors"
"github.com/rancher/norman/objectclient"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
var defaultGRLabel = map[string]string{"authz.management.cattle.io/bootstrapping": "default-globalrole"}
var defaultRTLabel = map[string]string{"authz.management.cattle.io/bootstrapping": "default-roletemplate"}
type roleBuilder struct {
previous *roleBuilder
next *roleBuilder
name string
displayName string
context string
builtin bool
external bool
hidden bool
roleTemplateNames []string
rules []*ruleBuilder
}
func (rb *roleBuilder) String() string {
return fmt.Sprintf("%s (%s): %s", rb.displayName, rb.name, rb.rules)
}
func newRoleBuilder() *roleBuilder {
return &roleBuilder{}
}
func (rb *roleBuilder) addRoleTemplate(displayName, name, context string, builtin, external, hidden bool) *roleBuilder {
r := rb.addRole(displayName, name)
r.context = context
r.builtin = builtin
r.external = external
r.hidden = hidden
return r
}
func (rb *roleBuilder) addRole(displayName, name string) *roleBuilder {
if rb.name == "" {
rb.name = name
rb.displayName = displayName
return rb
}
if rb.next != nil {
return rb.next.addRole(displayName, name)
}
rb.next = &roleBuilder{
name: name,
displayName: displayName,
previous: rb,
}
return rb.next
}
func (rb *roleBuilder) addRule() *ruleBuilder {
r := &ruleBuilder{
rb: rb,
}
rb.rules = append(rb.rules, r)
return r
}
func (rb *roleBuilder) setRoleTemplateNames(names ...string) *roleBuilder {
rb.roleTemplateNames = names
return rb
}
func (rb *roleBuilder) first() *roleBuilder {
if rb.previous == nil {
return rb
}
return rb.previous.first()
}
func (rb *roleBuilder) policyRules() []rbacv1.PolicyRule {
var prs []rbacv1.PolicyRule
for _, r := range rb.rules {
prs = append(prs, r.toPolicyRule())
}
return prs
}
type ruleBuilder struct {
rb *roleBuilder
_verbs []string
_resources []string
_resourceNames []string
_apiGroups []string
_urls []string
}
func (r *ruleBuilder) String() string {
return fmt.Sprintf("apigroups: %v, resource: %v, resourceNames: %v, nonResourceURLs %v, verbs: %v", r._apiGroups, r._resources, r._resourceNames, r._urls, r._verbs)
}
func (r *ruleBuilder) verbs(v ...string) *ruleBuilder {
r._verbs = append(r._verbs, v...)
return r
}
func (r *ruleBuilder) resources(rc ...string) *ruleBuilder {
r._resources = append(r._resources, rc...)
return r
}
func (r *ruleBuilder) resourceNames(rn ...string) *ruleBuilder {
r._resourceNames = append(r._resourceNames, rn...)
return r
}
func (r *ruleBuilder) apiGroups(a ...string) *ruleBuilder {
r._apiGroups = append(r._apiGroups, a...)
return r
}
func (r *ruleBuilder) nonResourceURLs(u ...string) *ruleBuilder {
r._urls = append(r._urls, u...)
return r
}
func (r *ruleBuilder) addRoleTemplate(diplsayName, name, context string, builtin, external, hidden bool) *roleBuilder {
return r.rb.addRoleTemplate(diplsayName, name, context, builtin, external, hidden)
}
func (r *ruleBuilder) addRole(diplsayName, name string) *roleBuilder {
return r.rb.addRole(diplsayName, name)
}
func (r *ruleBuilder) addRule() *ruleBuilder {
return r.rb.addRule()
}
func (r *ruleBuilder) setRoleTemplateNames(names ...string) *roleBuilder {
return r.rb.setRoleTemplateNames(names...)
}
func (r *ruleBuilder) reconcileGlobalRoles(mgmt *config.ManagementContext) error {
return r.rb.reconcileGlobalRoles(mgmt)
}
func (r *ruleBuilder) reconcileRoleTemplates(mgmt *config.ManagementContext) error {
return r.rb.reconcileRoleTemplates(mgmt)
}
func (r *ruleBuilder) toPolicyRule() rbacv1.PolicyRule {
return rbacv1.PolicyRule{
APIGroups: r._apiGroups,
Resources: r._resources,
ResourceNames: r._resourceNames,
NonResourceURLs: r._urls,
Verbs: r._verbs,
}
}
type buildFnc func(thisRB *roleBuilder) (string, runtime.Object)
type compareAndModifyFnc func(have runtime.Object, want runtime.Object) (bool, runtime.Object, error)
type gatherExistingFnc func() (map[string]runtime.Object, error)
func (rb *roleBuilder) reconcile(build buildFnc, gatherExisting gatherExistingFnc, compareAndModify compareAndModifyFnc, client *objectclient.ObjectClient) error {
current := rb.first()
builtRoles := map[string]runtime.Object{}
for current != nil {
name, role := build(current)
builtRoles[name] = role
current = current.next
}
existing, err := gatherExisting()
if err != nil {
return err
}
for name := range existing {
if _, ok := builtRoles[name]; !ok {
logrus.Infof("Remvoing %v", name)
if err := client.Delete(name, &v1.DeleteOptions{}); err != nil {
return errors.Wrapf(err, "couldn't delete %v", name)
}
delete(existing, name)
}
}
for name, gr := range builtRoles {
if existingCR, ok := existing[name]; ok {
equal, modified, err := compareAndModify(existingCR, gr)
if err != nil {
return err
}
if !equal {
if _, err := client.Update(name, modified); err != nil {
return errors.Wrapf(err, "couldn't update %v", name)
}
}
continue
}
logrus.Infof("Creating %v", name)
if _, err := client.Create(gr); err != nil {
return errors.Wrapf(err, "couldn't create %v", name)
}
}
return nil
}
func (rb *roleBuilder) reconcileGlobalRoles(mgmt *config.ManagementContext) error {
logrus.Info("Reconciling GlobalRoles")
build := func(current *roleBuilder) (string, runtime.Object) {
gr := &v3.GlobalRole{
ObjectMeta: v1.ObjectMeta{
Name: current.name,
Labels: defaultGRLabel,
},
DisplayName: current.displayName,
Rules: current.policyRules(),
}
return gr.Name, gr
}
grCli := mgmt.Management.GlobalRoles("")
gather := func() (map[string]runtime.Object, error) {
set := labels.Set(defaultGRLabel)
existingList, err := grCli.List(v1.ListOptions{LabelSelector: set.String()})
if err != nil {
return nil, errors.Wrapf(err, "couldn't list globalRoles with selector %s", set)
}
existing := map[string]runtime.Object{}
for _, e := range existingList.Items {
existing[e.Name] = e.DeepCopy()
}
return existing, nil
}
compareAndMod := func(have runtime.Object, want runtime.Object) (bool, runtime.Object, error) {
haveGR, ok := have.(*v3.GlobalRole)
wantGR, ok2 := want.(*v3.GlobalRole)
if !ok || !ok2 {
return false, nil, errors.Errorf("unexpected type comparing %v and %v", have, want)
}
equal := haveGR.DisplayName == wantGR.DisplayName && reflect.DeepEqual(haveGR.Rules, wantGR.Rules)
haveGR.DisplayName = wantGR.DisplayName
haveGR.Rules = wantGR.Rules
return equal, haveGR, nil
}
return rb.reconcile(build, gather, compareAndMod, grCli.ObjectClient())
}
func (rb *roleBuilder) reconcileRoleTemplates(mgmt *config.ManagementContext) error {
logrus.Info("Reconciling RoleTemplates")
build := func(current *roleBuilder) (string, runtime.Object) {
role := &v3.RoleTemplate{
ObjectMeta: v1.ObjectMeta{
Name: current.name,
Labels: defaultRTLabel,
},
DisplayName: current.displayName,
Builtin: current.builtin,
External: current.external,
Hidden: current.hidden,
Context: current.context,
Rules: current.policyRules(),
RoleTemplateNames: current.roleTemplateNames,
}
return role.Name, role
}
client := mgmt.Management.RoleTemplates("")
gather := func() (map[string]runtime.Object, error) {
set := labels.Set(defaultRTLabel)
existingList, err := client.List(v1.ListOptions{LabelSelector: set.String()})
if err != nil {
return nil, errors.Wrapf(err, "couldn't list roleTemplate with selector %s", set)
}
existing := map[string]runtime.Object{}
for _, e := range existingList.Items {
existing[e.Name] = e.DeepCopy()
}
return existing, nil
}
compareAndMod := func(have runtime.Object, want runtime.Object) (bool, runtime.Object, error) {
haveRT, ok := have.(*v3.RoleTemplate)
wantRT, ok2 := want.(*v3.RoleTemplate)
if !ok || !ok2 {
return false, nil, errors.Errorf("unexpected type comparing %v and %v", have, want)
}
equal := haveRT.DisplayName == wantRT.DisplayName && reflect.DeepEqual(haveRT.Rules, wantRT.Rules) &&
reflect.DeepEqual(haveRT.RoleTemplateNames, wantRT.RoleTemplateNames) && haveRT.Builtin != wantRT.Builtin &&
haveRT.External == wantRT.External && haveRT.Hidden == wantRT.Hidden && haveRT.Context == wantRT.Context
haveRT.DisplayName = wantRT.DisplayName
haveRT.Rules = wantRT.Rules
haveRT.RoleTemplateNames = wantRT.RoleTemplateNames
haveRT.Builtin = wantRT.Builtin
haveRT.External = wantRT.External
haveRT.Hidden = wantRT.Hidden
haveRT.Context = wantRT.Context
return equal, haveRT, nil
}
return rb.reconcile(build, gather, compareAndMod, client.ObjectClient())
}