Skip to content

Commit

Permalink
fix: allow special char in tag value
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
andyzhangx authored and k8s-infra-cherrypick-robot committed Mar 30, 2024
1 parent 74cbf2f commit ec2f343
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 ec2f343

Please sign in to comment.