Skip to content

Commit

Permalink
remove parser interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: nikpivkin <[email protected]>
  • Loading branch information
nikpivkin committed Aug 13, 2024
1 parent b68f254 commit ca73bbc
Show file tree
Hide file tree
Showing 23 changed files with 101 additions and 309 deletions.
9 changes: 2 additions & 7 deletions pkg/iac/scanners/azure/arm/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/aquasecurity/trivy/pkg/iac/scanners/azure"
"github.com/aquasecurity/trivy/pkg/iac/scanners/azure/arm/parser/armjson"
"github.com/aquasecurity/trivy/pkg/iac/scanners/azure/resolver"
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
"github.com/aquasecurity/trivy/pkg/iac/types"
"github.com/aquasecurity/trivy/pkg/log"
)
Expand All @@ -19,15 +18,11 @@ type Parser struct {
logger *log.Logger
}

func New(targetFS fs.FS, opts ...options.ParserOption) *Parser {
p := &Parser{
func New(targetFS fs.FS) *Parser {
return &Parser{
targetFS: targetFS,
logger: log.WithPrefix("arm parser"),
}
for _, opt := range opts {
opt(p)
}
return p
}

func (p *Parser) ParseFS(ctx context.Context, dir string) ([]azure.Deployment, error) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/iac/scanners/azure/arm/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ var _ options.ConfigurableScanner = (*Scanner)(nil)
type Scanner struct {
mu sync.Mutex
scannerOptions []options.ScannerOption
parserOptions []options.ParserOption
logger *log.Logger
frameworks []framework.Framework
regoOnly bool
Expand Down Expand Up @@ -112,7 +111,7 @@ func (s *Scanner) initRegoScanner(srcFS fs.FS) error {
}

func (s *Scanner) ScanFS(ctx context.Context, fsys fs.FS, dir string) (scan.Results, error) {
p := parser.New(fsys, s.parserOptions...)
p := parser.New(fsys)
deployments, err := p.ParseFS(ctx, dir)
if err != nil {
return nil, err
Expand Down
31 changes: 12 additions & 19 deletions pkg/iac/scanners/cloudformation/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ import (
"gopkg.in/yaml.v3"

"github.com/aquasecurity/trivy/pkg/iac/ignore"
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
"github.com/aquasecurity/trivy/pkg/log"
)

var _ options.ConfigurableParser = (*Parser)(nil)

type Parser struct {
logger *log.Logger
parameterFiles []string
Expand All @@ -28,31 +25,27 @@ type Parser struct {
configsFS fs.FS
}

func WithParameters(params map[string]any) options.ParserOption {
return func(cp options.ConfigurableParser) {
if p, ok := cp.(*Parser); ok {
p.parameters = params
}
type Option func(*Parser)

func WithParameters(params map[string]any) Option {
return func(p *Parser) {
p.parameters = params
}
}

func WithParameterFiles(files ...string) options.ParserOption {
return func(cp options.ConfigurableParser) {
if p, ok := cp.(*Parser); ok {
p.parameterFiles = files
}
func WithParameterFiles(files ...string) Option {
return func(p *Parser) {
p.parameterFiles = files
}
}

func WithConfigsFS(fsys fs.FS) options.ParserOption {
return func(cp options.ConfigurableParser) {
if p, ok := cp.(*Parser); ok {
p.configsFS = fsys
}
func WithConfigsFS(fsys fs.FS) Option {
return func(p *Parser) {
p.configsFS = fsys
}
}

func New(opts ...options.ParserOption) *Parser {
func New(opts ...Option) *Parser {
p := &Parser{
logger: log.WithPrefix("cloudformation parser"),
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/iac/scanners/cloudformation/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ import (
func WithParameters(params map[string]any) options.ScannerOption {
return func(cs options.ConfigurableScanner) {
if s, ok := cs.(*Scanner); ok {
s.addParserOptions(parser.WithParameters(params))
s.addParserOption(parser.WithParameters(params))
}
}
}

func WithParameterFiles(files ...string) options.ScannerOption {
return func(cs options.ConfigurableScanner) {
if s, ok := cs.(*Scanner); ok {
s.addParserOptions(parser.WithParameterFiles(files...))
s.addParserOption(parser.WithParameterFiles(files...))
}
}
}

func WithConfigsFS(fsys fs.FS) options.ScannerOption {
return func(cs options.ConfigurableScanner) {
if s, ok := cs.(*Scanner); ok {
s.addParserOptions(parser.WithConfigsFS(fsys))
s.addParserOption(parser.WithConfigsFS(fsys))
}
}
}
Expand All @@ -58,14 +58,14 @@ type Scanner struct {
loadEmbeddedPolicies bool
loadEmbeddedLibraries bool
options []options.ScannerOption
parserOptions []options.ParserOption
parserOptions []parser.Option
frameworks []framework.Framework
spec string
}

func (s *Scanner) SetIncludeDeprecatedChecks(bool) {}

func (s *Scanner) addParserOptions(opt options.ParserOption) {
func (s *Scanner) addParserOption(opt parser.Option) {
s.parserOptions = append(s.parserOptions, opt)
}

Expand Down
11 changes: 2 additions & 9 deletions pkg/iac/scanners/dockerfile/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,18 @@ import (
"github.com/moby/buildkit/frontend/dockerfile/parser"

"github.com/aquasecurity/trivy/pkg/iac/providers/dockerfile"
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
"github.com/aquasecurity/trivy/pkg/log"
)

var _ options.ConfigurableParser = (*Parser)(nil)

type Parser struct {
logger *log.Logger
}

// New creates a new Dockerfile parser
func New(opts ...options.ParserOption) *Parser {
p := &Parser{
func New() *Parser {
return &Parser{
logger: log.WithPrefix("dockerfile parser"),
}
for _, option := range opts {
option(p)
}
return p
}

func (p *Parser) ParseFS(ctx context.Context, target fs.FS, path string) (map[string]*dockerfile.Dockerfile, error) {
Expand Down
29 changes: 12 additions & 17 deletions pkg/iac/scanners/helm/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,50 @@ import (
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
)

type ConfigurableHelmScanner interface {
options.ConfigurableScanner
AddParserOptions(options ...options.ParserOption)
}

func ScannerWithValuesFile(paths ...string) options.ScannerOption {
return func(s options.ConfigurableScanner) {
if helmScanner, ok := s.(ConfigurableHelmScanner); ok {
helmScanner.AddParserOptions(parser.OptionWithValuesFile(paths...))
if helmScanner, ok := s.(*Scanner); ok {
helmScanner.addParserOptions(parser.OptionWithValuesFile(paths...))
}
}
}

func ScannerWithValues(values ...string) options.ScannerOption {
return func(s options.ConfigurableScanner) {
if helmScanner, ok := s.(ConfigurableHelmScanner); ok {
helmScanner.AddParserOptions(parser.OptionWithValues(values...))
if helmScanner, ok := s.(*Scanner); ok {
helmScanner.addParserOptions(parser.OptionWithValues(values...))
}
}
}

func ScannerWithFileValues(values ...string) options.ScannerOption {
return func(s options.ConfigurableScanner) {
if helmScanner, ok := s.(ConfigurableHelmScanner); ok {
helmScanner.AddParserOptions(parser.OptionWithFileValues(values...))
if helmScanner, ok := s.(*Scanner); ok {
helmScanner.addParserOptions(parser.OptionWithFileValues(values...))
}
}
}

func ScannerWithStringValues(values ...string) options.ScannerOption {
return func(s options.ConfigurableScanner) {
if helmScanner, ok := s.(ConfigurableHelmScanner); ok {
helmScanner.AddParserOptions(parser.OptionWithStringValues(values...))
if helmScanner, ok := s.(*Scanner); ok {
helmScanner.addParserOptions(parser.OptionWithStringValues(values...))
}
}
}

func ScannerWithAPIVersions(values ...string) options.ScannerOption {
return func(s options.ConfigurableScanner) {
if helmScanner, ok := s.(ConfigurableHelmScanner); ok {
helmScanner.AddParserOptions(parser.OptionWithAPIVersions(values...))
if helmScanner, ok := s.(*Scanner); ok {
helmScanner.addParserOptions(parser.OptionWithAPIVersions(values...))
}
}
}

func ScannerWithKubeVersion(values string) options.ScannerOption {
return func(s options.ConfigurableScanner) {
if helmScanner, ok := s.(ConfigurableHelmScanner); ok {
helmScanner.AddParserOptions(parser.OptionWithKubeVersion(values))
if helmScanner, ok := s.(*Scanner); ok {
helmScanner.addParserOptions(parser.OptionWithKubeVersion(values))
}
}
}
53 changes: 20 additions & 33 deletions pkg/iac/scanners/helm/parser/option.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package parser

import "github.com/aquasecurity/trivy/pkg/iac/scanners/options"

type ConfigurableHelmParser interface {
options.ConfigurableParser
SetValuesFile(...string)
SetValues(...string)
SetFileValues(...string)
Expand All @@ -12,50 +9,40 @@ type ConfigurableHelmParser interface {
SetKubeVersion(string)
}

func OptionWithValuesFile(paths ...string) options.ParserOption {
return func(p options.ConfigurableParser) {
if helmParser, ok := p.(ConfigurableHelmParser); ok {
helmParser.SetValuesFile(paths...)
}
type Option func(p *Parser)

func OptionWithValuesFile(paths ...string) Option {
return func(p *Parser) {
p.valuesFiles = paths
}
}

func OptionWithValues(values ...string) options.ParserOption {
return func(p options.ConfigurableParser) {
if helmParser, ok := p.(ConfigurableHelmParser); ok {
helmParser.SetValues(values...)
}
func OptionWithValues(values ...string) Option {
return func(p *Parser) {
p.values = values
}
}

func OptionWithFileValues(values ...string) options.ParserOption {
return func(p options.ConfigurableParser) {
if helmParser, ok := p.(ConfigurableHelmParser); ok {
helmParser.SetValues(values...)
}
func OptionWithFileValues(values ...string) Option {
return func(p *Parser) {
p.fileValues = values
}
}

func OptionWithStringValues(values ...string) options.ParserOption {
return func(p options.ConfigurableParser) {
if helmParser, ok := p.(ConfigurableHelmParser); ok {
helmParser.SetValues(values...)
}
func OptionWithStringValues(values ...string) Option {
return func(p *Parser) {
p.stringValues = values
}
}

func OptionWithAPIVersions(values ...string) options.ParserOption {
return func(p options.ConfigurableParser) {
if helmParser, ok := p.(ConfigurableHelmParser); ok {
helmParser.SetAPIVersions(values...)
}
func OptionWithAPIVersions(values ...string) Option {
return func(p *Parser) {
p.apiVersions = values
}
}

func OptionWithKubeVersion(value string) options.ParserOption {
return func(p options.ConfigurableParser) {
if helmParser, ok := p.(ConfigurableHelmParser); ok {
helmParser.SetKubeVersion(value)
}
func OptionWithKubeVersion(value string) Option {
return func(p *Parser) {
p.kubeVersion = value
}
}
27 changes: 1 addition & 26 deletions pkg/iac/scanners/helm/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"helm.sh/helm/v3/pkg/releaseutil"

"github.com/aquasecurity/trivy/pkg/iac/detection"
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
"github.com/aquasecurity/trivy/pkg/log"
)

Expand All @@ -47,31 +46,7 @@ type ChartFile struct {
ManifestContent string
}

func (p *Parser) SetValuesFile(s ...string) {
p.valuesFiles = s
}

func (p *Parser) SetValues(values ...string) {
p.values = values
}

func (p *Parser) SetFileValues(values ...string) {
p.fileValues = values
}

func (p *Parser) SetStringValues(values ...string) {
p.stringValues = values
}

func (p *Parser) SetAPIVersions(values ...string) {
p.apiVersions = values
}

func (p *Parser) SetKubeVersion(value string) {
p.kubeVersion = value
}

func New(path string, opts ...options.ParserOption) (*Parser, error) {
func New(path string, opts ...Option) (*Parser, error) {

client := action.NewInstall(&action.Configuration{})
client.DryRun = true // don't do anything
Expand Down
4 changes: 2 additions & 2 deletions pkg/iac/scanners/helm/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Scanner struct {
dataDirs []string
logger *log.Logger
options []options.ScannerOption
parserOptions []options.ParserOption
parserOptions []parser.Option
policyReaders []io.Reader
loadEmbeddedLibraries bool
loadEmbeddedPolicies bool
Expand Down Expand Up @@ -68,7 +68,7 @@ func New(opts ...options.ScannerOption) *Scanner {
return s
}

func (s *Scanner) AddParserOptions(opts ...options.ParserOption) {
func (s *Scanner) addParserOptions(opts ...parser.Option) {
s.parserOptions = append(s.parserOptions, opts...)
}

Expand Down
Loading

0 comments on commit ca73bbc

Please sign in to comment.