Skip to content

Commit 529957e

Browse files
authored
feat: replace TinyGo with standard Go for WebAssembly modules (#8496)
1 parent fe09410 commit 529957e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+337
-521
lines changed

.github/workflows/cache-test-images.yaml

+2-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ jobs:
5757
go-version-file: go.mod
5858
cache: false
5959

60-
- name: Install tools
61-
uses: aquaproj/[email protected]
62-
with:
63-
aqua_version: v1.25.0
60+
- name: Install Go tools
61+
run: go install tool # GOBIN is added to the PATH by the setup-go action
6462

6563
- name: Generate image list digest
6664
if: github.ref_name == 'main'

.github/workflows/test.yaml

+2-8
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ jobs:
5151
if: ${{ failure() && steps.lint.conclusion == 'failure' }}
5252

5353
- name: Install tools
54-
uses: aquaproj/[email protected]
55-
with:
56-
aqua_version: v1.25.0
57-
aqua_opts: ""
54+
run: go install tool # GOBIN is added to the PATH by the setup-go action
5855

5956
- name: Check if CLI references are up-to-date
6057
run: |
@@ -136,10 +133,7 @@ jobs:
136133
cache: false
137134

138135
- name: Install tools
139-
uses: aquaproj/[email protected]
140-
with:
141-
aqua_version: v1.25.0
142-
aqua_opts: ""
136+
run: go install tool # GOBIN is added to the PATH by the setup-go action
143137

144138
- name: Generate image list digest
145139
id: image-digest

aqua.yaml

-10
This file was deleted.

docs/docs/advanced/modules.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ They provide a way to extend the core feature set of Trivy, but without updating
1212

1313
- They can be added and removed from a Trivy installation without impacting the core Trivy tool.
1414
- They can be written in any programming language supporting WebAssembly.
15-
- It supports only [TinyGo][tinygo] at the moment.
15+
- It supports only Go at the moment.
1616

1717
You can write your own detection logic.
1818

@@ -94,9 +94,9 @@ $ trivy module uninstall ghcr.io/aquasecurity/trivy-module-spring4shell
9494
```
9595

9696
## Building Modules
97-
It supports TinyGo only at the moment.
97+
It supports Go only at the moment.
9898

99-
### TinyGo
99+
### Go
100100
Trivy provides Go SDK including three interfaces.
101101
Your own module needs to implement either or both `Analyzer` and `PostScanner` in addition to `Module`.
102102

@@ -113,7 +113,7 @@ type Analyzer interface {
113113

114114
type PostScanner interface {
115115
PostScanSpec() serialize.PostScanSpec
116-
PostScan(serialize.Results) (serialize.Results, error)
116+
PostScan(types.Results) (types.Results, error)
117117
}
118118
```
119119

@@ -142,6 +142,9 @@ const (
142142
name = "wordpress-module"
143143
)
144144

145+
// main is required for Go to compile the Wasm module
146+
func main() {}
147+
145148
type WordpressModule struct{
146149
// Cannot define fields as modules can't keep state.
147150
}
@@ -203,7 +206,7 @@ func (WordpressModule) Analyze(filePath string) (*serialize.AnalysisResult, erro
203206
}
204207

205208
return &serialize.AnalysisResult{
206-
CustomResources: []serialize.CustomResource{
209+
CustomResources: []ftypes.CustomResource{
207210
{
208211
Type: typeWPVersion,
209212
FilePath: filePath,
@@ -246,7 +249,7 @@ func (WordpressModule) PostScanSpec() serialize.PostScanSpec {
246249
}
247250
}
248251

249-
func (WordpressModule) PostScan(results serialize.Results) (serialize.Results, error) {
252+
func (WordpressModule) PostScan(results types.Results) (types.Results, error) {
250253
// e.g. results
251254
// [
252255
// {
@@ -288,7 +291,7 @@ func (WordpressModule) PostScan(results serialize.Results) (serialize.Results, e
288291

289292
if vulnerable {
290293
// Add CVE-2020-36326
291-
results = append(results, serialize.Result{
294+
results = append(results, types.Result{
292295
Target: wpPath,
293296
Class: types.ClassLangPkg,
294297
Type: "wordpress",
@@ -318,10 +321,10 @@ In the `Delete` action, `PostScan` needs to return results you want to delete.
318321
If `PostScan` returns an empty, Trivy will not delete anything.
319322

320323
#### Build
321-
Follow [the install guide][tinygo-installation] and install TinyGo.
324+
Follow [the install guide][go-installation] and install Go.
322325

323326
```bash
324-
$ tinygo build -o wordpress.wasm -scheduler=none -target=wasi --no-debug wordpress.go
327+
$ GOOS=wasip1 GOARCH=wasm go build -o wordpress.wasm -buildmode=c-shared wordpress.go
325328
```
326329

327330
Put the built binary to the module directory that is under the home directory by default.
@@ -347,12 +350,11 @@ Digest: sha256:6416d0199d66ce52ced19f01d75454b22692ff3aa7737e45f7a189880840424f
347350

348351
[regexp]: https://github.com/google/re2/wiki/Syntax
349352

350-
[tinygo]: https://tinygo.org/
351353
[spring4shell]: https://blog.aquasec.com/zero-day-rce-vulnerability-spring4shell
352354
[wazero]: https://github.com/tetratelabs/wazero
353355

354356
[trivy-module-spring4shell]: https://github.com/aquasecurity/trivy/tree/main/examples/module/spring4shell
355357
[trivy-module-wordpress]: https://github.com/aquasecurity/trivy-module-wordpress
356358

357-
[tinygo-installation]: https://tinygo.org/getting-started/install/
359+
[go-installation]: https://go.dev/doc/install
358360
[oras]: https://oras.land/cli/

examples/module/spring4shell/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This module provides a more in-depth investigation of Spring4Shell detection.
55
## Set up
66

77
```
8-
$ tinygo build -o spring4shell.wasm -scheduler=none -target=wasi --no-debug spring4shell.go
8+
$ GOOS=wasip1 GOARCH=wasm go build -o spring4shell.wasm -buildmode=c-shared spring4shell.go
99
$ mkdir -p ~/.trivy/modules
1010
$ cp spring4shell.wasm ~/.trivy/modules
1111
```

examples/module/spring4shell/spring4shell.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:generate tinygo build -o spring4shell.wasm -target=wasip1 --buildmode=c-shared spring4shell.go
2-
//go:build tinygo.wasm
1+
//go:generate go build -o spring4shell.wasm -buildmode=c-shared spring4shell.go
2+
//go:build wasip1
33

44
package main
55

@@ -13,9 +13,11 @@ import (
1313
"strconv"
1414
"strings"
1515

16+
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
1617
"github.com/aquasecurity/trivy/pkg/module/api"
1718
"github.com/aquasecurity/trivy/pkg/module/serialize"
1819
"github.com/aquasecurity/trivy/pkg/module/wasm"
20+
"github.com/aquasecurity/trivy/pkg/types"
1921
)
2022

2123
const (
@@ -29,6 +31,9 @@ var (
2931
tomcatVersionRegex = regexp.MustCompile(`Apache Tomcat Version ([\d.]+)`)
3032
)
3133

34+
// main is required for Go to compile the Wasm module
35+
func main() {}
36+
3237
func init() {
3338
wasm.RegisterModule(Spring4Shell{})
3439
}
@@ -94,7 +99,7 @@ func (Spring4Shell) parseJavaRelease(f *os.File, filePath string) (*serialize.An
9499
}
95100

96101
return &serialize.AnalysisResult{
97-
CustomResources: []serialize.CustomResource{
102+
CustomResources: []ftypes.CustomResource{
98103
{
99104
Type: TypeJavaMajor,
100105
FilePath: filePath,
@@ -116,7 +121,7 @@ func (Spring4Shell) parseTomcatReleaseNotes(f *os.File, filePath string) (*seria
116121
}
117122

118123
return &serialize.AnalysisResult{
119-
CustomResources: []serialize.CustomResource{
124+
CustomResources: []ftypes.CustomResource{
120125
{
121126
Type: TypeTomcatVersion,
122127
FilePath: filePath,
@@ -221,7 +226,7 @@ func (Spring4Shell) PostScanSpec() serialize.PostScanSpec {
221226
// }
222227
//
223228
// ]
224-
func (Spring4Shell) PostScan(results serialize.Results) (serialize.Results, error) {
229+
func (Spring4Shell) PostScan(results types.Results) (types.Results, error) {
225230
var javaMajorVersion int
226231
var tomcatVersion string
227232
for _, result := range results {

go.mod

-39
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,7 @@ require (
421421
)
422422

423423
require (
424-
github.com/STARRY-S/zip v0.2.1 // indirect
425-
github.com/adrg/xdg v0.5.3 // indirect
426424
github.com/alessio/shellescape v1.4.1 // indirect
427-
github.com/andybalholm/brotli v1.1.1 // indirect
428-
github.com/aquaproj/aqua/v2 v2.45.0 // indirect
429425
github.com/aws/aws-sdk-go v1.55.6 // indirect
430426
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 // indirect
431427
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect
@@ -437,55 +433,20 @@ require (
437433
github.com/aws/aws-sdk-go-v2/service/sso v1.25.1 // indirect
438434
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.29.1 // indirect
439435
github.com/aws/aws-sdk-go-v2/service/sts v1.33.17 // indirect
440-
github.com/bahlo/generic-list-go v0.2.0 // indirect
441-
github.com/bodgit/plumbing v1.3.0 // indirect
442-
github.com/bodgit/sevenzip v1.6.0 // indirect
443-
github.com/bodgit/windows v1.0.1 // indirect
444-
github.com/buger/jsonparser v1.1.1 // indirect
445436
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
446-
github.com/expr-lang/expr v1.16.9 // indirect
447-
github.com/forPelevin/gomoji v1.3.0 // indirect
448-
github.com/gdamore/encoding v1.0.0 // indirect
449-
github.com/gdamore/tcell/v2 v2.6.0 // indirect
450437
github.com/google/go-github/v31 v31.0.0 // indirect
451-
github.com/google/go-github/v69 v69.2.0 // indirect
452438
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2 // indirect
453439
github.com/google/subcommands v1.2.0 // indirect
454-
github.com/invopop/jsonschema v0.13.0 // indirect
455-
github.com/klauspost/pgzip v1.2.6 // indirect
456440
github.com/knqyf263/labeler v0.0.0-20200423181506-7a6e545148c3 // indirect
457-
github.com/ktr0731/go-ansisgr v0.1.0 // indirect
458-
github.com/ktr0731/go-fuzzyfinder v0.8.0 // indirect
459-
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
460-
github.com/mholt/archives v0.1.0 // indirect
461-
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
462-
github.com/nsf/termbox-go v1.1.1 // indirect
463-
github.com/nwaples/rardecode/v2 v2.0.0-beta.4.0.20241112120701-034e449c6e78 // indirect
464441
github.com/oklog/ulid/v2 v2.1.0 // indirect
465-
github.com/otiai10/copy v1.14.1 // indirect
466-
github.com/otiai10/mint v1.6.3 // indirect
467442
github.com/pelletier/go-toml v1.9.5 // indirect
468-
github.com/pierrec/lz4/v4 v4.1.21 // indirect
469443
github.com/samber/oops v1.15.0 // indirect
470-
github.com/schollz/progressbar/v3 v3.18.0 // indirect
471-
github.com/sorairolake/lzip-go v0.3.5 // indirect
472-
github.com/suzuki-shunsuke/go-error-with-exit-code v1.0.0 // indirect
473-
github.com/suzuki-shunsuke/go-findconfig v1.2.0 // indirect
474-
github.com/suzuki-shunsuke/go-osenv v0.1.0 // indirect
475-
github.com/suzuki-shunsuke/logrus-error v0.1.4 // indirect
476-
github.com/suzuki-shunsuke/urfave-cli-help-all v0.0.4 // indirect
477-
github.com/therootcompany/xz v1.0.1 // indirect
478444
github.com/tonglil/versioning v0.0.0-20170205083536-8b2a4334bd1d // indirect
479-
github.com/urfave/cli/v2 v2.27.5 // indirect
480-
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
481-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
482-
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
483445
gopkg.in/yaml.v2 v2.4.0 // indirect
484446
sigs.k8s.io/kind v0.19.0 // indirect
485447
)
486448

487449
tool (
488-
github.com/aquaproj/aqua/v2/cmd/aqua
489450
github.com/google/wire/cmd/wire
490451
github.com/knqyf263/labeler
491452
github.com/magefile/mage

0 commit comments

Comments
 (0)