|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +// Copyright 2020-2023 Project Capsule Authors. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package e2e |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + "math/rand" |
| 13 | + |
| 14 | + . "github.com/onsi/ginkgo/v2" |
| 15 | + . "github.com/onsi/gomega" |
| 16 | + capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + "k8s.io/apimachinery/pkg/types" |
| 19 | +) |
| 20 | + |
| 21 | +var _ = Describe("creating several Namespaces for a Tenant", func() { |
| 22 | + tnt := &capsulev1beta2.Tenant{ |
| 23 | + ObjectMeta: metav1.ObjectMeta{ |
| 24 | + Name: "capsule-ns-attack-1", |
| 25 | + }, |
| 26 | + Spec: capsulev1beta2.TenantSpec{ |
| 27 | + Owners: capsulev1beta2.OwnerListSpec{ |
| 28 | + { |
| 29 | + Name: "charlie", |
| 30 | + Kind: "User", |
| 31 | + }, |
| 32 | + { |
| 33 | + Kind: "ServiceAccount", |
| 34 | + Name: "system:serviceaccount:attacker-system:attacker", |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + kubeSystem := &corev1.Namespace{ |
| 41 | + ObjectMeta: metav1.ObjectMeta{ |
| 42 | + Name: "kube-system", |
| 43 | + }, |
| 44 | + } |
| 45 | + JustBeforeEach(func() { |
| 46 | + EventuallyCreation(func() (err error) { |
| 47 | + tnt.ResourceVersion = "" |
| 48 | + err = k8sClient.Create(context.TODO(), tnt) |
| 49 | + |
| 50 | + return |
| 51 | + }).Should(Succeed()) |
| 52 | + }) |
| 53 | + JustAfterEach(func() { |
| 54 | + Expect(k8sClient.Delete(context.TODO(), tnt)).Should(Succeed()) |
| 55 | + |
| 56 | + }) |
| 57 | + |
| 58 | + It("Can't hijack offlimits namespace", func() { |
| 59 | + tenant := &capsulev1beta2.Tenant{} |
| 60 | + Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: tnt.Name}, tenant)).Should(Succeed()) |
| 61 | + |
| 62 | + // Get the namespace |
| 63 | + Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: kubeSystem.GetName()}, kubeSystem)).Should(Succeed()) |
| 64 | + |
| 65 | + for _, owner := range tnt.Spec.Owners { |
| 66 | + cs := ownerClient(owner) |
| 67 | + |
| 68 | + patch := []byte(fmt.Sprintf(`{"metadata":{"ownerReferences":[{"apiVersion":"%s/%s","kind":"Tenant","name":"%s","uid":"%s"}]}}`, capsulev1beta2.GroupVersion.Group, capsulev1beta2.GroupVersion.Version, tenant.GetName(), tenant.GetUID())) |
| 69 | + |
| 70 | + _, err := cs.CoreV1().Namespaces().Patch(context.TODO(), kubeSystem.Name, types.StrategicMergePatchType, patch, metav1.PatchOptions{}) |
| 71 | + Expect(err).To(HaveOccurred()) |
| 72 | + |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + It("Owners can create and attempt to patch new namespaces but patches should not be applied", func() { |
| 77 | + for _, owner := range tnt.Spec.Owners { |
| 78 | + cs := ownerClient(owner) |
| 79 | + |
| 80 | + // Each owner creates a new namespace |
| 81 | + ns := NewNamespace("") |
| 82 | + NamespaceCreation(ns, owner, defaultTimeoutInterval).Should(Succeed()) |
| 83 | + |
| 84 | + // Attempt to patch the owner references of the new namespace |
| 85 | + tenant := &capsulev1beta2.Tenant{} |
| 86 | + Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: tnt.Name}, tenant)).Should(Succeed()) |
| 87 | + |
| 88 | + randomUID := types.UID(fmt.Sprintf("%d", rand.Int())) |
| 89 | + randomName := fmt.Sprintf("random-tenant-%d", rand.Int()) |
| 90 | + patch := []byte(fmt.Sprintf(`{"metadata":{"ownerReferences":[{"apiVersion":"%s/%s","kind":"Tenant","name":"%s","uid":"%s"}]}}`, capsulev1beta2.GroupVersion.Group, capsulev1beta2.GroupVersion.Version, randomName, randomUID)) |
| 91 | + |
| 92 | + _, err := cs.CoreV1().Namespaces().Patch(context.TODO(), ns.Name, types.StrategicMergePatchType, patch, metav1.PatchOptions{}) |
| 93 | + Expect(err).ToNot(HaveOccurred()) |
| 94 | + |
| 95 | + retrievedNs := &corev1.Namespace{} |
| 96 | + Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: ns.Name}, retrievedNs)).Should(Succeed()) |
| 97 | + |
| 98 | + // Check if the namespace has an owner reference with the specific UID and name |
| 99 | + hasSpecificOwnerRef := false |
| 100 | + for _, ownerRef := range retrievedNs.OwnerReferences { |
| 101 | + if ownerRef.UID == randomUID && ownerRef.Name == randomName { |
| 102 | + hasSpecificOwnerRef = true |
| 103 | + break |
| 104 | + } |
| 105 | + } |
| 106 | + Expect(hasSpecificOwnerRef).To(BeFalse(), "Namespace should not have owner reference with UID %s and name %s", randomUID, randomName) |
| 107 | + |
| 108 | + hasOriginReference := false |
| 109 | + for _, ownerRef := range retrievedNs.OwnerReferences { |
| 110 | + if ownerRef.UID == tenant.GetUID() && ownerRef.Name == tenant.GetName() { |
| 111 | + hasOriginReference = true |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + Expect(hasOriginReference).To(BeTrue(), "Namespace should have origin reference", tenant.GetUID(), tenant.GetName()) |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | +}) |
0 commit comments