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

Add SHA512 and SHA1 support for downloaded files #97

Merged
merged 1 commit into from
Feb 5, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,27 @@ type Data struct {
HeaderSize string `xml:"header-size"`
}

func (d *Data) SHA512() (string, error) {
if d.Checksum.Type == "sha512" {
return d.Checksum.Text, nil
}
return "", fmt.Errorf("no sha512 found")
}

func (d *Data) SHA256() (string, error) {
if d.Checksum.Type == "sha256" {
return d.Checksum.Text, nil
}
return "", fmt.Errorf("no sha256 found")
}

func (d *Data) SHA() (string, error) {
if d.Checksum.Type == "sha" {
return d.Checksum.Text, nil
}
return "", fmt.Errorf("no sha found")
}

type Repomd struct {
XMLName xml.Name `xml:"repomd"`
Text string `xml:",chardata"`
Expand Down
52 changes: 45 additions & 7 deletions pkg/repo/fetch.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package repo

import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
Expand Down Expand Up @@ -189,23 +192,25 @@ func (r *RepoFetcherImpl) fetchFile(fileType string, repo *bazeldnf.Repository,
if err != nil {
return fmt.Errorf("Failed to load primary repository file from %s: %v", fileURL, err)
}
sha := sha256.New()
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("Failed to download %s: %v ", fileURL, fmt.Errorf("status : %v", resp.StatusCode))
}
sha, shasum, err := chooseHashType(file)
if err != nil {
return err
}

body := io.TeeReader(resp.Body, sha)
err = r.CacheHelper.WriteToRepoDir(repo, body, fileName)
if err != nil {
return fmt.Errorf("Failed to write file.xml from %s to file: %v", fileURL, err)
}
sha256sum, err := file.SHA256()
if err != nil {
return fmt.Errorf("failed to get sha256sum of file: %v", err)
}
if sha256sum != toHex(sha) {
return fmt.Errorf("Expected sha256 sum %s, but got %s", sha256sum, toHex(sha))

if shasum != toHex(sha) {
return fmt.Errorf("Expected sha sum %s, but got %s", shasum, toHex(sha))
}

return nil
}

Expand Down Expand Up @@ -243,3 +248,36 @@ func (*getterImpl) Get(rawURL string) (*http.Response, error) {
func toHex(hasher hash.Hash) string {
return hex.EncodeToString(hasher.Sum(nil))
}

// chooseHashType tries to get the SHA(512|256|1) sum for the file
// and returns the appropriate hash write as well as that sum
func chooseHashType(f *api.Data) (hash.Hash, string, error) {
sha512sum, err := f.SHA512()
if err == nil {
return sha512.New(), sha512sum, nil
}

if err.Error() != "no sha512 found" {
return nil, "", fmt.Errorf("error getting sha512sum of file: %w", err)
}

sha256sum, err := f.SHA256()
if err == nil {
return sha256.New(), sha256sum, nil
}

if err.Error() != "no sha256 found" {
return nil, "", fmt.Errorf("error getting sha256sum of file: %w", err)
}

sha1sum, err := f.SHA()
if err == nil {
return sha1.New(), sha1sum, nil
}

if err.Error() != "no sha found" {
return nil, "", fmt.Errorf("err getting sha1sum of file: %w", err)
}

return nil, "", errors.New("Unable identify file checksum: no sha512, sha256, or sha1 found")
}