diff --git a/.luchaignore b/.luchaignore index 929b91e..492cd21 100644 --- a/.luchaignore +++ b/.luchaignore @@ -1,8 +1,6 @@ # Test Comment -lib/test.txt lucha-sbom.json lucha.yaml lucha go.sum -go.mod -.git \ No newline at end of file +go.mod \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 8f8ccf0..c060e61 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,7 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + @@ -15,6 +16,14 @@ "program": "${workspaceRoot}/main.go", "args": ["scan", "."] }, + { + "name": "Debug (Don't ignore git)", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "${workspaceRoot}/main.go", + "args": ["scan", "--git", "."] + }, { "name": "Debug (Recursive)", "type": "go", diff --git a/cmd/scan.go b/cmd/scan.go index 5ae56ee..1b7f29d 100644 --- a/cmd/scan.go +++ b/cmd/scan.go @@ -13,6 +13,7 @@ import ( ) var ( + includeGit bool recursive bool minSeverity int scanCmd = &cobra.Command{ @@ -38,6 +39,7 @@ var ( } fs.Recursive = recursive + fs.IncludeGit = includeGit err := initScan(fs) @@ -83,6 +85,7 @@ func init() { rootCmd.AddCommand(scanCmd) scanCmd.PersistentFlags().BoolVarP(&recursive, "recursive", "r", false, "If true, lucha will recurse subdirectories") scanCmd.PersistentFlags().IntVar(&minSeverity, "min-severity", 0, "Only report on severities higher than this value") + scanCmd.PersistentFlags().BoolVarP(&includeGit, "git", "g", false, "If true, lucha not ignore the .git directory") } func initScan(fs lib.FileSystem) (err error) { diff --git a/lib/filesystem.go b/lib/filesystem.go index d8dce3c..7b49f9a 100644 --- a/lib/filesystem.go +++ b/lib/filesystem.go @@ -21,6 +21,7 @@ type FileSystem struct { fs afero.Fs SearchPath string Recursive bool + IncludeGit bool } //AbsoluteSearchPath returns the the absolute path for the (possibly) relative search path @@ -54,36 +55,6 @@ func isUTF8(fs FileSystem, file afero.File) bool { return true } -// func canIgnore(file os.FileInfo, originalRoot string, path string, recursive bool) bool { -// if !recursive && strings.Count(path, "/") > 1 { -// return true -// } -// for _, ignore := range Ignores { -// name := file.Name() -// if ignore == name { -// return true -// } -// if strings.HasPrefix(path, ignore) { -// return true -// } -// if path != "." { -// pathedIgnore := fmt.Sprintf("%s%s", originalRoot, ignore) -// if strings.HasPrefix(path, pathedIgnore) { -// return true -// } -// if strings.HasSuffix(path, ignore) { -// return true -// } -// } - -// } -// return false -// } - -// func filterFiles(fs FileSystem, fileList []string, ignoreList []string) (filteredList []string) { - -// } - func shouldIgnore(file string, ignoreList []string) (ignore bool) { var absIgnore []string @@ -107,6 +78,16 @@ func matchIgnore(s []string, str string) (matches bool) { return } +func shouldIgnoreDir(fs FileSystem, f os.FileInfo, path string) bool { + if f.IsDir() && f.Name() == ".git" { + return !fs.IncludeGit + } + if (f.IsDir() && !fs.Recursive) && fs.AbsoluteSearchPath() != path { + return true + } + return false +} + //BuildFileList gathers all of the files from the searchpath down the folder tree func BuildFileList(fs FileSystem) (fileList []string, err error) { path, err := filepath.Abs(fs.SearchPath) @@ -115,6 +96,9 @@ func BuildFileList(fs FileSystem) (fileList []string, err error) { } ignores, _ := LoadIgnore(fs) err = fs.Afero().Walk(path, func(path string, f os.FileInfo, err error) error { + if shouldIgnoreDir(fs, f, path) { + return filepath.SkipDir + } if shouldIgnore(path, ignores) { fileList = append(fileList, path) } diff --git a/lib/filesystem_test.go b/lib/filesystem_test.go index ff2bdf2..c1d76a9 100644 --- a/lib/filesystem_test.go +++ b/lib/filesystem_test.go @@ -82,3 +82,30 @@ func Test_NewOsFs(t *testing.T) { // _, err = f.BuildFileList("...", true) // assert.Error(t, err, "There should be an error because the folder ... shouldn't exist") // } + +func TestFileSystem_AbsoluteSearchPath(t *testing.T) { + fs := FileSystem{ + fs: afero.NewMemMapFs(), + SearchPath: ".", + } + assert.Contains(t, fs.AbsoluteSearchPath(), "/lucha/lib") +} + +func Test_shouldIgnoreDir(t *testing.T) { + fs := FileSystem{ + fs: afero.NewMemMapFs(), + SearchPath: ".", + } + fs.Afero().Mkdir(".git", 0644) + fi, _ := fs.Afero().ReadDir(fs.SearchPath) + assert.Len(t, fi, 1) + + dir := fi[0] + + shouldIgnore := shouldIgnoreDir(fs, dir, "") + assert.True(t, shouldIgnore) + + fs.IncludeGit = true + shouldIgnore = shouldIgnoreDir(fs, dir, "") + assert.False(t, shouldIgnore) +} diff --git a/lib/issues.go b/lib/issues.go index 729ea91..3677b1a 100644 --- a/lib/issues.go +++ b/lib/issues.go @@ -48,6 +48,7 @@ func FindIssues(fs FileSystem, minSeverity int) (violations []ScanFile, violatio } } + // this could go into a verbose or trace flag // else { // fmt.Println("Ignoring ", file.Name()) // } diff --git a/lucha-sbom.json b/lucha-sbom.json index cd3999e..88c834b 100644 --- a/lucha-sbom.json +++ b/lucha-sbom.json @@ -1,10 +1,10 @@ { "bomFormat": "CycloneDX", "specVersion": "1.2", - "serialNumber": "urn:uuid:e01de6f9-1abf-4729-a0b2-916b5c0c2008", + "serialNumber": "urn:uuid:47dac047-42ee-4302-9524-cfdda03d092f", "version": 1, "metadata": { - "timestamp": "2021-07-10T14:59:17-06:00", + "timestamp": "2021-07-12T12:21:46-06:00", "tools": [ { "vendor": "CycloneDX", @@ -31,11 +31,11 @@ } ], "component": { - "bom-ref": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210709212051-962480554a8e", + "bom-ref": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210712092326-93af2ed6aa54", "type": "application", "name": "github.com/devops-kung-fu/lucha", - "version": "v0.0.0-20210709212051-962480554a8e", - "purl": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210709212051-962480554a8e", + "version": "v0.0.0-20210712092326-93af2ed6aa54", + "purl": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210712092326-93af2ed6aa54", "externalReferences": [ { "url": "https://github.com/devops-kung-fu/lucha", @@ -520,7 +520,7 @@ "ref": "pkg:golang/gopkg.in/yaml.v2@v2.4.0" }, { - "ref": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210709212051-962480554a8e", + "ref": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210712092326-93af2ed6aa54", "dependsOn": [ "pkg:golang/github.com/briandowns/spinner@v1.16.0", "pkg:golang/github.com/dustin/go-humanize@v1.0.0",