-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
magefile.go
427 lines (371 loc) · 10.8 KB
/
magefile.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"
)
var (
GOPATH = os.Getenv("GOPATH")
GOBIN = filepath.Join(GOPATH, "bin")
ENV = map[string]string{
"CGO_ENABLED": "0",
}
)
func version() (string, error) {
if ver, err := sh.Output("git", "describe", "--tags", "--always"); err != nil {
return "", err
} else {
// Strips the v prefix from the tag
return strings.TrimPrefix(ver, "v"), nil
}
}
func buildLdflags() (string, error) {
ver, err := version()
if err != nil {
return "", err
}
return fmt.Sprintf("-s -w -X=github.com/aquasecurity/trivy/pkg/version.ver=%s", ver), nil
}
type Tool mg.Namespace
// Aqua installs aqua if not installed
func (Tool) Aqua() error {
if exists(filepath.Join(GOBIN, "aqua")) {
return nil
}
return sh.Run("go", "install", "github.com/aquaproj/aqua/v2/cmd/aqua@v2.2.1")
}
// Wire installs wire if not installed
func (Tool) Wire() error {
if installed("wire") {
return nil
}
return sh.Run("go", "install", "github.com/google/wire/cmd/wire@v0.5.0")
}
// GolangciLint installs golangci-lint
func (Tool) GolangciLint() error {
const version = "v1.54.2"
if exists(filepath.Join(GOBIN, "golangci-lint")) {
return nil
}
command := fmt.Sprintf("curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b %s %s", GOBIN, version)
return sh.Run("bash", "-c", command)
}
// Labeler installs labeler
func (Tool) Labeler() error {
if exists(filepath.Join(GOBIN, "labeler")) {
return nil
}
return sh.Run("go", "install", "github.com/knqyf263/labeler@latest")
}
// EasyJSON installs easyjson
func (Tool) EasyJSON() error {
if exists(filepath.Join(GOBIN, "easyjson")) {
return nil
}
return sh.Run("go", "install", "github.com/mailru/easyjson/...@v0.7.7")
}
// Kind installs kind cluster
func (Tool) Kind() error {
return sh.RunWithV(ENV, "go", "install", "sigs.k8s.io/kind@v0.19.0")
}
// Goyacc installs goyacc
func (Tool) Goyacc() error {
if exists(filepath.Join(GOBIN, "goyacc")) {
return nil
}
return sh.Run("go", "install", "golang.org/x/tools/cmd/goyacc@v0.7.0")
}
// Mockery installs mockery
func (Tool) Mockery() error {
if exists(filepath.Join(GOBIN, "mockery")) {
return nil
}
return sh.Run("go", "install", "github.com/knqyf263/mockery/cmd/mockery@latest")
}
// Wire generates the wire_gen.go file for each package
func Wire() error {
mg.Deps(Tool{}.Wire)
return sh.RunV("wire", "gen", "./pkg/commands/...", "./pkg/rpc/...", "./pkg/k8s/...")
}
// Mock generates mocks
func Mock(dir string) error {
mg.Deps(Tool{}.Mockery)
mockeryArgs := []string{
"-all",
"-inpkg",
"-case=snake",
"-dir",
dir,
}
return sh.RunV("mockery", mockeryArgs...)
}
// Protoc parses PROTO_FILES and generates the Go code for client/server mode
func Protoc() error {
// It is called in the protoc container
if _, ok := os.LookupEnv("TRIVY_PROTOC_CONTAINER"); ok {
protoFiles, err := findProtoFiles()
if err != nil {
return err
}
for _, file := range protoFiles {
// Check if the generated Go file is up-to-date
dst := strings.TrimSuffix(file, ".proto") + ".pb.go"
if updated, err := target.Path(dst, file); err != nil {
return err
} else if !updated {
continue
}
// Generate
if err = sh.RunV("protoc", "--twirp_out", ".", "--twirp_opt", "paths=source_relative",
"--go_out", ".", "--go_opt", "paths=source_relative", file); err != nil {
return err
}
}
return nil
}
// It is called on the host
if err := sh.RunV("bash", "-c", "docker build -t trivy-protoc - < Dockerfile.protoc"); err != nil {
return err
}
return sh.Run("docker", "run", "--rm", "-it", "--platform", "linux/x86_64", "-v", "${PWD}:/app", "-w", "/app", "trivy-protoc", "mage", "protoc")
}
// Yacc generates parser
func Yacc() error {
mg.Deps(Tool{}.Goyacc)
return sh.Run("go", "generate", "./pkg/licensing/expression/...")
}
// Easyjson generates JSON marshaler/unmarshaler for TinyGo/WebAssembly as TinyGo doesn't support encoding/json.
func Easyjson() error {
mg.Deps(Tool{}.EasyJSON)
return sh.Run("easyjson", "./pkg/module/serialize/types.go")
}
type Test mg.Namespace
// FixtureContainerImages downloads and extracts required images
func (Test) FixtureContainerImages() error {
return fixtureContainerImages()
}
// FixtureVMImages downloads and extracts required VM images
func (Test) FixtureVMImages() error {
return fixtureVMImages()
}
// GenerateModules compiles WASM modules for unit tests
func (Test) GenerateModules() error {
pattern := filepath.Join("pkg", "module", "testdata", "*", "*.go")
if err := compileWasmModules(pattern); err != nil {
return err
}
return nil
}
// GenerateExampleModules compiles example Wasm modules for integration tests
func (Test) GenerateExampleModules() error {
pattern := filepath.Join("examples", "module", "*", "*.go")
if err := compileWasmModules(pattern); err != nil {
return err
}
return nil
}
// UpdateGolden updates golden files for integration tests
func (Test) UpdateGolden() error {
return sh.RunWithV(ENV, "go", "test", "-tags=integration", "./integration/...", "./pkg/fanal/test/integration/...", "-update")
}
func compileWasmModules(pattern string) error {
goFiles, err := filepath.Glob(pattern)
if err != nil {
return err
}
for _, src := range goFiles {
// e.g. examples/module/spring4shell/spring4shell.go
// => examples/module/spring4shell/spring4shell.wasm
dst := strings.TrimSuffix(src, ".go") + ".wasm"
if updated, err := target.Path(dst, src); err != nil {
return err
} else if !updated {
continue
}
// Check if TinyGo is installed
if !installed("tinygo") {
return errors.New("need to install TinyGo, follow https://tinygo.org/getting-started/install/")
}
if err = sh.Run("go", "generate", src); err != nil {
return err
}
}
return nil
}
// Unit runs unit tests
func (t Test) Unit() error {
mg.Deps(t.GenerateModules)
return sh.RunWithV(ENV, "go", "test", "-v", "-short", "-coverprofile=coverage.txt", "-covermode=atomic", "./...")
}
// Integration runs integration tests
func (t Test) Integration() error {
mg.Deps(t.FixtureContainerImages)
return sh.RunWithV(ENV, "go", "test", "-timeout", "15m", "-v", "-tags=integration", "./integration/...", "./pkg/fanal/test/integration/...")
}
// K8s runs k8s integration tests
func (t Test) K8s() error {
mg.Deps(Tool{}.Kind)
err := sh.RunWithV(ENV, "kind", "create", "cluster", "--name", "kind-test")
if err != nil {
return err
}
defer func() {
_ = sh.RunWithV(ENV, "kind", "delete", "cluster", "--name", "kind-test")
}()
err = sh.RunWithV(ENV, "kubectl", "apply", "-f", "./integration/testdata/fixtures/k8s/test_nginx.yaml")
if err != nil {
return err
}
return sh.RunWithV(ENV, "go", "test", "-v", "-tags=k8s_integration", "./integration/...")
}
// Module runs Wasm integration tests
func (t Test) Module() error {
mg.Deps(t.FixtureContainerImages, t.GenerateExampleModules)
return sh.RunWithV(ENV, "go", "test", "-v", "-tags=module_integration", "./integration/...")
}
// UpdateModuleGolden updates golden files for Wasm integration tests
func (t Test) UpdateModuleGolden() error {
mg.Deps(t.FixtureContainerImages, t.GenerateExampleModules)
return sh.RunWithV(ENV, "go", "test", "-v", "-tags=module_integration", "./integration/...", "-update")
}
// VM runs VM integration tests
func (t Test) VM() error {
mg.Deps(t.FixtureVMImages)
return sh.RunWithV(ENV, "go", "test", "-v", "-tags=vm_integration", "./integration/...")
}
// UpdateVMGolden updates golden files for integration tests
func (t Test) UpdateVMGolden() error {
mg.Deps(t.FixtureVMImages)
return sh.RunWithV(ENV, "go", "test", "-v", "-tags=vm_integration", "./integration/...", "-update")
}
type Lint mg.Namespace
// Run runs linters
func (Lint) Run() error {
mg.Deps(Tool{}.GolangciLint)
return sh.RunV("golangci-lint", "run", "--timeout", "5m")
}
// Fix auto fixes linters
func (Lint) Fix() error {
mg.Deps(Tool{}.GolangciLint)
return sh.RunV("golangci-lint", "run", "--timeout", "5m", "--fix")
}
// Fmt formats Go code and proto files
func Fmt() error {
// Check if clang-format is installed
if !installed("clang-format") {
return errors.New("need to install clang-format")
}
// Format proto files
protoFiles, err := findProtoFiles()
if err != nil {
return err
}
for _, file := range protoFiles {
if err = sh.Run("clang-format", "-i", file); err != nil {
return err
}
}
// Format Go code
return sh.Run("go", "fmt", "./...")
}
// Tidy makes sure go.mod matches the source code in the module
func Tidy() error {
return sh.RunV("go", "mod", "tidy")
}
// Build builds Trivy
func Build() error {
if updated, err := target.Dir("trivy", "pkg", "cmd"); err != nil {
return err
} else if !updated {
return nil
}
ldflags, err := buildLdflags()
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
return sh.RunWith(ENV, "go", "build", "-ldflags", ldflags, filepath.Join(wd, "cmd", "trivy"))
}
// Install installs Trivy
func Install() error {
ldflags, err := buildLdflags()
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
return sh.RunWith(ENV, "go", "install", "-ldflags", ldflags, filepath.Join(wd, "cmd", "trivy"))
}
// Clean cleans up the fixtures
func Clean() error {
fixtureDir := filepath.Join("integration", "testdata", "fixtures")
paths := []string{
filepath.Join(fixtureDir, "images"),
filepath.Join(fixtureDir, "vm-images"),
}
for _, p := range paths {
if err := sh.Rm(p); err != nil {
return err
}
}
return nil
}
// Label updates labels
func Label() error {
mg.Deps(Tool{}.Labeler)
return sh.RunV("labeler", "apply", "misc/triage/labels.yaml", "-l", "5")
}
type Docs mg.Namespace
// Serve launches MkDocs development server to preview the documentation page
func (Docs) Serve() error {
const (
mkdocsImage = "aquasec/mkdocs-material:dev"
mkdocsPort = "8000"
)
if err := sh.Run("docker", "build", "-t", mkdocsImage, "-f", "docs/build/Dockerfile", "docs/build"); err != nil {
return err
}
return sh.Run("docker", "run", "--name", "mkdocs-serve", "--rm", "-v", "${PWD}:/docs", "-p", mkdocsPort+":8000", mkdocsImage)
}
// Generate generates CLI references
func (Docs) Generate() error {
return sh.RunWith(ENV, "go", "run", "-tags=mage_docs", "./magefiles")
}
func findProtoFiles() ([]string, error) {
var files []string
err := filepath.WalkDir("rpc", func(path string, d fs.DirEntry, err error) error {
switch {
case err != nil:
return err
case d.IsDir():
return nil
case filepath.Ext(path) == ".proto":
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
func exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func installed(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}