-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathubuntu.go
64 lines (52 loc) · 1.3 KB
/
ubuntu.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
package ubuntu
import (
"bufio"
"context"
"os"
"slices"
"strings"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
fos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)
func init() {
analyzer.RegisterAnalyzer(&ubuntuOSAnalyzer{})
}
const (
version = 1
ubuntuConfFilePath = "etc/lsb-release"
)
var requiredFiles = []string{
ubuntuConfFilePath,
}
type ubuntuOSAnalyzer struct{}
func (a ubuntuOSAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
isUbuntu := false
scanner := bufio.NewScanner(input.Content)
for scanner.Scan() {
line := scanner.Text()
if line == "DISTRIB_ID=Ubuntu" {
isUbuntu = true
continue
}
if isUbuntu && strings.HasPrefix(line, "DISTRIB_RELEASE=") {
return &analyzer.AnalysisResult{
OS: types.OS{
Family: types.Ubuntu,
Name: strings.TrimSpace(line[16:]),
},
}, nil
}
}
return nil, xerrors.Errorf("ubuntu: %w", fos.AnalyzeOSError)
}
func (a ubuntuOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return slices.Contains(requiredFiles, filePath)
}
func (a ubuntuOSAnalyzer) Type() analyzer.Type {
return analyzer.TypeUbuntu
}
func (a ubuntuOSAnalyzer) Version() int {
return version
}