Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate account_id length in google_service_account #793

Merged
merged 4 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions google/resource_google_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ func resourceGoogleServiceAccount() *schema.Resource {
Computed: true,
},
"account_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateRFC1035Name(6, 30),
},
"display_name": &schema.Schema{
Type: schema.TypeString,
Expand Down
7 changes: 4 additions & 3 deletions google/resource_google_service_account_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ func resourceGoogleServiceAccountKey() *schema.Resource {
Schema: map[string]*schema.Schema{
// Required
"service_account_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateRFC1035Name(6, 30),
},
// Optional
"key_algorithm": &schema.Schema{
Expand Down
16 changes: 16 additions & 0 deletions google/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,19 @@ func validateRFC3339Time(v interface{}, k string) (warnings []string, errors []e
}
return
}

func validateRFC1035Name(min, max int) schema.SchemaValidateFunc {
if min < 2 || max < min {
return func(i interface{}, k string) (s []string, errors []error) {
if min < 2 {
errors = append(errors, fmt.Errorf("min must be at least 2. Got: %d", min))
}
if max < min {
errors = append(errors, fmt.Errorf("max must greater than min. Got [%d, %d]", min, max))
}
return
}
}

return validateRegexp(fmt.Sprintf(`^[a-z]([-a-z0-9]{%d,%d}[a-z0-9])$`, min-2, max-2))
}
32 changes: 32 additions & 0 deletions google/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,38 @@ func TestValidateRFC3339Time(t *testing.T) {
}
}

func TestValidateRFC1035Name(t *testing.T) {
cases := []struct {
TestName string
Value string
Min, Max int
ExpectError bool
}{
{TestName: "valid", Min: 6, Max: 30, Value: "a-valid-name0"},
{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: "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: 1, Max: 10, Value: "", ExpectError: true},
{TestName: "min < max", Min: 6, Max: 5, Value: "", ExpectError: true},
}

for _, c := range cases {
errors := testStringValidation(StringValidationTestCase{
TestName: c.TestName,
Value: c.Value,
ExpectError: c.ExpectError,
}, validateRFC1035Name(c.Min, c.Max))

if len(errors) > 0 {
t.Errorf("%s failed; %v", c.TestName, errors)
}
}
}

type StringValidationTestCase struct {
TestName string
Value string
Expand Down