-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathexecutable.go
53 lines (41 loc) · 1.25 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
package executable
import (
"context"
"os"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/digest"
"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
}
dig, err := digest.CalcSHA256(input.Content)
if err != nil {
return nil, xerrors.Errorf("sha256 error: %w", err)
}
return &analyzer.AnalysisResult{
Digests: map[string]string{
input.FilePath: dig.String(),
},
}, 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
}