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

SHA-256 hash support for unverified upstream sources #147

Merged
merged 1 commit into from
Jan 13, 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
23 changes: 23 additions & 0 deletions impl/create_srpm_from_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ import (
"code.arista.io/eos/tools/eext/util"
)

func checkSHA256Hash(srcFilePath string, sha256InManifest string,
errPrefix util.ErrPrefix) error {
sha256Hash, err := util.GenerateSha256Hash(srcFilePath)
if err != nil {
return fmt.Errorf("%s SHA256 generation failed with '%s' for file: %s",
errPrefix, err, srcFilePath)
}
if sha256Hash != sha256InManifest {
return fmt.Errorf("%s bad SHA256: '%s' expected: '%s' for file: %s",
errPrefix, sha256Hash, sha256InManifest, srcFilePath)
}
return nil
}

func (bldr *srpmBuilder) getUpstreamSourceForOthers(upstreamSrcFromManifest manifest.UpstreamSrc,
downloadDir string) (*upstreamSrcSpec, error) {

Expand Down Expand Up @@ -50,6 +64,15 @@ func (bldr *srpmBuilder) getUpstreamSourceForOthers(upstreamSrcFromManifest mani
}
bldr.log("downloaded")

if upstreamSrcFromManifest.Sha256 != "" {
sha256InManifest := upstreamSrcFromManifest.Sha256
srcFilePath := filepath.Join(downloadDir, upstreamSrc.sourceFile)
err := checkSHA256Hash(srcFilePath, sha256InManifest, bldr.errPrefix)
if err != nil {
return nil, err
}
}

upstreamSrc.skipSigCheck = upstreamSrcFromManifest.Signature.SkipCheck
pubKey := upstreamSrcFromManifest.Signature.DetachedSignature.PubKey

Expand Down
21 changes: 21 additions & 0 deletions impl/create_srpm_from_others_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,24 @@ func TestMatchTarballSignature(t *testing.T) {
os.Remove(intermediateTarball)
require.NoError(t, err)
}

func getSHA256hash(folderName string, sha256InManifest string) error {
sourceFile := "mrtparse-2.0.1.tar.gz"
cwd, _ := os.Getwd()
downloadDir := filepath.Join(cwd, folderName)
srcFilePath := filepath.Join(downloadDir, sourceFile)
checkSHA256HashErr := checkSHA256Hash(srcFilePath, sha256InManifest, "")
return checkSHA256HashErr
}

func TestUpstreamSourcesBadSHA256Hash(t *testing.T) {
sha256InManifest := "ac4456cda847db6a757fjhagdfjaghdfk3ee3b59e0d7ca224266aafa326163aec"
err := getSHA256hash("testData/upstream-hash-check-bad", sha256InManifest)
require.Error(t, err)
}

func TestUpstreamSourcesGoodSHA256Hash(t *testing.T) {
sha256InManifest := "ac4456cda847db6a757f0c27cb09ad9b3ee3b59e0d7ca224266aafa326163aec"
err := getSHA256hash("testData/upstream-hash-check-good", sha256InManifest)
require.NoError(t, err)
}
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type UpstreamSrc struct {
FullURL string `yaml:"full-url"`
GitBundle GitBundle `yaml:"git"`
Signature Signature `yaml:"signature"`
Sha256 string `yaml:"sha256"`
}

// Package spec
Expand Down
16 changes: 16 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package util

import (
"crypto/sha256"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -161,3 +162,18 @@ func GetRepoDir(repo string) string {
}
return repoDir
}

// Generate SHA256 hash of file
func GenerateSha256Hash(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("GenerateSha256Hash: %s", err)
}
defer file.Close()
hashComputer := sha256.New()
if _, err := io.Copy(hashComputer, file); err != nil {
return "", fmt.Errorf("GenerateSha256Hash: %s", err)
}
sha256Hash := fmt.Sprintf("%x", hashComputer.Sum(nil))
return sha256Hash, nil
}
Loading