Skip to content

Commit

Permalink
feat(misconf): Support custom data for cloud policies
Browse files Browse the repository at this point in the history
Signed-off-by: Simar <simar@linux.com>
  • Loading branch information
simar7 committed Jul 1, 2023
1 parent 6008192 commit 654aeae
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
3 changes: 3 additions & 0 deletions pkg/cloud/aws/commands/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,9 @@ func Test_Run(t *testing.T) {
PolicyPaths: []string{
filepath.Join(regoDir, "policies"),
},
DataPaths: []string{
filepath.Join(regoDir, "policies"),
},
PolicyNamespaces: []string{
"user",
},
Expand Down
23 changes: 17 additions & 6 deletions pkg/cloud/aws/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aquasecurity/trivy/pkg/commands/operation"
"github.com/aquasecurity/trivy/pkg/flag"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/misconf"
)

type AWSScanner struct {
Expand Down Expand Up @@ -77,13 +78,14 @@ func (s *AWSScanner) Scan(ctx context.Context, option flag.Options) (scan.Result
}
policyPaths = append(policyPaths, option.RegoOptions.PolicyPaths...)
scannerOpts = append(scannerOpts, options.ScannerWithPolicyDirs(policyPaths...))

if len(option.RegoOptions.PolicyNamespaces) > 0 {
scannerOpts = append(
scannerOpts,
options.ScannerWithPolicyNamespaces(option.RegoOptions.PolicyNamespaces...),
)
dataFS, dataPaths, err := misconf.CreateDataFS(option.RegoOptions.DataPaths)
if err != nil {
log.Logger.Errorf("Could not load config data: %s", err)
}
scannerOpts = append(scannerOpts, options.ScannerWithDataDirs(dataPaths...))
scannerOpts = append(scannerOpts, options.ScannerWithDataFilesystem(dataFS))

scannerOpts = addPolicyNamespaces(option.RegoOptions.PolicyNamespaces, scannerOpts)

if option.Compliance.Spec.ID != "" {
scannerOpts = append(scannerOpts, options.ScannerWithSpec(option.Compliance.Spec.ID))
Expand Down Expand Up @@ -141,3 +143,12 @@ func (d *defsecLogger) Write(p []byte) (n int, err error) {
log.Logger.Debug("[defsec] " + strings.TrimSpace(string(p)))
return len(p), nil
}
func addPolicyNamespaces(namespaces []string, scannerOpts []options.ScannerOption) []options.ScannerOption {
if len(namespaces) > 0 {
scannerOpts = append(
scannerOpts,
options.ScannerWithPolicyNamespaces(namespaces...),
)
}
return scannerOpts
}
2 changes: 1 addition & 1 deletion pkg/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func initScannerConfig(opts flag.Options, cacheClient cache.Cache) (ScannerConfi
Trace: opts.Trace,
Namespaces: append(opts.PolicyNamespaces, defaultPolicyNamespaces...),
PolicyPaths: append(opts.PolicyPaths, downloadedPolicyPaths...),
DataPaths: opts.DataPaths,
DataPaths: append(opts.DataPaths, downloadedPolicyPaths...),
HelmValues: opts.HelmValues,
HelmValueFiles: opts.HelmValueFiles,
HelmFileValues: opts.HelmFileValues,
Expand Down
12 changes: 7 additions & 5 deletions pkg/misconf/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func scannerOptions(t detection.FileType, opt ScannerOption) ([]options.ScannerO
opts = append(opts, options.ScannerWithPolicyFilesystem(policyFS))
}

dataFS, dataPaths, err := createDataFS(opt.DataPaths, opt.K8sVersion)
dataFS, dataPaths, err := CreateDataFS(opt.DataPaths, opt.K8sVersion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -306,11 +306,12 @@ func createPolicyFS(policyPaths []string) (fs.FS, []string, error) {
return mfs, policyPaths, nil
}

func createDataFS(dataPaths []string, k8sVersion string) (fs.FS, []string, error) {
func CreateDataFS(dataPaths []string, options ...string) (fs.FS, []string, error) {
fsys := mapfs.New()

// Create a virtual file for Kubernetes scanning
if k8sVersion != "" {
// Check if k8sVersion is provided
if len(options) > 0 {
k8sVersion := options[0]
if err := fsys.MkdirAll("system", 0700); err != nil {
return nil, nil, err
}
Expand All @@ -319,13 +320,14 @@ func createDataFS(dataPaths []string, k8sVersion string) (fs.FS, []string, error
return nil, nil, err
}
}

for _, path := range dataPaths {
if err := fsys.CopyFilesUnder(path); err != nil {
return nil, nil, err
}
}

// data paths are no longer needed as fs.FS contains only needed files now.
// dataPaths are no longer needed as fs.FS contains only needed files now.
dataPaths = []string{"."}

return fsys, dataPaths, nil
Expand Down
30 changes: 23 additions & 7 deletions pkg/misconf/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package misconf

import (
"context"
"io/fs"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -157,13 +158,28 @@ func Test_createPolicyFS(t *testing.T) {
tmpDir := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(tmpDir, "subdir/testdir"), 0750))
f, got, err := createPolicyFS([]string{filepath.Join(tmpDir, "subdir/testdir")})
require.NoError(t, err)
assert.Equal(t, []string{"."}, got)
assertFS(t, tmpDir, f, got, err)
})
}

d, err := f.Open(tmpDir)
require.NoError(t, err)
stat, err := d.Stat()
require.NoError(t, err)
assert.True(t, stat.IsDir())
func Test_CreateDataFS(t *testing.T) {
t.Run("outside pwd", func(t *testing.T) {
tmpDir := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(tmpDir, "subdir/testdir"), 0750))
f, got, err := CreateDataFS([]string{filepath.Join(tmpDir, "subdir/testdir")})
assertFS(t, tmpDir, f, got, err)
})
}

func assertFS(t *testing.T, tmpDir string, f fs.FS, got []string, err error) {
t.Helper()

require.NoError(t, err)
assert.Equal(t, []string{"."}, got)

d, err := f.Open(tmpDir)
require.NoError(t, err)
stat, err := d.Stat()
require.NoError(t, err)
assert.True(t, stat.IsDir())
}

0 comments on commit 654aeae

Please sign in to comment.