github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/toml/scanner.go (about)

     1  package toml
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"io/fs"
     7  	"sync"
     8  
     9  	"github.com/aquasecurity/defsec/pkg/debug"
    10  	"github.com/aquasecurity/defsec/pkg/framework"
    11  	"github.com/aquasecurity/defsec/pkg/rego"
    12  	"github.com/aquasecurity/defsec/pkg/scan"
    13  	"github.com/aquasecurity/defsec/pkg/scanners/options"
    14  	"github.com/aquasecurity/defsec/pkg/types"
    15  	"github.com/aquasecurity/trivy-iac/pkg/scanners/toml/parser"
    16  )
    17  
    18  var _ options.ConfigurableScanner = (*Scanner)(nil)
    19  
    20  type Scanner struct {
    21  	debug         debug.Logger
    22  	options       []options.ScannerOption
    23  	policyDirs    []string
    24  	policyReaders []io.Reader
    25  	parser        *parser.Parser
    26  	regoScanner   *rego.Scanner
    27  	skipRequired  bool
    28  	sync.Mutex
    29  	frameworks            []framework.Framework
    30  	spec                  string
    31  	loadEmbeddedPolicies  bool
    32  	loadEmbeddedLibraries bool
    33  }
    34  
    35  func (s *Scanner) SetRegoOnly(bool) {}
    36  
    37  func (s *Scanner) SetFrameworks(frameworks []framework.Framework) {
    38  	s.frameworks = frameworks
    39  }
    40  
    41  func (s *Scanner) SetSpec(spec string) {
    42  	s.spec = spec
    43  }
    44  
    45  func (s *Scanner) SetUseEmbeddedPolicies(b bool) {
    46  	s.loadEmbeddedPolicies = b
    47  }
    48  
    49  func (s *Scanner) SetUseEmbeddedLibraries(b bool) {
    50  	s.loadEmbeddedLibraries = b
    51  }
    52  
    53  func (s *Scanner) Name() string {
    54  	return "TOML"
    55  }
    56  
    57  func (s *Scanner) SetPolicyReaders(readers []io.Reader) {
    58  	s.policyReaders = readers
    59  }
    60  
    61  func (s *Scanner) SetSkipRequiredCheck(skip bool) {
    62  	s.skipRequired = skip
    63  }
    64  
    65  func (s *Scanner) SetDebugWriter(writer io.Writer) {
    66  	s.debug = debug.New(writer, "toml", "scanner")
    67  }
    68  
    69  func (s *Scanner) SetTraceWriter(_ io.Writer)        {}
    70  func (s *Scanner) SetPerResultTracingEnabled(_ bool) {}
    71  
    72  func (s *Scanner) SetPolicyDirs(dirs ...string) {
    73  	s.policyDirs = dirs
    74  }
    75  
    76  func (s *Scanner) SetDataDirs(_ ...string)         {}
    77  func (s *Scanner) SetPolicyNamespaces(_ ...string) {}
    78  
    79  func (s *Scanner) SetPolicyFilesystem(_ fs.FS) {
    80  	// handled by rego when option is passed on
    81  }
    82  
    83  func (s *Scanner) SetDataFilesystem(_ fs.FS) {
    84  	// handled by rego when option is passed on
    85  }
    86  func (s *Scanner) SetRegoErrorLimit(_ int) {}
    87  
    88  func NewScanner(opts ...options.ScannerOption) *Scanner {
    89  	s := &Scanner{
    90  		options: opts,
    91  	}
    92  	for _, opt := range opts {
    93  		opt(s)
    94  	}
    95  	s.parser = parser.New(options.ParserWithSkipRequiredCheck(s.skipRequired))
    96  	return s
    97  }
    98  
    99  func (s *Scanner) ScanFS(ctx context.Context, fs fs.FS, path string) (scan.Results, error) {
   100  
   101  	files, err := s.parser.ParseFS(ctx, fs, path)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	if len(files) == 0 {
   107  		return nil, nil
   108  	}
   109  
   110  	var inputs []rego.Input
   111  	for path, file := range files {
   112  		inputs = append(inputs, rego.Input{
   113  			Path:     path,
   114  			Contents: file,
   115  			FS:       fs,
   116  		})
   117  	}
   118  
   119  	results, err := s.scanRego(ctx, fs, inputs...)
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  	return results, nil
   124  }
   125  
   126  func (s *Scanner) ScanFile(ctx context.Context, fs fs.FS, path string) (scan.Results, error) {
   127  	parsed, err := s.parser.ParseFile(ctx, fs, path)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  	s.debug.Log("Scanning %s...", path)
   132  	return s.scanRego(ctx, fs, rego.Input{
   133  		Path:     path,
   134  		Contents: parsed,
   135  	})
   136  }
   137  
   138  func (s *Scanner) initRegoScanner(srcFS fs.FS) (*rego.Scanner, error) {
   139  	s.Lock()
   140  	defer s.Unlock()
   141  	if s.regoScanner != nil {
   142  		return s.regoScanner, nil
   143  	}
   144  	regoScanner := rego.NewScanner(types.SourceTOML, s.options...)
   145  	regoScanner.SetParentDebugLogger(s.debug)
   146  	if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, srcFS, s.policyDirs, s.policyReaders); err != nil {
   147  		return nil, err
   148  	}
   149  	s.regoScanner = regoScanner
   150  	return regoScanner, nil
   151  }
   152  
   153  func (s *Scanner) scanRego(ctx context.Context, srcFS fs.FS, inputs ...rego.Input) (scan.Results, error) {
   154  	regoScanner, err := s.initRegoScanner(srcFS)
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  	results, err := regoScanner.ScanInput(ctx, inputs...)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	results.SetSourceAndFilesystem("", srcFS, false)
   163  	return results, nil
   164  }