-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathdocker.go
78 lines (64 loc) · 1.64 KB
/
docker.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
package dockerfile
import (
"context"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)
const version = 1
var requiredFiles = []string{"Dockerfile", "Containerfile"}
type ConfigAnalyzer struct {
filePattern *regexp.Regexp
}
func NewConfigAnalyzer(filePattern *regexp.Regexp) ConfigAnalyzer {
return ConfigAnalyzer{
filePattern: filePattern,
}
}
func (s ConfigAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
b, err := io.ReadAll(input.Content)
if err != nil {
return nil, xerrors.Errorf("failed to read %s: %w", input.FilePath, err)
}
return &analyzer.AnalysisResult{
Files: map[types.HandlerType][]types.File{
// It will be passed to misconfig post handler
types.MisconfPostHandler: {
{
Type: types.Dockerfile,
Path: input.FilePath,
Content: b,
},
},
},
}, nil
}
// Required does a case-insensitive check for filePath and returns true if
// filePath equals/startsWith/hasExtension requiredFiles
func (s ConfigAnalyzer) Required(filePath string, _ os.FileInfo) bool {
if s.filePattern != nil && s.filePattern.MatchString(filePath) {
return true
}
base := filepath.Base(filePath)
ext := filepath.Ext(base)
for _, file := range requiredFiles {
if strings.EqualFold(base, file+ext) {
return true
}
if strings.EqualFold(ext, "."+file) {
return true
}
}
return false
}
func (s ConfigAnalyzer) Type() analyzer.Type {
return analyzer.TypeDockerfile
}
func (s ConfigAnalyzer) Version() int {
return version
}