From 5714ede70b76496719a82ea1bd0f2c0c60654219 Mon Sep 17 00:00:00 2001 From: Rafi Date: Tue, 26 Nov 2024 10:22:18 +0100 Subject: [PATCH] add latest tag Signed-off-by: Rafi --- .../commands/intoto/intoto_record.go | 29 ++++++++++++++--- .../commands/intoto/intoto_record_test.go | 32 +++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 cmd/devguard-scanner/commands/intoto/intoto_record_test.go diff --git a/cmd/devguard-scanner/commands/intoto/intoto_record.go b/cmd/devguard-scanner/commands/intoto/intoto_record.go index 80a5c3a..0af0350 100644 --- a/cmd/devguard-scanner/commands/intoto/intoto_record.go +++ b/cmd/devguard-scanner/commands/intoto/intoto_record.go @@ -22,6 +22,7 @@ import ( "strings" toto "github.com/in-toto/in-toto-golang/in_toto" + "github.com/l3montree-dev/devguard/internal/utils" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -47,6 +48,24 @@ func getTokenFromCommandOrKeyring(cmd *cobra.Command) (string, error) { return token, nil } + +func parseGitIgnore(path string) ([]string, error) { + // read .gitignore if exists + content, err := os.ReadFile(path) + if err == nil { + ignorePaths := strings.Split(string(content), "\n") + // make sure to remove new lines and empty strings + ignorePaths = utils.Filter( + utils.Map(ignorePaths, strings.TrimSpace), + func(e string) bool { + return e != "" && e != "\n" + }) + return ignorePaths, nil + } + + return nil, err +} + func parseCommand(cmd *cobra.Command) ( step string, supplyChainId string, key toto.Key, materials, products, ignore []string, err error) { token, err := getTokenFromCommandOrKeyring(cmd) @@ -68,10 +87,12 @@ func parseCommand(cmd *cobra.Command) ( return "", "", toto.Key{}, nil, nil, nil, err } - // read .gitignore if exists - content, err := os.ReadFile(".gitignore") - if err == nil { - ignore = append(ignore, strings.Split(string(content), "\n")...) + pathsFromGitIgnore, err := parseGitIgnore(".gitignore") + if err != nil { + // just swallow the error + slog.Warn("could not read .gitignore file. This is not to bad if you do not have a .gitignore file.", "error", err) + } else { + ignore = append(ignore, pathsFromGitIgnore...) } key, err = tokenToInTotoKey(token) diff --git a/cmd/devguard-scanner/commands/intoto/intoto_record_test.go b/cmd/devguard-scanner/commands/intoto/intoto_record_test.go new file mode 100644 index 0000000..4393f5b --- /dev/null +++ b/cmd/devguard-scanner/commands/intoto/intoto_record_test.go @@ -0,0 +1,32 @@ +package intotocmd + +import ( + "os" + "path" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseGitIgnore(t *testing.T) { + t.Run("parseGitIgnore with empty strings", func(t *testing.T) { + // create temp dir for testing + dir, err := os.MkdirTemp("", "test") + assert.NoError(t, err, "failed to create temporary directory") + + defer os.RemoveAll(dir) + + // Create a temporary .gitignore file for testing + gitignoreContent := "\n.DS_Store\n\t\t\t\n" + + filepath := path.Join(dir, ".gitignore") + + err = os.WriteFile(filepath, []byte(gitignoreContent), 0644) + assert.NoError(t, err, "failed to create temporary .gitignore file") + + ignorePaths, err := parseGitIgnore(filepath) + assert.NoError(t, err, "expected no error when reading .gitignore") + assert.Equal(t, []string{".DS_Store"}, ignorePaths, "unexpected ignore paths") + + }) +}