Skip to content

Commit 639987a

Browse files
author
Cosmin Cojocar
authored
Merge pull request #223 from ccojocar/fail_by_severity
Add a flag to specify the severity for which the scanning will be failed
2 parents c0db486 + de10a74 commit 639987a

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

cmd/gosec/main.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,12 @@ var (
9191
// go build tags
9292
flagBuildTags = flag.String("tags", "", "Comma separated list of build tags")
9393

94+
// scan the vendor folder
9495
flagScanVendor = flag.Bool("vendor", false, "Scan the vendor folder")
9596

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+
96100
logger *log.Logger
97101
)
98102

@@ -224,6 +228,20 @@ func resolvePackage(pkg string, searchPaths []string) string {
224228
return pkg
225229
}
226230

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+
227245
func main() {
228246

229247
// Setup usage description
@@ -256,6 +274,11 @@ func main() {
256274
logger = log.New(logWriter, "[gosec] ", log.LstdFlags)
257275
}
258276

277+
failSeverity, err := convertToScore(*flagSeverity)
278+
if err != nil {
279+
logger.Fatal(err)
280+
}
281+
259282
// Load config
260283
config, err := loadConfig(*flagConfig)
261284
if err != nil {
@@ -299,17 +322,24 @@ func main() {
299322
// Collect the results
300323
issues, metrics := analyzer.Report()
301324

302-
issuesFound := len(issues) > 0
303-
// Exit quietly if nothing was found
304-
if !issuesFound && *flagQuiet {
305-
os.Exit(0)
306-
}
307-
308325
// Sort the issue by severity
309326
if *flagSortIssues {
310327
sortIssues(issues)
311328
}
312329

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+
313343
// Create output report
314344
if err := saveOutput(*flagOutput, *flagFormat, issues, metrics); err != nil {
315345
logger.Fatal(err)

0 commit comments

Comments
 (0)