diff --git a/core/pkg/fixhandler/fixhandler.go b/core/pkg/fixhandler/fixhandler.go index 07d6d4b1..6815b9b5 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" @@ -62,11 +63,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. Supported scanning targets are: a local git repo, a directory or a file") } func getLocalPath(report *reporthandlingv2.PostureReport) string { @@ -78,6 +80,10 @@ func getLocalPath(report *reporthandlingv2.PostureReport) string { return report.Metadata.ContextMetadata.DirectoryContextMetadata.BasePath } + if report.Metadata.ScanMetadata.ScanningTarget == reporthandlingv2.File { + return filepath.Dir(report.Metadata.ContextMetadata.FileContextMetadata.FilePath) + } + return "" } diff --git a/core/pkg/resourcehandler/filesloader.go b/core/pkg/resourcehandler/filesloader.go index e59765ff..b9b9ed85 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{} @@ -108,6 +109,12 @@ func getResourcesFromPath(path string) (map[string]reporthandling.Source, []work repoRoot, _ = filepath.Abs(path) } + // 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) + } + // load resource from local file system sourceToWorkloads := cautils.LoadResourcesFromFiles(path, repoRoot) @@ -117,6 +124,7 @@ func getResourcesFromPath(path string) (map[string]reporthandling.Source, []work workloads = append(workloads, ws...) relSource, err := filepath.Rel(repoRoot, source) + if err == nil { source = relSource }