Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hcl2: fix crash on malformed overrides #11881

Merged
merged 1 commit into from
Jul 20, 2022
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
4 changes: 4 additions & 0 deletions hcl2template/fixtures/malformed_override.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provisioner "shell-local" {
inline = ["echo 'hi'"]
override = "hello"
}
6 changes: 6 additions & 0 deletions hcl2template/fixtures/malformed_override_innards.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
provisioner "shell-local" {
inline = ["echo 'hi'"]
override = {
test = "hello"
}
}
9 changes: 9 additions & 0 deletions hcl2template/fixtures/well_formed_provisioner.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
provisioner "shell-local" {
inline = ["echo 'hi'"]
override = {
test = {
"hello" = "new value"
}
}
}

19 changes: 19 additions & 0 deletions hcl2template/types.build.provisioners.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,28 @@ func (p *Parser) decodeProvisioner(block *hcl.Block, ectx *hcl.EvalContext) (*Pr
}

if !b.Override.IsNull() {
if !b.Override.Type().IsObjectType() {
return nil, append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "provisioner's override block must be an HCL object",
Subject: block.DefRange.Ptr(),
})
}

override := make(map[string]interface{})
for buildName, overrides := range b.Override.AsValueMap() {
buildOverrides := make(map[string]interface{})

if !overrides.Type().IsObjectType() {
return nil, append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf(
"provisioner's override.'%s' block must be an HCL object",
buildName),
Subject: block.DefRange.Ptr(),
})
}

for option, value := range overrides.AsValueMap() {
buildOverrides[option] = hcl2shim.ConfigValueFromHCL2(value)
}
Expand Down
82 changes: 82 additions & 0 deletions hcl2template/types.build.provisioners_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package hcl2template

import (
"testing"

"github.com/hashicorp/hcl/v2"
)

func TestPackerConfig_ParseProvisionerBlock(t *testing.T) {
tests := []struct {
name string
inputFile string
expectError bool
expectedErrorMessage string
}{
{
"success - provisioner is valid",
"fixtures/well_formed_provisioner.pkr.hcl",
false,
"",
},
{
"failure - provisioner override is malformed",
"fixtures/malformed_override.pkr.hcl",
true,
"provisioner's override block must be an HCL object",
},
{
"failure - provisioner override.test is malformed",
"fixtures/malformed_override_innards.pkr.hcl",
true,
"provisioner's override.'test' block must be an HCL object",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfg := PackerConfig{parser: getBasicParser()}
f, diags := cfg.parser.ParseHCLFile(test.inputFile)
if diags.HasErrors() {
t.Errorf("failed to parse input file %s", test.inputFile)
for _, d := range diags {
t.Errorf("%s", d)
}
return
}
provBlock := f.OutermostBlockAtPos(hcl.Pos{
Line: 1,
Column: 1,
Byte: 0,
})
_, diags = cfg.parser.decodeProvisioner(provBlock, nil)

if !diags.HasErrors() {
if !test.expectError {
return
}

t.Fatalf("unexpected success")
}

if !test.expectError {
for _, d := range diags {
t.Errorf("%s", d)
}
}

gotExpectedErr := false
for _, d := range diags {
if d.Summary == test.expectedErrorMessage {
gotExpectedErr = true
}

t.Logf("got error (expected): '%s'", d.Summary)
}

if !gotExpectedErr {
t.Errorf("never got expected error: '%s'", test.expectedErrorMessage)
}
})
}
}