|
91 | 91 | // go build tags
|
92 | 92 | flagBuildTags = flag.String("tags", "", "Comma separated list of build tags")
|
93 | 93 |
|
| 94 | + // scan the vendor folder |
94 | 95 | flagScanVendor = flag.Bool("vendor", false, "Scan the vendor folder")
|
95 | 96 |
|
| 97 | + // fail by severity |
| 98 | + flagSeverity = flag.String("severity", "low", "Fail the scanning for issues with the given or higher severity. Valid options are: low, medium, high") |
| 99 | + |
96 | 100 | logger *log.Logger
|
97 | 101 | )
|
98 | 102 |
|
@@ -224,6 +228,20 @@ func resolvePackage(pkg string, searchPaths []string) string {
|
224 | 228 | return pkg
|
225 | 229 | }
|
226 | 230 |
|
| 231 | +func convertToScore(severity string) (gosec.Score, error) { |
| 232 | + severity = strings.ToLower(severity) |
| 233 | + switch severity { |
| 234 | + case "low": |
| 235 | + return gosec.Low, nil |
| 236 | + case "medium": |
| 237 | + return gosec.Medium, nil |
| 238 | + case "high": |
| 239 | + return gosec.High, nil |
| 240 | + default: |
| 241 | + return gosec.Low, fmt.Errorf("provided severity '%s' not valid. Valid options: low, medium, high", severity) |
| 242 | + } |
| 243 | +} |
| 244 | + |
227 | 245 | func main() {
|
228 | 246 |
|
229 | 247 | // Setup usage description
|
@@ -256,6 +274,11 @@ func main() {
|
256 | 274 | logger = log.New(logWriter, "[gosec] ", log.LstdFlags)
|
257 | 275 | }
|
258 | 276 |
|
| 277 | + failSeverity, err := convertToScore(*flagSeverity) |
| 278 | + if err != nil { |
| 279 | + logger.Fatal(err) |
| 280 | + } |
| 281 | + |
259 | 282 | // Load config
|
260 | 283 | config, err := loadConfig(*flagConfig)
|
261 | 284 | if err != nil {
|
@@ -299,17 +322,24 @@ func main() {
|
299 | 322 | // Collect the results
|
300 | 323 | issues, metrics := analyzer.Report()
|
301 | 324 |
|
302 |
| - issuesFound := len(issues) > 0 |
303 |
| - // Exit quietly if nothing was found |
304 |
| - if !issuesFound && *flagQuiet { |
305 |
| - os.Exit(0) |
306 |
| - } |
307 |
| - |
308 | 325 | // Sort the issue by severity
|
309 | 326 | if *flagSortIssues {
|
310 | 327 | sortIssues(issues)
|
311 | 328 | }
|
312 | 329 |
|
| 330 | + issuesFound := false |
| 331 | + for _, issue := range issues { |
| 332 | + if issue.Severity >= failSeverity { |
| 333 | + issuesFound = true |
| 334 | + break |
| 335 | + } |
| 336 | + } |
| 337 | + |
| 338 | + // Exit quietly if nothing was found |
| 339 | + if !issuesFound && *flagQuiet { |
| 340 | + os.Exit(0) |
| 341 | + } |
| 342 | + |
313 | 343 | // Create output report
|
314 | 344 | if err := saveOutput(*flagOutput, *flagFormat, issues, metrics); err != nil {
|
315 | 345 | logger.Fatal(err)
|
|
0 commit comments