Skip to content

Commit

Permalink
fix: show cloud notices for any report (#1255)
Browse files Browse the repository at this point in the history
* fix: move cloud info log out of security output

* refactor: formatStr no longer pointer

* refactor: move info and warning logs from run to output format
  • Loading branch information
elsapet authored Sep 12, 2023
1 parent caad6bf commit 2c6da6c
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 64 deletions.
2 changes: 1 addition & 1 deletion new/detector/composition/testhelper/testhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,5 @@ func (runner *Runner) scanSingleFile(t *testing.T, testDataPath string, fileRela
}

cupaloyCopy := cupaloy.NewDefaultConfig().WithOptions(cupaloy.SnapshotSubdirectory(snapshotsPath))
cupaloyCopy.SnapshotT(t, *report)
cupaloyCopy.SnapshotT(t, report)
}
13 changes: 2 additions & 11 deletions pkg/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ func (r *runner) Report(
formatStr, err := reportoutput.FormatOutput(
reportData,
r.scanSettings,
cacheUsed,
report.Inputgocloc,
startTime,
endTime,
Expand All @@ -358,9 +359,7 @@ func (r *runner) Report(
return false, fmt.Errorf("error generating report %s", err)
}

logger(*formatStr)

outputCachedDataWarning(cacheUsed, r.scanSettings.Scan.Quiet)
logger(formatStr)

return reportData.ReportFailed, nil
}
Expand All @@ -369,14 +368,6 @@ func (r *runner) ReportPath() string {
return r.reportPath
}

func outputCachedDataWarning(cacheUsed bool, quietMode bool) {
if quietMode || !cacheUsed {
return
}

outputhandler.StdErrLog("Cached data used (no code changes detected). Unexpected? Use --force to force a re-scan.\n")
}

func anySupportedLanguagesPresent(inputgocloc *gocloc.Result, config settings.Config) (bool, error) {
if inputgocloc == nil {
return true, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/output/dataflow/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewFormatter(reportData *outputtypes.ReportData, config settings.Config) *F
}
}

func (f Formatter) Format(format string) (output *string, err error) {
func (f Formatter) Format(format string) (output string, err error) {
switch format {
case flag.FormatEmpty, flag.FormatJSON:
return outputhandler.ReportJSON(f.ReportData.Dataflow)
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/output/detectors/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewFormatter(reportData *outputtypes.ReportData, config settings.Config) *F
}
}

func (f Formatter) Format(format string) (output *string, err error) {
func (f Formatter) Format(format string) (output string, err error) {
switch format {
case flag.FormatEmpty, flag.FormatJSON:
return outputhandler.ReportJSON(f.ReportData.Detectors)
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/output/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestJuiceShopSarif(t *testing.T) {
}

var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, []byte(*output), "", "\t")
err = json.Indent(&prettyJSON, []byte(output), "", "\t")
if err != nil {
t.Fatalf("error indenting output, err: %s", err)
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/report/output/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var wrapperTemplate string
//go:embed styles.css
var siteCss string

func ReportHTMLWrapper(title string, body *string) (*string, error) {
func ReportHTMLWrapper(title string, body *string) (string, error) {
htmlContent := &strings.Builder{}

t := time.Now()
Expand All @@ -41,17 +41,16 @@ func ReportHTMLWrapper(title string, body *string) (*string, error) {
}
pageTemplate, err := template.New("pageTemplate").Parse(wrapperTemplate)
if err != nil {
return nil, err
return "", err
}

err = pageTemplate.Execute(htmlContent, wrapperContent)

if err != nil {
return nil, err
return "", err
}

content := htmlContent.String()
return &content, nil
return htmlContent.String(), nil
}

func ReportSecurityHTML(detections map[string][]securitytypes.Finding) (*string, error) {
Expand Down
36 changes: 27 additions & 9 deletions pkg/report/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ func GetData(
config settings.Config,
baseBranchFindings *basebranchfindings.Findings,
) (*types.ReportData, error) {
sendToCloud := false

data := &types.ReportData{}
// add detectors
err := detectors.AddReportData(data, report, config)
Expand All @@ -48,14 +46,14 @@ func GetData(
case flag.ReportDataFlow:
return data, err
case flag.ReportSecurity:
sendToCloud = true
data.SendToCloud = true
err = security.AddReportData(data, config, baseBranchFindings)
case flag.ReportSaaS:
if err = security.AddReportData(data, config, baseBranchFindings); err != nil {
return nil, err
}

sendToCloud = true
data.SendToCloud = true
err = saas.GetReport(data, config, false)
case flag.ReportPrivacy:
err = privacy.AddReportData(data, config)
Expand All @@ -65,7 +63,7 @@ func GetData(
return nil, fmt.Errorf(`--report flag "%s" is not supported`, config.Report.Report)
}

if sendToCloud && config.Client != nil && config.Client.Error == nil {
if data.SendToCloud && config.Client != nil && config.Client.Error == nil {
// send SaaS report to Cloud
saas.SendReport(config, data)
}
Expand All @@ -88,10 +86,11 @@ func GetDataflow(reportData *types.ReportData, report globaltypes.Report, config
func FormatOutput(
reportData *types.ReportData,
config settings.Config,
cacheUsed bool,
goclocResult *gocloc.Result,
startTime time.Time,
endTime time.Time,
) (*string, error) {
) (string, error) {
var formatter types.GenericFormatter
switch config.Report.Report {
case flag.ReportDetectors:
Expand All @@ -107,15 +106,34 @@ func FormatOutput(
case flag.ReportStats:
formatter = stats.NewFormatter(reportData, config)
default:
return nil, fmt.Errorf(`--report flag "%s" is not supported`, config.Report.Report)
return "", fmt.Errorf(`--report flag "%s" is not supported`, config.Report.Report)
}

formatStr, err := formatter.Format(config.Report.Format)
if err != nil {
return formatStr, err
}
if formatStr == nil {
return nil, fmt.Errorf(`--report flag "%s" does not support --format flag "%s"`, config.Report.Report, config.Report.Format)
if formatStr == "" {
return "", fmt.Errorf(`--report flag "%s" does not support --format flag "%s"`, config.Report.Report, config.Report.Format)
}

if !config.Scan.Quiet && (reportData.SendToCloud || cacheUsed) {
// add cached data warning message
if cacheUsed {
formatStr += "\n\nCached data used (no code changes detected). Unexpected? Use --force to force a re-scan."
}

// add cloud info message
if reportData.SendToCloud {
if config.Client.Error == nil {
formatStr += "\n\nData successfully sent to Bearer Cloud."
} else {
// client error
formatStr += fmt.Sprintf("\n\nFailed to send data to Bearer Cloud. %s ", *config.Client.Error)
}
}

formatStr += "\n"
}

return formatStr, err
Expand Down
5 changes: 2 additions & 3 deletions pkg/report/output/privacy/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ func NewFormatter(reportData *outputtypes.ReportData, config settings.Config) *F
}
}

func (f Formatter) Format(format string) (output *string, err error) {
func (f Formatter) Format(format string) (output string, err error) {
switch format {
case flag.FormatEmpty, flag.FormatCSV:
stringBuilder, err := BuildCsvString(f.ReportData, f.Config)
if err != nil {
return output, err
}
csvStr := stringBuilder.String()
output = &csvStr
output = stringBuilder.String()
case flag.FormatJSON:
return outputhandler.ReportJSON(f.ReportData.PrivacyReport)
case flag.FormatYAML:
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/output/reviewdog/reviewdog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestRailsGoatReviewdog(t *testing.T) {
}

var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, []byte(*sarifOutput), "", "\t")
err = json.Indent(&prettyJSON, []byte(sarifOutput), "", "\t")
if err != nil {
t.Fatalf("error indenting output, err: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/output/saas/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewFormatter(reportData *outputtypes.ReportData, config settings.Config) *F
}
}

func (f Formatter) Format(format string) (output *string, err error) {
func (f Formatter) Format(format string) (output string, err error) {
switch format {
case flag.FormatEmpty, flag.FormatJSON:
return outputhandler.ReportJSON(f.ReportData.SaasReport)
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/output/saas/saas.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func createBearerGzipFileReport(

content, _ := util.ReportJSON(reportData.SaasReport)
gzWriter := gzip.NewWriter(file)
_, err = gzWriter.Write([]byte(*content))
_, err = gzWriter.Write([]byte(content))
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/output/sarif/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestJuiceShopSarif(t *testing.T) {
}

var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, []byte(*sarifOutput), "", "\t")
err = json.Indent(&prettyJSON, []byte(sarifOutput), "", "\t")
if err != nil {
t.Fatalf("error indenting output, err: %s", err)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/report/output/security/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ func NewFormatter(reportData *outputtypes.ReportData, config settings.Config, go
}
}

func (f Formatter) Format(format string) (output *string, err error) {
func (f Formatter) Format(format string) (output string, err error) {
switch format {
case flag.FormatEmpty:
reportStr := BuildReportString(f.ReportData, f.Config, f.GoclocResult).String()
output = &reportStr
output = BuildReportString(f.ReportData, f.Config, f.GoclocResult).String()
case flag.FormatSarif:
sarifContent, sarifErr := sarif.ReportSarif(f.ReportData.FindingsBySeverity, f.Config.Rules)
if sarifErr != nil {
Expand Down
15 changes: 0 additions & 15 deletions pkg/report/output/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,6 @@ func BuildReportString(reportData *outputtypes.ReportData, config settings.Confi
writeStatsToString(reportData, reportStr, config, lineOfCodeOutput)
}

writeApiClientResultToString(reportStr, config)

reportStr.WriteString("\nNeed help or want to discuss the output? Join the Community https://discord.gg/eaHZBJUXRF\n")

if config.Client == nil {
Expand Down Expand Up @@ -530,19 +528,6 @@ func writeRuleListToString(
return totalRuleCount
}

func writeApiClientResultToString(
reportStr *strings.Builder,
config settings.Config,
) {
if config.Client != nil {
if config.Client.Error == nil {
reportStr.WriteString("\nData successfully sent to Bearer Cloud.\n")
} else {
reportStr.WriteString(fmt.Sprintf("\nFailed to send data to Bearer Cloud. %s \n", *config.Client.Error))
}
}
}

func countRules(
rules map[string]*settings.Rule,
languages map[string]*gocloc.Language,
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/output/stats/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewFormatter(reportData *outputtypes.ReportData, config settings.Config) *F
}
}

func (f Formatter) Format(format string) (output *string, err error) {
func (f Formatter) Format(format string) (output string, err error) {
switch format {
case flag.FormatEmpty, flag.FormatJSON:
return outputhandler.ReportJSON(f.ReportData.Stats)
Expand Down
3 changes: 2 additions & 1 deletion pkg/report/output/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type ReportData struct {
ReportFailed bool
SendToCloud bool
Files []string
Detectors []any
Dataflow *DataFlow
Expand All @@ -28,5 +29,5 @@ type DataFlow struct {
}

type GenericFormatter interface {
Format(format string) (*string, error) // TODO: ensure format is an expected format (from report flags)
Format(format string) (string, error) // TODO: ensure format is an expected format (from report flags)
}
14 changes: 6 additions & 8 deletions pkg/util/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,20 @@ func Fatal(message string) {
os.Exit(1)
}

func ReportJSON(outputDetections any) (*string, error) {
func ReportJSON(outputDetections any) (string, error) {
jsonBytes, err := json.Marshal(&outputDetections)
if err != nil {
return nil, fmt.Errorf("failed to json marshal detections: %s", err)
return "", fmt.Errorf("failed to json marshal detections: %s", err)
}

content := string(jsonBytes)
return &content, nil
return string(jsonBytes), nil
}

func ReportYAML(outputDetections any) (*string, error) {
func ReportYAML(outputDetections any) (string, error) {
yamlBytes, err := yaml.Marshal(&outputDetections)
if err != nil {
return nil, fmt.Errorf("failed to yaml marshal detections: %s", err)
return "", fmt.Errorf("failed to yaml marshal detections: %s", err)
}

content := string(yamlBytes)
return &content, nil
return string(yamlBytes), nil
}

0 comments on commit 2c6da6c

Please sign in to comment.