-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
mariner.go
67 lines (56 loc) · 1.43 KB
/
mariner.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
package mariner
import (
"bufio"
"context"
"io"
"os"
"path/filepath"
"strings"
"golang.org/x/xerrors"
"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(&marinerOSAnalyzer{})
}
const (
version = 1
requiredFile = "etc/mariner-release"
)
type marinerOSAnalyzer struct{}
func (a marinerOSAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
foundOS, err := a.parseRelease(input.Content)
if err != nil {
return nil, xerrors.Errorf("release parse error: %w", err)
}
return &analyzer.AnalysisResult{
OS: foundOS,
}, nil
}
func (a marinerOSAnalyzer) parseRelease(r io.Reader) (types.OS, error) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) != 2 {
continue
}
if strings.ToLower(fields[0]) == "cbl-mariner" {
return types.OS{
Family: aos.CBLMariner,
Name: fields[1],
}, nil
}
}
return types.OS{}, xerrors.Errorf("cbl-mariner: %w", aos.AnalyzeOSError)
}
func (a marinerOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return filepath.ToSlash(filePath) == requiredFile
}
func (a marinerOSAnalyzer) Type() analyzer.Type {
return analyzer.TypeCBLMariner
}
func (a marinerOSAnalyzer) Version() int {
return version
}