|
| 1 | +package e2e_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2" |
| 9 | + capsulemeta "github.com/projectcapsule/capsule/pkg/api/meta" |
| 10 | + capsulerbac "github.com/projectcapsule/capsule/pkg/api/rbac" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + rbacv1 "k8s.io/api/rbac/v1" |
| 13 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/types" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + |
| 18 | + "github.com/projectcapsule/capsule-proxy/internal/controllers" |
| 19 | +) |
| 20 | + |
| 21 | +var _ = Describe("RoleBinding reflection", func() { |
| 22 | + const ( |
| 23 | + reflectedTenantName = "rbac-reflected-tenant" |
| 24 | + privateTenantName = "rbac-private-tenant" |
| 25 | + reflectedNamespace = "rbac-reflected-namespace" |
| 26 | + privateNamespace = "rbac-private-namespace" |
| 27 | + ) |
| 28 | + |
| 29 | + owner := capsulerbac.OwnerListSpec{{ |
| 30 | + CoreOwnerSpec: capsulerbac.CoreOwnerSpec{ |
| 31 | + UserSpec: capsulerbac.UserSpec{Name: "alice", Kind: "User"}, |
| 32 | + }, |
| 33 | + }} |
| 34 | + |
| 35 | + reflectedTenant := &capsulev1beta2.Tenant{ |
| 36 | + ObjectMeta: metav1.ObjectMeta{Name: reflectedTenantName, Labels: e2eLabels()}, |
| 37 | + Spec: capsulev1beta2.TenantSpec{ |
| 38 | + Owners: owner, |
| 39 | + AdditionalRoleBindings: []capsulerbac.AdditionalRoleBindingsSpec{{ |
| 40 | + ClusterRoleName: "view", |
| 41 | + Subjects: []rbacv1.Subject{{ |
| 42 | + Kind: rbacv1.UserKind, APIGroup: rbacv1.GroupName, Name: "bob", |
| 43 | + }}, |
| 44 | + Labels: map[string]string{controllers.RoleBindingReflectionLabel: "true"}, |
| 45 | + }}, |
| 46 | + }, |
| 47 | + } |
| 48 | + privateTenant := &capsulev1beta2.Tenant{ |
| 49 | + ObjectMeta: metav1.ObjectMeta{Name: privateTenantName, Labels: e2eLabels()}, |
| 50 | + Spec: capsulev1beta2.TenantSpec{Owners: owner}, |
| 51 | + } |
| 52 | + |
| 53 | + BeforeEach(func() { |
| 54 | + for _, tenant := range []*capsulev1beta2.Tenant{reflectedTenant, privateTenant} { |
| 55 | + tenant := tenant |
| 56 | + Eventually(func() error { |
| 57 | + tenant.ResourceVersion = "" |
| 58 | + |
| 59 | + return k8sClient.Create(context.Background(), tenant) |
| 60 | + }, defaultTimeoutInterval, defaultPollInterval).Should(Succeed()) |
| 61 | + } |
| 62 | + |
| 63 | + for _, namespace := range []*corev1.Namespace{ |
| 64 | + NewNamespace(reflectedNamespace, map[string]string{capsulemeta.TenantLabel: reflectedTenantName}), |
| 65 | + NewNamespace(privateNamespace, map[string]string{capsulemeta.TenantLabel: privateTenantName}), |
| 66 | + } { |
| 67 | + NamespaceCreation(namespace, owner[0], defaultTimeoutInterval).Should(Succeed()) |
| 68 | + } |
| 69 | + |
| 70 | + Eventually(func(g Gomega) { |
| 71 | + bindings := &rbacv1.RoleBindingList{} |
| 72 | + g.Expect(k8sClient.List(context.Background(), bindings, |
| 73 | + client.InNamespace(reflectedNamespace), |
| 74 | + client.MatchingLabels{controllers.RoleBindingReflectionLabel: "true"}, |
| 75 | + )).To(Succeed()) |
| 76 | + g.Expect(bindings.Items).To(HaveLen(1)) |
| 77 | + g.Expect(bindings.Items[0].RoleRef.Kind).To(Equal("ClusterRole")) |
| 78 | + g.Expect(bindings.Items[0].RoleRef.Name).To(Equal("view")) |
| 79 | + }, defaultTimeoutInterval, defaultPollInterval).Should(Succeed()) |
| 80 | + }) |
| 81 | + |
| 82 | + JustAfterEach(func() { |
| 83 | + for _, name := range []string{reflectedNamespace, privateNamespace} { |
| 84 | + name := name |
| 85 | + Eventually(func() error { |
| 86 | + return client.IgnoreNotFound(k8sClient.Delete(context.Background(), &corev1.Namespace{ |
| 87 | + ObjectMeta: metav1.ObjectMeta{Name: name}, |
| 88 | + })) |
| 89 | + }, defaultTimeoutInterval, defaultPollInterval).Should(Succeed()) |
| 90 | + Eventually(func() bool { |
| 91 | + err := k8sClient.Get(context.Background(), types.NamespacedName{Name: name}, &corev1.Namespace{}) |
| 92 | + |
| 93 | + return apierrors.IsNotFound(err) |
| 94 | + }, defaultTimeoutInterval, defaultPollInterval).Should(BeTrue()) |
| 95 | + } |
| 96 | + |
| 97 | + Eventually(func() error { |
| 98 | + return cleanResources([]client.Object{&capsulev1beta2.Tenant{}}, e2eSelector()) |
| 99 | + }, defaultTimeoutInterval, defaultPollInterval).Should(Succeed()) |
| 100 | + }) |
| 101 | + |
| 102 | + It("lists only the namespace with the labelled additional RoleBinding", func() { |
| 103 | + bobClient, err := loadKubeConfig("bob") |
| 104 | + Expect(err).NotTo(HaveOccurred()) |
| 105 | + |
| 106 | + Eventually(func() ([]string, error) { |
| 107 | + list, err := bobClient.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{}) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + names := make([]string, 0, len(list.Items)) |
| 113 | + for i := range list.Items { |
| 114 | + names = append(names, list.Items[i].Name) |
| 115 | + } |
| 116 | + |
| 117 | + return names, nil |
| 118 | + }, defaultTimeoutInterval, defaultPollInterval). |
| 119 | + Should(ConsistOf(reflectedNamespace)) |
| 120 | + }) |
| 121 | + |
| 122 | + It("lists Pods only from the tenant with the labelled additional RoleBinding", func() { |
| 123 | + pods := []*corev1.Pod{ |
| 124 | + { |
| 125 | + ObjectMeta: metav1.ObjectMeta{Name: "reflected-pod", Namespace: reflectedNamespace, Labels: map[string]string{capsulemeta.ManagedByCapsuleLabel: reflectedTenantName}}, |
| 126 | + Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "pause", Image: "registry.k8s.io/pause:3.10"}}}, |
| 127 | + }, |
| 128 | + { |
| 129 | + ObjectMeta: metav1.ObjectMeta{Name: "private-pod", Namespace: privateNamespace, Labels: map[string]string{capsulemeta.ManagedByCapsuleLabel: privateTenantName}}, |
| 130 | + Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "pause", Image: "registry.k8s.io/pause:3.10"}}}, |
| 131 | + }, |
| 132 | + } |
| 133 | + for _, pod := range pods { |
| 134 | + pod := pod |
| 135 | + Eventually(func() error { |
| 136 | + return k8sClient.Create(context.Background(), pod) |
| 137 | + }, defaultTimeoutInterval, defaultPollInterval).Should(Succeed()) |
| 138 | + } |
| 139 | + |
| 140 | + bobClient, err := loadKubeConfig("bob") |
| 141 | + Expect(err).NotTo(HaveOccurred()) |
| 142 | + |
| 143 | + Eventually(func() ([]string, error) { |
| 144 | + list, err := bobClient.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{}) |
| 145 | + if err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + |
| 149 | + names := make([]string, 0, len(list.Items)) |
| 150 | + for i := range list.Items { |
| 151 | + names = append(names, list.Items[i].Name) |
| 152 | + } |
| 153 | + |
| 154 | + return names, nil |
| 155 | + }, defaultTimeoutInterval, defaultPollInterval). |
| 156 | + Should(ConsistOf("reflected-pod")) |
| 157 | + }) |
| 158 | +}) |
0 commit comments