From ec2f343f44d1534902c200cf5e19c4d5e762deb9 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Fri, 29 Mar 2024 02:44:32 +0000 Subject: [PATCH] fix: allow special char in tag value fix --- pkg/azuredisk/azure_managedDiskController.go | 6 ++---- pkg/util/util.go | 4 ++++ pkg/util/util_test.go | 12 ++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/azuredisk/azure_managedDiskController.go b/pkg/azuredisk/azure_managedDiskController.go index 630e96f07c..11542050ac 100644 --- a/pkg/azuredisk/azure_managedDiskController.go +++ b/pkg/azuredisk/azure_managedDiskController.go @@ -124,10 +124,8 @@ func (c *ManagedDiskController) CreateManagedDisk(ctx context.Context, options * newTags[consts.CreatedByTag] = &azureDDTag if options.Tags != nil { for k, v := range options.Tags { - // Azure won't allow / (forward slash) in tags - newKey := strings.Replace(k, "/", "-", -1) - newValue := strings.Replace(v, "/", "-", -1) - newTags[newKey] = &newValue + value := v + newTags[k] = &value } } diff --git a/pkg/util/util.go b/pkg/util/util.go index 61dd6c91be..90bfe33cb4 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -96,6 +96,10 @@ func ConvertTagsToMap(tags string) (map[string]string, error) { if key == "" { return nil, fmt.Errorf("Tags '%s' are invalid, the format should like: 'key1=value1,key2=value2'", tags) } + // <>%&?/. are not allowed in tag key + if strings.ContainsAny(key, "<>%&?/.") { + return nil, fmt.Errorf("Tag key '%s' contains invalid characters", key) + } value := strings.TrimSpace(kv[1]) m[key] = value } diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index b7e9b9439d..0cc62e791f 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -109,6 +109,18 @@ func TestConvertTagsToMap(t *testing.T) { expectedOutput: nil, expectedError: true, }, + { + desc: "should return error for invalid characters in key", + tags: "key/1=value1,%&?/.", + expectedOutput: map[string]string{"key1": "value1", "key2": "<>%&?/."}, + expectedError: false, + }, } for i, c := range testCases {