-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
remove_from_project.go
149 lines (120 loc) · 4.63 KB
/
remove_from_project.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
package policy
import (
"fmt"
"io"
"sort"
"github.com/spf13/cobra"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const (
RemoveGroupRecommendedName = "remove-group"
RemoveUserRecommendedName = "remove-user"
)
type RemoveFromProjectOptions struct {
BindingNamespace string
Client client.Interface
Groups []string
Users []string
Out io.Writer
}
// NewCmdRemoveGroupFromProject implements the OpenShift cli remove-group command
func NewCmdRemoveGroupFromProject(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &RemoveFromProjectOptions{Out: out}
cmd := &cobra.Command{
Use: name + " GROUP [GROUP ...]",
Short: "Remove group from the current project",
Long: `Remove group from the current project`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args, &options.Groups, "group"); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.Run(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
return cmd
}
// NewCmdRemoveUserFromProject implements the OpenShift cli remove-user command
func NewCmdRemoveUserFromProject(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &RemoveFromProjectOptions{Out: out}
cmd := &cobra.Command{
Use: name + " USER [USER ...]",
Short: "Remove user from the current project",
Long: `Remove user from the current project`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args, &options.Users, "user"); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.Run(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
return cmd
}
func (o *RemoveFromProjectOptions) Complete(f *clientcmd.Factory, args []string, target *[]string, targetName string) error {
if len(args) < 1 {
return fmt.Errorf("You must specify at least one argument: <%s> [%s]...", targetName, targetName)
}
*target = append(*target, args...)
var err error
if o.Client, _, err = f.Clients(); err != nil {
return err
}
if o.BindingNamespace, err = f.DefaultNamespace(); err != nil {
return err
}
return nil
}
func (o *RemoveFromProjectOptions) Run() error {
bindingList, err := o.Client.PolicyBindings(o.BindingNamespace).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
sort.Sort(authorizationapi.PolicyBindingSorter(bindingList.Items))
usersRemoved := util.StringSet{}
groupsRemoved := util.StringSet{}
for _, currPolicyBinding := range bindingList.Items {
for _, currBinding := range authorizationapi.SortRoleBindings(currPolicyBinding.RoleBindings, true) {
bindingHasGroups := len(o.Groups) > 0 && currBinding.Groups.HasAny(o.Groups...)
bindingHasUsers := len(o.Users) > 0 && currBinding.Users.HasAny(o.Users...)
if !bindingHasGroups && !bindingHasUsers {
continue
}
existingGroups := util.NewStringSet(currBinding.Groups.List()...)
existingUsers := util.NewStringSet(currBinding.Users.List()...)
currBinding.Groups.Delete(o.Groups...)
currBinding.Users.Delete(o.Users...)
_, err = o.Client.RoleBindings(o.BindingNamespace).Update(currBinding)
if err != nil {
return err
}
roleDisplayName := fmt.Sprintf("%s/%s", currBinding.RoleRef.Namespace, currBinding.RoleRef.Name)
if len(currBinding.RoleRef.Namespace) == 0 {
roleDisplayName = currBinding.RoleRef.Name
}
if diff := existingGroups.Difference(currBinding.Groups); len(diff) != 0 {
fmt.Fprintf(o.Out, "Removing %s from groups %v in project %s.\n", roleDisplayName, diff.List(), o.BindingNamespace)
groupsRemoved.Insert(diff.List()...)
}
if diff := existingUsers.Difference(currBinding.Users); len(diff) != 0 {
fmt.Fprintf(o.Out, "Removing %s from users %v in project %s.\n", roleDisplayName, diff.List(), o.BindingNamespace)
usersRemoved.Insert(diff.List()...)
}
}
}
if diff := util.NewStringSet(o.Groups...).Difference(groupsRemoved); len(diff) != 0 {
fmt.Fprintf(o.Out, "Groups %v were not bound to roles in project %s.\n", diff.List(), o.BindingNamespace)
}
if diff := util.NewStringSet(o.Users...).Difference(usersRemoved); len(diff) != 0 {
fmt.Fprintf(o.Out, "Users %v were not bound to roles in project %s.\n", diff.List(), o.BindingNamespace)
}
return nil
}