Skip to content

Commit 935b8d6

Browse files
committed
Validate tags created for the resources
1 parent eaafc00 commit 935b8d6

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

api/v1beta1/tags.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1beta1
1818

1919
import (
2020
"fmt"
21+
"regexp"
2122

2223
"github.com/google/go-cmp/cmp"
2324
"k8s.io/apimachinery/pkg/types"
@@ -74,10 +75,18 @@ func (t Tags) Merge(other Tags) {
7475
}
7576
}
7677

77-
// Validate checks if tags are valid for the AWS API. Keys must have at
78-
// least 1 character and max 128. Values must be max 256 characters long.
78+
// Validate checks if tags are valid for the AWS API/Resources.
79+
// Keys must have at least 1 and max 128 characters.
80+
// Values must be max 256 characters long.
81+
// Keys and Values can only have alphabets, numbers, spaces and _ . : / = + - @ as characters.
82+
// Tag's key cannot have prefix "aws:".
83+
// Max count of User tags for a specific resource can be 50.
7984
func (t Tags) Validate() []*field.Error {
85+
// Defines the maximum number of user tags which can be created for a specific resource
86+
const maxUserTagsAllowed = 50
8087
var errs field.ErrorList
88+
var userTagCount = len(t)
89+
re := regexp.MustCompile(`^[a-zA-Z0-9\\s\_\.\:\=\+\-\@\/]*$`)
8190

8291
for k, v := range t {
8392
if len(k) < 1 {
@@ -95,11 +104,39 @@ func (t Tags) Validate() []*field.Error {
95104
field.Invalid(field.NewPath("spec", "additionalTags"), v, "value cannot be longer than 256 characters"),
96105
)
97106
}
107+
if wrongUserTagNomenclature(k) {
108+
errs = append(errs,
109+
field.Invalid(field.NewPath("spec", "additionalTags"), k, "user created tag's key cannot have prefix aws:"),
110+
)
111+
}
112+
val := re.MatchString(k)
113+
if !val {
114+
errs = append(errs,
115+
field.Invalid(field.NewPath("spec", "additionalTags"), k, "key cannot have characters other than alphabets, numbers, spaces and _ . : / = + - @ ."),
116+
)
117+
}
118+
val = re.MatchString(v)
119+
if !val {
120+
errs = append(errs,
121+
field.Invalid(field.NewPath("spec", "additionalTags"), v, "value cannot have characters other than alphabets, numbers, spaces and _ . : / = + - @ ."),
122+
)
123+
}
124+
}
125+
126+
if userTagCount > maxUserTagsAllowed {
127+
errs = append(errs,
128+
field.Invalid(field.NewPath("spec", "additionalTags"), t, "user created tags cannot be more than 50"),
129+
)
98130
}
99131

100132
return errs
101133
}
102134

135+
// Checks whether the tag created is user tag or not.
136+
func wrongUserTagNomenclature(k string) bool {
137+
return len(k) > 3 && k[0:4] == "aws:"
138+
}
139+
103140
// ResourceLifecycle configures the lifecycle of a resource.
104141
type ResourceLifecycle string
105142

api/v1beta1/tags_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,68 @@ func TestTags_Validate(t *testing.T) {
271271
},
272272
},
273273
},
274+
{
275+
name: "key has aws: prefix",
276+
self: Tags{
277+
"aws:key": "validValue",
278+
},
279+
expected: []*field.Error{
280+
{
281+
Type: field.ErrorTypeInvalid,
282+
Detail: "user created tag's key cannot have prefix aws:",
283+
Field: "spec.additionalTags",
284+
BadValue: "aws:key",
285+
},
286+
},
287+
},
288+
{
289+
name: "key has wrong characters",
290+
self: Tags{
291+
"wrong*key": "validValue",
292+
},
293+
expected: []*field.Error{
294+
{
295+
Type: field.ErrorTypeInvalid,
296+
Detail: "key cannot have characters other than alphabets, numbers, spaces and _ . : / = + - @ .",
297+
Field: "spec.additionalTags",
298+
BadValue: "wrong*key",
299+
},
300+
},
301+
},
302+
{
303+
name: "value has wrong characters",
304+
self: Tags{
305+
"validKey": "wrong*value",
306+
},
307+
expected: []*field.Error{
308+
{
309+
Type: field.ErrorTypeInvalid,
310+
Detail: "value cannot have characters other than alphabets, numbers, spaces and _ . : / = + - @ .",
311+
Field: "spec.additionalTags",
312+
BadValue: "wrong*value",
313+
},
314+
},
315+
},
316+
{
317+
name: "value and key both has wrong characters",
318+
self: Tags{
319+
"wrong*key": "wrong*value",
320+
},
321+
expected: []*field.Error{
322+
{
323+
Type: field.ErrorTypeInvalid,
324+
Detail: "key cannot have characters other than alphabets, numbers, spaces and _ . : / = + - @ .",
325+
Field: "spec.additionalTags",
326+
BadValue: "wrong*key",
327+
},
328+
{
329+
Type: field.ErrorTypeInvalid,
330+
Detail: "value cannot have characters other than alphabets, numbers, spaces and _ . : / = + - @ .",
331+
Field: "spec.additionalTags",
332+
BadValue: "wrong*value",
333+
},
334+
},
335+
},
274336
}
275337
for _, tc := range tests {
276338
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)