Skip to content

Commit

Permalink
google_compute_security_policy: force send enforce_on_key so it can b…
Browse files Browse the repository at this point in the history
…e unset on (#7454) (#5326)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Mar 14, 2023
1 parent 371c678 commit 6b09248
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .changelog/7454.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: fixed bug where `enforce_on_key_name` could not be unset on `google_compute_security_policy`
```
16 changes: 8 additions & 8 deletions google-beta/resource_compute_security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,7 @@ func expandSecurityPolicyRuleRateLimitOptions(configured []interface{}) *compute
EnforceOnKeyConfigs: expandSecurityPolicyEnforceOnKeyConfigs(data["enforce_on_key_configs"].([]interface{})),
BanDurationSec: int64(data["ban_duration_sec"].(int)),
ExceedRedirectOptions: expandSecurityPolicyRuleRedirectOptions(data["exceed_redirect_options"].([]interface{})),
ForceSendFields: []string{"EnforceOnKey", "EnforceOnKeyName", "EnforceOnKeyConfigs"},
}
}

Expand Down Expand Up @@ -1247,14 +1248,13 @@ func flattenSecurityPolicyRuleRateLimitOptions(conf *compute.SecurityPolicyRuleR
}

data := map[string]interface{}{
"ban_threshold": flattenThreshold(conf.BanThreshold),
"rate_limit_threshold": flattenThreshold(conf.RateLimitThreshold),
"exceed_action": conf.ExceedAction,
"conform_action": conf.ConformAction,
"enforce_on_key": conf.EnforceOnKey,
"enforce_on_key_name": conf.EnforceOnKeyName,
"enforce_on_key_configs": flattenSecurityPolicyEnforceOnKeyConfigs(conf.EnforceOnKeyConfigs),

"ban_threshold": flattenThreshold(conf.BanThreshold),
"rate_limit_threshold": flattenThreshold(conf.RateLimitThreshold),
"exceed_action": conf.ExceedAction,
"conform_action": conf.ConformAction,
"enforce_on_key": conf.EnforceOnKey,
"enforce_on_key_name": conf.EnforceOnKeyName,
"enforce_on_key_configs": flattenSecurityPolicyEnforceOnKeyConfigs(conf.EnforceOnKeyConfigs),
"ban_duration_sec": conf.BanDurationSec,
"exceed_redirect_options": flattenSecurityPolicyRedirectOptions(conf.ExceedRedirectOptions),
}
Expand Down
127 changes: 125 additions & 2 deletions google-beta/resource_compute_security_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,52 @@ func TestAccComputeSecurityPolicy_withRateLimitOption_withMultipleEnforceOnKeyCo
})
}

func TestAccComputeSecurityPolicy_EnforceOnKeyUpdates(t *testing.T) {
t.Parallel()

spName := fmt.Sprintf("tf-test-%s", RandString(t, 10))

VcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: TestAccProviders,
CheckDestroy: testAccCheckComputeSecurityPolicyDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeSecurityPolicy_withRateLimitOptions_withEnforceOnKeyName(spName),
},
{
ResourceName: "google_compute_security_policy.policy",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeSecurityPolicy_withRateLimitOptions_withEnforceOnKey(spName),
},
{
ResourceName: "google_compute_security_policy.policy",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeSecurityPolicy_withRateLimitOptions_withEnforceOnKeyConfigs(spName),
},
{
ResourceName: "google_compute_security_policy.policy",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeSecurityPolicy_withRateLimitOptions_withEnforceOnKey(spName),
},
{
ResourceName: "google_compute_security_policy.policy",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccComputeSecurityPolicy_withRecaptchaOptionsConfig(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1166,12 +1212,89 @@ resource "google_compute_security_policy" "policy" {
`, spName)
}

func testAccComputeSecurityPolicy_withRateLimitOptions_withEnforceOnKey(spName string) string {
return fmt.Sprintf(`
resource "google_compute_security_policy" "policy" {
name = "%s"
description = "throttle rule with enforce_on_key_configs"
rule {
action = "throttle"
priority = "2147483647"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["*"]
}
}
description = "default rule"
rate_limit_options {
conform_action = "allow"
exceed_action = "redirect"
enforce_on_key = "IP"
exceed_redirect_options {
type = "EXTERNAL_302"
target = "https://www.example.com"
}
rate_limit_threshold {
count = 10
interval_sec = 60
}
}
}
}
`, spName)
}

func testAccComputeSecurityPolicy_withRateLimitOptions_withEnforceOnKeyName(spName string) string {
return fmt.Sprintf(`
resource "google_compute_security_policy" "policy" {
name = "%s"
description = "throttle rule with enforce_on_key_configs"
rule {
action = "throttle"
priority = "2147483647"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["*"]
}
}
description = "default rule"
rate_limit_options {
conform_action = "allow"
exceed_action = "redirect"
enforce_on_key = "HTTP_HEADER"
enforce_on_key_name = "user-agent"
exceed_redirect_options {
type = "EXTERNAL_302"
target = "https://www.example.com"
}
rate_limit_threshold {
count = 10
interval_sec = 60
}
}
}
}
`, spName)
}

func testAccComputeSecurityPolicy_withRateLimitOptions_withEnforceOnKeyConfigs(spName string) string {
return fmt.Sprintf(`
resource "google_compute_security_policy" "policy" {
name = "%s"
description = "throttle rule with enforce_on_key_configs"
rule {
action = "throttle"
priority = "2147483647"
Expand Down Expand Up @@ -1212,7 +1335,7 @@ func testAccComputeSecurityPolicy_withRateLimitOption_withMultipleEnforceOnKeyCo
resource "google_compute_security_policy" "policy" {
name = "%s"
description = "throttle rule with enforce_on_key_configs"
rule {
action = "throttle"
priority = "2147483647"
Expand Down

0 comments on commit 6b09248

Please sign in to comment.