Skip to content

Commit 9f864f2

Browse files
committed
Make some policy commands behave "better"
Instead of deprecating add/remove-role commands, change them to behave better. On add: do not add to a random rolebinding, always create a new rolebinding if none was specified explicitly. On Remove: if a rolebinding name is specified remove only from it. Signed-off-by: Simo Sorce <[email protected]>
1 parent bd4b691 commit 9f864f2

File tree

2 files changed

+207
-36
lines changed

2 files changed

+207
-36
lines changed

pkg/oc/admin/policy/modify_roles.go

+25-27
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ func NewCmdAddRoleToGroup(name, fullName string, f *clientcmd.Factory, out io.Wr
8080
printSuccessForCommand(options.RoleName, true, "group", options.Targets, true, options.DryRun, out)
8181
}
8282
},
83-
Deprecated: fmt.Sprintf("Use oc edit rolebinding"),
8483
}
8584

86-
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify or create. If left empty, appends to the first rolebinding found for the given role")
85+
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify or create. If left empty creates a new rolebinding with a default name")
8786
cmd.Flags().StringVar(&options.RoleNamespace, "role-namespace", "", "namespace where the role is located: empty means a role defined in cluster policy")
8887

8988
kcmdutil.AddDryRunFlag(cmd)
@@ -117,7 +116,7 @@ func NewCmdAddRoleToUser(name, fullName string, f *clientcmd.Factory, out io.Wri
117116
Deprecated: fmt.Sprintf("Use oc edit rolebinding"),
118117
}
119118

120-
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify or create. If left empty, appends to the first rolebinding found for the given role")
119+
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify or create. If left empty creates a new rolebinding with a default name")
121120
cmd.Flags().StringVar(&options.RoleNamespace, "role-namespace", "", "namespace where the role is located: empty means a role defined in cluster policy")
122121
cmd.Flags().StringSliceVarP(&saNames, "serviceaccount", "z", saNames, "service account in the current namespace to use as a user")
123122

@@ -147,9 +146,9 @@ func NewCmdRemoveRoleFromGroup(name, fullName string, f *clientcmd.Factory, out
147146
printSuccessForCommand(options.RoleName, false, "group", options.Targets, true, options.DryRun, out)
148147
}
149148
},
150-
Deprecated: fmt.Sprintf("Use oc edit rolebinding"),
151149
}
152150

151+
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify. If left empty it will operate on all rolebindings")
153152
cmd.Flags().StringVar(&options.RoleNamespace, "role-namespace", "", "namespace where the role is located: empty means a role defined in cluster policy")
154153

155154
kcmdutil.AddDryRunFlag(cmd)
@@ -179,9 +178,9 @@ func NewCmdRemoveRoleFromUser(name, fullName string, f *clientcmd.Factory, out i
179178
printSuccessForCommand(options.RoleName, false, "user", options.Targets, true, options.DryRun, out)
180179
}
181180
},
182-
Deprecated: fmt.Sprintf("Use oc edit rolebinding"),
183181
}
184182

183+
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify. If left empty it will operate on all rolebindings")
185184
cmd.Flags().StringVar(&options.RoleNamespace, "role-namespace", "", "namespace where the role is located: empty means a role defined in cluster policy")
186185
cmd.Flags().StringSliceVarP(&saNames, "serviceaccount", "z", saNames, "service account in the current namespace to use as a user")
187186

@@ -211,10 +210,9 @@ func NewCmdAddClusterRoleToGroup(name, fullName string, f *clientcmd.Factory, ou
211210
printSuccessForCommand(options.RoleName, true, "group", options.Targets, false, options.DryRun, out)
212211
}
213212
},
214-
Deprecated: fmt.Sprintf("Use oc edit clusterrolebinding"),
215213
}
216214

217-
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify or create. If left empty, appends to the first rolebinding found for the given role")
215+
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify or create. If left empty creates a new rolebinding with a default name")
218216
kcmdutil.AddDryRunFlag(cmd)
219217
kcmdutil.AddPrinterFlags(cmd)
220218
return cmd
@@ -242,10 +240,9 @@ func NewCmdAddClusterRoleToUser(name, fullName string, f *clientcmd.Factory, out
242240
printSuccessForCommand(options.RoleName, true, "user", options.Targets, false, options.DryRun, out)
243241
}
244242
},
245-
Deprecated: fmt.Sprintf("Use oc edit clusterrolebinding"),
246243
}
247244

248-
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify or create. If left empty, appends to the first rolebinding found for the given role")
245+
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify or create. If left empty creates a new rolebinding with a default name")
249246
cmd.Flags().StringSliceVarP(&saNames, "serviceaccount", "z", saNames, "service account in the current namespace to use as a user")
250247

251248
kcmdutil.AddDryRunFlag(cmd)
@@ -274,9 +271,10 @@ func NewCmdRemoveClusterRoleFromGroup(name, fullName string, f *clientcmd.Factor
274271
printSuccessForCommand(options.RoleName, false, "group", options.Targets, false, options.DryRun, out)
275272
}
276273
},
277-
Deprecated: fmt.Sprintf("Use oc edit clusterrolebinding"),
278274
}
279275

276+
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify. If left empty it will operate on all rolebindings")
277+
280278
kcmdutil.AddDryRunFlag(cmd)
281279
kcmdutil.AddPrinterFlags(cmd)
282280
return cmd
@@ -304,9 +302,9 @@ func NewCmdRemoveClusterRoleFromUser(name, fullName string, f *clientcmd.Factory
304302
printSuccessForCommand(options.RoleName, false, "user", options.Targets, false, options.DryRun, out)
305303
}
306304
},
307-
Deprecated: fmt.Sprintf("Use oc edit clusterrolebinding"),
308305
}
309306

307+
cmd.Flags().StringVar(&options.RoleBindingName, "rolebinding-name", "", "Name of the rolebinding to modify. If left empty it will operate on all rolebindings")
310308
cmd.Flags().StringSliceVarP(&saNames, "serviceaccount", "z", saNames, "service account in the current namespace to use as a user")
311309

312310
kcmdutil.AddDryRunFlag(cmd)
@@ -436,18 +434,7 @@ func (o *RoleModificationOptions) getUserSpecifiedBinding() (*authorizationapi.R
436434
}
437435

438436
func (o *RoleModificationOptions) getUnspecifiedBinding() (*authorizationapi.RoleBinding, bool /* isUpdate */, error) {
439-
// Look for existing bindings by role.
440-
roleBindings, err := o.RoleBindingAccessor.GetExistingRoleBindingsForRole(o.RoleNamespace, o.RoleName)
441-
if err != nil {
442-
return nil, false, err
443-
}
444-
445-
if len(roleBindings) > 0 {
446-
// only need to add the user or group to a single roleBinding on the role. Just choose the first one
447-
return roleBindings[0], true, nil
448-
}
449-
450-
// Create a new rolebinding with the default naming.
437+
// Always create a new role binding with the default naming
451438
roleBinding := &authorizationapi.RoleBinding{}
452439
roleBindingNames, err := o.RoleBindingAccessor.GetExistingRoleBindingNames()
453440
if err != nil {
@@ -520,9 +507,20 @@ subjectCheck:
520507
}
521508

522509
func (o *RoleModificationOptions) RemoveRole() error {
523-
roleBindings, err := o.RoleBindingAccessor.GetExistingRoleBindingsForRole(o.RoleNamespace, o.RoleName)
524-
if err != nil {
525-
return err
510+
var roleBindings []*authorizationapi.RoleBinding
511+
var err error
512+
if len(o.RoleBindingName) > 0 {
513+
existingRoleBinding, err := o.RoleBindingAccessor.GetRoleBinding(o.RoleBindingName)
514+
if err != nil {
515+
return err
516+
}
517+
roleBindings = make([]*authorizationapi.RoleBinding, 1)
518+
roleBindings[0] = existingRoleBinding
519+
} else {
520+
roleBindings, err = o.RoleBindingAccessor.GetExistingRoleBindingsForRole(o.RoleNamespace, o.RoleName)
521+
if err != nil {
522+
return err
523+
}
526524
}
527525
if len(roleBindings) == 0 {
528526
return fmt.Errorf("unable to locate RoleBinding for %v/%v", o.RoleNamespace, o.RoleName)
@@ -554,7 +552,7 @@ func (o *RoleModificationOptions) RemoveRole() error {
554552
for _, roleBinding := range roleBindings {
555553
roleBinding.Subjects = removeSubjects(roleBinding.Subjects, subjectsToRemove)
556554

557-
err = o.RoleBindingAccessor.UpdateRoleBinding(roleBinding)
555+
err := o.RoleBindingAccessor.UpdateRoleBinding(roleBinding)
558556
if err != nil {
559557
return err
560558
}

0 commit comments

Comments
 (0)