From 0a9b12b22f6553ec9cd806ae9a80c7cc5f7ceaee Mon Sep 17 00:00:00 2001 From: Sid Shukla Date: Wed, 2 Oct 2024 11:09:52 +0200 Subject: [PATCH 1/2] fix(bug): Encode name in filter by name FIQL Query Not encoding the name can cause errors when non-standard characters are present in the name. --- controllers/helpers.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/controllers/helpers.go b/controllers/helpers.go index 4efe8a521e..1d5688fe1a 100644 --- a/controllers/helpers.go +++ b/controllers/helpers.go @@ -20,6 +20,7 @@ import ( "context" "errors" "fmt" + "net/url" "reflect" "strconv" "strings" @@ -664,7 +665,8 @@ func GetProjectUUID(ctx context.Context, client *prismclientv3.Client, projectNa } func getFilterForName(name string) string { - return fmt.Sprintf("name==%s", name) + encodedName := url.QueryEscape(name) + return fmt.Sprintf("name==%s", encodedName) } func hasPEClusterServiceEnabled(peCluster *prismclientv3.ClusterIntentResponse, serviceName string) bool { From cfbda29889ea822fd7eb3bf6db0c4e4f3b496664 Mon Sep 17 00:00:00 2001 From: Sid Shukla Date: Wed, 2 Oct 2024 12:39:41 +0200 Subject: [PATCH 2/2] Add unit tests for FIQL name filter function --- controllers/helpers_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/controllers/helpers_test.go b/controllers/helpers_test.go index 5093b40981..acc23a23d5 100644 --- a/controllers/helpers_test.go +++ b/controllers/helpers_test.go @@ -229,3 +229,39 @@ func TestGetPrismCentralClientForCluster(t *testing.T) { assert.NoError(t, err) }) } + +func TestGetFilterForName(t *testing.T) { + tt := []struct { + name string + expected string + testName string + }{ + { + name: "test-name", + expected: "name==test-name", + testName: "ReturnsCorrectFilterForValidName", + }, + { + name: "test name with spaces", + expected: "name==test+name+with+spaces", + testName: "ReturnsCorrectFilterForNameWithSpecialCharacters", + }, + { + name: "", + expected: "name==", + testName: "ReturnsCorrectFilterForEmptyName", + }, + { + name: "test|name=with?special#characters", + expected: "name==test%7Cname%3Dwith%3Fspecial%23characters", + testName: "ReturnsCorrectFilterForNameWithURLCharacters", + }, + } + + for _, tc := range tt { + t.Run(tc.testName, func(t *testing.T) { + filter := getFilterForName(tc.name) + assert.Equal(t, tc.expected, filter) + }) + } +}