forked from aquasecurity/trivy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(deps): Merge trivy-iac into Trivy (aquasecurity#6005)
- Loading branch information
Showing
777 changed files
with
68,473 additions
and
45 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package testutil | ||
|
||
import ( | ||
"encoding/json" | ||
"io/fs" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/liamg/memoryfs" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aquasecurity/defsec/pkg/scan" | ||
) | ||
|
||
func AssertRuleFound(t *testing.T, ruleID string, results scan.Results, message string, args ...interface{}) { | ||
found := ruleIDInResults(ruleID, results.GetFailed()) | ||
assert.True(t, found, append([]interface{}{message}, args...)...) | ||
for _, result := range results.GetFailed() { | ||
if result.Rule().LongID() == ruleID { | ||
m := result.Metadata() | ||
meta := &m | ||
for meta != nil { | ||
assert.NotNil(t, meta.Range(), 0) | ||
assert.Greater(t, meta.Range().GetStartLine(), 0) | ||
assert.Greater(t, meta.Range().GetEndLine(), 0) | ||
meta = meta.Parent() | ||
} | ||
} | ||
} | ||
} | ||
|
||
func AssertRuleNotFound(t *testing.T, ruleID string, results scan.Results, message string, args ...interface{}) { | ||
found := ruleIDInResults(ruleID, results.GetFailed()) | ||
assert.False(t, found, append([]interface{}{message}, args...)...) | ||
} | ||
|
||
func ruleIDInResults(ruleID string, results scan.Results) bool { | ||
for _, res := range results { | ||
if res.Rule().LongID() == ruleID { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func CreateFS(t *testing.T, files map[string]string) fs.FS { | ||
memfs := memoryfs.New() | ||
for name, content := range files { | ||
name := strings.TrimPrefix(name, "/") | ||
err := memfs.MkdirAll(filepath.Dir(name), 0o700) | ||
require.NoError(t, err) | ||
err = memfs.WriteFile(name, []byte(content), 0o644) | ||
require.NoError(t, err) | ||
} | ||
return memfs | ||
} | ||
|
||
func AssertDefsecEqual(t *testing.T, expected, actual interface{}) { | ||
expectedJson, err := json.MarshalIndent(expected, "", "\t") | ||
require.NoError(t, err) | ||
actualJson, err := json.MarshalIndent(actual, "", "\t") | ||
require.NoError(t, err) | ||
|
||
if expectedJson[0] == '[' { | ||
var expectedSlice []map[string]interface{} | ||
require.NoError(t, json.Unmarshal(expectedJson, &expectedSlice)) | ||
var actualSlice []map[string]interface{} | ||
require.NoError(t, json.Unmarshal(actualJson, &actualSlice)) | ||
expectedSlice = purgeMetadataSlice(expectedSlice) | ||
actualSlice = purgeMetadataSlice(actualSlice) | ||
assert.Equal(t, expectedSlice, actualSlice, "defsec adapted and expected values do not match") | ||
} else { | ||
var expectedMap map[string]interface{} | ||
require.NoError(t, json.Unmarshal(expectedJson, &expectedMap)) | ||
var actualMap map[string]interface{} | ||
require.NoError(t, json.Unmarshal(actualJson, &actualMap)) | ||
expectedMap = purgeMetadata(expectedMap) | ||
actualMap = purgeMetadata(actualMap) | ||
assert.Equal(t, expectedMap, actualMap, "defsec adapted and expected values do not match") | ||
} | ||
} | ||
|
||
func purgeMetadata(input map[string]interface{}) map[string]interface{} { | ||
for k, v := range input { | ||
if k == "metadata" || k == "Metadata" { | ||
delete(input, k) | ||
continue | ||
} | ||
if v, ok := v.(map[string]interface{}); ok { | ||
input[k] = purgeMetadata(v) | ||
} | ||
if v, ok := v.([]interface{}); ok { | ||
if len(v) > 0 { | ||
if _, ok := v[0].(map[string]interface{}); ok { | ||
maps := make([]map[string]interface{}, len(v)) | ||
for i := range v { | ||
maps[i] = v[i].(map[string]interface{}) | ||
} | ||
input[k] = purgeMetadataSlice(maps) | ||
} | ||
} | ||
} | ||
} | ||
return input | ||
} | ||
|
||
func purgeMetadataSlice(input []map[string]interface{}) []map[string]interface{} { | ||
for i := range input { | ||
input[i] = purgeMetadata(input[i]) | ||
} | ||
return input | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package extrafs | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
/* | ||
Go does not currently support symlinks in io/fs. | ||
We work around this by wrapping the fs.FS returned by os.DirFS with our own type which bolts on the ReadLinkFS | ||
*/ | ||
|
||
type OSFS interface { | ||
fs.FS | ||
fs.StatFS | ||
} | ||
|
||
type ReadLinkFS interface { | ||
ResolveSymlink(name, dir string) (string, error) | ||
} | ||
|
||
type FS interface { | ||
OSFS | ||
ReadLinkFS | ||
} | ||
|
||
type filesystem struct { | ||
root string | ||
underlying OSFS | ||
} | ||
|
||
func OSDir(path string) FS { | ||
return &filesystem{ | ||
root: path, | ||
underlying: os.DirFS(path).(OSFS), | ||
} | ||
} | ||
|
||
func (f *filesystem) Open(name string) (fs.File, error) { | ||
return f.underlying.Open(name) | ||
} | ||
|
||
func (f *filesystem) Stat(name string) (fs.FileInfo, error) { | ||
return f.underlying.Stat(name) | ||
} | ||
|
||
func (f *filesystem) ResolveSymlink(name, dir string) (string, error) { | ||
link, err := os.Readlink(filepath.Join(f.root, dir, name)) | ||
if err == nil { | ||
return filepath.Join(dir, link), nil | ||
} | ||
return name, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.