Merge pull request #6089 from hashicorp/oracle-classic-valdiation

Validate destination image name.
This commit is contained in:
Matthew Hooker 2018-04-02 11:32:09 -07:00 committed by GitHub
commit 704cd85f51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"os" "os"
"regexp"
"time" "time"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
@ -94,6 +95,21 @@ func NewConfig(raws ...interface{}) (*Config, error) {
} }
} }
// Object names can contain only alphanumeric characters, hyphens, underscores, and periods
reValidObject := regexp.MustCompile("^[a-zA-Z0-9-._/]+$")
var objectValidation = []struct {
name string
value string
}{
{"dest_image_list", c.DestImageList},
{"image_name", c.ImageName},
}
for _, ov := range objectValidation {
if !reValidObject.MatchString(ov.value) {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("%s can contain only alphanumeric characters, hyphens, underscores, and periods.", ov.name))
}
}
if c.Attributes != "" && c.AttributesFile != "" { if c.Attributes != "" && c.AttributesFile != "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified.")) errs = packer.MultiErrorAppend(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
} else if c.AttributesFile != "" { } else if c.AttributesFile != "" {

View File

@ -2,6 +2,8 @@ package classic
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func testConfig() map[string]interface{} { func testConfig() map[string]interface{} {
@ -59,3 +61,27 @@ func TestValidationsIgnoresOptional(t *testing.T) {
t.Fatalf("Shouldn't care if ssh_username is missing: err: %#v", err.Error()) t.Fatalf("Shouldn't care if ssh_username is missing: err: %#v", err.Error())
} }
} }
func TestConfigValidatesObjects(t *testing.T) {
var objectTests = []struct {
object string
valid bool
}{
{"foo-BAR.0_9", true},
{"%", false},
{"Matt...?", false},
{"/Config-thing/myuser/myimage", true},
}
for _, s := range []string{"dest_image_list", "image_name"} {
for _, tt := range objectTests {
tc := testConfig()
tc[s] = tt.object
_, err := NewConfig(tc)
if tt.valid {
assert.NoError(t, err, tt.object)
} else {
assert.Error(t, err, tt.object)
}
}
}
}