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

Consul Provider token rotation issue #298

Closed
askmike1 opened this issue Mar 8, 2022 · 1 comment · Fixed by #321
Closed

Consul Provider token rotation issue #298

askmike1 opened this issue Mar 8, 2022 · 1 comment · Fixed by #321
Assignees

Comments

@askmike1
Copy link

askmike1 commented Mar 8, 2022

Terraform Version

Tested with 0.12.31 & 1.1.7

Affected Resource(s)

  • consul_key_prefix

Terraform Configuration Files

provider "consul" {
  version = "~> 2.14"
  address    = "https://<consul_url>"
  datacenter = "<consul_datacenter>"
}

resource "consul_key_prefix" "subnet_nums" {
  token = "<token>"
  path_prefix = "terraform/path/path/"
  subkey {
    path  = "nums"
    value  = "val"
  }
}

Debug Output

N/A

Panic Output

N/A

Expected Behavior

When token changes it should pick up the new one

Actual Behavior

It tries to use whatever token is in the state file and thus fails as it no longer exists

Steps to Reproduce

  1. terraform init
  2. terraform plan
  3. terraform apply
  4. Delete old consul token and replace with new one (alternatively, just update the state file to change the token to some bogus value)
  5. terraform plan
    Output:
Error: Failed to list Consul keys under prefix 'terraform/path/path/': Unexpected response code: 403

Important Factoids

N/A

References

N/A

@remilapeyre
Copy link
Contributor

Hi @askmike1, thanks for opening this issue.

Sadly the issue here lies in how Terraform interact with providers, when reading a resource the provider only gets the current state as input, not the current user-configuration: https://github.com/hashicorp/terraform/blob/e6dbb7faf0407f6f98a451bbe8b0eb5fc160b4ec/docs/plugin-protocol/tfplugin6.2.proto#L249-L254.

The token argument in the consul_key_prefix resource has been here for 8 years, before this kind of issue was well understood.

The best way forward is to use the token argument in the provider configuration: https://registry.terraform.io/providers/hashicorp/consul/latest/docs#token, that way the client will always use the token you are expecting.

I will probably mark the token attributes and other similar attributes as deprecated and remove them in a major release, they have too much drawbacks and complicated needlessly the design of the provider.

@remilapeyre remilapeyre self-assigned this Aug 28, 2022
remilapeyre added a commit to remilapeyre/terraform-provider-consul that referenced this issue Aug 28, 2022
Having the token attribute in the resources causes issues: when a user
updates it the previous value is used when calling the `Read()` method.

This is expected based on the protocol Terraform Core uses and there is
no way to have the correct behavior here. To fix this I mark the "token"
attribute as deprecated so that users only set the one in the "provider"
block.

I also mark all "datacenter", "namespace" and "partition" attribute as
ForceNew to avoid the same issue with those.

Closes hashicorp#298
remilapeyre added a commit that referenced this issue Dec 12, 2022
Having the token attribute in the resources causes issues: when a user
updates it the previous value is used when calling the `Read()` method.

This is expected based on the protocol Terraform Core uses and there is
no way to have the correct behavior here. To fix this I mark the "token"
attribute as deprecated so that users only set the one in the "provider"
block.

I also mark all "datacenter", "namespace" and "partition" attribute as
ForceNew to avoid the same issue with those.

Closes #298
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants