Commit e39965e3 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware: validate iso_url more strictly

parent c98c16a5
...@@ -8,8 +8,10 @@ import ( ...@@ -8,8 +8,10 @@ import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
"math/rand" "math/rand"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"time" "time"
) )
...@@ -93,6 +95,36 @@ func (b *Builder) Prepare(raw interface{}) (err error) { ...@@ -93,6 +95,36 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
if b.config.ISOUrl == "" { if b.config.ISOUrl == "" {
errs = append(errs, errors.New("An iso_url must be specified.")) errs = append(errs, errors.New("An iso_url must be specified."))
} else {
url, err := url.Parse(b.config.ISOUrl)
if err != nil {
errs = append(errs, fmt.Errorf("iso_url is not a valid URL: %s", err))
} else {
if url.Scheme == "" {
url.Scheme = "file"
}
if url.Scheme == "file" {
if _, err := os.Stat(b.config.ISOUrl); err != nil {
errs = append(errs, fmt.Errorf("iso_url points to bad file: %s", err))
}
} else {
supportedSchemes := []string{"file", "http", "https"}
scheme := strings.ToLower(url.Scheme)
found := false
for _, supported := range supportedSchemes {
if scheme == supported {
found = true
break
}
}
if !found {
errs = append(errs, fmt.Errorf("Unsupported URL scheme in iso_url: %s", scheme))
}
}
}
} }
if b.config.SSHUser == "" { if b.config.SSHUser == "" {
......
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
func testConfig() map[string]interface{} { func testConfig() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"iso_url": "foo", "iso_url": "http://www.packer.io",
"ssh_username": "foo", "ssh_username": "foo",
} }
} }
...@@ -104,10 +104,22 @@ func TestBuilderPrepare_ISOUrl(t *testing.T) { ...@@ -104,10 +104,22 @@ func TestBuilderPrepare_ISOUrl(t *testing.T) {
t.Fatal("should have error") t.Fatal("should have error")
} }
config["iso_url"] = "exists" config["iso_url"] = "i/am/a/file/that/doesnt/exist"
err = b.Prepare(config)
if err == nil {
t.Error("should have error")
}
config["iso_url"] = "file:i/am/a/file/that/doesnt/exist"
err = b.Prepare(config)
if err == nil {
t.Error("should have error")
}
config["iso_url"] = "http://www.packer.io"
err = b.Prepare(config) err = b.Prepare(config)
if err != nil { if err != nil {
t.Fatalf("should not have error: %s", err) t.Errorf("should not have error: %s", err)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment