Skip to content

Commit

Permalink
Add scan color for different vuls (#719)
Browse files Browse the repository at this point in the history

Signed-off-by: munishchouhan <[email protected]>
Signed-off-by: Paolo Di Tommaso <[email protected]>
Co-authored-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
munishchouhan and pditommaso authored Oct 25, 2024
1 parent a958a06 commit ab81b6d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/main/groovy/io/seqera/wave/controller/ViewController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package io.seqera.wave.controller

import java.util.regex.Pattern

import groovy.transform.Canonical
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.micronaut.context.annotation.Value
Expand All @@ -45,6 +46,7 @@ import io.seqera.wave.service.persistence.WaveBuildRecord
import io.seqera.wave.service.persistence.WaveScanRecord
import io.seqera.wave.service.scan.ContainerScanService
import io.seqera.wave.service.scan.ScanEntry
import io.seqera.wave.service.scan.ScanVulnerability
import io.seqera.wave.util.JacksonHelper
import jakarta.inject.Inject
import static io.seqera.wave.util.DataTimeUtils.formatDuration
Expand Down Expand Up @@ -408,7 +410,10 @@ class ViewController {
}

Map<String, Object> makeScanViewBinding(WaveScanRecord result, Map<String,Object> binding=new HashMap(10)) {
final color = getScanColor(result.vulnerabilities)
binding.should_refresh = !result.done()
binding.scan_color_bg = color.background
binding.scan_color_fg = color.foreground
binding.scan_id = result.id
binding.scan_container_image = result.containerImage ?: '-'
binding.scan_platform = result.platform?.toString() ?: '-'
Expand Down Expand Up @@ -437,4 +442,24 @@ class ViewController {
return binding
}

@Canonical
static class Colour {
final background
final foreground
}

protected static Colour getScanColor(List<ScanVulnerability> vulnerabilities){
boolean hasMedium = vulnerabilities.stream()
.anyMatch(v -> v.severity.equals("MEDIUM"))
boolean hasHighOrCritical = vulnerabilities.stream()
.anyMatch(v -> v.severity.equals("HIGH") || v.severity.equals("CRITICAL"))
if(hasHighOrCritical){
return new Colour('#ffe4e2', '#e00404')
}
else if(hasMedium){
return new Colour('#f7dc6f', "#000000")
}
return new Colour('#dff0d8', '#3c763d')
}

}
4 changes: 2 additions & 2 deletions src/main/resources/io/seqera/wave/scan-view.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ body {
{{#if scan_exist}}
{{#if scan_completed}}
{{#if scan_failed}}
<div style="color: #e00404; background-color: #ffe4e2; padding: 15px; border: 1px solid transparent; border-radius: 4px;">
<div style="color: black; background-color: #d3d3d3; padding: 15px; border: 1px solid transparent; border-radius: 4px;">
<h4 style="margin-top:0; color: inherit;">
Unable to complete the container security scan successfully
</h4>
</div>
{{else}}
<div style="color: #3c763d; background-color: #dff0d8; padding: 15px; border: 1px solid transparent; border-radius: 4px;">
<div style="color: {{scan_color_fg}}; background-color: {{scan_color_bg}}; padding: 15px; border: 1px solid transparent; border-radius: 4px;">
<h4 style="margin-top:0; color: inherit;">
Container security scan completed
</h4>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ import io.seqera.wave.tower.User
import jakarta.inject.Inject
import static io.seqera.wave.util.DataTimeUtils.formatDuration
import static io.seqera.wave.util.DataTimeUtils.formatTimestamp

import static io.seqera.wave.controller.ViewController.Colour

/**
*
* @author Paolo Di Tommaso <[email protected]>
Expand Down Expand Up @@ -743,4 +746,21 @@ class ViewControllerTest extends Specification {
null | false
'1234567890abcdef' | true
}

@Unroll
def 'should return correct scan color based on vulnerabilities'() {
expect:
ViewController.getScanColor(VULNERABILITIES) == EXPEXTED_COLOR

where:
VULNERABILITIES | EXPEXTED_COLOR
[new ScanVulnerability(severity: 'LOW')] | new Colour('#dff0d8','#3c763d')
[new ScanVulnerability(severity: 'MEDIUM')] | new Colour('#f7dc6f','#000000')
[new ScanVulnerability(severity: 'HIGH')] | new Colour('#ffe4e2','#e00404')
[new ScanVulnerability(severity: 'CRITICAL')] | new Colour('#ffe4e2','#e00404')
[new ScanVulnerability(severity: 'LOW'), new ScanVulnerability(severity: 'MEDIUM')] | new Colour('#f7dc6f','#000000')
[new ScanVulnerability(severity: 'LOW'), new ScanVulnerability(severity: 'HIGH')] | new Colour('#ffe4e2','#e00404')
[new ScanVulnerability(severity: 'MEDIUM'), new ScanVulnerability(severity: 'CRITICAL')] | new Colour('#ffe4e2','#e00404')
[] | new Colour('#dff0d8','#3c763d')
}
}

0 comments on commit ab81b6d

Please sign in to comment.