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

Forcenew sensitive #394

Merged
merged 1 commit into from
Dec 7, 2021
Merged

Forcenew sensitive #394

merged 1 commit into from
Dec 7, 2021

Conversation

uturunku1
Copy link
Collaborator

@uturunku1 uturunku1 commented Dec 2, 2021

Description

This PR resolves this GitHub issue: #317

Replicate the Issue

  1. To replicate this issue, and prior to pulling changes from this branch, create a variable in your terraform config that has sensitive param set to true. For example:
resource "tfe_variable" "foobar" {
  key          = "some-key-name"
  value        = "some-value"
  description  = "some-description"
  category     = "terraform"
  workspace_id = "some-workspace-id"
  sensitive = true
}
  1. Run terraform apply to create the variable
  2. Then make a change to this variable configuration by changing sensitive = true to false
  3. Run terraform apply and terraform will request an update in place:

Screen Shot 2021-12-02 at 9 33 04 AM

  1. When the prompt asks "Do you want to perform these actions?", enter the value "yes"
  2. You should see this error pop up:

Screen Shot 2021-12-02 at 9 31 42 AM

Solution

We have previously used CustomizeDiff func to check if the value of the key parameter has changed:

CustomizeDiff: customdiff.ForceNewIf("key", func(_ context.Context, d *schema.ResourceDiff, m interface{}) bool {

If it has changed and the sensitive param was set to true, terraform forces a replacement, which means it will destroy the variable that owns that key first and then it will create a new resource using the diff info. This is accomplished with the forceNew method from the plugin SDK:

https://github.com/hashicorp/terraform-plugin-sdk/blob/56627b161e2cf04e9350e77fc9c303ecbae9c5bc/helper/schema/resource_diff.go#L340

Rather than terraform attempting to "update in place" when a sensitive is changed to false, seems more convenient to reuse this customizeDiff func to recreate this resource. Right now users who are changing sensitive to false are forced to manually delete their resource and add a new one anyways, so we might as well force the recreation for them.

Output from acceptance tests

To smoke test, pull the changes from this branch locally, and follow the instructions to build the provider in your dev environment: https://github.com/hashicorp/terraform-provider-tfe#manually-building-the-provider.

Cautious note: do not configure a remote execution mode when testing the changes because the TFC workers may attempt to use their own version of the provider instead of the one you built locally.

Now you can re-do the steps from the above section "Replicate the Issue" up to step number 4. Except that this time, after running terraform apply, you'll see a "force replacement" message instead of "update in-place":

Screen Shot 2021-12-02 at 10 04 09 AM

After you enter "yes" for the prompt, the recreation of the resource should succeed:

Screen Shot 2021-12-02 at 10 04 54 AM

Acceptance tests can be run locally:

$ TESTARGS="-run TestAccTFEWorkspace" make testacc 

...

Or just look at the output of this test in CircleCi :)

@uturunku1 uturunku1 marked this pull request as ready for review December 3, 2021 17:00
@uturunku1 uturunku1 requested a review from a team as a code owner December 3, 2021 17:00
@uturunku1 uturunku1 force-pushed the forcenew-sensitive branch 3 times, most recently from 5ea3829 to 74e5d7e Compare December 3, 2021 17:25

if wasSensitiveVar {
d.ForceNew("sensitive")
} else if d.Get("sensitive").(bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the check here be d.HasChange("key") && d.Get("sensitive").(bool) ? I'm basing this off the comment you left... feel free to clarify here.

Copy link
Collaborator Author

@uturunku1 uturunku1 Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sebasslash taking a look to https://github.com/hashicorp/terraform-plugin-sdk/blob/56627b161e2cf04e9350e77fc9c303ecbae9c5bc/helper/schema/resource_diff.go#L341, I see that ForceNew is also checking for d.HasChange("key"), which, at the beginning, I thought it would take care of that d.HasChange("key") check for me, but! Now I realize this would return an error for the poor customer, which is something I want to avoid. So, you are right, I should do that check myself too, before running d.ForceNew("key")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When ForceNew() is called and the key specified has not changed it will throw an error which is not something we want. However if this is intended behavior (which I doubt), we could add a comment then for readability, something like: // We don't explicitly check "key" param has changed because ForceNew() takes care of this check.

My money is on preventing this error from being thrown in the first place so I would go with the preemptive check.

wasSensitiveVar := d.HasChange("sensitive") && !(d.Get("sensitive").(bool))

if wasSensitiveVar {
d.ForceNew("sensitive")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should return d.ForceNew("sensitive") as that function returns an error and you want to capture that if it happens.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed!

Resolves #317
Allow CustomizeDiff to check when sensitive param changes from true to false in order to force the destruction and creating of a resource.
@uturunku1 uturunku1 merged commit 7917a53 into main Dec 7, 2021
@uturunku1 uturunku1 deleted the forcenew-sensitive branch December 7, 2021 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants