Skip to content

Commit

Permalink
Merge pull request #1667 from cpanato/vulndash-logs
Browse files Browse the repository at this point in the history
vulndash: Add page size when listing vulnerabilities more logs and build v0.3.0 image
  • Loading branch information
k8s-ci-robot authored Nov 1, 2020
2 parents 7a28c00 + 6b2cc47 commit f12c7b4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/vulndash/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SHELL=/bin/bash -o pipefail

REGISTRY ?= gcr.io/k8s-staging-artifact-promoter
IMGNAME = vulndash
IMAGE_VERSION ?= v0.2.1
IMAGE_VERSION ?= v0.3.0
CONFIG ?= buster

IMAGE = $(REGISTRY)/$(IMGNAME)
Expand Down
10 changes: 10 additions & 0 deletions cmd/vulndash/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type options struct {
project string
bucket string
dashboardFilePath string
pageSize int32
logLevel string
}

Expand All @@ -57,6 +58,7 @@ var (
projectFlag = "project"
bucketFlag = "bucket"
dashboardFilePathFlag = "dashboard-file-path"
pageSizeFlag = "page-size"

// requiredFlags only if the config flag is not set
requiredFlags = []string{
Expand Down Expand Up @@ -102,6 +104,13 @@ func init() {
"info",
"the logging verbosity, either 'panic', 'fatal', 'error', 'warn', 'warning', 'info', 'debug' or 'trace'",
)

rootCmd.PersistentFlags().Int32Var(
&opts.pageSize,
pageSizeFlag,
200,
"the page size when getting the list of vulnerabilities",
)
}

func initLogging(*cobra.Command, []string) error {
Expand Down Expand Up @@ -132,6 +141,7 @@ func run(opts *options) error {
opts.dashboardFilePath,
opts.project,
opts.bucket,
opts.pageSize,
)
if updateErr != nil {
return errors.Wrap(updateErr, "updating vulnerability dashboard")
Expand Down
2 changes: 1 addition & 1 deletion cmd/vulndash/variants.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variants:
default:
IMAGE_VERSION: 'v0.2.1'
IMAGE_VERSION: 'v0.3.0'
GO_VERSION: '1.15.3'
DISTROLESS_IMAGE: 'static-debian10'
2 changes: 1 addition & 1 deletion dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ dependencies:

# Images: k8s.io/artifact-promoter
- name: "k8s.io/artifact-promoter/vulndash"
version: v0.2.1
version: v0.3.0
refPaths:
- path: cmd/vulndash/Makefile
match: IMAGE_VERSION\ \?=\ v((([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)
Expand Down
32 changes: 25 additions & 7 deletions pkg/vulndash/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
containeranalysis "cloud.google.com/go/containeranalysis/apiv1"
"cloud.google.com/go/storage"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/net/html"
"google.golang.org/api/iterator"
grafeaspb "google.golang.org/genproto/googleapis/grafeas/v1"
Expand Down Expand Up @@ -71,6 +72,7 @@ func uploadFile(directory, filename, bucket string) error {
// with images in a specific project using the Container Analysis Service.
func GetAllVulnerabilities(
projectID string,
pageSize int32,
) ([]*grafeaspb.Occurrence, error) {
ctx := context.Background()
client, err := containeranalysis.NewClient(ctx)
Expand All @@ -80,14 +82,18 @@ func GetAllVulnerabilities(
defer client.Close()

req := &grafeaspb.ListOccurrencesRequest{
Parent: fmt.Sprintf("projects/%s", projectID),
Filter: fmt.Sprintf("kind = %q", "VULNERABILITY"),
Parent: fmt.Sprintf("projects/%s", projectID),
Filter: fmt.Sprintf("kind = %q", "VULNERABILITY"),
PageSize: pageSize,
}

logrus.Info("listing the vulnerabilities, will take a while...")
var occurrenceList []*grafeaspb.Occurrence
it := client.GetGrafeasClient().ListOccurrences(ctx, req)
for {
occ, err := it.Next()
var occ *grafeaspb.Occurrence
var err error
occ, err = it.Next()
if err == iterator.Done {
break
}
Expand All @@ -96,6 +102,7 @@ func GetAllVulnerabilities(
}
occurrenceList = append(occurrenceList, occ)
}
logrus.Infof("done listing the vulnerabilities")

return occurrenceList, err
}
Expand Down Expand Up @@ -164,46 +171,57 @@ func UpdateVulnerabilityDashboard(
dashboardPath string,
vulnProject string,
dashboardBucket string,
pageSize int32,
) error {
htmlReader, openErr := os.Open(dashboardPath + "dashboard.html")
dashboardHTML := dashboardPath + "dashboard.html"
logrus.Infof("opening %s", dashboardHTML)
htmlReader, openErr := os.Open(dashboardHTML)
if openErr != nil {
return errors.Wrap(openErr, "opening dashboard file")
}

logrus.Infof("parsing %s", dashboardHTML)
_, err := html.Parse(htmlReader)
if err != nil {
return errors.Errorf("dashboard.html is not valid HTML: %v", err)
}

logrus.Infof("uploading %s to gcs", dashboardHTML)
err = uploadFile(dashboardPath, "dashboard.html", dashboardBucket)
if err != nil {
return errors.Errorf("Unable to upload latest version of "+
"dashboard HTML: %v", err)
}

logrus.Info("uploading updated dashboard.js to gcs")
err = uploadFile(dashboardPath, "dashboard.js", dashboardBucket)
if err != nil {
return errors.Errorf("Unable to upload latest version of "+
"dashboard JS: %v", err)
}

productionVulnerabilities, getVulnErr := GetAllVulnerabilities(vulnProject)
logrus.Infof("checking all vulnerabilities for %s", vulnProject)
productionVulnerabilities, getVulnErr := GetAllVulnerabilities(vulnProject, pageSize)
if getVulnErr != nil {
return errors.Wrap(getVulnErr, "getting all vulnerabilities")
}

logrus.Infof("parsing the vulnerabilities for %s", vulnProject)
vulnBreakdowns := GenerateVulnerabilityBreakdown(productionVulnerabilities)
jsonFile, err := json.MarshalIndent(vulnBreakdowns, "", " ")
if err != nil {
return errors.Errorf("Unable to generate dashboard json: %v", err)
}

err = ioutil.WriteFile(dashboardPath+"dashboard.json",
jsonFile, 0644)
dashboardJSON := dashboardPath + "dashboard.json"
logrus.Infof("writing the vulnerabilities for %s in the file %s", vulnProject, dashboardJSON)
err = ioutil.WriteFile(dashboardJSON, jsonFile, 0644)
if err != nil {
return errors.Errorf("Unable to create temporary local"+
"JSON file for the dashboard: %v", err)
}

logrus.Infof("uploading updated %s to gcs", dashboardJSON)
err = uploadFile(dashboardPath, "dashboard.json", dashboardBucket)
if err != nil {
return errors.Errorf("Unable to upload latest version of "+
Expand Down

0 comments on commit f12c7b4

Please sign in to comment.