Skip to content

Commit 6fe4db1

Browse files
shaneuttopenshift-merge-bot[bot]
authored andcommitted
test: add e2e for gwapi rbac mgmt
Signed-off-by: Shane Utt <[email protected]>
1 parent 2fe87da commit 6fe4db1

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

test/e2e/gateway_api_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ func TestGatewayAPI(t *testing.T) {
105105
t.Log("Gateway API Controller not enabled, skipping controller tests")
106106
}
107107
t.Run("testGatewayAPIResourcesProtection", testGatewayAPIResourcesProtection)
108+
109+
t.Run("testGatewayAPIRBAC", testGatewayAPIRBAC)
108110
}
109111

110112
// testGatewayAPIResources tests that Gateway API Custom Resource Definitions are available.
@@ -402,6 +404,25 @@ func testGatewayAPIResourcesProtection(t *testing.T) {
402404
}
403405
}
404406

407+
// testGatewayAPIRBAC checks whether RBAC resources for Gateway API (such as the
408+
// aggregated ClusterRoles) are properly deployed and aggregated.
409+
func testGatewayAPIRBAC(t *testing.T) {
410+
aggregationMapping := map[string][]string{
411+
"system:openshift:gateway-api:aggregate-to-admin": {"admin", "edit"},
412+
"system:openshift:gateway-api:aggregate-to-view": {"view"},
413+
}
414+
415+
for srcClusterRoleName, destClusterRoleNames := range aggregationMapping {
416+
for _, destClusterRoleName := range destClusterRoleNames {
417+
t.Logf("verifying that ClusterRole %s aggregates all PolicyRules from %s", destClusterRoleName, srcClusterRoleName)
418+
419+
if err := eventuallyClusterRoleContainsAggregatedPolicies(t, destClusterRoleName, srcClusterRoleName); err != nil {
420+
t.Errorf("ClusterRole %s did not aggregate PolicyRules from %s", destClusterRoleName, srcClusterRoleName)
421+
}
422+
}
423+
}
424+
}
425+
405426
// ensureCRDs tests that the Gateway API custom resource definitions exist.
406427
func ensureCRDs(t *testing.T) {
407428
t.Helper()

test/e2e/util_gatewayapi_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io/ioutil"
1212
"net"
1313
"net/http"
14+
"slices"
1415
"strings"
1516
"testing"
1617
"time"
@@ -21,9 +22,11 @@ import (
2122
util "github.com/openshift/cluster-ingress-operator/pkg/util"
2223
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2324

25+
"github.com/google/go-cmp/cmp"
2426
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
2527
appsv1 "k8s.io/api/apps/v1"
2628
corev1 "k8s.io/api/core/v1"
29+
rbacv1 "k8s.io/api/rbac/v1"
2730
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2831
kerrors "k8s.io/apimachinery/pkg/api/errors"
2932
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -947,3 +950,48 @@ func (m *vapManager) enable() {
947950
m.t.Errorf("failed to find vap %q: %v", m.name, err)
948951
}
949952
}
953+
954+
func eventuallyClusterRoleContainsAggregatedPolicies(t *testing.T, destClusterRoleName, srcClusterRoleName string) error {
955+
t.Helper()
956+
957+
return wait.PollImmediate(time.Second, timeout, func() (bool, error) {
958+
var destClusterRole rbacv1.ClusterRole
959+
if err := kclient.Get(context.Background(), types.NamespacedName{Name: destClusterRoleName}, &destClusterRole); err != nil {
960+
t.Logf("Failed to get destination ClusterRole %s; retrying...: %v", destClusterRoleName, err)
961+
return false, nil
962+
}
963+
964+
var srcClusterRole rbacv1.ClusterRole
965+
if err := kclient.Get(context.Background(), types.NamespacedName{Name: srcClusterRoleName}, &srcClusterRole); err != nil {
966+
t.Logf("Failed to get source ClusterRole %s: %v; retrying...", srcClusterRoleName, err)
967+
return false, nil
968+
}
969+
970+
if len(destClusterRole.Rules) == 0 {
971+
return false, fmt.Errorf("ClusterRole %s unexpectedly had no PolicyRules set", destClusterRoleName)
972+
}
973+
974+
if len(srcClusterRole.Rules) == 0 {
975+
return false, fmt.Errorf("ClusterRole %s unexpectedly had no PolicyRules set", srcClusterRoleName)
976+
}
977+
978+
if containsPolicyRules(destClusterRole.Rules, srcClusterRole.Rules) {
979+
t.Logf("ClusterRole %s aggregated all rules from %s", destClusterRoleName, srcClusterRoleName)
980+
return true, nil
981+
}
982+
983+
return false, nil
984+
})
985+
}
986+
987+
func containsPolicyRules(destRules, srcRules []rbacv1.PolicyRule) bool {
988+
for _, srcRule := range srcRules {
989+
if !slices.ContainsFunc(destRules, func(destRule rbacv1.PolicyRule) bool {
990+
return cmp.Equal(destRule, srcRule)
991+
}) {
992+
return false
993+
}
994+
}
995+
996+
return true
997+
}

0 commit comments

Comments
 (0)