forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templateinstance_impersonation.go
336 lines (294 loc) · 12.8 KB
/
templateinstance_impersonation.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 templates
import (
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/retry"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
userapi "github.com/openshift/origin/pkg/user/apis/user"
exutil "github.com/openshift/origin/test/extended/util"
)
// 1. Check that users can't create or update templateinstances unless they are,
// or can impersonate, the requester.
// 2. Check that templateinstancespecs, particularly including
// requester.username and groups, are immutable.
var _ = g.Describe("[Conformance][templates] templateinstance impersonation tests", func() {
defer g.GinkgoRecover()
var (
cli = exutil.NewCLI("templates", exutil.KubeConfigPath())
adminuser *userapi.User // project admin, but can't impersonate anyone
impersonateuser *userapi.User // project edit, and can impersonate edituser1
impersonatebygroupuser *userapi.User
impersonategroup *userapi.Group
edituser1 *userapi.User // project edit, can be impersonated by impersonateuser
edituser2 *userapi.User // project edit
viewuser *userapi.User // project view
dummytemplateinstance *templateapi.TemplateInstance
dummycondition = templateapi.TemplateInstanceCondition{
Type: templateapi.TemplateInstanceConditionType("dummy"),
Status: kapi.ConditionTrue,
}
tests []struct {
user *userapi.User
expectCreateSuccess bool
expectDeleteSuccess bool
hasUpdatePermission bool
hasUpdateStatusPermission bool
}
)
g.BeforeEach(func() {
var err error
adminuser = createUser(cli, "adminuser", bootstrappolicy.AdminRoleName)
impersonateuser = createUser(cli, "impersonateuser", bootstrappolicy.EditRoleName)
impersonatebygroupuser = createUser(cli, "impersonatebygroupuser", bootstrappolicy.EditRoleName)
impersonategroup = createGroup(cli, "impersonategroup", bootstrappolicy.EditRoleName)
addUserToGroup(cli, impersonatebygroupuser.Name, impersonategroup.Name)
edituser1 = createUser(cli, "edituser1", bootstrappolicy.EditRoleName)
edituser2 = createUser(cli, "edituser2", bootstrappolicy.EditRoleName)
viewuser = createUser(cli, "viewuser", bootstrappolicy.ViewRoleName)
dummytemplateinstance = &templateapi.TemplateInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: templateapi.TemplateInstanceSpec{
Template: templateapi.Template{
ObjectMeta: metav1.ObjectMeta{
Name: "template",
Namespace: "dummy",
},
Objects: []runtime.Object{},
},
// all the tests work with a templateinstance which is set up to
// impersonate edituser1
Requester: &templateapi.TemplateInstanceRequester{
Username: edituser1.Name,
},
},
}
tests = []struct {
user *userapi.User
expectCreateSuccess bool
expectDeleteSuccess bool
hasUpdatePermission bool
hasUpdateStatusPermission bool
}{
{
user: nil, // system-admin
expectCreateSuccess: true, // can impersonate anyone
expectDeleteSuccess: true,
hasUpdatePermission: true,
hasUpdateStatusPermission: true,
},
{
user: adminuser,
expectCreateSuccess: false, // cannot impersonate edituser1
expectDeleteSuccess: true,
hasUpdatePermission: true,
hasUpdateStatusPermission: false,
},
{
user: impersonateuser,
expectCreateSuccess: true, // can impersonate edituser1
expectDeleteSuccess: true,
hasUpdatePermission: true,
hasUpdateStatusPermission: false,
},
{
user: impersonatebygroupuser,
expectCreateSuccess: true, // can impersonate edituser1
expectDeleteSuccess: true,
hasUpdatePermission: true,
hasUpdateStatusPermission: false,
},
{
user: edituser1,
expectCreateSuccess: true, // is edituser1
expectDeleteSuccess: true,
hasUpdatePermission: true,
hasUpdateStatusPermission: false,
},
{
user: edituser2,
expectCreateSuccess: false, // cannot impersonate edituser1
expectDeleteSuccess: true,
hasUpdatePermission: true,
hasUpdateStatusPermission: false,
},
{
user: viewuser,
expectCreateSuccess: false, // cannot create things and cannot impersonate edituser1
expectDeleteSuccess: false,
hasUpdatePermission: false,
hasUpdateStatusPermission: false,
},
}
// additional plumbing to enable impersonateuser to impersonate edituser1
role, err := cli.AdminClient().Roles(cli.Namespace()).Create(&authorizationapi.Role{
ObjectMeta: metav1.ObjectMeta{
Name: "impersonater",
},
Rules: []authorizationapi.PolicyRule{
{
Verbs: sets.NewString("assign"),
APIGroups: []string{templateapi.GroupName},
Resources: sets.NewString("templateinstances"),
},
},
})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = cli.AdminClient().RoleBindings(cli.Namespace()).Create(&authorizationapi.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "impersonater-binding",
},
RoleRef: kapi.ObjectReference{
Name: role.Name,
Namespace: cli.Namespace(),
},
Subjects: []kapi.ObjectReference{
{
Kind: authorizationapi.UserKind,
Name: impersonateuser.Name,
},
{
Kind: authorizationapi.GroupKind,
Name: impersonategroup.Name,
},
},
})
o.Expect(err).NotTo(o.HaveOccurred())
})
g.AfterEach(func() {
deleteUser(cli, adminuser)
deleteUser(cli, impersonateuser)
deleteUser(cli, edituser1)
deleteUser(cli, edituser2)
deleteUser(cli, viewuser)
deleteUser(cli, impersonatebygroupuser)
deleteGroup(cli, impersonategroup)
})
g.It("should pass impersonation creation tests", func() {
// check who can create TemplateInstances (anyone with project write access
// AND is/can impersonate spec.requester.username)
for _, test := range tests {
setUser(cli, test.user)
templateinstancecopy, err := kapi.Scheme.DeepCopy(dummytemplateinstance)
o.Expect(err).NotTo(o.HaveOccurred())
templateinstance, err := cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Create(templateinstancecopy.(*templateapi.TemplateInstance))
if !test.expectCreateSuccess {
o.Expect(err).To(o.HaveOccurred())
o.Expect(kerrors.IsInvalid(err) || kerrors.IsForbidden(err)).To(o.BeTrue())
} else {
o.Expect(err).NotTo(o.HaveOccurred())
err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Delete(templateinstance.Name, nil)
o.Expect(err).NotTo(o.HaveOccurred())
}
}
})
g.It("should pass impersonation update tests", func() {
// check who can update TemplateInstances. Via Update(), spec updates
// should be rejected (with the exception of spec.metadata fields used
// by the garbage collector, not tested here). Status updates should be
// silently ignored. Via UpdateStatus(), spec updates should be
// silently ignored. Status should only be updatable by a user with
// update access to that endpoint. In practice this is intended only to
// be the templateinstance controller and system:admin.
for _, test := range tests {
var templateinstancecopy *templateapi.TemplateInstance
setUser(cli, test.user)
templateinstance, err := cli.AdminTemplateClient().Template().TemplateInstances(cli.Namespace()).Create(dummytemplateinstance)
o.Expect(err).NotTo(o.HaveOccurred())
// ensure spec (particularly including spec.requester.username and groups) are
// immutable via Update()
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
templateinstancecopy, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Get(templateinstance.Name, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
templateinstancecopy.Spec.Requester.Username = edituser2.Name
_, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Update(templateinstancecopy)
return err
})
o.Expect(err).To(o.HaveOccurred())
o.Expect(kerrors.IsInvalid(err) || kerrors.IsForbidden(err)).To(o.BeTrue())
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
templateinstancecopy, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Get(templateinstance.Name, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
templateinstancecopy.Spec.Requester.Groups = append(templateinstancecopy.Spec.Requester.Groups, "foo")
_, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Update(templateinstancecopy)
return err
})
o.Expect(err).To(o.HaveOccurred())
o.Expect(kerrors.IsInvalid(err) || kerrors.IsForbidden(err)).To(o.BeTrue())
// ensure status changes are ignored via Update()
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
templateinstancecopy, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Get(templateinstance.Name, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
templateinstancecopy.Status.Conditions = append(templateinstancecopy.Status.Conditions, dummycondition)
templateinstancecopy, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Update(templateinstancecopy)
return err
})
if !test.hasUpdatePermission {
o.Expect(err).To(o.HaveOccurred())
o.Expect(kerrors.IsInvalid(err) || kerrors.IsForbidden(err)).To(o.BeTrue())
} else {
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(templateinstancecopy.Status.Conditions).NotTo(o.ContainElement(dummycondition))
}
// ensure spec changes are ignored via UpdateStatus()
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
templateinstancecopy, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Get(templateinstance.Name, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
templateinstancecopy.Spec.Requester.Username = edituser2.Name
templateinstancecopy, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).UpdateStatus(templateinstancecopy)
return err
})
if !test.hasUpdateStatusPermission {
o.Expect(err).To(o.HaveOccurred())
o.Expect(kerrors.IsInvalid(err) || kerrors.IsForbidden(err)).To(o.BeTrue())
} else {
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(templateinstancecopy.Spec).To(o.Equal(dummytemplateinstance.Spec))
}
// ensure status changes are allowed via UpdateStatus()
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
templateinstancecopy, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Get(templateinstance.Name, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
templateinstancecopy.Status.Conditions = []templateapi.TemplateInstanceCondition{dummycondition}
templateinstancecopy, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).UpdateStatus(templateinstancecopy)
return err
})
if !test.hasUpdateStatusPermission {
o.Expect(err).To(o.HaveOccurred())
o.Expect(kerrors.IsInvalid(err) || kerrors.IsForbidden(err)).To(o.BeTrue())
} else {
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(templateinstancecopy.Status.Conditions).To(o.ContainElement(dummycondition))
}
err = cli.AdminTemplateClient().Template().TemplateInstances(cli.Namespace()).Delete(templateinstance.Name, nil)
o.Expect(err).NotTo(o.HaveOccurred())
}
})
g.It("should pass impersonation deletion tests", func() {
// check who can delete TemplateInstances (anyone with project write access)
for _, test := range tests {
setUser(cli, test.user)
templateinstancecopy, err := kapi.Scheme.DeepCopy(dummytemplateinstance)
o.Expect(err).NotTo(o.HaveOccurred())
templateinstance, err := cli.AdminTemplateClient().Template().TemplateInstances(cli.Namespace()).Create(templateinstancecopy.(*templateapi.TemplateInstance))
o.Expect(err).NotTo(o.HaveOccurred())
err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Delete(templateinstance.Name, nil)
if test.expectDeleteSuccess {
o.Expect(err).NotTo(o.HaveOccurred())
} else {
o.Expect(err).To(o.HaveOccurred())
o.Expect(kerrors.IsForbidden(err)).To(o.BeTrue())
err = cli.AdminTemplateClient().Template().TemplateInstances(cli.Namespace()).Delete(templateinstance.Name, nil)
o.Expect(err).NotTo(o.HaveOccurred())
}
}
})
})