Skip to content

Commit

Permalink
fix panic when import id is empty string
Browse files Browse the repository at this point in the history
We can only detect this during evaluation since the import id
is an hcl expression.

Fixes #33505
  • Loading branch information
DanielMSchmidt committed Feb 7, 2024
1 parent 64cd0da commit 2b7c7b2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/terraform/context_plan_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,30 @@ func TestContext2Plan_importIdInvalidNull(t *testing.T) {
}
}

func TestContext2Plan_importIdInvalidEmptyString(t *testing.T) {
p := testProvider("test")
m := testModule(t, "import-id-invalid-null")
ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})

_, diags := ctx.Plan(m, states.NewState(), &PlanOpts{
SetVariables: InputValues{
"the_id": &InputValue{
Value: cty.StringVal(""),
},
},
})
if !diags.HasErrors() {
t.Fatal("succeeded; want errors")
}
if got, want := diags.Err().Error(), "The import ID value evaluates to an empty string, please provide a non-empty value."; !strings.Contains(got, want) {
t.Fatalf("wrong error:\ngot: %s\nwant: message containing %q", got, want)
}
}

func TestContext2Plan_importIdInvalidUnknown(t *testing.T) {
p := testProvider("test")
m := testModule(t, "import-id-invalid-unknown")
Expand Down
9 changes: 9 additions & 0 deletions internal/terraform/eval_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func evaluateImportIdExpression(expr hcl.Expression, ctx EvalContext, keyData in
})
}

if importId == "" {
return "", diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid import id argument",
Detail: "The import ID value evaluates to an empty string, please provide a non-empty value.",
Subject: expr.Range().Ptr(),
})
}

return importId, diags
}

Expand Down

0 comments on commit 2b7c7b2

Please sign in to comment.