Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Commit

Permalink
feat: Display vulnerabilities summary
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>
  • Loading branch information
danielpacak committed Jan 28, 2020
1 parent 6d36f5c commit d683adf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
36 changes: 29 additions & 7 deletions cmd/octant-risky-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ func handlePrint(request *service.PrintRequest) (plugin.PrintResponse, error) {
View: view.NewDebug("THIS IS A TEST"),
})

summary, err := repository.GetVulnerabilitiesSummary(request.Context(), data.Workload{
Kind: "Pod",
Name: unstructuredPod.GetName(),
})
if err != nil {
return plugin.PrintResponse{}, err
}

vs := component.NewSummary("Vulnerabilities",
summarySectionsFor(summary)...,
)

printItems = append(printItems, component.FlexLayoutItem{
Width: component.WidthHalf,
View: vs,
})

// When printing an object, you can create multiple types of content. In this
// example, the plugin is:
//
Expand All @@ -132,12 +149,17 @@ func handlePrint(request *service.PrintRequest) (plugin.PrintResponse, error) {
Config: []component.SummarySection{
{Header: "Last Scanned At", Content: component.NewText(fmt.Sprintf("%s", time.Now().Format(time.RFC3339)))},
},
Status: []component.SummarySection{
{Header: "Critical Severity Vulnerabilities", Content: component.NewText(strconv.Itoa(15))},
{Header: "High Severity Vulnerabilities", Content: component.NewText(strconv.Itoa(3))},
{Header: "Medium Severity Vulnerabilities", Content: component.NewText(strconv.Itoa(7))},
{Header: "Low Severity Vulnerabilities", Content: component.NewText(strconv.Itoa(1))},
},
Items: printItems,
Status: summarySectionsFor(summary),
Items: printItems,
}, nil
}

func summarySectionsFor(summary data.VulnerabilitiesSummary) []component.SummarySection {
return []component.SummarySection{
{Header: "Critical Vulnerabilities", Content: component.NewText(strconv.Itoa(summary.CriticalCount))},
{Header: "High Vulnerabilities", Content: component.NewText(strconv.Itoa(summary.HighCount))},
{Header: "Medium Vulnerabilities", Content: component.NewText(strconv.Itoa(summary.MediumCount))},
{Header: "Low Vulnerabilities", Content: component.NewText(strconv.Itoa(summary.LowCount))},
{Header: "Unknown Vulnerabilities", Content: component.NewText(strconv.Itoa(summary.UnknownCount))},
}
}
32 changes: 32 additions & 0 deletions pkg/data/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ type ContainerImageScanReport struct {
Report security.ImageScanReport
}

type VulnerabilitiesSummary struct {
CriticalCount int
HighCount int
MediumCount int
LowCount int
UnknownCount int
}

func (r *Repository) GetVulnerabilitiesSummary(ctx context.Context, options Workload) (vs VulnerabilitiesSummary, err error) {
containerReports, err := r.GetImageScanReports(ctx, options)
if err != nil {
return vs, err
}
for _, cr := range containerReports {
for _, v := range cr.Report.Spec.Vulnerabilities {
switch v.Severity {
case "CRITICAL":
vs.CriticalCount++
case "HIGH":
vs.HighCount++
case "MEDIUM":
vs.MediumCount++
case "LOW":
vs.LowCount++
default:
vs.UnknownCount++
}
}
}
return
}

func (r *Repository) GetImageScanReports(ctx context.Context, options Workload) ([]ContainerImageScanReport, error) {
unstructuredList, err := r.client.List(ctx, store.Key{
APIVersion: "security.danielpacak.github.com/v1",
Expand Down
1 change: 1 addition & 0 deletions pkg/view/vulnerabilities_summary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package view

0 comments on commit d683adf

Please sign in to comment.