|
| 1 | +package cel |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/go-openapi/spec" |
| 9 | + "github.com/google/cel-go/cel" |
| 10 | + "google.golang.org/protobuf/types/known/structpb" |
| 11 | +) |
| 12 | + |
| 13 | +const ruleKey = "x-deckhouse-validations" |
| 14 | + |
| 15 | +type rule struct { |
| 16 | + Expression string `json:"expression" yaml:"expression"` |
| 17 | + Message string `json:"message" yaml:"message"` |
| 18 | +} |
| 19 | + |
| 20 | +// Validate validates config values against x-deckhouse-validation rules in schema |
| 21 | +func Validate(schema *spec.Schema, values map[string]interface{}) error { |
| 22 | + env, err := cel.NewEnv(cel.Variable("self", cel.MapType(cel.StringType, cel.DynType))) |
| 23 | + if err != nil { |
| 24 | + return fmt.Errorf("create CEL env: %w", err) |
| 25 | + } |
| 26 | + |
| 27 | + raw, found := schema.Extensions[ruleKey] |
| 28 | + if !found { |
| 29 | + return nil |
| 30 | + } |
| 31 | + |
| 32 | + var rules []rule |
| 33 | + switch v := raw.(type) { |
| 34 | + case []interface{}: |
| 35 | + for _, entry := range v { |
| 36 | + mapEntry, ok := entry.(map[string]interface{}) |
| 37 | + if !ok || len(mapEntry) == 0 { |
| 38 | + return fmt.Errorf("x-deckhouse-validations invalid") |
| 39 | + } |
| 40 | + |
| 41 | + if val, ok := mapEntry["expression"]; !ok || len(val.(string)) == 0 { |
| 42 | + return fmt.Errorf("x-deckhouse-validations invalid: missing expression") |
| 43 | + } |
| 44 | + if val, ok := mapEntry["message"]; !ok || len(val.(string)) == 0 { |
| 45 | + return fmt.Errorf("x-deckhouse-validations invalid: missing message") |
| 46 | + } |
| 47 | + |
| 48 | + rules = append(rules, rule{ |
| 49 | + Expression: mapEntry["expression"].(string), |
| 50 | + Message: mapEntry["message"].(string), |
| 51 | + }) |
| 52 | + } |
| 53 | + default: |
| 54 | + return fmt.Errorf("x-deckhouse-validations invalid") |
| 55 | + } |
| 56 | + |
| 57 | + obj, err := structpb.NewStruct(values) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("convert values to struct: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + for _, r := range rules { |
| 63 | + ast, issues := env.Compile(r.Expression) |
| 64 | + if issues.Err() != nil { |
| 65 | + return fmt.Errorf("compile the '%s' rule: %w", r.Expression, issues.Err()) |
| 66 | + } |
| 67 | + |
| 68 | + prg, err := env.Program(ast) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("create program for the '%s' rule: %w", r.Expression, err) |
| 71 | + } |
| 72 | + |
| 73 | + out, _, err := prg.Eval(map[string]interface{}{"self": obj}) |
| 74 | + if err != nil { |
| 75 | + if strings.Contains(err.Error(), "no such key:") { |
| 76 | + continue |
| 77 | + } |
| 78 | + return fmt.Errorf("evaluate the '%s' rule: %w", r.Expression, err) |
| 79 | + } |
| 80 | + |
| 81 | + pass, ok := out.Value().(bool) |
| 82 | + if !ok { |
| 83 | + return errors.New("rule should return boolean") |
| 84 | + } |
| 85 | + if !pass { |
| 86 | + return errors.New(r.Message) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
0 commit comments