■ ■ ■ ■ ■ ■
pkg/commands/artifact/run.go
skipped 12 lines 13 13 "strings" 14 14 15 15 "github.com/google/uuid" 16 + "github.com/hhatto/gocloc" 16 17 "github.com/rs/zerolog/log" 17 18 18 19 "golang.org/x/xerrors" skipped 4 lines 23 24 "github.com/bearer/bearer/pkg/commands/process/worker/work" 24 25 "github.com/bearer/bearer/pkg/flag" 25 26 reportoutput "github.com/bearer/bearer/pkg/report/output" 27 + "github.com/bearer/bearer/pkg/report/output/security" 28 + "github.com/bearer/bearer/pkg/report/output/stats" 26 29 "github.com/bearer/bearer/pkg/util/output" 27 30 outputhandler "github.com/bearer/bearer/pkg/util/output" 28 31 skipped 258 lines 287 290 outputhandler.StdErrLogger().Msg("Using cached data") 288 291 } 289 292 293 + detections, lineOfCodeOutput, dataflow, err := reportoutput.GetOutput(report, config) 294 + if err != nil { 295 + return false, err 296 + } 297 + 298 + reportSupported, err := anySupportedLanguagesPresent(lineOfCodeOutput, config) 299 + if err != nil { 300 + return false, err 301 + } 302 + 303 + if !reportSupported && config.Report.Report != flag.ReportPrivacy { 304 + var placeholderStr *strings.Builder 305 + placeholderStr, err = getPlaceholderOutput(report, config, lineOfCodeOutput) 306 + if err != nil { 307 + return false, err 308 + } 309 + 310 + output.StdOutLogger().Msg(placeholderStr.String()) 311 + return true, nil 312 + } 313 + 290 314 if config.Report.Format == flag.FormatEmpty { 291 315 if config.Report.Report == flag.ReportSecurity { 292 - // for policy report, default report format is NOT JSON 293 - reportPassed, err := reportoutput.ReportSecurity(report, logger, config) 294 - if err != nil { 295 - return false, fmt.Errorf("error generating report %w", err) 296 - } 297 - outputCachedDataWarning(cacheUsed, config.Scan.Quiet) 316 + // for security report, default report format is Table 317 + report := detections.(*security.Results) 318 + reportStr, reportPassed := security.BuildReportString(config, report, lineOfCodeOutput, dataflow) 319 + 320 + output.StdOutLogger().Msg(reportStr.String()) 321 + 298 322 return reportPassed, nil 299 - } 300 - if config.Report.Report == flag.ReportPrivacy { 323 + } else if config.Report.Report == flag.ReportPrivacy { 301 324 // for privacy report, default report format is CSV 302 - err := reportoutput.ReportCSV(report, logger, config) 325 + content, err := reportoutput.GetPrivacyReportCSVOutput(report, lineOfCodeOutput, dataflow, config) 303 326 if err != nil { 304 - return false, fmt.Errorf("error generating report %w ", err) 327 + return false, fmt.Errorf("error generating report %s ", err) 305 328 } 306 - outputCachedDataWarning(cacheUsed, config.Scan.Quiet) 329 + 330 + output.StdOutLogger().Msg(*content) 331 + 307 332 return true, nil 308 333 } 309 334 } skipped 1 lines 311 336 switch config.Report.Format { 312 337 case flag.FormatEmpty, flag.FormatJSON: 313 338 // default report format for is JSON 314 - err := reportoutput.ReportJSON(report , logger , config) 339 + content , err := reportoutput.ReportJSON(detections , config) 315 340 if err != nil { 316 - return false, fmt.Errorf("error generating report %w ", err) 341 + return false, fmt.Errorf("error generating report %s ", err) 317 342 } 343 + 344 + logger.Msg(*content) 318 345 case flag.FormatYAML: 319 - err := reportoutput.ReportYAML(report , logger , config) 346 + content , err := reportoutput.ReportYAML(detections , config) 320 347 if err != nil { 321 - return false, fmt.Errorf("error generating report %w ", err) 348 + return false, fmt.Errorf("error generating report %s ", err) 322 349 } 350 + 351 + logger.Msg(*content) 323 352 } 353 + 324 354 outputCachedDataWarning(cacheUsed, config.Scan.Quiet) 325 355 return true, nil 326 356 } skipped 6 lines 333 363 outputhandler.StdErrLogger().Msg("Cached data used (no code changes detected). Unexpected? Use --force to force a re-scan.\n") 334 364 } 335 365 366 + func anySupportedLanguagesPresent(inputgocloc *gocloc.Result, config settings.Config) (bool, error) { 367 + if inputgocloc == nil { 368 + return true, nil 369 + } 370 + 371 + ruleLanguages := make(map[string]bool) 372 + for _, rule := range config.Rules { 373 + for _, language := range rule.Languages { 374 + ruleLanguages[language] = true 375 + } 376 + } 377 + 378 + foundLanguages := make(map[string]bool) 379 + for _, language := range inputgocloc.Languages { 380 + foundLanguages[strings.ToLower(language.Name)] = true 381 + } 382 + 383 + _, rubyPresent := foundLanguages["ruby"] 384 + if rubyPresent { 385 + return true, nil 386 + } 387 + 388 + _, javascriptPresent := foundLanguages["javascript"] 389 + if javascriptPresent { 390 + return true, nil 391 + } 392 + 393 + log.Debug().Msg("No language found for which rules are applicable") 394 + return false, nil 395 + } 396 + 397 + func getPlaceholderOutput(report types.Report, config settings.Config, inputgocloc *gocloc.Result) (outputStr *strings.Builder, err error) { 398 + dataflowOutput, _, _, err := reportoutput.GetDataflow(report, config, true) 399 + if err != nil { 400 + return 401 + } 402 + 403 + return stats.GetPlaceholderOutput(inputgocloc, dataflowOutput, config) 404 + } 405 +