Skip to content

Commit

Permalink
resource/aws_mq_broker: validate user password length range
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaowei.wang committed Jan 28, 2018
1 parent 45210b8 commit 7276aea
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
7 changes: 4 additions & 3 deletions aws/resource_aws_mq_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ func resourceAwsMqBroker() *schema.Resource {
Optional: true,
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
Type: schema.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validateStringLengthRange(12, 250),
},
"username": {
Type: schema.TypeString,
Expand Down
11 changes: 11 additions & 0 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@ func validateMaxLength(length int) schema.SchemaValidateFunc {
}
}

func validateStringLengthRange(min, max int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) < min || len(value) > max {
errors = append(errors, fmt.Errorf(
"%q must be %d to %d characters long. provided string length: %d", k, min, max, len(value)))
}
return
}
}

func validateIntegerInRange(min, max int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
Expand Down
20 changes: 20 additions & 0 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,26 @@ func TestValidateIntegerInRange(t *testing.T) {
}
}

func TestValidateStringLengthRange(t *testing.T) {
min := 7
max := 11
validStrings := []string{"1234567", "1234567890", "12345678901"}
for _, v := range validStrings {
_, errors := validateStringLengthRange(min, max)(v, "name")
if len(errors) != 0 {
t.Fatalf("length of %q %d should be in range [%d, %d]: %q", v, len(v), min, max, errors)
}
}

invalidStrings := []string{"123456", "123456789012"}
for _, v := range invalidStrings {
_, errors := validateStringLengthRange(min, max)(v, "name")
if len(errors) == 0 {
t.Fatalf("length of %q %d should be outside range [%d, %d]", v, len(v), min, max)
}
}
}

func TestResourceAWSElastiCacheClusterIdValidation(t *testing.T) {
cases := []struct {
Value string
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/mq_broker.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ The following arguments are supported:

* `console_access` - (Optional) Whether to enable access to the the [ActiveMQ Web Console](http://activemq.apache.org/web-console.html) for the user.
* `groups` - (Optional) The list of groups (20 maximum) to which the ActiveMQ user belongs.
* `password` - (Required) The password of the user.
* `password` - (Required) The password of the user, must be 12 to 250 characters long.
* `username` - (Required) The username of the user.

## Attributes Reference
Expand Down

0 comments on commit 7276aea

Please sign in to comment.