Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(helm): load in-memory files #6383

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 23 additions & 35 deletions pkg/iac/scanners/helm/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -192,17 +191,7 @@ func (p *Parser) extractChartName(chartPath string) error {
}

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)
workingChart, err := p.loadChart()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -246,19 +235,36 @@ func (p *Parser) getRelease(chrt *chart.Chart) (*release.Release, error) {
return r, nil
}

func loadChart(tempFs string) (*chart.Chart, error) {
loadedChart, err := loader.Load(tempFs)
func (p *Parser) loadChart() (*chart.Chart, error) {

var files []*loader.BufferedFile

for _, filePath := range p.filepaths {
b, err := fs.ReadFile(p.workingFS, filePath)
if err != nil {
return nil, err
}

filePath = strings.TrimPrefix(filePath, p.rootPath+"/")
filePath = filepath.ToSlash(filePath)
files = append(files, &loader.BufferedFile{
Name: filePath,
Data: b,
})
}

c, err := loader.LoadFiles(files)
if err != nil {
return nil, err
}

if req := loadedChart.Metadata.Dependencies; req != nil {
if err := action.CheckDependencies(loadedChart, req); err != nil {
if req := c.Metadata.Dependencies; req != nil {
if err := action.CheckDependencies(c, req); err != nil {
return nil, err
}
}

return loadedChart, nil
return c, nil
}

func (*Parser) getRenderedManifests(manifestsKeys []string, splitManifests map[string]string) []ChartFile {
Expand Down Expand Up @@ -290,24 +296,6 @@ func getManifestPath(manifest string) string {
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
Expand Down
5 changes: 1 addition & 4 deletions pkg/iac/scanners/helm/test/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ func Test_helm_parser(t *testing.T) {
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
chartName := test.chartName

t.Logf("Running test: %s", test.testName)

helmParser := parser.New(chartName)
err := helmParser.ParseFS(context.TODO(), os.DirFS(filepath.Join("testdata", chartName)), ".")
err := helmParser.ParseFS(context.TODO(), os.DirFS("testdata"), chartName)
require.NoError(t, err)
manifests, err := helmParser.RenderedChartFiles()
require.NoError(t, err)
Expand Down