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

resource/aws_ssm_document: Added name validation #1638

Merged
merged 2 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions aws/resource_aws_ssm_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func resourceAwsSsmDocument() *schema.Resource {
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAwsSSMName,
},
"content": {
Type: schema.TypeString,
Expand Down
12 changes: 12 additions & 0 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,18 @@ func validateIamRoleDescription(v interface{}, k string) (ws []string, errors []
return
}

func validateAwsSSMName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if !regexp.MustCompile(`^[a-zA-Z0-9_\-.]{3,128}$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"Only alphanumeric characters, hyphens, dots & underscores allowed in %q: %q (Must satisfy regular expression pattern: ^[a-zA-Z0-9_\\-.]{3,128}$)",
k, value))
}

return
}

func validateSsmParameterType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
types := map[string]bool{
Expand Down
25 changes: 25 additions & 0 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2375,6 +2375,31 @@ func TestValidateIamRoleDescription(t *testing.T) {
}
}

func TestValidateAwsSSMName(t *testing.T) {
validNames := []string{
".foo-bar_123",
strings.Repeat("W", 128),
}
for _, v := range validNames {
_, errors := validateAwsSSMName(v, "name")
if len(errors) != 0 {
t.Fatalf("%q should be a valid SSM Name: %q", v, errors)
}
}

invalidNames := []string{
"foo+bar",
"tf",
strings.Repeat("W", 129), // > 128
}
for _, v := range invalidNames {
_, errors := validateAwsSSMName(v, "name")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid SSM Name: %q", v, errors)
}
}
}

func TestValidateSsmParameterType(t *testing.T) {
validTypes := []string{
"String",
Expand Down