Skip to content

Commit e7c16a7

Browse files
nikpivkinknqyf263
andauthored
refactor(misconf): replace github.com/liamg/memoryfs with internal mapfs and testing/fstest (aquasecurity#9282)
Signed-off-by: nikpivkin <[email protected]> Co-authored-by: knqyf263 <[email protected]>
1 parent c446a5c commit e7c16a7

File tree

29 files changed

+221
-278
lines changed

29 files changed

+221
-278
lines changed

.golangci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ linters:
5959
recommendations:
6060
- github.com/aquasecurity/go-version
6161
reason: "`aquasecurity/go-version` is designed for our use-cases"
62+
- github.com/liamg/memoryfs:
63+
recommendations:
64+
- github.com/aquasecurity/trivy/pkg/mapfs
6265
gosec:
6366
excludes:
6467
- G101

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ require (
7070
github.com/knqyf263/go-rpmdb v0.1.1
7171
github.com/knqyf263/nested v0.0.1
7272
github.com/kylelemons/godebug v1.1.0
73-
github.com/liamg/memoryfs v1.6.0
7473
github.com/magefile/mage v1.15.0
7574
github.com/masahiro331/go-disk v0.0.0-20240625071113-56c933208fee
7675
github.com/masahiro331/go-ebs-file v0.0.0-20240917043618-e6d2bea5c32e

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,6 @@ github.com/lestrrat-go/option/v2 v2.0.0 h1:XxrcaJESE1fokHy3FpaQ/cXW8ZsIdWcdFzzLO
860860
github.com/lestrrat-go/option/v2 v2.0.0/go.mod h1:oSySsmzMoR0iRzCDCaUfsCzxQHUEuhOViQObyy7S6Vg=
861861
github.com/letsencrypt/boulder v0.0.0-20240620165639-de9c06129bec h1:2tTW6cDth2TSgRbAhD7yjZzTQmcN25sDRPEeinR51yQ=
862862
github.com/letsencrypt/boulder v0.0.0-20240620165639-de9c06129bec/go.mod h1:TmwEoGCwIti7BCeJ9hescZgRtatxRE+A72pCoPfmcfk=
863-
github.com/liamg/memoryfs v1.6.0 h1:jAFec2HI1PgMTem5gR7UT8zi9u4BfG5jorCRlLH06W8=
864-
github.com/liamg/memoryfs v1.6.0/go.mod h1:z7mfqXFQS8eSeBBsFjYLlxYRMRyiPktytvYCYTb3BSk=
865863
github.com/lib/pq v0.0.0-20150723085316-0dad96c0b94f/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
866864
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
867865
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=

internal/testutil/util.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package testutil
33
import (
44
"encoding/json"
55
"io/fs"
6-
"path/filepath"
76
"strings"
87
"testing"
8+
"testing/fstest"
99

10-
"github.com/liamg/memoryfs"
1110
"github.com/samber/lo"
1211
"github.com/stretchr/testify/assert"
1312
"github.com/stretchr/testify/require"
@@ -56,16 +55,10 @@ func ruleIDInResults(ruleID string, results scan.Results) bool {
5655
return false
5756
}
5857

59-
func CreateFS(t *testing.T, files map[string]string) fs.FS {
60-
memfs := memoryfs.New()
61-
for name, content := range files {
62-
name := strings.TrimPrefix(name, "/")
63-
err := memfs.MkdirAll(filepath.Dir(name), 0o700)
64-
require.NoError(t, err)
65-
err = memfs.WriteFile(name, []byte(content), 0o644)
66-
require.NoError(t, err)
67-
}
68-
return memfs
58+
func CreateFS(files map[string]string) fs.FS {
59+
return fstest.MapFS(lo.MapEntries(files, func(k, v string) (string, *fstest.MapFile) {
60+
return strings.TrimPrefix(k, "/"), &fstest.MapFile{Data: []byte(v)}
61+
}))
6962
}
7063

7164
func AssertDefsecEqual(t *testing.T, expected, actual any) {

pkg/iac/adapters/arm/adaptertest/adaptertest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
type adaptFn[T any] func(deployment azure.Deployment) T
1414

1515
func AdaptAndCompare[T any](t *testing.T, source string, expected any, fn adaptFn[T]) {
16-
fsys := testutil.CreateFS(t, map[string]string{
16+
fsys := testutil.CreateFS(map[string]string{
1717
"test.json": source,
1818
})
1919

pkg/iac/adapters/cloudformation/testutil/testutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
type adaptFn[T any] func(fctx parser.FileContext) T
1313

1414
func AdaptAndCompare[T any](t *testing.T, source string, expected any, fn adaptFn[T]) {
15-
fsys := testutil.CreateFS(t, map[string]string{
15+
fsys := testutil.CreateFS(map[string]string{
1616
"main.yaml": source,
1717
})
1818

pkg/iac/adapters/terraform/tftestutil/testutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func CreateModulesFromSource(t *testing.T, source, ext string) terraform.Modules {
14-
fs := testutil.CreateFS(t, map[string]string{
14+
fs := testutil.CreateFS(map[string]string{
1515
"source" + ext: source,
1616
})
1717
p := parser.New(fs, "", parser.OptionStopOnHCLError(true))

pkg/iac/rego/scanner_test.go

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,25 @@ package rego_test
33
import (
44
"bytes"
55
"fmt"
6-
"io/fs"
76
"os"
87
"path/filepath"
98
"strings"
109
"testing"
1110
"testing/fstest"
1211

13-
"github.com/liamg/memoryfs"
1412
"github.com/stretchr/testify/assert"
1513
"github.com/stretchr/testify/require"
1614

15+
"github.com/aquasecurity/trivy/internal/testutil"
1716
"github.com/aquasecurity/trivy/pkg/iac/rego"
1817
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
1918
"github.com/aquasecurity/trivy/pkg/iac/severity"
2019
"github.com/aquasecurity/trivy/pkg/iac/types"
2120
)
2221

23-
func CreateFS(t *testing.T, files map[string]string) fs.FS {
24-
memfs := memoryfs.New()
25-
for name, content := range files {
26-
name := strings.TrimPrefix(name, "/")
27-
err := memfs.MkdirAll(filepath.Dir(name), 0o700)
28-
require.NoError(t, err)
29-
err = memfs.WriteFile(name, []byte(content), 0o644)
30-
require.NoError(t, err)
31-
}
32-
return memfs
33-
}
34-
3522
func Test_RegoScanning_Deny(t *testing.T) {
3623

37-
srcFS := CreateFS(t, map[string]string{
24+
srcFS := testutil.CreateFS(map[string]string{
3825
"policies/test.rego": `
3926
# METADATA
4027
# title: Custom policy
@@ -128,7 +115,7 @@ deny {
128115
}
129116

130117
func Test_RegoScanning_Allow(t *testing.T) {
131-
srcFS := CreateFS(t, map[string]string{
118+
srcFS := testutil.CreateFS(map[string]string{
132119
"policies/test.rego": `# METADATA
133120
# title: Custom policy
134121
# description: Custom policy for testing
@@ -176,7 +163,7 @@ func Test_RegoScanning_WithRuntimeValues(t *testing.T) {
176163

177164
t.Setenv("DEFSEC_RUNTIME_VAL", "AOK")
178165

179-
srcFS := CreateFS(t, map[string]string{
166+
srcFS := testutil.CreateFS(map[string]string{
180167
"policies/test.rego": `# METADATA
181168
# title: Custom policy
182169
# description: Custom policy for testing
@@ -220,7 +207,7 @@ deny_evil {
220207
}
221208

222209
func Test_RegoScanning_WithDenyMessage(t *testing.T) {
223-
srcFS := CreateFS(t, map[string]string{
210+
srcFS := testutil.CreateFS(map[string]string{
224211
"policies/test.rego": `# METADATA
225212
# title: Custom policy
226213
# description: Custom policy for testing
@@ -267,7 +254,7 @@ deny[msg] {
267254
}
268255

269256
func Test_RegoScanning_WithDenyMetadata_ImpliedPath(t *testing.T) {
270-
srcFS := CreateFS(t, map[string]string{
257+
srcFS := testutil.CreateFS(map[string]string{
271258
"policies/test.rego": `
272259
# METADATA
273260
# title: Custom policy
@@ -322,7 +309,7 @@ deny[res] {
322309
}
323310

324311
func Test_RegoScanning_WithDenyMetadata_PersistedPath(t *testing.T) {
325-
srcFS := CreateFS(t, map[string]string{
312+
srcFS := testutil.CreateFS(map[string]string{
326313
"policies/test.rego": `
327314
# METADATA
328315
# title: Custom policy
@@ -378,7 +365,7 @@ deny[res] {
378365
}
379366

380367
func Test_RegoScanning_WithStaticMetadata(t *testing.T) {
381-
srcFS := CreateFS(t, map[string]string{
368+
srcFS := testutil.CreateFS(map[string]string{
382369
"policies/test.rego": `
383370
package defsec.test
384371
@@ -439,7 +426,7 @@ deny[res] {
439426
}
440427

441428
func Test_RegoScanning_WithMatchingInputSelector(t *testing.T) {
442-
srcFS := CreateFS(t, map[string]string{
429+
srcFS := testutil.CreateFS(map[string]string{
443430
"policies/test.rego": `# METADATA
444431
# title: Custom policy
445432
# description: Custom policy for testing
@@ -487,7 +474,7 @@ deny {
487474
}
488475

489476
func Test_RegoScanning_WithNonMatchingInputSelector(t *testing.T) {
490-
srcFS := CreateFS(t, map[string]string{
477+
srcFS := testutil.CreateFS(map[string]string{
491478
"policies/test.rego": `
492479
package defsec.test
493480
@@ -521,7 +508,7 @@ deny {
521508

522509
func Test_RegoScanning_NoTracingByDefault(t *testing.T) {
523510

524-
srcFS := CreateFS(t, map[string]string{
511+
srcFS := testutil.CreateFS(map[string]string{
525512
"policies/test.rego": `# METADATA
526513
# title: Custom policy
527514
# description: Custom policy for testing
@@ -567,7 +554,7 @@ deny {
567554

568555
func Test_RegoScanning_GlobalTracingEnabled(t *testing.T) {
569556

570-
srcFS := CreateFS(t, map[string]string{
557+
srcFS := testutil.CreateFS(map[string]string{
571558
"policies/test.rego": `# METADATA
572559
# title: Custom policy
573560
# description: Custom policy for testing
@@ -617,7 +604,7 @@ deny {
617604

618605
func Test_RegoScanning_PerResultTracingEnabled(t *testing.T) {
619606

620-
srcFS := CreateFS(t, map[string]string{
607+
srcFS := testutil.CreateFS(map[string]string{
621608
"policies/test.rego": `# METADATA
622609
# title: Custom policy
623610
# description: Custom policy for testing
@@ -663,7 +650,7 @@ deny {
663650

664651
func Test_dynamicMetadata(t *testing.T) {
665652

666-
srcFS := CreateFS(t, map[string]string{
653+
srcFS := testutil.CreateFS(map[string]string{
667654
"policies/test.rego": `
668655
package defsec.test
669656
@@ -695,7 +682,7 @@ deny {
695682

696683
func Test_staticMetadata(t *testing.T) {
697684

698-
srcFS := CreateFS(t, map[string]string{
685+
srcFS := testutil.CreateFS(map[string]string{
699686
"policies/test.rego": `
700687
package defsec.test
701688
@@ -727,7 +714,7 @@ deny {
727714

728715
func Test_annotationMetadata(t *testing.T) {
729716

730-
srcFS := CreateFS(t, map[string]string{
717+
srcFS := testutil.CreateFS(map[string]string{
731718
"policies/test.rego": `# METADATA
732719
# title: i am a title
733720
# description: i am a description
@@ -782,7 +769,7 @@ deny {
782769

783770
func Test_RegoScanning_WithInvalidInputSchema(t *testing.T) {
784771

785-
srcFS := CreateFS(t, map[string]string{
772+
srcFS := testutil.CreateFS(map[string]string{
786773
"policies/test.rego": `# METADATA
787774
# schemas:
788775
# - input: schema["input"]
@@ -802,7 +789,7 @@ deny {
802789

803790
func Test_RegoScanning_WithValidInputSchema(t *testing.T) {
804791

805-
srcFS := CreateFS(t, map[string]string{
792+
srcFS := testutil.CreateFS(map[string]string{
806793
"policies/test.rego": `# METADATA
807794
# schemas:
808795
# - input: schema["input"]
@@ -821,7 +808,7 @@ deny {
821808
}
822809

823810
func Test_RegoScanning_WithFilepathToSchema(t *testing.T) {
824-
srcFS := CreateFS(t, map[string]string{
811+
srcFS := testutil.CreateFS(map[string]string{
825812
"policies/test.rego": `# METADATA
826813
# schemas:
827814
# - input: schema["dockerfile"]
@@ -846,7 +833,7 @@ deny {
846833
}
847834

848835
func Test_RegoScanning_CustomData(t *testing.T) {
849-
srcFS := CreateFS(t, map[string]string{
836+
srcFS := testutil.CreateFS(map[string]string{
850837
"policies/test.rego": `# METADATA
851838
# title: Custom policy
852839
# description: Custom policy for testing
@@ -871,7 +858,7 @@ deny {
871858
`,
872859
})
873860

874-
dataFS := CreateFS(t, map[string]string{
861+
dataFS := testutil.CreateFS(map[string]string{
875862
"data/data.json": `{
876863
"settings": {
877864
"DS123":{
@@ -899,7 +886,7 @@ deny {
899886
}
900887

901888
func Test_RegoScanning_InvalidFS(t *testing.T) {
902-
srcFS := CreateFS(t, map[string]string{
889+
srcFS := testutil.CreateFS(map[string]string{
903890
"policies/test.rego": `# METADATA
904891
# title: Custom policy
905892
# description: Custom policy for testing
@@ -924,7 +911,7 @@ deny {
924911
`,
925912
})
926913

927-
dataFS := CreateFS(t, map[string]string{
914+
dataFS := testutil.CreateFS(map[string]string{
928915
"data/data.json": `{
929916
"settings": {
930917
"DS123":{

0 commit comments

Comments
 (0)