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

provider/aws: Validate WAF metric names #13885

Merged
merged 1 commit into from
Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions builtin/providers/aws/resource_aws_waf_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func resourceAwsWafRule() *schema.Resource {
ForceNew: true,
},
"metric_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateWafMetricName,
},
"predicates": &schema.Schema{
Type: schema.TypeSet,
Expand Down
7 changes: 4 additions & 3 deletions builtin/providers/aws/resource_aws_waf_web_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func resourceAwsWafWebAcl() *schema.Resource {
},
},
"metric_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateWafMetricName,
},
"rules": &schema.Schema{
Type: schema.TypeSet,
Expand Down
10 changes: 10 additions & 0 deletions builtin/providers/aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -1291,3 +1291,13 @@ func validateCognitoIdentityProvidersProviderName(v interface{}, k string) (ws [

return
}

func validateWafMetricName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9A-Za-z]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"Only alphanumeric characters allowed in %q: %q",
k, value))
}
return
}
31 changes: 31 additions & 0 deletions builtin/providers/aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2178,3 +2178,34 @@ func TestValidateCognitoIdentityProvidersProviderName(t *testing.T) {
}
}
}

func TestValidateWafMetricName(t *testing.T) {
validNames := []string{
"testrule",
"testRule",
"testRule123",
}
for _, v := range validNames {
_, errors := validateWafMetricName(v, "name")
if len(errors) != 0 {
t.Fatalf("%q should be a valid WAF metric name: %q", v, errors)
}
}

invalidNames := []string{
"!",
"/",
" ",
":",
";",
"white space",
"/slash-at-the-beginning",
"slash-at-the-end/",
}
for _, v := range invalidNames {
_, errors := validateWafMetricName(v, "name")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid WAF metric name", v)
}
}
}