Skip to content

Commit 4996b9d

Browse files
authored
fix(api): publish open spec as x-kubernetes-preserve-unknown-fields, not additionalProperties:true (#2867)
## What this PR does `cozystack-api` published the free-form `.spec` of aggregated `apps.cozystack.io` resources (Tenant, Application, …) as the boolean `additionalProperties: true`. The Kubernetes ValidatingAdmissionPolicy **status type-checker** run by `kube-controller-manager` nil-dereferences when it recurses into that node (`k8s.io/apiserver/pkg/cel/openapi.isExtension`). As soon as a VAP matches one of these resources — `cozystack-tenant-host-policy` matches `tenants` — KCM `CrashLoopBackOff`s on every control-plane node, stalls reconciliation, and times out the install (`kubectl wait hr --all` never completes). With the 3× install retry removed, a single occurrence fails E2E outright. This emits `x-kubernetes-preserve-unknown-fields: true` instead, in `pkg/cmd/server/openapi.go::patchSpec` (both the schemaless and custom-schema branches, covering the OpenAPI v2 and v3 post-processors). It is semantically equivalent — "arbitrary fields allowed" — and the type-checker handles it safely. All managed-app custom schemas already use the safe **object-form** `additionalProperties`, so `patchSpec` was the only source of the boolean form. ### Regression test `TestPatchedSpecDoesNotPanicVAPTypeChecker` reproduces the exact crash through `k8s.io/apiserver/pkg/cel/openapi.SchemaDeclType` — the entry point KCM's `validatingadmissionpolicystatus` controller uses. It **panics on the pre-fix code and passes after the fix**. Companion structural tests assert the published spec carries `x-kubernetes-preserve-unknown-fields: true` and never the boolean `additionalProperties: true`, and that genuine object-form maps are left untouched. Verified against the pristine vendored `k8s.io/[email protected]` (the version cozystack-api builds against), which reproduces the panic, and previously confirmed end-to-end on `kindest/node:v1.33.1`. ### Upstream The underlying nil-dereference is a Kubernetes KCM bug — the VAP status type-checker should treat `additionalProperties: true` as dynamic rather than panic. A separate upstream report is being prepared; cozystack fixes the symptom immediately with the schema change above. Fixes #2863 ### Release note ```release-note fix(api): publish the free-form resource `.spec` as `x-kubernetes-preserve-unknown-fields` instead of `additionalProperties: true`, fixing a cluster-wide `kube-controller-manager` CrashLoopBackOff (nil-pointer panic in the ValidatingAdmissionPolicy status type-checker) that stalled installs. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Ensured published OpenAPI schemas no longer contain boolean-form additionalProperties anywhere; top-level and nested schemas now use preserve-unknown-fields for free-form specs, preventing validation/runtime issues. * **Tests** * Added regression tests that feed varied untrusted schemas and verify boolean additionalProperties are removed and published schemas remain valid for tooling. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 547500f + 379bc58 commit 4996b9d

2 files changed

Lines changed: 374 additions & 11 deletions

File tree

‎pkg/cmd/server/openapi.go‎

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,90 @@ func findSpecContainer(s *spec.Schema) *spec.Schema {
6363
return nil
6464
}
6565

66+
// markOpenObject marks a schema as a free-form object that accepts arbitrary
67+
// properties, using the x-kubernetes-preserve-unknown-fields extension rather
68+
// than the boolean additionalProperties:true form.
69+
//
70+
// This distinction matters: a boolean additionalProperties node (the JSON
71+
// `true` or `false` form) carries a nil inner schema. The Kubernetes
72+
// ValidatingAdmissionPolicy status type-checker (run by kube-controller-manager)
73+
// dereferences that nil inner schema, crash-looping KCM cluster-wide as soon as
74+
// a VAP matches one of these aggregated resources (e.g.
75+
// cozystack-tenant-host-policy on tenants). The extension is semantically
76+
// equivalent ("arbitrary fields allowed") and the type-checker handles it
77+
// safely. See https://github.com/cozystack/cozystack/issues/2863.
78+
func markOpenObject(s *spec.Schema) {
79+
if len(s.Type) == 0 {
80+
s.Type = spec.StringOrArray{"object"}
81+
}
82+
s.AdditionalProperties = nil
83+
if s.Extensions == nil {
84+
s.Extensions = spec.Extensions{}
85+
}
86+
s.Extensions["x-kubernetes-preserve-unknown-fields"] = true
87+
}
88+
89+
// sanitizeBooleanAdditionalProperties recursively rewrites every boolean-form
90+
// additionalProperties (one whose inner Schema is nil — both the JSON `true`
91+
// and `false` forms) anywhere in s, because both crash the VAP status
92+
// type-checker (#2863): additionalProperties:true becomes the equivalent
93+
// x-kubernetes-preserve-unknown-fields:true, and additionalProperties:false is
94+
// dropped (the published schema is consumed only for documentation and
95+
// type-checking, not for enforcement, so "declared properties only" is
96+
// preserved without the crash-prone node). Object-form additionalProperties (a
97+
// real map value schema) is safe; it is recursed into, never rewritten.
98+
//
99+
// ApplicationDefinition.openAPISchema is untrusted input — external-apps and
100+
// operator-authored definitions flow through here without validation — so this
101+
// closes the whole class of crash rather than only the schemas cozystack ships.
102+
func sanitizeBooleanAdditionalProperties(s *spec.Schema) {
103+
if s == nil {
104+
return
105+
}
106+
if ap := s.AdditionalProperties; ap != nil && ap.Schema == nil {
107+
if ap.Allows {
108+
markOpenObject(s) // additionalProperties:true -> preserve-unknown-fields
109+
} else {
110+
s.AdditionalProperties = nil // additionalProperties:false -> drop
111+
}
112+
} else if ap != nil && ap.Schema != nil {
113+
sanitizeBooleanAdditionalProperties(ap.Schema)
114+
}
115+
for k := range s.Properties {
116+
prop := s.Properties[k]
117+
sanitizeBooleanAdditionalProperties(&prop)
118+
s.Properties[k] = prop
119+
}
120+
if s.Items != nil {
121+
sanitizeBooleanAdditionalProperties(s.Items.Schema)
122+
for i := range s.Items.Schemas {
123+
sanitizeBooleanAdditionalProperties(&s.Items.Schemas[i])
124+
}
125+
}
126+
for i := range s.AllOf {
127+
sanitizeBooleanAdditionalProperties(&s.AllOf[i])
128+
}
129+
for i := range s.AnyOf {
130+
sanitizeBooleanAdditionalProperties(&s.AnyOf[i])
131+
}
132+
for i := range s.OneOf {
133+
sanitizeBooleanAdditionalProperties(&s.OneOf[i])
134+
}
135+
sanitizeBooleanAdditionalProperties(s.Not)
136+
}
137+
66138
// patchSpec injects/overrides ".spec" with user JSON (or schemaless object).
67139
func patchSpec(target *spec.Schema, raw string) error {
140+
if target.Properties == nil {
141+
target.Properties = map[string]spec.Schema{}
142+
}
143+
144+
// Schemaless: publish a fresh, fully open object. Starting from a fresh
145+
// schema (rather than mutating the cloned base) discards any inherited
146+
// $ref or properties so the published spec is unambiguously open.
68147
if strings.TrimSpace(raw) == "" {
69-
if target.Properties == nil {
70-
target.Properties = map[string]spec.Schema{}
71-
}
72-
prop := target.Properties["spec"]
73-
prop.AdditionalProperties = &spec.SchemaOrBool{Allows: true}
148+
prop := spec.Schema{}
149+
markOpenObject(&prop)
74150
target.Properties["spec"] = prop
75151
return nil
76152
}
@@ -79,12 +155,21 @@ func patchSpec(target *spec.Schema, raw string) error {
79155
if err := json.Unmarshal([]byte(raw), &custom); err != nil {
80156
return err
81157
}
82-
if custom.AdditionalProperties == nil {
83-
custom.AdditionalProperties = &spec.SchemaOrBool{Allows: true}
84-
}
85-
if target.Properties == nil {
86-
target.Properties = map[string]spec.Schema{}
158+
159+
// Neutralize any crash-prone boolean additionalProperties at any depth in
160+
// the user-supplied schema before publishing it.
161+
topLevelAPAbsent := custom.AdditionalProperties == nil
162+
sanitizeBooleanAdditionalProperties(&custom)
163+
164+
// Preserve the historical "spec is a superset of the documented values"
165+
// behavior: when the documented schema does not itself declare
166+
// additionalProperties, mark the top level open so undocumented Helm
167+
// values are accepted rather than rejected. A schema that explicitly
168+
// declared additionalProperties (now sanitized) keeps its own intent.
169+
if topLevelAPAbsent {
170+
markOpenObject(&custom)
87171
}
172+
88173
target.Properties["spec"] = custom
89174
return nil
90175
}
@@ -299,7 +384,7 @@ func sanitizeForV2(s *spec.Schema) {
299384
if hasIntAndStringAnyOf(s.AnyOf) {
300385
s.Type = spec.StringOrArray{"string"}
301386
if s.Extensions == nil {
302-
s.Extensions = map[string]any{}
387+
s.Extensions = spec.Extensions{}
303388
}
304389
s.Extensions["x-kubernetes-int-or-string"] = true
305390
}

0 commit comments

Comments
 (0)