forked from aquasecurity/trivy
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(misconf): introduce generic scanner (aquasecurity#7515)
Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
- Loading branch information
Showing
19 changed files
with
469 additions
and
1,235 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,114 +1,12 @@ | ||
package dockerfile | ||
|
||
import ( | ||
"context" | ||
"io/fs" | ||
"sync" | ||
|
||
"github.com/aquasecurity/trivy/pkg/iac/framework" | ||
"github.com/aquasecurity/trivy/pkg/iac/rego" | ||
"github.com/aquasecurity/trivy/pkg/iac/scan" | ||
"github.com/aquasecurity/trivy/pkg/iac/scanners" | ||
"github.com/aquasecurity/trivy/pkg/iac/scanners/dockerfile/parser" | ||
"github.com/aquasecurity/trivy/pkg/iac/scanners/generic" | ||
"github.com/aquasecurity/trivy/pkg/iac/scanners/options" | ||
"github.com/aquasecurity/trivy/pkg/iac/types" | ||
"github.com/aquasecurity/trivy/pkg/log" | ||
) | ||
|
||
var _ scanners.FSScanner = (*Scanner)(nil) | ||
var _ options.ConfigurableScanner = (*Scanner)(nil) | ||
|
||
type Scanner struct { | ||
mu sync.Mutex | ||
logger *log.Logger | ||
parser *parser.Parser | ||
regoScanner *rego.Scanner | ||
options []options.ScannerOption | ||
} | ||
|
||
func (s *Scanner) SetIncludeDeprecatedChecks(bool) {} | ||
func (s *Scanner) SetRegoOnly(bool) {} | ||
func (s *Scanner) SetFrameworks(frameworks []framework.Framework) {} | ||
|
||
func (s *Scanner) Name() string { | ||
return "Dockerfile" | ||
} | ||
|
||
func NewScanner(opts ...options.ScannerOption) *Scanner { | ||
s := &Scanner{ | ||
options: opts, | ||
logger: log.WithPrefix("dockerfile scanner"), | ||
} | ||
for _, opt := range opts { | ||
opt(s) | ||
} | ||
s.parser = parser.New() | ||
return s | ||
} | ||
|
||
func (s *Scanner) ScanFS(ctx context.Context, fsys fs.FS, path string) (scan.Results, error) { | ||
|
||
files, err := s.parser.ParseFS(ctx, fsys, path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(files) == 0 { | ||
return nil, nil | ||
} | ||
|
||
var inputs []rego.Input | ||
for path, dfile := range files { | ||
inputs = append(inputs, rego.Input{ | ||
Path: path, | ||
FS: fsys, | ||
Contents: dfile.ToRego(), | ||
}) | ||
} | ||
|
||
results, err := s.scanRego(ctx, fsys, inputs...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return results, nil | ||
} | ||
|
||
func (s *Scanner) ScanFile(ctx context.Context, fsys fs.FS, path string) (scan.Results, error) { | ||
dockerfile, err := s.parser.ParseFile(ctx, fsys, path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s.logger.Debug("Scanning", log.FilePath(path)) | ||
return s.scanRego(ctx, fsys, rego.Input{ | ||
Path: path, | ||
Contents: dockerfile.ToRego(), | ||
}) | ||
} | ||
|
||
func (s *Scanner) initRegoScanner(srcFS fs.FS) (*rego.Scanner, error) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
if s.regoScanner != nil { | ||
return s.regoScanner, nil | ||
} | ||
|
||
regoScanner := rego.NewScanner(types.SourceDockerfile, s.options...) | ||
if err := regoScanner.LoadPolicies(srcFS); err != nil { | ||
return nil, err | ||
} | ||
s.regoScanner = regoScanner | ||
return regoScanner, nil | ||
} | ||
|
||
func (s *Scanner) scanRego(ctx context.Context, srcFS fs.FS, inputs ...rego.Input) (scan.Results, error) { | ||
regoScanner, err := s.initRegoScanner(srcFS) | ||
if err != nil { | ||
return nil, err | ||
} | ||
results, err := regoScanner.ScanInput(ctx, inputs...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
results.SetSourceAndFilesystem("", srcFS, false) | ||
return results, nil | ||
func NewScanner(opts ...options.ScannerOption) *generic.GenericScanner { | ||
return generic.NewScanner("Dockerfile", types.SourceDockerfile, generic.ParseFunc(parser.Parse), opts...) | ||
} |
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.