|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "mime" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/google/cel-go/cel" |
| 11 | + "github.com/google/cel-go/checker/decls" |
| 12 | + "github.com/google/cel-go/common/types" |
| 13 | + "github.com/google/cel-go/common/types/ref" |
| 14 | + "github.com/google/cel-go/common/types/traits" |
| 15 | + celext "github.com/google/cel-go/ext" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | +) |
| 18 | + |
| 19 | +func newCELEvaluator(expr string, req *http.Request) (resourcePredicate, error) { |
| 20 | + env, err := makeCELEnv() |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + parsed, issues := env.Parse(expr) |
| 25 | + if issues != nil && issues.Err() != nil { |
| 26 | + return nil, fmt.Errorf("failed to parse expression %v: %w", expr, issues.Err()) |
| 27 | + } |
| 28 | + |
| 29 | + checked, issues := env.Check(parsed) |
| 30 | + if issues != nil && issues.Err() != nil { |
| 31 | + return nil, fmt.Errorf("expression %v check failed: %w", expr, issues.Err()) |
| 32 | + } |
| 33 | + |
| 34 | + prg, err := env.Program(checked, cel.EvalOptions(cel.OptOptimize)) |
| 35 | + if err != nil { |
| 36 | + return nil, fmt.Errorf("expression %v failed to create a Program: %w", expr, err) |
| 37 | + } |
| 38 | + |
| 39 | + body := map[string]any{} |
| 40 | + // Only decodes the body for the expression if the body is JSON. |
| 41 | + // Technically you could generate several resources without any body. |
| 42 | + if isJSONContent(req) { |
| 43 | + if err := json.NewDecoder(req.Body).Decode(&body); err != nil { |
| 44 | + return nil, fmt.Errorf("failed to parse request body as JSON: %s", err) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return func(obj client.Object) (*bool, error) { |
| 49 | + data, err := clientObjectToMap(obj) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + out, _, err := prg.Eval(map[string]any{ |
| 55 | + "resource": data, |
| 56 | + "request": body, |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("expression %v failed to evaluate: %w", expr, err) |
| 60 | + } |
| 61 | + |
| 62 | + v, ok := out.(types.Bool) |
| 63 | + if !ok { |
| 64 | + return nil, fmt.Errorf("expression %q did not return a boolean value", expr) |
| 65 | + } |
| 66 | + |
| 67 | + result := v.Value().(bool) |
| 68 | + |
| 69 | + return &result, nil |
| 70 | + }, nil |
| 71 | +} |
| 72 | + |
| 73 | +func makeCELEnv() (*cel.Env, error) { |
| 74 | + mapStrDyn := decls.NewMapType(decls.String, decls.Dyn) |
| 75 | + return cel.NewEnv( |
| 76 | + celext.Strings(), |
| 77 | + celext.Encoders(), |
| 78 | + notifications(), |
| 79 | + cel.Declarations( |
| 80 | + decls.NewVar("resource", mapStrDyn), |
| 81 | + decls.NewVar("request", mapStrDyn), |
| 82 | + )) |
| 83 | +} |
| 84 | + |
| 85 | +func isJSONContent(r *http.Request) bool { |
| 86 | + contentType := r.Header.Get("Content-type") |
| 87 | + for _, v := range strings.Split(contentType, ",") { |
| 88 | + t, _, err := mime.ParseMediaType(v) |
| 89 | + if err != nil { |
| 90 | + break |
| 91 | + } |
| 92 | + if t == "application/json" { |
| 93 | + return true |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return false |
| 98 | +} |
| 99 | + |
| 100 | +func notifications() cel.EnvOption { |
| 101 | + r, err := types.NewRegistry() |
| 102 | + if err != nil { |
| 103 | + panic(err) // TODO: Do something better? |
| 104 | + } |
| 105 | + |
| 106 | + return cel.Lib(¬ificationsLib{registry: r}) |
| 107 | +} |
| 108 | + |
| 109 | +type notificationsLib struct { |
| 110 | + registry *types.Registry |
| 111 | +} |
| 112 | + |
| 113 | +// LibraryName implements the SingletonLibrary interface method. |
| 114 | +func (*notificationsLib) LibraryName() string { |
| 115 | + return "flux.notifications.lib" |
| 116 | +} |
| 117 | + |
| 118 | +// CompileOptions implements the Library interface method. |
| 119 | +func (l *notificationsLib) CompileOptions() []cel.EnvOption { |
| 120 | + listStrDyn := cel.ListType(cel.DynType) |
| 121 | + opts := []cel.EnvOption{ |
| 122 | + cel.Function("first", |
| 123 | + cel.MemberOverload("first_list", []*cel.Type{listStrDyn}, cel.DynType, |
| 124 | + cel.UnaryBinding(listFirst))), |
| 125 | + cel.Function("last", |
| 126 | + cel.MemberOverload("last_list", []*cel.Type{listStrDyn}, cel.DynType, |
| 127 | + cel.UnaryBinding(listLast))), |
| 128 | + } |
| 129 | + |
| 130 | + return opts |
| 131 | +} |
| 132 | + |
| 133 | +// ProgramOptions implements the Library interface method. |
| 134 | +func (*notificationsLib) ProgramOptions() []cel.ProgramOption { |
| 135 | + return []cel.ProgramOption{} |
| 136 | +} |
| 137 | + |
| 138 | +func listLast(val ref.Val) ref.Val { |
| 139 | + l := val.(traits.Lister) |
| 140 | + sz := l.Size().Value().(int64) |
| 141 | + |
| 142 | + if sz == 0 { |
| 143 | + return types.NullValue |
| 144 | + } |
| 145 | + |
| 146 | + return l.Get(types.Int(sz - 1)) |
| 147 | +} |
| 148 | + |
| 149 | +func listFirst(val ref.Val) ref.Val { |
| 150 | + l := val.(traits.Lister) |
| 151 | + sz := l.Size().Value().(int64) |
| 152 | + |
| 153 | + if sz == 0 { |
| 154 | + return types.NullValue |
| 155 | + } |
| 156 | + |
| 157 | + return l.Get(types.Int(0)) |
| 158 | +} |
| 159 | + |
| 160 | +func clientObjectToMap(v client.Object) (map[string]any, error) { |
| 161 | + b, err := json.Marshal(v) |
| 162 | + if err != nil { |
| 163 | + return nil, fmt.Errorf("failed to marshal PartialObjectMetadata from resource for CEL expression: %w", err) |
| 164 | + } |
| 165 | + |
| 166 | + var result map[string]any |
| 167 | + if err := json.Unmarshal(b, &result); err != nil { |
| 168 | + return nil, fmt.Errorf("failed to unmarshal PartialObjectMetadata from resource for CEL expression: %w", err) |
| 169 | + } |
| 170 | + |
| 171 | + return result, nil |
| 172 | +} |
0 commit comments