fix execution policy parser to not interfere with legit integers, and add tests (#8997)

This commit is contained in:
Megan Marsh 2020-04-07 01:32:58 -07:00 committed by GitHub
parent 1515875217
commit bdcc95f989
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 126 additions and 0 deletions

View File

@ -4,6 +4,7 @@ package powershell
import (
"reflect"
"strconv"
)
// ExecutionPolicy setting to run the command(s).
@ -27,5 +28,14 @@ func StringToExecutionPolicyHook(f reflect.Kind, t reflect.Kind, data interface{
}
raw := data.(string)
// It's possible that the thing being read is not supposed to be an
// execution policy; if the string provided is actally an int, just return
// the int.
i, err := strconv.Atoi(raw)
if err == nil {
return i, nil
}
// If it can't just be cast to an int, try to parse string into an
// execution policy.
return ExecutionPolicyString(raw)
}

View File

@ -188,6 +188,13 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
}
if p.config.ExecutionPolicy > 7 {
errs = packer.MultiErrorAppend(errs, fmt.Errorf(`Invalid execution `+
`policy provided. Please supply one of: "bypass", "allsigned",`+
` "default", "remotesigned", "restricted", "undefined", `+
`"unrestricted", "none".`))
}
if errs != nil {
return errs
}

View File

@ -3,6 +3,7 @@ package powershell
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"regexp"
@ -11,6 +12,7 @@ import (
"time"
"github.com/hashicorp/packer/packer"
"github.com/stretchr/testify/assert"
)
func testConfig() map[string]interface{} {
@ -564,6 +566,113 @@ func TestProvisioner_createFlattenedElevatedEnvVars_windows(t *testing.T) {
}
}
func TestProvisionerCorrectlyInterpolatesValidExitCodes(t *testing.T) {
type testCases struct {
Input interface{}
Expected []int
}
validExitCodeTests := []testCases{
{"0", []int{0}},
{[]string{"0"}, []int{0}},
{[]int{0, 12345}, []int{0, 12345}},
{[]string{"0", "12345"}, []int{0, 12345}},
{"0,12345", []int{0, 12345}},
}
for _, tc := range validExitCodeTests {
p := new(Provisioner)
config := testConfig()
config["valid_exit_codes"] = tc.Input
err := p.Prepare(config)
if err != nil {
t.Fatalf("Shouldn't have had error interpolating exit codes")
}
assert.ElementsMatchf(t, p.config.ValidExitCodes, tc.Expected,
fmt.Sprintf("expected exit codes to be: %#v, got %#v.", p.config.ValidExitCodes, tc.Expected))
}
}
func TestProvisionerCorrectlyInterpolatesExecutionPolicy(t *testing.T) {
type testCases struct {
Input interface{}
Expected ExecutionPolicy
ErrExpected bool
}
tests := []testCases{
{
Input: "bypass",
Expected: ExecutionPolicy(0),
ErrExpected: false,
},
{
Input: "allsigned",
Expected: ExecutionPolicy(1),
ErrExpected: false,
},
{
Input: "default",
Expected: ExecutionPolicy(2),
ErrExpected: false,
},
{
Input: "remotesigned",
Expected: ExecutionPolicy(3),
ErrExpected: false,
},
{
Input: "restricted",
Expected: ExecutionPolicy(4),
ErrExpected: false,
},
{
Input: "undefined",
Expected: ExecutionPolicy(5),
ErrExpected: false,
},
{
Input: "unrestricted",
Expected: ExecutionPolicy(6),
ErrExpected: false,
},
{
Input: "none",
Expected: ExecutionPolicy(7),
ErrExpected: false,
},
{
Input: "0", // User can supply a valid number for policy, too
Expected: 0,
ErrExpected: false,
},
{
Input: "invalid",
Expected: 0,
ErrExpected: true,
},
{
Input: "100", // If number is invalid policy, reject.
Expected: 100,
ErrExpected: true,
},
}
for _, tc := range tests {
p := new(Provisioner)
config := testConfig()
config["execution_policy"] = tc.Input
err := p.Prepare(config)
if (err != nil) != tc.ErrExpected {
t.Fatalf("Either err was expected, or shouldn't have happened: %#v", tc)
}
if err == nil {
assert.Equal(t, p.config.ExecutionPolicy, tc.Expected,
fmt.Sprintf("expected %#v, got %#v.", p.config.ExecutionPolicy, tc.Expected))
}
}
}
func TestProvisioner_createFlattenedEnvVars_windows(t *testing.T) {
var flattenedEnvVars string
config := testConfig()