-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathapk.go
98 lines (80 loc) · 2.15 KB
/
apk.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package apk
import (
"bufio"
"context"
"os"
"regexp"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
ver "github.com/aquasecurity/go-version/pkg/version"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
aos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)
func init() {
analyzer.RegisterAnalyzer(&apkRepoAnalyzer{})
}
const version = 1
const edgeVersion = "edge"
var (
requiredFiles = []string{"etc/apk/repositories"}
urlParseRegexp = regexp.MustCompile(`(https*|ftp)://[0-9A-Za-z.-]+/([A-Za-z]+)/v?([0-9A-Za-z_.-]+)/`)
)
type apkRepoAnalyzer struct{}
func (a apkRepoAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
scanner := bufio.NewScanner(input.Content)
var osFamily, repoVer string
for scanner.Scan() {
line := scanner.Text()
m := urlParseRegexp.FindStringSubmatch(line)
if len(m) != 4 {
continue
}
newOSFamily := m[2]
newVersion := m[3]
// Find OS Family
if osFamily != "" && osFamily != newOSFamily {
return nil, xerrors.Errorf("mixing different distributions in etc/apk/repositories: %s != %s", osFamily, newOSFamily)
}
osFamily = newOSFamily
// Find max Release version
switch {
case repoVer == "":
repoVer = newVersion
case repoVer == edgeVersion || newVersion == edgeVersion:
repoVer = edgeVersion
default:
oldVer, err := ver.Parse(repoVer)
if err != nil {
continue
}
newVer, err := ver.Parse(newVersion)
if err != nil {
continue
}
// Take the maximum version in apk repositories
if newVer.GreaterThan(oldVer) {
repoVer = newVersion
}
}
}
// Currently, we support only Alpine Linux in apk repositories.
if osFamily != aos.Alpine || repoVer == "" {
return nil, nil
}
return &analyzer.AnalysisResult{
Repository: &types.Repository{
Family: osFamily,
Release: repoVer,
},
}, nil
}
func (a apkRepoAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return slices.Contains(requiredFiles, filePath)
}
func (a apkRepoAnalyzer) Type() analyzer.Type {
return analyzer.TypeApkRepo
}
func (a apkRepoAnalyzer) Version() int {
return version
}