cloudflare / terraform-provider-cloudflare

Cloudflare Terraform Provider
https://registry.terraform.io/providers/cloudflare/cloudflare
Mozilla Public License 2.0
736 stars 555 forks source link

Perpetual drift from Access mTLS cert associated hostname reordering #3436

Open bporter816 opened 6 days ago

bporter816 commented 6 days ago

Confirmation

Terraform and Cloudflare provider version

Terraform v1.9.1 on darwin_amd64

Affected resource(s)

Terraform configuration files

# Configure the Cloudflare provider using the required_providers stanza
# required with Terraform 0.13 and beyond. You may optionally use version
# directive to prevent breaking changes occurring unannounced.
terraform {
  required_providers {
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "~> 4.0"
    }
  }
}

variable "account_id" {
  type    = string
  default = "<account id>"
}

variable "certificate" {
  type    = string
  default = <<EOF
<self signed cert generated with openssl>
  EOF
}

resource "cloudflare_access_mutual_tls_certificate" "test" {
  name        = "test"
  account_id  = var.account_id
  certificate = var.certificate
  associated_hostnames = [
    "<hostname 1>",
    "<hostname 2>",
  ]
}

Link to debug output

https://gist.github.com/bporter816/a339f01e2c2ea6107279e589bb5d21b5

Panic output

No response

Expected output

When the ordering of associated hostnames returned from the Cloudflare API differs from the ordering in Terraform, we expect one of two outcomes:

  1. Applying Terraform (or doing a PUT against the API) with the same list of hostnames in a different order should actually reorder them on the server side so that future runs don't produce any drift.
  2. Terraform should not care about the order of the returned hostnames and report no drift if the order differs from what the API returns.

Actual output

If the ordering differs, we get a permanent drift, like so:

Terraform will perform the following actions:

  # cloudflare_access_mutual_tls_certificate.test will be updated in-place
  ~ resource "cloudflare_access_mutual_tls_certificate" "test" {
      ~ associated_hostnames = [
          - "<hostname 1>",
            "<hostname 2>",
          + "<hostname 1>",
        ]
        id                   = "<id>"
        name                 = "test"
        # (3 unchanged attributes hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Both applying this change through Terraform and directly through the Cloudflare API does not actually reflect the new ordering on the server side, and the drift continues to show up in subsequent runs. We could work around this if the ordering was deterministic and something we could account for in our Terraform config (if it is, please advise and we can take that approach) but we notice that even for the same list of hostnames, they are returned in a different order on different certs.

Steps to reproduce

  1. Create an Access mTLS cert with a multiple associated hostnames
  2. Change the ordering of the hostnames in the Terraform configuration to a different order than the Cloudflare API returns them in
  3. This results in drift which will show up in subsequent plan/apply and will not go away upon successful apply

Additional factoids

No response

References

No response

github-actions[bot] commented 6 days ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue