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

resources/wafv2_web_acl and wafv2_rule_group: add support for ForwardedIPConfig #14685

Merged
merged 2 commits into from
Aug 20, 2020
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
121 changes: 110 additions & 11 deletions aws/resource_aws_wafv2_rule_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,12 @@ func TestAccAwsWafv2RuleGroup_GeoMatchStatement(t *testing.T) {
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "wafv2", regexp.MustCompile(`regional/rulegroup/.+$`)),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "rule.*", map[string]string{
"statement.#": "1",
"statement.0.geo_match_statement.#": "1",
"statement.0.geo_match_statement.0.country_codes.#": "2",
"statement.0.geo_match_statement.0.country_codes.0": "US",
"statement.0.geo_match_statement.0.country_codes.1": "NL",
"statement.#": "1",
"statement.0.geo_match_statement.#": "1",
"statement.0.geo_match_statement.0.country_codes.#": "2",
"statement.0.geo_match_statement.0.country_codes.0": "US",
"statement.0.geo_match_statement.0.country_codes.1": "NL",
"statement.0.geo_match_statement.0.forwarded_ip_config.#": "0",
}),
),
},
Expand All @@ -705,12 +706,69 @@ func TestAccAwsWafv2RuleGroup_GeoMatchStatement(t *testing.T) {
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "wafv2", regexp.MustCompile(`regional/rulegroup/.+$`)),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "rule.*", map[string]string{
"statement.#": "1",
"statement.0.geo_match_statement.#": "1",
"statement.0.geo_match_statement.0.country_codes.#": "3",
"statement.0.geo_match_statement.0.country_codes.0": "ZM",
"statement.0.geo_match_statement.0.country_codes.1": "EE",
"statement.0.geo_match_statement.0.country_codes.2": "MM",
"statement.#": "1",
"statement.0.geo_match_statement.#": "1",
"statement.0.geo_match_statement.0.country_codes.#": "3",
"statement.0.geo_match_statement.0.country_codes.0": "ZM",
"statement.0.geo_match_statement.0.country_codes.1": "EE",
"statement.0.geo_match_statement.0.country_codes.2": "MM",
"statement.0.geo_match_statement.0.forwarded_ip_config.#": "0",
}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccAwsWafv2RuleGroupImportStateIdFunc(resourceName),
},
},
})
}

func TestAccAwsWafv2RuleGroup_GeoMatchStatement_ForwardedIPConfig(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsWafv2RuleGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsWafv2RuleGroupConfig_GeoMatchStatement_ForwardedIPConfig(ruleGroupName, "MATCH", "X-Forwarded-For"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsWafv2RuleGroupExists(resourceName, &v),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "wafv2", regexp.MustCompile(`regional/rulegroup/.+$`)),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "rule.*", map[string]string{
"statement.#": "1",
"statement.0.geo_match_statement.#": "1",
"statement.0.geo_match_statement.0.country_codes.#": "2",
"statement.0.geo_match_statement.0.country_codes.0": "US",
"statement.0.geo_match_statement.0.country_codes.1": "NL",
"statement.0.geo_match_statement.0.forwarded_ip_config.#": "1",
"statement.0.geo_match_statement.0.forwarded_ip_config.0.fallback_behavior": "MATCH",
"statement.0.geo_match_statement.0.forwarded_ip_config.0.header_name": "X-Forwarded-For",
}),
),
},
{
Config: testAccAwsWafv2RuleGroupConfig_GeoMatchStatement_ForwardedIPConfig(ruleGroupName, "NO_MATCH", "Updated"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsWafv2RuleGroupExists(resourceName, &v),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "wafv2", regexp.MustCompile(`regional/rulegroup/.+$`)),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "rule.*", map[string]string{
"statement.#": "1",
"statement.0.geo_match_statement.#": "1",
"statement.0.geo_match_statement.0.country_codes.#": "2",
"statement.0.geo_match_statement.0.country_codes.0": "US",
"statement.0.geo_match_statement.0.country_codes.1": "NL",
"statement.0.geo_match_statement.0.forwarded_ip_config.#": "1",
"statement.0.geo_match_statement.0.forwarded_ip_config.0.fallback_behavior": "NO_MATCH",
"statement.0.geo_match_statement.0.forwarded_ip_config.0.header_name": "Updated",
}),
),
},
Expand Down Expand Up @@ -2201,6 +2259,47 @@ resource "aws_wafv2_rule_group" "test" {
`, name)
}

func testAccAwsWafv2RuleGroupConfig_GeoMatchStatement_ForwardedIPConfig(name, fallbackBehavior, headerName string) string {
return fmt.Sprintf(`
resource "aws_wafv2_rule_group" "test" {
capacity = 2
name = "%s"
scope = "REGIONAL"

rule {
name = "rule-1"
priority = 1

action {
allow {}
}

statement {
geo_match_statement {
country_codes = ["US", "NL"]
forwarded_ip_config {
fallback_behavior = "%s"
header_name = "%s"
}
}
}

visibility_config {
cloudwatch_metrics_enabled = false
metric_name = "friendly-rule-metric-name"
sampled_requests_enabled = false
}
}

visibility_config {
cloudwatch_metrics_enabled = false
metric_name = "friendly-metric-name"
sampled_requests_enabled = false
}
}
`, name, fallbackBehavior, headerName)
}

func testAccAwsWafv2RuleGroupConfig_GeoMatchStatement_Update(name string) string {
return fmt.Sprintf(`
resource "aws_wafv2_rule_group" "test" {
Expand Down
18 changes: 11 additions & 7 deletions aws/resource_aws_wafv2_web_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,14 @@ func wafv2RateBasedStatementSchema(level int) *schema.Schema {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Required field but currently only supports "IP"
// Required field
"aggregate_key_type": {
Type: schema.TypeString,
Optional: true,
Default: wafv2.RateBasedStatementAggregateKeyTypeIp,
ValidateFunc: validation.StringInSlice([]string{
wafv2.RateBasedStatementAggregateKeyTypeIp,
}, false),
Type: schema.TypeString,
Optional: true,
Default: wafv2.RateBasedStatementAggregateKeyTypeIp,
ValidateFunc: validation.StringInSlice(wafv2.RateBasedStatementAggregateKeyType_Values(), false),
},
"forwarded_ip_config": wafv2ForwardedIPConfig(),
"limit": {
Type: schema.TypeInt,
Required: true,
Expand Down Expand Up @@ -627,6 +626,10 @@ func expandWafv2RateBasedStatement(l []interface{}) *wafv2.RateBasedStatement {
Limit: aws.Int64(int64(m["limit"].(int))),
}

if v, ok := m["forwarded_ip_config"]; ok {
r.ForwardedIPConfig = expandWafv2ForwardedIPConfig(v.([]interface{}))
}

s := m["scope_down_statement"].([]interface{})
if len(s) > 0 && s[0] != nil {
r.ScopeDownStatement = expandWafv2Statement(s[0].(map[string]interface{}))
Expand Down Expand Up @@ -819,6 +822,7 @@ func flattenWafv2RateBasedStatement(r *wafv2.RateBasedStatement) interface{} {
m := map[string]interface{}{
"limit": int(aws.Int64Value(r.Limit)),
"aggregate_key_type": aws.StringValue(r.AggregateKeyType),
"forwarded_ip_config": flattenWafv2ForwardedIPConfig(r.ForwardedIPConfig),
"scope_down_statement": nil,
}

Expand Down
Loading