From 7c905c587f573e37b8ce1c5baf3a2f609a7fabe6 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Fri, 23 Oct 2020 15:57:43 -0400 Subject: [PATCH] builtin: Disable Terraform provider version checks The builtin Terraform provider's remote state data source uses a configured backend to fetch a given state, in order to allow access to its root module outputs. Until this commit, this was only possible with remote states which are from the current Terraform version or older, forcing multi-state users to carefully orchestrate Terraform upgrades. Building on previous commits in this branch, we now disable this version check, and allow any Terraform state file that the current Terraform version can parse. Since we are only ever accessing root module outputs, this is very likely to be safe for the foreseeable future. --- .../providers/terraform/data_source_state.go | 4 ++-- .../terraform/data_source_state_test.go | 20 +++++++++++++++++++ .../terraform/testdata/future.tfstate | 12 +++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 builtin/providers/terraform/testdata/future.tfstate diff --git a/builtin/providers/terraform/data_source_state.go b/builtin/providers/terraform/data_source_state.go index 3cec518ad58f..a2c776a22306 100644 --- a/builtin/providers/terraform/data_source_state.go +++ b/builtin/providers/terraform/data_source_state.go @@ -107,7 +107,7 @@ func dataSourceRemoteStateRead(d cty.Value) (cty.Value, tfdiags.Diagnostics) { workspaceName = workspaceVal.AsString() } - state, err := b.StateMgr(workspaceName) + state, err := b.StateMgrWithoutCheckVersion(workspaceName) if err != nil { diags = diags.Append(tfdiags.AttributeValue( tfdiags.Error, @@ -118,7 +118,7 @@ func dataSourceRemoteStateRead(d cty.Value) (cty.Value, tfdiags.Diagnostics) { return cty.NilVal, diags } - if err := state.RefreshState(); err != nil { + if err := state.RefreshStateWithoutCheckVersion(); err != nil { diags = diags.Append(err) return cty.NilVal, diags } diff --git a/builtin/providers/terraform/data_source_state_test.go b/builtin/providers/terraform/data_source_state_test.go index c557931ce01c..42465301b105 100644 --- a/builtin/providers/terraform/data_source_state_test.go +++ b/builtin/providers/terraform/data_source_state_test.go @@ -166,6 +166,26 @@ func TestState_basic(t *testing.T) { }), false, }, + "future version": { + cty.ObjectVal(map[string]cty.Value{ + "backend": cty.StringVal("local"), + "config": cty.ObjectVal(map[string]cty.Value{ + "path": cty.StringVal("./testdata/future.tfstate"), + }), + }), + cty.ObjectVal(map[string]cty.Value{ + "backend": cty.StringVal("local"), + "config": cty.ObjectVal(map[string]cty.Value{ + "path": cty.StringVal("./testdata/future.tfstate"), + }), + "outputs": cty.ObjectVal(map[string]cty.Value{ + "foo": cty.StringVal("bar"), + }), + "defaults": cty.NullVal(cty.DynamicPseudoType), + "workspace": cty.NullVal(cty.String), + }), + false, + }, "missing": { cty.ObjectVal(map[string]cty.Value{ "backend": cty.StringVal("local"), diff --git a/builtin/providers/terraform/testdata/future.tfstate b/builtin/providers/terraform/testdata/future.tfstate new file mode 100644 index 000000000000..0968eb8fc6d3 --- /dev/null +++ b/builtin/providers/terraform/testdata/future.tfstate @@ -0,0 +1,12 @@ +{ + "version": 4, + "terraform_version": "999.0.0", + "serial": 0, + "lineage": "", + "outputs": { + "foo": { + "value": "bar", + "type": "string" + } + } +}