-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
redhatbase.go
84 lines (66 loc) · 2.12 KB
/
redhatbase.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 redhatbase
import (
"bufio"
"context"
"io"
"os"
"regexp"
"strings"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"golang.org/x/xerrors"
aos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
)
const redhatAnalyzerVersion = 1
func init() {
analyzer.RegisterAnalyzer(&redhatOSAnalyzer{})
}
var redhatRe = regexp.MustCompile(`(.*) release (\d[\d\.]*)`)
type redhatOSAnalyzer struct{}
func (a redhatOSAnalyzer) 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 redhatOSAnalyzer) parseRelease(r io.Reader) (types.OS, error) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
result := redhatRe.FindStringSubmatch(strings.TrimSpace(line))
if len(result) != 3 {
return types.OS{}, xerrors.New("redhat: invalid redhat-release")
}
switch strings.ToLower(result[1]) {
case "centos", "centos linux":
return types.OS{Family: aos.CentOS, Name: result[2]}, nil
case "rocky", "rocky linux":
return types.OS{Family: aos.Rocky, Name: result[2]}, nil
case "alma", "almalinux", "alma linux":
return types.OS{Family: aos.Alma, Name: result[2]}, nil
case "oracle", "oracle linux", "oracle linux server":
return types.OS{Family: aos.Oracle, Name: result[2]}, nil
case "fedora", "fedora linux":
return types.OS{Family: aos.Fedora, Name: result[2]}, nil
default:
return types.OS{Family: aos.RedHat, Name: result[2]}, nil
}
}
return types.OS{}, xerrors.Errorf("redhatbase: %w", aos.AnalyzeOSError)
}
func (a redhatOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, a.requiredFiles())
}
func (a redhatOSAnalyzer) requiredFiles() []string {
return []string{"etc/redhat-release"}
}
func (a redhatOSAnalyzer) Type() analyzer.Type {
return analyzer.TypeRedHatBase
}
func (a redhatOSAnalyzer) Version() int {
return redhatAnalyzerVersion
}