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

Add validation to google_project_iam_custom_role.id #1712

Merged
merged 2 commits into from
May 6, 2019
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
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
2 changes: 1 addition & 1 deletion build/terraform-mapper
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func resourceGoogleProjectIamCustomRole() *schema.Resource {

Schema: map[string]*schema.Schema{
"role_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateIAMCustomRoleID,
},
"title": {
Type: schema.TypeString,
Expand Down
12 changes: 12 additions & 0 deletions third_party/terraform/utils/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (
// Format of default Compute service accounts created by Google
// ${PROJECT_ID}-compute@developer.gserviceaccount.com where PROJECT_ID is an int64 (max 20 digits)
ComputeServiceAccountNameRegex = "[0-9]{1,20}-compute@developer.gserviceaccount.com"

// https://cloud.google.com/iam/docs/understanding-custom-roles#naming_the_role
IAMCustomRoleIDRegex = "^[a-zA-Z0-9_\\.\\-]{1,30}$"
)

var (
Expand Down Expand Up @@ -155,6 +158,15 @@ func validateCloudIoTID(v interface{}, k string) (warnings []string, errors []er
return
}

func validateIAMCustomRoleID(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(IAMCustomRoleIDRegex).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q (%q) doesn't match regexp %q", k, value, IAMCustomRoleIDRegex))
}
return
}

func orEmpty(f schema.SchemaValidateFunc) schema.SchemaValidateFunc {
return func(i interface{}, k string) ([]string, []error) {
v, ok := i.(string)
Expand Down
27 changes: 27 additions & 0 deletions third_party/terraform/utils/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,30 @@ func TestValidateProjectName(t *testing.T) {
t.Errorf("Failed to validate project ID's: %v", es)
}
}

func TestValidateIAMCustomRoleIDRegex(t *testing.T) {
x := []StringValidationTestCase{
// No errors
{TestName: "basic", Value: "foobar"},
{TestName: "with numbers", Value: "foobar123"},
{TestName: "with capipals", Value: "FooBar"},
{TestName: "short", Value: "f"},
{TestName: "long", Value: "foobarfoobarfoobarfoobarfoobar"},
{TestName: "has a hyphen", Value: "foo-bar"},
{TestName: "has a dot", Value: "foo.bar"},
{TestName: "has an underscore", Value: "foo_bar"},
{TestName: "all of the above", Value: "foo.Bar-Baz_123"},

// With errors
{TestName: "empty", Value: "", ExpectError: true},
{TestName: "has an slash", Value: "foo/bar", ExpectError: true},
{TestName: "has a dollar", Value: "foo$", ExpectError: true},
{TestName: "has a space", Value: "foo bar", ExpectError: true},
{TestName: "too long", Value: strings.Repeat("f", 31), ExpectError: true},
}

es := testStringValidationCases(x, validateIAMCustomRoleID)
if len(es) > 0 {
t.Errorf("Failed to validate IAMCustomRole IDs: %v", es)
}
}