From 211ee487b3ac7731cdb700250741ea6cc4c3911c Mon Sep 17 00:00:00 2001 From: David Wertenteil Date: Sun, 4 Jun 2023 15:12:31 +0300 Subject: [PATCH] core(metrics api): Update API default behavior (#1250) * scan default frameworks Signed-off-by: David Wertenteil * wip: update context Signed-off-by: David Wertenteil * do not trigger host scan Signed-off-by: David Wertenteil * adding unitests Signed-off-by: David Wertenteil --------- Signed-off-by: David Wertenteil --- httphandler/handlerequests/v1/prometheus.go | 26 ++++++++++++------- .../handlerequests/v1/prometheus_test.go | 6 ++++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/httphandler/handlerequests/v1/prometheus.go b/httphandler/handlerequests/v1/prometheus.go index a7b114c1..f2a429ea 100644 --- a/httphandler/handlerequests/v1/prometheus.go +++ b/httphandler/handlerequests/v1/prometheus.go @@ -1,6 +1,7 @@ package v1 import ( + "context" "fmt" "net/http" "os" @@ -9,7 +10,10 @@ import ( logger "github.com/kubescape/go-logger" "github.com/kubescape/go-logger/helpers" "github.com/kubescape/kubescape/v2/core/cautils" + "github.com/kubescape/kubescape/v2/core/cautils/getter" + apisv1 "github.com/kubescape/opa-utils/httpserver/apis/v1" utilsapisv1 "github.com/kubescape/opa-utils/httpserver/apis/v1" + "go.opentelemetry.io/otel/trace" "github.com/google/uuid" ) @@ -32,7 +36,7 @@ func (handler *HTTPHandler) Metrics(w http.ResponseWriter, r *http.Request) { scanInfo: scanInfo, scanID: scanID, } - scanParams.ctx = r.Context() + scanParams.ctx = trace.ContextWithSpanContext(context.Background(), trace.SpanContextFromContext(r.Context())) handler.scanResponseChan.set(scanID) // add scan to channel defer handler.scanResponseChan.delete(scanID) @@ -69,15 +73,17 @@ func (handler *HTTPHandler) Metrics(w http.ResponseWriter, r *http.Request) { func getPrometheusDefaultScanCommand(scanID, resultsFile string) *cautils.ScanInfo { scanInfo := defaultScanInfo() - scanInfo.Submit = false // do not submit results every scan - scanInfo.Local = true // do not submit results every scan + scanInfo.UseArtifactsFrom = getter.DefaultLocalStore // Load files from cache (this will prevent kubescape fom downloading the artifacts every time) + scanInfo.Submit = false // do not submit results every scan + scanInfo.Local = true // do not submit results every scan scanInfo.FrameworkScan = true - scanInfo.ScanAll = true // scan all frameworks - scanInfo.ScanID = scanID // scan ID - scanInfo.FailThreshold = 100 // Do not fail scanning - scanInfo.ComplianceThreshold = 0 // Do not fail scanning - scanInfo.Output = resultsFile // results output - scanInfo.Format = envToString("KS_FORMAT", "prometheus") // default output should be json - scanInfo.HostSensorEnabled.SetBool(envToBool("KS_ENABLE_HOST_SCANNER", false)) // enable host scanner + scanInfo.HostSensorEnabled.SetBool(false) // disable host scanner + scanInfo.ScanAll = false // do not scan all frameworks + scanInfo.ScanID = scanID // scan ID + scanInfo.FailThreshold = 100 // Do not fail scanning + scanInfo.ComplianceThreshold = 0 // Do not fail scanning + scanInfo.Output = resultsFile // results output + scanInfo.Format = envToString("KS_FORMAT", "prometheus") // default output should be json + scanInfo.SetPolicyIdentifiers(getter.NativeFrameworks, apisv1.KindFramework) return scanInfo } diff --git a/httphandler/handlerequests/v1/prometheus_test.go b/httphandler/handlerequests/v1/prometheus_test.go index a39aa81e..5c230a08 100644 --- a/httphandler/handlerequests/v1/prometheus_test.go +++ b/httphandler/handlerequests/v1/prometheus_test.go @@ -16,6 +16,10 @@ func TestGetPrometheusDefaultScanCommand(t *testing.T) { assert.Equal(t, scanID, scanInfo.ScanID) assert.Equal(t, outputFile, scanInfo.Output) assert.Equal(t, "prometheus", scanInfo.Format) - // assert.False(t, *scanInfo.HostSensorEnabled.Get()) + assert.False(t, scanInfo.Submit) + assert.True(t, scanInfo.Local) + assert.True(t, scanInfo.FrameworkScan) + assert.False(t, scanInfo.ScanAll) + assert.False(t, scanInfo.HostSensorEnabled.GetBool()) assert.Equal(t, getter.DefaultLocalStore, scanInfo.UseArtifactsFrom) }