-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathexpression_template.go
More file actions
390 lines (351 loc) · 11.4 KB
/
Copy pathexpression_template.go
File metadata and controls
390 lines (351 loc) · 11.4 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package template
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"regexp"
"slices"
"strconv"
"strings"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/ast"
"github.com/expr-lang/expr/parser"
"github.com/argoproj/argo-workflows/v4/util/logging"
"github.com/argoproj/argo-workflows/v4/util/maps"
varkeys "github.com/argoproj/argo-workflows/v4/util/variables/keys"
)
func init() {
if os.Getenv("EXPRESSION_TEMPLATES") != "false" {
registerKind(kindExpression)
}
}
var (
variablesToCheck = []string{
varkeys.Item.Template(),
varkeys.Retries.Template(),
varkeys.RetriesLastExitCode.Template(),
varkeys.RetriesLastStatus.Template(),
varkeys.RetriesLastDuration.Template(),
varkeys.RetriesLastMessage.Template(),
varkeys.RetriesExitCodes.Template(),
varkeys.WorkflowStatus.Template(),
varkeys.WorkflowFailures.Template(),
}
)
// missingVarsInEnv returns the identifiers referenced by the expression that are absent from env
// (a present-but-nil leaf counts as present). Errors if the expression cannot be parsed.
func missingVarsInEnv(expression string, env map[string]any) ([]string, error) {
identifiers, err := getIdentifiers(expression)
if err != nil {
return nil, err
}
var missing []string
for _, id := range identifiers {
if !hasVarInEnv(env, id) {
missing = append(missing, id)
}
}
return missing, nil
}
// anyVarNotInEnv returns the first late-binding variable (variablesToCheck) that the expression
// references but env lacks, or nil if there is none.
func anyVarNotInEnv(expression string, env map[string]any) *string {
missing, err := missingVarsInEnv(expression, env)
if err != nil {
// Unparseable expressions can't be checked; compile/run will surface the error.
return nil
}
for _, id := range missing {
if slices.Contains(variablesToCheck, id) {
return &id
}
}
return nil
}
func expressionReplaceStrict(ctx context.Context, w io.Writer, expression string, env map[string]any, strictRegex *regexp.Regexp) (int, error) {
// The template is JSON-marshaled. This JSON-unmarshals the expression to undo any character escapes.
var unmarshalledExpression string
err := json.Unmarshal(fmt.Appendf(nil, `"%s"`, expression), &unmarshalledExpression)
if err != nil {
// If we can't unmarshal, we can't parse. Fallback to expressionReplaceCore to handle it (likely error).
return expressionReplaceCore(ctx, w, expression, env, false)
}
missingIdentifiers, err := missingVarsInEnv(unmarshalledExpression, env)
if err != nil {
// If we can't parse, we can't check variables. Fallback to expressionReplaceCore(false) to report syntax error.
return expressionReplaceCore(ctx, w, expression, env, false)
}
for _, id := range missingIdentifiers {
if strictRegex != nil && strictRegex.MatchString(id) {
return 0, fmt.Errorf("failed to evaluate expression: %s is missing", id)
}
}
// If we have missing identifiers but they are NOT strict, we allow unresolved.
// If we have NO missing identifiers, we enforce resolution (to catch runtime errors),
// unless the caller allows unresolved (strictRegex == nil), in which case runtime
// failures are tolerated and the expression is left unresolved for later evaluation.
allowUnresolved := len(missingIdentifiers) > 0 || strictRegex == nil
return expressionReplaceCore(ctx, w, expression, env, allowUnresolved)
}
type identifierVisitor struct {
identifiers []string
seen map[string]bool
guarded map[ast.Node]bool
}
func (v *identifierVisitor) Visit(node *ast.Node) {
if v.guarded[*node] {
return
}
if n, ok := (*node).(*ast.IdentifierNode); ok {
if !v.seen[n.Value] {
v.identifiers = append(v.identifiers, n.Value)
v.seen[n.Value] = true
}
}
if n, ok := (*node).(*ast.MemberNode); ok {
path, ok := getMemberPath(n)
if ok {
if !v.seen[path] {
v.identifiers = append(v.identifiers, path)
v.seen[path] = true
}
}
}
}
func getMemberPath(node *ast.MemberNode) (string, bool) {
var parts []string
curr := node
for {
if curr.Optional {
return "", false
}
prop, ok := curr.Property.(*ast.StringNode)
if !ok {
return "", false
}
parts = append([]string{prop.Value}, parts...)
if id, isIdent := curr.Node.(*ast.IdentifierNode); isIdent {
parts = append([]string{id.Value}, parts...)
return strings.Join(parts, "."), true
}
next, ok := curr.Node.(*ast.MemberNode)
if !ok {
return "", false
}
curr = next
}
}
// guardVisitor collects the member nodes whose access is guarded by the
// nil-coalescing (??) or optional-chaining (?.) operators, so they are not
// reported as strictly-required identifiers. Base identifiers are left
// untouched so a genuinely-unavailable variable still triggers a requeue.
type guardVisitor struct {
guarded map[ast.Node]bool
}
func (v *guardVisitor) Visit(node *ast.Node) {
switch n := (*node).(type) {
case *ast.BinaryNode:
if n.Operator == "??" {
ast.Walk(&n.Left, &memberMarker{guarded: v.guarded})
}
case *ast.MemberNode:
if n.Optional {
if _, ok := n.Node.(*ast.MemberNode); ok {
v.guarded[n.Node] = true
}
}
}
}
type memberMarker struct {
guarded map[ast.Node]bool
}
func (m *memberMarker) Visit(node *ast.Node) {
if _, ok := (*node).(*ast.MemberNode); ok {
m.guarded[*node] = true
}
}
func getIdentifiers(expression string) ([]string, error) {
tree, err := parser.Parse(expression)
if err != nil {
return nil, err
}
guarded := make(map[ast.Node]bool)
ast.Walk(&tree.Node, &guardVisitor{guarded: guarded})
visitor := &identifierVisitor{
seen: make(map[string]bool),
guarded: guarded,
}
ast.Walk(&tree.Node, visitor)
return visitor.identifiers, nil
}
func expressionReplaceCore(ctx context.Context, w io.Writer, expression string, env map[string]any, allowUnresolved bool) (int, error) {
shouldAllowFailure := false
maps.VisitMap(env, func(key string, value any) bool {
rv := reflect.Indirect(reflect.ValueOf(value))
if rv.Kind() == reflect.String {
if IsPlaceholder(rv.String()) {
shouldAllowFailure = true
return false
}
}
return true
})
log := logging.RequireLoggerFromContext(ctx)
// The template is JSON-marshaled. This JSON-unmarshals the expression to undo any character escapes.
var unmarshalledExpression string
err := json.Unmarshal(fmt.Appendf(nil, `"%s"`, expression), &unmarshalledExpression)
if err != nil && allowUnresolved {
log.WithError(err).Debug(ctx, "unresolved is allowed")
return fmt.Fprintf(w, "{{%s%s}}", kindExpression, expression)
}
if err != nil {
return 0, fmt.Errorf("failed to unmarshall JSON expression: %w", err)
}
varNameNotInEnv := anyVarNotInEnv(unmarshalledExpression, env)
if varNameNotInEnv != nil && allowUnresolved {
// this is to make sure expressions don't get resolved to nil or an empty string when certain variables
// don't exist in the env during the "global" replacement.
// See https://github.com/argoproj/argo-workflows/issues/5388, https://github.com/argoproj/argo-workflows/issues/15008,
// https://github.com/argoproj/argo-workflows/issues/10393, https://github.com/expr-lang/expr/issues/330
log.WithField("variable", *varNameNotInEnv).Debug(ctx, "variable not in env but unresolved is allowed")
return fmt.Fprintf(w, "{{%s%s}}", kindExpression, expression)
}
program, err := expr.Compile(unmarshalledExpression, expr.Env(env))
// This allowUnresolved check is not great
// it allows for errors that are obviously
// not failed reference checks to also pass
if err != nil && !allowUnresolved && !shouldAllowFailure {
return 0, fmt.Errorf("failed to evaluate expression: %w", err)
}
result, err := expr.Run(program, env)
if (err != nil || result == nil) && (allowUnresolved || shouldAllowFailure) {
// <nil> result is also un-resolved, and any error can be unresolved
log.WithError(err).Debug(ctx, "Result and error are unresolved")
return fmt.Fprintf(w, "{{%s%s}}", kindExpression, expression)
}
if err != nil {
return 0, fmt.Errorf("failed to evaluate expression: %w", err)
}
if result == nil {
return 0, fmt.Errorf("failed to evaluate expression %q", expression)
}
resultMarshaled, err := json.Marshal(result)
if (err != nil || resultMarshaled == nil) && allowUnresolved {
log.WithError(err).Debug(ctx, "resultMarshaled is nil and unresolved is allowed ")
return fmt.Fprintf(w, "{{%s%s}}", kindExpression, expression)
}
if err != nil {
return 0, fmt.Errorf("failed to marshal evaluated expression: %w", err)
}
if resultMarshaled == nil {
return 0, fmt.Errorf("failed to marshal evaluated marshaled expression %q", expression)
}
marshaledLength := len(resultMarshaled)
// Trim leading and trailing quotes. The value is being inserted into something that's already a string.
if len(resultMarshaled) > 1 && resultMarshaled[0] == '"' && resultMarshaled[marshaledLength-1] == '"' {
return w.Write(resultMarshaled[1 : marshaledLength-1])
}
resultQuoted := []byte(strconv.Quote(string(resultMarshaled)))
return w.Write(resultQuoted[1 : len(resultQuoted)-1])
}
func EnvMap(replaceMap map[string]string) map[string]any {
envMap := make(map[string]any)
for k, v := range replaceMap {
envMap[k] = v
}
return envMap
}
// hasVarInEnv checks if a parameter is in env or not
func hasVarInEnv(env map[string]any, parameter string) bool {
if _, ok := env[parameter]; ok {
return true
}
parts := strings.Split(parameter, ".")
var current any
found := false
remainingParts := parts
// Try to find the longest matching prefix in env
for i := len(parts); i > 0; i-- {
prefix := strings.Join(parts[:i], ".")
if val, ok := env[prefix]; ok {
current = val
remainingParts = parts[i:]
found = true
break
}
}
if !found {
// If no prefix found, start from env itself (if env is the root object)
// But in our case env is a map[string]any, so if no key matched, we probably can't traverse.
// However, let's keep existing behavior: start traversing from env as if it's the root.
current = env
remainingParts = parts
}
// Traverse the remaining parts
for i, part := range remainingParts {
if current == nil {
return false
}
rVal := reflect.ValueOf(current)
for rVal.Kind() == reflect.Pointer {
if rVal.IsNil() {
return false
}
rVal = rVal.Elem()
}
switch rVal.Kind() {
case reflect.Map:
val := rVal.MapIndex(reflect.ValueOf(part))
if !val.IsValid() {
return false
}
current = val.Interface()
case reflect.Struct:
field := rVal.FieldByName(part)
if !field.IsValid() {
// Search anonymous fields manually to ensure we find embedded fields
for j := 0; j < rVal.NumField(); j++ {
fType := rVal.Type().Field(j)
if fType.Anonymous {
embeddedValue := rVal.Field(j)
// Handle pointer to embedded struct
for embeddedValue.Kind() == reflect.Pointer {
if embeddedValue.IsNil() {
break
}
embeddedValue = embeddedValue.Elem()
}
if embeddedValue.Kind() == reflect.Struct {
// If we are looking for the embedded type itself (e.g. "Time" in metav1.Time)
if fType.Name == part {
field = rVal.Field(j)
break
}
if foundField := embeddedValue.FieldByName(part); foundField.IsValid() {
field = foundField
break
}
}
}
}
}
if !field.IsValid() {
return false
}
if !field.CanInterface() {
return false
}
current = field.Interface()
default:
return false
}
// If this was the last part, we found it
if i == len(remainingParts)-1 {
return true
}
}
return found && len(remainingParts) == 0
}