From 92a2704fa6ca23c0fc49441e2dfb124627ae7ef2 Mon Sep 17 00:00:00 2001 From: suhasgumma Date: Tue, 29 Nov 2022 23:55:19 +0530 Subject: [PATCH 1/5] Fix RelSource for Files --- core/pkg/fixhandler/fixhandler.go | 9 +++++++-- core/pkg/resourcehandler/filesloader.go | 9 ++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/pkg/fixhandler/fixhandler.go b/core/pkg/fixhandler/fixhandler.go index 07d6d4b1..bdd2df33 100644 --- a/core/pkg/fixhandler/fixhandler.go +++ b/core/pkg/fixhandler/fixhandler.go @@ -62,11 +62,12 @@ func NewFixHandler(fixInfo *metav1.FixInfo) (*FixHandler, error) { } func isSupportedScanningTarget(report *reporthandlingv2.PostureReport) error { - if report.Metadata.ScanMetadata.ScanningTarget == reporthandlingv2.GitLocal || report.Metadata.ScanMetadata.ScanningTarget == reporthandlingv2.Directory { + scanningTarget := report.Metadata.ScanMetadata.ScanningTarget + if scanningTarget == reporthandlingv2.GitLocal || scanningTarget == reporthandlingv2.Directory || scanningTarget == reporthandlingv2.File { return nil } - return fmt.Errorf("unsupported scanning target. Only local git and directory scanning targets are supported") + return fmt.Errorf("unsupported scanning target. Only local git, directory and file scanning targets are supported") } func getLocalPath(report *reporthandlingv2.PostureReport) string { @@ -78,6 +79,10 @@ func getLocalPath(report *reporthandlingv2.PostureReport) string { return report.Metadata.ContextMetadata.DirectoryContextMetadata.BasePath } + if report.Metadata.ScanMetadata.ScanningTarget == reporthandlingv2.File { + return report.Metadata.ContextMetadata.FileContextMetadata.FilePath + } + return "" } diff --git a/core/pkg/resourcehandler/filesloader.go b/core/pkg/resourcehandler/filesloader.go index e59765ff..81c25256 100644 --- a/core/pkg/resourcehandler/filesloader.go +++ b/core/pkg/resourcehandler/filesloader.go @@ -88,6 +88,7 @@ func (fileHandler *FileResourceHandler) GetResources(sessionObj *cautils.OPASess } func getResourcesFromPath(path string) (map[string]reporthandling.Source, []workloadinterface.IMetadata, error) { + workloadIDToSource := make(map[string]reporthandling.Source, 0) workloads := []workloadinterface.IMetadata{} @@ -116,7 +117,13 @@ func getResourcesFromPath(path string) (map[string]reporthandling.Source, []work for source, ws := range sourceToWorkloads { workloads = append(workloads, ws...) - relSource, err := filepath.Rel(repoRoot, source) + var relSource string + if repoRoot == source { + relSource = repoRoot + } else { + relSource, err = filepath.Rel(repoRoot, source) + } + if err == nil { source = relSource } From 15f7b9f954555146881b1d27dc62b93840246373 Mon Sep 17 00:00:00 2001 From: suhasgumma Date: Tue, 29 Nov 2022 23:57:48 +0530 Subject: [PATCH 2/5] Add Comment --- core/pkg/resourcehandler/filesloader.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/pkg/resourcehandler/filesloader.go b/core/pkg/resourcehandler/filesloader.go index 81c25256..59d6b88a 100644 --- a/core/pkg/resourcehandler/filesloader.go +++ b/core/pkg/resourcehandler/filesloader.go @@ -119,6 +119,7 @@ func getResourcesFromPath(path string) (map[string]reporthandling.Source, []work var relSource string if repoRoot == source { + // This is to preserve the relSource of individual files. relSource = repoRoot } else { relSource, err = filepath.Rel(repoRoot, source) From a755f365df97f781c4d790ae2525e42266eeb620 Mon Sep 17 00:00:00 2001 From: suhasgumma Date: Wed, 14 Dec 2022 03:07:52 +0530 Subject: [PATCH 3/5] Fixed: Fix not working when multiple individual files are passed --- core/pkg/fixhandler/fixhandler.go | 3 ++- core/pkg/resourcehandler/filesloader.go | 13 ++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/pkg/fixhandler/fixhandler.go b/core/pkg/fixhandler/fixhandler.go index bdd2df33..4ed0cc2c 100644 --- a/core/pkg/fixhandler/fixhandler.go +++ b/core/pkg/fixhandler/fixhandler.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path" + "path/filepath" "strconv" "strings" @@ -80,7 +81,7 @@ func getLocalPath(report *reporthandlingv2.PostureReport) string { } if report.Metadata.ScanMetadata.ScanningTarget == reporthandlingv2.File { - return report.Metadata.ContextMetadata.FileContextMetadata.FilePath + return filepath.Dir(report.Metadata.ContextMetadata.FileContextMetadata.FilePath) } return "" diff --git a/core/pkg/resourcehandler/filesloader.go b/core/pkg/resourcehandler/filesloader.go index 59d6b88a..7c4fd478 100644 --- a/core/pkg/resourcehandler/filesloader.go +++ b/core/pkg/resourcehandler/filesloader.go @@ -109,6 +109,11 @@ func getResourcesFromPath(path string) (map[string]reporthandling.Source, []work repoRoot, _ = filepath.Abs(path) } + // Adjusting the repoRoot incase a file is passed to scan + if cautils.IsYaml(repoRoot) { + repoRoot = filepath.Dir(repoRoot) + } + // load resource from local file system sourceToWorkloads := cautils.LoadResourcesFromFiles(path, repoRoot) @@ -117,13 +122,7 @@ func getResourcesFromPath(path string) (map[string]reporthandling.Source, []work for source, ws := range sourceToWorkloads { workloads = append(workloads, ws...) - var relSource string - if repoRoot == source { - // This is to preserve the relSource of individual files. - relSource = repoRoot - } else { - relSource, err = filepath.Rel(repoRoot, source) - } + relSource, err := filepath.Rel(repoRoot, source) if err == nil { source = relSource From 0c23579db7ce71a7ed227cb7b40601502a04fad6 Mon Sep 17 00:00:00 2001 From: Vlad Klokun Date: Wed, 11 Jan 2023 20:59:22 +0200 Subject: [PATCH 4/5] docs: clarify the comment adjusting the repoRoot --- core/pkg/resourcehandler/filesloader.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/pkg/resourcehandler/filesloader.go b/core/pkg/resourcehandler/filesloader.go index 7c4fd478..b9b9ed85 100644 --- a/core/pkg/resourcehandler/filesloader.go +++ b/core/pkg/resourcehandler/filesloader.go @@ -109,7 +109,8 @@ func getResourcesFromPath(path string) (map[string]reporthandling.Source, []work repoRoot, _ = filepath.Abs(path) } - // Adjusting the repoRoot incase a file is passed to scan + // when scanning a single file, we consider the repository root to be + // the directory of the scanned file if cautils.IsYaml(repoRoot) { repoRoot = filepath.Dir(repoRoot) } From 9025ba5537a4a11a7f6cc01b01ea654660e104d7 Mon Sep 17 00:00:00 2001 From: Vlad Klokun Date: Wed, 11 Jan 2023 21:02:53 +0200 Subject: [PATCH 5/5] chore: reword unsupported scanning target error message --- core/pkg/fixhandler/fixhandler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/pkg/fixhandler/fixhandler.go b/core/pkg/fixhandler/fixhandler.go index 4ed0cc2c..6815b9b5 100644 --- a/core/pkg/fixhandler/fixhandler.go +++ b/core/pkg/fixhandler/fixhandler.go @@ -68,7 +68,7 @@ func isSupportedScanningTarget(report *reporthandlingv2.PostureReport) error { return nil } - return fmt.Errorf("unsupported scanning target. Only local git, directory and file scanning targets are supported") + return fmt.Errorf("unsupported scanning target. Supported scanning targets are: a local git repo, a directory or a file") } func getLocalPath(report *reporthandlingv2.PostureReport) string {