Debian Codegraph @

evaluator.go

raw
1 package imagebuilder
2
3 import (
4 "fmt"
5 "io"
6 "strings"
7
8 buildkitparser "github.com/moby/buildkit/frontend/dockerfile/parser"
9 "github.com/openshift/imagebuilder/dockerfile/command"
10 "github.com/openshift/imagebuilder/dockerfile/parser"
11 )
12
13 // ParseDockerfile parses the provided stream as a canonical Dockerfile
14 func ParseDockerfile(r io.Reader) (*parser.Node, error) {
15 result, err := parser.Parse(r)
16 if err != nil {
17 return nil, err
18 }
19 return result.AST, nil
20 }
21
22 // Environment variable interpolation will happen on these statements only.
23 var replaceEnvAllowed = map[string]bool{
24 command.Env: true,
25 command.Label: true,
26 command.Add: true,
27 command.Copy: true,
28 command.Workdir: true,
29 command.Expose: true,
30 command.Volume: true,
31 command.User: true,
32 command.StopSignal: true,
33 command.Arg: true,
34 }
35
36 // Certain commands are allowed to have their args split into more
37 // words after env var replacements. Meaning:
38 //
39 // ENV foo="123 456"
40 // EXPOSE $foo
41 //
42 // should result in the same thing as:
43 //
44 // EXPOSE 123 456
45 //
46 // and not treat "123 456" as a single word.
47 // Note that: EXPOSE "$foo" and EXPOSE $foo are not the same thing.
48 // Quotes will cause it to still be treated as single word.
49 var allowWordExpansion = map[string]bool{
50 command.Expose: true,
51 }
52
53 // Step represents the input Env and the output command after all
54 // post processing of the command arguments is done.
55 type Step struct {
56 Env []string
57
58 Command string
59 Args []string
60 Flags []string
61 Attrs map[string]bool
62 Message string
63 Heredocs []buildkitparser.Heredoc
64 Original string
65 }
66
67 // Resolve transforms a parsed Dockerfile line into a command to execute,
68 // resolving any arguments.
69 //
70 // Almost all nodes will have this structure:
71 // Child[Node, Node, Node] where Child is from parser.Node.Children and each
72 // node comes from parser.Node.Next. This forms a "line" with a statement and
73 // arguments and we process them in this normalized form by hitting
74 // evaluateTable with the leaf nodes of the command and the Builder object.
75 //
76 // ONBUILD is a special case; in this case the parser will emit:
77 // Child[Node, Child[Node, Node...]] where the first node is the literal
78 // "onbuild" and the child entrypoint is the command of the ONBUILD statement,
79 // such as `RUN` in ONBUILD RUN foo. There is special case logic in here to
80 // deal with that, at least until it becomes more of a general concern with new
81 // features.
82 func (b *Step) Resolve(ast *parser.Node) error {
83 b.Heredocs = ast.Heredocs
84 cmd := ast.Value
85 upperCasedCmd := strings.ToUpper(cmd)
86
87 // To ensure the user is given a decent error message if the platform
88 // on which the daemon is running does not support a builder command.
89 if err := platformSupports(strings.ToLower(cmd)); err != nil {
90 return err
91 }
92
93 attrs := ast.Attributes
94 original := ast.Original
95 flags := ast.Flags
96 strList := []string{}
97 msg := upperCasedCmd
98
99 if len(ast.Flags) > 0 {
100 msg += " " + strings.Join(ast.Flags, " ")
101 }
102
103 if cmd == "onbuild" {
104 if ast.Next == nil {
105 return fmt.Errorf("ONBUILD requires at least one argument")
106 }
107 ast = ast.Next.Children[0]
108 strList = append(strList, ast.Value)
109 msg += " " + ast.Value
110
111 if len(ast.Flags) > 0 {
112 msg += " " + strings.Join(ast.Flags, " ")
113 }
114
115 }
116
117 // count the number of nodes that we are going to traverse first
118 // so we can pre-create the argument and message array. This speeds up the
119 // allocation of those list a lot when they have a lot of arguments
120 cursor := ast
121 var n int
122 for cursor.Next != nil {
123 cursor = cursor.Next
124 n++
125 }
126 msgList := make([]string, n)
127
128 var i int
129 envs := b.Env
130 for ast.Next != nil {
131 ast = ast.Next
132 str := ast.Value
133 if replaceEnvAllowed[cmd] {
134 var err error
135 var words []string
136
137 if allowWordExpansion[cmd] {
138 words, err = ProcessWords(str, envs)
139 if err != nil {
140 return err
141 }
142 strList = append(strList, words...)
143 } else {
144 str, err = ProcessWord(str, envs)
145 if err != nil {
146 return err
147 }
148 strList = append(strList, str)
149 }
150 } else {
151 strList = append(strList, str)
152 }
153 msgList[i] = ast.Value
154 i++
155 }
156
157 msg += " " + strings.Join(msgList, " ")
158
159 b.Message = msg
160 b.Command = cmd
161 b.Args = strList
162 b.Original = original
163 b.Attrs = attrs
164 b.Flags = flags
165 return nil
166 }