diff --git a/common/iso_config.go b/common/iso_config.go index b8a201402..20db11a8e 100644 --- a/common/iso_config.go +++ b/common/iso_config.go @@ -7,8 +7,14 @@ import ( "encoding/hex" "errors" "fmt" + "log" + "os" "strings" + urlhelper "github.com/hashicorp/go-getter/v2/helper/url" + + "github.com/hashicorp/go-getter/v2" + "github.com/hashicorp/packer/template/interpolate" ) @@ -151,11 +157,41 @@ func (c *ISOConfig) Prepare(*interpolate.Context) (warnings []string, errs []err errs = append(errs, fmt.Errorf("A checksum must be specified")) } if c.ISOChecksumType == "file" { - url := c.ISOChecksum if c.ISOChecksumURL != "" { - url = c.ISOChecksumURL + c.ISOChecksum = c.ISOChecksumURL } - cksum, err := defaultGetterClient.ChecksumFromFile(context.TODO(), url, c.ISOUrls[0]) + + u, err := urlhelper.Parse(c.ISOUrls[0]) + if err != nil { + return warnings, append(errs, fmt.Errorf("url parse: %s", err)) + } + + if cs := u.Query().Get("checksum"); cs != "" { + c.ISOChecksum = cs + } + if c.ISOChecksumType != "" && c.ISOChecksumType != "none" { + // add checksum to url query params as go getter will checksum for us + q := u.Query() + q.Set("checksum", c.ISOChecksumType+":"+c.ISOChecksum) + u.RawQuery = q.Encode() + } else if c.ISOChecksum != "" { + q := u.Query() + q.Set("checksum", c.ISOChecksum) + u.RawQuery = q.Encode() + } + + wd, err := os.Getwd() + if err != nil { + log.Printf("get working directory: %v", err) + // here we ignore the error in case the + // working directory is not needed. + } + + req := &getter.Request{ + Src: u.String(), + Pwd: wd, + } + cksum, err := defaultGetterClient.GetChecksum(context.TODO(), req) if err != nil { errs = append(errs, fmt.Errorf("Couldn't extract checksum from checksum file: %v", err)) } else { diff --git a/common/iso_config_test.go b/common/iso_config_test.go index a9f13b6fb..3d0ab08c6 100644 --- a/common/iso_config_test.go +++ b/common/iso_config_test.go @@ -3,8 +3,10 @@ package common import ( + "io" "net/http" "net/http/httptest" + "os" "path/filepath" "reflect" "testing" @@ -279,6 +281,39 @@ func TestISOConfigPrepare_ISOChecksumURLMyTest(t *testing.T) { } } +func TestISOConfigPrepare_ISOChecksumLocalFile(t *testing.T) { + // Creates checksum file in local dir + p := filepath.Join(fixtureDir, "root/subfolder.sum") + source, err := os.Open(p) + if err != nil { + t.Fatalf(err.Error()) + } + defer source.Close() + destination, err := os.Create("local.sum") + if err != nil { + t.Fatalf(err.Error()) + } + defer os.Remove("local.sum") + defer destination.Close() + if _, err := io.Copy(destination, source); err != nil { + t.Fatalf(err.Error()) + } + + i := ISOConfig{ + ISOChecksumURL: "./local.sum", + ISOChecksumType: "sha256", + ISOUrls: []string{"http://hashicorp.com/ubuntu/dists/bionic-updates/main/installer-amd64/current/images/netboot/mini.iso"}, + } + + warns, errs := i.Prepare(nil) + if len(warns) > 0 { + t.Fatalf("Bad: should not have warnings") + } + if len(errs) > 0 { + t.Fatalf("Bad; should not have errored. %v", errs) + } +} + const fixtureDir = "./test-fixtures" func httpTestModule(n string) *httptest.Server { diff --git a/go.mod b/go.mod index f1fb3a332..c2c1f8298 100644 --- a/go.mod +++ b/go.mod @@ -76,8 +76,7 @@ require ( github.com/hashicorp/go-cty-funcs/encoding v0.0.0-20200203151509-c92509f48b18 github.com/hashicorp/go-cty-funcs/filesystem v0.0.0-20200203151509-c92509f48b18 github.com/hashicorp/go-cty-funcs/uuid v0.0.0-20200203151509-c92509f48b18 - github.com/hashicorp/go-getter v1.3.1-0.20190906090232-a0f878cb75da // indirect - github.com/hashicorp/go-getter/v2 v2.0.0-20200318090939-0b1d527d9793 + github.com/hashicorp/go-getter/v2 v2.0.0-20200511085045-ad575698c524 github.com/hashicorp/go-multierror v1.0.0 github.com/hashicorp/go-oracle-terraform v0.0.0-20181016190316-007121241b79 github.com/hashicorp/go-retryablehttp v0.5.2 // indirect diff --git a/go.sum b/go.sum index ea82bfc07..f855a1190 100644 --- a/go.sum +++ b/go.sum @@ -265,8 +265,18 @@ github.com/hashicorp/go-cty-funcs/uuid v0.0.0-20200203151509-c92509f48b18 h1:CxY github.com/hashicorp/go-cty-funcs/uuid v0.0.0-20200203151509-c92509f48b18/go.mod h1:QFbv9KeSic7KIgfOYbUW02G4LxOf3Fh9Ylm4n174LUQ= github.com/hashicorp/go-getter v1.3.1-0.20190906090232-a0f878cb75da h1:HAasZmyRrb7/paYuww5RfVwY3wkFpsbMNYwBxOSZquY= github.com/hashicorp/go-getter v1.3.1-0.20190906090232-a0f878cb75da/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY= +github.com/hashicorp/go-getter v1.4.1 h1:3A2Mh8smGFcf5M+gmcv898mZdrxpseik45IpcyISLsA= +github.com/hashicorp/go-getter v1.4.1/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY= github.com/hashicorp/go-getter/v2 v2.0.0-20200318090939-0b1d527d9793 h1:jH222Ag2I+p5tq5IagFwCfj5CnRzqcGW8RgcQ8jEPMs= github.com/hashicorp/go-getter/v2 v2.0.0-20200318090939-0b1d527d9793/go.mod h1:jlmxRRjTpY0KdWrV1Uq38GUVskrjIZUrjOAybo0OArw= +github.com/hashicorp/go-getter/v2 v2.0.0-20200427140258-32e0fe310a1d h1:EbUwSFm9Dkq/pgx6OtKZBmQheJtJRSqL/6ltzk4AheE= +github.com/hashicorp/go-getter/v2 v2.0.0-20200427140258-32e0fe310a1d/go.mod h1:ouW3tfHUq9ebkm9+QYiGdnxwS5+YkNHEMiT9FAZdKKM= +github.com/hashicorp/go-getter/v2 v2.0.0-20200428121039-ea2d4f264fe2 h1:ZM8JBXeUSU1+kF5x+3OmWhIvW95y5BmszLuiN6b7XaA= +github.com/hashicorp/go-getter/v2 v2.0.0-20200428121039-ea2d4f264fe2/go.mod h1:ouW3tfHUq9ebkm9+QYiGdnxwS5+YkNHEMiT9FAZdKKM= +github.com/hashicorp/go-getter/v2 v2.0.0-20200428131008-7ca0077af588 h1:jIkZMaAG0noQODaZLYgXeyZNhPBLrcdGoHRVN0KZBfQ= +github.com/hashicorp/go-getter/v2 v2.0.0-20200428131008-7ca0077af588/go.mod h1:ouW3tfHUq9ebkm9+QYiGdnxwS5+YkNHEMiT9FAZdKKM= +github.com/hashicorp/go-getter/v2 v2.0.0-20200511085045-ad575698c524 h1:xjV9iDvMeEgjUYEG3GB3KwN6OciqotOQk0J5ggl/B+E= +github.com/hashicorp/go-getter/v2 v2.0.0-20200511085045-ad575698c524/go.mod h1:ouW3tfHUq9ebkm9+QYiGdnxwS5+YkNHEMiT9FAZdKKM= github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0= github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= github.com/hashicorp/go-msgpack v0.5.3 h1:zKjpN5BK/P5lMYrLmBHdBULWbJ0XpYR+7NGzqkZzoD4= diff --git a/vendor/github.com/hashicorp/go-getter/v2/README.md b/vendor/github.com/hashicorp/go-getter/v2/README.md index bbcd15de9..f5abbbc69 100644 --- a/vendor/github.com/hashicorp/go-getter/v2/README.md +++ b/vendor/github.com/hashicorp/go-getter/v2/README.md @@ -1,12 +1,10 @@ # go-getter [![CircleCI](https://circleci.com/gh/hashicorp/go-getter/tree/master.svg?style=svg)][circleci] -[![Build status](https://ci.appveyor.com/api/projects/status/ulq3qr43n62croyq/branch/master?svg=true)][appveyor] [![Go Documentation](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][godocs] [circleci]: https://circleci.com/gh/hashicorp/go-getter/tree/master [godocs]: http://godoc.org/github.com/hashicorp/go-getter -[appveyor]: https://ci.appveyor.com/project/hashicorp/go-getter/branch/master go-getter is a library for Go (golang) for downloading files or directories from various sources using a URL as the primary form of input. @@ -139,8 +137,8 @@ If you downloaded this to the `/tmp` directory, then the file directory in this repository, but because we specified a subdirectory, go-getter automatically copied only that directory contents. -Subdirectory paths may contain may also use filesystem glob patterns. -The path must match _exactly one_ entry or go-getter will return an error. +Subdirectory paths may also use filesystem glob patterns. The path must +match _exactly one_ entry or go-getter will return an error. This is useful if you're not sure the exact directory name but it follows a predictable naming structure. diff --git a/vendor/github.com/hashicorp/go-getter/v2/appveyor.yml b/vendor/github.com/hashicorp/go-getter/v2/appveyor.yml deleted file mode 100644 index 1e8718e17..000000000 --- a/vendor/github.com/hashicorp/go-getter/v2/appveyor.yml +++ /dev/null @@ -1,16 +0,0 @@ -version: "build-{branch}-{build}" -image: Visual Studio 2017 -clone_folder: c:\gopath\github.com\hashicorp\go-getter -environment: - GOPATH: c:\gopath -install: -- cmd: >- - echo %Path% - - go version - - go env - - go get -d -v -t ./... -build_script: -- cmd: go test ./... diff --git a/vendor/github.com/hashicorp/go-getter/v2/checksum.go b/vendor/github.com/hashicorp/go-getter/v2/checksum.go index 94e137435..df2a00346 100644 --- a/vendor/github.com/hashicorp/go-getter/v2/checksum.go +++ b/vendor/github.com/hashicorp/go-getter/v2/checksum.go @@ -12,7 +12,6 @@ import ( "fmt" "hash" "io" - "net/url" "os" "path/filepath" "strings" @@ -75,15 +74,17 @@ func (c *FileChecksum) checksum(source string) error { return nil } -// extractChecksum will return a FileChecksum based on the 'checksum' -// parameter of u. +// GetChecksum extracts the checksum from the `checksum` parameter +// of the src of the Request // ex: // http://hashicorp.com/terraform?checksum= // http://hashicorp.com/terraform?checksum=: // http://hashicorp.com/terraform?checksum=file: -// when checksumming from a file, extractChecksum will go get checksum_url -// in a temporary directory, parse the content of the file then delete it. -// Content of files are expected to be BSD style or GNU style. +// when the checksum is in a file, GetChecksum will first client.Get it +// in a temporary directory, parse the content of the file and finally delete it. +// The content of a checksum file is expected to be BSD style or GNU style. +// For security reasons GetChecksum does not try to get the current working directory +// and as a result, relative files will only be found when Request.Pwd is set. // // BSD-style checksum: // MD5 (file1) = @@ -92,10 +93,15 @@ func (c *FileChecksum) checksum(source string) error { // GNU-style: // file1 // *file2 -// -// see parseChecksumLine for more detail on checksum file parsing -func (c *Client) extractChecksum(ctx context.Context, u *url.URL) (*FileChecksum, error) { - q := u.Query() +func (c *Client) GetChecksum(ctx context.Context, req *Request) (*FileChecksum, error) { + var err error + if req.u == nil { + req.u, err = urlhelper.Parse(req.Src) + if err != nil { + return nil, err + } + } + q := req.u.Query() v := q.Get("checksum") if v == "" { @@ -109,16 +115,16 @@ func (c *Client) extractChecksum(ctx context.Context, u *url.URL) (*FileChecksum default: // here, we try to guess the checksum from it's length // if the type was not passed - return newChecksumFromValue(v, filepath.Base(u.EscapedPath())) + return newChecksumFromValue(v, filepath.Base(req.u.EscapedPath())) } checksumType, checksumValue := vs[0], vs[1] switch checksumType { case "file": - return c.ChecksumFromFile(ctx, checksumValue, u.Path) + return c.checksumFromFile(ctx, checksumValue, req.u.Path, req.Pwd) default: - return newChecksumFromType(checksumType, checksumValue, filepath.Base(u.EscapedPath())) + return newChecksumFromType(checksumType, checksumValue, filepath.Base(req.u.EscapedPath())) } } @@ -184,15 +190,15 @@ func newChecksumFromValue(checksumValue, filename string) (*FileChecksum, error) return c, nil } -// ChecksumFromFile will return the first file checksum found in the +// checksumFromFile will return the first file checksum found in the // `checksumURL` file that corresponds to the `checksummedPath` path. // -// ChecksumFromFile will infer the hashing algorithm based on the checksumURL +// checksumFromFile will infer the hashing algorithm based on the checksumURL // file content. // -// ChecksumFromFile will only return checksums for files that match +// checksumFromFile will only return checksums for files that match // checksummedPath, which is the object being checksummed. -func (c *Client) ChecksumFromFile(ctx context.Context, checksumURL, checksummedPath string) (*FileChecksum, error) { +func (c *Client) checksumFromFile(ctx context.Context, checksumURL string, checksummedPath string, pwd string) (*FileChecksum, error) { checksumFileURL, err := urlhelper.Parse(checksumURL) if err != nil { return nil, err @@ -205,12 +211,13 @@ func (c *Client) ChecksumFromFile(ctx context.Context, checksumURL, checksummedP defer os.Remove(tempfile) req := &Request{ - // Pwd: c.Pwd, TODO(adrien): pass pwd ? + Pwd: pwd, Mode: ModeFile, Src: checksumURL, Dst: tempfile, // ProgressListener: c.ProgressListener, TODO(adrien): pass progress bar ? } + if _, err = c.Get(ctx, req); err != nil { return nil, fmt.Errorf( "Error downloading checksum file: %s", err) diff --git a/vendor/github.com/hashicorp/go-getter/v2/client.go b/vendor/github.com/hashicorp/go-getter/v2/client.go index a6a1bf2bd..c34aafd0b 100644 --- a/vendor/github.com/hashicorp/go-getter/v2/client.go +++ b/vendor/github.com/hashicorp/go-getter/v2/client.go @@ -142,7 +142,7 @@ func (c *Client) Get(ctx context.Context, req *Request) (*GetResult, error) { } // Determine checksum if we have one - checksum, err := c.extractChecksum(ctx, req.u) + checksum, err := c.GetChecksum(ctx, req) if err != nil { return nil, fmt.Errorf("invalid checksum: %s", err) } diff --git a/vendor/github.com/hashicorp/go-getter/v2/go.mod b/vendor/github.com/hashicorp/go-getter/v2/go.mod index 3335e86a2..fdb259051 100644 --- a/vendor/github.com/hashicorp/go-getter/v2/go.mod +++ b/vendor/github.com/hashicorp/go-getter/v2/go.mod @@ -5,22 +5,15 @@ require ( github.com/aws/aws-sdk-go v1.15.78 github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d github.com/cheggaaa/pb v1.0.27 - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/fatih/color v1.7.0 // indirect github.com/google/go-cmp v0.3.0 github.com/hashicorp/go-cleanhttp v0.5.0 + github.com/hashicorp/go-getter v1.4.1 github.com/hashicorp/go-safetemp v1.0.0 github.com/hashicorp/go-version v1.1.0 - github.com/mattn/go-colorable v0.0.9 // indirect - github.com/mattn/go-isatty v0.0.4 // indirect - github.com/mattn/go-runewidth v0.0.4 // indirect github.com/mitchellh/go-homedir v1.0.0 github.com/mitchellh/go-testing-interface v1.0.0 - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.2.2 // indirect github.com/ulikunitz/xz v0.5.5 google.golang.org/api v0.9.0 - gopkg.in/cheggaaa/pb.v1 v1.0.27 // indirect ) go 1.13 diff --git a/vendor/github.com/hashicorp/go-getter/v2/go.sum b/vendor/github.com/hashicorp/go-getter/v2/go.sum index b88c747ce..1f32e953d 100644 --- a/vendor/github.com/hashicorp/go-getter/v2/go.sum +++ b/vendor/github.com/hashicorp/go-getter/v2/go.sum @@ -44,6 +44,8 @@ github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/hashicorp/go-cleanhttp v0.5.0 h1:wvCrVc9TjDls6+YGAF2hAifE1E5U1+b4tH6KdvN3Gig= github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-getter v1.4.1 h1:3A2Mh8smGFcf5M+gmcv898mZdrxpseik45IpcyISLsA= +github.com/hashicorp/go-getter v1.4.1/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY= github.com/hashicorp/go-safetemp v1.0.0 h1:2HR189eFNrjHQyENnQMMpCiBAsRxzbTMIgBhEyExpmo= github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoDHxNAB65b+Rj1I= github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0= diff --git a/vendor/modules.txt b/vendor/modules.txt index 3a8769c6a..0781c657d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -318,9 +318,9 @@ github.com/hashicorp/go-cty-funcs/encoding github.com/hashicorp/go-cty-funcs/filesystem # github.com/hashicorp/go-cty-funcs/uuid v0.0.0-20200203151509-c92509f48b18 github.com/hashicorp/go-cty-funcs/uuid -# github.com/hashicorp/go-getter v1.3.1-0.20190906090232-a0f878cb75da +# github.com/hashicorp/go-getter v1.4.1 github.com/hashicorp/go-getter/helper/url -# github.com/hashicorp/go-getter/v2 v2.0.0-20200318090939-0b1d527d9793 +# github.com/hashicorp/go-getter/v2 v2.0.0-20200511085045-ad575698c524 github.com/hashicorp/go-getter/v2 github.com/hashicorp/go-getter/v2/helper/url # github.com/hashicorp/go-immutable-radix v1.0.0