Skip to content

Commit

Permalink
Merge pull request #1638 from Ninir/f-ssm-document-name-validation
Browse files Browse the repository at this point in the history
Added validation for the SSM document name
  • Loading branch information
Ninir authored Sep 12, 2017
2 parents 904cd8a + f91a0ee commit 2d6194c
Show file tree
Hide file tree
Showing 3 changed files with 41 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
13 changes: 13 additions & 0 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,19 @@ func validateIamRoleDescription(v interface{}, k string) (ws []string, errors []
return
}

func validateAwsSSMName(v interface{}, k string) (ws []string, errors []error) {
// http://docs.aws.amazon.com/systems-manager/latest/APIReference/API_CreateDocument.html#EC2-CreateDocument-request-Name
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 2d6194c

Please sign in to comment.