-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcel.go
More file actions
310 lines (274 loc) · 9.19 KB
/
Copy pathcel.go
File metadata and controls
310 lines (274 loc) · 9.19 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
package cel
import (
"fmt"
"log/slog"
"os"
"reflect"
"strings"
"sync"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
)
// Context holds the runtime data available to CEL expressions.
type Context struct {
Steps map[string]map[string]any // steps.<name>.output → step outputs
Inputs map[string]any // inputs.<name> → workflow inputs
Trigger map[string]any // trigger.payload → webhook trigger data
Artifacts map[string]map[string]any // artifacts.<name> → {name, url, size}
Hooks map[string]map[string]any // hooks.<name>.output → hook step outputs
Execution map[string]any // execution.status, execution.error, etc.
}
const maxProgramCacheSize = 10000
// Evaluator evaluates CEL expressions against a runtime context.
type Evaluator struct {
env *cel.Env
configEnv map[string]string // config-sourced env values from mantle.yaml
valuesEnv map[string]string // values-file env overrides (between config and OS)
envCache map[string]string // cached, filtered environment variables
programMu sync.Mutex
programCache map[string]cel.Program // expression string -> compiled cel.Program
}
// NewEvaluator creates a CEL evaluator with the standard Mantle expression environment.
func NewEvaluator() (*Evaluator, error) {
opts := []cel.EnvOption{
cel.Variable("steps", cel.MapType(cel.StringType, cel.DynType)),
cel.Variable("inputs", cel.MapType(cel.StringType, cel.DynType)),
cel.Variable("env", cel.MapType(cel.StringType, cel.StringType)),
cel.Variable("trigger", cel.MapType(cel.StringType, cel.DynType)),
cel.Variable("artifacts", cel.MapType(cel.StringType, cel.DynType)),
cel.Variable("hooks", cel.MapType(cel.StringType, cel.DynType)),
cel.Variable("execution", cel.MapType(cel.StringType, cel.DynType)),
}
opts = append(opts, customFunctions()...)
env, err := cel.NewEnv(opts...)
if err != nil {
return nil, fmt.Errorf("creating CEL environment: %w", err)
}
return &Evaluator{env: env, envCache: mergeEnvVars(nil, nil), programCache: make(map[string]cel.Program)}, nil
}
// Eval evaluates a CEL expression and returns the result as a Go value.
// Compiled programs are cached by expression string to avoid redundant compilation.
func (e *Evaluator) Eval(expression string, ctx *Context) (any, error) {
prog, err := e.getOrCompile(expression)
if err != nil {
return nil, err
}
trigger := ctx.Trigger
if trigger == nil {
trigger = map[string]any{}
}
artifacts := ctx.Artifacts
if artifacts == nil {
artifacts = map[string]map[string]any{}
}
hooks := ctx.Hooks
if hooks == nil {
hooks = map[string]map[string]any{}
}
execution := ctx.Execution
if execution == nil {
execution = map[string]any{}
}
vars := map[string]any{
"steps": ctx.Steps,
"inputs": ctx.Inputs,
"env": e.envCache,
"trigger": trigger,
"artifacts": artifacts,
"hooks": hooks,
"execution": execution,
}
out, _, err := prog.Eval(vars)
if err != nil {
return nil, fmt.Errorf("evaluating %q: %w", expression, err)
}
return refToNative(out), nil
}
// getOrCompile returns a cached compiled program or compiles and caches a new one.
// The cache is bounded to maxProgramCacheSize entries; when full it is cleared
// (simple eviction — sufficient for the expected expression cardinality).
func (e *Evaluator) getOrCompile(expression string) (cel.Program, error) {
e.programMu.Lock()
if cached, ok := e.programCache[expression]; ok {
e.programMu.Unlock()
return cached, nil
}
e.programMu.Unlock()
ast, issues := e.env.Compile(expression)
if issues != nil && issues.Err() != nil {
return nil, fmt.Errorf("compiling expression %q: %w", expression, issues.Err())
}
prog, err := e.env.Program(ast)
if err != nil {
return nil, fmt.Errorf("creating program for %q: %w", expression, err)
}
e.programMu.Lock()
if len(e.programCache) >= maxProgramCacheSize {
e.programCache = make(map[string]cel.Program)
}
e.programCache[expression] = prog
e.programMu.Unlock()
return prog, nil
}
// CompileCheck compiles a CEL expression and returns any syntax/type errors
// without evaluating it. This is used for offline validation.
func (e *Evaluator) CompileCheck(expression string) error {
_, err := e.getOrCompile(expression)
return err
}
// EvalBool evaluates a CEL expression that should return a boolean.
func (e *Evaluator) EvalBool(expression string, ctx *Context) (bool, error) {
result, err := e.Eval(expression, ctx)
if err != nil {
return false, err
}
b, ok := result.(bool)
if !ok {
return false, fmt.Errorf("expression %q returned %T, expected bool", expression, result)
}
return b, nil
}
// ResolveString resolves CEL expressions embedded in a string value.
// Expressions are delimited by {{ and }}. If the entire string is a single
// expression, the raw result is returned (preserving type). Otherwise,
// expressions are interpolated into the string.
func (e *Evaluator) ResolveString(value string, ctx *Context) (any, error) {
trimmed := strings.TrimSpace(value)
// If the entire value is a single {{ expr }}, evaluate and return the raw result.
if strings.HasPrefix(trimmed, "{{") && strings.HasSuffix(trimmed, "}}") {
inner := strings.Count(trimmed, "{{")
if inner == 1 {
expr := strings.TrimSpace(trimmed[2 : len(trimmed)-2])
return e.Eval(expr, ctx)
}
}
// Otherwise, interpolate all {{ expr }} occurrences into the string.
result := value
for {
start := strings.Index(result, "{{")
if start == -1 {
break
}
end := strings.Index(result[start:], "}}")
if end == -1 {
break
}
end += start + 2
expr := strings.TrimSpace(result[start+2 : end-2])
val, err := e.Eval(expr, ctx)
if err != nil {
return nil, err
}
result = result[:start] + fmt.Sprintf("%v", val) + result[end:]
}
return result, nil
}
// ResolveParams recursively resolves CEL expressions in a params map.
func (e *Evaluator) ResolveParams(params map[string]any, ctx *Context) (map[string]any, error) {
resolved := make(map[string]any, len(params))
for k, v := range params {
r, err := e.resolveValue(v, ctx)
if err != nil {
return nil, fmt.Errorf("resolving param %q: %w", k, err)
}
resolved[k] = r
}
return resolved, nil
}
func (e *Evaluator) resolveValue(v any, ctx *Context) (any, error) {
switch val := v.(type) {
case string:
return e.ResolveString(val, ctx)
case map[string]any:
return e.ResolveParams(val, ctx)
case []any:
result := make([]any, len(val))
for i, item := range val {
r, err := e.resolveValue(item, ctx)
if err != nil {
return nil, err
}
result[i] = r
}
return result, nil
default:
return v, nil
}
}
// SetConfigEnv sets the config-sourced env values and rebuilds the env cache.
// This merges config values with MANTLE_ENV_* environment variables, where
// OS env vars take precedence over config values.
func (e *Evaluator) SetConfigEnv(configEnv map[string]string) {
e.configEnv = configEnv
e.envCache = mergeEnvVars(configEnv, e.valuesEnv)
}
// SetValuesEnv sets the values-file env overrides and rebuilds the env cache.
// Values-file env sits between config env and MANTLE_ENV_* OS variables in
// the precedence stack: config < values < MANTLE_ENV_*.
func (e *Evaluator) SetValuesEnv(valuesEnv map[string]string) {
e.valuesEnv = valuesEnv
e.envCache = mergeEnvVars(e.configEnv, e.valuesEnv)
}
// mergeEnvVars builds the env map available to CEL expressions.
// It starts with config-sourced values (from the env: section in mantle.yaml),
// then overlays values-file overrides, then overlays MANTLE_ENV_* environment
// variables (stripping the prefix). When a key exists in both config/values and
// the OS env, the OS env var wins and an info log is emitted.
// This prevents CEL expressions from reading sensitive variables like
// MANTLE_ENCRYPTION_KEY, MANTLE_DATABASE_URL, or AWS_SECRET_ACCESS_KEY.
func mergeEnvVars(configEnv map[string]string, valuesEnv map[string]string) map[string]string {
env := make(map[string]string)
// Layer 1: config values (lowest precedence).
for k, v := range configEnv {
env[k] = v
}
// Layer 2: values-file overrides.
for k, v := range valuesEnv {
env[k] = v
}
// Layer 3: MANTLE_ENV_* from OS environment (highest precedence).
const prefix = "MANTLE_ENV_"
for _, e := range os.Environ() {
parts := strings.SplitN(e, "=", 2)
if len(parts) == 2 && strings.HasPrefix(parts[0], prefix) {
key := strings.TrimPrefix(parts[0], prefix)
if _, exists := env[key]; exists {
slog.Info("env variable overrides config", "key", prefix+key, "config_key", "env."+key)
}
env[key] = parts[1]
}
}
return env
}
// refToNative converts a CEL ref.Val to a native Go value.
func refToNative(v ref.Val) any {
switch v.Type() {
case types.BoolType:
return v.Value()
case types.IntType:
return v.Value()
case types.DoubleType:
return v.Value()
case types.StringType:
return v.Value()
case types.MapType:
nv, err := v.ConvertToNative(mapType)
if err != nil {
return v.Value()
}
return nv
case types.ListType:
nv, err := v.ConvertToNative(sliceType)
if err != nil {
return v.Value()
}
return nv
default:
return v.Value()
}
}
var (
mapType = reflect.TypeOf(map[string]any{})
sliceType = reflect.TypeOf([]any{})
)