-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Bug - Fix regex for aws_transfer_user #8304
Changes from all commits
06325c4
0261323
0714b28
7345bd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,37 @@ func TestAccAWSTransferUser_disappears(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccAWSTransferUserName_validation(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSTransferUserDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSTransferUserName_validation("!@#$%^"), | ||
ExpectError: regexp.MustCompile(`Invalid "user_name": must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, "user_name" cannot begin with a hyphen`), | ||
}, | ||
{ | ||
Config: testAccAWSTransferUserName_validation(acctest.RandString(2)), | ||
ExpectError: regexp.MustCompile(`Invalid "user_name": must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, "user_name" cannot begin with a hyphen`), | ||
}, | ||
{ | ||
Config: testAccAWSTransferUserName_validation(acctest.RandString(33)), | ||
ExpectError: regexp.MustCompile(`Invalid "user_name": must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, "user_name" cannot begin with a hyphen`), | ||
}, | ||
{ | ||
Config: testAccAWSTransferUserName_validation("-abcdef"), | ||
ExpectError: regexp.MustCompile(`Invalid "user_name": must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, "user_name" cannot begin with a hyphen`), | ||
}, | ||
{ | ||
Config: testAccAWSTransferUserName_validation("valid_username"), | ||
ExpectNonEmptyPlan: true, | ||
PlanOnly: true, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's cool! You never test that your validation function itself "passes" though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I understand, is that not what I had already done via below?
I'll be sure to add whatever else is required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant is that you've tested all the errors but you have no test saying that the name is correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood, I'll take care of that now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @charlyx could you take another look at this? |
||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSTransferUserExists(n string, res *transfer.DescribedUser) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
|
@@ -260,6 +291,40 @@ resource "aws_transfer_user" "foo" { | |
`, rName, rName) | ||
} | ||
|
||
func testAccAWSTransferUserName_validation(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_transfer_server" "foo" { | ||
identity_provider_type = "SERVICE_MANAGED" | ||
tags = { | ||
NAME = "tf-acc-test-transfer-server" | ||
} | ||
} | ||
resource "aws_transfer_user" "foo" { | ||
server_id = "${aws_transfer_server.foo.id}" | ||
user_name = "%s" | ||
role = "${aws_iam_role.foo.arn}" | ||
} | ||
resource "aws_iam_role" "foo" { | ||
name = "tf-test-transfer-user-iam-role" | ||
|
||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "transfer.amazonaws.com" | ||
}, | ||
"Action": "sts:AssumeRole" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccAWSTransferUserConfig_options(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_transfer_server" "foo" { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,15 +117,10 @@ func validateTransferServerID(v interface{}, k string) (ws []string, errors []er | |
|
||
func validateTransferUserName(v interface{}, k string) (ws []string, errors []error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: In the future this can be simplified to use Terraform Provider SDK validation functions to show multiple validation errors easier: ValidateFunc: validation.All(
validation.StringLenBetween(3, 32),
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9_]`), "must begin with alphanumeric or underscore character"),
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9-_]+$`), "must contain only alphanumeric, hyphen, and underscore characters"),
), There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I wasn't aware of this. Is there a tech-debt effort to change other validation functions as well in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought I had created the technical debt issue for that, but apparently not. I'll try to get that created later today. For now, this is a good start on a similar effort: #7906 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's an initial proposal issue for fixing up |
||
value := v.(string) | ||
|
||
// https://docs.aws.amazon.com/transfer/latest/userguide/API_CreateUser.html | ||
pattern := `^[a-z0-9]{3,32}$` | ||
if !regexp.MustCompile(pattern).MatchString(value) { | ||
errors = append(errors, fmt.Errorf( | ||
"%q isn't a valid transfer user name (only lowercase alphanumeric characters are allowed): %q", | ||
k, value)) | ||
if !regexp.MustCompile(`^[a-zA-Z0-9_][a-zA-Z0-9_-]{2,31}$`).MatchString(value) { | ||
errors = append(errors, fmt.Errorf("Invalid %q: must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, %q cannot begin with a hyphen", k, k)) | ||
} | ||
|
||
return | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: The acceptance test naming format should match:
TestAccAWSTransferUser_XXX
so all testing for the resource can be run with the same prefix and underscore, e.g.TestAccAWSTransferUser_UserName_Validation
Otherwise we have this situation (note lack of this new test being run):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll update and make another PR. Thanks for the review!