-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathesm.go
79 lines (65 loc) · 1.65 KB
/
esm.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
package ubuntu
import (
"context"
"encoding/json"
"os"
"slices"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)
func init() {
analyzer.RegisterAnalyzer(&ubuntuESMAnalyzer{})
}
const (
ESMAnalyzerVersion = 1
esmConfFilePath = "var/lib/ubuntu-advantage/status.json"
esmServiceName = "esm-infra"
esmStatusEnabled = "enabled"
)
var ESMRequiredFiles = []string{
esmConfFilePath,
}
type ubuntuESMAnalyzer struct{}
func (a ubuntuESMAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
st := status{}
err := json.NewDecoder(input.Content).Decode(&st)
if err != nil {
return nil, xerrors.Errorf("ubuntu ESM analyze error: %w", err)
}
if esmEnabled(st) {
return &analyzer.AnalysisResult{
OS: types.OS{
Family: types.Ubuntu,
Extended: true,
},
}, nil
}
// if ESM is disabled - return nil to reduce the amount of logic in the OS.Merge function
return nil, nil
}
func (a ubuntuESMAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return slices.Contains(ESMRequiredFiles, filePath)
}
func (a ubuntuESMAnalyzer) Type() analyzer.Type {
return analyzer.TypeUbuntuESM
}
func (a ubuntuESMAnalyzer) Version() int {
return ESMAnalyzerVersion
}
// structs to parse ESM status
type status struct {
Services []service `json:"services"`
}
type service struct {
Name string `json:"name"`
Status string `json:"status"`
}
func esmEnabled(st status) bool {
for _, s := range st.Services { // Find ESM Service
if s.Name == esmServiceName && s.Status == esmStatusEnabled {
return true
}
}
return false
}