forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify_roles.go
379 lines (304 loc) · 11.8 KB
/
modify_roles.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
package policy
import (
"errors"
"fmt"
"io"
"github.com/spf13/cobra"
kapi "k8s.io/kubernetes/pkg/api"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
uservalidation "github.com/openshift/origin/pkg/user/api/validation"
)
const (
AddRoleToGroupRecommendedName = "add-role-to-group"
AddRoleToUserRecommendedName = "add-role-to-user"
RemoveRoleFromGroupRecommendedName = "remove-role-from-group"
RemoveRoleFromUserRecommendedName = "remove-role-from-user"
AddClusterRoleToGroupRecommendedName = "add-cluster-role-to-group"
AddClusterRoleToUserRecommendedName = "add-cluster-role-to-user"
RemoveClusterRoleFromGroupRecommendedName = "remove-cluster-role-from-group"
RemoveClusterRoleFromUserRecommendedName = "remove-cluster-role-from-user"
)
type RoleModificationOptions struct {
RoleNamespace string
RoleName string
RoleBindingAccessor RoleBindingAccessor
Users []string
Groups []string
Subjects []kapi.ObjectReference
}
// NewCmdAddRoleToGroup implements the OpenShift cli add-role-to-group command
func NewCmdAddRoleToGroup(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &RoleModificationOptions{}
cmd := &cobra.Command{
Use: name + " ROLE GROUP [GROUP ...]",
Short: "Add groups to a role in the current project",
Long: `Add groups to a role in the current project`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args, &options.Groups, "group", true); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.AddRole(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().StringVar(&options.RoleNamespace, "role-namespace", "", "namespace where the role is located: empty means a role defined in cluster policy")
return cmd
}
// NewCmdAddRoleToUser implements the OpenShift cli add-role-to-user command
func NewCmdAddRoleToUser(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &RoleModificationOptions{}
saNames := util.StringList{}
cmd := &cobra.Command{
Use: name + " ROLE USER [USER ...]",
Short: "Add users to a role in the current project",
Long: `Add users to a role in the current project`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.CompleteUserWithSA(f, args, saNames); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.AddRole(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().StringVar(&options.RoleNamespace, "role-namespace", "", "namespace where the role is located: empty means a role defined in cluster policy")
cmd.Flags().VarP(&saNames, "serviceaccount", "z", "service account in the current namespace to use as a user")
return cmd
}
// NewCmdRemoveRoleFromGroup implements the OpenShift cli remove-role-from-group command
func NewCmdRemoveRoleFromGroup(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &RoleModificationOptions{}
cmd := &cobra.Command{
Use: name + " ROLE GROUP [GROUP ...]",
Short: "Remove group from role in the current project",
Long: `Remove group from role in the current project`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args, &options.Groups, "group", true); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.RemoveRole(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().StringVar(&options.RoleNamespace, "role-namespace", "", "namespace where the role is located: empty means a role defined in cluster policy")
return cmd
}
// NewCmdRemoveRoleFromUser implements the OpenShift cli remove-role-from-user command
func NewCmdRemoveRoleFromUser(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &RoleModificationOptions{}
saNames := util.StringList{}
cmd := &cobra.Command{
Use: name + " ROLE USER [USER ...]",
Short: "Remove user from role in the current project",
Long: `Remove user from role in the current project`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.CompleteUserWithSA(f, args, saNames); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.RemoveRole(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().StringVar(&options.RoleNamespace, "role-namespace", "", "namespace where the role is located: empty means a role defined in cluster policy")
cmd.Flags().VarP(&saNames, "serviceaccount", "z", "service account in the current namespace to use as a user")
return cmd
}
// NewCmdAddClusterRoleToGroup implements the OpenShift cli add-cluster-role-to-group command
func NewCmdAddClusterRoleToGroup(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &RoleModificationOptions{}
cmd := &cobra.Command{
Use: name + " <role> <group> [group]...",
Short: "Add groups to a role for all projects in the cluster",
Long: `Add groups to a role for all projects in the cluster`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args, &options.Groups, "group", false); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.AddRole(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
return cmd
}
// NewCmdAddClusterRoleToUser implements the OpenShift cli add-cluster-role-to-user command
func NewCmdAddClusterRoleToUser(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &RoleModificationOptions{}
cmd := &cobra.Command{
Use: name + " <role> <user> [user]...",
Short: "Add users to a role for all projects in the cluster",
Long: `Add users to a role for all projects in the cluster`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args, &options.Users, "user", false); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.AddRole(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
return cmd
}
// NewCmdRemoveClusterRoleFromGroup implements the OpenShift cli remove-cluster-role-from-group command
func NewCmdRemoveClusterRoleFromGroup(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &RoleModificationOptions{}
cmd := &cobra.Command{
Use: name + " <role> <group> [group]...",
Short: "Remove group from role for all projects in the cluster",
Long: `Remove group from role for all projects in the cluster`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args, &options.Groups, "group", false); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.RemoveRole(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
return cmd
}
// NewCmdRemoveClusterRoleFromUser implements the OpenShift cli remove-cluster-role-from-user command
func NewCmdRemoveClusterRoleFromUser(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &RoleModificationOptions{}
cmd := &cobra.Command{
Use: name + " <role> <user> [user]...",
Short: "Remove user from role for all projects in the cluster",
Long: `Remove user from role for all projects in the cluster`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args, &options.Users, "user", false); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.RemoveRole(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
return cmd
}
func (o *RoleModificationOptions) CompleteUserWithSA(f *clientcmd.Factory, args []string, saNames util.StringList) error {
if (len(args) < 2) && (len(saNames) == 0) {
return errors.New("you must specify at least two arguments: <role> <user> [user]...")
}
o.RoleName = args[0]
if len(args) > 1 {
o.Users = append(o.Users, args[1:]...)
}
osClient, _, err := f.Clients()
if err != nil {
return err
}
roleBindingNamespace, _, err := f.DefaultNamespace()
if err != nil {
return err
}
o.RoleBindingAccessor = NewLocalRoleBindingAccessor(roleBindingNamespace, osClient)
for _, sa := range saNames {
o.Subjects = append(o.Subjects, kapi.ObjectReference{Name: sa, Kind: "ServiceAccount"})
}
return nil
}
func (o *RoleModificationOptions) Complete(f *clientcmd.Factory, args []string, target *[]string, targetName string, isNamespaced bool) error {
if len(args) < 2 {
return fmt.Errorf("you must specify at least two arguments: <role> <%s> [%s]...", targetName, targetName)
}
o.RoleName = args[0]
*target = append(*target, args[1:]...)
osClient, _, err := f.Clients()
if err != nil {
return err
}
if isNamespaced {
roleBindingNamespace, _, err := f.DefaultNamespace()
if err != nil {
return err
}
o.RoleBindingAccessor = NewLocalRoleBindingAccessor(roleBindingNamespace, osClient)
} else {
o.RoleBindingAccessor = NewClusterRoleBindingAccessor(osClient)
}
return nil
}
func (o *RoleModificationOptions) AddRole() error {
roleBindings, err := o.RoleBindingAccessor.GetExistingRoleBindingsForRole(o.RoleNamespace, o.RoleName)
if err != nil {
return err
}
roleBindingNames, err := o.RoleBindingAccessor.GetExistingRoleBindingNames()
if err != nil {
return err
}
var roleBinding *authorizationapi.RoleBinding
isUpdate := true
if len(roleBindings) == 0 {
roleBinding = &authorizationapi.RoleBinding{}
isUpdate = false
} else {
// only need to add the user or group to a single roleBinding on the role. Just choose the first one
roleBinding = roleBindings[0]
}
roleBinding.RoleRef.Namespace = o.RoleNamespace
roleBinding.RoleRef.Name = o.RoleName
newSubjects := authorizationapi.BuildSubjects(o.Users, o.Groups, uservalidation.ValidateUserName, uservalidation.ValidateGroupName)
newSubjects = append(newSubjects, o.Subjects...)
subjectCheck:
for _, newSubject := range newSubjects {
for _, existingSubject := range roleBinding.Subjects {
if existingSubject.Kind == newSubject.Kind &&
existingSubject.Name == newSubject.Name &&
existingSubject.Namespace == newSubject.Namespace {
continue subjectCheck
}
}
roleBinding.Subjects = append(roleBinding.Subjects, newSubject)
}
if isUpdate {
err = o.RoleBindingAccessor.UpdateRoleBinding(roleBinding)
} else {
roleBinding.Name = getUniqueName(o.RoleName, roleBindingNames)
err = o.RoleBindingAccessor.CreateRoleBinding(roleBinding)
}
if err != nil {
return err
}
return nil
}
func (o *RoleModificationOptions) RemoveRole() error {
roleBindings, err := o.RoleBindingAccessor.GetExistingRoleBindingsForRole(o.RoleNamespace, o.RoleName)
if err != nil {
return err
}
if len(roleBindings) == 0 {
return fmt.Errorf("unable to locate RoleBinding for %v/%v", o.RoleNamespace, o.RoleName)
}
subjectsToRemove := authorizationapi.BuildSubjects(o.Users, o.Groups, uservalidation.ValidateUserName, uservalidation.ValidateGroupName)
subjectsToRemove = append(subjectsToRemove, o.Subjects...)
for _, roleBinding := range roleBindings {
roleBinding.Subjects = removeSubjects(roleBinding.Subjects, subjectsToRemove)
err = o.RoleBindingAccessor.UpdateRoleBinding(roleBinding)
if err != nil {
return err
}
}
return nil
}
func removeSubjects(haystack, needles []kapi.ObjectReference) []kapi.ObjectReference {
newSubjects := []kapi.ObjectReference{}
existingLoop:
for _, existingSubject := range haystack {
for _, toRemove := range needles {
if existingSubject.Kind == toRemove.Kind &&
existingSubject.Name == toRemove.Name &&
existingSubject.Namespace == toRemove.Namespace {
continue existingLoop
}
}
newSubjects = append(newSubjects, existingSubject)
}
return newSubjects
}