forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templateservicebroker_security.go
192 lines (160 loc) · 7.28 KB
/
templateservicebroker_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
package templates
import (
"crypto/tls"
"net/http"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
"github.com/pborman/uuid"
"golang.org/x/net/context"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kapi "k8s.io/kubernetes/pkg/api"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
"github.com/openshift/origin/pkg/openservicebroker/api"
"github.com/openshift/origin/pkg/openservicebroker/client"
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"
testutil "github.com/openshift/origin/test/util"
)
var _ = g.Describe("[templates] templateservicebroker security test", func() {
defer g.GinkgoRecover()
var (
cli = exutil.NewCLI("templates", exutil.KubeConfigPath())
instanceID = uuid.NewRandom().String()
bindingID = uuid.NewRandom().String()
template *templateapi.Template
clusterrolebinding *authorizationapi.ClusterRoleBinding
brokercli client.Client
service *api.Service
plan *api.Plan
viewuser *userapi.User
edituser *userapi.User
nopermsuser *userapi.User
)
g.BeforeEach(func() {
var err error
template, err = cli.Client().Templates("openshift").Get("cakephp-mysql-persistent", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
clusterrolebinding, err = cli.AdminClient().ClusterRoleBindings().Create(&authorizationapi.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: cli.Namespace() + "templateservicebroker-client",
},
RoleRef: kapi.ObjectReference{
Name: bootstrappolicy.TemplateServiceBrokerClientRoleName,
},
Subjects: []kapi.ObjectReference{
{
Kind: authorizationapi.GroupKind,
Name: bootstrappolicy.UnauthenticatedGroup,
},
},
})
o.Expect(err).NotTo(o.HaveOccurred())
adminClientConfig, err := testutil.GetClusterAdminClientConfig(exutil.KubeConfigPath())
o.Expect(err).NotTo(o.HaveOccurred())
brokercli = client.NewClient(&http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}, adminClientConfig.Host+templateapi.ServiceBrokerRoot)
viewuser = createUser(cli, "viewuser", bootstrappolicy.ViewRoleName)
edituser = createUser(cli, "edituser", bootstrappolicy.EditRoleName)
nopermsuser = createUser(cli, "nopermsuser", "")
})
g.AfterEach(func() {
deleteUser(cli, viewuser)
deleteUser(cli, edituser)
deleteUser(cli, nopermsuser)
err := cli.AdminClient().ClusterRoleBindings().Delete(clusterrolebinding.Name)
o.Expect(err).NotTo(o.HaveOccurred())
cli.AdminTemplateClient().Template().BrokerTemplateInstances().Delete(instanceID, nil)
})
catalog := func() {
g.By("returning a catalog")
catalog, err := brokercli.Catalog(context.Background())
o.Expect(err).NotTo(o.HaveOccurred())
for _, service = range catalog.Services {
if service.ID == string(template.UID) {
o.Expect(service.Plans).NotTo(o.BeEmpty())
plan = &service.Plans[0]
break
}
}
o.Expect(service.ID).To(o.BeEquivalentTo(template.UID))
}
provision := func(username string) error {
g.By("provisioning a service")
_, err := brokercli.Provision(context.Background(), instanceID, &api.ProvisionRequest{
ServiceID: service.ID,
PlanID: plan.ID,
Context: api.KubernetesContext{
Platform: api.ContextPlatformKubernetes,
Namespace: cli.Namespace(),
},
Parameters: map[string]string{
templateapi.RequesterUsernameParameterKey: username,
},
})
return err
}
bind := func(username string) error {
g.By("binding to a service")
_, err := brokercli.Bind(context.Background(), instanceID, bindingID, &api.BindRequest{
ServiceID: service.ID,
PlanID: plan.ID,
Parameters: map[string]string{
templateapi.RequesterUsernameParameterKey: username,
},
})
return err
}
g.It("should pass security tests", func() {
catalog()
g.By("having no permissions to the namespace, provision should fail with 403")
err := provision(nopermsuser.Name)
o.Expect(err).To(o.HaveOccurred())
o.Expect(err).To(o.BeAssignableToTypeOf(&client.ServerError{}))
o.Expect(err.(*client.ServerError).StatusCode).To(o.Equal(http.StatusForbidden))
g.By("having no permissions to the namespace, no BrokerTemplateInstance should be created")
_, err = cli.AdminTemplateClient().Template().BrokerTemplateInstances().Get(instanceID, metav1.GetOptions{})
o.Expect(err).To(o.HaveOccurred())
o.Expect(kerrors.IsNotFound(err)).To(o.BeTrue())
g.By("having view permissions to the namespace, provision should fail with 403")
err = provision(viewuser.Name)
o.Expect(err).To(o.HaveOccurred())
o.Expect(err).To(o.BeAssignableToTypeOf(&client.ServerError{}))
o.Expect(err.(*client.ServerError).StatusCode).To(o.Equal(http.StatusForbidden))
g.By("having view permissions to the namespace, no BrokerTemplateInstance should be created")
_, err = cli.AdminTemplateClient().Template().BrokerTemplateInstances().Get(instanceID, metav1.GetOptions{})
o.Expect(err).To(o.HaveOccurred())
o.Expect(kerrors.IsNotFound(err)).To(o.BeTrue())
g.By("having edit permissions to the namespace, provision should succeed")
err = provision(edituser.Name)
o.Expect(err).NotTo(o.HaveOccurred())
brokerTemplateInstance, err := cli.AdminTemplateClient().Template().BrokerTemplateInstances().Get(instanceID, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(brokerTemplateInstance.Spec.BindingIDs).To(o.HaveLen(0))
g.By("having no permissions to the namespace, bind should fail with 403")
err = bind(nopermsuser.Name)
o.Expect(err).To(o.HaveOccurred())
o.Expect(err).To(o.BeAssignableToTypeOf(&client.ServerError{}))
o.Expect(err.(*client.ServerError).StatusCode).To(o.Equal(http.StatusForbidden))
g.By("having no permissions to the namespace, the BrokerTemplateInstance should be unchanged")
brokerTemplateInstance, err = cli.AdminTemplateClient().Template().BrokerTemplateInstances().Get(instanceID, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(brokerTemplateInstance.Spec.BindingIDs).To(o.HaveLen(0))
g.By("having view permissions to the namespace, bind should fail with 403") // view does not enable reading Secrets
err = bind(viewuser.Name)
o.Expect(err).To(o.HaveOccurred())
o.Expect(err).To(o.BeAssignableToTypeOf(&client.ServerError{}))
o.Expect(err.(*client.ServerError).StatusCode).To(o.Equal(http.StatusForbidden))
g.By("having view permissions to the namespace, the BrokerTemplateInstance should be unchanged")
brokerTemplateInstance, err = cli.AdminTemplateClient().Template().BrokerTemplateInstances().Get(instanceID, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(brokerTemplateInstance.Spec.BindingIDs).To(o.HaveLen(0))
g.By("having edit permissions to the namespace, bind should succeed")
err = bind(edituser.Name)
o.Expect(err).NotTo(o.HaveOccurred())
brokerTemplateInstance, err = cli.AdminTemplateClient().Template().BrokerTemplateInstances().Get(instanceID, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(brokerTemplateInstance.Spec.BindingIDs).To(o.Equal([]string{bindingID}))
})
})