From e6cf6cd7580a52022f41295096b4a7ad71ab15f0 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Fri, 5 Jun 2020 09:11:44 -0400 Subject: [PATCH] backend/remote: do not panic if PrepareConfig or Configure receive null (#25135) * backend/remote: do not panic if PrepareConfig or Configure receive null objects If a user cancels (ctrl-c) terraform init while it is requesting missing configuration options for the remote backend, the PrepareConfig and Configure functions would receive a null cty.Value which would result in panics. This PR adds a check for null objects to the two functions in question. Fixes #23992 --- backend/remote/backend.go | 6 ++++++ backend/remote/backend_test.go | 3 +++ 2 files changed, 9 insertions(+) diff --git a/backend/remote/backend.go b/backend/remote/backend.go index a6b7051e87..c261f20220 100644 --- a/backend/remote/backend.go +++ b/backend/remote/backend.go @@ -142,6 +142,9 @@ func (b *Remote) ConfigSchema() *configschema.Block { // PrepareConfig implements backend.Backend. func (b *Remote) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) { var diags tfdiags.Diagnostics + if obj.IsNull() { + return obj, diags + } if val := obj.GetAttr("organization"); val.IsNull() || val.AsString() == "" { diags = diags.Append(tfdiags.AttributeValue( @@ -188,6 +191,9 @@ func (b *Remote) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) { // Configure implements backend.Enhanced. func (b *Remote) Configure(obj cty.Value) tfdiags.Diagnostics { var diags tfdiags.Diagnostics + if obj.IsNull() { + return diags + } // Get the hostname. if val := obj.GetAttr("hostname"); !val.IsNull() && val.AsString() != "" { diff --git a/backend/remote/backend_test.go b/backend/remote/backend_test.go index 053a1b35e7..8c1e9a80fc 100644 --- a/backend/remote/backend_test.go +++ b/backend/remote/backend_test.go @@ -123,6 +123,9 @@ func TestRemote_config(t *testing.T) { }), valErr: `Only one of workspace "name" or "prefix" is allowed`, }, + "null config": { + config: cty.NullVal(cty.EmptyObject), + }, } for name, tc := range cases {