fix: revert the overriden ScanningTarget when submitting reports

Before this change, we used to override a scan info `ScanningTarget` to
submit a result that is compatible with the backend for Kubescape.
However, previously we forgot to change back to the original value.

When printing scan results, if the correct order of events (Print →
Score → Submit) was not enforced, this broke the SARIF printer so that
it did not output results due to incorrect `basePath` for the results.

This change reverts to the original `ScanningTarget` value after
submitting the results and fixes the SARIF printer.
This commit is contained in:
Vlad Klokun
2022-12-22 15:00:59 +02:00
committed by Vlad Klokun
parent f48f81c0b5
commit 73c55fe253
2 changed files with 57 additions and 2 deletions
@@ -85,10 +85,18 @@ func (report *ReportEventReceiver) SetClusterName(clusterName string) {
}
func (report *ReportEventReceiver) prepareReport(opaSessionObj *cautils.OPASessionObj) error {
// All scans whose target is not a cluster, currently their target is a file, which is what the backend expects
// (e.g. local-git, directory, etc)
// The backend for Kubescape expects scanning targets to be either
// Clusters or Files, not other types we support (GitLocal, Directory
// etc). So, to submit a compatible report to the backend, we have to
// override the scanning target, submit the report and then restore the
// original value.
originalScanningTarget := opaSessionObj.Metadata.ScanMetadata.ScanningTarget
if opaSessionObj.Metadata.ScanMetadata.ScanningTarget != reporthandlingv2.Cluster {
opaSessionObj.Metadata.ScanMetadata.ScanningTarget = reporthandlingv2.File
defer func() {
opaSessionObj.Metadata.ScanMetadata.ScanningTarget = originalScanningTarget
}()
}
report.initEventReceiverURL()
@@ -5,6 +5,7 @@ import (
"testing"
"github.com/kubescape/kubescape/v2/core/cautils"
reporthandlingv2 "github.com/kubescape/opa-utils/reporthandling/v2"
"github.com/stretchr/testify/assert"
)
@@ -106,3 +107,49 @@ func TestGetURL(t *testing.T) {
assert.Equal(t, "https://cloud.armosec.io/account/sign-up?customerGUID=1234&invitationToken=token&utm_campaign=Submit&utm_medium=CLI&utm_source=GitHub", reporter.GetURL())
}
}
func Test_prepareReportKeepsOriginalScanningTarget(t *testing.T) {
// prepareReport should keep the original scanning target it received, and not mutate it
testCases := []struct {
Name string
Want reporthandlingv2.ScanningTarget
}{
{"Cluster", reporthandlingv2.Cluster},
{"File", reporthandlingv2.File},
{"Repo", reporthandlingv2.Repo},
{"GitLocal", reporthandlingv2.GitLocal},
{"Directory", reporthandlingv2.Directory},
}
reporter := NewReportEventReceiver(
&cautils.ConfigObj{
AccountID: "1e3ae7c4-a8bb-4d7c-9bdf-eb86bc25e6bb",
Token: "token",
ClusterName: "test",
},
"",
SubmitContextScan,
)
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
want := tc.Want
opaSessionObj := &cautils.OPASessionObj{
Report: &reporthandlingv2.PostureReport{},
Metadata: &reporthandlingv2.Metadata{
ScanMetadata: reporthandlingv2.ScanMetadata{ScanningTarget: want},
},
}
reporter.prepareReport(opaSessionObj)
got := opaSessionObj.Metadata.ScanMetadata.ScanningTarget
if got != want {
t.Errorf("Scanning targets dont match after preparing report. Got: %v, want %v", got, want)
}
},
)
}
}