|
| 1 | +/* |
| 2 | +Copyright 2026 The Cozystack Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package validation |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "regexp" |
| 24 | + "strings" |
| 25 | + |
| 26 | + appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1" |
| 27 | +) |
| 28 | + |
| 29 | +// NameSchema is an application's own declaration of what its name may look |
| 30 | +// like. A chart's naming budget follows from the sub-resources the chart |
| 31 | +// renders and the names they take — facts that live in the chart and nowhere |
| 32 | +// else — so the budget travels with the chart's schema instead of being a |
| 33 | +// branch on Kind in the API server. |
| 34 | +type NameSchema struct { |
| 35 | + Type string `json:"type,omitempty"` |
| 36 | + Description string `json:"description,omitempty"` |
| 37 | + MinLength *int64 `json:"minLength,omitempty"` |
| 38 | + MaxLength *int64 `json:"maxLength,omitempty"` |
| 39 | + Pattern string `json:"pattern,omitempty"` |
| 40 | + |
| 41 | + compiledPattern *regexp.Regexp |
| 42 | +} |
| 43 | + |
| 44 | +// MatchesPattern reports whether name satisfies the declared pattern, if any. |
| 45 | +// The pattern is unanchored, as JSON Schema specifies: a declaration that means |
| 46 | +// the whole name writes ^...$. |
| 47 | +func (s *NameSchema) MatchesPattern(name string) bool { |
| 48 | + return s.compiledPattern == nil || s.compiledPattern.MatchString(name) |
| 49 | +} |
| 50 | + |
| 51 | +// nameSchemaKeywords are the keywords a declaration may carry that the API |
| 52 | +// server interprets. |
| 53 | +var nameSchemaKeywords = map[string]bool{ |
| 54 | + "type": true, "description": true, "minLength": true, "maxLength": true, "pattern": true, |
| 55 | +} |
| 56 | + |
| 57 | +// jsonSchemaAnnotations constrain nothing, so a declaration carrying one is |
| 58 | +// enforced as if it did not. Rejecting them would turn a generator adding a |
| 59 | +// title or an example into an outage for every kind it regenerates. |
| 60 | +var jsonSchemaAnnotations = map[string]bool{ |
| 61 | + "title": true, "default": true, "examples": true, "deprecated": true, |
| 62 | + "readOnly": true, "writeOnly": true, "$comment": true, |
| 63 | +} |
| 64 | + |
| 65 | +// ParseNameSchema extracts the x-cozystack-name declaration from an |
| 66 | +// ApplicationDefinition's openAPISchema. Returns (nil, nil) when the schema |
| 67 | +// declares nothing about the name, which includes a schema that is not a JSON |
| 68 | +// object at all: there is no declaration in it to enforce, and the spec |
| 69 | +// schema's own loader is where that failure is reported. |
| 70 | +func ParseNameSchema(raw string) (*NameSchema, error) { |
| 71 | + var doc map[string]json.RawMessage |
| 72 | + if err := json.Unmarshal([]byte(raw), &doc); err != nil { |
| 73 | + return nil, nil |
| 74 | + } |
| 75 | + declared, ok := doc[appsv1alpha1.NameSchemaExtension] |
| 76 | + if !ok { |
| 77 | + return nil, nil |
| 78 | + } |
| 79 | + return ParseNameDeclaration(declared) |
| 80 | +} |
| 81 | + |
| 82 | +// ParseNameDeclaration parses the value of an x-cozystack-name key. The |
| 83 | +// openAPISchema is untrusted input — definitions from other repositories are |
| 84 | +// written by hand — so a constraint keyword the server does not enforce, a |
| 85 | +// wrong type, a null, or lengths no name can satisfy are an error rather than |
| 86 | +// a constraint silently dropped. A pattern is only checked to compile as RE2; |
| 87 | +// whether it matches anything is the chart's to get right. |
| 88 | +func ParseNameDeclaration(declared []byte) (*NameSchema, error) { |
| 89 | + // encoding/json decodes null as "leave unset", which would turn a null |
| 90 | + // declaration into an empty one and a null keyword into a dropped |
| 91 | + // constraint. |
| 92 | + var keywords map[string]json.RawMessage |
| 93 | + if err := json.Unmarshal(declared, &keywords); err != nil { |
| 94 | + return nil, fmt.Errorf("decode: %w", err) |
| 95 | + } |
| 96 | + if keywords == nil { |
| 97 | + return nil, fmt.Errorf("declaration is null") |
| 98 | + } |
| 99 | + for k, v := range keywords { |
| 100 | + switch { |
| 101 | + case jsonSchemaAnnotations[k], strings.HasPrefix(k, "x-"): |
| 102 | + continue |
| 103 | + case !nameSchemaKeywords[k]: |
| 104 | + return nil, fmt.Errorf("unsupported keyword %q", k) |
| 105 | + case string(bytes.TrimSpace(v)) == "null": |
| 106 | + return nil, fmt.Errorf("%s is null", k) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + var ns NameSchema |
| 111 | + if err := json.Unmarshal(declared, &ns); err != nil { |
| 112 | + return nil, fmt.Errorf("decode: %w", err) |
| 113 | + } |
| 114 | + switch { |
| 115 | + case ns.Type != "" && ns.Type != "string": |
| 116 | + return nil, fmt.Errorf("type %q: a name is a string", ns.Type) |
| 117 | + case ns.MaxLength != nil && *ns.MaxLength < 1: |
| 118 | + return nil, fmt.Errorf("maxLength %d admits no name", *ns.MaxLength) |
| 119 | + case ns.MinLength != nil && *ns.MinLength < 0: |
| 120 | + return nil, fmt.Errorf("minLength %d is negative", *ns.MinLength) |
| 121 | + case ns.MinLength != nil && ns.MaxLength != nil && *ns.MinLength > *ns.MaxLength: |
| 122 | + return nil, fmt.Errorf("minLength %d exceeds maxLength %d", *ns.MinLength, *ns.MaxLength) |
| 123 | + } |
| 124 | + if ns.Pattern != "" { |
| 125 | + re, err := regexp.Compile(ns.Pattern) |
| 126 | + if err != nil { |
| 127 | + return nil, fmt.Errorf("pattern %q does not compile as RE2: %w", ns.Pattern, err) |
| 128 | + } |
| 129 | + ns.compiledPattern = re |
| 130 | + } |
| 131 | + return &ns, nil |
| 132 | +} |
0 commit comments