-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathexecutable.go
56 lines (44 loc) · 1.29 KB
/
executable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package executable
import (
"context"
"crypto/sha256"
"encoding/hex"
"io"
"os"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)
func init() {
analyzer.RegisterAnalyzer(&executableAnalyzer{})
}
const version = 1
// executableAnalyzer calculates SHA-256 for each binary not managed by package managers (called unpackaged binaries)
// so that it can search for SBOM attestation in post-handler.
type executableAnalyzer struct{}
func (a executableAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
// Skip non-binaries
isBinary, err := utils.IsBinary(input.Content, input.Info.Size())
if !isBinary || err != nil {
return nil, nil
}
h := sha256.New()
if _, err = io.Copy(h, input.Content); err != nil {
return nil, xerrors.Errorf("sha256 error: %w", err)
}
s := hex.EncodeToString(h.Sum(nil))
return &analyzer.AnalysisResult{
Digests: map[string]string{
input.FilePath: "sha256:" + s,
},
}, nil
}
func (a executableAnalyzer) Required(_ string, fileInfo os.FileInfo) bool {
return utils.IsExecutable(fileInfo)
}
func (a executableAnalyzer) Type() analyzer.Type {
return analyzer.TypeExecutable
}
func (a executableAnalyzer) Version() int {
return version
}