Repository navigation
Expand file tree
/
Copy pathopenapi.go
More file actions
519 lines (466 loc) · 16 KB
/
Copy pathopenapi.go
File metadata and controls
519 lines (466 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package server
import (
"encoding/json"
"fmt"
"strings"
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
"github.com/cozystack/cozystack/pkg/apiserver"
"github.com/cozystack/cozystack/pkg/config"
sampleopenapi "github.com/cozystack/cozystack/pkg/generated/openapi"
"k8s.io/apiserver/pkg/endpoints/openapi"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/kube-openapi/pkg/spec3"
"k8s.io/kube-openapi/pkg/validation/spec"
)
// -----------------------------------------------------------------------------
// shared helpers
// -----------------------------------------------------------------------------
const (
apiPrefix = "com.github.cozystack.cozystack.pkg.apis.apps.v1alpha1"
baseRef = apiPrefix + ".Application"
baseListRef = apiPrefix + ".ApplicationList"
baseStatusRef = apiPrefix + ".ApplicationStatus"
smp = "application/strategic-merge-patch+json"
)
// deepCopySchema clones *spec.Schema via JSON-marshal/unmarshal.
func deepCopySchema(in *spec.Schema) *spec.Schema {
if in == nil {
return nil
}
raw, err := json.Marshal(in)
if err != nil {
panic(fmt.Errorf("failed to marshal schema: %w", err))
}
var out spec.Schema
err = json.Unmarshal(raw, &out)
if err != nil {
panic(fmt.Errorf("failed to unmarshal schema: %w", err))
}
return &out
}
// findSpecContainer returns first object owning ".spec".
func findSpecContainer(s *spec.Schema) *spec.Schema {
if s == nil {
return nil
}
if len(s.Type) > 0 && s.Type.Contains("object") {
if _, ok := s.Properties["spec"]; ok {
return s
}
}
for _, branch := range [][]spec.Schema{s.AllOf, s.OneOf, s.AnyOf} {
for i := range branch {
if res := findSpecContainer(&branch[i]); res != nil {
return res
}
}
}
return nil
}
// markOpenObject marks a schema as a free-form object that accepts arbitrary
// properties, using the x-kubernetes-preserve-unknown-fields extension rather
// than the boolean additionalProperties:true form.
//
// This distinction matters: a boolean additionalProperties node (the JSON
// `true` or `false` form) carries a nil inner schema. The Kubernetes
// ValidatingAdmissionPolicy status type-checker (run by kube-controller-manager)
// dereferences that nil inner schema, crash-looping KCM cluster-wide as soon as
// a VAP matches one of these aggregated resources (e.g.
// cozystack-tenant-host-policy on tenants). The extension is semantically
// equivalent ("arbitrary fields allowed") and the type-checker handles it
// safely. See https://github.com/cozystack/cozystack/issues/2863.
func markOpenObject(s *spec.Schema) {
if len(s.Type) == 0 {
s.Type = spec.StringOrArray{"object"}
}
s.AdditionalProperties = nil
if s.Extensions == nil {
s.Extensions = spec.Extensions{}
}
s.Extensions["x-kubernetes-preserve-unknown-fields"] = true
}
// sanitizeBooleanAdditionalProperties recursively rewrites every boolean-form
// additionalProperties (one whose inner Schema is nil — both the JSON `true`
// and `false` forms) anywhere in s, because both crash the VAP status
// type-checker (#2863): additionalProperties:true becomes the equivalent
// x-kubernetes-preserve-unknown-fields:true, and additionalProperties:false is
// dropped (the published schema is consumed only for documentation and
// type-checking, not for enforcement, so "declared properties only" is
// preserved without the crash-prone node). Object-form additionalProperties (a
// real map value schema) is safe; it is recursed into, never rewritten.
//
// ApplicationDefinition.openAPISchema is untrusted input — external-apps and
// operator-authored definitions flow through here without validation — so this
// closes the whole class of crash rather than only the schemas cozystack ships.
func sanitizeBooleanAdditionalProperties(s *spec.Schema) {
if s == nil {
return
}
if ap := s.AdditionalProperties; ap != nil && ap.Schema == nil {
if ap.Allows {
markOpenObject(s) // additionalProperties:true -> preserve-unknown-fields
} else {
s.AdditionalProperties = nil // additionalProperties:false -> drop
}
} else if ap != nil && ap.Schema != nil {
sanitizeBooleanAdditionalProperties(ap.Schema)
}
for k := range s.Properties {
prop := s.Properties[k]
sanitizeBooleanAdditionalProperties(&prop)
s.Properties[k] = prop
}
if s.Items != nil {
sanitizeBooleanAdditionalProperties(s.Items.Schema)
for i := range s.Items.Schemas {
sanitizeBooleanAdditionalProperties(&s.Items.Schemas[i])
}
}
for i := range s.AllOf {
sanitizeBooleanAdditionalProperties(&s.AllOf[i])
}
for i := range s.AnyOf {
sanitizeBooleanAdditionalProperties(&s.AnyOf[i])
}
for i := range s.OneOf {
sanitizeBooleanAdditionalProperties(&s.OneOf[i])
}
sanitizeBooleanAdditionalProperties(s.Not)
}
// patchSpec injects/overrides ".spec" with user JSON (or schemaless object).
func patchSpec(target *spec.Schema, raw string) error {
if target.Properties == nil {
target.Properties = map[string]spec.Schema{}
}
// Schemaless: publish a fresh, fully open object. Starting from a fresh
// schema (rather than mutating the cloned base) discards any inherited
// $ref or properties so the published spec is unambiguously open.
if strings.TrimSpace(raw) == "" {
prop := spec.Schema{}
markOpenObject(&prop)
target.Properties["spec"] = prop
return nil
}
var custom spec.Schema
if err := json.Unmarshal([]byte(raw), &custom); err != nil {
return err
}
// The name declaration describes metadata.name, not anything inside spec.
delete(custom.Extensions, appsv1alpha1.NameSchemaExtension)
// Neutralize any crash-prone boolean additionalProperties at any depth in
// the user-supplied schema before publishing it.
topLevelAPAbsent := custom.AdditionalProperties == nil
sanitizeBooleanAdditionalProperties(&custom)
// Preserve the historical "spec is a superset of the documented values"
// behavior: when the documented schema does not itself declare
// additionalProperties, mark the top level open so undocumented Helm
// values are accepted rather than rejected. A schema that explicitly
// declared additionalProperties (now sanitized) keeps its own intent.
if topLevelAPAbsent {
markOpenObject(&custom)
}
target.Properties["spec"] = custom
return nil
}
/* ────────────────────────────────────────────────────────────────────────── */
/* DRY helpers */
/* ────────────────────────────────────────────────────────────────────────── */
// cloneKindSchemas: from base schemas, create new schemas for a specific kind.
func cloneKindSchemas(kind string, base, baseStatus, baseList *spec.Schema, v3 bool) (obj, status, list *spec.Schema) {
obj = deepCopySchema(base)
status = deepCopySchema(baseStatus)
list = deepCopySchema(baseList)
// Ensure we have valid clones
if obj == nil || status == nil || list == nil {
return nil, nil, nil
}
// GVK-extensions
setGVK := func(s *spec.Schema, k string) {
s.Extensions = map[string]any{
"x-kubernetes-group-version-kind": []any{
map[string]any{"group": "apps.cozystack.io", "version": "v1alpha1", "kind": k},
},
}
}
setGVK(obj, kind)
setGVK(list, kind+"List")
// fix refs
refPrefix := "#/components/schemas/" // v3
if !v3 {
refPrefix = "#/definitions/"
}
statusRef := refPrefix + apiPrefix + "." + kind + "Status"
itemRef := refPrefix + apiPrefix + "." + kind
if prop, ok := obj.Properties["status"]; ok {
prop.Ref = spec.MustCreateRef(statusRef)
obj.Properties["status"] = prop
}
if list.Properties != nil {
if items := list.Properties["items"]; items.Items != nil && items.Items.Schema != nil {
items.Items.Schema.Ref = spec.MustCreateRef(itemRef)
list.Properties["items"] = items
}
}
return
}
// rewriteDocRefs rewrites all $ref in the OpenAPI document
func rewriteDocRefs(doc any) ([]byte, error) {
raw, err := json.Marshal(doc)
if err != nil {
return nil, fmt.Errorf("failed to marshal OpenAPI document: %w", err)
}
var parsed any
if err := json.Unmarshal(raw, &parsed); err != nil {
return nil, err
}
walkAndRewriteRefs(parsed, "")
return json.Marshal(parsed)
}
// walkAndRewriteRefs walks arbitrary JSON (map/array) and
// - when encountering x-kubernetes-group-version-kind, extracts kind,
// updating the currentKind context;
// - rewrites all $ref inside the current context from Application* → kind*.
func walkAndRewriteRefs(node any, currentKind string) {
switch n := node.(type) {
case map[string]any:
if gvk, ok := n["x-kubernetes-group-version-kind"]; ok {
switch g := gvk.(type) {
case map[string]any:
if k, ok := g["kind"].(string); ok {
currentKind = k
}
case []any:
if len(g) > 0 {
if mm, ok := g[0].(map[string]any); ok {
if k, ok := mm["kind"].(string); ok {
currentKind = k
}
}
}
}
}
for k, v := range n {
if k == "$ref" && currentKind != "" {
if s, ok := v.(string); ok {
n[k] = rewriteRefForKind(s, currentKind)
continue
}
}
walkAndRewriteRefs(v, currentKind)
}
case []any:
for _, v := range n {
walkAndRewriteRefs(v, currentKind)
}
}
}
// rewriteRefForKind rewrites a reference to a specific kind.
func rewriteRefForKind(old, kind string) string {
var base string
switch {
case strings.HasPrefix(old, "#/components/schemas/"):
base = "#/components/schemas/"
case strings.HasPrefix(old, "#/definitions/"):
base = "#/definitions/"
default:
return old
}
switch {
case strings.HasSuffix(old, ".Application"):
return base + apiPrefix + "." + kind
case strings.HasSuffix(old, ".ApplicationList"):
return base + apiPrefix + "." + kind + "List"
case strings.HasSuffix(old, ".ApplicationStatus"):
return base + apiPrefix + "." + kind + "Status"
default:
return old
}
}
// -----------------------------------------------------------------------------
// OpenAPI **v3** post-processor
// -----------------------------------------------------------------------------
// BuildPostProcessV3 returns an OpenAPI v3 post-processor that clones base
// Application schemas into per-kind schemas and rewrites $ref pointers.
func BuildPostProcessV3(kindSchemas map[string]string) func(*spec3.OpenAPI) (*spec3.OpenAPI, error) {
return func(doc *spec3.OpenAPI) (*spec3.OpenAPI, error) {
if doc.Components == nil {
doc.Components = &spec3.Components{}
}
if doc.Components.Schemas == nil {
doc.Components.Schemas = map[string]*spec.Schema{}
}
// Get base schemas
base, ok1 := doc.Components.Schemas[baseRef]
list, ok2 := doc.Components.Schemas[baseListRef]
stat, ok3 := doc.Components.Schemas[baseStatusRef]
if !(ok1 && ok2 && ok3) {
return doc, nil // not the apps GV — nothing to patch
}
// Clone base schemas for each kind
for kind, raw := range kindSchemas {
ref := apiPrefix + "." + kind
statusRef := ref + "Status"
listRef := ref + "List"
obj, status, l := cloneKindSchemas(kind, base, stat, list /*v3=*/, true)
doc.Components.Schemas[ref] = obj
doc.Components.Schemas[statusRef] = status
doc.Components.Schemas[listRef] = l
// patch .spec
container := findSpecContainer(obj)
if container == nil {
container = obj
}
if err := patchSpec(container, raw); err != nil {
return nil, fmt.Errorf("kind %s: %w", kind, err)
}
}
// Delete base schemas
delete(doc.Components.Schemas, baseRef)
delete(doc.Components.Schemas, baseListRef)
delete(doc.Components.Schemas, baseStatusRef)
// Disable strategic-merge-patch+json
for p, pi := range doc.Paths.Paths {
if pi != nil && pi.Patch != nil && pi.Patch.RequestBody != nil {
delete(pi.Patch.RequestBody.Content, smp)
doc.Paths.Paths[p] = pi
}
}
// Rewrite all $ref in the document
out, err := rewriteDocRefs(doc)
if err != nil {
return nil, err
}
return doc, json.Unmarshal(out, doc)
}
}
// hasIntAndStringAnyOf returns true if anyOf is exactly a combination of string and integer.
func hasIntAndStringAnyOf(anyOf []spec.Schema) bool {
seen := map[string]bool{}
for i := range anyOf {
for _, t := range anyOf[i].Type {
seen[t] = true
}
}
return seen["string"] && seen["integer"] && len(seen) <= 2
}
// sanitizeForV2 removes unsupported constructs for Swagger v2 and normalizes common patterns.
func sanitizeForV2(s *spec.Schema) {
if s == nil {
return
}
if len(s.AnyOf) > 0 {
if hasIntAndStringAnyOf(s.AnyOf) {
s.Type = spec.StringOrArray{"string"}
if s.Extensions == nil {
s.Extensions = spec.Extensions{}
}
s.Extensions["x-kubernetes-int-or-string"] = true
}
s.AnyOf = nil
}
if len(s.OneOf) > 0 {
s.OneOf = nil
}
if s.AdditionalProperties != nil {
ap := s.AdditionalProperties
if ap.Schema != nil {
sanitizeForV2(ap.Schema)
}
}
for k := range s.Properties {
prop := s.Properties[k]
sanitizeForV2(&prop)
s.Properties[k] = prop
}
if s.Items != nil {
if s.Items.Schema != nil {
sanitizeForV2(s.Items.Schema)
}
for i := range s.Items.Schemas {
sanitizeForV2(&s.Items.Schemas[i])
}
}
for i := range s.AllOf {
sanitizeForV2(&s.AllOf[i])
}
}
// KindSchemasFromConfig extracts the kind→OpenAPISchema mapping from a ResourceConfig.
func KindSchemasFromConfig(rc *config.ResourceConfig) map[string]string {
m := make(map[string]string, len(rc.Resources))
for _, r := range rc.Resources {
m[r.Application.Kind] = r.Application.OpenAPISchema
}
return m
}
// ConfigureOpenAPI sets up OpenAPI v2 and v3 on a GenericAPIServer Config,
// including the post-processors that clone Application schemas to per-kind schemas.
func ConfigureOpenAPI(cfg *genericapiserver.Config, kindSchemas map[string]string, title, version string) {
cfg.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(
sampleopenapi.GetOpenAPIDefinitions, openapi.NewDefinitionNamer(apiserver.Scheme),
)
cfg.OpenAPIConfig.Info.Title = title
cfg.OpenAPIConfig.Info.Version = version
cfg.OpenAPIConfig.PostProcessSpec = BuildPostProcessV2(kindSchemas)
cfg.OpenAPIV3Config = genericapiserver.DefaultOpenAPIV3Config(
sampleopenapi.GetOpenAPIDefinitions, openapi.NewDefinitionNamer(apiserver.Scheme),
)
cfg.OpenAPIV3Config.Info.Title = title
cfg.OpenAPIV3Config.Info.Version = version
cfg.OpenAPIV3Config.PostProcessSpec = BuildPostProcessV3(kindSchemas)
}
// -----------------------------------------------------------------------------
// OpenAPI **v2** (swagger) post-processor
// -----------------------------------------------------------------------------
// BuildPostProcessV2 returns a Swagger post-processor that clones base
// Application schemas into per-kind schemas and rewrites $ref pointers.
func BuildPostProcessV2(kindSchemas map[string]string) func(*spec.Swagger) (*spec.Swagger, error) {
return func(sw *spec.Swagger) (*spec.Swagger, error) {
defs := sw.Definitions
base, ok1 := defs[baseRef]
list, ok2 := defs[baseListRef]
stat, ok3 := defs[baseStatusRef]
if !(ok1 && ok2 && ok3) {
return sw, nil // not the apps GV — nothing to patch
}
for kind, raw := range kindSchemas {
ref := apiPrefix + "." + kind
statusRef := ref + "Status"
listRef := ref + "List"
obj, status, l := cloneKindSchemas(kind, &base, &stat, &list, false)
if err := patchSpec(obj, raw); err != nil {
return nil, fmt.Errorf("kind %s: %w", kind, err)
}
defs[ref] = *obj
defs[statusRef] = *status
defs[listRef] = *l
}
delete(defs, baseRef)
delete(defs, baseListRef)
delete(defs, baseStatusRef)
for p, op := range sw.Paths.Paths {
if op.Patch != nil && len(op.Patch.Consumes) > 0 {
var out []string
for _, c := range op.Patch.Consumes {
if c != smp {
out = append(out, c)
}
}
op.Patch.Consumes = out
sw.Paths.Paths[p] = op
}
}
out, err := rewriteDocRefs(sw)
if err != nil {
return nil, err
}
if err := json.Unmarshal(out, sw); err != nil {
return nil, err
}
for name := range sw.Definitions {
s := sw.Definitions[name]
sanitizeForV2(&s)
sw.Definitions[name] = s
}
return sw, nil
}
}