Add pwd to gogetter ChecksumFromFile (#9129)

This commit is contained in:
Sylvia Moss 2020-05-11 11:14:50 +02:00 committed by GitHub
parent 9c37d20721
commit 25971b3b5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 118 additions and 54 deletions

View File

@ -7,8 +7,14 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"log"
"os"
"strings" "strings"
urlhelper "github.com/hashicorp/go-getter/v2/helper/url"
"github.com/hashicorp/go-getter/v2"
"github.com/hashicorp/packer/template/interpolate" "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")) errs = append(errs, fmt.Errorf("A checksum must be specified"))
} }
if c.ISOChecksumType == "file" { if c.ISOChecksumType == "file" {
url := c.ISOChecksum
if c.ISOChecksumURL != "" { 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 { if err != nil {
errs = append(errs, fmt.Errorf("Couldn't extract checksum from checksum file: %v", err)) errs = append(errs, fmt.Errorf("Couldn't extract checksum from checksum file: %v", err))
} else { } else {

View File

@ -3,8 +3,10 @@
package common package common
import ( import (
"io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "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" const fixtureDir = "./test-fixtures"
func httpTestModule(n string) *httptest.Server { func httpTestModule(n string) *httptest.Server {

3
go.mod
View File

@ -76,8 +76,7 @@ require (
github.com/hashicorp/go-cty-funcs/encoding v0.0.0-20200203151509-c92509f48b18 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/filesystem v0.0.0-20200203151509-c92509f48b18
github.com/hashicorp/go-cty-funcs/uuid 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-20200511085045-ad575698c524
github.com/hashicorp/go-getter/v2 v2.0.0-20200318090939-0b1d527d9793
github.com/hashicorp/go-multierror v1.0.0 github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/go-oracle-terraform v0.0.0-20181016190316-007121241b79 github.com/hashicorp/go-oracle-terraform v0.0.0-20181016190316-007121241b79
github.com/hashicorp/go-retryablehttp v0.5.2 // indirect github.com/hashicorp/go-retryablehttp v0.5.2 // indirect

10
go.sum
View File

@ -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-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 h1:HAasZmyRrb7/paYuww5RfVwY3wkFpsbMNYwBxOSZquY=
github.com/hashicorp/go-getter v1.3.1-0.20190906090232-a0f878cb75da/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY= 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 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-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 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= 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= github.com/hashicorp/go-msgpack v0.5.3 h1:zKjpN5BK/P5lMYrLmBHdBULWbJ0XpYR+7NGzqkZzoD4=

View File

@ -1,12 +1,10 @@
# go-getter # go-getter
[![CircleCI](https://circleci.com/gh/hashicorp/go-getter/tree/master.svg?style=svg)][circleci] [![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] [![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 [circleci]: https://circleci.com/gh/hashicorp/go-getter/tree/master
[godocs]: http://godoc.org/github.com/hashicorp/go-getter [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 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. 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, directory in this repository, but because we specified a subdirectory,
go-getter automatically copied only that directory contents. go-getter automatically copied only that directory contents.
Subdirectory paths may contain may also use filesystem glob patterns. Subdirectory paths may also use filesystem glob patterns. The path must
The path must match _exactly one_ entry or go-getter will return an error. 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 This is useful if you're not sure the exact directory name but it follows
a predictable naming structure. a predictable naming structure.

View File

@ -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 ./...

View File

@ -12,7 +12,6 @@ import (
"fmt" "fmt"
"hash" "hash"
"io" "io"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -75,15 +74,17 @@ func (c *FileChecksum) checksum(source string) error {
return nil return nil
} }
// extractChecksum will return a FileChecksum based on the 'checksum' // GetChecksum extracts the checksum from the `checksum` parameter
// parameter of u. // of the src of the Request
// ex: // ex:
// http://hashicorp.com/terraform?checksum=<checksumValue> // http://hashicorp.com/terraform?checksum=<checksumValue>
// http://hashicorp.com/terraform?checksum=<checksumType>:<checksumValue> // http://hashicorp.com/terraform?checksum=<checksumType>:<checksumValue>
// http://hashicorp.com/terraform?checksum=file:<checksum_url> // http://hashicorp.com/terraform?checksum=file:<checksum_url>
// when checksumming from a file, extractChecksum will go get checksum_url // when the checksum is in a file, GetChecksum will first client.Get it
// in a temporary directory, parse the content of the file then delete it. // in a temporary directory, parse the content of the file and finally delete it.
// Content of files are expected to be BSD style or GNU style. // 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: // BSD-style checksum:
// MD5 (file1) = <checksum> // MD5 (file1) = <checksum>
@ -92,10 +93,15 @@ func (c *FileChecksum) checksum(source string) error {
// GNU-style: // GNU-style:
// <checksum> file1 // <checksum> file1
// <checksum> *file2 // <checksum> *file2
// func (c *Client) GetChecksum(ctx context.Context, req *Request) (*FileChecksum, error) {
// see parseChecksumLine for more detail on checksum file parsing var err error
func (c *Client) extractChecksum(ctx context.Context, u *url.URL) (*FileChecksum, error) { if req.u == nil {
q := u.Query() req.u, err = urlhelper.Parse(req.Src)
if err != nil {
return nil, err
}
}
q := req.u.Query()
v := q.Get("checksum") v := q.Get("checksum")
if v == "" { if v == "" {
@ -109,16 +115,16 @@ func (c *Client) extractChecksum(ctx context.Context, u *url.URL) (*FileChecksum
default: default:
// here, we try to guess the checksum from it's length // here, we try to guess the checksum from it's length
// if the type was not passed // 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] checksumType, checksumValue := vs[0], vs[1]
switch checksumType { switch checksumType {
case "file": case "file":
return c.ChecksumFromFile(ctx, checksumValue, u.Path) return c.checksumFromFile(ctx, checksumValue, req.u.Path, req.Pwd)
default: 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 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. // `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. // 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. // 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) checksumFileURL, err := urlhelper.Parse(checksumURL)
if err != nil { if err != nil {
return nil, err return nil, err
@ -205,12 +211,13 @@ func (c *Client) ChecksumFromFile(ctx context.Context, checksumURL, checksummedP
defer os.Remove(tempfile) defer os.Remove(tempfile)
req := &Request{ req := &Request{
// Pwd: c.Pwd, TODO(adrien): pass pwd ? Pwd: pwd,
Mode: ModeFile, Mode: ModeFile,
Src: checksumURL, Src: checksumURL,
Dst: tempfile, Dst: tempfile,
// ProgressListener: c.ProgressListener, TODO(adrien): pass progress bar ? // ProgressListener: c.ProgressListener, TODO(adrien): pass progress bar ?
} }
if _, err = c.Get(ctx, req); err != nil { if _, err = c.Get(ctx, req); err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"Error downloading checksum file: %s", err) "Error downloading checksum file: %s", err)

View File

@ -142,7 +142,7 @@ func (c *Client) Get(ctx context.Context, req *Request) (*GetResult, error) {
} }
// Determine checksum if we have one // Determine checksum if we have one
checksum, err := c.extractChecksum(ctx, req.u) checksum, err := c.GetChecksum(ctx, req)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid checksum: %s", err) return nil, fmt.Errorf("invalid checksum: %s", err)
} }

View File

@ -5,22 +5,15 @@ require (
github.com/aws/aws-sdk-go v1.15.78 github.com/aws/aws-sdk-go v1.15.78
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d
github.com/cheggaaa/pb v1.0.27 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/google/go-cmp v0.3.0
github.com/hashicorp/go-cleanhttp v0.5.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-safetemp v1.0.0
github.com/hashicorp/go-version v1.1.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-homedir v1.0.0
github.com/mitchellh/go-testing-interface 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 github.com/ulikunitz/xz v0.5.5
google.golang.org/api v0.9.0 google.golang.org/api v0.9.0
gopkg.in/cheggaaa/pb.v1 v1.0.27 // indirect
) )
go 1.13 go 1.13

View File

@ -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/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 h1:wvCrVc9TjDls6+YGAF2hAifE1E5U1+b4tH6KdvN3Gig=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= 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 h1:2HR189eFNrjHQyENnQMMpCiBAsRxzbTMIgBhEyExpmo=
github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoDHxNAB65b+Rj1I= github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoDHxNAB65b+Rj1I=
github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0= github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0=

4
vendor/modules.txt vendored
View File

@ -318,9 +318,9 @@ github.com/hashicorp/go-cty-funcs/encoding
github.com/hashicorp/go-cty-funcs/filesystem 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 v0.0.0-20200203151509-c92509f48b18
github.com/hashicorp/go-cty-funcs/uuid 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/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
github.com/hashicorp/go-getter/v2/helper/url github.com/hashicorp/go-getter/v2/helper/url
# github.com/hashicorp/go-immutable-radix v1.0.0 # github.com/hashicorp/go-immutable-radix v1.0.0