Merge pull request #6089 from hashicorp/oracle-classic-valdiation
Validate destination image name.
This commit is contained in:
commit
704cd85f51
|
@ -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 != "" {
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue