-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
367 lines (297 loc) · 8.81 KB
/
runner.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package conflint
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
yaml "gopkg.in/yaml.v3"
)
type Runner struct {
Output io.Writer
ConfigFile string
Errformat string
WorkDir string
Delim string
LogLevel string
}
type Config struct {
Conftest []ConftestConfig `yaml:"conftest"`
Kubeval []KubevalConfig `yaml:"kubeval"`
}
type ConftestConfig struct {
Files []string `yaml:"files"`
Policy string `yaml:"policy"`
Input string `yaml:"input"`
Combine bool `yaml:"combine"`
FailOnWarn bool `yaml:"failOnWarn"`
Data []string `yaml:"data"`
AllNamespaces bool `yaml:"allNamespaces`
Namespaces []string `yaml:"namespaces"`
}
type KubevalConfig struct {
Files []string `yaml:"files"`
Strict bool `yaml:"strict"`
SchemaLocations []string `yaml:"schemaLocations"`
IgnoreMissingSchemas bool `yaml:"ignoreMissingSchemas"`
IgnoredFilenamePatterns []string `yaml:"ignoredFilenamePatterns`
SkipKinds []string `yaml:"skipKinds"`
}
type KubevalOutput = []KubevalFileResult
type KubevalFileResult struct {
Filename string `yaml:"filename"`
Kind string `yaml:"kind"`
Status string `yaml:"status"`
Errors []string `yaml:"errors"`
}
type ConftestOutput = []ConftestFileResult
type ConftestFileResult struct {
Filename string `yaml:"filename"`
Warnings []ConftestResult `yaml:"warnings"`
Failures []ConftestResult `yaml:"failures"`
}
type ConftestResult struct {
Msg string `yaml:"msg"`
}
func (r *Runner) Run() error {
var config Config
file := filepath.Join(r.WorkDir, r.ConfigFile)
bs, err := ioutil.ReadFile(file)
if err != nil {
return err
}
if err := yaml.Unmarshal(bs, &config); err != nil {
return err
}
var output int
if len(config.Conftest) > 0 {
_, err := exec.LookPath("conftest")
if err != nil {
return fmt.Errorf("looking for executable: \"conftest\" not found in PATH")
}
}
for _, ct := range config.Conftest {
for _, fp := range ct.Files {
files, err := filepath.Glob(filepath.Join(r.WorkDir, fp))
if err != nil {
return fmt.Errorf("searching files matching %s: %w", fp, err)
}
var fs []string
for _, f := range files {
f = strings.TrimPrefix(f, r.WorkDir)
f = strings.TrimPrefix(f, "/")
fs = append(fs, f)
}
args := []string{"test"}
args = append(args, fs...)
args = append(args, "-p", ct.Policy, "-o", "json")
if ct.Input != "" {
args = append(args, "-i", ct.Input)
}
if ct.Combine {
args = append(args, "--combine")
}
if ct.AllNamespaces {
args = append(args, "--all-namespaces")
}
if len(ct.Data) > 0 {
args = append(args, "--data", strings.Join(ct.Data, ","))
}
if len(ct.Namespaces) > 0 {
args = append(args, "--namespace", strings.Join(ct.Namespaces, ","))
}
cmd := exec.Command("conftest", args...)
cmd.Dir = r.WorkDir
out, err := cmd.CombinedOutput()
if err != nil && r.LogLevel == "DEBUG" {
fmt.Fprintf(os.Stderr, "DEBUG: running conftest %s: %v\n", strings.Join(args, " "), err)
}
var conftestOut ConftestOutput
if err := yaml.Unmarshal(out, &conftestOut); err != nil {
return err
}
for _, res := range conftestOut {
handle := func(msg string) error {
sub := strings.SplitN(msg, r.Delim, 2)
if len(sub) > 1 {
line, col, err := getLineColFromJsonpathExpr(filepath.Join(r.WorkDir, res.Filename), "$."+sub[0])
if err != nil {
return fmt.Errorf("processing %s: %w", sub[0], err)
}
if err := r.Print(res.Filename, line, col, sub[1]); err != nil {
return fmt.Errorf("printing %s: %w", sub[1], err)
}
output++
} else {
log.Printf("ignoring unsupported output: %s", msg)
}
return nil
}
for _, f := range res.Failures {
if err := handle(f.Msg); err != nil {
return err
}
}
}
}
}
if len(config.Kubeval) > 0 {
_, err := exec.LookPath("kubeval")
if err != nil {
return fmt.Errorf("looking for executable: \"kubeval\" not found in PATH")
}
}
for _, ke := range config.Kubeval {
for _, fp := range ke.Files {
files, err := filepath.Glob(filepath.Join(r.WorkDir, fp))
if err != nil {
return fmt.Errorf("searching files matching %s: %w", fp, err)
}
for _, f := range files {
f = strings.TrimPrefix(f, r.WorkDir)
f = strings.TrimPrefix(f, "/")
args := []string{f, "-o", "json"}
if ke.Strict {
args = append(args, "--strict")
}
if ke.IgnoreMissingSchemas {
args = append(args, "--ignore-missing-schemas")
}
if len(ke.IgnoredFilenamePatterns) > 0 {
args = append(args, "--ignored-filename-patterns", strings.Join(ke.IgnoredFilenamePatterns, ","))
}
if len(ke.SkipKinds) > 0 {
args = append(args, "--skip-kinds", strings.Join(ke.SkipKinds, ","))
}
if len(ke.SchemaLocations) > 0 {
args = append(args, "--schema-location", ke.SchemaLocations[0])
if len(ke.SchemaLocations) > 1 {
args = append(args, "--additional-schema-locations", strings.Join(ke.SchemaLocations[1:], ","))
}
}
cmd := exec.Command("kubeval", args...)
cmd.Dir = r.WorkDir
out, err := cmd.CombinedOutput()
if err != nil && r.LogLevel == "DEBUG" {
fmt.Fprintf(os.Stderr, "DEBUG: running kubeval %s: %v\n", strings.Join(args, " "), err)
}
var effectiveLines []string
allLines := bufio.NewScanner(bytes.NewReader(out))
for allLines.Scan() {
line := allLines.Text()
if strings.HasPrefix(line, "WARN - Set to ignore missing schemas") {
} else {
effectiveLines = append(effectiveLines, line)
}
}
var conftestOut KubevalOutput
jsonDocText := []byte(strings.Join(effectiveLines, "\n"))
if err := yaml.Unmarshal(jsonDocText, &conftestOut); err != nil {
fmt.Fprintf(os.Stderr, "kubeeval failed with output:\n%s", string(out))
return fmt.Errorf("unmarshalling yaml: %w", err)
}
for _, res := range conftestOut {
handle := func(msg string) error {
sub := strings.SplitN(msg, ": ", 2)
if len(sub) > 1 {
line, col, err := getLineColFromJsonpathExpr(filepath.Join(r.WorkDir, f), "$."+sub[0])
if err != nil {
return fmt.Errorf("processing %s: %w", sub[0], err)
}
if err := r.Print(f, line, col, sub[1]); err != nil {
return fmt.Errorf("printing %s: %w", sub[1], err)
}
output++
} else {
log.Printf("ignoring unsupported output: %s", msg)
}
return nil
}
for _, f := range res.Errors {
if err := handle(f); err != nil {
return err
}
}
}
}
}
}
if output > 0 {
var word string
if output > 1 {
word = "errors"
} else {
word = "error"
}
return fmt.Errorf("found %d linter %s", output, word)
}
return nil
}
func getLineColFromJsonpathExpr(file string, jsonpathExpr string) (int, int, error) {
if jsonpathExpr[0] != '$' {
return 0, 0, fmt.Errorf("Expression must start with $, but got: %s", jsonpathExpr)
}
path, err := parseJsonpath(jsonpathExpr)
if err != nil {
return 0, 0, fmt.Errorf("parsing jsonpath %s: %w", jsonpathExpr, err)
}
f, err := os.Open(file)
if err != nil {
return 0, 0, fmt.Errorf("opening file %s: %w", file, err)
}
defer f.Close()
dec := yaml.NewDecoder(f)
next := func() (*yaml.Node, error) {
doc := &yaml.Node{}
if err := dec.Decode(doc); err != nil {
if err == io.EOF {
return nil, nil
}
return nil, fmt.Errorf("decoding yaml from %s: %w", file, err)
}
if doc.Kind != yaml.DocumentNode {
panic(fmt.Errorf("the top-level yaml node must be a document node. got %v", doc.Kind))
}
node := doc.Content[0]
if node.Kind != yaml.MappingNode {
panic(fmt.Errorf("the only yaml node in a document must be a mapping node. got %v", node.Kind))
}
got, err := path.Get(node)
if err != nil {
return nil, fmt.Errorf("getting node at %s: %w", jsonpathExpr, err)
}
return got, nil
}
var lastErr error
for {
node, err := next()
if node != nil {
return node.Line, node.Column, nil
}
if err == nil {
break
}
lastErr = err
}
if lastErr != nil {
return 0, 0, fmt.Errorf("getting line and column numbers from %s: %w", file, lastErr)
}
return 0, 0, fmt.Errorf("gettling line and colum numbers from %s: no value found at %s", file, jsonpathExpr)
}
func (r *Runner) Print(file string, line, col int, msg string) error {
// TODO maybe use https://github.com/phayes/checkstyle for additional checkstyle xml output?
replacer := strings.NewReplacer("%m", msg, "%f", file, "%l", fmt.Sprintf("%d", line), "%c", fmt.Sprintf("%d", col))
printed := replacer.Replace(r.Errformat)
if _, err := r.Output.Write([]byte(printed)); err != nil {
return err
}
if _, err := r.Output.Write([]byte("\n")); err != nil {
return err
}
return nil
}