forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
modify_scc.go
336 lines (268 loc) · 8.88 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
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
package policy
import (
"errors"
"fmt"
"io"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kapi "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
securityclientinternal "github.com/openshift/origin/pkg/security/generated/internalclientset"
securitytypedclient "github.com/openshift/origin/pkg/security/generated/internalclientset/typed/security/internalversion"
)
const (
AddSCCToGroupRecommendedName = "add-scc-to-group"
AddSCCToUserRecommendedName = "add-scc-to-user"
RemoveSCCFromGroupRecommendedName = "remove-scc-from-group"
RemoveSCCFromUserRecommendedName = "remove-scc-from-user"
)
var (
addSCCToUserExample = templates.Examples(`
# Add the 'restricted' security context contraint to user1 and user2
%[1]s restricted user1 user2
# Add the 'privileged' security context contraint to the service account serviceaccount1 in the current namespace
%[1]s privileged -z serviceaccount1`)
)
type SCCModificationOptions struct {
SCCName string
SCCInterface securitytypedclient.SecurityContextConstraintsInterface
DefaultSubjectNamespace string
Subjects []kapi.ObjectReference
IsGroup bool
DryRun bool
Output string
PrintObj func(runtime.Object) error
Out io.Writer
}
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, cmd, args, out); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageErrorf(cmd, err.Error()))
}
if err := options.AddSCC(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
kcmdutil.AddDryRunFlag(cmd)
kcmdutil.AddPrinterFlags(cmd)
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 | -z SERVICEACCOUNT) [USER ...]",
Short: "Add users or serviceaccount to a security context constraint",
Long: `Add users or serviceaccount to a security context constraint`,
Example: fmt.Sprintf(addSCCToUserExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
if err := options.CompleteUsers(f, cmd, args, saNames, out); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageErrorf(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")
kcmdutil.AddDryRunFlag(cmd)
kcmdutil.AddPrinterFlags(cmd)
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, cmd, args, out); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageErrorf(cmd, err.Error()))
}
if err := options.RemoveSCC(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
kcmdutil.AddDryRunFlag(cmd)
kcmdutil.AddPrinterFlags(cmd)
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, cmd, args, saNames, out); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageErrorf(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")
kcmdutil.AddDryRunFlag(cmd)
kcmdutil.AddPrinterFlags(cmd)
return cmd
}
func (o *SCCModificationOptions) CompleteUsers(f *clientcmd.Factory, cmd *cobra.Command, args []string, saNames []string, out io.Writer) error {
if len(args) < 1 {
return errors.New("you must specify a scc")
}
o.Out = out
o.SCCName = args[0]
o.Subjects = authorizationapi.BuildSubjects(args[1:], []string{})
if (len(o.Subjects) == 0) && (len(saNames) == 0) {
return errors.New("you must specify at least one user or service account")
}
o.DryRun = kcmdutil.GetFlagBool(cmd, "dry-run")
o.Output = kcmdutil.GetFlagString(cmd, "output")
o.PrintObj = func(obj runtime.Object) error {
return kcmdutil.PrintObject(cmd, obj, out)
}
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
securityClient, err := securityclientinternal.NewForConfig(clientConfig)
if err != nil {
return err
}
o.SCCInterface = securityClient.Security().SecurityContextConstraints()
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, cmd *cobra.Command, args []string, out io.Writer) error {
if len(args) < 2 {
return errors.New("you must specify at least two arguments: <scc> <group> [group]...")
}
o.Out = out
o.Output = kcmdutil.GetFlagString(cmd, "output")
o.PrintObj = func(obj runtime.Object) error {
return kcmdutil.PrintObject(cmd, obj, out)
}
o.IsGroup = true
o.SCCName = args[0]
o.Subjects = authorizationapi.BuildSubjects([]string{}, args[1:])
o.DryRun = kcmdutil.GetFlagBool(cmd, "dry-run")
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
securityClient, err := securityclientinternal.NewForConfig(clientConfig)
if err != nil {
return err
}
o.SCCInterface = securityClient.Security().SecurityContextConstraints()
o.DefaultSubjectNamespace, _, err = f.DefaultNamespace()
if err != nil {
return err
}
return nil
}
func (o *SCCModificationOptions) AddSCC() error {
scc, err := o.SCCInterface.Get(o.SCCName, metav1.GetOptions{})
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...)
if len(o.Output) > 0 && o.PrintObj != nil {
return o.PrintObj(scc)
}
if o.DryRun {
printSuccess(o.SCCName, true, o.IsGroup, users, groups, o.DryRun, o.Out)
return nil
}
_, err = o.SCCInterface.Update(scc)
if err != nil {
return err
}
printSuccess(o.SCCName, true, o.IsGroup, users, groups, o.DryRun, o.Out)
return nil
}
func (o *SCCModificationOptions) RemoveSCC() error {
scc, err := o.SCCInterface.Get(o.SCCName, metav1.GetOptions{})
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
if len(o.Output) > 0 && o.PrintObj != nil {
return o.PrintObj(scc)
}
if o.DryRun {
printSuccess(o.SCCName, false, o.IsGroup, users, groups, o.DryRun, o.Out)
return nil
}
_, err = o.SCCInterface.Update(scc)
if err != nil {
return err
}
printSuccess(o.SCCName, false, o.IsGroup, users, groups, o.DryRun, o.Out)
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
}
// prints affirmative output
func printSuccess(scc string, didAdd bool, isGroup bool, usersToAdd, groupsToAdd []string, dryRun bool, out io.Writer) {
verb := "removed from"
allTargets := fmt.Sprintf("%q", usersToAdd)
dryRunText := ""
if isGroup {
allTargets = fmt.Sprintf("%q", groupsToAdd)
}
if didAdd {
verb = "added to"
}
if isGroup {
verb += " groups"
}
msg := "scc %q %s: %s%s"
if dryRun {
dryRunText = " (dry run)"
}
fmt.Fprintf(out, msg+"\n", scc, verb, allTargets, dryRunText)
}