-
Notifications
You must be signed in to change notification settings - Fork 117
/
scanner.go
60 lines (47 loc) · 1.18 KB
/
scanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package terraformplan
import (
"context"
"io"
"os"
"github.com/aquasecurity/defsec/pkg/debug"
"github.com/aquasecurity/defsec/pkg/scan"
terraformScanner "github.com/aquasecurity/defsec/pkg/scanners/terraform"
"github.com/aquasecurity/defsec/pkg/scanners/terraformplan/parser"
)
type Scanner struct {
parser parser.Parser
parserOpt []parser.Option
debug debug.Logger
}
func New(options ...Option) *Scanner {
scanner := &Scanner{
parser: *parser.New(),
}
for _, o := range options {
o(scanner)
}
return scanner
}
func (s *Scanner) SetDebugWriter(writer io.Writer) {
s.debug = debug.New(writer, "tfplan", "scanner")
}
func (s *Scanner) ScanFile(filepath string) (scan.Results, error) {
s.debug.Log("Scanning file %s", filepath)
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
return s.Scan(file)
}
func (s *Scanner) Scan(reader io.Reader) (scan.Results, error) {
planFile, err := s.parser.Parse(reader)
if err != nil {
return nil, err
}
planFS, err := planFile.ToFS()
if err != nil {
return nil, err
}
scanner := terraformScanner.New(terraformScanner.ScannerWithStopOnHCLError(true))
return scanner.ScanFS(context.TODO(), planFS, ".")
}