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

service/iam: replace custom SchemaValidateFunc with provided ones #12865

Merged
merged 3 commits into from
May 12, 2020
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
20 changes: 7 additions & 13 deletions aws/resource_aws_iam_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/service/iam"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func resourceAwsIamGroup() *schema.Resource {
Expand All @@ -31,9 +32,12 @@ func resourceAwsIamGroup() *schema.Resource {
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAwsIamGroupName,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[0-9A-Za-z=,.@\-_+]+$`),
fmt.Sprintf("must only contain alphanumeric characters, hyphens, underscores, commas, periods, @ symbols, plus and equals signs"),
),
},
"path": {
Type: schema.TypeString,
Expand Down Expand Up @@ -130,13 +134,3 @@ func resourceAwsIamGroupDelete(d *schema.ResourceData, meta interface{}) error {
}
return nil
}

func validateAwsIamGroupName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9A-Za-z=,.@\-_+]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only alphanumeric characters, hyphens, underscores, commas, periods, @ symbols, plus and equals signs allowed in %q: %q",
k, value))
}
return
}
37 changes: 0 additions & 37 deletions aws/resource_aws_iam_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestValidateIamGroupName(t *testing.T) {
validNames := []string{
"test-group",
"test_group",
"testgroup123",
"TestGroup",
"Test-Group",
"test.group",
"test.123,group",
"testgroup@hashicorp",
"test+group@hashicorp.com",
}
for _, v := range validNames {
_, errs := validateAwsIamGroupName(v, "name")
if len(errs) != 0 {
t.Fatalf("%q should be a valid IAM Group name: %q", v, errs)
}
}

invalidNames := []string{
"!",
"/",
" ",
":",
";",
"test name",
"/slash-at-the-beginning",
"slash-at-the-end/",
}
for _, v := range invalidNames {
_, errs := validateAwsIamGroupName(v, "name")
if len(errs) == 0 {
t.Fatalf("%q should be an invalid IAM Group name", v)
}
}
}

func TestAccAWSIAMGroup_basic(t *testing.T) {
var conf iam.GetGroupOutput
resourceName := "aws_iam_group.test"
Expand Down
35 changes: 9 additions & 26 deletions aws/resource_aws_iam_instance_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func resourceAwsIamInstanceProfile() *schema.Resource {
Expand Down Expand Up @@ -45,39 +46,21 @@ func resourceAwsIamInstanceProfile() *schema.Resource {
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
// https://github.com/boto/botocore/blob/2485f5c/botocore/data/iam/2010-05-08/service-2.json#L8196-L8201
value := v.(string)
if len(value) > 128 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 128 characters", k))
}
if !regexp.MustCompile(`^[\w+=,.@-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must match [\\w+=,.@-]", k))
}
return
},
ValidateFunc: validation.All(
validation.StringLenBetween(1, 128),
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]*$`), "must match [\\w+=,.@-]"),
),
},

"name_prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"name"},
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
// https://github.com/boto/botocore/blob/2485f5c/botocore/data/iam/2010-05-08/service-2.json#L8196-L8201
value := v.(string)
if len(value) > 64 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 64 characters, name is limited to 128", k))
}
if !regexp.MustCompile(`^[\w+=,.@-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must match [\\w+=,.@-]", k))
}
return
},
ValidateFunc: validation.All(
validation.StringLenBetween(1, 64),
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]*$`), "must match [\\w+=,.@-]"),
),
},

"path": {
Expand Down
35 changes: 9 additions & 26 deletions aws/resource_aws_iam_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func resourceAwsIamPolicy() *schema.Resource {
Expand Down Expand Up @@ -47,38 +48,20 @@ func resourceAwsIamPolicy() *schema.Resource {
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
// https://github.com/boto/botocore/blob/2485f5c/botocore/data/iam/2010-05-08/service-2.json#L8329-L8334
value := v.(string)
if len(value) > 128 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 128 characters", k))
}
if !regexp.MustCompile(`^[\w+=,.@-]*$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must match [\\w+=,.@-]", k))
}
return
},
ValidateFunc: validation.All(
validation.StringLenBetween(1, 128),
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]*$`), "must match [\\w+=,.@-]"),
),
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"name"},
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
// https://github.com/boto/botocore/blob/2485f5c/botocore/data/iam/2010-05-08/service-2.json#L8329-L8334
value := v.(string)
if len(value) > 96 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 96 characters, name is limited to 128", k))
}
if !regexp.MustCompile(`^[\w+=,.@-]*$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must match [\\w+=,.@-]", k))
}
return
},
ValidateFunc: validation.All(
validation.StringLenBetween(1, 96),
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]*$`), "must match [\\w+=,.@-]"),
),
},
"arn": {
Type: schema.TypeString,
Expand Down
34 changes: 8 additions & 26 deletions aws/resource_aws_iam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,21 @@ func resourceAwsIamRole() *schema.Resource {
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
// https://github.com/boto/botocore/blob/2485f5c/botocore/data/iam/2010-05-08/service-2.json#L8329-L8334
value := v.(string)
if len(value) > 64 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 64 characters", k))
}
if !regexp.MustCompile(`^[\w+=,.@-]*$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must match [\\w+=,.@-]", k))
}
return
},
ValidateFunc: validation.All(
validation.StringLenBetween(1, 64),
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]*$`), "must match [\\w+=,.@-]"),
),
},

"name_prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"name"},
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
// https://github.com/boto/botocore/blob/2485f5c/botocore/data/iam/2010-05-08/service-2.json#L8329-L8334
value := v.(string)
if len(value) > 32 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 32 characters, name is limited to 64", k))
}
if !regexp.MustCompile(`^[\w+=,.@-]*$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must match [\\w+=,.@-]", k))
}
return
},
ValidateFunc: validation.All(
validation.StringLenBetween(1, 32),
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]*$`), "must match [\\w+=,.@-]"),
),
},

"path": {
Expand Down
19 changes: 6 additions & 13 deletions aws/resource_aws_iam_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ func resourceAwsIamUser() *schema.Resource {
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAwsIamUserName,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[0-9A-Za-z=,.@\-_+]+$`),
fmt.Sprintf("must only contain alphanumeric characters, hyphens, underscores, commas, periods, @ symbols, plus and equals signs"),
),
},
"path": {
Type: schema.TypeString,
Expand Down Expand Up @@ -244,16 +247,6 @@ func resourceAwsIamUserDelete(d *schema.ResourceData, meta interface{}) error {
return nil
}

func validateAwsIamUserName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9A-Za-z=,.@\-_+]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only alphanumeric characters, hyphens, underscores, commas, periods, @ symbols, plus and equals signs allowed in %q: %q",
k, value))
}
return
}

func deleteAwsIamUserGroupMemberships(conn *iam.IAM, username string) error {
var groups []string
listGroups := &iam.ListGroupsForUserInput{
Expand Down
37 changes: 0 additions & 37 deletions aws/resource_aws_iam_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,6 @@ import (
"github.com/pquerna/otp/totp"
)

func TestValidateIamUserName(t *testing.T) {
validNames := []string{
"test-user",
"test_user",
"testuser123",
"TestUser",
"Test-User",
"test.user",
"test.123,user",
"testuser@hashicorp",
"test+user@hashicorp.com",
}
for _, v := range validNames {
_, errors := validateAwsIamUserName(v, "name")
if len(errors) != 0 {
t.Fatalf("%q should be a valid IAM User name: %q", v, errors)
}
}

invalidNames := []string{
"!",
"/",
" ",
":",
";",
"test name",
"/slash-at-the-beginning",
"slash-at-the-end/",
}
for _, v := range invalidNames {
_, errors := validateAwsIamUserName(v, "name")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid IAM User name", v)
}
}
}

func init() {
resource.AddTestSweepers("aws_iam_user", &resource.Sweeper{
Name: "aws_iam_user",
Expand Down