From ca7a08eb9557d7dba8969ba640448caab0b53b94 Mon Sep 17 00:00:00 2001 From: Alexandre Gaudreault Date: Tue, 10 Sep 2024 16:34:51 -0400 Subject: [PATCH] fix(deeplinks): do not evaluate template when condition is false (#19625) (#19868) Signed-off-by: Alexandre Gaudreault --- server/deeplinks/deeplinks.go | 48 ++++++++++++++---------------- server/deeplinks/deeplinks_test.go | 22 ++++++++++++++ 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/server/deeplinks/deeplinks.go b/server/deeplinks/deeplinks.go index fae05ecc49e4f..933dcde42decb 100644 --- a/server/deeplinks/deeplinks.go +++ b/server/deeplinks/deeplinks.go @@ -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, diff --git a/server/deeplinks/deeplinks_test.go b/server/deeplinks/deeplinks_test.go index 2132648a966d6..217b8b93b7dbe 100644 --- a/server/deeplinks/deeplinks_test.go +++ b/server/deeplinks/deeplinks_test.go @@ -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 : error calling index: cannot index slice/array with nil", + }, + }, } for _, tc := range testTable {