core(metrics api): Update API default behavior (#1250)

* scan default frameworks

Signed-off-by: David Wertenteil <dwertent@armosec.io>

* wip: update context

Signed-off-by: David Wertenteil <dwertent@armosec.io>

* do not trigger host scan

Signed-off-by: David Wertenteil <dwertent@armosec.io>

* adding unitests

Signed-off-by: David Wertenteil <dwertent@armosec.io>

---------

Signed-off-by: David Wertenteil <dwertent@armosec.io>
This commit is contained in:
David Wertenteil
2023-06-04 15:12:31 +03:00
committed by GitHub
parent bbe46c9fab
commit 211ee487b3
2 changed files with 21 additions and 11 deletions
+16 -10
View File
@@ -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
}
@@ -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)
}