Skip to content

Commit

Permalink
FWI-5422 - Log CI scanning non-blocking errors (#853)
Browse files Browse the repository at this point in the history
* WIP - print scan errors

* add scan-errors logs

* bump version

* fix changelog

* fix typo

* adding test back
  • Loading branch information
vitorvezani authored Dec 18, 2023
1 parent 69da32a commit e26ec92
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions plugins/ci/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 5.3.1
* Print soft-errors to output to increase error awareness

## 5.3.0
* Added files that were modified to CI scan response

Expand Down
8 changes: 8 additions & 0 deletions plugins/ci/pkg/ci/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ func (ci *CIScan) ProcessRepository() ([]*models.ReportInfo, error) {
}

if len(scanErrorsReportProperties.Items) > 0 {
printScanErrors(scanErrorsReportProperties)
scanErrorsReport, err := ci.processScanErrorsReportProperties(scanErrorsReportProperties)
if err != nil {
return nil, fmt.Errorf("unable to process scan errors report items: %w", err)
Expand Down Expand Up @@ -806,3 +807,10 @@ func createFileFromConfig(path, filename string, cfg models.Configuration) error
}
return nil
}

func printScanErrors(scanErrorReport models.ScanErrorsReportProperties) {
fmt.Println("Scan Errors(these are soft errors that do not prevent the scan from completing):")
for _, r := range scanErrorReport.Items {
fmt.Println("\t- " + createErrorItemMessage(r))
}
}
26 changes: 26 additions & 0 deletions plugins/ci/pkg/ci/scan-errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/fairwindsops/insights-plugins/plugins/ci/pkg/models"
)
Expand All @@ -30,3 +31,28 @@ func (ci *CIScan) processScanErrorsReportProperties(reportProperties models.Scan
}
return report, nil
}

func createErrorItemMessage(r models.ScanErrorsReportResult) string {
var sb strings.Builder
if r.ErrorContext != "" {
sb.WriteString(r.ErrorContext)
sb.WriteString("/")
}
if r.Kind != "" {
sb.WriteString(r.Kind)
sb.WriteString("/")
}
if r.ResourceName != "" {
sb.WriteString(r.ResourceName)
}
if sb.Len() > 0 {
sb.WriteString(" - ")
}
sb.WriteString(r.ErrorMessage)
if r.Filename != "" {
sb.WriteString(" (")
sb.WriteString(r.Filename)
sb.WriteString(")")
}
return sb.String()
}
22 changes: 22 additions & 0 deletions plugins/ci/pkg/ci/scan-errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ci

import (
"testing"

"github.com/fairwindsops/insights-plugins/plugins/ci/pkg/models"
"github.com/stretchr/testify/assert"
)

func TestAddScanErrorsReportResultFromAMultiError(t *testing.T) {
assert.Equal(t, "context from the first error/deployment/myapp - an error with additional context (deployment.yaml)",
createErrorItemMessage(models.ScanErrorsReportResult{
ErrorMessage: "an error with additional context",
ErrorContext: "context from the first error",
Kind: "deployment",
ResourceName: "myapp",
Severity: 2.0,
Category: "Security",
Remediation: "fix the deployment",
Filename: "deployment.yaml",
}))
}
2 changes: 1 addition & 1 deletion plugins/ci/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.0
5.3.1

0 comments on commit e26ec92

Please sign in to comment.