Skip to content

Commit

Permalink
add validation for vulnerability scan options
Browse files Browse the repository at this point in the history
  • Loading branch information
karanibm6 committed Apr 5, 2024
1 parent 763b7bf commit d8d1c21
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/apis/build/v1beta1/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const (
OutputTimestampNotSupported BuildReason = "OutputTimestampNotSupported"
// OutputTimestampNotValid indicates that the output timestamp value is not valid
OutputTimestampNotValid BuildReason = "OutputTimestampNotValid"
// VulnerabilityScanSeverityNotValid indicates that the output vulnerability scan severity is not valid
VulnerabilityScanSeverityNotValid BuildReason = "VulnerabilityScanSeverityNotValid"

// AllValidationsSucceeded indicates a Build was successfully validated
AllValidationsSucceeded = "all validations succeeded"
Expand Down
33 changes: 33 additions & 0 deletions pkg/validate/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package validate
import (
"context"
"strconv"
"strings"

build "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -47,10 +48,42 @@ func (b *BuildSpecOutputValidator) ValidatePath(_ context.Context) error {
}
}

if b.Build.Spec.Output.VulnerabilityScan != nil {
if b.Build.Spec.Output.VulnerabilityScan.Ignore != nil {
if b.Build.Spec.Output.VulnerabilityScan.Ignore.Severity != nil {
severitiesStr := *b.Build.Spec.Output.VulnerabilityScan.Ignore.Severity
severities := strings.Split(severitiesStr, ",")
if !areValidSeverities(severities) {
b.Build.Status.Reason = build.BuildReasonPtr(build.VulnerabilityScanSeverityNotValid)
b.Build.Status.Message = pointer.String("output vulnerability scan severity is invalid, must be a comma separated combination of these values: Low, Medium, High, Critical, Unknown")
}
}
}
}

return nil
}

func (b *BuildSpecOutputValidator) isEmptySource() bool {
return b.Build.Spec.Source == nil ||
b.Build.Spec.Source.Git == nil && b.Build.Spec.Source.OCIArtifact == nil && b.Build.Spec.Source.Local == nil
}

func areValidSeverities(severities []string) bool {
for _, severity := range severities {
if !isValidSeverity(strings.ToUpper(severity)) {
return false
}
}
return true
}

func isValidSeverity(severity string) bool {
switch severity {
case "LOW", "MEDIUM", "HIGH", "CRITICAL", "UNKNOWN":
return true
default:
return false
}

}
51 changes: 51 additions & 0 deletions pkg/validate/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,55 @@ var _ = Describe("BuildSpecOutputValidator", func() {
Expect(*build.Status.Message).To(ContainSubstring("output timestamp value is invalid"))
})
})

Context("output vulnerabilityScan is specified", func() {
var sampleBuild = func(vulnerabilitySettings VulnerabilityScanOptions) *Build {
return &Build{
ObjectMeta: corev1.ObjectMeta{
Namespace: "foo",
Name: "bar",
},
Spec: BuildSpec{
Source: &Source{
Type: GitType,
Git: &Git{
URL: "https://github.com/shipwright-io/sample-go",
},
},
Strategy: Strategy{
Name: "magic",
},
Output: Image{
VulnerabilityScan: &vulnerabilitySettings,
},
},
}
}

It("should pass for valid severities", func() {
severities := "Low,HIGH,Medium"
vulnerabilitySettings := VulnerabilityScanOptions{
Ignore: &VulnerabilityIgnoreOptions{
Severity: &severities,
},
}
build := sampleBuild(vulnerabilitySettings)
validate(build)
Expect(build.Status.Reason).To(BeNil())
Expect(build.Status.Message).To(BeNil())
})

It("should fail for invvalid severities", func() {
severities := "LOWE"
vulnerabilitySettings := VulnerabilityScanOptions{
Ignore: &VulnerabilityIgnoreOptions{
Severity: &severities,
},
}
build := sampleBuild(vulnerabilitySettings)
validate(build)
Expect(*build.Status.Reason).To(Equal(VulnerabilityScanSeverityNotValid))
Expect(*build.Status.Message).To(ContainSubstring("vulnerability scan severity is invalid"))
})
})
})

0 comments on commit d8d1c21

Please sign in to comment.