Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add --fixable flag to vulnerability cmd #148

Merged
merged 1 commit into from
Jun 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion cli/cmd/vulnerability.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ var (

// display extended details about a vulnerability scan/report
Details bool

// display only fixable vulnerabilities
Fixable bool
}{PollInterval: time.Second * 5}

// vulnerability represents the vulnerability command
Expand Down Expand Up @@ -297,6 +300,12 @@ func init() {
vulReportCmd.Flags(),
)

setFixableFlag(
vulScanRunCmd.Flags(),
vulScanShowCmd.Flags(),
vulReportCmd.Flags(),
)

vulReportCmd.Flags().BoolVar(
&vulCmdState.ImageID, "image_id", false,
"tread the provided sha256 hash as image id",
Expand All @@ -314,6 +323,16 @@ func setPollFlag(cmds ...*flag.FlagSet) {
}
}

func setFixableFlag(cmds ...*flag.FlagSet) {
for _, cmd := range cmds {
if cmd != nil {
cmd.BoolVar(&vulCmdState.Fixable, "fixable", false,
"display only fixable vulnerabilities",
)
}
}
}

func setDetailsFlag(cmds ...*flag.FlagSet) {
for _, cmd := range cmds {
if cmd != nil {
Expand Down Expand Up @@ -427,7 +446,7 @@ func buildVulnerabilityReport(report *api.VulContainerReport) string {
})
t.Render()

if vulCmdState.Details {
if vulCmdState.Details || vulCmdState.Fixable {
mainReport.WriteString(buildVulnerabilityReportDetails(report))
mainReport.WriteString("\n")
} else {
Expand Down Expand Up @@ -476,6 +495,9 @@ func vulContainerImageLayersToTable(image *api.VulContainerImage) [][]string {
for _, layer := range image.ImageLayers {
for _, pkg := range layer.Packages {
for _, vul := range pkg.Vulnerabilities {
if vulCmdState.Fixable && vul.FixVersion == "" {
continue
}
space := regexp.MustCompile(`\s+`)
createdBy := space.ReplaceAllString(layer.CreatedBy, " ")

Expand Down