Skip to content

Commit f7a9b57

Browse files
committed
fix(api): drop caller-supplied internal.cozystack.io keys in tenantsecret writes
Labels and annotations under internal.cozystack.io/ are platform verdicts, not tenant metadata. The lineage admission webhook stamps internal.cozystack.io/tenantresource to decide whether a Secret is visible through this API at all, and the cacert controller stamps internal.cozystack.io/tenant-ca, which an ApplicationDefinition selects on in spec.secrets to promote a trust anchor to the tenant. The registry copied every caller-supplied key onto the backing Secret verbatim, so a principal holding write access to tenantsecrets could plant either one on a Secret of its choosing, or strip one to take an object out of the platform's view. Only read-only tenant RBAC, which lives in a different package, stood between a caller and that path. tenantToSecret is the choke point every write verb goes through, PATCH included: rest.Patcher is Getter+Updater, so the endpoint installer serves PATCH out of Update. It now skips the platform keys, and since the update target starts as a copy of the stored Secret, ignoring the caller there also means a platform key cannot be removed. Guard on the whole prefix rather than on a list of keys, so a platform key added later is protected without anyone remembering to come back here. The Patch method on this type is a second write path that no request reaches, since its signature matches no interface the installer asserts. It gets the same treatment rather than being left as the weaker of two doors: the raw patch lands on the backing Secret with no conversion, so it restores the platform keys afterwards, which generalises the tenant-resource marker repair already there. That repair no longer swallows its error -- a write that could not be put right must not report success. Keys stay visible on read. A tenant may see the platform's verdicts, and failing a write that echoes them back would break a read-modify-write round trip for no gain, so they are dropped silently instead of rejected. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 6c82988 commit f7a9b57

2 files changed

Lines changed: 432 additions & 10 deletions

File tree

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

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"net/http"
1212
"strconv"
13+
"strings"
1314
"time"
1415

1516
corev1 "k8s.io/api/core/v1"
@@ -43,9 +44,55 @@ const (
4344
singularName = "tenantsecret"
4445
kindTenantSecret = "TenantSecret"
4546
kindTenantSecretList = "TenantSecretList"
47+
48+
// internalPrefix namespaces the labels and annotations the platform owns:
49+
// the lineage webhook's tenantresource verdict, the cacert controller's
50+
// tenant-ca selector and its ownership markers. They are the platform's
51+
// answer to "may this tenant see this Secret", and an ApplicationDefinition
52+
// selects on them, so a caller writing through this API must not be able to
53+
// plant or strip one on the backing Secret. The guard is on the prefix
54+
// rather than on a list of keys so a platform key added later is protected
55+
// without anyone remembering to come back here.
56+
internalPrefix = "internal.cozystack.io/"
4657
)
4758

48-
func stripInternal(m map[string]string) map[string]string {
59+
// isInternalKey reports whether a metadata key belongs to the platform.
60+
func isInternalKey(k string) bool { return strings.HasPrefix(k, internalPrefix) }
61+
62+
// restoreInternal rewrites the platform-owned entries of m to exactly those of
63+
// cur, leaving every other entry alone, and reports whether m changed. It is
64+
// the repair half of the guard, for the write path that reaches the backing
65+
// Secret without passing through tenantToSecret.
66+
func restoreInternal(m, cur map[string]string) (map[string]string, bool) {
67+
changed := false
68+
for k := range m {
69+
if !isInternalKey(k) {
70+
continue
71+
}
72+
if _, keep := cur[k]; !keep {
73+
delete(m, k)
74+
changed = true
75+
}
76+
}
77+
for k, v := range cur {
78+
if !isInternalKey(k) {
79+
continue
80+
}
81+
if got, ok := m[k]; !ok || got != v {
82+
if m == nil {
83+
m = map[string]string{}
84+
}
85+
m[k] = v
86+
changed = true
87+
}
88+
}
89+
return m, changed
90+
}
91+
92+
// stripTenantMarker hides the tenant-resource marker, and only that key. The
93+
// read side is deliberately narrower than the write side: a tenant may see the
94+
// platform's other verdicts, tenant-ca among them, it just cannot write them.
95+
func stripTenantMarker(m map[string]string) map[string]string {
4996
if m == nil {
5097
return nil
5198
}
@@ -93,7 +140,7 @@ func secretToTenant(sec *corev1.Secret) *corev1alpha1.TenantSecret {
93140
UID: sec.UID,
94141
ResourceVersion: sec.ResourceVersion,
95142
CreationTimestamp: sec.CreationTimestamp,
96-
Labels: stripInternal(sec.Labels),
143+
Labels: stripTenantMarker(sec.Labels),
97144
Annotations: sec.Annotations,
98145
},
99146
Type: string(sec.Type),
@@ -115,13 +162,24 @@ func tenantToSecret(ts *corev1alpha1.TenantSecret, cur *corev1.Secret) *corev1.S
115162
}
116163
out.Labels[tsLabelKey] = tsLabelValue
117164
for k, v := range ts.Labels {
165+
// Platform keys are dropped rather than rejected: a caller that GETs an
166+
// object, edits it and PUTs it back sends them straight back at us, and
167+
// failing that round-trip would break `kubectl edit` for no gain. On an
168+
// update out starts as a copy of the stored Secret, so ignoring the
169+
// caller here also means a platform key cannot be stripped.
170+
if isInternalKey(k) {
171+
continue
172+
}
118173
out.Labels[k] = v
119174
}
120175

121176
if out.Annotations == nil {
122177
out.Annotations = map[string]string{}
123178
}
124179
for k, v := range ts.Annotations {
180+
if isInternalKey(k) {
181+
continue
182+
}
125183
out.Annotations[k] = v
126184
}
127185

@@ -434,14 +492,27 @@ func (r *REST) Patch(
434492
return nil, err
435493
}
436494

437-
// Ensure tenant secret label is preserved
438-
if out.Labels == nil {
439-
out.Labels = make(map[string]string)
440-
}
441-
442-
if out.Labels[tsLabelKey] != tsLabelValue {
443-
out.Labels[tsLabelKey] = tsLabelValue
444-
_ = r.c.Update(ctx, out, &client.UpdateOptions{Raw: &metav1.UpdateOptions{}})
495+
// This method is not what serves a PATCH request: rest.Patcher is
496+
// Getter+Updater, so the endpoint installer routes PATCH through Update,
497+
// and the guard that covers it is the one in tenantToSecret. Nothing
498+
// reaches this code today. It is kept in step anyway rather than left as
499+
// the weaker of two doors — the raw patch here lands on the backing Secret
500+
// without any conversion, so the platform keys have to be put back
501+
// afterwards, the tenant-resource marker among them.
502+
lbls, lblsChanged := restoreInternal(out.Labels, current.Labels)
503+
anns, annsChanged := restoreInternal(out.Annotations, current.Annotations)
504+
if lblsChanged || annsChanged {
505+
out.Labels, out.Annotations = lbls, anns
506+
// Carry the caller's options over: this repair now fires on any platform
507+
// key, not just a missing marker, so a dry-run patch would otherwise
508+
// commit it for real.
509+
raw := &metav1.UpdateOptions{}
510+
if opts != nil {
511+
raw.DryRun, raw.FieldManager, raw.FieldValidation = opts.DryRun, opts.FieldManager, opts.FieldValidation
512+
}
513+
if err := r.c.Update(ctx, out, &client.UpdateOptions{Raw: raw}); err != nil {
514+
return nil, err
515+
}
445516
}
446517

447518
return secretToTenant(out), nil

0 commit comments

Comments
 (0)