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

Backport of backend: Validate remote backend Terraform version into v0.12 #27002

Conversation

alisdair
Copy link
Contributor

Backport

This PR is a manual backport of #26947 targeted at the 0.12 branch for future release, likely in 0.12.30.

The below text is copied from the body of the original PR.


When using the enhanced remote backend, a subset of all Terraform operations are supported. Of these, only plan and apply can be executed on the remote infrastructure (e.g. Terraform Cloud). Other operations run locally and use the remote backend for state storage.

This causes problems when the local version of Terraform does not match the configured version from the remote workspace. If the two versions are incompatible, an import or state mv operation can cause the remote workspace to be unusable until a manual fix is applied.

To prevent this from happening accidentally, this commit introduces a check that the local Terraform version and the configured remote workspace Terraform version are compatible. This check is skipped for commands which do not write state, and can also be disabled by the use of a new command-line flag, -ignore-remote-version.

Terraform version compatibility is defined as:

  • For all releases before 0.14.0, local must exactly equal remote, as two different versions cannot share state;
  • 0.14.0 to 1.0.x are compatible, as we will not change the state version number until at least Terraform 1.1.0;
  • Versions after 1.1.0 must have the same major and minor versions, as we will not change the state version number in a patch release.

If the two versions are incompatible, a diagnostic is displayed, advising that the error can be suppressed with -ignore-remote-version. When this flag is used, the diagnostic is still displayed, but as a warning instead of an error.

Commands which will not write state can assert this fact by calling the helper meta.ignoreRemoteBackendVersionConflict, which will disable the checks. Those which can write state should instead call the helper meta.remoteBackendVersionCheck, which will return diagnostics for display.

In addition to these explicit paths for managing the version check, we have an implicit check in the remote backend's state manager initialization method. Both of the above helpers will disable this check. This fallback is in place to ensure that future code paths which access state cannot accidentally skip the remote version check.

Release plans

This change is intended to ship with 0.15.0, and we also intend to backport to earlier release series. The current plan is 0.14, 0.13, and 0.12.

Screenshot

Here's a session which demonstrates both the error and warning forms of the diagnostics:

remote-version-check

When using the enhanced remote backend, a subset of all Terraform
operations are supported. Of these, only plan and apply can be executed
on the remote infrastructure (e.g. Terraform Cloud). Other operations
run locally and use the remote backend for state storage.

This causes problems when the local version of Terraform does not match
the configured version from the remote workspace. If the two versions
are incompatible, an `import` or `state mv` operation can cause the
remote workspace to be unusable until a manual fix is applied.

To prevent this from happening accidentally, this commit introduces a
check that the local Terraform version and the configured remote
workspace Terraform version are compatible. This check is skipped for
commands which do not write state, and can also be disabled by the use
of a new command-line flag, `-ignore-remote-version`.

Terraform version compatibility is defined as:

- For all releases before 0.14.0, local must exactly equal remote, as
  two different versions cannot share state;
- 0.14.0 to 1.0.x are compatible, as we will not change the state
  version number until at least Terraform 1.1.0;
- Versions after 1.1.0 must have the same major and minor versions, as
  we will not change the state version number in a patch release.

If the two versions are incompatible, a diagnostic is displayed,
advising that the error can be suppressed with `-ignore-remote-version`.
When this flag is used, the diagnostic is still displayed, but as a
warning instead of an error.

Commands which will not write state can assert this fact by calling the
helper `meta.ignoreRemoteBackendVersionConflict`, which will disable the
checks. Those which can write state should instead call the helper
`meta.remoteBackendVersionCheck`, which will return diagnostics for
display.

In addition to these explicit paths for managing the version check, we
have an implicit check in the remote backend's state manager
initialization method. Both of the above helpers will disable this
check. This fallback is in place to ensure that future code paths which
access state cannot accidentally skip the remote version check.
@alisdair alisdair self-assigned this Nov 20, 2020
@codecov
Copy link

codecov bot commented Nov 20, 2020

Codecov Report

Merging #27002 (a9063ef) into v0.12 (c5c0268) will decrease coverage by 1.19%.
The diff coverage is 55.83%.

Impacted Files Coverage Δ
command/state_mv.go 42.12% <4.34%> (-1.84%) ⬇️
command/state_rm.go 43.15% <8.33%> (-2.21%) ⬇️
command/untaint.go 37.58% <17.39%> (-0.35%) ⬇️
command/meta_backend.go 53.96% <22.22%> (-1.27%) ⬇️
command/taint.go 38.35% <27.27%> (-0.38%) ⬇️
command/import.go 50.42% <44.44%> (-0.45%) ⬇️
command/state_meta.go 46.15% <50.00%> (-2.52%) ⬇️
command/state_push.go 35.36% <71.42%> (+0.79%) ⬆️
backend/remote/backend_mock.go 62.57% <72.72%> (+0.02%) ⬆️
backend/remote/backend.go 61.70% <100.00%> (+3.68%) ⬆️
... and 669 more

@alisdair alisdair merged commit 4652139 into v0.12 Dec 8, 2020
@alisdair alisdair deleted the backport/alisdair/backend-validate-remote-backend-terraform-version/pleasantly-engaged-ringtail branch December 8, 2020 15:05
@ghost
Copy link

ghost commented Jan 8, 2021

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked as resolved and limited conversation to collaborators Jan 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant