Skip to content

Commit 64eff1f

Browse files
committed
fix(registry): run validating admission on TenantSecret writes
The apiserver hands validating admission to a storage as the ValidateObjectFunc it passes into Create, Update and Delete: mutating admission runs in the handler, but validating webhooks and ValidatingAdmissionPolicies only execute when the storage calls that callback. TenantSecret bound all three to _, so every write answered as if no policy existed. The cluster-wide no-delete guardrail and any webhook written for tenantsecrets were inert on this resource, while application and securitygroup honour them. Call the callbacks on all four write paths, including the create-on-update branch, which is a create and so carries createValidation rather than updateValidation. The object handed to admission is the aggregated TenantSecret the caller submitted or the stored one it replaces, not the backing Secret, so a policy sees the resource it was written against. Assisted-by: LLM Signed-off-by: IvanHunters <[email protected]>
1 parent 37e22dc commit 64eff1f

2 files changed

Lines changed: 171 additions & 4 deletions

File tree

‎pkg/registry/core/tenantsecret/rest.go‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,20 @@ func buildTenantSelector(opts *metainternal.ListOptions) (labels.Selector, bool)
250250
func (r *REST) Create(
251251
ctx context.Context,
252252
obj runtime.Object,
253-
_ rest.ValidateObjectFunc,
253+
createValidation rest.ValidateObjectFunc,
254254
opts *metav1.CreateOptions,
255255
) (runtime.Object, error) {
256256
in, ok := obj.(*corev1alpha1.TenantSecret)
257257
if !ok {
258258
return nil, fmt.Errorf("expected TenantSecret, got %T", obj)
259259
}
260260

261+
if createValidation != nil {
262+
if err := createValidation(ctx, obj); err != nil {
263+
return nil, err
264+
}
265+
}
266+
261267
sec := tenantToSecret(in, nil)
262268
err := r.c.Create(ctx, sec, &client.CreateOptions{Raw: opts})
263269
if err != nil {
@@ -366,8 +372,8 @@ func (r *REST) Update(
366372
ctx context.Context,
367373
name string,
368374
objInfo rest.UpdatedObjectInfo,
369-
_ rest.ValidateObjectFunc,
370-
_ rest.ValidateObjectUpdateFunc,
375+
createValidation rest.ValidateObjectFunc,
376+
updateValidation rest.ValidateObjectUpdateFunc,
371377
forceCreate bool,
372378
opts *metav1.UpdateOptions,
373379
) (runtime.Object, bool, error) {
@@ -401,10 +407,21 @@ func (r *REST) Update(
401407
if !forceCreate {
402408
return nil, false, apierrors.NewNotFound(r.gvr.GroupResource(), name)
403409
}
410+
if createValidation != nil {
411+
if err := createValidation(ctx, newObj); err != nil {
412+
return nil, false, err
413+
}
414+
}
404415
err := r.c.Create(ctx, newSec, &client.CreateOptions{Raw: &metav1.CreateOptions{}})
405416
return secretToTenant(newSec), true, err
406417
}
407418

419+
if updateValidation != nil {
420+
if err := updateValidation(ctx, newObj, secretToTenant(cur)); err != nil {
421+
return nil, false, err
422+
}
423+
}
424+
408425
newSec.ResourceVersion = cur.ResourceVersion
409426
err = r.c.Update(ctx, newSec, &client.UpdateOptions{Raw: opts})
410427
return secretToTenant(newSec), false, err
@@ -413,7 +430,7 @@ func (r *REST) Update(
413430
func (r *REST) Delete(
414431
ctx context.Context,
415432
name string,
416-
_ rest.ValidateObjectFunc,
433+
deleteValidation rest.ValidateObjectFunc,
417434
opts *metav1.DeleteOptions,
418435
) (runtime.Object, bool, error) {
419436
ns, err := nsFrom(ctx)
@@ -427,6 +444,12 @@ func (r *REST) Delete(
427444
if current.Labels == nil || current.Labels[tsLabelKey] != tsLabelValue {
428445
return nil, false, apierrors.NewNotFound(r.gvr.GroupResource(), name)
429446
}
447+
if deleteValidation != nil {
448+
if err := deleteValidation(ctx, secretToTenant(current)); err != nil {
449+
return nil, false, err
450+
}
451+
}
452+
430453
err = r.c.Delete(ctx, &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Namespace: ns, Name: name}}, &client.DeleteOptions{Raw: opts})
431454
return nil, err == nil, err
432455
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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

Comments
 (0)