This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
analyzer.go
245 lines (208 loc) · 6.17 KB
/
analyzer.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package analyzer
import (
"context"
"os"
"sort"
"strings"
"sync"
"golang.org/x/sync/semaphore"
"golang.org/x/xerrors"
aos "github.com/aquasecurity/fanal/analyzer/os"
"github.com/aquasecurity/fanal/log"
"github.com/aquasecurity/fanal/types"
)
var (
analyzers = map[Type]analyzer{}
configAnalyzers = map[Type]configAnalyzer{}
// ErrUnknownOS occurs when unknown OS is analyzed.
ErrUnknownOS = xerrors.New("unknown OS")
// ErrPkgAnalysis occurs when the analysis of packages is failed.
ErrPkgAnalysis = xerrors.New("failed to analyze packages")
// ErrNoPkgsDetected occurs when the required files for an OS package manager are not detected
ErrNoPkgsDetected = xerrors.New("no packages detected")
)
type AnalysisTarget struct {
Dir string
FilePath string
Content []byte
}
type analyzer interface {
Type() Type
Version() int
Analyze(input AnalysisTarget) (*AnalysisResult, error)
Required(filePath string, info os.FileInfo) bool
}
type configAnalyzer interface {
Type() Type
Version() int
Analyze(targetOS types.OS, content []byte) ([]types.Package, error)
Required(osFound types.OS) bool
}
func RegisterAnalyzer(analyzer analyzer) {
analyzers[analyzer.Type()] = analyzer
}
func RegisterConfigAnalyzer(analyzer configAnalyzer) {
configAnalyzers[analyzer.Type()] = analyzer
}
type Opener func() ([]byte, error)
type AnalysisResult struct {
m sync.Mutex
OS *types.OS
PackageInfos []types.PackageInfo
Applications []types.Application
Configs []types.Config
SystemInstalledFiles []string // A list of files installed by OS package manager
}
func (r *AnalysisResult) isEmpty() bool {
return r.OS == nil && len(r.PackageInfos) == 0 && len(r.Applications) == 0 &&
len(r.Configs) == 0 && len(r.SystemInstalledFiles) == 0
}
func (r *AnalysisResult) Sort() {
sort.Slice(r.PackageInfos, func(i, j int) bool {
return r.PackageInfos[i].FilePath < r.PackageInfos[j].FilePath
})
for _, pi := range r.PackageInfos {
sort.Slice(pi.Packages, func(i, j int) bool {
return pi.Packages[i].Name < pi.Packages[j].Name
})
}
sort.Slice(r.Applications, func(i, j int) bool {
return r.Applications[i].FilePath < r.Applications[j].FilePath
})
for _, app := range r.Applications {
sort.Slice(app.Libraries, func(i, j int) bool {
if app.Libraries[i].Library.Name != app.Libraries[j].Library.Name {
return app.Libraries[i].Library.Name < app.Libraries[j].Library.Name
}
return app.Libraries[i].Library.Version < app.Libraries[j].Library.Version
})
}
}
func (r *AnalysisResult) Merge(new *AnalysisResult) {
if new == nil || new.isEmpty() {
return
}
// this struct is accessed by multiple goroutines
r.m.Lock()
defer r.m.Unlock()
if new.OS != nil {
// OLE also has /etc/redhat-release and it detects OLE as RHEL by mistake.
// In that case, OS must be overwritten with the content of /etc/oracle-release.
// There is the same problem between Debian and Ubuntu.
if r.OS == nil || r.OS.Family == aos.RedHat || r.OS.Family == aos.Debian {
r.OS = new.OS
}
}
if len(new.PackageInfos) > 0 {
r.PackageInfos = append(r.PackageInfos, new.PackageInfos...)
}
if len(new.Applications) > 0 {
r.Applications = append(r.Applications, new.Applications...)
}
for _, m := range new.Configs {
r.Configs = append(r.Configs, m)
}
r.SystemInstalledFiles = append(r.SystemInstalledFiles, new.SystemInstalledFiles...)
}
type Analyzer struct {
drivers []analyzer
configDrivers []configAnalyzer
disabledAnalyzers []Type
}
func NewAnalyzer(disabledAnalyzers []Type) Analyzer {
var drivers []analyzer
for analyzerType, a := range analyzers {
if isDisabled(analyzerType, disabledAnalyzers) {
continue
}
drivers = append(drivers, a)
}
var configDrivers []configAnalyzer
for analyzerType, a := range configAnalyzers {
if isDisabled(analyzerType, disabledAnalyzers) {
continue
}
configDrivers = append(configDrivers, a)
}
return Analyzer{
drivers: drivers,
configDrivers: configDrivers,
disabledAnalyzers: disabledAnalyzers,
}
}
// AnalyzerVersions returns analyzer version identifier used for cache keys.
func (a Analyzer) AnalyzerVersions() map[string]int {
versions := map[string]int{}
for analyzerType, aa := range analyzers {
if isDisabled(analyzerType, a.disabledAnalyzers) {
versions[string(analyzerType)] = 0
continue
}
versions[string(analyzerType)] = aa.Version()
}
return versions
}
// ImageConfigAnalyzerVersions returns analyzer version identifier used for cache keys.
func (a Analyzer) ImageConfigAnalyzerVersions() map[string]int {
versions := map[string]int{}
for _, ca := range configAnalyzers {
if isDisabled(ca.Type(), a.disabledAnalyzers) {
versions[string(ca.Type())] = 0
continue
}
versions[string(ca.Type())] = ca.Version()
}
return versions
}
func (a Analyzer) AnalyzeFile(ctx context.Context, wg *sync.WaitGroup, limit *semaphore.Weighted, result *AnalysisResult,
dir, filePath string, info os.FileInfo, opener Opener) error {
if info.IsDir() {
return nil
}
for _, d := range a.drivers {
// filepath extracted from tar file doesn't have the prefix "/"
if !d.Required(strings.TrimLeft(filePath, "/"), info) {
continue
}
b, err := opener()
if err != nil {
return xerrors.Errorf("unable to open a file (%s): %w", filePath, err)
}
if err = limit.Acquire(ctx, 1); err != nil {
return xerrors.Errorf("semaphore acquire: %w", err)
}
wg.Add(1)
go func(a analyzer, target AnalysisTarget) {
defer limit.Release(1)
defer wg.Done()
ret, err := a.Analyze(target)
if err != nil && !xerrors.Is(err, aos.AnalyzeOSError) {
log.Logger.Debugf("Analysis error: %s", err)
return
}
result.Merge(ret)
}(d, AnalysisTarget{Dir: dir, FilePath: filePath, Content: b})
}
return nil
}
func (a Analyzer) AnalyzeImageConfig(targetOS types.OS, configBlob []byte) []types.Package {
for _, d := range a.configDrivers {
if !d.Required(targetOS) {
continue
}
pkgs, err := d.Analyze(targetOS, configBlob)
if err != nil {
continue
}
return pkgs
}
return nil
}
func isDisabled(t Type, disabled []Type) bool {
for _, d := range disabled {
if t == d {
return true
}
}
return false
}