feature: skipPersistence request parameter added

Signed-off-by: Mujib Ahasan <ahasanmujib8@gmail.com>
This commit is contained in:
Mujib Ahasan
2025-12-05 21:42:47 +05:30
parent 60d7276de3
commit efbb8e8367
4 changed files with 22 additions and 15 deletions

View File

@@ -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,

View File

@@ -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

View File

@@ -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
}

View File

@@ -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