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

Whitespace changed causes recreation of the ruleset rules #4767

Closed
3 tasks done
Dragotic opened this issue Dec 13, 2024 · 4 comments
Closed
3 tasks done

Whitespace changed causes recreation of the ruleset rules #4767

Dragotic opened this issue Dec 13, 2024 · 4 comments
Labels
kind/support Categorizes issue or PR as related to user support.

Comments

@Dragotic
Copy link

Dragotic commented Dec 13, 2024

Confirmation

  • This is a bug with an existing resource and is not a feature request or enhancement. Feature requests should be submitted with Cloudflare Support or your account team.
  • I have searched the issue tracker and my issue isn't already found.
  • I have replicated my issue using the latest version of the provider and it is still present.

Terraform and Cloudflare provider version

Terraform v1.6.6
on darwin_arm64
+ provider registry.terraform.io/cloudflare/cloudflare v4.48.0

Affected resource(s)

  • cloudflare_ruleset

Terraform configuration files

# From cf-terraforming generate --resource-type cloudflare_ruleset
resource "cloudflare_ruleset" "ratelimit_ruleset" {
  kind    = "zone"
  name    = "default"
  phase   = "http_ratelimit"
  zone_id = local.zone_id
  rules {
    action = "block"
    action_parameters {
      response {
        content      = "{\"errors\":[{\"code\":\"too_many_requests\",\"message\":\"Too many requests. Please try again in a bit.\"}]}\"
        content_type = "application/json"
        status_code  = 429
      }
    }
    description = "Containing /prepare_verification (3req/10s)"
    enabled     = true
    expression  = "(http.request.uri.path contains \"/something\" and http.request.method in {\"POST\" \"PUT\" \"PATCH\"})"
    ratelimit {
      characteristics     = ["ip.src", "cf.colo.id"]
      mitigation_timeout  = 600
      period              = 10
      requests_per_period = 3
    }
  }
}

# After importing the resource and making the config more readable
locals {
  error_message = jsonencode({
    errors = [{ 
      message = "Too many requests. Please try again in a bit.",
      code    = "too_many_requests" 
    }]
  })
}

resource "cloudflare_ruleset" "ratelimit_ruleset" {
  kind    = "zone"
  name    = "default"
  phase   = "http_ratelimit"
  zone_id = local.zone_id
  rules {
    action = "block"
    action_parameters {
      response {
        content      = local.error_message
        content_type = "application/json"
        status_code  = 429
      }
    }
    description = "Containing /prepare_verification (3req/10s)"
    enabled     = true
    expression  = "(http.request.uri.path contains \"/something\" and http.request.method in {\"POST\" \"PUT\" \"PATCH\"})"
    ratelimit {
      characteristics     = ["ip.src", "cf.colo.id"]
      mitigation_timeout  = 600
      period              = 10
      requests_per_period = 3
    }
  }
}

Link to debug output

https://gist.github.com/Dragotic/90193d616755c15356f079fbe3431fcf

Panic output

No response

Expected output

No changes. Your infrastructure matches the configuration.

Actual output

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # cloudflare_ruleset.ratelimit_ruleset will be updated in-place
  ~ resource "cloudflare_ruleset" "ratelimit_ruleset" {
        id      = "................00e"
        name    = "default"
        # (3 unchanged attributes hidden)

      ~ rules {
          ~ id          = "........4fb" -> (known after apply)
          ~ ref         = ".......4fb" -> (known after apply)
            # (4 unchanged attributes hidden)

          ~ action_parameters {
              ~ response {
                  ~ content      = jsonencode( # whitespace changes
                        {
                            errors = [
                                {
                                    code    = "too_many_requests"
                                    message = "Too many requests. Please try again in a bit."
                                },
                            ]
                        }
                    )
                    # (2 unchanged attributes hidden)
                }
            }

            # (1 unchanged block hidden)
        }
}
Plan: 0 to add, 1 to change, 0 to destroy.

After typing yes on the terraform apply the rules are recreated with new IDs

Steps to reproduce

  1. import cloudflare_ruleset by using `cf-terraforming generate --email $CLOUDFLARE_EMAIL --token $CLOUDFLARE_API_TOKEN --resource-type cloudflare_ruleset --zone $CLOUDFLARE_ZONE_ID
  2. Change the response message to a terraform map with jsonencode() function before passing it to the response of the cloudflare_ruleset resource
  3. terraform apply
  4. Changes are shown
  5. apply the changes
  6. rules are recreated

Additional factoids

It's not only that whitespace is recognised as change by the cloudflare API, but it also recreates the rules when applied. Effectively losing activity for last 24 hours

References

No response

@Dragotic Dragotic added kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Dec 13, 2024
Copy link
Contributor

github-actions bot commented Dec 13, 2024

Terraform debug log detected ✅

@github-actions github-actions bot added triage/needs-information Indicates an issue needs more information in order to work on it. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Dec 13, 2024
Copy link
Contributor

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@github-actions github-actions bot added triage/debug-log-attached Indicates an issue or PR has a complete Terraform debug log. and removed triage/needs-information Indicates an issue needs more information in order to work on it. labels Dec 13, 2024
@jacobbednarz
Copy link
Member

jsonencode and the string there are not equivalent so the diff here makes sense. you can confirm the difference by expecting the state or running it through terraform console. if you don't want the diff, you should adjust the value or formatting accordingly.

if you define the ref field, the analytics UI keeps track of the changes. see the release notes for the latest updates and guidance on that.

@jacobbednarz jacobbednarz closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2024
@jacobbednarz jacobbednarz added kind/support Categorizes issue or PR as related to user support. and removed kind/bug Categorizes issue or PR as related to a bug. triage/debug-log-attached Indicates an issue or PR has a complete Terraform debug log. labels Dec 13, 2024
@Dragotic
Copy link
Author

@jacobbednarz Thanks for the quick reply. I didn't catch the ref fix on the v4.48 version, thanks for pointing it out.

Do the whitespace changes cause any other side-effect on Cloudflare API apart from the look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Categorizes issue or PR as related to user support.
Projects
None yet
Development

No branches or pull requests

2 participants