From 4bf75ac0f38e7fe7e8caf353990de82eaa6db008 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 30 May 2019 15:34:18 -0500 Subject: [PATCH 1/2] fix null file descriptor error --- provisioner/powershell/provisioner.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index b554ca6f2..6b0038947 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -9,6 +9,7 @@ import ( "fmt" "log" "os" + "path/filepath" "sort" "strings" "time" @@ -252,6 +253,14 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C ui.Say(fmt.Sprintf("Provisioning with powershell script: %s", path)) log.Printf("Opening %s for reading", path) + fi, err := os.Stat(path) + if err != nil { + return fmt.Errorf("Error stating powershell script: %s", err) + } + if strings.HasSuffix(p.config.RemotePath, `\`) { + // path is a directory + p.config.RemotePath += filepath.Base((fi).Name()) + } f, err := os.Open(path) if err != nil { return fmt.Errorf("Error opening powershell script: %s", err) @@ -272,7 +281,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C if _, err := f.Seek(0, 0); err != nil { return err } - if err := comm.Upload(p.config.RemotePath, f, nil); err != nil { + if err := comm.Upload(p.config.RemotePath, f, &fi); err != nil { return fmt.Errorf("Error uploading script: %s", err) } From 7b1f7c87e5634e85061f0eadffae0a32956e4398 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 31 May 2019 16:54:25 -0600 Subject: [PATCH 2/2] add a check and test so that the winrm communicator upload doesn't fail becuase of a nil file pointer --- communicator/winrm/communicator.go | 6 +++++- communicator/winrm/communicator_test.go | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/communicator/winrm/communicator.go b/communicator/winrm/communicator.go index 548abe9f5..1e543dceb 100644 --- a/communicator/winrm/communicator.go +++ b/communicator/winrm/communicator.go @@ -130,7 +130,11 @@ func (c *Communicator) Upload(path string, input io.Reader, fi *os.FileInfo) err } if strings.HasSuffix(path, `\`) { // path is a directory - path += filepath.Base((*fi).Name()) + if fi != nil { + path += filepath.Base((*fi).Name()) + } else { + return fmt.Errorf("Was unable to infer file basename for upload.") + } } log.Printf("Uploading file to '%s'", path) return wcp.Write(path, input) diff --git a/communicator/winrm/communicator_test.go b/communicator/winrm/communicator_test.go index 3f650a000..b7bc78304 100644 --- a/communicator/winrm/communicator_test.go +++ b/communicator/winrm/communicator_test.go @@ -120,5 +120,25 @@ func TestUpload(t *testing.T) { if downloadedPayload != PAYLOAD { t.Fatalf("files are not equal: expected [%s] length: %v, got [%s] length %v", PAYLOAD, len(PAYLOAD), downloadedPayload, len(downloadedPayload)) } +} + +func TestUpload_nilFileInfo(t *testing.T) { + wrm := newMockWinRMServer(t) + defer wrm.Close() + c, err := New(&Config{ + Host: wrm.Host, + Port: wrm.Port, + Username: "user", + Password: "pass", + Timeout: 30 * time.Second, + }) + if err != nil { + t.Fatalf("error creating communicator: %s", err) + } + file := "C:\\Temp\\" + err = c.Upload(file, strings.NewReader(PAYLOAD), nil) + if err == nil { + t.Fatalf("Should have errored because of nil fileinfo") + } }