Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev' into add_exclude_nuget
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas committed Feb 25, 2024
2 parents 441470b + bd9687a commit 3255cf9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
4 changes: 2 additions & 2 deletions build/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ func (gm *GoModule) getPackagePathIfExists(cachePath, encodedDependencyId string
func populateZip(packageId, zipPath string) (zipDependency entities.Dependency, err error) {
// Zip file dependency for the build-info
zipDependency = entities.Dependency{Id: packageId}
md5, sha1, sha2, err := utils.GetFileChecksums(zipPath)
checksums, err := utils.GetFileChecksums(zipPath)
if err != nil {
return
}
zipDependency.Type = "zip"
zipDependency.Checksum = entities.Checksum{Sha1: sha1, Md5: md5, Sha256: sha2}
zipDependency.Checksum = entities.Checksum{Sha1: checksums[utils.SHA1], Md5: checksums[utils.MD5], Sha256: checksums[utils.SHA256]}
return
}

Expand Down
9 changes: 7 additions & 2 deletions build/utils/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"encoding/json"
"errors"
"fmt"
"golang.org/x/exp/slices"
"os"
"os/exec"
"path/filepath"
"strings"

"golang.org/x/exp/slices"

"github.com/buger/jsonparser"
"github.com/jfrog/build-info-go/entities"
"github.com/jfrog/build-info-go/utils"
Expand Down Expand Up @@ -380,7 +381,11 @@ func calculateChecksum(cacache *cacache, name, version, integrity string) (md5 s
if err != nil {
return
}
return utils.GetFileChecksums(path)
checksums, err := utils.GetFileChecksums(path)
if err != nil {
return
}
return checksums[utils.MD5], checksums[utils.SHA1], checksums[utils.SHA256], err
}

// Merge two scopes and remove duplicates.
Expand Down
16 changes: 5 additions & 11 deletions utils/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package utils

import (
"bufio"
"errors"

//#nosec G501 -- md5 is supported by Artifactory.
"crypto/md5"
//#nosec G505 -- sha1 is supported by Artifactory.
Expand Down Expand Up @@ -30,23 +32,15 @@ var algorithmFunc = map[Algorithm]func() hash.Hash{
SHA256: sha256.New,
}

func GetFileChecksums(filePath string) (md5, sha1, sha2 string, err error) {
func GetFileChecksums(filePath string, checksumType ...Algorithm) (checksums map[Algorithm]string, err error) {
file, err := os.Open(filePath)
if err != nil {
return
}
defer func() {
e := file.Close()
if err == nil {
err = e
}
err = errors.Join(err, file.Close())
}()
checksumInfo, err := CalcChecksums(file)
if err != nil {
return
}
md5, sha1, sha2 = checksumInfo[MD5], checksumInfo[SHA1], checksumInfo[SHA256]
return
return CalcChecksums(file, checksumType...)
}

// CalcChecksums calculates all hashes at once using AsyncMultiWriter. The file is therefore read only once.
Expand Down
42 changes: 42 additions & 0 deletions utils/checksum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package utils

import (
"os"
"testing"

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

const (
fileContent = "Why did the robot bring a ladder to the bar? It heard the drinks were on the house."
expectedMd5 = "70bd6370a86813f2504020281e4a2e2e"
expectedSha1 = "8c3578ac814c9f02803001a5d3e5d78a7fd0f9cc"
expectedSha256 = "093d901b28a59f7d95921f3f4fb97a03fe7a1cf8670507ffb1d6f9a01b3e890a"
)

func TestGetFileChecksums(t *testing.T) {
// Create a temporary file
tempFile, err := os.CreateTemp("", "TestGetFileChecksums")
assert.NoError(t, err)
defer func() {
assert.NoError(t, tempFile.Close())
assert.NoError(t, os.Remove(tempFile.Name()))
}()

// Write something to the file
_, err = tempFile.Write([]byte(fileContent))
assert.NoError(t, err)

// Calculate only sha1 and match
checksums, err := GetFileChecksums(tempFile.Name(), SHA1)
assert.NoError(t, err)
assert.Len(t, checksums, 1)
assert.Equal(t, expectedSha1, checksums[SHA1])

// Calculate md5, sha1 and sha256 checksums and match
checksums, err = GetFileChecksums(tempFile.Name())
assert.NoError(t, err)
assert.Equal(t, expectedMd5, checksums[MD5])
assert.Equal(t, expectedSha1, checksums[SHA1])
assert.Equal(t, expectedSha256, checksums[SHA256])
}
4 changes: 2 additions & 2 deletions utils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,10 @@ func calcChecksumDetails(filePath string) (checksum entities.Checksum, err error
err = errors.Join(err, file.Close())
}()

checksumInfo, err := CalcChecksums(file)
checksums, err := CalcChecksums(file)
if err != nil {
return entities.Checksum{}, err
}
checksum = entities.Checksum{Md5: checksumInfo[MD5], Sha1: checksumInfo[SHA1], Sha256: checksumInfo[SHA256]}
checksum = entities.Checksum{Md5: checksums[MD5], Sha1: checksums[SHA1], Sha256: checksums[SHA256]}
return
}

0 comments on commit 3255cf9

Please sign in to comment.