forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prune_command.go
181 lines (149 loc) · 5.22 KB
/
prune_command.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
package auth
import (
"io"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
rbacv1client "k8s.io/client-go/kubernetes/typed/rbac/v1"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
authclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset"
securitytypedclient "github.com/openshift/origin/pkg/security/generated/internalclientset/typed/security/internalversion"
userclient "github.com/openshift/origin/pkg/user/generated/internalclientset"
)
// PruneRolesOptions holds all the required options for pruning roles.
type PruneAuthOptions struct {
FilenameOptions resource.FilenameOptions
Selector string
All bool
Builder *resource.Builder
RoleBindingClient rbacv1client.RoleBindingsGetter
ClusterRoleBindingClient rbacv1client.ClusterRoleBindingsGetter
// TODO switch these to external clients
UserInternalClient userclient.Interface
AuthorizationInternalClient authclient.Interface
OAuthInternalClient oauthclient.Interface
SCCClient securitytypedclient.SecurityInterface
Out io.Writer
}
// NewCmdPruneRoles implements the OpenShift cli prune roles command.
func NewCmdPruneAuth(f kcmdutil.Factory, name string, streams genericclioptions.IOStreams) *cobra.Command {
o := &PruneAuthOptions{
Out: streams.Out,
}
cmd := &cobra.Command{
Use: name,
Short: "Removes references to the specified roles, clusterroles, users, and groups.",
Long: "Removes references to the specified roles, clusterroles, users, and groups. Other types are ignored",
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(o.Complete(f, cmd, args))
kcmdutil.CheckErr(o.RunPrune())
},
}
kcmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, "containing the resource to delete.")
cmd.Flags().StringVarP(&o.Selector, "selector", "l", "", "Selector (label query) to filter on.")
cmd.Flags().BoolVar(&o.All, "all", o.All, "Prune all roles in the namespace.")
return cmd
}
func (o *PruneAuthOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []string) error {
var err error
clientConfig, err := f.ToRESTConfig()
if err != nil {
return nil
}
o.RoleBindingClient, err = rbacv1client.NewForConfig(clientConfig)
if err != nil {
return nil
}
o.ClusterRoleBindingClient, err = rbacv1client.NewForConfig(clientConfig)
if err != nil {
return nil
}
o.UserInternalClient, err = userclient.NewForConfig(clientConfig)
if err != nil {
return nil
}
o.AuthorizationInternalClient, err = authclient.NewForConfig(clientConfig)
if err != nil {
return nil
}
o.OAuthInternalClient, err = oauthclient.NewForConfig(clientConfig)
if err != nil {
return nil
}
o.SCCClient, err = securitytypedclient.NewForConfig(clientConfig)
if err != nil {
return nil
}
cmdNamespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
o.Builder = f.NewBuilder().
Unstructured().
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.FilenameOptions).
LabelSelectorParam(o.Selector).
SelectAllParam(o.All).
ResourceTypeOrNameArgs(false, args...).
RequireObject(false).
Flatten()
return nil
}
func (o *PruneAuthOptions) RunPrune() error {
r := o.Builder.Do()
if r.Err() != nil {
return r.Err()
}
// this is weird, but we do this here for easy compatibility with existing reapers. This command doesn't make sense
// without a server connection. Still. Don't do this at home.
err := r.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
switch {
case isRole(info.Mapping):
reapForRole(o.RoleBindingClient, info.Namespace, info.Name, o.Out)
case isClusterRole(info.Mapping):
reapForClusterRole(o.ClusterRoleBindingClient, o.RoleBindingClient, info.Namespace, info.Name, o.Out)
case isUser(info.Mapping):
reapForUser(o.UserInternalClient, o.AuthorizationInternalClient, o.OAuthInternalClient, o.SCCClient.SecurityContextConstraints(), info.Name, o.Out)
case isGroup(info.Mapping):
reapForGroup(o.AuthorizationInternalClient, o.SCCClient.SecurityContextConstraints(), info.Name, o.Out)
}
return nil
})
return err
}
func isRole(mapping *meta.RESTMapping) bool {
if mapping.Resource.Group != "rbac.authorization.k8s.io" && mapping.Resource.Group != "authorization.openshift.io" {
return false
}
if mapping.Resource.Resource != "roles" {
return false
}
return true
}
func isClusterRole(mapping *meta.RESTMapping) bool {
if mapping.Resource.Group != "rbac.authorization.k8s.io" && mapping.Resource.Group != "authorization.openshift.io" {
return false
}
if mapping.Resource.Resource != "clusterroles" {
return false
}
return true
}
func isUser(mapping *meta.RESTMapping) bool {
if mapping.Resource.Group == "user.openshift.io" && mapping.Resource.Resource == "users" {
return true
}
return false
}
func isGroup(mapping *meta.RESTMapping) bool {
if mapping.Resource.Group == "user.openshift.io" && mapping.Resource.Resource == "groups" {
return true
}
return false
}