-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser.go
194 lines (165 loc) · 4.3 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package parser
import (
"context"
"fmt"
"io"
"io/fs"
"path/filepath"
"strings"
"github.com/aquasecurity/defsec/pkg/debug"
"github.com/aquasecurity/defsec/pkg/types"
"github.com/aquasecurity/defsec/pkg/scanners/options"
"github.com/aquasecurity/trivy-iac/pkg/scanners/azure"
"github.com/aquasecurity/trivy-iac/pkg/scanners/azure/arm/parser/armjson"
"github.com/aquasecurity/trivy-iac/pkg/scanners/azure/resolver"
)
type Parser struct {
targetFS fs.FS
skipRequired bool
debug debug.Logger
}
func (p *Parser) SetDebugWriter(writer io.Writer) {
p.debug = debug.New(writer, "azure", "arm")
}
func (p *Parser) SetSkipRequiredCheck(b bool) {
p.skipRequired = b
}
func New(targetFS fs.FS, opts ...options.ParserOption) *Parser {
p := &Parser{
targetFS: targetFS,
}
for _, opt := range opts {
opt(p)
}
return p
}
func (p *Parser) ParseFS(ctx context.Context, dir string) ([]azure.Deployment, error) {
var deployments []azure.Deployment
if err := fs.WalkDir(p.targetFS, dir, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if entry.IsDir() {
return nil
}
if !p.Required(path) {
return nil
}
f, err := p.targetFS.Open(path)
if err != nil {
return err
}
defer f.Close()
deployment, err := p.parseFile(f, path)
if err != nil {
return err
}
deployments = append(deployments, *deployment)
return nil
}); err != nil {
return nil, err
}
return deployments, nil
}
func (p *Parser) Required(path string) bool {
if p.skipRequired {
return true
}
if !strings.HasSuffix(path, ".json") {
return false
}
data, err := fs.ReadFile(p.targetFS, path)
if err != nil {
return false
}
var template Template
root := types.NewMetadata(
types.NewRange(filepath.Base(path), 0, 0, "", p.targetFS),
"",
)
if err := armjson.Unmarshal(data, &template, &root); err != nil {
p.debug.Log("Error scanning %s: %s", path, err)
return false
}
if template.Schema.Kind != azure.KindString {
return false
}
return strings.HasPrefix(template.Schema.AsString(), "https://schema.management.azure.com")
}
func (p *Parser) parseFile(r io.Reader, filename string) (*azure.Deployment, error) {
var template Template
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
root := types.NewMetadata(
types.NewRange(filename, 0, 0, "", p.targetFS),
"",
).WithInternal(resolver.NewResolver())
if err := armjson.Unmarshal(data, &template, &root); err != nil {
return nil, fmt.Errorf("failed to parse template: %w", err)
}
return p.convertTemplate(template), nil
}
func (p *Parser) convertTemplate(template Template) *azure.Deployment {
deployment := azure.Deployment{
Metadata: template.Metadata,
TargetScope: azure.ScopeResourceGroup, // TODO: override from --resource-group?
Parameters: nil,
Variables: nil,
Resources: nil,
Outputs: nil,
}
if r, ok := template.Metadata.Internal().(resolver.Resolver); ok {
r.SetDeployment(&deployment)
}
// TODO: the references passed here should probably not be the name - maybe params.NAME.DefaultValue?
for name, param := range template.Parameters {
deployment.Parameters = append(deployment.Parameters, azure.Parameter{
Variable: azure.Variable{
Name: name,
Value: param.DefaultValue,
},
Default: param.DefaultValue,
Decorators: nil,
})
}
for name, variable := range template.Variables {
deployment.Variables = append(deployment.Variables, azure.Variable{
Name: name,
Value: variable,
})
}
for name, output := range template.Outputs {
deployment.Outputs = append(deployment.Outputs, azure.Output{
Name: name,
Value: output,
})
}
for _, resource := range template.Resources {
deployment.Resources = append(deployment.Resources, p.convertResource(resource))
}
return &deployment
}
func (p *Parser) convertResource(input Resource) azure.Resource {
var children []azure.Resource
for _, child := range input.Resources {
children = append(children, p.convertResource(child))
}
resource := azure.Resource{
Metadata: input.Metadata,
APIVersion: input.APIVersion,
Type: input.Type,
Kind: input.Kind,
Name: input.Name,
Location: input.Location,
Properties: input.Properties,
Resources: children,
}
return resource
}