forked from anchore/syft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add file source digest support (anchore#1914)
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
- Loading branch information
Showing
18 changed files
with
286 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
go.work | ||
go.work.sum | ||
/.bin | ||
CHANGELOG.md | ||
VERSION | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package config | ||
|
||
import "github.com/spf13/viper" | ||
|
||
type sourceCfg struct { | ||
Name string `json:"name" yaml:"name" mapstructure:"name"` | ||
Version string `json:"version" yaml:"version" mapstructure:"version"` | ||
File fileSource `json:"file" yaml:"file" mapstructure:"file"` | ||
} | ||
|
||
type fileSource struct { | ||
Digests []string `json:"digests" yaml:"digests" mapstructure:"digests"` | ||
} | ||
|
||
func (cfg sourceCfg) loadDefaultValues(v *viper.Viper) { | ||
v.SetDefault("source.file.digests", []string{"sha256"}) | ||
} |
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,76 @@ | ||
package file | ||
|
||
import ( | ||
"crypto" | ||
"fmt" | ||
"hash" | ||
"io" | ||
"strings" | ||
|
||
"github.com/anchore/syft/syft/file" | ||
) | ||
|
||
func supportedHashAlgorithms() []crypto.Hash { | ||
return []crypto.Hash{ | ||
crypto.MD5, | ||
crypto.SHA1, | ||
crypto.SHA224, | ||
crypto.SHA256, | ||
crypto.SHA384, | ||
crypto.SHA512, | ||
} | ||
} | ||
|
||
func NewDigestsFromFile(closer io.ReadCloser, hashes []crypto.Hash) ([]file.Digest, error) { | ||
// create a set of hasher objects tied together with a single writer to feed content into | ||
hashers := make([]hash.Hash, len(hashes)) | ||
writers := make([]io.Writer, len(hashes)) | ||
for idx, hashObj := range hashes { | ||
hashers[idx] = hashObj.New() | ||
writers[idx] = hashers[idx] | ||
} | ||
|
||
size, err := io.Copy(io.MultiWriter(writers...), closer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if size == 0 { | ||
return make([]file.Digest, 0), nil | ||
} | ||
|
||
result := make([]file.Digest, len(hashes)) | ||
// only capture digests when there is content. It is important to do this based on SIZE and not | ||
// FILE TYPE. The reasoning is that it is possible for a tar to be crafted with a header-only | ||
// file type but a body is still allowed. | ||
for idx, hasher := range hashers { | ||
result[idx] = file.Digest{ | ||
Algorithm: CleanDigestAlgorithmName(hashes[idx].String()), | ||
Value: fmt.Sprintf("%+x", hasher.Sum(nil)), | ||
} | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func Hashers(names ...string) ([]crypto.Hash, error) { | ||
hashByName := make(map[string]crypto.Hash) | ||
for _, h := range supportedHashAlgorithms() { | ||
hashByName[CleanDigestAlgorithmName(h.String())] = h | ||
} | ||
|
||
var hashers []crypto.Hash | ||
for _, hashStr := range names { | ||
hashObj, ok := hashByName[CleanDigestAlgorithmName(hashStr)] | ||
if !ok { | ||
return nil, fmt.Errorf("unsupported hash algorithm: %s", hashStr) | ||
} | ||
hashers = append(hashers, hashObj) | ||
} | ||
return hashers, nil | ||
} | ||
|
||
func CleanDigestAlgorithmName(name string) string { | ||
lower := strings.ToLower(name) | ||
return strings.ReplaceAll(lower, "-", "") | ||
} |
Oops, something went wrong.