Skip to content

Commit

Permalink
refactor improved-scan-report sections to map from scan-report
Browse files Browse the repository at this point in the history
  • Loading branch information
cx-joses committed Apr 11, 2024
1 parent 956ceb1 commit 08f4dde
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 42 deletions.
66 changes: 33 additions & 33 deletions internal/commands/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,14 @@ const (
pendingStatus = "Pending"
pdfToEmailFlagDescription = "Send the PDF report to the specified email address." +
" Use \",\" as the delimiter for multiple emails"
pdfOptionsFlagDescription = "Sections to generate PDF report. Available options: Iac-Security,Sast,Sca and " +
defaultPdfOptionsDataSections + defaultPdfOprtionsImprovedDataSections
pdfOptionsFlagDescription = "Sections to generate PDF report. Available options: Iac-Security,Sast,Sca," +
defaultPdfOptionsDataSections
sbomReportFlagDescription = "Sections to generate SBOM report. Available options: CycloneDxJson,CycloneDxXml,SpdxJson"
delayValueForReport = 10
reportNameScanReport = "scan-report"
reportNameImprovedScanReport = "improved-scan-report"
reportTypeEmail = "email"
defaultPdfOptionsDataSections = "ScanSummary,ExecutiveSummary,ScanResults with NEW_SAST_SCAN_REPORT_ENABLED feature flag disabled or "
defaultPdfOprtionsImprovedDataSections = "scan-information,results-overview,scan-results,categories,resolved-results,vulnerability-details with the flag enabled"
defaultPdfOptionsDataSections = "ScanSummary,ExecutiveSummary,ScanResults"
defaultSbomOption = "CycloneDxJson"
exploitablePathFlagDescription = "Enable or disable exploitable path in scan. Available options: true,false"
scaLastScanTimeFlagDescription = "SCA last scan time. Available options: integer above 1"
Expand Down Expand Up @@ -204,7 +203,7 @@ func resultShowSubCommand(
)
resultShowCmd.PersistentFlags().String(commonParams.ReportFormatPdfToEmailFlag, "", pdfToEmailFlagDescription)
resultShowCmd.PersistentFlags().String(commonParams.ReportSbomFormatFlag, defaultSbomOption, sbomReportFlagDescription)
resultShowCmd.PersistentFlags().String(commonParams.ReportFormatPdfOptionsFlag, "", pdfOptionsFlagDescription)
resultShowCmd.PersistentFlags().String(commonParams.ReportFormatPdfOptionsFlag, defaultPdfOptionsDataSections, pdfOptionsFlagDescription)
resultShowCmd.PersistentFlags().String(commonParams.TargetFlag, "cx_result", "Output file")
resultShowCmd.PersistentFlags().String(commonParams.TargetPathFlag, ".", "Output Path")
resultShowCmd.PersistentFlags().StringSlice(commonParams.FilterFlag, []string{}, filterResultsListFlagUsage)
Expand Down Expand Up @@ -1306,57 +1305,58 @@ func parsePDFOptions(pdfOptions string, enabledEngines []string, reportName stri
"executivesummary": "ExecutiveSummary",
"scanresults": "ScanResults",
}
var pdfOptionsSectionsMapImproved = map[string]string{
"scan-information": "scan-information",
"results-overview": "results-overview",
"scan-results": "scan-results",
"categories": "categories",
"resolved-results": "resolved-results",
"vulnerability-details": "vulnerability-details",
}

var pdfOptionsEnginesMap = map[string]string{
commonParams.ScaType: "SCA",
commonParams.SastType: "SAST",
commonParams.KicsType: "KICS",
commonParams.IacType: "KICS",
}

var pdfReportOptionsSections = map[string]map[string]string{
reportNameImprovedScanReport: pdfOptionsSectionsMapImproved,
reportNameScanReport: pdfOptionsSectionsMap,
}

var pdfReportOptionsEngines = map[string]map[string]string{
reportNameImprovedScanReport: pdfOptionsEnginesMap,
reportNameScanReport: pdfOptionsEnginesMap,
}

pdfOptions = strings.ToLower(strings.ReplaceAll(pdfOptions, " ", ""))
// if no options are provided, report service defaults to all values
if pdfOptions == "" {
return pdfOptionsSections, pdfOptionsSections, nil
}

options := strings.Split(strings.ReplaceAll(pdfOptions, "\n", ""), ",")
for _, s := range options {
if pdfReportOptionsEngines[reportName][s] != "" {
pdfOptionsEngines = append(pdfOptionsEngines, pdfReportOptionsEngines[reportName][s])
} else if pdfReportOptionsSections[reportName][s] != "" {
pdfOptionsSections = append(pdfOptionsSections, pdfReportOptionsSections[reportName][s])
if pdfOptionsEnginesMap[s] != "" {
pdfOptionsEngines = append(pdfOptionsEngines, pdfOptionsEnginesMap[s])
} else if pdfOptionsSectionsMap[s] != "" {
pdfOptionsSections = append(pdfOptionsSections, pdfOptionsSectionsMap[s])
} else {
return nil, nil, errors.Errorf("report option \"%s\" unavailable", s)
}
}
if pdfOptionsEngines == nil {
for _, engine := range enabledEngines {
if pdfOptionsEnginesMap[engine] != "" {
pdfOptionsEngines = append(pdfOptionsEngines, pdfReportOptionsEngines[reportName][engine])
pdfOptionsEngines = append(pdfOptionsEngines, pdfOptionsEnginesMap[engine])
}
}
}

if reportName == reportNameImprovedScanReport {
pdfOptionsSections = translateReportSectionsForImproved(pdfOptionsSections)
}

return pdfOptionsSections, pdfOptionsEngines, nil
}

func translateReportSectionsForImproved(sections []string) []string {
var resultSections = make([]string, 0)

var pdfOptionsSectionsImprovedTranslation = map[string][]string{
"ScanSummary": {"scan-information"},
"ExecutiveSummary": {"results-overview"},
"ScanResults": {"scan-results", "categories", "resolved-results", "vulnerability-details"},
}

for _, section := range sections {
if translatedSections := pdfOptionsSectionsImprovedTranslation[section]; translatedSections != nil {
resultSections = append(resultSections, translatedSections...)
}
}

return resultSections
}

func convertCxResultsToSarif(results *wrappers.ScanResultsCollection) *wrappers.SarifResultsCollection {
var sarif = new(wrappers.SarifResultsCollection)
sarif.Schema = "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json"
Expand Down
8 changes: 4 additions & 4 deletions internal/commands/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,15 @@ func TestRunGetResultsGeneratingPdfReportWithEmailAndOptions(t *testing.T) {
assert.NilError(t, err)
}

func TestRunGetResultsGeneratingPdfReportWithOptionsImproved(t *testing.T) {
func TestRunGetResultsGeneratingPdfReportWithOptionsImprovedMappingHappens(t *testing.T) {
mock.Flags = wrappers.FeatureFlagsResponseModel{{Name: wrappers.NewScanReportEnabled, Status: true}}
cmd := createASTTestCommand()
err := executeTestCommand(cmd,
"results", "show",
"--report-format", "pdf",
"--scan-id", "MOCK",
"--report-pdf-email", "[email protected],[email protected]",
"--report-pdf-options", "Iac-Security,Sast,Sca,scan-information")
"--report-pdf-options", "Iac-Security,Sast,Sca,scansummary,scanresults")
assert.NilError(t, err)
}

Expand All @@ -311,8 +311,8 @@ func TestRunGetResultsGeneratingPdfReportWithInvalidOptionsImproved(t *testing.T
"--report-format", "pdf",
"--scan-id", "MOCK",
"--report-pdf-email", "[email protected],[email protected]",
"--report-pdf-options", "Iac-Security,Sast,Sca,ScanSummary")
assert.Error(t, err, "report option \"scansummary\" unavailable")
"--report-pdf-options", "Iac-Security,Sast,Sca,scan-information")
assert.Error(t, err, "report option \"scan-information\" unavailable")
}

func TestRunGetResultsGeneratingPdfReportWithOptions(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ func scanCreateSubCommand(
createScanCmd.PersistentFlags().String(commonParams.ScaPrivatePackageVersionFlag, "", scaPrivatePackageVersionFlagDescription)
createScanCmd.PersistentFlags().String(commonParams.ReportFormatPdfToEmailFlag, "", pdfToEmailFlagDescription)
createScanCmd.PersistentFlags().String(commonParams.ReportSbomFormatFlag, defaultSbomOption, sbomReportFlagDescription)
createScanCmd.PersistentFlags().String(commonParams.ReportFormatPdfOptionsFlag, "", pdfOptionsFlagDescription)
createScanCmd.PersistentFlags().String(commonParams.ReportFormatPdfOptionsFlag, defaultPdfOptionsDataSections, pdfOptionsFlagDescription)
createScanCmd.PersistentFlags().String(commonParams.TargetFlag, "cx_result", "Output file")
createScanCmd.PersistentFlags().String(commonParams.TargetPathFlag, ".", "Output Path")
createScanCmd.PersistentFlags().StringSlice(commonParams.FilterFlag, []string{}, filterResultsListFlagUsage)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func TestResultsGeneratingPdfReportWithPdfOptions(t *testing.T) {
"results", "show",
flag(params.ScanIDFlag), scanID,
flag(params.TargetFormatFlag), "pdf",
flag(params.ReportFormatPdfOptionsFlag), "Iac-Security,scan-information",
flag(params.ReportFormatPdfOptionsFlag), "Iac-Security,ScanSummary,ExecutiveSummary,ScanResults",
flag(params.TargetFlag), fileName,
)
defer func() {
Expand All @@ -229,7 +229,7 @@ func TestResultsGeneratingPdfReportAndSendToEmail(t *testing.T) {
"results", "show",
flag(params.ScanIDFlag), scanID,
flag(params.TargetFormatFlag), "pdf",
flag(params.ReportFormatPdfOptionsFlag), "Iac-Security,scan-information",
flag(params.ReportFormatPdfOptionsFlag), "Iac-Security,ScanSummary,ExecutiveSummary,ScanResults",
flag(params.ReportFormatPdfToEmailFlag), "[email protected],[email protected]",
)
assert.Assert(t, outputBuffer != nil, "Scan must complete successfully")
Expand Down
2 changes: 1 addition & 1 deletion test/integration/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ func TestScanGeneratingPdfReportWithPdfOptions(t *testing.T) {
flag(params.PresetName), "Checkmarx Default",
flag(params.BranchFlag), "dummy_branch",
flag(params.TargetFormatFlag), "pdf",
flag(params.ReportFormatPdfOptionsFlag), "Iac-Security,scan-information",
flag(params.ReportFormatPdfOptionsFlag), "Iac-Security,ScanSummary,ExecutiveSummary,ScanResults",
flag(params.TargetFlag), fileName,
)
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/util_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func executeCmdWithTimeOutNilAssertion(
func executeWithTimeout(cmd *cobra.Command, timeout time.Duration, args ...string) error {

args = append(args, flag(params.RetryFlag), "3", flag(params.RetryDelayFlag), "5")
args = appendProxyArgs(args)
//args = appendProxyArgs(args)
cmd.SetArgs(args)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down

0 comments on commit 08f4dde

Please sign in to comment.