Skip to content

Commit

Permalink
Fixed recaptcha_options permadiff causing rules being recreated (#12458
Browse files Browse the repository at this point in the history
…) (#20617)

[upstream:b0b7f57d696854f3025089d6866a2c8a385c5536]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Dec 6, 2024
1 parent 0ee47a8 commit f8b70a6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .changelog/12458.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: fixed permadiff on the `recaptcha_options` field for `google_compute_security_policy` resource
```
54 changes: 34 additions & 20 deletions google/services/compute/resource_compute_security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ import (
"google.golang.org/api/compute/v1"
)

func verifyRulePriorityCompareEmptyValues(d *schema.ResourceData, rulePriority int, schemaKey string) bool {
if schemaRules, ok := d.GetOk("rule"); ok {
for _, itemRaw := range schemaRules.(*schema.Set).List() {
if itemRaw == nil {
continue
}
item := itemRaw.(map[string]interface{})

schemaPriority := item["priority"].(int)
if rulePriority == schemaPriority {
if tpgresource.IsEmptyValue(reflect.ValueOf(item[schemaKey])) {
return true
}
break
}
}
}
return false
}

// IsEmptyValue does not consider a empty PreconfiguredWafConfig object as empty so we check it's nested values
func preconfiguredWafConfigIsEmptyValue(config *compute.SecurityPolicyRulePreconfiguredWafConfig) bool {
if tpgresource.IsEmptyValue(reflect.ValueOf(config.Exclusions)) &&
Expand Down Expand Up @@ -1155,7 +1175,7 @@ func flattenSecurityPolicyRules(rules []*compute.SecurityPolicyRule, d *schema.R
"priority": rule.Priority,
"action": rule.Action,
"preview": rule.Preview,
"match": flattenMatch(rule.Match),
"match": flattenMatch(rule.Match, d, int(rule.Priority)),
"preconfigured_waf_config": flattenPreconfiguredWafConfig(rule.PreconfiguredWafConfig, d, int(rule.Priority)),
"rate_limit_options": flattenSecurityPolicyRuleRateLimitOptions(rule.RateLimitOptions),
"redirect_options": flattenSecurityPolicyRedirectOptions(rule.RedirectOptions),
Expand All @@ -1166,7 +1186,7 @@ func flattenSecurityPolicyRules(rules []*compute.SecurityPolicyRule, d *schema.R
return rulesSchema
}

func flattenMatch(match *compute.SecurityPolicyRuleMatcher) []map[string]interface{} {
func flattenMatch(match *compute.SecurityPolicyRuleMatcher, d *schema.ResourceData, rulePriority int) []map[string]interface{} {
if match == nil {
return nil
}
Expand All @@ -1175,7 +1195,7 @@ func flattenMatch(match *compute.SecurityPolicyRuleMatcher) []map[string]interfa
"versioned_expr": match.VersionedExpr,
"config": flattenMatchConfig(match.Config),
"expr": flattenMatchExpr(match),
"expr_options": flattenMatchExprOptions(match.ExprOptions),
"expr_options": flattenMatchExprOptions(match.ExprOptions, d, rulePriority),
}

return []map[string]interface{}{data}
Expand All @@ -1193,11 +1213,18 @@ func flattenMatchConfig(conf *compute.SecurityPolicyRuleMatcherConfig) []map[str
return []map[string]interface{}{data}
}

func flattenMatchExprOptions(exprOptions *compute.SecurityPolicyRuleMatcherExprOptions) []map[string]interface{} {
func flattenMatchExprOptions(exprOptions *compute.SecurityPolicyRuleMatcherExprOptions, d *schema.ResourceData, rulePriority int) []map[string]interface{} {
if exprOptions == nil {
return nil
}

// We check if the API is returning a empty non-null value then we find the current value for this field in the rule config and check if its empty
if (tpgresource.IsEmptyValue(reflect.ValueOf(exprOptions.RecaptchaOptions.ActionTokenSiteKeys)) &&
tpgresource.IsEmptyValue(reflect.ValueOf(exprOptions.RecaptchaOptions.SessionTokenSiteKeys))) &&
verifyRulePriorityCompareEmptyValues(d, rulePriority, "recaptcha_options") {
return nil
}

data := map[string]interface{}{
"recaptcha_options": flattenMatchExprOptionsRecaptchaOptions(exprOptions.RecaptchaOptions),
}
Expand Down Expand Up @@ -1239,22 +1266,9 @@ func flattenPreconfiguredWafConfig(config *compute.SecurityPolicyRulePreconfigur
return nil
}

// We find the current value for this field in the config and check if its empty, then check if the API is returning a empty non-null value
if schemaRules, ok := d.GetOk("rule"); ok {
for _, itemRaw := range schemaRules.(*schema.Set).List() {
if itemRaw == nil {
continue
}
item := itemRaw.(map[string]interface{})

schemaPriority := item["priority"].(int)
if rulePriority == schemaPriority {
if preconfiguredWafConfigIsEmptyValue(config) && tpgresource.IsEmptyValue(reflect.ValueOf(item["preconfigured_waf_config"])) {
return nil
}
break
}
}
// We check if the API is returning a empty non-null value then we find the current value for this field in the rule config and check if its empty
if preconfiguredWafConfigIsEmptyValue(config) && verifyRulePriorityCompareEmptyValues(d, rulePriority, "preconfigured_waf_config") {
return nil
}

data := map[string]interface{}{
Expand Down

0 comments on commit f8b70a6

Please sign in to comment.