Skip to content

Commit

Permalink
fix(deeplinks): do not evaluate template when condition is false (arg…
Browse files Browse the repository at this point in the history
…oproj#19625) (argoproj#19868)

Signed-off-by: Alexandre Gaudreault <[email protected]>
  • Loading branch information
agaudreault authored Sep 10, 2024
1 parent 5776554 commit ca7a08e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
48 changes: 22 additions & 26 deletions server/deeplinks/deeplinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,45 +84,41 @@ func EvaluateDeepLinksResponse(obj map[string]interface{}, name string, links []
finalLinks := []*application.LinkInfo{}
errors := []string{}
for _, link := range links {
t, err := template.New("deep-link").Funcs(sprigFuncMap).Parse(link.URL)
if err != nil {
errors = append(errors, fmt.Sprintf("failed to parse link template '%v', error=%v", link.URL, err.Error()))
continue
}
finalURL := bytes.Buffer{}
err = t.Execute(&finalURL, obj)
if err != nil {
errors = append(errors, fmt.Sprintf("failed to evaluate link template '%v' with resource %v, error=%v", link.URL, name, err.Error()))
continue
}
if link.Condition != nil {
out, err := expr.Eval(*link.Condition, obj)
if err != nil {
errors = append(errors, fmt.Sprintf("failed to evaluate link condition '%v' with resource %v, error=%v", *link.Condition, name, err.Error()))
continue
}
switch resOut := out.(type) {
switch condResult := out.(type) {
case bool:
if resOut {
finalLinks = append(finalLinks, &application.LinkInfo{
Title: ptr.To(link.Title),
Url: ptr.To(finalURL.String()),
Description: link.Description,
IconClass: link.IconClass,
})
if !condResult {
continue
}
default:
errors = append(errors, fmt.Sprintf("link condition '%v' evaluated to non-boolean value for resource %v", *link.Condition, name))
continue
}
} else {
finalLinks = append(finalLinks, &application.LinkInfo{
Title: ptr.To(link.Title),
Url: ptr.To(finalURL.String()),
Description: link.Description,
IconClass: link.IconClass,
})
}

t, err := template.New("deep-link").Funcs(sprigFuncMap).Parse(link.URL)
if err != nil {
errors = append(errors, fmt.Sprintf("failed to parse link template '%v', error=%v", link.URL, err.Error()))
continue
}
finalURL := bytes.Buffer{}
err = t.Execute(&finalURL, obj)
if err != nil {
errors = append(errors, fmt.Sprintf("failed to evaluate link template '%v' with resource %v, error=%v", link.URL, name, err.Error()))
continue
}

finalLinks = append(finalLinks, &application.LinkInfo{
Title: ptr.To(link.Title),
Url: ptr.To(finalURL.String()),
Description: link.Description,
IconClass: link.IconClass,
})
}
return &application.LinksResponse{
Items: finalLinks,
Expand Down
22 changes: 22 additions & 0 deletions server/deeplinks/deeplinks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,28 @@ func TestDeepLinks(t *testing.T) {
}},
error: []string{},
},
{
name: "evaluate template for valid condition",
appObj: appObj,
resourceObj: resourceObj,
projectObj: projectObj,
inputLinks: []settings.DeepLink{
{
Title: "link",
URL: "http://not-evaluated.com/{{ index \"invalid\" .application.metadata.labels }}",
Condition: ptr.To(`false`),
},
{
Title: "link",
URL: "http://evaluated.com/{{ index \"invalid\" .application.metadata.labels }}",
Condition: ptr.To(`true`),
},
},
outputLinks: []*application.LinkInfo{},
error: []string{
"failed to evaluate link template 'http://evaluated.com/{{ index \"invalid\" .application.metadata.labels }}' with resource test, error=template: deep-link:1:24: executing \"deep-link\" at <index \"invalid\" .application.metadata.labels>: error calling index: cannot index slice/array with nil",
},
},
}

for _, tc := range testTable {
Expand Down

0 comments on commit ca7a08e

Please sign in to comment.