-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser.go
109 lines (93 loc) · 2.21 KB
/
parser.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package parser
import (
"bytes"
"context"
"io"
"io/fs"
"path/filepath"
"strings"
"github.com/aquasecurity/defsec/pkg/debug"
"github.com/aquasecurity/defsec/pkg/scanners/options"
"github.com/aquasecurity/trivy-iac/pkg/detection"
"gopkg.in/yaml.v3"
)
var _ options.ConfigurableParser = (*Parser)(nil)
type Parser struct {
debug debug.Logger
skipRequired bool
}
func (p *Parser) SetDebugWriter(writer io.Writer) {
p.debug = debug.New(writer, "yaml", "parser")
}
func (p *Parser) SetSkipRequiredCheck(b bool) {
p.skipRequired = b
}
// New creates a new parser
func New(opts ...options.ParserOption) *Parser {
p := &Parser{}
for _, opt := range opts {
opt(p)
}
return p
}
func (p *Parser) ParseFS(ctx context.Context, target fs.FS, path string) (map[string][]interface{}, error) {
files := make(map[string][]interface{})
if err := fs.WalkDir(target, filepath.ToSlash(path), func(path string, entry fs.DirEntry, err error) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err != nil {
return err
}
if entry.IsDir() {
return nil
}
if !p.Required(path) {
return nil
}
df, err := p.ParseFile(ctx, target, path)
if err != nil {
p.debug.Log("Parse error in '%s': %s", path, err)
return nil
}
files[path] = df
return nil
}); err != nil {
return nil, err
}
return files, nil
}
// ParseFile parses yaml content from the provided filesystem path.
func (p *Parser) ParseFile(_ context.Context, fs fs.FS, path string) ([]interface{}, error) {
f, err := fs.Open(filepath.ToSlash(path))
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
contents, err := io.ReadAll(f)
if err != nil {
return nil, err
}
var results []interface{}
marker := "\n---\n"
altMarker := "\r\n---\r\n"
if bytes.Contains(contents, []byte(altMarker)) {
marker = altMarker
}
for _, partial := range strings.Split(string(contents), marker) {
var target interface{}
if err := yaml.Unmarshal([]byte(partial), &target); err != nil {
return nil, err
}
results = append(results, target)
}
return results, nil
}
func (p *Parser) Required(path string) bool {
if p.skipRequired {
return true
}
return detection.IsType(path, nil, detection.FileTypeYAML)
}