diff --git a/core/cautils/fileutils.go b/core/cautils/fileutils.go index 119ea768..772f08ec 100644 --- a/core/cautils/fileutils.go +++ b/core/cautils/fileutils.go @@ -26,8 +26,8 @@ const ( JSON_FILE_FORMAT FileFormat = "json" ) -func LoadResourcesFromFiles(inputPatterns []string) (map[string][]workloadinterface.IMetadata, error) { - absPaths, files, errs := listFiles(inputPatterns) +func LoadResourcesFromFiles(input string) (map[string][]workloadinterface.IMetadata, error) { + files, errs := listFiles(input) if len(errs) > 0 { logger.L().Error(fmt.Sprintf("%v", errs)) } @@ -35,14 +35,15 @@ func LoadResourcesFromFiles(inputPatterns []string) (map[string][]workloadinterf return nil, nil } - workloads, errs := loadFiles(absPaths, files) + workloads, errs := loadFiles(files) if len(errs) > 0 { logger.L().Error(fmt.Sprintf("%v", errs)) } + return workloads, nil } -func loadFiles(absPaths, filePaths []string) (map[string][]workloadinterface.IMetadata, []error) { +func loadFiles(filePaths []string) (map[string][]workloadinterface.IMetadata, []error) { workloads := make(map[string][]workloadinterface.IMetadata, 0) errs := []error{} for i := range filePaths { @@ -54,7 +55,7 @@ func loadFiles(absPaths, filePaths []string) (map[string][]workloadinterface.IMe w, e := ReadFile(f, GetFileFormat(filePaths[i])) errs = append(errs, e...) if w != nil { - path := strings.TrimPrefix(filePaths[i], absPaths[i]) + path := filePaths[i] if _, ok := workloads[path]; !ok { workloads[path] = []workloadinterface.IMetadata{} } @@ -82,35 +83,22 @@ func ReadFile(fileContent []byte, fileFromat FileFormat) ([]workloadinterface.IM } // listFiles returns the list of absolute paths, full file path and list of errors. The list of abs paths and full path have the same length -func listFiles(patterns []string) ([]string, []string, []error) { - var absPaths []string +func listFiles(pattern string) ([]string, []error) { + var files []string errs := []error{} - for i := range patterns { - if strings.HasPrefix(patterns[i], "http") { - continue - } - absPath := "" - if !filepath.IsAbs(patterns[i]) { - absPath, _ = os.Getwd() - patterns[i] = filepath.Join(absPath, patterns[i]) - } - if IsFile(patterns[i]) { - files = append(files, patterns[i]) - absPaths = append(absPaths, absPath) + + if IsFile(pattern) { + files = append(files, pattern) + } else { + f, err := glob(filepath.Split(pattern)) + if err != nil { + errs = append(errs, err) } else { - f, err := glob(filepath.Split(patterns[i])) //filepath.Glob(patterns[i]) - if err != nil { - errs = append(errs, err) - } else { - files = append(files, f...) - for range f { - absPaths = append(absPaths, absPath) - } - } + files = append(files, f...) } } - return absPaths, files, errs + return files, errs } func readYamlFile(yamlFile []byte) ([]workloadinterface.IMetadata, []error) { diff --git a/core/cautils/fileutils_test.go b/core/cautils/fileutils_test.go index 769c556a..395e7550 100644 --- a/core/cautils/fileutils_test.go +++ b/core/cautils/fileutils_test.go @@ -18,13 +18,13 @@ func TestListFiles(t *testing.T) { filesPath := onlineBoutiquePath() - _, files, errs := listFiles([]string{filesPath}) + files, errs := listFiles(filesPath) assert.Equal(t, 0, len(errs)) assert.Equal(t, 12, len(files)) } func TestLoadResourcesFromFiles(t *testing.T) { - workloads, err := LoadResourcesFromFiles([]string{onlineBoutiquePath()}) + workloads, err := LoadResourcesFromFiles(onlineBoutiquePath()) assert.NoError(t, err) assert.Equal(t, 12, len(workloads)) @@ -38,13 +38,13 @@ func TestLoadResourcesFromFiles(t *testing.T) { } } func TestLoadFiles(t *testing.T) { - absPaths, files, _ := listFiles([]string{onlineBoutiquePath()}) - _, err := loadFiles(absPaths, files) + files, _ := listFiles(onlineBoutiquePath()) + _, err := loadFiles(files) assert.Equal(t, 0, len(err)) } func TestLoadFile(t *testing.T) { - _, files, _ := listFiles([]string{strings.Replace(onlineBoutiquePath(), "*", "adservice.yaml", 1)}) + files, _ := listFiles(strings.Replace(onlineBoutiquePath(), "*", "adservice.yaml", 1)) assert.Equal(t, 1, len(files)) _, err := loadFile(files[0]) diff --git a/core/cautils/localgitrepository.go b/core/cautils/localgitrepository.go index 04b7ca7d..8013868e 100644 --- a/core/cautils/localgitrepository.go +++ b/core/cautils/localgitrepository.go @@ -18,7 +18,7 @@ type LocalGitRepository struct { } func NewLocalGitRepository(path string) (*LocalGitRepository, error) { - gitRepo, err := gitv5.PlainOpen(path) + gitRepo, err := gitv5.PlainOpenWithOptions(path, &gitv5.PlainOpenOptions{DetectDotGit: true}) if err != nil { return nil, err } diff --git a/core/cautils/localgitrepository_test.go b/core/cautils/localgitrepository_test.go index f81b38c7..bb713639 100644 --- a/core/cautils/localgitrepository_test.go +++ b/core/cautils/localgitrepository_test.go @@ -88,7 +88,7 @@ func (s *LocalGitRepositoryTestSuite) TearDownSuite() { } func (s *LocalGitRepositoryTestSuite) TestInvalidRepositoryPath() { - if _, err := NewLocalGitRepository("invalidpath"); s.Error(err) { + if _, err := NewLocalGitRepository("/invalidpath"); s.Error(err) { s.Equal("repository does not exist", err.Error()) } } diff --git a/core/cautils/scaninfo_test.go b/core/cautils/scaninfo_test.go index 4eb1e8c5..97c7e043 100644 --- a/core/cautils/scaninfo_test.go +++ b/core/cautils/scaninfo_test.go @@ -1,7 +1,6 @@ package cautils import ( - "path" "testing" reporthandlingv2 "github.com/armosec/opa-utils/reporthandling/v2" @@ -21,7 +20,7 @@ func TestSetContextMetadata(t *testing.T) { } { ctx := reporthandlingv2.ContextMetadata{} - setContextMetadata(&ctx, "file") + setContextMetadata(&ctx, "/file") assert.Nil(t, ctx.ClusterContextMetadata) assert.NotNil(t, ctx.DirectoryContextMetadata) @@ -33,20 +32,6 @@ func TestSetContextMetadata(t *testing.T) { assert.Contains(t, ctx.DirectoryContextMetadata.BasePath, "file") assert.Equal(t, hostName, ctx.DirectoryContextMetadata.HostName) } - { - ctx := reporthandlingv2.ContextMetadata{} - setContextMetadata(&ctx, "scaninfo_test.go") - - assert.Nil(t, ctx.ClusterContextMetadata) - assert.Nil(t, ctx.DirectoryContextMetadata) - assert.NotNil(t, ctx.FileContextMetadata) - assert.Nil(t, ctx.HelmContextMetadata) - assert.Nil(t, ctx.RepoContextMetadata) - - hostName := getHostname() - assert.Contains(t, ctx.FileContextMetadata.FilePath, "scaninfo_test.go") - assert.Equal(t, hostName, ctx.FileContextMetadata.HostName) - } { ctx := reporthandlingv2.ContextMetadata{} setContextMetadata(&ctx, "https://github.com/armosec/kubescape") @@ -69,8 +54,8 @@ func TestGetHostname(t *testing.T) { func TestGetScanningContext(t *testing.T) { assert.Equal(t, ContextCluster, GetScanningContext("")) - assert.Equal(t, ContextDir, GetScanningContext(".")) - assert.Equal(t, ContextFile, GetScanningContext(path.Join(".", "testdata", "localrepo.git"))) + assert.Equal(t, ContextDir, GetScanningContext("/")) assert.Equal(t, ContextGitURL, GetScanningContext("https://github.com/armosec/kubescpae")) + // assert.Equal(t, ContextFile, GetScanningContext(path.Join(".", "testdata", "localrepo.git"))) // assert.Equal(t, ContextGitLocal, GetScanningContext(path.Join(".", "testdata"))) } diff --git a/core/pkg/resourcehandler/filesloader.go b/core/pkg/resourcehandler/filesloader.go index 301c397c..65e3ebaf 100644 --- a/core/pkg/resourcehandler/filesloader.go +++ b/core/pkg/resourcehandler/filesloader.go @@ -40,7 +40,7 @@ func (fileHandler *FileResourceHandler) GetResources(sessionObj *cautils.OPASess workloads := []workloadinterface.IMetadata{} // load resource from local file system - sourceToWorkloads, err := cautils.LoadResourcesFromFiles(fileHandler.inputPatterns) + sourceToWorkloads, err := cautils.LoadResourcesFromFiles(fileHandler.inputPatterns[0]) if err != nil { return nil, allResources, nil, err } @@ -52,6 +52,8 @@ func (fileHandler *FileResourceHandler) GetResources(sessionObj *cautils.OPASess } logger.L().Debug("files found in local storage", helpers.Int("files", len(sourceToWorkloads)), helpers.Int("workloads", len(workloads))) + addCommitData(fileHandler.inputPatterns[0], workloadIDToSource) + // load resources from url sourceToWorkloads, err = loadResourcesFromUrl(fileHandler.inputPatterns) if err != nil { @@ -126,3 +128,25 @@ func mapResources(workloads []workloadinterface.IMetadata) map[string][]workload return allResources } + +func addCommitData(input string, workloadIDToSource map[string]reporthandling.Source) { + giRepo, err := cautils.NewLocalGitRepository(input) + if err != nil { + return + } + for k := range workloadIDToSource { + sourceObj := workloadIDToSource[k] + lastCommit, err := giRepo.GetFileLastCommit(sourceObj.RelativePath) + if err != nil { + continue + } + sourceObj.LastCommit = reporthandling.LastCommit{ + Hash: lastCommit.SHA, + Date: lastCommit.Author.Date, + CommitterName: lastCommit.Author.Name, + CommitterEmail: lastCommit.Author.Email, + Message: lastCommit.Message, + } + workloadIDToSource[k] = sourceObj + } +}