From 819986d19fdfc803b6abcf7b845e7bd3c1418faa Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 29 May 2015 16:24:29 -0700 Subject: [PATCH] builder/docker: validate export path is not a dir [GH-2105] --- CHANGELOG.md | 1 + builder/docker/config.go | 8 ++++++++ builder/docker/config_test.go | 13 +++++++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d935fc24d..d3f7473ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ BUG FIXES: * builder/docker: Use `docker exec` for newer versions of Docker for running scripts [GH-1993] * builder/docker: Fix crash that could occur at certain timed ctrl-c [GH-1838] + * builder/docker: validate that `export_path` is not a directory [GH-2105] * builder/qemu: Add `disk_discard` option [GH-2120] * builder/virtualbox: Added SCSI support * builder/vmware: Case-insensitive match of MAC address to find IP [GH-1989] diff --git a/builder/docker/config.go b/builder/docker/config.go index 4fc6f762a..024b915af 100644 --- a/builder/docker/config.go +++ b/builder/docker/config.go @@ -2,6 +2,7 @@ package docker import ( "fmt" + "os" "github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/common" @@ -79,6 +80,13 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { fmt.Errorf("both commit and export_path cannot be set")) } + if c.ExportPath != "" { + if fi, err := os.Stat(c.ExportPath); err == nil && fi.IsDir() { + errs = packer.MultiErrorAppend(errs, fmt.Errorf( + "export_path must be a file, not a directory")) + } + } + if errs != nil && len(errs.Errors) > 0 { return nil, nil, errs } diff --git a/builder/docker/config_test.go b/builder/docker/config_test.go index 3f535b9e9..907222b4f 100644 --- a/builder/docker/config_test.go +++ b/builder/docker/config_test.go @@ -1,6 +1,8 @@ package docker import ( + "io/ioutil" + "os" "testing" ) @@ -42,6 +44,12 @@ func testConfigOk(t *testing.T, warns []string, err error) { } func TestConfigPrepare_exportPath(t *testing.T) { + td, err := ioutil.TempDir("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(td) + raw := testConfig() // No export path @@ -53,6 +61,11 @@ func TestConfigPrepare_exportPath(t *testing.T) { raw["export_path"] = "good" _, warns, errs = NewConfig(raw) testConfigOk(t, warns, errs) + + // Bad export path (directory) + raw["export_path"] = td + _, warns, errs = NewConfig(raw) + testConfigErr(t, warns, errs) } func TestConfigPrepare_exportPathAndCommit(t *testing.T) {