forked from aquasecurity/trivy-iac
/
parser.go
318 lines (266 loc) · 6.98 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package parser
import (
"bytes"
"context"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"gopkg.in/yaml.v3"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/releaseutil"
"github.com/aquasecurity/defsec/pkg/debug"
"github.com/aquasecurity/defsec/pkg/scanners/options"
"github.com/google/uuid"
"github.com/nordcloud/trivy-iac/pkg/detection"
)
var manifestNameRegex = regexp.MustCompile("# Source: [^/]+/(.+)")
type Parser struct {
helmClient *action.Install
rootPath string
ChartSource string
filepaths []string
debug debug.Logger
skipRequired bool
workingFS fs.FS
valuesFiles []string
values []string
fileValues []string
stringValues []string
apiVersions []string
}
type ChartFile struct {
TemplateFilePath string
ManifestContent string
}
func (p *Parser) SetDebugWriter(writer io.Writer) {
p.debug = debug.New(writer, "helm", "parser")
}
func (p *Parser) SetSkipRequiredCheck(b bool) {
p.skipRequired = b
}
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 New(path string, options ...options.ParserOption) *Parser {
client := action.NewInstall(&action.Configuration{})
client.DryRun = true // don't do anything
client.Replace = true // skip name check
client.ClientOnly = true // don't try to talk to a cluster
p := &Parser{
helmClient: client,
ChartSource: path,
}
for _, option := range options {
option(p)
}
if p.apiVersions != nil {
p.helmClient.APIVersions = p.apiVersions
}
return p
}
func (p *Parser) ParseFS(ctx context.Context, target fs.FS, path string) error {
p.workingFS = target
if err := fs.WalkDir(p.workingFS, 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, p.workingFS) {
return nil
}
if detection.IsArchive(path) {
tarFS, err := p.addTarToFS(path)
if err != nil {
return err
}
targetPath := filepath.Dir(path)
if targetPath == "" {
targetPath = "."
}
if err := p.ParseFS(ctx, tarFS, targetPath); err != nil {
return err
}
return nil
}
return p.addPaths(path)
}); err != nil {
return err
}
return nil
}
func (p *Parser) addPaths(paths ...string) error {
for _, path := range paths {
if _, err := fs.Stat(p.workingFS, path); err != nil {
return err
}
if strings.HasSuffix(path, "Chart.yaml") && p.rootPath == "" {
if err := p.extractChartName(path); err != nil {
return err
}
p.rootPath = filepath.Dir(path)
}
p.filepaths = append(p.filepaths, path)
}
return nil
}
func (p *Parser) extractChartName(chartPath string) error {
chart, err := p.workingFS.Open(chartPath)
if err != nil {
return err
}
defer func() { _ = chart.Close() }()
var chartContent map[string]interface{}
if err := yaml.NewDecoder(chart).Decode(&chartContent); err != nil {
// the chart likely has the name templated and so cannot be parsed as yaml - use a temporary name
if dir := filepath.Dir(chartPath); dir != "" && dir != "." {
p.helmClient.ReleaseName = dir
} else {
p.helmClient.ReleaseName = uuid.NewString()
}
return nil
}
if name, ok := chartContent["name"]; !ok {
return fmt.Errorf("could not extract the chart name from %s", chartPath)
} else {
p.helmClient.ReleaseName = fmt.Sprintf("%v", name)
}
return nil
}
func (p *Parser) RenderedChartFiles() ([]ChartFile, error) {
tempDir, err := os.MkdirTemp(os.TempDir(), "defsec")
if err != nil {
return nil, err
}
if err := p.writeBuildFiles(tempDir); err != nil {
return nil, err
}
workingChart, err := loadChart(tempDir)
if err != nil {
return nil, err
}
workingRelease, err := p.getRelease(workingChart)
if err != nil {
return nil, err
}
var manifests bytes.Buffer
_, _ = fmt.Fprintln(&manifests, strings.TrimSpace(workingRelease.Manifest))
splitManifests := releaseutil.SplitManifests(manifests.String())
manifestsKeys := make([]string, 0, len(splitManifests))
for k := range splitManifests {
manifestsKeys = append(manifestsKeys, k)
}
return p.getRenderedManifests(manifestsKeys, splitManifests), nil
}
func (p *Parser) getRelease(chart *chart.Chart) (*release.Release, error) {
opts := &ValueOptions{
ValueFiles: p.valuesFiles,
Values: p.values,
FileValues: p.fileValues,
StringValues: p.stringValues,
}
vals, err := opts.MergeValues()
if err != nil {
return nil, err
}
r, err := p.helmClient.RunWithContext(context.Background(), chart, vals)
if err != nil {
return nil, err
}
if r == nil {
return nil, fmt.Errorf("there is nothing in the release")
}
return r, nil
}
func loadChart(tempFs string) (*chart.Chart, error) {
loadedChart, err := loader.Load(tempFs)
if err != nil {
return nil, err
}
if req := loadedChart.Metadata.Dependencies; req != nil {
if err := action.CheckDependencies(loadedChart, req); err != nil {
return nil, err
}
}
return loadedChart, nil
}
func (*Parser) getRenderedManifests(manifestsKeys []string, splitManifests map[string]string) []ChartFile {
sort.Sort(releaseutil.BySplitManifestsOrder(manifestsKeys))
var manifestsToRender []ChartFile
for _, manifestKey := range manifestsKeys {
manifest := splitManifests[manifestKey]
submatch := manifestNameRegex.FindStringSubmatch(manifest)
if len(submatch) == 0 {
continue
}
manifestsToRender = append(manifestsToRender, ChartFile{
TemplateFilePath: getManifestPath(manifest),
ManifestContent: manifest,
})
}
return manifestsToRender
}
func getManifestPath(manifest string) string {
lines := strings.Split(manifest, "\n")
if len(lines) == 0 {
return "unknown.yaml"
}
manifestFilePathParts := strings.SplitN(strings.TrimPrefix(lines[0], "# Source: "), "/", 2)
if len(manifestFilePathParts) > 1 {
return manifestFilePathParts[1]
}
return manifestFilePathParts[0]
}
func (p *Parser) writeBuildFiles(tempFs string) error {
for _, path := range p.filepaths {
content, err := fs.ReadFile(p.workingFS, path)
if err != nil {
return err
}
workingPath := strings.TrimPrefix(path, p.rootPath)
workingPath = filepath.Join(tempFs, workingPath)
if err := os.MkdirAll(filepath.Dir(workingPath), os.ModePerm); err != nil {
return err
}
if err := os.WriteFile(workingPath, content, os.ModePerm); err != nil {
return err
}
}
return nil
}
func (p *Parser) required(path string, workingFS fs.FS) bool {
if p.skipRequired {
return true
}
content, err := fs.ReadFile(workingFS, path)
if err != nil {
return false
}
return detection.IsType(path, bytes.NewReader(content), detection.FileTypeHelm)
}