Skip to content

Commit

Permalink
add new constraints on length. and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
karolgorc committed Oct 11, 2024
1 parent db61094 commit 823b3e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
35 changes: 21 additions & 14 deletions mmv1/third_party/terraform/verify/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (

SubnetworkLinkRegex = "projects/(" + ProjectRegex + ")/regions/(" + RegionRegex + ")/subnetworks/(" + SubnetworkRegex + ")$"

RFC1035NameTemplate = "[a-z]([-a-z0-9]{%d,%d}[a-z0-9])?"
RFC1035NameTemplate = "[a-z]([-a-z0-9]%v[a-z0-9])?"
CloudIoTIdRegex = "^[a-zA-Z][-a-zA-Z0-9._+~%]{2,254}$"

// Format of default Compute service accounts created by Google
Expand All @@ -41,7 +41,7 @@ var (
// The first and last characters have different restrictions, than
// the middle characters. The middle characters length must be between
// 4 and 28 since the first and last character are excluded.
ServiceAccountNameRegex = fmt.Sprintf(RFC1035NameTemplate, 4, 28)
ServiceAccountNameRegex = fmt.Sprintf(RFC1035NameTemplate, "{4,28}")

ServiceAccountLinkRegexPrefix = "projects/" + ProjectRegexWildCard + "/serviceAccounts/"
PossibleServiceAccountNames = []string{
Expand All @@ -54,7 +54,7 @@ var (
ServiceAccountKeyNameRegex = ServiceAccountLinkRegexPrefix + "(.+)/keys/(.+)"

// Format of service accounts created through the API
CreatedServiceAccountNameRegex = fmt.Sprintf(RFC1035NameTemplate, 4, 28) + "@" + ProjectNameInDNSFormRegex + "\\.iam\\.gserviceaccount\\.com$"
CreatedServiceAccountNameRegex = fmt.Sprintf(RFC1035NameTemplate, "{4,28}") + "@" + ProjectNameInDNSFormRegex + "\\.iam\\.gserviceaccount\\.com$"

// Format of service-created service account
// examples are:
Expand Down Expand Up @@ -194,19 +194,26 @@ func ValidateRFC3339Time(v interface{}, k string) (warnings []string, errors []e
}

func ValidateRFC1035Name(min, max int) schema.SchemaValidateFunc {
if min < 1 || max < min {
return func(i interface{}, k string) (s []string, errors []error) {
if min < 1 {
errors = append(errors, fmt.Errorf("min must be at least 1. Got: %d", min))
}
if max < min {
errors = append(errors, fmt.Errorf("max must greater than min. Got [%d, %d]", min, max))
}
return
return func(i interface{}, k string) (s []string, errors []error) {
value := i.(string)
re := fmt.Sprintf("^"+RFC1035NameTemplate+"$", "*")
if min < 1 {
errors = append(errors, fmt.Errorf("min must be at least 1. Got: %d", min))
}
if max < min {
errors = append(errors, fmt.Errorf("max must greater than min. Got [%d, %d]", min, max))
}

if len(value) < min || len(value) > max {
errors = append(errors, fmt.Errorf("%q (%q) must be between %d and %d characters long", k, value, min, max))
}

if !regexp.MustCompile(re).MatchString(value) {
errors = append(errors, fmt.Errorf("%q (%q) must match regex %q", k, value, re))
}
}

return ValidateRegexp(fmt.Sprintf("^"+RFC1035NameTemplate+"$", min-1, max-2))
return
}
}

func ValidateIpCidrRange(v interface{}, k string) (warnings []string, errors []error) {
Expand Down
6 changes: 6 additions & 0 deletions mmv1/third_party/terraform/verify/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,18 @@ func TestValidateRFC1035Name(t *testing.T) {
{TestName: "valid lower bound", Min: 12, Max: 30, Value: "a-valid-name"},
{TestName: "valid upper bound", Min: 6, Max: 12, Value: "a-valid-name"},
{TestName: "valid with numbers", Min: 6, Max: 30, Value: "valid000-name"},
{TestName: "valid shortest", Min: 1, Max: 63, Value: "a"},
{TestName: "valid longest", Min: 1, Max: 63, Value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{TestName: "must start with a letter", Min: 6, Max: 10, Value: "0invalid", ExpectError: true},
{TestName: "cannot end with a dash", Min: 6, Max: 10, Value: "invalid-", ExpectError: true},
{TestName: "too short", Min: 6, Max: 10, Value: "short", ExpectError: true},
{TestName: "too long", Min: 6, Max: 10, Value: "toolooooong", ExpectError: true},
{TestName: "min too small", Min: 0, Max: 10, Value: "", ExpectError: true},
{TestName: "min < max", Min: 6, Max: 5, Value: "", ExpectError: true},
{TestName: "min < max", Min: 6, Max: 5, Value: "", ExpectError: true},
{TestName: "invalid smallest possible w/ higher limit", Min: 2, Max: 63, Value: "a", ExpectError: true},
{TestName: "invalid smallest possible hyphen", Min: 1, Max: 1, Value: "-", ExpectError: true},
{TestName: "invalid smallest possible ends with hyphen", Min: 2, Max: 63, Value: "a-", ExpectError: true},
}

for _, c := range cases {
Expand Down

0 comments on commit 823b3e7

Please sign in to comment.