-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding CEL validation for permission claims on bindings and exports
* Validate that empty group means only internal types that are valid * Validate that if a group is set, that an identityHash must also be set.
- Loading branch information
Shawn Hurley
committed
Jul 15, 2022
1 parent
0b9bb45
commit aed23f7
Showing
6 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
Copyright 2022 The KCP Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package apiexport | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
apisv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/apis/v1alpha1" | ||
clientset "github.com/kcp-dev/kcp/pkg/client/clientset/versioned" | ||
"github.com/kcp-dev/kcp/test/e2e/framework" | ||
) | ||
|
||
// TestAPIExportCELValidation will test the validation the permission claims for an otherwise valid APIExport. | ||
func TestAPIExportCELValidation(t *testing.T) { | ||
|
||
testCases := []struct { | ||
name string | ||
claim apisv1alpha1.PermissionClaim | ||
validExport bool | ||
}{ | ||
{ | ||
name: "valid", | ||
claim: apisv1alpha1.PermissionClaim{ | ||
GroupResource: apisv1alpha1.GroupResource{ | ||
Group: "", | ||
Resource: "configmaps", | ||
}, | ||
}, | ||
validExport: true, | ||
}, | ||
{ | ||
name: "invalid core resource", | ||
claim: apisv1alpha1.PermissionClaim{ | ||
GroupResource: apisv1alpha1.GroupResource{ | ||
Group: "", | ||
Resource: "fakeresources", | ||
}, | ||
}, | ||
validExport: false, | ||
}, | ||
{ | ||
name: "invalid non core resource", | ||
claim: apisv1alpha1.PermissionClaim{ | ||
GroupResource: apisv1alpha1.GroupResource{ | ||
Group: "new.core.resources", | ||
Resource: "fakeresources", | ||
}, | ||
}, | ||
validExport: false, | ||
}, | ||
{ | ||
name: "valid non core resource", | ||
claim: apisv1alpha1.PermissionClaim{ | ||
GroupResource: apisv1alpha1.GroupResource{ | ||
Group: "new.core.resources", | ||
Resource: "fakeresources", | ||
}, | ||
IdentityHash: "fakehashhere", | ||
}, | ||
validExport: true, | ||
}, | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
server := framework.SharedKcpServer(t) | ||
orgClusterName := framework.NewOrganizationFixture(t, server) | ||
|
||
t.Cleanup(cancel) | ||
|
||
cfg := server.DefaultConfig(t) | ||
|
||
kcpClients, err := clientset.NewClusterForConfig(cfg) | ||
require.NoError(t, err, "failed to construct kcp cluster client for server") | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
providerWorkspace := framework.NewWorkspaceFixture(t, server, orgClusterName) | ||
|
||
// create API Bindings here | ||
export := apisv1alpha1.APIExport{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: strings.ToLower(strings.Join(strings.Split(tc.name, " "), "-")), | ||
}, | ||
Spec: apisv1alpha1.APIExportSpec{ | ||
PermissionClaims: []apisv1alpha1.PermissionClaim{ | ||
tc.claim, | ||
}, | ||
}, | ||
} | ||
|
||
_, err := kcpClients.Cluster(providerWorkspace).ApisV1alpha1().APIExports().Create(ctx, &export, metav1.CreateOptions{}) | ||
if err != nil && !tc.validExport { | ||
t.Logf("invalid binding did get an error") | ||
return | ||
} | ||
if tc.validExport && err == nil { | ||
t.Logf("valid binding created successfully") | ||
return | ||
} | ||
t.Errorf("test case expected: %v but instead got %v", tc.validExport, err) | ||
}) | ||
} | ||
} |