@@ -30,7 +30,7 @@ import (
30
30
"github.com/stretchr/testify/require"
31
31
32
32
authorizationv1 "k8s.io/api/authorization/v1"
33
- v1 "k8s.io/api/core/v1"
33
+ corev1 "k8s.io/api/core/v1"
34
34
rbacv1 "k8s.io/api/rbac/v1"
35
35
"k8s.io/apimachinery/pkg/api/errors"
36
36
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -106,22 +106,25 @@ func TestAuthorizer(t *testing.T) {
106
106
return err == nil
107
107
}, 2 * wait .ForeverTestTimeout , 100 * time .Millisecond )
108
108
109
- tests := map [string ]func (t * testing.T ){
110
- "as org member, workspace admin user-1 can access everything" : func (t * testing.T ) {
109
+ testCases := []struct {
110
+ name string
111
+ run func (t * testing.T )
112
+ }{
113
+ {"as org member, workspace admin user-1 can access everything" , func (t * testing.T ) {
111
114
_ , err := user1KubeClusterClient .Cluster (org1 .Join ("workspace1" )).CoreV1 ().ConfigMaps ("default" ).List (ctx , metav1.ListOptions {})
112
115
require .NoError (t , err )
113
- _ , err = user1KubeClusterClient .Cluster (org1 .Join ("workspace1" )).CoreV1 ().Namespaces ().Create (ctx , & v1 .Namespace {ObjectMeta : metav1.ObjectMeta {Name : "test" }}, metav1.CreateOptions {})
116
+ _ , err = user1KubeClusterClient .Cluster (org1 .Join ("workspace1" )).CoreV1 ().Namespaces ().Create (ctx , & corev1 .Namespace {ObjectMeta : metav1.ObjectMeta {Name : "test" }}, metav1.CreateOptions {})
114
117
require .NoError (t , err )
115
- _ , err = user1KubeClusterClient .Cluster (org1 .Join ("workspace1" )).CoreV1 ().ConfigMaps ("test" ).Create (ctx , & v1 .ConfigMap {ObjectMeta : metav1.ObjectMeta {Name : "test" }}, metav1.CreateOptions {})
118
+ _ , err = user1KubeClusterClient .Cluster (org1 .Join ("workspace1" )).CoreV1 ().ConfigMaps ("test" ).Create (ctx , & corev1 .ConfigMap {ObjectMeta : metav1.ObjectMeta {Name : "test" }}, metav1.CreateOptions {})
116
119
require .NoError (t , err )
117
- },
118
- "with org access, workspace1 non-admin user-2 can access according to local policy" : func (t * testing.T ) {
119
- _ , err := user2KubeClusterClient .Cluster (org1 .Join ("workspace1" )).CoreV1 ().Namespaces ().Create (ctx , & v1 .Namespace {ObjectMeta : metav1.ObjectMeta {Name : "test" }}, metav1.CreateOptions {})
120
+ }} ,
121
+ { "with org access, workspace1 non-admin user-2 can access according to local policy" , func (t * testing.T ) {
122
+ _ , err := user2KubeClusterClient .Cluster (org1 .Join ("workspace1" )).CoreV1 ().Namespaces ().Create (ctx , & corev1 .Namespace {ObjectMeta : metav1.ObjectMeta {Name : "test" }}, metav1.CreateOptions {})
120
123
require .Errorf (t , err , "user-2 should not be able to create namespace in %s" , org1 .Join ("workspace1" ))
121
124
_ , err = user2KubeClusterClient .Cluster (org1 .Join ("workspace1" )).CoreV1 ().Secrets ("default" ).List (ctx , metav1.ListOptions {})
122
125
require .NoErrorf (t , err , "user-2 should be able to list secrets in %s as defined in the local policy" , org1 .Join ("workspace1" ))
123
- },
124
- "with org access, workspace1 non-admin user-2 can access /healthz, /livez, /readyz etc" : func (t * testing.T ) {
126
+ }} ,
127
+ { "with org access, workspace1 non-admin user-2 can access /healthz, /livez, /readyz etc" , func (t * testing.T ) {
125
128
cl := user2KubeClusterClient .RESTClient ()
126
129
requestPath := org1 .RequestPath ()
127
130
{
@@ -141,24 +144,24 @@ func TestAuthorizer(t *testing.T) {
141
144
version := new (apimachineryversion.Info )
142
145
require .NoError (t , json .Unmarshal (respBytes , version ))
143
146
}
144
- },
145
- "without org access, org1 workspace1 admin user-1 cannot access org2, not even discovery" : func (t * testing.T ) {
147
+ }} ,
148
+ { "without org access, org1 workspace1 admin user-1 cannot access org2, not even discovery" , func (t * testing.T ) {
146
149
_ , err := user1KubeClusterClient .Cluster (org2 .Join ("workspace1" )).CoreV1 ().ConfigMaps ("default" ).List (ctx , metav1.ListOptions {})
147
150
require .Errorf (t , err , "user-1 should not be able to list configmaps in a different org (%s)" , org2 .Join ("workspace1" ))
148
151
_ , err = user1KubeDiscoveryClient .Cluster (org2 .Join ("workspace1" )).ServerResourcesForGroupVersion ("rbac.authorization.k8s.io/v1" ) // can't be core because that always returns nil
149
152
require .Errorf (t , err , "user-1 should not be able to list server resources in a different org (%s)" , org2 .Join ("workspace1" ))
150
- },
151
- "as org member, workspace1 admin user-1 cannot access workspace2, not even discovery" : func (t * testing.T ) {
153
+ }} ,
154
+ { "as org member, workspace1 admin user-1 cannot access workspace2, not even discovery" , func (t * testing.T ) {
152
155
_ , err := user1KubeClusterClient .Cluster (org1 .Join ("workspace2" )).CoreV1 ().ConfigMaps ("default" ).List (ctx , metav1.ListOptions {})
153
156
require .Errorf (t , err , "user-1 should not be able to list configmaps in a different workspace (%s)" , org1 .Join ("workspace2" ))
154
157
_ , err = user1KubeDiscoveryClient .Cluster (org2 .Join ("workspace1" )).ServerResourcesForGroupVersion ("rbac.authorization.k8s.io/v1" ) // can't be core because that always returns nil
155
158
require .Errorf (t , err , "user-1 should not be able to list server resources in a different workspace (%s)" , org1 .Join ("workspace2" ))
156
- },
157
- "with org access, workspace2 admin user-2 can access workspace2" : func (t * testing.T ) {
159
+ }} ,
160
+ { "with org access, workspace2 admin user-2 can access workspace2" , func (t * testing.T ) {
158
161
_ , err := user2KubeClusterClient .Cluster (org1 .Join ("workspace2" )).CoreV1 ().ConfigMaps ("default" ).List (ctx , metav1.ListOptions {})
159
162
require .NoError (t , err , "user-2 should be able to list configmaps in workspace2 (%s)" , org1 .Join ("workspace2" ))
160
- },
161
- "cluster admins can use wildcard clusters, non-cluster admin cannot" : func (t * testing.T ) {
163
+ }} ,
164
+ { "cluster admins can use wildcard clusters, non-cluster admin cannot" , func (t * testing.T ) {
162
165
// create client talking directly to root shard to test wildcard requests
163
166
rootKubeClusterClient , err := kcpkubernetesclientset .NewForConfig (rootShardCfg )
164
167
require .NoError (t , err )
@@ -169,8 +172,8 @@ func TestAuthorizer(t *testing.T) {
169
172
require .NoError (t , err )
170
173
_ , err = user1RootKubeClusterClient .CoreV1 ().Namespaces ().List (ctx , metav1.ListOptions {})
171
174
require .Error (t , err , "Only cluster admins can use all clusters at once" )
172
- },
173
- "with system:admin permissions, workspace2 non-admin user-3 can list Namespaces with a bootstrap ClusterRole" : func (t * testing.T ) {
175
+ }} ,
176
+ { "with system:admin permissions, workspace2 non-admin user-3 can list Namespaces with a bootstrap ClusterRole" , func (t * testing.T ) {
174
177
// get workspace2 shard and create a client to tweak the local bootstrap policy
175
178
shardKubeClusterClient , err := kcpkubernetesclientset .NewForConfig (rootShardCfg )
176
179
require .NoError (t , err )
@@ -223,8 +226,8 @@ func TestAuthorizer(t *testing.T) {
223
226
}
224
227
return true
225
228
}, wait .ForeverTestTimeout , time .Millisecond * 100 , "User-3 should now be able to list Namespaces in %s" , org1 .Join ("workspace2" ))
226
- },
227
- "without org access, a deep SAR with user-1 against org2 succeeds even without org access for user-1" : func (t * testing.T ) {
229
+ }} ,
230
+ { "without org access, a deep SAR with user-1 against org2 succeeds even without org access for user-1" , func (t * testing.T ) {
228
231
t .Logf ("try to list ConfigMap as user-1 in %q without access, should fail" , org2 .Join ("workspace1" ))
229
232
_ , err := user1KubeClusterClient .Cluster (org2 .Join ("workspace1" )).CoreV1 ().ConfigMaps ("default" ).List (ctx , metav1.ListOptions {})
230
233
require .Errorf (t , err , "user-1 should not be able to list configmaps in %q" , org2 .Join ("workspace1" ))
@@ -254,15 +257,14 @@ func TestAuthorizer(t *testing.T) {
254
257
resp , err = deepSARClient .Cluster (org2 .Join ("workspace1" )).AuthorizationV1 ().SubjectAccessReviews ().Create (ctx , sar , metav1.CreateOptions {})
255
258
require .NoError (t , err )
256
259
require .Truef (t , resp .Status .Allowed , "SAR should answer hypothetically that user-1 could list configmaps in %q if it had access" , org2 .Join ("workspace1" ))
257
- },
260
+ }} ,
258
261
}
259
262
260
- for tcName , tcFunc := range tests {
261
- tcName := tcName
262
- tcFunc := tcFunc
263
- t .Run (tcName , func (t * testing.T ) {
263
+ for i := range testCases {
264
+ testCase := testCases [i ]
265
+ t .Run (testCase .name , func (t * testing.T ) {
264
266
t .Parallel ()
265
- tcFunc (t )
267
+ testCase . run (t )
266
268
})
267
269
}
268
270
}
0 commit comments