2021-06-15 14:53:42 +02:00
|
|
|
package fortify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
2021-10-29 10:03:01 +02:00
|
|
|
"encoding/json"
|
2021-06-15 14:53:42 +02:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2021-10-29 10:03:01 +02:00
|
|
|
"strconv"
|
2021-06-15 14:53:42 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-02-23 10:30:19 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/format"
|
2021-06-15 14:53:42 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/reporting"
|
|
|
|
"github.com/piper-validation/fortify-client-go/models"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FortifyReportData struct {
|
2021-10-29 10:03:01 +02:00
|
|
|
ToolName string `json:"toolName"`
|
|
|
|
ToolInstance string `json:"toolInstance"`
|
|
|
|
ProjectName string `json:"projectName"`
|
|
|
|
ProjectVersion string `json:"projectVersion"`
|
|
|
|
ProjectVersionID int64 `json:"projectVersionID"`
|
|
|
|
Violations int `json:"violations"`
|
|
|
|
CorporateTotal int `json:"corporateTotal"`
|
|
|
|
CorporateAudited int `json:"corporateAudited"`
|
|
|
|
AuditAllTotal int `json:"auditAllTotal"`
|
|
|
|
AuditAllAudited int `json:"auditAllAudited"`
|
|
|
|
SpotChecksTotal int `json:"spotChecksTotal"`
|
|
|
|
SpotChecksAudited int `json:"spotChecksAudited"`
|
|
|
|
SpotChecksGap int `json:"spotChecksGap"`
|
|
|
|
Suspicious int `json:"suspicious"`
|
|
|
|
Exploitable int `json:"exploitable"`
|
|
|
|
Suppressed int `json:"suppressed"`
|
|
|
|
AtleastOneSpotChecksCategoryAudited bool `json:"atleastOneSpotChecksCategoryAudited"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
SpotChecksCategories *[]SpotChecksAuditCount `json:"spotChecksCategories"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SpotChecksAuditCount struct {
|
|
|
|
Audited int `json:"spotChecksCategories"`
|
|
|
|
Total int `json:"total"`
|
|
|
|
Type string `json:"type"`
|
2021-06-15 14:53:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateCustomReport(data FortifyReportData, issueGroups []*models.ProjectVersionIssueGroup) reporting.ScanReport {
|
|
|
|
|
|
|
|
scanReport := reporting.ScanReport{
|
|
|
|
Title: "Fortify SAST Report",
|
|
|
|
Subheaders: []reporting.Subheader{
|
|
|
|
{Description: "Fortify project name", Details: data.ProjectName},
|
|
|
|
{Description: "Fortify project version", Details: data.ProjectVersion},
|
|
|
|
},
|
|
|
|
Overview: []reporting.OverviewRow{
|
2021-07-09 10:19:42 +02:00
|
|
|
{Description: "Number of compliance violations", Details: fmt.Sprint(data.Violations)},
|
|
|
|
{Description: "Number of issues suppressed", Details: fmt.Sprint(data.Suppressed)},
|
2021-06-15 14:53:42 +02:00
|
|
|
{Description: "Unaudited corporate issues", Details: fmt.Sprint(data.CorporateTotal - data.CorporateAudited)},
|
|
|
|
{Description: "Unaudited audit all issues", Details: fmt.Sprint(data.AuditAllTotal - data.AuditAllAudited)},
|
|
|
|
{Description: "Unaudited spot check issues", Details: fmt.Sprint(data.SpotChecksTotal - data.SpotChecksAudited)},
|
2021-07-09 10:19:42 +02:00
|
|
|
{Description: "Number of suspicious issues", Details: fmt.Sprint(data.Suspicious)},
|
|
|
|
{Description: "Number of exploitable issues", Details: fmt.Sprint(data.Exploitable)},
|
2021-06-15 14:53:42 +02:00
|
|
|
},
|
|
|
|
ReportTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
detailTable := reporting.ScanDetailTable{
|
|
|
|
NoRowsMessage: "No findings detected",
|
|
|
|
Headers: []string{
|
|
|
|
"Issue group",
|
|
|
|
"Total count",
|
|
|
|
"Audited count",
|
|
|
|
},
|
|
|
|
WithCounter: true,
|
|
|
|
CounterHeader: "Entry #",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, group := range issueGroups {
|
|
|
|
row := reporting.ScanRow{}
|
|
|
|
row.AddColumn(fmt.Sprint(*group.CleanName), 0)
|
|
|
|
row.AddColumn(fmt.Sprint(*group.TotalCount), 0)
|
|
|
|
row.AddColumn(fmt.Sprint(*group.AuditedCount), 0)
|
|
|
|
|
|
|
|
detailTable.Rows = append(detailTable.Rows, row)
|
|
|
|
}
|
2021-09-01 14:07:12 +02:00
|
|
|
|
2021-06-15 14:53:42 +02:00
|
|
|
scanReport.DetailTable = detailTable
|
2021-09-01 14:07:12 +02:00
|
|
|
scanReport.SuccessfulScan = data.Violations == 0
|
2021-06-15 14:53:42 +02:00
|
|
|
|
|
|
|
return scanReport
|
|
|
|
}
|
|
|
|
|
2021-10-29 10:03:01 +02:00
|
|
|
func CreateJSONReport(reportData FortifyReportData, spotChecksCountByCategory []SpotChecksAuditCount, serverURL string) FortifyReportData {
|
|
|
|
reportData.AtleastOneSpotChecksCategoryAudited = true
|
|
|
|
for _, spotChecksElement := range spotChecksCountByCategory {
|
|
|
|
if spotChecksElement.Total > 0 && spotChecksElement.Audited == 0 {
|
|
|
|
reportData.AtleastOneSpotChecksCategoryAudited = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reportData.SpotChecksCategories = &spotChecksCountByCategory
|
|
|
|
reportData.URL = serverURL + "/html/ssc/version/" + strconv.FormatInt(reportData.ProjectVersionID, 10)
|
|
|
|
reportData.ToolInstance = serverURL
|
|
|
|
reportData.ToolName = "fortify"
|
|
|
|
|
|
|
|
return reportData
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteJSONReport(jsonReport FortifyReportData) ([]piperutils.Path, error) {
|
|
|
|
utils := piperutils.Files{}
|
|
|
|
reportPaths := []piperutils.Path{}
|
|
|
|
|
|
|
|
// Standard JSON Report
|
|
|
|
jsonComplianceReportPath := filepath.Join(ReportsDirectory, "piper_fortify_report.json")
|
|
|
|
// Ensure reporting directory exists
|
|
|
|
if err := utils.MkdirAll(ReportsDirectory, 0777); err != nil {
|
|
|
|
return reportPaths, errors.Wrapf(err, "failed to create report directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
file, _ := json.Marshal(jsonReport)
|
|
|
|
if err := utils.FileWrite(jsonComplianceReportPath, file, 0666); err != nil {
|
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
|
|
|
return reportPaths, errors.Wrapf(err, "failed to write fortify json compliance report")
|
|
|
|
}
|
|
|
|
reportPaths = append(reportPaths, piperutils.Path{Name: "Fortify JSON Compliance Report", Target: jsonComplianceReportPath})
|
|
|
|
|
2022-02-08 15:10:40 +02:00
|
|
|
return reportPaths, nil
|
|
|
|
}
|
|
|
|
|
2022-02-23 10:30:19 +02:00
|
|
|
func WriteSarif(sarif format.SARIF) ([]piperutils.Path, error) {
|
2022-02-08 15:10:40 +02:00
|
|
|
utils := piperutils.Files{}
|
|
|
|
reportPaths := []piperutils.Path{}
|
|
|
|
|
|
|
|
sarifReportPath := filepath.Join(ReportsDirectory, "result.sarif")
|
|
|
|
// Ensure reporting directory exists
|
|
|
|
if err := utils.MkdirAll(ReportsDirectory, 0777); err != nil {
|
|
|
|
return reportPaths, errors.Wrapf(err, "failed to create report directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
file, _ := json.MarshalIndent(sarif, "", " ")
|
|
|
|
if err := utils.FileWrite(sarifReportPath, file, 0666); err != nil {
|
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
|
|
|
return reportPaths, errors.Wrapf(err, "failed to write fortify SARIF report")
|
|
|
|
}
|
|
|
|
reportPaths = append(reportPaths, piperutils.Path{Name: "Fortify SARIF Report", Target: sarifReportPath})
|
|
|
|
|
2021-10-29 10:03:01 +02:00
|
|
|
return reportPaths, nil
|
|
|
|
}
|
|
|
|
|
2022-01-21 11:52:17 +02:00
|
|
|
func WriteCustomReports(scanReport reporting.ScanReport) ([]piperutils.Path, error) {
|
2021-06-15 14:53:42 +02:00
|
|
|
utils := piperutils.Files{}
|
|
|
|
reportPaths := []piperutils.Path{}
|
|
|
|
|
|
|
|
// ignore templating errors since template is in our hands and issues will be detected with the automated tests
|
|
|
|
htmlReport, _ := scanReport.ToHTML()
|
|
|
|
htmlReportPath := filepath.Join(ReportsDirectory, "piper_fortify_report.html")
|
|
|
|
// Ensure reporting directory exists
|
|
|
|
if err := utils.MkdirAll(ReportsDirectory, 0777); err != nil {
|
|
|
|
return reportPaths, errors.Wrapf(err, "failed to create report directory")
|
|
|
|
}
|
|
|
|
if err := utils.FileWrite(htmlReportPath, htmlReport, 0666); err != nil {
|
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
|
|
|
return reportPaths, errors.Wrapf(err, "failed to write html report")
|
|
|
|
}
|
|
|
|
reportPaths = append(reportPaths, piperutils.Path{Name: "Fortify Report", Target: htmlReportPath})
|
|
|
|
|
|
|
|
// we do not add the json report to the overall list of reports for now,
|
|
|
|
// since it is just an intermediary report used as input for later
|
|
|
|
// and there does not seem to be real benefit in archiving it.
|
|
|
|
return reportPaths, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func reportShaFortify(parts []string) string {
|
|
|
|
reportShaData := []byte(strings.Join(parts, ","))
|
|
|
|
return fmt.Sprintf("%x", sha1.Sum(reportShaData))
|
|
|
|
}
|