Skip to content

Commit

Permalink
Added validation for the SSM document name
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninir committed Sep 11, 2017
1 parent 328f135 commit 9ddc832
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
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

0 comments on commit 9ddc832

Please sign in to comment.