-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathdockerfile.go
136 lines (113 loc) · 3.61 KB
/
dockerfile.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
package buildinfo
import (
"context"
"os"
"path/filepath"
"strings"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/moby/buildkit/frontend/dockerfile/shell"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)
func init() {
analyzer.RegisterAnalyzer(&dockerfileAnalyzer{})
}
const dockerfileAnalyzerVersion = 1
// For Red Hat products
type dockerfileAnalyzer struct{}
func (a dockerfileAnalyzer) Analyze(_ context.Context, target analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
// ported from https://github.com/moby/buildkit/blob/b33357bcd2e3319b0323037c900c13b45a228df1/frontend/dockerfile/dockerfile2llb/convert.go#L73
dockerfile, err := parser.Parse(target.Content)
if err != nil {
return nil, xerrors.Errorf("dockerfile parse error: %w", err)
}
stages, metaArgs, err := instructions.Parse(dockerfile.AST)
if err != nil {
return nil, xerrors.Errorf("instruction parse error: %w", err)
}
var args []instructions.KeyValuePairOptional
for _, cmd := range metaArgs {
for _, metaArg := range cmd.Args {
args = append(args, setKVValue(metaArg, nil))
}
}
shlex := shell.NewLex(dockerfile.EscapeToken)
env := metaArgsToMap(args)
var component, arch string
for _, st := range stages {
for _, cmd := range st.Commands {
switch c := cmd.(type) {
case *instructions.EnvCommand:
for _, kvp := range c.Env {
env[kvp.Key] = kvp.Value
}
case *instructions.LabelCommand:
for _, kvp := range c.Labels {
key, err := shlex.ProcessWordWithMap(kvp.Key, env)
if err != nil {
return nil, xerrors.Errorf("unable to evaluate the label '%s': %w", kvp.Key, err)
}
key = strings.ToLower(key)
if key == "com.redhat.component" || key == "bzcomponent" {
component, err = shlex.ProcessWordWithMap(kvp.Value, env)
} else if key == "architecture" {
arch, err = shlex.ProcessWordWithMap(kvp.Value, env)
}
if err != nil {
return nil, xerrors.Errorf("failed to process the label '%s': %w", key, err)
}
}
}
}
}
if component == "" {
return nil, xerrors.New("no component found")
} else if arch == "" {
return nil, xerrors.New("no arch found")
}
return &analyzer.AnalysisResult{
BuildInfo: &types.BuildInfo{
Nvr: component + "-" + parseVersion(target.FilePath),
Arch: arch,
},
}, nil
}
func (a dockerfileAnalyzer) Required(filePath string, _ os.FileInfo) bool {
dir, file := filepath.Split(filepath.ToSlash(filePath))
if dir != "root/buildinfo/" {
return false
}
return strings.HasPrefix(file, "Dockerfile")
}
func (a dockerfileAnalyzer) Type() analyzer.Type {
return analyzer.TypeRedHatDockerfileType
}
func (a dockerfileAnalyzer) Version() int {
return dockerfileAnalyzerVersion
}
// parseVersion parses version from a file name
func parseVersion(nvr string) string {
releaseIndex := strings.LastIndex(nvr, "-")
if releaseIndex < 0 {
return ""
}
versionIndex := strings.LastIndex(nvr[:releaseIndex], "-")
version := nvr[versionIndex+1:]
return version
}
// https://github.com/moby/buildkit/blob/b33357bcd2e3319b0323037c900c13b45a228df1/frontend/dockerfile/dockerfile2llb/convert.go#L474-L482
func metaArgsToMap(metaArgs []instructions.KeyValuePairOptional) map[string]string {
m := map[string]string{}
for _, arg := range metaArgs {
m[arg.Key] = arg.ValueString()
}
return m
}
func setKVValue(kvpo instructions.KeyValuePairOptional, values map[string]string) instructions.KeyValuePairOptional {
if v, ok := values[kvpo.Key]; ok {
kvpo.Value = &v
}
return kvpo
}