diff --git a/go.mod b/go.mod index 041aa32d..77f21c15 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/go-git/go-git/v5 v5.13.0 github.com/google/go-containerregistry v0.19.1 github.com/google/uuid v1.6.0 + github.com/johnfercher/go-tree v1.1.0 github.com/johnfercher/maroto/v2 v2.2.2 github.com/json-iterator/go v1.1.12 github.com/jwalton/gchalk v1.3.0 @@ -302,7 +303,6 @@ require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect - github.com/johnfercher/go-tree v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/jung-kurt/gofpdf v1.16.2 // indirect github.com/jwalton/go-supportscolor v1.1.0 // indirect diff --git a/httphandler/handlerequests/v1/datastructuremethods.go b/httphandler/handlerequests/v1/datastructuremethods.go index 719ae104..ce1140c4 100644 --- a/httphandler/handlerequests/v1/datastructuremethods.go +++ b/httphandler/handlerequests/v1/datastructuremethods.go @@ -1,8 +1,14 @@ package v1 import ( + "encoding/json" + "fmt" + "os" + "path/filepath" "strings" + "github.com/kubescape/go-logger" + "github.com/kubescape/go-logger/helpers" "github.com/kubescape/kubescape/v3/core/cautils" "github.com/kubescape/kubescape/v3/core/cautils/getter" apisv1 "github.com/kubescape/opa-utils/httpserver/apis/v1" @@ -67,6 +73,10 @@ func ToScanInfo(scanRequest *utilsmetav1.PostScanRequest) *cautils.ScanInfo { scanInfo.IsDeletedScanObject = *scanRequest.IsDeletedScanObject } + if scanRequest.Exceptions != nil { + scanInfo.UseExceptions = loadexception(scanRequest) + + } return scanInfo } @@ -92,3 +102,26 @@ func setTargetInScanInfo(scanRequest *utilsmetav1.PostScanRequest, scanInfo *cau scanInfo.ScanAll = true } } + +func loadexception(exceptions *utilsmetav1.PostScanRequest) (path string) { + exceptionJSON, err := json.Marshal(exceptions.Exceptions) + if err != nil { + logger.L().Error("Failed to marshal exceptions", helpers.Error(err)) + } else { + exePath, err := os.Executable() + if err != nil { + fmt.Printf("Failed to get executable path, reason: %s", err) + } + exeDir := filepath.Dir(exePath) + exdir := filepath.Dir(exeDir) + edir := filepath.Dir(exdir) + exceptionpath := filepath.Join(edir, ".kubescape", "exceptions.json") + if err := os.WriteFile(exceptionpath, exceptionJSON, 0644); err != nil { + logger.L().Error("Failed to write exceptions file to disk", helpers.String("path", exceptionpath), helpers.Error(err)) + return + } + print(exceptionpath) + return exceptionpath // to test + } + return +}