-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
modify_scc.go
233 lines (183 loc) · 6.19 KB
/
modify_scc.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
package policy
import (
"errors"
"io"
"github.com/spf13/cobra"
kapi "k8s.io/kubernetes/pkg/api"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/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 (
AddSCCToGroupRecommendedName = "add-scc-to-group"
AddSCCToUserRecommendedName = "add-scc-to-user"
RemoveSCCFromGroupRecommendedName = "remove-scc-from-group"
RemoveSCCFromUserRecommendedName = "remove-scc-from-user"
)
type SCCModificationOptions struct {
SCCName string
SCCInterface kclient.SecurityContextConstraintsInterface
DefaultSubjectNamespace string
Subjects []kapi.ObjectReference
}
func NewCmdAddSCCToGroup(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &SCCModificationOptions{}
cmd := &cobra.Command{
Use: name + " SCC GROUP [GROUP ...]",
Short: "Add groups to a security context constraint",
Long: `Add groups to a security context constraint`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.CompleteGroups(f, args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.AddSCC(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
return cmd
}
func NewCmdAddSCCToUser(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &SCCModificationOptions{}
saNames := []string{}
cmd := &cobra.Command{
Use: name + " SCC USER [USER ...]",
Short: "Add users to a security context constraint",
Long: `Add users to a security context constraint`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.CompleteUsers(f, args, saNames); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.AddSCC(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().StringSliceVarP(&saNames, "serviceaccount", "z", saNames, "service account in the current namespace to use as a user")
return cmd
}
func NewCmdRemoveSCCFromGroup(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &SCCModificationOptions{}
cmd := &cobra.Command{
Use: name + " SCC GROUP [GROUP ...]",
Short: "Remove group from scc",
Long: `Remove group from scc`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.CompleteGroups(f, args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.RemoveSCC(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
return cmd
}
func NewCmdRemoveSCCFromUser(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &SCCModificationOptions{}
saNames := []string{}
cmd := &cobra.Command{
Use: name + " SCC USER [USER ...]",
Short: "Remove user from scc",
Long: `Remove user from scc`,
Run: func(cmd *cobra.Command, args []string) {
if err := options.CompleteUsers(f, args, saNames); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.RemoveSCC(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().StringSliceVarP(&saNames, "serviceaccount", "z", saNames, "service account in the current namespace to use as a user")
return cmd
}
func (o *SCCModificationOptions) CompleteUsers(f *clientcmd.Factory, args []string, saNames []string) error {
if (len(args) < 2) && (len(saNames) == 0) {
return errors.New("you must specify at least two arguments (<scc> <user> [user]...) or a service account (<scc> -z <service account name>) ")
}
o.SCCName = args[0]
o.Subjects = authorizationapi.BuildSubjects(args[1:], []string{}, uservalidation.ValidateUserName, uservalidation.ValidateGroupName)
var err error
_, o.SCCInterface, err = f.Clients()
if err != nil {
return err
}
o.DefaultSubjectNamespace, _, err = f.DefaultNamespace()
if err != nil {
return err
}
for _, sa := range saNames {
o.Subjects = append(o.Subjects, kapi.ObjectReference{Namespace: o.DefaultSubjectNamespace, Name: sa, Kind: "ServiceAccount"})
}
return nil
}
func (o *SCCModificationOptions) CompleteGroups(f *clientcmd.Factory, args []string) error {
if len(args) < 2 {
return errors.New("you must specify at least two arguments: <scc> <group> [group]...")
}
o.SCCName = args[0]
o.Subjects = authorizationapi.BuildSubjects([]string{}, args[1:], uservalidation.ValidateUserName, uservalidation.ValidateGroupName)
var err error
_, o.SCCInterface, err = f.Clients()
if err != nil {
return err
}
o.DefaultSubjectNamespace, _, err = f.DefaultNamespace()
if err != nil {
return err
}
return nil
}
func (o *SCCModificationOptions) AddSCC() error {
scc, err := o.SCCInterface.SecurityContextConstraints().Get(o.SCCName)
if err != nil {
return err
}
users, groups := authorizationapi.StringSubjectsFor(o.DefaultSubjectNamespace, o.Subjects)
usersToAdd, _ := diff(users, scc.Users)
groupsToAdd, _ := diff(groups, scc.Groups)
scc.Users = append(scc.Users, usersToAdd...)
scc.Groups = append(scc.Groups, groupsToAdd...)
_, err = o.SCCInterface.SecurityContextConstraints().Update(scc)
if err != nil {
return err
}
return nil
}
func (o *SCCModificationOptions) RemoveSCC() error {
scc, err := o.SCCInterface.SecurityContextConstraints().Get(o.SCCName)
if err != nil {
return err
}
users, groups := authorizationapi.StringSubjectsFor(o.DefaultSubjectNamespace, o.Subjects)
_, remainingUsers := diff(users, scc.Users)
_, remainingGroups := diff(groups, scc.Groups)
scc.Users = remainingUsers
scc.Groups = remainingGroups
_, err = o.SCCInterface.SecurityContextConstraints().Update(scc)
if err != nil {
return err
}
return nil
}
func diff(lhsSlice, rhsSlice []string) (lhsOnly []string, rhsOnly []string) {
return singleDiff(lhsSlice, rhsSlice), singleDiff(rhsSlice, lhsSlice)
}
func singleDiff(lhsSlice, rhsSlice []string) (lhsOnly []string) {
for _, lhs := range lhsSlice {
found := false
for _, rhs := range rhsSlice {
if lhs == rhs {
found = true
break
}
}
if !found {
lhsOnly = append(lhsOnly, lhs)
}
}
return lhsOnly
}