Skip to content

Commit

Permalink
Merge pull request #236 from blackducksoftware/risk-profile-status-fix
Browse files Browse the repository at this point in the history
changed from enums to constants
  • Loading branch information
Rob Rati authored Jul 16, 2019
2 parents 910d18d + 03e87c2 commit cc63e16
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 281 deletions.
4 changes: 2 additions & 2 deletions pkg/core/model/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func createNewModel1() *Model {
model.Images[sha1].SetScanResults(&hub.ScanResults{
PolicyStatus: hub.PolicyStatus{
OverallStatus: hub.PolicyStatusTypeInViolation,
ComponentVersionStatusCounts: map[hub.PolicyStatusType]int{hub.PolicyStatusTypeInViolation: 3}}})
ComponentVersionStatusCounts: map[string]int{hub.PolicyStatusTypeInViolation: 3}}})
return model
}

Expand All @@ -87,7 +87,7 @@ func createNewModel2() *Model {
model.Images[sha1].SetScanResults(&hub.ScanResults{
PolicyStatus: hub.PolicyStatus{
OverallStatus: hub.PolicyStatusTypeInViolation,
ComponentVersionStatusCounts: map[hub.PolicyStatusType]int{hub.PolicyStatusTypeInViolation: 3}}})
ComponentVersionStatusCounts: map[string]int{hub.PolicyStatusTypeInViolation: 3}}})
model.Images[sha3].ScanStatus = ScanStatusComplete
model.Images[sha3].SetScanResults(&hub.ScanResults{
PolicyStatus: hub.PolicyStatus{
Expand Down
8 changes: 4 additions & 4 deletions pkg/core/model/modelextensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func scanResults(model *Model) (api.ScanResults, error) {
Name: pod.Name,
PolicyViolations: podScan.PolicyViolations,
Vulnerabilities: podScan.Vulnerabilities,
OverallStatus: podScan.OverallStatus.String()})
OverallStatus: podScan.OverallStatus})
}

// images
Expand All @@ -119,7 +119,7 @@ func scanResults(model *Model) (api.ScanResults, error) {
Sha: string(image.Sha),
PolicyViolations: imageInfo.ScanResults.PolicyViolationCount(),
Vulnerabilities: imageInfo.ScanResults.VulnerabilityCount(),
OverallStatus: imageInfo.ScanResults.OverallStatus().String(),
OverallStatus: imageInfo.ScanResults.OverallStatus(),
ComponentsURL: imageInfo.ScanResults.ComponentsHref}
images = append(images, apiImage)
}
Expand Down Expand Up @@ -230,7 +230,7 @@ func metrics(model *Model) *Metrics {
continue
}
if podScan != nil {
podStatus[podScan.OverallStatus.String()]++
podStatus[podScan.OverallStatus]++
podPolicyViolations[podScan.PolicyViolations]++
podVulnerabilities[podScan.Vulnerabilities]++
} else {
Expand All @@ -250,7 +250,7 @@ func metrics(model *Model) *Metrics {
log.Errorf("found nil scan results for completed image %s", sha)
continue
}
imageStatus[imageScan.OverallStatus().String()]++
imageStatus[imageScan.OverallStatus()]++
imagePolicyViolations[imageScan.PolicyViolationCount()]++
imageVulnerabilities[imageScan.VulnerabilityCount()]++
} else {
Expand Down
6 changes: 2 additions & 4 deletions pkg/core/model/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ under the License.

package model

import "github.com/blackducksoftware/perceptor/pkg/hub"

// Scan .....
// Scan denotes the status of the scan with vulnerability and policy violation of the status
type Scan struct {
OverallStatus hub.PolicyStatusType
OverallStatus string
PolicyViolations int
Vulnerabilities int
}
64 changes: 64 additions & 0 deletions pkg/hub/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright (C) 2019 Synopsys, Inc.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package hub

// PolicyStatusTypeNotInViolation denotes policy status type NOT_IN_VIOLATION
const PolicyStatusTypeNotInViolation = "NOT_IN_VIOLATION"

// PolicyStatusTypeInViolation denotes policy status type IN_VIOLATION
const PolicyStatusTypeInViolation = "IN_VIOLATION"

// PolicyStatusTypeInViolationOverridden denotes policy status type IN_VIOLATION_OVERRIDDEN
const PolicyStatusTypeInViolationOverridden = "IN_VIOLATION_OVERRIDDEN"

// RiskProfileCategoryActivity denotes the type ACTIVITY in Risk Profile category
const RiskProfileCategoryActivity = "ACTIVITY"

// RiskProfileCategoryLicense denotes the type LICENSE in Risk Profile category
const RiskProfileCategoryLicense = "LICENSE"

// RiskProfileCategoryOperational denotes the type OPERATIONAL in Risk Profile category
const RiskProfileCategoryOperational = "OPERATIONAL"

// RiskProfileCategoryVersion denotes the type VERSION in Risk Profile category
const RiskProfileCategoryVersion = "VERSION"

// RiskProfileCategoryVulnerability denotes the type VULNERABILITY in Risk Profile category
const RiskProfileCategoryVulnerability = "VULNERABILITY"

// RiskProfileStatusCritical denotes risk profile status CRITICAL
const RiskProfileStatusCritical = "CRITICAL"

// RiskProfileStatusHigh denotes risk profile status HIGH
const RiskProfileStatusHigh = "HIGH"

// RiskProfileStatusMedium denotes risk profile status MEDIUM
const RiskProfileStatusMedium = "MEDIUM"

// RiskProfileStatusLow denotes risk profile status LOW
const RiskProfileStatusLow = "LOW"

// RiskProfileStatusOK denotes risk profile status OK
const RiskProfileStatusOK = "OK"

// RiskProfileStatusUnknown denotes risk profile status UNKNOWN
const RiskProfileStatusUnknown = "UNKNOWN"
206 changes: 15 additions & 191 deletions pkg/hub/datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ under the License.
package hub

import (
"encoding/json"
"fmt"

"github.com/blackducksoftware/hub-client-go/hubapi"
Expand Down Expand Up @@ -109,9 +108,9 @@ func (status ClientStatus) MarshalText() (text []byte, err error) {

// PolicyStatus .....
type PolicyStatus struct {
OverallStatus PolicyStatusType
OverallStatus string
UpdatedAt string
ComponentVersionStatusCounts map[PolicyStatusType]int
ComponentVersionStatusCounts map[string]int
}

// ViolationCount .....
Expand All @@ -123,51 +122,6 @@ func (ps *PolicyStatus) ViolationCount() int {
return violationCount
}

// PolicyStatusType .....
type PolicyStatusType int

// .....
const (
PolicyStatusTypeNotInViolation PolicyStatusType = iota
PolicyStatusTypeInViolation PolicyStatusType = iota
PolicyStatusTypeInViolationOverridden PolicyStatusType = iota
)

// String .....
func (p PolicyStatusType) String() string {
switch p {
case PolicyStatusTypeNotInViolation:
return "NOT_IN_VIOLATION"
case PolicyStatusTypeInViolation:
return "IN_VIOLATION"
case PolicyStatusTypeInViolationOverridden:
return "IN_VIOLATION_OVERRIDDEN"
default:
panic(fmt.Errorf("invalid PolicyStatusType value: %d", p))
}
}

// MarshalJSON .....
func (p PolicyStatusType) MarshalJSON() ([]byte, error) {
jsonString := fmt.Sprintf(`"%s"`, p.String())
return []byte(jsonString), nil
}

// MarshalText .....
func (p PolicyStatusType) MarshalText() (text []byte, err error) {
return []byte(p.String()), nil
}

// UnmarshalText .....
func (p *PolicyStatusType) UnmarshalText(text []byte) (err error) {
status, err := parseHubPolicyStatusType(string(text))
if err != nil {
return err
}
*p = status
return nil
}

// Project .....
type Project struct {
Name string
Expand All @@ -177,162 +131,32 @@ type Project struct {

// RiskProfile .....
type RiskProfile struct {
Categories map[RiskProfileCategory]RiskProfileStatusCounts
Categories map[string]RiskProfileStatusCounts
BomLastUpdatedAt string
}

// HighRiskVulnerabilityCount .....
func (rp *RiskProfile) HighRiskVulnerabilityCount() int {
// CriticalAndHighRiskVulnerabilityCount returns the combination of CRITICAL and HIGH risk profile count
func (rp *RiskProfile) CriticalAndHighRiskVulnerabilityCount() int {
vulnerabilities, ok := rp.Categories[RiskProfileCategoryVulnerability]
if !ok {
return 0
}
return vulnerabilities.HighRiskVulnerabilityCount()
}

// RiskProfileCategory .....
type RiskProfileCategory int

// .....
const (
RiskProfileCategoryActivity RiskProfileCategory = iota
RiskProfileCategoryLicense RiskProfileCategory = iota
RiskProfileCategoryOperational RiskProfileCategory = iota
RiskProfileCategoryVersion RiskProfileCategory = iota
RiskProfileCategoryVulnerability RiskProfileCategory = iota
)

// String .....
func (r RiskProfileCategory) String() string {
switch r {
case RiskProfileCategoryActivity:
return "ACTIVITY"
case RiskProfileCategoryLicense:
return "LICENSE"
case RiskProfileCategoryOperational:
return "OPERATIONAL"
case RiskProfileCategoryVersion:
return "VERSION"
case RiskProfileCategoryVulnerability:
return "VULNERABILITY"
default:
panic(fmt.Errorf("invalid RiskProfileCategory value: %d", r))
}
}

// func (r RiskProfileCategory) MarshalJSON() ([]byte, error) {
// jsonString := fmt.Sprintf(`"%s"`, r.String())
// return []byte(jsonString), nil
// }

// UnmarshalJSON .....
func (r *RiskProfileCategory) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}
status, err := parseHubRiskProfileCategory(str)
if err != nil {
return err
}
*r = status
return nil
}

// MarshalText .....
func (r RiskProfileCategory) MarshalText() (text []byte, err error) {
return []byte(r.String()), nil
}

// UnmarshalText .....
func (r *RiskProfileCategory) UnmarshalText(text []byte) (err error) {
status, err := parseHubRiskProfileCategory(string(text))
if err != nil {
return err
}
*r = status
return nil
}

// RiskProfileStatus .....
type RiskProfileStatus int

// .....
const (
RiskProfileStatusHigh RiskProfileStatus = iota
RiskProfileStatusMedium RiskProfileStatus = iota
RiskProfileStatusLow RiskProfileStatus = iota
RiskProfileStatusOK RiskProfileStatus = iota
RiskProfileStatusUnknown RiskProfileStatus = iota
)

// String .....
func (r RiskProfileStatus) String() string {
switch r {
case RiskProfileStatusHigh:
return "HIGH"
case RiskProfileStatusMedium:
return "MEDIUM"
case RiskProfileStatusLow:
return "LOW"
case RiskProfileStatusOK:
return "OK"
case RiskProfileStatusUnknown:
return "UNKNOWN"
default:
panic(fmt.Errorf("invalid RiskProfileStatus value: %d", r))
}
}

// MarshalJSON .....
func (r RiskProfileStatus) MarshalJSON() ([]byte, error) {
jsonString := fmt.Sprintf(`"%s"`, r.String())
return []byte(jsonString), nil
}

// UnmarshalJSON .....
func (r *RiskProfileStatus) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}
status, err := parseHubRiskProfileStatus(str)
if err != nil {
return err
}
*r = status
return nil
}

// MarshalText .....
func (r RiskProfileStatus) MarshalText() (text []byte, err error) {
return []byte(r.String()), nil
}

// UnmarshalText .....
func (r *RiskProfileStatus) UnmarshalText(text []byte) (err error) {
status, err := parseHubRiskProfileStatus(string(text))
if err != nil {
return err
}
*r = status
return nil
return vulnerabilities.HighRiskVulnerabilityCount() + vulnerabilities.CriticalRiskVulnerabilityCount()
}

// RiskProfileStatusCounts .....
type RiskProfileStatusCounts struct {
StatusCounts map[RiskProfileStatus]int
StatusCounts map[string]int
}

// HighRiskVulnerabilityCount .....
func (r *RiskProfileStatusCounts) HighRiskVulnerabilityCount() int {
highCount, ok := r.StatusCounts[RiskProfileStatusHigh]
if !ok {
return 0
}
return highCount
return r.StatusCounts[RiskProfileStatusHigh]
}

// CriticalRiskVulnerabilityCount return the CRITICAL vulnerability count
func (r *RiskProfileStatusCounts) CriticalRiskVulnerabilityCount() int {
return r.StatusCounts[RiskProfileStatusCritical]
}

// ScanStage describes the current stage of the scan
Expand Down Expand Up @@ -425,7 +249,7 @@ func (scan *ScanResults) IsDone() bool {

// VulnerabilityCount .....
func (scan *ScanResults) VulnerabilityCount() int {
return scan.RiskProfile.HighRiskVulnerabilityCount()
return scan.RiskProfile.CriticalAndHighRiskVulnerabilityCount()
}

// PolicyViolationCount .....
Expand All @@ -434,7 +258,7 @@ func (scan *ScanResults) PolicyViolationCount() int {
}

// OverallStatus .....
func (scan *ScanResults) OverallStatus() PolicyStatusType {
func (scan *ScanResults) OverallStatus() string {
return scan.PolicyStatus.OverallStatus
}

Expand Down
Loading

0 comments on commit cc63e16

Please sign in to comment.