mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-06 23:23:23 +09:00
hack/values-injection renders every chart under packages/apps and packages/extra with generated values for each string field and map key its schema allows, including the charts its HelmReleases install, and fails when a value changes the structure of the output rather than only its content. Fields multiline by design are listed in hack/testdata/values-injection/allowlist.txt. It runs as values-injection-check, a prerequisite of unit-tests, and the Helm Charts conventions in docs/agents/overview.md gain the rule it enforces. Assisted-by: LLM Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
469 lines
11 KiB
Go
469 lines
11 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// step is one hop of a schema path: a named property ('p'), the entry of a
|
|
// free-form map ('m'), or an array element ('i').
|
|
type step struct {
|
|
kind byte
|
|
name string
|
|
}
|
|
|
|
type schemaPath []step
|
|
|
|
func (p schemaPath) with(s step) schemaPath {
|
|
out := make(schemaPath, len(p), len(p)+1)
|
|
copy(out, p)
|
|
return append(out, s)
|
|
}
|
|
|
|
func (p schemaPath) key() string {
|
|
var b strings.Builder
|
|
for _, s := range p {
|
|
b.WriteByte(s.kind)
|
|
if s.kind == 'p' {
|
|
b.WriteString(strconv.Itoa(len(s.name)))
|
|
b.WriteByte(':')
|
|
b.WriteString(s.name)
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// slot is a string a tenant can set: a value, or the keys of a free-form map.
|
|
type slot struct {
|
|
path schemaPath
|
|
kind string // "value" or "key"
|
|
node map[string]any
|
|
}
|
|
|
|
func truthy(v any) bool {
|
|
switch x := v.(type) {
|
|
case nil:
|
|
return false
|
|
case bool:
|
|
return x
|
|
case string:
|
|
return x != ""
|
|
case int:
|
|
return x != 0
|
|
case float64:
|
|
return x != 0
|
|
case json.Number:
|
|
f, err := x.Float64()
|
|
return err != nil || f != 0
|
|
case []any:
|
|
return len(x) > 0
|
|
case map[string]any:
|
|
return len(x) > 0
|
|
}
|
|
return true
|
|
}
|
|
|
|
func asMap(v any) map[string]any {
|
|
if m, ok := v.(map[string]any); ok {
|
|
return m
|
|
}
|
|
return map[string]any{}
|
|
}
|
|
|
|
func typesOf(node map[string]any) map[string]bool {
|
|
t := node["type"]
|
|
if list, ok := t.([]any); ok {
|
|
out := map[string]bool{}
|
|
for _, e := range list {
|
|
out[pyStr(e)] = true
|
|
}
|
|
return out
|
|
}
|
|
if truthy(t) {
|
|
return map[string]bool{pyStr(t): true}
|
|
}
|
|
if truthy(node["x-kubernetes-int-or-string"]) {
|
|
return map[string]bool{"int-or-string": true}
|
|
}
|
|
_, hasProps := node["properties"]
|
|
_, hasAP := node["additionalProperties"]
|
|
if hasProps || hasAP {
|
|
return map[string]bool{"object": true}
|
|
}
|
|
return map[string]bool{}
|
|
}
|
|
|
|
// freeForm is the value schema of an object whose keys the schema does not
|
|
// name; ok is false for any other node.
|
|
func freeForm(node map[string]any) (map[string]any, bool) {
|
|
if !typesOf(node)["object"] {
|
|
return nil, false
|
|
}
|
|
ap, has := node["additionalProperties"]
|
|
if m, ok := ap.(map[string]any); ok {
|
|
return m, true
|
|
}
|
|
if ap == true || ((!has || ap == nil) && !truthy(node["properties"])) {
|
|
return map[string]any{}, true
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func alternatives(node map[string]any) []map[string]any {
|
|
var out []map[string]any
|
|
for _, k := range []string{"oneOf", "anyOf", "allOf"} {
|
|
list, _ := node[k].([]any)
|
|
for _, alt := range list {
|
|
if m, ok := alt.(map[string]any); ok {
|
|
out = append(out, m)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// slots calls yield for every string a tenant can set below node.
|
|
func slots(node any, path schemaPath, depth int, yield func(slot)) {
|
|
m, ok := node.(map[string]any)
|
|
if !ok || depth > 12 {
|
|
return
|
|
}
|
|
t := typesOf(m)
|
|
if t["string"] || t["int-or-string"] || (len(t) == 0 && !anyNonEmpty(alternatives(m))) {
|
|
yield(slot{path, "value", m})
|
|
}
|
|
props := asMap(m["properties"])
|
|
for _, name := range sortedKeys(props) {
|
|
slots(props[name], path.with(step{'p', name}), depth+1, yield)
|
|
}
|
|
ap, isFree := freeForm(m)
|
|
if def, ok := m["default"].(map[string]any); isFree && ok {
|
|
// A free-form block whose default names its keys (a provider's
|
|
// credentials): those keys are what the template reads.
|
|
for _, name := range sortedKeys(def) {
|
|
switch def[name].(type) {
|
|
case map[string]any, []any:
|
|
continue
|
|
}
|
|
if _, named := props[name]; !named {
|
|
slots(map[string]any{"type": "string"}, path.with(step{'p', name}), depth+1, yield)
|
|
}
|
|
}
|
|
}
|
|
if isFree {
|
|
yield(slot{path, "key", ap})
|
|
if len(ap) > 0 {
|
|
slots(ap, path.with(step{'m', ""}), depth+1, yield)
|
|
} else {
|
|
yield(slot{path.with(step{'m', ""}), "value", map[string]any{}})
|
|
}
|
|
}
|
|
if items, ok := m["items"].(map[string]any); ok && t["array"] {
|
|
slots(items, path.with(step{'i', ""}), depth+1, yield)
|
|
}
|
|
for _, alt := range alternatives(m) {
|
|
slots(alt, path, depth+1, yield)
|
|
}
|
|
}
|
|
|
|
func anyNonEmpty(alts []map[string]any) bool {
|
|
for _, a := range alts {
|
|
if len(a) > 0 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func dotted(path schemaPath, kind string) string {
|
|
out := ""
|
|
for _, s := range path {
|
|
switch s.kind {
|
|
case 'p':
|
|
if out != "" {
|
|
out += "."
|
|
}
|
|
out += s.name
|
|
case 'm':
|
|
out += ".*"
|
|
default:
|
|
out += "[0]"
|
|
}
|
|
}
|
|
if kind == "key" {
|
|
if out != "" {
|
|
out += ".<key>"
|
|
} else {
|
|
out = "<key>"
|
|
}
|
|
}
|
|
if out == "" {
|
|
return "<root>"
|
|
}
|
|
return out
|
|
}
|
|
|
|
var exactGuesses = map[string]string{
|
|
"address": "10.0.0.1", "gateway": "10.0.0.1", "ip": "10.0.0.1", "netmask": "255.255.255.0",
|
|
"mac": "00:00:5e:00:53:01", "annotationprefix": "dns1.example.org/",
|
|
}
|
|
|
|
var partialGuesses = []struct {
|
|
parts []string
|
|
value string
|
|
}{
|
|
{[]string{"dnsserver", "nameserver", "timeserver"}, "10.0.0.1"},
|
|
{[]string{"whitelist", "allowlist", "cidr", "subnet", "sourcerange"}, "10.0.0.0/8"},
|
|
{[]string{"serveraddress"}, "http://vmselect.tenant-test.svc:8481"},
|
|
{[]string{"version"}, "v1.10.3"},
|
|
{[]string{"schedule"}, "0 2 * * *"},
|
|
{[]string{"memory", "size", "storage", "quantity"}, "1Gi"},
|
|
{[]string{"cpu"}, "1"},
|
|
{[]string{"url", "endpoint", "server"}, "https://s3.example.org"},
|
|
{[]string{"path", "destination"}, "s3://bucket/path/"},
|
|
{[]string{"time"}, "2026-01-01T00:00:00Z"},
|
|
{[]string{"host", "domain"}, "app.example.org"},
|
|
}
|
|
|
|
// guess is a plausible value for a field, so the chart's own checks accept it.
|
|
func guess(name string, node map[string]any) any {
|
|
if enum, ok := node["enum"].([]any); ok && len(enum) > 0 {
|
|
return enum[0]
|
|
}
|
|
if def, ok := node["default"].(string); ok && def != "" {
|
|
return def
|
|
}
|
|
n := strings.ToLower(name)
|
|
if v, ok := exactGuesses[n]; ok {
|
|
return v
|
|
}
|
|
for _, g := range partialGuesses {
|
|
for _, p := range g.parts {
|
|
if strings.Contains(n, p) {
|
|
return g.value
|
|
}
|
|
}
|
|
}
|
|
if typesOf(node)["int-or-string"] {
|
|
return "1"
|
|
}
|
|
return "fill"
|
|
}
|
|
|
|
// sample is a minimal instance of a schema, for an object created from scratch.
|
|
func sample(node any, depth int, name string) any {
|
|
m, ok := node.(map[string]any)
|
|
if !ok || depth > 8 {
|
|
return "fill"
|
|
}
|
|
if def, has := m["default"]; has {
|
|
return deepCopy(def)
|
|
}
|
|
if enum, ok := m["enum"].([]any); ok && len(enum) > 0 {
|
|
return deepCopy(enum[0])
|
|
}
|
|
t := typesOf(m)
|
|
switch {
|
|
case t["object"]:
|
|
props := asMap(m["properties"])
|
|
out := map[string]any{}
|
|
required, _ := m["required"].([]any)
|
|
for _, r := range required {
|
|
rs := pyStr(r)
|
|
sub, ok := props[rs]
|
|
if !ok {
|
|
sub = map[string]any{}
|
|
}
|
|
out[rs] = sample(sub, depth+1, rs)
|
|
}
|
|
return out
|
|
case t["array"]:
|
|
return []any{}
|
|
case t["boolean"]:
|
|
return false
|
|
case t["integer"] || t["number"]:
|
|
return 1
|
|
}
|
|
return guess(name, m)
|
|
}
|
|
|
|
// build is values that put leaf at path, or key as a key of the map at path.
|
|
// Variant 1 also sets `enabled: true` on every object along the path, and
|
|
// variant 2 also fills the empty strings beside it below the top level.
|
|
func build(node any, path schemaPath, leaf any, key *string, variant int, fresh bool, depth int, keyValue *string) any {
|
|
m := asMap(node)
|
|
if len(path) == 0 {
|
|
if key != nil {
|
|
if keyValue != nil {
|
|
return map[string]any{*key: *keyValue}
|
|
}
|
|
if ap, _ := freeForm(m); len(ap) > 0 {
|
|
return map[string]any{*key: sample(ap, 0, "")}
|
|
}
|
|
return map[string]any{*key: "fill"}
|
|
}
|
|
return leaf
|
|
}
|
|
s, rest := path[0], path[1:]
|
|
switch s.kind {
|
|
case 'p':
|
|
var obj map[string]any
|
|
if fresh {
|
|
obj, _ = sample(m, 0, "").(map[string]any)
|
|
}
|
|
if obj == nil {
|
|
obj = map[string]any{}
|
|
}
|
|
props := asMap(m["properties"])
|
|
if variant >= 1 && typesOf(asMap(props["enabled"]))["boolean"] {
|
|
obj["enabled"] = true
|
|
}
|
|
if variant >= 2 && depth > 0 {
|
|
for _, name := range sortedKeys(props) {
|
|
sub, ok := props[name].(map[string]any)
|
|
if name == s.name || !ok {
|
|
continue
|
|
}
|
|
if typesOf(sub)["string"] && !truthy(sub["enum"]) && !truthy(sub["default"]) {
|
|
obj[name] = guess(name, sub)
|
|
}
|
|
}
|
|
}
|
|
sub, ok := props[s.name]
|
|
if !ok {
|
|
sub = map[string]any{}
|
|
}
|
|
obj[s.name] = build(sub, rest, leaf, key, variant, fresh, depth+1, keyValue)
|
|
return obj
|
|
case 'm':
|
|
ap, _ := freeForm(m)
|
|
return map[string]any{"fuzzkey": build(ap, rest, leaf, key, variant, true, depth+1, keyValue)}
|
|
}
|
|
items := m["items"]
|
|
if !truthy(items) {
|
|
items = map[string]any{}
|
|
}
|
|
return []any{build(items, rest, leaf, key, variant, true, depth+1, keyValue)}
|
|
}
|
|
|
|
// put sets the field at path in values that build made, after a merge over
|
|
// it.
|
|
func put(values any, path schemaPath, leaf any) error {
|
|
node := values
|
|
for i, s := range path {
|
|
last := i == len(path)-1
|
|
switch s.kind {
|
|
case 'i':
|
|
list, ok := node.([]any)
|
|
if !ok || len(list) == 0 {
|
|
return fmt.Errorf("%s: no list to put into", dotted(path[:i+1], "value"))
|
|
}
|
|
if last {
|
|
list[0] = leaf
|
|
} else {
|
|
node = list[0]
|
|
}
|
|
default:
|
|
name := s.name
|
|
if s.kind == 'm' {
|
|
name = "fuzzkey"
|
|
}
|
|
obj, ok := node.(map[string]any)
|
|
if !ok {
|
|
return fmt.Errorf("%s: no map to put into", dotted(path[:i+1], "value"))
|
|
}
|
|
if last {
|
|
obj[name] = leaf
|
|
} else {
|
|
node = obj[name]
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func substitute(v any, c string) any {
|
|
switch x := v.(type) {
|
|
case map[string]any:
|
|
out := make(map[string]any, len(x))
|
|
for k, e := range x {
|
|
out[strings.ReplaceAll(k, "$CANARY", c)] = substitute(e, c)
|
|
}
|
|
return out
|
|
case []any:
|
|
out := make([]any, len(x))
|
|
for i, e := range x {
|
|
out[i] = substitute(e, c)
|
|
}
|
|
return out
|
|
case string:
|
|
return strings.ReplaceAll(x, "$CANARY", c)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func deepCopy(v any) any {
|
|
switch x := v.(type) {
|
|
case map[string]any:
|
|
out := make(map[string]any, len(x))
|
|
for k, e := range x {
|
|
out[k] = deepCopy(e)
|
|
}
|
|
return out
|
|
case []any:
|
|
out := make([]any, len(x))
|
|
for i, e := range x {
|
|
out[i] = deepCopy(e)
|
|
}
|
|
return out
|
|
}
|
|
return v
|
|
}
|
|
|
|
func container(v any) string {
|
|
switch v.(type) {
|
|
case map[string]any:
|
|
return "map"
|
|
case []any:
|
|
return "list"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// merge is b over a. Lists merge element by element, so a profile can
|
|
// complete an array element the fuzzer creates.
|
|
func merge(a, b any) any {
|
|
if al, ok := a.([]any); ok {
|
|
if bl, ok := b.([]any); ok {
|
|
n := min(len(al), len(bl))
|
|
out := make([]any, 0, max(len(al), len(bl)))
|
|
for i := 0; i < n; i++ {
|
|
if k := container(al[i]); k != "" && k == container(bl[i]) {
|
|
out = append(out, merge(al[i], bl[i]))
|
|
} else {
|
|
out = append(out, deepCopy(bl[i]))
|
|
}
|
|
}
|
|
tail := al[n:]
|
|
if len(bl) > len(al) {
|
|
tail = bl[n:]
|
|
}
|
|
for _, e := range tail {
|
|
out = append(out, deepCopy(e))
|
|
}
|
|
return out
|
|
}
|
|
}
|
|
out := asMap(deepCopy(a))
|
|
for k, v := range asMap(b) {
|
|
if kind := container(v); kind != "" && kind == container(out[k]) {
|
|
out[k] = merge(out[k], v)
|
|
} else {
|
|
out[k] = deepCopy(v)
|
|
}
|
|
}
|
|
return out
|
|
}
|