-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add multiple strategies for release and asset querying (#11)
Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
13 changed files
with
1,098 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,101 @@ | ||
package internal | ||
|
||
import ( | ||
"crypto/md5" // nolint:gosec // MD5 is used for legacy compatibility | ||
"crypto/sha1" // nolint:gosec // SHA1 is used for legacy compatibility | ||
"crypto/sha256" | ||
"crypto/sha512" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/anchore/binny/internal/log" | ||
"github.com/go-git/go-git/v5/plumbing/hash" | ||
|
||
"github.com/anchore/go-logger" | ||
) | ||
|
||
func DownloadFile(url string, filepath string, checksum string) (err error) { | ||
out, err := os.Create(filepath) | ||
func DownloadFile(lgr logger.Logger, url string, filepath string, checksum string) (err error) { | ||
reader, err := DownloadURL(lgr, url) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
defer reader.Close() | ||
|
||
resp, err := http.Get(url) // nolint:gosec | ||
out, err := os.Create(filepath) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("bad status: %s", resp.Status) | ||
} | ||
defer out.Close() | ||
|
||
// take sha256 of file and compare with checksum while copying to disk | ||
h := sha256.New() | ||
tee := io.TeeReader(resp.Body, h) | ||
// hash the file and compare with checksum while copying to disk | ||
h := getHasher(checksum) | ||
tee := io.TeeReader(reader, h) | ||
|
||
if _, err := io.Copy(out, tee); err != nil { | ||
return err | ||
} | ||
|
||
if checksum != "" { | ||
if checksum != fmt.Sprintf("%x", h.Sum(nil)) { | ||
expectedChecksum := cleanChecksum(checksum) | ||
actualChecksum := fmt.Sprintf("%x", h.Sum(nil)) | ||
|
||
if expectedChecksum != actualChecksum { | ||
lgr.WithFields("url", url, "expected", expectedChecksum, "actual", actualChecksum).Warn("checksum mismatch") | ||
return fmt.Errorf("checksum mismatch for %q", filepath) | ||
} | ||
|
||
log.WithFields("checksum", checksum, "asset", filepath, "url", url).Trace("checksum verified") | ||
lgr.WithFields("checksum", expectedChecksum, "asset", filepath, "url", url).Trace("checksum verified") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func DownloadURL(lgr logger.Logger, url string) (io.ReadCloser, error) { | ||
resp, err := http.Get(url) // nolint: gosec // we must be able to get arbitrary URLs | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to download %q: %w", url, err) | ||
} | ||
|
||
lgr.WithFields("http-status", resp.StatusCode).Tracef("http get %q", url) | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, nil | ||
} | ||
return resp.Body, nil | ||
} | ||
|
||
func cleanChecksum(checksum string) string { | ||
parts := strings.SplitN(checksum, ":", 2) | ||
if len(parts) < 2 { | ||
return checksum | ||
} | ||
|
||
return parts[1] | ||
} | ||
|
||
func getHasher(checksum string) hash.Hash { | ||
// Default to SHA-256 if no prefix or unsupported prefix | ||
defaultHash := sha256.New() | ||
|
||
parts := strings.SplitN(checksum, ":", 2) | ||
if len(parts) < 2 { | ||
return defaultHash | ||
} | ||
|
||
algorithm := strings.ToLower(parts[0]) | ||
|
||
switch algorithm { | ||
case "sha256": | ||
return sha256.New() | ||
case "sha1": | ||
return sha1.New() // nolint:gosec // SHA1 is used for legacy compatibility | ||
case "sha512": | ||
return sha512.New() | ||
case "md5": | ||
return md5.New() // nolint:gosec // MD5 is used for legacy compatibility | ||
default: | ||
return defaultHash | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package githubrelease | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type ghRelease struct { | ||
Tag string | ||
Date *time.Time | ||
IsLatest *bool | ||
IsDraft *bool | ||
Assets []ghAsset | ||
} | ||
|
||
type ghAsset struct { | ||
Name string | ||
ContentType string | ||
URL string | ||
Checksum string | ||
} | ||
|
||
func (a *ghAsset) addChecksum(value string) { | ||
if strings.Contains(value, ":") { | ||
a.Checksum = value | ||
return | ||
} | ||
|
||
// note: assume this is a hex digest | ||
var method string | ||
switch len(value) { | ||
case 32: | ||
method = "md5" | ||
case 40: | ||
method = "sha1" | ||
case 64: | ||
method = "sha256" | ||
case 128: | ||
method = "sha512" | ||
default: | ||
// dunno, just capture the value | ||
a.Checksum = value | ||
return | ||
} | ||
|
||
a.Checksum = fmt.Sprintf("%s:%s", method, value) | ||
} |
Oops, something went wrong.