-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathamazonlinux.go
78 lines (63 loc) · 1.73 KB
/
amazonlinux.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
package amazonlinux
import (
"bufio"
"context"
"io"
"os"
"strings"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
"github.com/aquasecurity/trivy/pkg/fanal/types"
aos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
)
func init() {
analyzer.RegisterAnalyzer(&amazonlinuxOSAnalyzer{})
}
const version = 1
var requiredFiles = []string{
"etc/system-release", // for 1 and 2 versions
"usr/lib/system-release", // for 2022 version
}
type amazonlinuxOSAnalyzer struct{}
func (a amazonlinuxOSAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
foundOS, err := a.parseRelease(input.Content)
if err != nil {
return nil, err
}
return &analyzer.AnalysisResult{
OS: &foundOS,
}, nil
}
func (a amazonlinuxOSAnalyzer) parseRelease(r io.Reader) (types.OS, error) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
// Only Amazon Linux Prefix
if strings.HasPrefix(line, "Amazon Linux release 2") {
if len(fields) < 5 {
continue
}
return types.OS{
Family: aos.Amazon,
Name: strings.Join(fields[3:], " "),
}, nil
} else if strings.HasPrefix(line, "Amazon Linux") {
return types.OS{
Family: aos.Amazon,
Name: strings.Join(fields[2:], " "),
}, nil
}
}
return types.OS{}, xerrors.Errorf("amazon: %w", aos.AnalyzeOSError)
}
func (a amazonlinuxOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, requiredFiles)
}
func (a amazonlinuxOSAnalyzer) Type() analyzer.Type {
return analyzer.TypeAmazon
}
func (a amazonlinuxOSAnalyzer) Version() int {
return version
}