Validate image name.

This commit is contained in:
Matthew Hooker 2018-04-02 11:12:07 -07:00
parent 0533e1ad9c
commit 52f69cd91a
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
2 changed files with 21 additions and 10 deletions

View File

@ -97,8 +97,17 @@ func NewConfig(raws ...interface{}) (*Config, error) {
// Object names can contain only alphanumeric characters, hyphens, underscores, and periods
reValidObject := regexp.MustCompile("^[a-zA-Z0-9-._/]+$")
if !reValidObject.MatchString(c.DestImageList) {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("dest_image_list can contain only alphanumeric characters, hyphens, underscores, and periods."))
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 != "" {

View File

@ -72,14 +72,16 @@ func TestConfigValidatesObjects(t *testing.T) {
{"Matt...?", false},
{"/Config-thing/myuser/myimage", true},
}
for _, tt := range objectTests {
tc := testConfig()
tc["dest_image_list"] = tt.object
_, err := NewConfig(tc)
if tt.valid {
assert.NoError(t, err, tt.object)
} else {
assert.Error(t, err, tt.object)
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)
}
}
}
}