mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
Merge pull request #945 from suhasgumma/fix-command1
Add support for fixing Individual Files using "fix" command
This commit is contained in:
@@ -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 ""
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user