From 68614aeb79f6d90b9a9e8a1ea200ac2c72612476 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Thu, 18 Sep 2014 13:43:26 -0700 Subject: [PATCH] terraform: Guard against future version changes --- terraform/state.go | 6 ++++++ terraform/state_test.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/terraform/state.go b/terraform/state.go index 640f6d353..37845072f 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -533,6 +533,12 @@ func ReadState(src io.Reader) (*State, error) { return nil, fmt.Errorf("Decoding state file failed: %v", err) } + // Check the version, this to ensure we don't read a future + // version that we don't understand + if state.Version > textStateVersion { + return nil, fmt.Errorf("State version %d not supported, please update.", + state.Version) + } return state, nil } diff --git a/terraform/state_test.go b/terraform/state_test.go index 097abde91..3066f0b7d 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -2,7 +2,9 @@ package terraform import ( "bytes" + "encoding/json" "reflect" + "strings" "testing" "github.com/hashicorp/terraform/config" @@ -193,6 +195,25 @@ func TestReadWriteState(t *testing.T) { } } +func TestReadStateNewVersion(t *testing.T) { + type out struct { + Version int + } + + buf, err := json.Marshal(&out{textStateVersion + 1}) + if err != nil { + t.Fatalf("err: %v", err) + } + + s, err := ReadState(bytes.NewReader(buf)) + if s != nil { + t.Fatalf("unexpected: %#v", s) + } + if !strings.Contains(err.Error(), "not supported") { + t.Fatalf("err: %v", err) + } +} + func TestUpgradeV1State(t *testing.T) { old := &StateV1{ Outputs: map[string]string{