-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathrelease.go
84 lines (68 loc) · 1.71 KB
/
release.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
package release
import (
"bufio"
"context"
"os"
"strings"
"golang.org/x/exp/slices"
"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(&osReleaseAnalyzer{})
}
const version = 1
var requiredFiles = []string{
"etc/os-release",
"usr/lib/os-release",
}
type osReleaseAnalyzer struct{}
func (a osReleaseAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
var id, versionID string
scanner := bufio.NewScanner(input.Content)
for scanner.Scan() {
line := scanner.Text()
ss := strings.SplitN(line, "=", 2)
if len(ss) != 2 {
continue
}
key, value := strings.TrimSpace(ss[0]), strings.TrimSpace(ss[1])
switch key {
case "ID":
id = strings.Trim(value, `"'`)
case "VERSION_ID":
versionID = strings.Trim(value, `"'`)
default:
continue
}
var family string
switch id {
case "alpine":
family = aos.Alpine
case "opensuse-tumbleweed":
family = aos.OpenSUSETumbleweed
case "opensuse-leap", "opensuse": // opensuse for leap:42, opensuse-leap for leap:15
family = aos.OpenSUSELeap
case "sles":
family = aos.SLES
case "photon":
family = aos.Photon
}
if family != "" && versionID != "" {
return &analyzer.AnalysisResult{
OS: &types.OS{Family: family, Name: versionID},
}, nil
}
}
return nil, nil
}
func (a osReleaseAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return slices.Contains(requiredFiles, filePath)
}
func (a osReleaseAnalyzer) Type() analyzer.Type {
return analyzer.TypeOSRelease
}
func (a osReleaseAnalyzer) Version() int {
return version
}