@@ -18,6 +18,7 @@ package v1beta1
18
18
19
19
import (
20
20
"fmt"
21
+ "regexp"
21
22
22
23
"github.com/google/go-cmp/cmp"
23
24
"k8s.io/apimachinery/pkg/types"
@@ -74,10 +75,18 @@ func (t Tags) Merge(other Tags) {
74
75
}
75
76
}
76
77
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.
79
84
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
80
87
var errs field.ErrorList
88
+ var userTagCount = len (t )
89
+ re := regexp .MustCompile (`^[a-zA-Z0-9\\s\_\.\:\=\+\-\@\/]*$` )
81
90
82
91
for k , v := range t {
83
92
if len (k ) < 1 {
@@ -95,11 +104,39 @@ func (t Tags) Validate() []*field.Error {
95
104
field .Invalid (field .NewPath ("spec" , "additionalTags" ), v , "value cannot be longer than 256 characters" ),
96
105
)
97
106
}
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
+ )
98
130
}
99
131
100
132
return errs
101
133
}
102
134
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
+
103
140
// ResourceLifecycle configures the lifecycle of a resource.
104
141
type ResourceLifecycle string
105
142
0 commit comments