Skip to content

Commit

Permalink
Add a proper logger to fetch (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
robmorgan authored Feb 5, 2021
1 parent 8c6520a commit 8a22319
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 106 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@
fetch
fetch.exe
bin/*

# Don't upload dependencies
vendor
6 changes: 4 additions & 2 deletions checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"io"
"os"
"reflect"

"github.com/sirupsen/logrus"
)

func verifyChecksumOfReleaseAsset(assetPath string, checksumMap map[string]bool, algorithm string) *FetchError {
func verifyChecksumOfReleaseAsset(logger *logrus.Logger, assetPath string, checksumMap map[string]bool, algorithm string) *FetchError {
computedChecksum, err := computeChecksum(assetPath, algorithm)
if err != nil {
return newError(errorWhileComputingChecksum, err.Error())
Expand All @@ -20,7 +22,7 @@ func verifyChecksumOfReleaseAsset(assetPath string, checksumMap map[string]bool,
keys := reflect.ValueOf(checksumMap).MapKeys()
return newError(checksumDoesNotMatch, fmt.Sprintf("Expected to checksum value to be one of %s, but instead got %s for Release Asset at %s. This means that either you are using the wrong checksum value in your call to fetch, (e.g. did you update the version of the module you're installing but not the checksum?) or that someone has replaced the asset with a potentially dangerous one and you should be very careful about proceeding.", keys, computedChecksum, assetPath))
}
fmt.Printf("Release asset checksum verified for %s\n", assetPath)
logger.Infof("Release asset checksum verified for %s\n", assetPath)

return nil
}
Expand Down
10 changes: 6 additions & 4 deletions checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var SAMPLE_RELEASE_ASSET_CHECKSUMS_SHA256_NO_MATCH = map[string]bool{

func TestVerifyReleaseAsset(t *testing.T) {
tmpDir := mkTempDir(t)
logger := GetProjectLogger()
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
Expand All @@ -37,7 +38,7 @@ func TestVerifyReleaseAsset(t *testing.T) {
t.Fatalf("Failed to parse sample release asset GitHub URL into Fetch GitHubRepo struct: %s", err)
}

assetPaths, fetchErr := downloadReleaseAssets(SAMPLE_RELEASE_ASSET_NAME, tmpDir, githubRepo, SAMPLE_RELEASE_ASSET_VERSION, false)
assetPaths, fetchErr := downloadReleaseAssets(logger, SAMPLE_RELEASE_ASSET_NAME, tmpDir, githubRepo, SAMPLE_RELEASE_ASSET_VERSION, false)
if fetchErr != nil {
t.Fatalf("Failed to download release asset: %s", fetchErr)
}
Expand All @@ -62,6 +63,7 @@ func TestVerifyReleaseAsset(t *testing.T) {

func TestVerifyChecksumOfReleaseAsset(t *testing.T) {
tmpDir := mkTempDir(t)
logger := GetProjectLogger()
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
Expand All @@ -72,7 +74,7 @@ func TestVerifyChecksumOfReleaseAsset(t *testing.T) {
t.Fatalf("Failed to parse sample release asset GitHub URL into Fetch GitHubRepo struct: %s", err)
}

assetPaths, fetchErr := downloadReleaseAssets(SAMPLE_RELEASE_ASSET_REGEX, tmpDir, githubRepo, SAMPLE_RELEASE_ASSET_VERSION, false)
assetPaths, fetchErr := downloadReleaseAssets(logger, SAMPLE_RELEASE_ASSET_REGEX, tmpDir, githubRepo, SAMPLE_RELEASE_ASSET_VERSION, false)
if fetchErr != nil {
t.Fatalf("Failed to download release asset: %s", fetchErr)
}
Expand All @@ -82,14 +84,14 @@ func TestVerifyChecksumOfReleaseAsset(t *testing.T) {
}

for _, assetPath := range assetPaths {
checksumErr := verifyChecksumOfReleaseAsset(assetPath, SAMPLE_RELEASE_ASSET_CHECKSUMS_SHA256, "sha256")
checksumErr := verifyChecksumOfReleaseAsset(logger, assetPath, SAMPLE_RELEASE_ASSET_CHECKSUMS_SHA256, "sha256")
if checksumErr != nil {
t.Fatalf("Expected downloaded asset to match one of %d checksums: %s", len(SAMPLE_RELEASE_ASSET_CHECKSUMS_SHA256), checksumErr)
}
}

for _, assetPath := range assetPaths {
checksumErr := verifyChecksumOfReleaseAsset(assetPath, SAMPLE_RELEASE_ASSET_CHECKSUMS_SHA256_NO_MATCH, "sha256")
checksumErr := verifyChecksumOfReleaseAsset(logger, assetPath, SAMPLE_RELEASE_ASSET_CHECKSUMS_SHA256_NO_MATCH, "sha256")
if checksumErr == nil {
t.Fatalf("Expected downloaded asset to not match any checksums")
}
Expand Down
6 changes: 5 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"os"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
)

// Download the zip file at the given URL to a temporary local directory.
// Returns the absolute path to the downloaded zip file.
// IMPORTANT: You must call "defer os.RemoveAll(dir)" in the calling function when done with the downloaded zip file!
func downloadGithubZipFile(gitHubCommit GitHubCommit, gitHubToken string, instance GitHubInstance) (string, *FetchError) {
func downloadGithubZipFile(logger *logrus.Logger, gitHubCommit GitHubCommit, gitHubToken string, instance GitHubInstance) (string, *FetchError) {

var zipFilePath string

Expand All @@ -33,6 +35,7 @@ func downloadGithubZipFile(gitHubCommit GitHubCommit, gitHubToken string, instan
return zipFilePath, wrapError(err)
}

logger.Debugf("Performing HTTP request to download GitHub ZIP Archive: %s", req.URL)
resp, err := httpClient.Do(req)
if err != nil {
return zipFilePath, wrapError(err)
Expand All @@ -51,6 +54,7 @@ func downloadGithubZipFile(gitHubCommit GitHubCommit, gitHubToken string, instan
return zipFilePath, wrapError(err)
}

logger.Debugf("Writing ZIP Archive to temporary path: %s", tempDir)
err = ioutil.WriteFile(filepath.Join(tempDir, "repo.zip"), respBodyBuffer.Bytes(), 0644)
if err != nil {
return zipFilePath, wrapError(err)
Expand Down
19 changes: 12 additions & 7 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestDownloadGitTagZipFile(t *testing.T) {
}

for _, tc := range cases {
logger := GetProjectLogger()
gitHubCommits := []GitHubCommit{
// Test as a GitTag
GitHubCommit{
Expand All @@ -63,7 +64,7 @@ func TestDownloadGitTagZipFile(t *testing.T) {
},
}
for _, gitHubCommit := range gitHubCommits {
zipFilePath, err := downloadGithubZipFile(gitHubCommit, tc.githubToken, tc.instance)
zipFilePath, err := downloadGithubZipFile(logger, gitHubCommit, tc.githubToken, tc.instance)

defer os.RemoveAll(zipFilePath)

Expand Down Expand Up @@ -118,6 +119,7 @@ func TestDownloadGitBranchZipFile(t *testing.T) {
}

for _, tc := range cases {
logger := GetProjectLogger()
gitHubCommits := []GitHubCommit{
GitHubCommit{
Repo: GitHubRepo{
Expand All @@ -135,7 +137,7 @@ func TestDownloadGitBranchZipFile(t *testing.T) {
},
}
for _, gitHubCommit := range gitHubCommits {
zipFilePath, err := downloadGithubZipFile(gitHubCommit, tc.githubToken, tc.instance)
zipFilePath, err := downloadGithubZipFile(logger, gitHubCommit, tc.githubToken, tc.instance)
defer os.RemoveAll(zipFilePath)
if err != nil {
t.Fatalf("Failed to download file: %s", err)
Expand Down Expand Up @@ -167,6 +169,7 @@ func TestDownloadBadGitBranchZipFile(t *testing.T) {
}

for _, tc := range cases {
logger := GetProjectLogger()
gitHubCommits := []GitHubCommit{
GitHubCommit{
Repo: GitHubRepo{
Expand All @@ -184,7 +187,7 @@ func TestDownloadBadGitBranchZipFile(t *testing.T) {
},
}
for _, gitHubCommit := range gitHubCommits {
zipFilePath, err := downloadGithubZipFile(gitHubCommit, tc.githubToken, tc.instance)
zipFilePath, err := downloadGithubZipFile(logger, gitHubCommit, tc.githubToken, tc.instance)
defer os.RemoveAll(zipFilePath)
if err == nil {
t.Fatalf("Expected that attempt to download repo %s/%s for branch \"%s\" would fail, but received no error.", tc.repoOwner, tc.repoName, tc.branchName)
Expand Down Expand Up @@ -215,6 +218,7 @@ func TestDownloadGitCommitFile(t *testing.T) {
}

for _, tc := range cases {
logger := GetProjectLogger()
GitHubCommits := []GitHubCommit{
GitHubCommit{
Repo: GitHubRepo{
Expand All @@ -232,7 +236,7 @@ func TestDownloadGitCommitFile(t *testing.T) {
},
}
for _, gitHubCommit := range GitHubCommits {
zipFilePath, err := downloadGithubZipFile(gitHubCommit, tc.githubToken, tc.instance)
zipFilePath, err := downloadGithubZipFile(logger, gitHubCommit, tc.githubToken, tc.instance)
defer os.RemoveAll(zipFilePath)
if err != nil {
t.Fatalf("Failed to download file: %s", err)
Expand Down Expand Up @@ -269,7 +273,7 @@ func TestDownloadBadGitCommitFile(t *testing.T) {
}

for _, tc := range cases {

logger := GetProjectLogger()
gitHubCommits := []GitHubCommit{
GitHubCommit{
Repo: GitHubRepo{
Expand All @@ -287,7 +291,7 @@ func TestDownloadBadGitCommitFile(t *testing.T) {
},
}
for _, gitHubCommit := range gitHubCommits {
zipFilePath, err := downloadGithubZipFile(gitHubCommit, tc.githubToken, tc.instance)
zipFilePath, err := downloadGithubZipFile(logger, gitHubCommit, tc.githubToken, tc.instance)
defer os.RemoveAll(zipFilePath)
if err == nil {
t.Fatalf("Expected that attempt to download repo %s/%s at commmit sha \"%s\" would fail, but received no error.", tc.repoOwner, tc.repoName, tc.commitSha)
Expand Down Expand Up @@ -315,6 +319,7 @@ func TestDownloadZipFileWithBadRepoValues(t *testing.T) {
}

for _, tc := range cases {
logger := GetProjectLogger()
gitHubCommits := []GitHubCommit{
GitHubCommit{
Repo: GitHubRepo{
Expand All @@ -333,7 +338,7 @@ func TestDownloadZipFileWithBadRepoValues(t *testing.T) {
}
for _, gitHubCommit := range gitHubCommits {

_, err := downloadGithubZipFile(gitHubCommit, tc.githubToken, tc.instance)
_, err := downloadGithubZipFile(logger, gitHubCommit, tc.githubToken, tc.instance)
if err == nil && err.errorCode != 500 {
t.Fatalf("Expected error for bad repo values: %s/%s:%s", tc.repoOwner, tc.repoName, tc.gitTag)
}
Expand Down
5 changes: 3 additions & 2 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/dustin/go-humanize"
"github.com/hashicorp/go-version"
"github.com/sirupsen/logrus"
)

type GitHubRepo struct {
Expand Down Expand Up @@ -75,7 +76,7 @@ type GitHubReleaseAsset struct {
Name string
}

func ParseUrlIntoGithubInstance(repoUrl string, apiv string) (GitHubInstance, *FetchError) {
func ParseUrlIntoGithubInstance(logger *logrus.Logger, repoUrl string, apiv string) (GitHubInstance, *FetchError) {
var instance GitHubInstance

u, err := url.Parse(repoUrl)
Expand All @@ -86,7 +87,7 @@ func ParseUrlIntoGithubInstance(repoUrl string, apiv string) (GitHubInstance, *F
baseUrl := u.Host
apiUrl := "api.github.com"
if baseUrl != "github.com" && baseUrl != "www.github.com" {
fmt.Printf("Assuming GitHub Enterprise since the provided url (%s) does not appear to be for GitHub.com\n", repoUrl)
logger.Infof("Assuming GitHub Enterprise since the provided url (%s) does not appear to be for GitHub.com\n", repoUrl)
apiUrl = baseUrl + "/api/" + apiv
}

Expand Down
6 changes: 4 additions & 2 deletions github_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func TestGetListOfReleasesFromGitHubRepo(t *testing.T) {
Expand Down Expand Up @@ -146,7 +147,8 @@ func TestParseUrlIntoGithubInstance(t *testing.T) {
}

for _, tc := range cases {
inst, err := ParseUrlIntoGithubInstance(tc.repoUrl, tc.apiv)
logger := GetProjectLogger()
inst, err := ParseUrlIntoGithubInstance(logger, tc.repoUrl, tc.apiv)
if err != nil {
t.Fatalf("error extracting url %s into a GitHubRepo struct: %s", tc.repoUrl, err)
}
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ go 1.14

require (
github.com/dustin/go-humanize v1.0.0
github.com/gruntwork-io/gruntwork-cli v0.7.2
github.com/hashicorp/go-version v1.2.1
github.com/kr/pretty v0.2.0 // indirect
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/urfave/cli.v1 v1.20.0
)
38 changes: 38 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/gruntwork-io/gruntwork-cli v0.7.2 h1:aZTztE9vVxUnpNFBecOPuqk1QYl5fPPIriE15Sp3ATs=
github.com/gruntwork-io/gruntwork-cli v0.7.2/go.mod h1:jp6Z7NcLF2avpY8v71fBx6hds9eOFPELSuD/VPv7w00=
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-zglob v0.0.1/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 h1:5/PjkGUjvEU5Gl6BxmvKRPpqo2uNMv4rcHBMwzk/st8=
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/urfave/cli.v1 v1.20.0 h1:NdAVW6RYxDif9DhDHaAortIu956m2c0v+09AZBPTbE0=
gopkg.in/urfave/cli.v1 v1.20.0/go.mod h1:vuBzUtMdQeixQj8LVd+/98pzhxNGQoyuPBlsXHOQNO0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 8a22319

Please sign in to comment.