-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathconfig.go
65 lines (53 loc) · 1.82 KB
/
config.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
package config
import (
"context"
"os"
"path/filepath"
"golang.org/x/xerrors"
"k8s.io/utils/strings/slices"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/misconf"
)
var (
_ analyzer.PostAnalyzer = (*Analyzer)(nil)
requiredExts = []string{".json", ".yaml", ".yml", ".tfvars"}
)
// Analyzer represents an analyzer for config files,
// which is embedded into each config analyzer such as Kubernetes.
type Analyzer struct {
typ analyzer.Type
version int
scanner *misconf.Scanner
}
type NewScanner func([]string, misconf.ScannerOption) (*misconf.Scanner, error)
func NewAnalyzer(t analyzer.Type, version int, newScanner NewScanner, opts analyzer.AnalyzerOptions) (*Analyzer, error) {
s, err := newScanner(opts.FilePatterns, opts.MisconfScannerOption)
if err != nil {
return nil, xerrors.Errorf("%s scanner init error: %w", t, err)
}
return &Analyzer{
typ: t,
version: version,
scanner: s,
}, nil
}
// PostAnalyze performs configuration analysis on the input filesystem and detect misconfigurations.
func (a *Analyzer) PostAnalyze(ctx context.Context, input analyzer.PostAnalysisInput) (*analyzer.AnalysisResult, error) {
misconfs, err := a.scanner.Scan(ctx, input.FS)
if err != nil {
return nil, xerrors.Errorf("%s scan error: %w", a.typ, err)
}
return &analyzer.AnalysisResult{Misconfigurations: misconfs}, nil
}
// Required checks if the given file path has one of the required file extensions.
func (a *Analyzer) Required(filePath string, _ os.FileInfo) bool {
return slices.Contains(requiredExts, filepath.Ext(filePath))
}
// Type returns the analyzer type of the current Analyzer instance.
func (a *Analyzer) Type() analyzer.Type {
return a.typ
}
// Version returns the version of the current Analyzer instance.
func (a *Analyzer) Version() int {
return a.version
}