|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2025 The KCP Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package kubeconfigrbac |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/go-logr/logr" |
| 28 | + |
| 29 | + kcptenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1" |
| 30 | + "github.com/kcp-dev/logicalcluster/v3" |
| 31 | + corev1 "k8s.io/api/core/v1" |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + "k8s.io/apimachinery/pkg/types" |
| 34 | + "k8s.io/apimachinery/pkg/util/wait" |
| 35 | + ctrlruntime "sigs.k8s.io/controller-runtime" |
| 36 | + ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 37 | + |
| 38 | + operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1" |
| 39 | + "github.com/kcp-dev/kcp-operator/test/utils" |
| 40 | +) |
| 41 | + |
| 42 | +func TestProvisionFrontProxyRBAC(t *testing.T) { |
| 43 | + ctrlruntime.SetLogger(logr.Discard()) |
| 44 | + |
| 45 | + client := utils.GetKubeClient(t) |
| 46 | + ctx := context.Background() |
| 47 | + |
| 48 | + rootCluster := logicalcluster.NewPath("root") |
| 49 | + namespace := utils.CreateSelfDestructingNamespace(t, ctx, client, "provision-frontproxy-rbac") |
| 50 | + externalHostname := fmt.Sprintf("front-proxy-front-proxy.%s.svc.cluster.local", namespace.Name) |
| 51 | + |
| 52 | + // deploy rootshard |
| 53 | + rootShard := utils.DeployRootShard(ctx, t, client, namespace.Name, externalHostname) |
| 54 | + |
| 55 | + // deploy front-proxy |
| 56 | + frontProxy := utils.DeployFrontProxy(ctx, t, client, namespace.Name, rootShard.Name, externalHostname) |
| 57 | + |
| 58 | + // create a dummy workspace where we later want to provision RBAC in |
| 59 | + t.Log("Creating dummy workspace…") |
| 60 | + workspace := &kcptenancyv1alpha1.Workspace{ |
| 61 | + ObjectMeta: metav1.ObjectMeta{ |
| 62 | + Name: "test", |
| 63 | + }, |
| 64 | + Spec: kcptenancyv1alpha1.WorkspaceSpec{ |
| 65 | + Type: kcptenancyv1alpha1.WorkspaceTypeReference{ |
| 66 | + Name: "universal", |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + dummyCluster := rootCluster.Join(workspace.Name) |
| 72 | + proxyClient := utils.ConnectWithRootShardProxy(t, ctx, client, &rootShard, rootCluster) |
| 73 | + if err := proxyClient.Create(ctx, workspace); err != nil { |
| 74 | + t.Fatalf("Failed to create workspace: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + // wait for workspace to be ready |
| 78 | + t.Log("Waiting for workspace to be ready…") |
| 79 | + dummyClient := utils.ConnectWithRootShardProxy(t, ctx, client, &rootShard, dummyCluster) |
| 80 | + |
| 81 | + err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 30*time.Second, false, func(ctx context.Context) (done bool, err error) { |
| 82 | + return dummyClient.List(ctx, &corev1.SecretList{}) == nil, nil |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("Failed to wait for workspace to become available: %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + // create my-config kubeconfig |
| 89 | + configSecretName := "kubeconfig-my-config-e2e" |
| 90 | + |
| 91 | + // as of now, this Kubeconfig will not grant any permissions yet |
| 92 | + fpConfig := operatorv1alpha1.Kubeconfig{} |
| 93 | + fpConfig.Name = "my-config" |
| 94 | + fpConfig.Namespace = namespace.Name |
| 95 | + fpConfig.Spec = operatorv1alpha1.KubeconfigSpec{ |
| 96 | + Target: operatorv1alpha1.KubeconfigTarget{ |
| 97 | + FrontProxyRef: &corev1.LocalObjectReference{ |
| 98 | + Name: frontProxy.Name, |
| 99 | + }, |
| 100 | + }, |
| 101 | + Username: "e2e", |
| 102 | + Validity: metav1.Duration{Duration: 2 * time.Hour}, |
| 103 | + SecretRef: corev1.LocalObjectReference{ |
| 104 | + Name: configSecretName, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + t.Log("Creating kubeconfig with no permissions attached…") |
| 109 | + if err := client.Create(ctx, &fpConfig); err != nil { |
| 110 | + t.Fatal(err) |
| 111 | + } |
| 112 | + utils.WaitForObject(t, ctx, client, &corev1.Secret{}, types.NamespacedName{Namespace: fpConfig.Namespace, Name: fpConfig.Spec.SecretRef.Name}) |
| 113 | + |
| 114 | + t.Log("Connecting to FrontProxy…") |
| 115 | + kcpClient := utils.ConnectWithKubeconfig(t, ctx, client, namespace.Name, fpConfig.Name, dummyCluster) |
| 116 | + |
| 117 | + // This should not work yet. |
| 118 | + t.Logf("Should not be able to list Secrets in %v.", dummyCluster) |
| 119 | + if err := kcpClient.List(ctx, &corev1.SecretList{}); err == nil { |
| 120 | + t.Fatal("Should not have been able to list Secrets, but was. Where have my permissions come from?") |
| 121 | + } |
| 122 | + |
| 123 | + // Now we extend the Kubeconfig with additional permissions. |
| 124 | + fpConfig.Spec.Authorization = &operatorv1alpha1.KubeconfigAuthorization{ |
| 125 | + ClusterRoleBindings: operatorv1alpha1.KubeconfigClusterRoleBindings{ |
| 126 | + WorkspacePath: dummyCluster.String(), |
| 127 | + ClusterRoles: []string{"cluster-admin"}, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + t.Log("Updating kubeconfig with permissions attached…") |
| 132 | + if err := client.Update(ctx, &fpConfig); err != nil { |
| 133 | + t.Fatal(err) |
| 134 | + } |
| 135 | + |
| 136 | + t.Logf("Should now be able to list Secrets in %v.", dummyCluster) |
| 137 | + err = wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 30*time.Second, false, func(ctx context.Context) (done bool, err error) { |
| 138 | + return kcpClient.List(ctx, &corev1.SecretList{}) == nil, nil |
| 139 | + }) |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("Failed to list Secrets in dummy workspace: %v", err) |
| 142 | + } |
| 143 | + |
| 144 | + // And now we remove the permissions again. |
| 145 | + t.Log("Updating kubeconfig to remove the attached permissions…") |
| 146 | + if err := client.Get(ctx, ctrlruntimeclient.ObjectKeyFromObject(&fpConfig), &fpConfig); err != nil { |
| 147 | + t.Fatal(err) |
| 148 | + } |
| 149 | + |
| 150 | + fpConfig.Spec.Authorization = nil |
| 151 | + |
| 152 | + if err := client.Update(ctx, &fpConfig); err != nil { |
| 153 | + t.Fatal(err) |
| 154 | + } |
| 155 | + |
| 156 | + t.Logf("Should no longer be able to list Secrets in %v.", dummyCluster) |
| 157 | + err = wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 30*time.Second, false, func(ctx context.Context) (done bool, err error) { |
| 158 | + return kcpClient.List(ctx, &corev1.SecretList{}) != nil, nil |
| 159 | + }) |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("Failed to wait for permissions to be gone: %v", err) |
| 162 | + } |
| 163 | +} |
0 commit comments