-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hcl2: fix crash on malformed overrides
When an override block is either not an object, or if one of its contents are not an object, Packer would crash trying to forcefully cast the cty.Value to a cty.Collection. To avoid this behaviour, we add extra checks that return hcl.Diagnostics when they fail.
- Loading branch information
1 parent
9f28ced
commit a8e06e1
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
provisioner "shell-local" { | ||
inline = ["echo 'hi'"] | ||
override = "hello" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
provisioner "shell-local" { | ||
inline = ["echo 'hi'"] | ||
override = { | ||
test = "hello" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package hcl2template | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
) | ||
|
||
func TestPackerConfig_ParseProvisionerBlock(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputFile string | ||
expectError bool | ||
}{ | ||
{ | ||
"success - provisioner is valid", | ||
"fixtures/well_formed_provisioner.pkr.hcl", | ||
false, | ||
}, | ||
{ | ||
"failure - provisioner override is malformed", | ||
"fixtures/malformed_override.pkr.hcl", | ||
true, | ||
}, | ||
{ | ||
"failure - provisioner override.test is malformed", | ||
"fixtures/malformed_override_innards.pkr.hcl", | ||
true, | ||
}, | ||
} | ||
|
||
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() == test.expectError { | ||
return | ||
} | ||
|
||
if !diags.HasErrors() { | ||
t.Fatalf("unexpected successful parsing of provisioner") | ||
} | ||
|
||
for _, d := range diags { | ||
t.Errorf("unexpected error: %s", d) | ||
} | ||
}) | ||
} | ||
} |