Skip to content

Commit

Permalink
checkmarxOneExecuteScan - Fix report generation in CxOne 3.20 (#5170)
Browse files Browse the repository at this point in the history
* Initial in progress

* compiling but not yet functional

* Missed file

* updated checkmarxone step

* Working up to fetching a project then breaks

* Missed file

* Breaks when retrieving projects+proxy set

* Create project & run scan working, now polling

* Fixed polling

* added back the zipfile remove command

* Fixed polling again

* Generates and downloads PDF report

* Updated and working, prep for refactor

* Added compliance steps

* Cleanup, reporting, added groovy connector

* fixed groovy file

* checkmarxone to checkmarxOne

* checkmarxone to checkmarxOne

* split credentials (id+secret, apikey), renamed pullrequestname to branch, groovy fix

* Fixed filenames & yaml

* missed the metadata_generated.go

* added json to sarif conversion

* fix:type in new checkmarxone package

* fix:type in new checkmarxone package

* removed test logs, added temp error log for creds

* extra debugging to fix crash

* improved auth logging, fixed query parse issue

* fixed bug with group fetch when using oauth user

* CWE can be -1 if not defined, can't be uint

* Query also had CweID

* Disabled predicates-fetch in sarif generation

* Removing leftover info log message

* Better error handling

* fixed default preset configuration

* removing .bat files - sorry

* Cleanup per initial review

* refactoring per Gist, fixed project find, add apps

* small fix - sorry for commit noise while testing

* Fixing issues with incremental scans.

* removing maxretries

* Updated per PR feedback, further changes todo toda

* JSON Report changes and reporting cleanup

* removing .bat (again?)

* adding docs, groovy unit test, linter fixes

* Started adding tests maybe 15% covered

* fix(checkmarxOne): test cases for pkg and reporting

* fix(checkmarxOne):fix formatting

* feat(checkmarxone): update interface with missing method

* feat(checkmarxone):change runStep signature to be able to inject dependency

* feat(checkmarxone): add tests for step (wip)

* Adding a bit more coverage

* feat(checkmarxOne): fix code review

* feat(checkmarxOne): fix code review

* feat(checkmarxOne): fix code review

* feat(checkmarxOne): fix integration test PR

* adding scan-summary bug workaround, reportgen fail

* enforceThresholds fix when no results passed in

* fixed gap when preset empty in yaml & project conf

* fixed another gap in preset selection

* fix 0-result panic

* fail when no preset is set anywhere

* removed comment

* initial project-under-app support

* fixing sarif reportgen

* some cleanup of error messages

* post-merge test fixes

* revert previous upstream merge

* adding "incremental" to "full" triggers

* wrong boolean

* project-in-application api change prep

* Fixing SARIF report without preset access

* fix sarif deeplink

* removing comments

* fix(cxone):formatting

* fix(cxone):formatting

* small sarif fixes

* fixed merge

* attempt at pulling git source repo branch

* fix(cxone):new endpoint for project creation

* fix(cxOne): taxa is an array

* fix(cxOne): get Git branch from commonPipelineEnvironment

* fix(cxOne): add params to tag a scan and a project

* fix(cxOne): unit test - update project

* fix(cxOne): unit test - update project tags

* fix(cxOne): improve logs

* fix(cxOne): improve logs

* adding RequestNewPDFReport function using v2 api

* added version check

* fix(cxone): JSON report using v2 API

* update to set reportType in v2 reportgen

---------

Co-authored-by: michael kubiaczyk <[email protected]>
Co-authored-by: thtri <[email protected]>
Co-authored-by: Thanh-Hai Trinh <[email protected]>
Co-authored-by: michaelkubiaczyk <[email protected]>
Co-authored-by: sumeet patil <[email protected]>
  • Loading branch information
6 people authored Nov 7, 2024
1 parent c1e800a commit 9626bfc
Showing 1 changed file with 91 additions and 4 deletions.
95 changes: 91 additions & 4 deletions pkg/checkmarxone/checkmarxone.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (
"net/http"
"net/url"
"os"

//"strconv"
"strconv"
"strings"
"time"

//"encoding/xml"
piperHttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
Expand Down Expand Up @@ -1314,6 +1312,19 @@ func (sys *SystemInstance) GetResultsPredicates(SimilarityID int64, ProjectID st

// RequestNewReport triggers the generation of a report for a specific scan addressed by scanID
func (sys *SystemInstance) RequestNewReport(scanID, projectID, branch, reportType string) (string, error) {
if strings.EqualFold("pdf", reportType) || strings.EqualFold("json", reportType) {
version, err := sys.GetVersion()
if err == nil {
if version.CheckCxOne("3.20.0") >= 0 && version.CheckCxOne("3.21.0") == -1 {
sys.logger.Debugf("Current version is %v - between 3.20.0 and 3.21.0 - using v2 %v report", reportType, version.CxOne)
return sys.RequestNewReportV2(scanID, reportType)
}
sys.logger.Debugf("Current version is %v - using v1 %v report", reportType, version.CxOne)
} else {
sys.logger.Errorf("Failed to get the CxOne version during report-gen request, will use v1 %v report. Error: %s", reportType, err)
}
}

jsonData := map[string]interface{}{
"fileFormat": reportType,
"reportType": "ui",
Expand Down Expand Up @@ -1352,13 +1363,52 @@ func (sys *SystemInstance) RequestNewReport(scanID, projectID, branch, reportTyp
return reportResponse.ReportId, err
}

// Use the new V2 Report API to generate a PDF report
func (sys *SystemInstance) RequestNewReportV2(scanID, reportType string) (string, error) {
jsonData := map[string]interface{}{
"reportName": "improved-scan-report",
"entities": []map[string]interface{}{
{
"entity": "scan",
"ids": []string{scanID},
"tags": []string{},
},
},
"filters": map[string][]string{
"scanners": {"sast"},
},
"reportType": "ui",
"fileFormat": reportType,
}

jsonValue, _ := json.Marshal(jsonData)

header := http.Header{}
header.Set("cxOrigin", cxOrigin)
header.Set("Content-Type", "application/json")
data, err := sendRequest(sys, http.MethodPost, "/reports/v2", bytes.NewBuffer(jsonValue), header, []int{})
if err != nil {
return "", errors.Wrapf(err, "Failed to trigger report generation for scan %v", scanID)
} else {
sys.logger.Infof("Generating report %v", string(data))
}

var reportResponse struct {
ReportId string
}
err = json.Unmarshal(data, &reportResponse)

return reportResponse.ReportId, err

}

// GetReportStatus returns the status of the report generation process
func (sys *SystemInstance) GetReportStatus(reportID string) (ReportStatus, error) {
var response ReportStatus

header := http.Header{}
header.Set("Accept", "application/json")
data, err := sendRequest(sys, http.MethodGet, fmt.Sprintf("/reports/%v", reportID), nil, header, []int{})
data, err := sendRequest(sys, http.MethodGet, fmt.Sprintf("/reports/%v?returnUrl=true", reportID), nil, header, []int{})
if err != nil {
sys.logger.Errorf("Failed to fetch report status for reportID %v: %s", reportID, err)
return response, errors.Wrapf(err, "failed to fetch report status for reportID %v", reportID)
Expand Down Expand Up @@ -1412,3 +1462,40 @@ func (sys *SystemInstance) GetVersion() (VersionInfo, error) {
err = json.Unmarshal(data, &version)
return version, err
}

func (v VersionInfo) CheckCxOne(version string) int {
check := versionStringToInts(version)
cx1 := versionStringToInts(v.CxOne)

if check[0] < cx1[0] {
return 1
} else if check[0] > cx1[0] {
return -1
} else {
if check[1] < cx1[1] {
return 1
} else if check[1] > cx1[1] {
return -1
} else {
if check[2] < cx1[2] {
return 1
} else if check[2] > cx1[2] {
return -1
} else {
return 0
}
}
}
}

func versionStringToInts(version string) []int64 {
if version == "" {
return []int64{0, 0, 0}
}
str := strings.Split(version, ".")
ints := make([]int64, len(str))
for id, val := range str {
ints[id], _ = strconv.ParseInt(val, 10, 64)
}
return ints
}

0 comments on commit 9626bfc

Please sign in to comment.