aws: Add validation for ECR repository name
This commit is contained in:
parent
a367886eaf
commit
d853d2cc93
|
@ -112,3 +112,25 @@ func validateElbName(v interface{}, k string) (ws []string, errors []error) {
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateEcrRepositoryName(v interface{}, k string) (ws []string, errors []error) {
|
||||||
|
value := v.(string)
|
||||||
|
if len(value) < 2 {
|
||||||
|
errors = append(errors, fmt.Errorf(
|
||||||
|
"%q must be at least 2 characters long: %q", k, value))
|
||||||
|
}
|
||||||
|
if len(value) > 256 {
|
||||||
|
errors = append(errors, fmt.Errorf(
|
||||||
|
"%q cannot be longer than 256 characters: %q", k, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_CreateRepository.html
|
||||||
|
pattern := `^(?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*$`
|
||||||
|
if !regexp.MustCompile(pattern).MatchString(value) {
|
||||||
|
errors = append(errors, fmt.Errorf(
|
||||||
|
"%q doesn't comply with restrictions (%q): %q",
|
||||||
|
k, pattern, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package aws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestValidateEcrRepositoryName(t *testing.T) {
|
||||||
|
validNames := []string{
|
||||||
|
"nginx-web-app",
|
||||||
|
"project-a/nginx-web-app",
|
||||||
|
"domain.ltd/nginx-web-app",
|
||||||
|
"3chosome-thing.com/01different-pattern",
|
||||||
|
"0123456789/999999999",
|
||||||
|
"double/forward/slash",
|
||||||
|
"000000000000000",
|
||||||
|
}
|
||||||
|
for _, v := range validNames {
|
||||||
|
_, errors := validateEcrRepositoryName(v, "name")
|
||||||
|
if len(errors) != 0 {
|
||||||
|
t.Fatalf("%q should be a valid ECR repository name: %q", v, errors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
invalidNames := []string{
|
||||||
|
// length > 256
|
||||||
|
"3cho_some-thing.com/01different.-_pattern01different.-_pattern01diff" +
|
||||||
|
"erent.-_pattern01different.-_pattern01different.-_pattern01different" +
|
||||||
|
".-_pattern01different.-_pattern01different.-_pattern01different.-_pa" +
|
||||||
|
"ttern01different.-_pattern01different.-_pattern234567",
|
||||||
|
// length < 2
|
||||||
|
"i",
|
||||||
|
"special@character",
|
||||||
|
"different+special=character",
|
||||||
|
"double//slash",
|
||||||
|
"double..dot",
|
||||||
|
"/slash-at-the-beginning",
|
||||||
|
"slash-at-the-end/",
|
||||||
|
}
|
||||||
|
for _, v := range invalidNames {
|
||||||
|
_, errors := validateEcrRepositoryName(v, "name")
|
||||||
|
if len(errors) == 0 {
|
||||||
|
t.Fatalf("%q should be an invalid ECR repository name", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue