Validate image name.
This commit is contained in:
parent
0533e1ad9c
commit
52f69cd91a
|
@ -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 != "" {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue