Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/backported/BUG FIXES-20250206-145217.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'backends: Fix crash when interrupting during interactive prompt for values'
time: 2025-02-06T14:52:17.033964+01:00
custom:
Issue: "36448"
7 changes: 7 additions & 0 deletions internal/backend/backendbase/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func (b Base) ConfigSchema() *configschema.Block {
func (b Base) PrepareConfig(configVal cty.Value) (cty.Value, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics

if configVal.IsNull() {
// We expect the backend configuration to be an object, so if it's
// null for some reason (e.g. because of an interrupt), we'll turn
// it into an empty object so that we can still coerce it
configVal = cty.EmptyObjectVal
}

schema := b.Schema

v, err := schema.CoerceValue(configVal)
Expand Down
38 changes: 38 additions & 0 deletions internal/backend/backendbase/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,41 @@ func TestBase_deprecatedArg(t *testing.T) {
}
})
}

func TestBase_nullCrash(t *testing.T) {
// This test ensures that we don't crash while applying defaults to
// a null value

b := Base{
Schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {
Type: cty.String,
Required: true,
},
},
},
SDKLikeDefaults: SDKLikeDefaults{
"foo": {
Fallback: "fallback",
},
},
}

t.Run("error", func(t *testing.T) {
// We pass an explicit null value here to simulate an interrupt
_, gotDiags := b.PrepareConfig(cty.NullVal(cty.Object(map[string]cty.Type{
"foo": cty.String,
})))
var wantDiags tfdiags.Diagnostics
wantDiags = wantDiags.Append(
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid backend configuration",
Detail: "The backend configuration is incorrect: attribute \"foo\" is required.",
})
if diff := cmp.Diff(wantDiags.ForRPC(), gotDiags.ForRPC()); diff != "" {
t.Errorf("wrong diagnostics\n%s", diff)
}
})
}