fix: Fail an apply command if the plan file was generated for a workspace that isn't the selected workspace
#37955
+196
−24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #37954
TODO:
This PR
This PR adds an explicit error if the currently selected workspace doesn't match the workspace a plan file was generated against:
We might need slightly different wording based on whether a user is using a cloud block or a backend block, as
workspacecommands aren't supported with HCPTF.Has this been implemented and removed in the past?
I took a look at the git blame data around the
BackendForLocalPlan(originally "BackendForPlan") method and it doesn't look like a comparison was even implemented and then removed. The closest thing in the blame data related to workspace names was #25262, but this just checks that the current workspace name is valid. It doesn't include comparison to the value in the plan file.Does this affect the
cloudbackend?Conclusion: No
When using the
cloudbackendworkspaceCLI commands are blocked because HCP Terraform doesn't support CE workspace features. The value in the.terraform/environmentfile is controlled by theinitcommand only; when the user changes theworkspacevalue in thecloudblock and runs aninitcommand the selected workspace is updated in that file.Using HCP Terraform in
Localexecution modeterraform initsets the selected workspace (i.e. the.terraform/environmentfile) to match the HCPTF workspace.,terraform plan -out=tfplanmakes a local plan file that contains the HCPTF workspace in use, i.e. matching what's in thecloudblock in the config.applycompletes as expected.I did find that if you change the
workspacevalue in the configuration after making a plan, from a user perspective they might think that they're applying the plan against workspace B when the plan (and working directory) describe using workspace A. This is because when a plan file is used Terraform doesn't check if thecloudblock matches the backend state file, because the plan file is used as the only source of config for configuring the backend. If that counts as a bug, it feels sufficiently different from #37954 to not address in this PR.Using HCP Terraform in
Remoteexecution modeIn this execution mode if you run
terraform plan -out=tfplanthe local plan file doesn't contain workspace data and instead references a specific run in HCPTF that has produced a plan remotely. If a user changes theworkspacevalue in the config between theplanandapplycommands Terraform does detect that change and prompts the user to run an init command. Due to this, there doesn't seem to be a scenario in Remote execution where a user can accidentally change what a preexisting plan is applied to.Target Release
1.15.x
Rollback Plan
Changes to Security Controls
Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.
CHANGELOG entry