Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Response.ContentLength & clarify log messages #49

Merged
merged 1 commit into from Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions internal/releasesjson/checksum_downloader.go
Expand Up @@ -121,12 +121,23 @@ func fileMapFromChecksums(checksums strings.Builder) (ChecksumFileMap, error) {
return csMap, nil
}

func compareChecksum(logger *log.Logger, r io.Reader, verifiedHashSum HashSum) error {
func compareChecksum(logger *log.Logger, r io.Reader, verifiedHashSum HashSum, filename string, expectedSize int64) error {
h := sha256.New()
_, err := io.Copy(h, r)

// This may take a while depending on network connection as the io.Reader
// is expected to be http.Response.Body which streams the bytes
// on demand over the network.
logger.Printf("copying %q (%d bytes) to calculate checksum", filename, expectedSize)
bytesCopied, err := io.Copy(h, r)
if err != nil {
return err
}
logger.Printf("copied %d bytes of %q", bytesCopied, filename)

if expectedSize != 0 && bytesCopied != int64(expectedSize) {
return fmt.Errorf("unexpected size (downloaded: %d, expected: %d)",
bytesCopied, expectedSize)
}

calculatedSum := h.Sum(nil)
if !bytes.Equal(calculatedSum, verifiedHashSum) {
Expand Down
21 changes: 9 additions & 12 deletions internal/releasesjson/downloader.go
Expand Up @@ -12,7 +12,6 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"

"github.com/hashicorp/hc-install/internal/httpclient"
)
Expand Down Expand Up @@ -95,14 +94,16 @@ func (d *Downloader) DownloadAndUnpack(ctx context.Context, pv *ProductVersion,
contentType, zipMimeTypes)
}

expectedSize := resp.ContentLength

if d.VerifyChecksum {
d.Logger.Printf("calculating checksum of %q", pb.Filename)
d.Logger.Printf("verifying checksum of %q", pb.Filename)
// provide extra reader to calculate & compare checksum
var buf bytes.Buffer
r := io.TeeReader(resp.Body, &buf)
pkgReader = &buf

err := compareChecksum(d.Logger, r, verifiedChecksum)
err := compareChecksum(d.Logger, r, verifiedChecksum, pb.Filename, expectedSize)
if err != nil {
return err
}
Expand All @@ -114,21 +115,17 @@ func (d *Downloader) DownloadAndUnpack(ctx context.Context, pv *ProductVersion,
}
defer pkgFile.Close()

d.Logger.Printf("copying downloaded file to %s", pkgFile.Name())
d.Logger.Printf("copying %q (%d bytes) to %s", pb.Filename, expectedSize, pkgFile.Name())
// Unless the bytes were already downloaded above for checksum verification
// this may take a while depending on network connection as the io.Reader
// is expected to be http.Response.Body which streams the bytes
// on demand over the network.
bytesCopied, err := io.Copy(pkgFile, pkgReader)
if err != nil {
return err
}
d.Logger.Printf("copied %d bytes to %s", bytesCopied, pkgFile.Name())

expectedSize := 0
if length := resp.Header.Get("content-length"); length != "" {
var err error
expectedSize, err = strconv.Atoi(length)
if err != nil {
return err
}
}
if expectedSize != 0 && bytesCopied != int64(expectedSize) {
return fmt.Errorf("unexpected size (downloaded: %d, expected: %d)",
bytesCopied, expectedSize)
Expand Down