forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
templateinstance_security.go
294 lines (266 loc) · 10.5 KB
/
templateinstance_security.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
package templates
import (
"fmt"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
kapiv1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
kapi "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/storage"
"github.com/openshift/origin/pkg/api/latest"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
routeapi "github.com/openshift/origin/pkg/route/apis/route"
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"
)
// Check that objects created through the TemplateInstance mechanism are done
// impersonating the requester, and that privilege escalation is not possible.
var _ = g.Describe("[Conformance][templates] templateinstance security tests", func() {
defer g.GinkgoRecover()
var (
cli = exutil.NewCLI("templates", exutil.KubeConfigPath())
adminuser, edituser, editbygroupuser *userapi.User
editgroup *userapi.Group
dummyroute = &routeapi.Route{
ObjectMeta: metav1.ObjectMeta{
Name: "route",
Namespace: "${NAMESPACE}",
},
Spec: routeapi.RouteSpec{
To: routeapi.RouteTargetReference{
Name: "dummyroute",
},
},
}
dummyrolebinding = &authorizationapi.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "rolebinding",
Namespace: "${NAMESPACE}",
},
RoleRef: kapi.ObjectReference{
Name: bootstrappolicy.AdminRoleName,
},
}
storageclass = &storage.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "storageclass",
},
Provisioner: "no-provisioning",
}
)
g.Context("", func() {
g.BeforeEach(func() {
adminuser = createUser(cli, "adminuser", bootstrappolicy.AdminRoleName)
edituser = createUser(cli, "edituser", bootstrappolicy.EditRoleName)
editbygroupuser = createUser(cli, "editbygroupuser", "")
editgroup = createGroup(cli, "editgroup", bootstrappolicy.EditRoleName)
addUserToGroup(cli, editbygroupuser.Name, editgroup.Name)
// I think we get flakes when the group cache hasn't yet noticed the
// new group membership made above. Wait until all it looks like
// all the users above have access to the namespace as expected.
err := wait.PollImmediate(time.Second, 30*time.Second, func() (done bool, err error) {
for _, user := range []*userapi.User{adminuser, edituser, editbygroupuser} {
cli.ChangeUser(user.Name)
sar, err := cli.AuthorizationClient().Authorization().LocalSubjectAccessReviews(cli.Namespace()).Create(&authorizationapi.LocalSubjectAccessReview{
Action: authorizationapi.Action{
Verb: "get",
Resource: "pods",
},
})
if err != nil {
return false, err
}
if !sar.Allowed {
return false, nil
}
}
return true, nil
})
o.Expect(err).NotTo(o.HaveOccurred())
})
g.AfterEach(func() {
if g.CurrentGinkgoTestDescription().Failed {
templateinstances, err := cli.AdminTemplateClient().Template().TemplateInstances(cli.Namespace()).List(metav1.ListOptions{})
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "TemplateInstances: %#v", templateinstances.Items)
}
}
deleteUser(cli, adminuser)
deleteUser(cli, edituser)
deleteUser(cli, editbygroupuser)
deleteGroup(cli, editgroup)
})
g.It("should pass security tests", func() {
tests := []struct {
by string
user *userapi.User
namespace string
objects []runtime.Object
expectCondition templateapi.TemplateInstanceConditionType
checkOK func(namespace string) bool
}{
{
by: "checking edituser can create an object in a permitted namespace",
user: edituser,
namespace: cli.Namespace(),
objects: []runtime.Object{dummyroute},
expectCondition: templateapi.TemplateInstanceReady,
checkOK: func(namespace string) bool {
_, err := cli.AdminRouteClient().Route().Routes(namespace).Get(dummyroute.Name, metav1.GetOptions{})
return err == nil
},
},
{
by: "checking editbygroupuser can create an object in a permitted namespace",
user: editbygroupuser,
namespace: cli.Namespace(),
objects: []runtime.Object{dummyroute},
expectCondition: templateapi.TemplateInstanceReady,
checkOK: func(namespace string) bool {
_, err := cli.AdminRouteClient().Route().Routes(namespace).Get(dummyroute.Name, metav1.GetOptions{})
return err == nil
},
},
{
by: "checking edituser can't create an object in a non-permitted namespace",
user: edituser,
namespace: "default",
objects: []runtime.Object{dummyroute},
expectCondition: templateapi.TemplateInstanceInstantiateFailure,
checkOK: func(namespace string) bool {
_, err := cli.AdminRouteClient().Route().Routes(namespace).Get(dummyroute.Name, metav1.GetOptions{})
return err != nil && kerrors.IsNotFound(err)
},
},
{
by: "checking editbygroupuser can't create an object in a non-permitted namespace",
user: editbygroupuser,
namespace: "default",
objects: []runtime.Object{dummyroute},
expectCondition: templateapi.TemplateInstanceInstantiateFailure,
checkOK: func(namespace string) bool {
_, err := cli.AdminRouteClient().Route().Routes(namespace).Get(dummyroute.Name, metav1.GetOptions{})
return err != nil && kerrors.IsNotFound(err)
},
},
{
by: "checking edituser can't create an object that requires admin",
user: edituser,
namespace: cli.Namespace(),
objects: []runtime.Object{dummyrolebinding},
expectCondition: templateapi.TemplateInstanceInstantiateFailure,
checkOK: func(namespace string) bool {
_, err := cli.AdminAuthorizationClient().Authorization().RoleBindings(namespace).Get(dummyrolebinding.Name, metav1.GetOptions{})
return err != nil && kerrors.IsNotFound(err)
},
},
{
by: "checking editbygroupuser can't create an object that requires admin",
user: editbygroupuser,
namespace: cli.Namespace(),
objects: []runtime.Object{dummyrolebinding},
expectCondition: templateapi.TemplateInstanceInstantiateFailure,
checkOK: func(namespace string) bool {
_, err := cli.AdminAuthorizationClient().Authorization().RoleBindings(namespace).Get(dummyrolebinding.Name, metav1.GetOptions{})
return err != nil && kerrors.IsNotFound(err)
},
},
{
by: "checking adminuser can create an object that requires admin",
user: adminuser,
namespace: cli.Namespace(),
objects: []runtime.Object{dummyrolebinding},
expectCondition: templateapi.TemplateInstanceReady,
checkOK: func(namespace string) bool {
_, err := cli.AdminAuthorizationClient().Authorization().RoleBindings(namespace).Get(dummyrolebinding.Name, metav1.GetOptions{})
return err == nil
},
},
{
by: "checking adminuser can't create an object that requires more than admin",
user: adminuser,
namespace: cli.Namespace(),
objects: []runtime.Object{storageclass},
expectCondition: templateapi.TemplateInstanceInstantiateFailure,
checkOK: func(namespace string) bool {
_, err := cli.AdminKubeClient().StorageV1().StorageClasses().Get(storageclass.Name, metav1.GetOptions{})
return err != nil && kerrors.IsNotFound(err)
},
},
}
targetVersions := []schema.GroupVersion{storagev1.SchemeGroupVersion}
targetVersions = append(targetVersions, latest.Versions...)
for _, test := range tests {
g.By(test.by)
cli.ChangeUser(test.user.Name)
secret, err := cli.KubeClient().CoreV1().Secrets(cli.Namespace()).Create(&kapiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret",
},
Data: map[string][]byte{
"NAMESPACE": []byte(test.namespace),
},
})
o.Expect(err).NotTo(o.HaveOccurred())
templateinstance := &templateapi.TemplateInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "templateinstance",
},
Spec: templateapi.TemplateInstanceSpec{
Template: templateapi.Template{
ObjectMeta: metav1.ObjectMeta{
Name: "template",
Namespace: cli.Namespace(),
},
Parameters: []templateapi.Parameter{
{
Name: "NAMESPACE",
Required: true,
},
},
},
Secret: &kapi.LocalObjectReference{
Name: "secret",
},
},
}
err = templateapi.AddObjectsToTemplate(&templateinstance.Spec.Template, test.objects, targetVersions...)
o.Expect(err).NotTo(o.HaveOccurred())
templateinstance, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Create(templateinstance)
o.Expect(err).NotTo(o.HaveOccurred())
err = wait.Poll(100*time.Millisecond, 1*time.Minute, func() (bool, error) {
templateinstance, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Get(templateinstance.Name, metav1.GetOptions{})
if err != nil {
return false, err
}
return len(templateinstance.Status.Conditions) != 0, nil
})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(templateinstance.HasCondition(test.expectCondition, kapi.ConditionTrue)).To(o.Equal(true))
o.Expect(test.checkOK(test.namespace)).To(o.BeTrue())
foreground := metav1.DeletePropagationForeground
err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Delete(templateinstance.Name, &metav1.DeleteOptions{PropagationPolicy: &foreground})
o.Expect(err).NotTo(o.HaveOccurred())
// wait for garbage collector to do its thing
err = wait.Poll(100*time.Millisecond, 30*time.Second, func() (bool, error) {
_, err = cli.TemplateClient().Template().TemplateInstances(cli.Namespace()).Get(templateinstance.Name, metav1.GetOptions{})
if kerrors.IsNotFound(err) {
return true, nil
}
return false, err
})
o.Expect(err).NotTo(o.HaveOccurred())
err = cli.KubeClient().CoreV1().Secrets(cli.Namespace()).Delete(secret.Name, nil)
o.Expect(err).NotTo(o.HaveOccurred())
}
})
})
})