Skip to content

Commit

Permalink
Merge pull request #2247 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…2246-to-release-1.30

[release-1.30] fix: allow special char in tag value
  • Loading branch information
andyzhangx committed Mar 31, 2024
2 parents 74cbf2f + ec2f343 commit d8abdb2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 2 additions & 4 deletions pkg/azuredisk/azure_managedDiskController.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,<bar",
expectedOutput: nil,
expectedError: true,
},
{
desc: "should return success for special characters in value",
tags: "key1=value1,key2=<>%&?/.",
expectedOutput: map[string]string{"key1": "value1", "key2": "<>%&?/."},
expectedError: false,
},
}

for i, c := range testCases {
Expand Down

0 comments on commit d8abdb2

Please sign in to comment.