|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package tenantsecret |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "testing" |
| 9 | + |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/runtime" |
| 14 | + "k8s.io/apimachinery/pkg/types" |
| 15 | + "k8s.io/apiserver/pkg/registry/rest" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + |
| 18 | + corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1" |
| 19 | +) |
| 20 | + |
| 21 | +// The apiserver delivers validating admission to a storage as the |
| 22 | +// ValidateObjectFunc it passes into Create, Update and Delete: mutating |
| 23 | +// admission runs in the handler, but validating webhooks and |
| 24 | +// ValidatingAdmissionPolicies run only when the storage calls that callback. |
| 25 | +// A storage that drops it answers every write as if no policy existed. |
| 26 | + |
| 27 | +var errDenied = errors.New("denied by test admission") |
| 28 | + |
| 29 | +func denyCreate(called *bool) rest.ValidateObjectFunc { |
| 30 | + return func(_ context.Context, _ runtime.Object) error { |
| 31 | + *called = true |
| 32 | + return errDenied |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func denyUpdate(called *bool) rest.ValidateObjectUpdateFunc { |
| 37 | + return func(_ context.Context, _, _ runtime.Object) error { |
| 38 | + *called = true |
| 39 | + return errDenied |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func tenantSecretIn(name string) *corev1alpha1.TenantSecret { |
| 44 | + return &corev1alpha1.TenantSecret{ |
| 45 | + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: testNamespace}, |
| 46 | + Type: string(corev1.SecretTypeOpaque), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func secretExists(t *testing.T, r *REST, name string) bool { |
| 51 | + t.Helper() |
| 52 | + err := r.c.Get(testCtx(), types.NamespacedName{Namespace: testNamespace, Name: name}, &corev1.Secret{}, &client.GetOptions{}) |
| 53 | + if err != nil && !apierrors.IsNotFound(err) { |
| 54 | + t.Fatalf("get backing Secret %q: %v", name, err) |
| 55 | + } |
| 56 | + return err == nil |
| 57 | +} |
| 58 | + |
| 59 | +func TestCreateRunsValidatingAdmission(t *testing.T) { |
| 60 | + r := newTestREST(t) |
| 61 | + called := false |
| 62 | + |
| 63 | + _, err := r.Create(testCtx(), tenantSecretIn("creds"), denyCreate(&called), &metav1.CreateOptions{}) |
| 64 | + if !called { |
| 65 | + t.Fatal("Create never invoked validating admission") |
| 66 | + } |
| 67 | + if !errors.Is(err, errDenied) { |
| 68 | + t.Fatalf("Create swallowed the admission denial: got %v", err) |
| 69 | + } |
| 70 | + if secretExists(t, r, "creds") { |
| 71 | + t.Error("Create wrote the backing Secret despite the denial") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestUpdateRunsValidatingAdmission(t *testing.T) { |
| 76 | + r := newTestREST(t, makeTenantSecret("creds", nil)) |
| 77 | + called := false |
| 78 | + |
| 79 | + in := tenantSecretIn("creds") |
| 80 | + in.Data = map[string][]byte{"k": []byte("v")} |
| 81 | + _, _, err := r.Update(testCtx(), "creds", rest.DefaultUpdatedObjectInfo(in), nil, denyUpdate(&called), false, &metav1.UpdateOptions{}) |
| 82 | + if !called { |
| 83 | + t.Fatal("Update never invoked validating admission") |
| 84 | + } |
| 85 | + if !errors.Is(err, errDenied) { |
| 86 | + t.Fatalf("Update swallowed the admission denial: got %v", err) |
| 87 | + } |
| 88 | + if got := backingSecret(t, r, "creds").Data["k"]; got != nil { |
| 89 | + t.Errorf("Update wrote the backing Secret despite the denial: got %q", got) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestUpdateForceCreateRunsValidatingAdmission(t *testing.T) { |
| 94 | + // The create-on-update path is a create, so it is createValidation that |
| 95 | + // carries admission for it. |
| 96 | + r := newTestREST(t) |
| 97 | + called := false |
| 98 | + |
| 99 | + _, _, err := r.Update(testCtx(), "creds", rest.DefaultUpdatedObjectInfo(tenantSecretIn("creds")), denyCreate(&called), nil, true, &metav1.UpdateOptions{}) |
| 100 | + if !called { |
| 101 | + t.Fatal("force-create Update never invoked validating admission") |
| 102 | + } |
| 103 | + if !errors.Is(err, errDenied) { |
| 104 | + t.Fatalf("force-create Update swallowed the admission denial: got %v", err) |
| 105 | + } |
| 106 | + if secretExists(t, r, "creds") { |
| 107 | + t.Error("force-create Update wrote the backing Secret despite the denial") |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestDeleteRunsValidatingAdmission(t *testing.T) { |
| 112 | + // This is the path the cluster-wide no-delete guardrail runs on. |
| 113 | + r := newTestREST(t, makeTenantSecret("creds", nil)) |
| 114 | + called := false |
| 115 | + |
| 116 | + _, deleted, err := r.Delete(testCtx(), "creds", denyCreate(&called), &metav1.DeleteOptions{}) |
| 117 | + if !called { |
| 118 | + t.Fatal("Delete never invoked validating admission") |
| 119 | + } |
| 120 | + if !errors.Is(err, errDenied) { |
| 121 | + t.Fatalf("Delete swallowed the admission denial: got %v", err) |
| 122 | + } |
| 123 | + if deleted { |
| 124 | + t.Error("Delete reported the object as deleted despite the denial") |
| 125 | + } |
| 126 | + if !secretExists(t, r, "creds") { |
| 127 | + t.Error("Delete removed the backing Secret despite the denial") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestWritesAcceptNilValidation(t *testing.T) { |
| 132 | + // The apiserver passes a nil callback when no admission plugin handles the |
| 133 | + // operation, so every write path must stay usable without one. |
| 134 | + r := newTestREST(t) |
| 135 | + if _, err := r.Create(testCtx(), tenantSecretIn("creds"), nil, &metav1.CreateOptions{}); err != nil { |
| 136 | + t.Fatalf("Create with nil validation: %v", err) |
| 137 | + } |
| 138 | + if _, _, err := r.Update(testCtx(), "creds", rest.DefaultUpdatedObjectInfo(tenantSecretIn("creds")), nil, nil, false, &metav1.UpdateOptions{}); err != nil { |
| 139 | + t.Fatalf("Update with nil validation: %v", err) |
| 140 | + } |
| 141 | + if _, _, err := r.Delete(testCtx(), "creds", nil, &metav1.DeleteOptions{}); err != nil { |
| 142 | + t.Fatalf("Delete with nil validation: %v", err) |
| 143 | + } |
| 144 | +} |
0 commit comments