From a8fa028ecc8545af193afdf0d8a8f86b19a6a1df Mon Sep 17 00:00:00 2001 From: Daniel Curran Date: Tue, 23 Apr 2024 16:18:05 -0500 Subject: [PATCH] remove output directory from file list for digest (#410) Co-authored-by: Dan Curran --- internal/chartverifier/reportBuilder.go | 16 ++++++++++++++++ internal/chartverifier/utils/logger.go | 6 +++--- internal/chartverifier/utils/logger_test.go | 4 ++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/internal/chartverifier/reportBuilder.go b/internal/chartverifier/reportBuilder.go index 9b14702c..97f825f6 100644 --- a/internal/chartverifier/reportBuilder.go +++ b/internal/chartverifier/reportBuilder.go @@ -169,6 +169,20 @@ type fileSorter struct { by func(p1, p2 *helmchart.File) bool // Closure used in the Less method. } +// If chart-verifier is run from within the helm chart directory +// the output will be sent to the OutputDirectory and affect the digest. +// This removes any verifier output files from the calculated digest +func filterOutputDirectory(files []*helmchart.File) []*helmchart.File { + n := 0 + for _, file := range files { + if !strings.Contains(file.Name, utils.OutputDirectory) { + files[n] = file + n++ + } + } + return files[:n] +} + func (by By) sort(files []*helmchart.File) { fs := &fileSorter{ files: files, @@ -200,6 +214,8 @@ func GenerateSha(rawFiles []*helmchart.File) string { chartSha := sha256.New() sortedFiles := rawFiles By(name).sort(sortedFiles) + sortedFiles = filterOutputDirectory(sortedFiles) + for _, chartFile := range sortedFiles { chartSha.Write(chartFile.Data) } diff --git a/internal/chartverifier/utils/logger.go b/internal/chartverifier/utils/logger.go index 052bae8b..8b7dfe36 100644 --- a/internal/chartverifier/utils/logger.go +++ b/internal/chartverifier/utils/logger.go @@ -39,7 +39,7 @@ var ( stderrFileName string ) -const outputDirectory string = "chartverifier" +const OutputDirectory string = "chartverifier" func InitLog(cobraCmd *cobra.Command, stdFilename string, suppressErrorLog bool) { cmd = cobraCmd @@ -123,7 +123,7 @@ func writeToFile(output string, fileName string) bool { LogError(fmt.Sprintf("error getting current working directory : %s", err)) return false } - outputDir := path.Join(currentDir, outputDirectory) + outputDir := path.Join(currentDir, OutputDirectory) outputFile := path.Join(outputDir, fileName) if _, err := os.Stat(outputDir); err != nil { // #nosec G301 @@ -200,7 +200,7 @@ func pruneLogFiles() { LogError(fmt.Sprintf("error getting current working directory : %s", err)) return } - logFilesPath := path.Join(currentDir, outputDirectory) + logFilesPath := path.Join(currentDir, OutputDirectory) if _, err := os.Stat(logFilesPath); err != nil { return diff --git a/internal/chartverifier/utils/logger_test.go b/internal/chartverifier/utils/logger_test.go index 0759e187..df09956f 100644 --- a/internal/chartverifier/utils/logger_test.go +++ b/internal/chartverifier/utils/logger_test.go @@ -176,7 +176,7 @@ func checkAndOrDeleteFiles(fileType string, expectedContent string) bool { "ror getting current working directory : %s", err)) return false } - logFilesPath := path.Join(currentDir, outputDirectory) + logFilesPath := path.Join(currentDir, OutputDirectory) files, err := os.ReadDir(logFilesPath) if err != nil { @@ -222,7 +222,7 @@ func howManyLogFiles() int { fmt.Printf("error getting current working directory : %s\n", err) return 0 } - logFilesPath := path.Join(currentDir, outputDirectory) + logFilesPath := path.Join(currentDir, OutputDirectory) files, err := os.ReadDir(logFilesPath) if err != nil {