From efbb8e8367f1b9ab43fad9c93dcca05c770c52de Mon Sep 17 00:00:00 2001 From: Mujib Ahasan Date: Fri, 5 Dec 2025 21:42:47 +0530 Subject: [PATCH] feature: skipPersistence request parameter added Signed-off-by: Mujib Ahasan --- httphandler/handlerequests/v1/prometheus.go | 7 +++--- .../handlerequests/v1/requestparser.go | 3 +++ .../handlerequests/v1/requestshandler_test.go | 4 ++-- .../handlerequests/v1/requestshandlerutils.go | 23 +++++++++++-------- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/httphandler/handlerequests/v1/prometheus.go b/httphandler/handlerequests/v1/prometheus.go index c5d6f9c2..add6e5b3 100644 --- a/httphandler/handlerequests/v1/prometheus.go +++ b/httphandler/handlerequests/v1/prometheus.go @@ -34,21 +34,22 @@ func (handler *HTTPHandler) Metrics(w http.ResponseWriter, r *http.Request) { handler.state.setBusy(scanID) defer handler.state.setNotBusy(scanID) - // Parse query parameters metricsQueryParams := &MetricsQueryParams{} if err := schema.NewDecoder().Decode(metricsQueryParams, r.URL.Query()); err != nil { w.WriteHeader(http.StatusBadRequest) handler.writeError(w, fmt.Errorf("failed to parse query params, reason: %s", err.Error()), scanID) return } + skipPersistence := r.URL.Query().Get("skipPersistence") == "true" resultsFile := filepath.Join(OutputDir, scanID) scanInfo := getPrometheusDefaultScanCommand(scanID, resultsFile, metricsQueryParams.Frameworks) scanParams := &scanRequestParams{ scanQueryParams: &ScanQueryParams{ - ReturnResults: true, - KeepResults: false, + ReturnResults: true, + KeepResults: false, + SkipPersistence: skipPersistence, }, scanInfo: scanInfo, scanID: scanID, diff --git a/httphandler/handlerequests/v1/requestparser.go b/httphandler/handlerequests/v1/requestparser.go index 1a7fe5d0..85df4c76 100644 --- a/httphandler/handlerequests/v1/requestparser.go +++ b/httphandler/handlerequests/v1/requestparser.go @@ -21,6 +21,9 @@ type ScanQueryParams struct { // Do not delete results after returning (relevant only for synchronous requests) // default: false KeepResults bool `schema:"keep" json:"keep"` + // Donot persist data after scanning + //default: false + SkipPersistence bool `schema:"skipPersistence" json:"skipPersistence"` } // swagger:parameters getScanResults diff --git a/httphandler/handlerequests/v1/requestshandler_test.go b/httphandler/handlerequests/v1/requestshandler_test.go index b86bec0d..01e94514 100644 --- a/httphandler/handlerequests/v1/requestshandler_test.go +++ b/httphandler/handlerequests/v1/requestshandler_test.go @@ -22,7 +22,7 @@ func testBody(t *testing.T) io.Reader { return bytes.NewReader(b) } -type scanner func(_ context.Context, _ *cautils.ScanInfo, _ string) (*reporthandlingv2.PostureReport, error) +type scanner func(_ context.Context, _ *cautils.ScanInfo, _ string, _ bool) (*reporthandlingv2.PostureReport, error) // TestScan tests that the scan handler passes the scan requests correctly to the underlying scan engine. func TestScan(t *testing.T) { @@ -30,7 +30,7 @@ func TestScan(t *testing.T) { // Our scanner is not setting up the k8s connection; the test is covering the rest of the wiring // that the signaling from the http handler goes all the way to the scanner implementation. defer func(o scanner) { scanImpl = o }(scanImpl) - scanImpl = func(context.Context, *cautils.ScanInfo, string) (*reporthandlingv2.PostureReport, error) { + scanImpl = func(context.Context, *cautils.ScanInfo, string, bool) (*reporthandlingv2.PostureReport, error) { return nil, nil } diff --git a/httphandler/handlerequests/v1/requestshandlerutils.go b/httphandler/handlerequests/v1/requestshandlerutils.go index cbe3449e..8e908bdc 100644 --- a/httphandler/handlerequests/v1/requestshandlerutils.go +++ b/httphandler/handlerequests/v1/requestshandlerutils.go @@ -30,7 +30,7 @@ func (handler *HTTPHandler) executeScan(scanReq *scanRequestParams) { response := &utilsmetav1.Response{} logger.L().Info("scan triggered", helpers.String("ID", scanReq.scanID)) - _, err := scanImpl(scanReq.ctx, scanReq.scanInfo, scanReq.scanID) + _, err := scanImpl(scanReq.ctx, scanReq.scanInfo, scanReq.scanID, scanReq.scanQueryParams.SkipPersistence) if err != nil { logger.L().Ctx(scanReq.ctx).Error("scanning failed", helpers.String("ID", scanReq.scanID), helpers.Error(err)) if scanReq.scanQueryParams.ReturnResults { @@ -62,7 +62,7 @@ func (handler *HTTPHandler) watchForScan() { handler.executeScan(scanReq) } } -func scan(ctx context.Context, scanInfo *cautils.ScanInfo, scanID string) (*reporthandlingv2.PostureReport, error) { +func scan(ctx context.Context, scanInfo *cautils.ScanInfo, scanID string, skipPersistence bool) (*reporthandlingv2.PostureReport, error) { ctx, spanScan := otel.Tracer("").Start(ctx, "kubescape.scan") defer spanScan.End() @@ -86,16 +86,19 @@ func scan(ctx context.Context, scanInfo *cautils.ScanInfo, scanID string) (*repo if err := result.HandleResults(ctx, scanInfo); err != nil { return nil, err } - store := storage.GetStorage() - // do not store results locally when we are sending them - if store != nil && config.GetAccount() == "" { - pr := result.GetResults() - if err := store.StorePostureReportResults(ctx, pr); err != nil { - return nil, err + if !skipPersistence { + store := storage.GetStorage() + // do not store results locally when we are sending them + if store != nil && config.GetAccount() == "" { + pr := result.GetResults() + + if err := store.StorePostureReportResults(ctx, pr); err != nil { + return nil, err + } + } else { + logger.L().Debug("storage is not initialized - skipping storing results") } - } else { - logger.L().Debug("storage is not initialized - skipping storing results") } return nil, nil