Search in sources :

Example 1 with ToolVersionScanResult

use of com.epam.pipeline.entity.scan.ToolVersionScanResult in project cloud-pipeline by epam.

the class ToolVulnerabilityDao method loadAllToolVersionScans.

public Map<String, ToolVersionScanResult> loadAllToolVersionScans(long toolId) {
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue(ToolVersionColumns.TOOL_ID.name(), toolId);
    Map<String, ToolVersionScanResult> resultMap = new HashMap<>();
    List<ToolVersionScanResult> toolVersionScanResults = getNamedParameterJdbcTemplate().query(loadToolAllVersionScansQuery, params, ToolVersionColumns.getRowMapper());
    Map<String, List<Vulnerability>> vulnerabilitiesByVersion = loadVulnerabilities(toolId);
    Map<String, List<ToolDependency>> dependenciesByVersion = loadDependencies(toolId);
    for (ToolVersionScanResult scanResult : toolVersionScanResults) {
        scanResult.setVulnerabilities(vulnerabilitiesByVersion.get(scanResult.getVersion()));
        scanResult.setDependencies(dependenciesByVersion.get(scanResult.getVersion()));
        resultMap.put(scanResult.getVersion(), scanResult);
    }
    return resultMap;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ToolVersionScanResult(com.epam.pipeline.entity.scan.ToolVersionScanResult) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ToolVersionScanResult

use of com.epam.pipeline.entity.scan.ToolVersionScanResult in project cloud-pipeline by epam.

the class AggregatingToolScanManager method doScan.

private ToolVersionScanResult doScan(Tool tool, String tag, DockerRegistry registry) throws ToolScanExternalServiceException {
    if (clairService == null) {
        LOGGER.error("Clair service is not configured!");
        ToolVersionScanResult result = new ToolVersionScanResult();
        result.setToolId(tool.getId());
        result.setVersion(tag);
        result.setStatus(ToolScanStatus.NOT_SCANNED);
        return result;
    }
    try {
        String clairRef = scanLayers(tool, tag, registry);
        String digest = getDockerClient(tool.getImage(), registry).getVersionAttributes(registry, tool.getImage(), tag).getDigest();
        ClairScanResult clairResult = getScanResult(tool, clairService.getScanResult(clairRef));
        DockerComponentScanResult dockerScanResult = dockerComponentService == null ? null : getScanResult(tool, dockerComponentService.getScanResult(clairRef));
        return convertResults(clairResult, dockerScanResult, tool, tag, digest);
    } catch (IOException e) {
        throw new ToolScanExternalServiceException(tool, e);
    }
}
Also used : ToolVersionScanResult(com.epam.pipeline.entity.scan.ToolVersionScanResult) ClairScanResult(com.epam.pipeline.manager.docker.scan.clair.ClairScanResult) DockerComponentScanResult(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentScanResult) IOException(java.io.IOException) ToolScanExternalServiceException(com.epam.pipeline.exception.ToolScanExternalServiceException)

Example 3 with ToolVersionScanResult

use of com.epam.pipeline.entity.scan.ToolVersionScanResult in project cloud-pipeline by epam.

the class AggregatingToolScanManager method getActualScan.

private Optional<ToolVersionScanResult> getActualScan(Tool tool, String tag, DockerRegistry registry) {
    Optional<ToolVersionScanResult> versionScanResult = toolManager.loadToolVersionScan(tool.getId(), tag);
    if (versionScanResult.isPresent() && versionScanResult.get().getLastLayerRef() != null) {
        ToolVersionScanResult vs = versionScanResult.get();
        LOGGER.info(messageHelper.getMessage(MessageConstants.INFO_TOOL_SCAN_ALREADY_SCANNED, tool.getImage()));
        DockerClient dockerClient = getDockerClient(tool.getImage(), registry);
        String dockerRef = dockerClient.getVersionAttributes(registry, tool.getImage(), tag).getDigest();
        boolean isActual = vs.getDigest() != null && dockerRef.equals(vs.getDigest());
        if (isActual) {
            vs.setScanDate(DateUtils.now());
            return Optional.of(vs);
        } else {
            LOGGER.info(messageHelper.getMessage(MessageConstants.INFO_TOOL_SCAN_NEW_LAYERS, tool.getImage(), tag, vs.getDigest(), dockerRef));
        }
    }
    return Optional.empty();
}
Also used : ToolVersionScanResult(com.epam.pipeline.entity.scan.ToolVersionScanResult) DockerClient(com.epam.pipeline.manager.docker.DockerClient)

Example 4 with ToolVersionScanResult

use of com.epam.pipeline.entity.scan.ToolVersionScanResult in project cloud-pipeline by epam.

the class AggregatingToolScanManager method checkTool.

public boolean checkTool(Tool tool, String tag) {
    Optional<ToolVersionScanResult> versionScanOp = toolManager.loadToolVersionScan(tool.getId(), tag);
    int graceHours = preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_GRACE_HOURS);
    boolean isGracePeriodOrWhiteList = versionScanOp.isPresent() && (gracePeriodIsActive(versionScanOp.get(), graceHours) || versionScanOp.get().isFromWhiteList());
    if (isGracePeriodOrWhiteList) {
        LOGGER.debug("Tool: " + tool.getId() + " version: " + tag + " is from White list or Grace period still active! Proceed with running!");
        return true;
    }
    boolean denyNotScanned = preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_POLICY_DENY_NOT_SCANNED);
    if (denyNotScanned && (!versionScanOp.isPresent() || versionScanOp.get().getStatus() == ToolScanStatus.NOT_SCANNED || versionScanOp.get().getSuccessScanDate() == null)) {
        return false;
    }
    if (versionScanOp.isPresent()) {
        ToolVersionScanResult toolVersionScanResult = versionScanOp.get();
        Map<VulnerabilitySeverity, Integer> severityCounters = toolVersionScanResult.getVulnerabilities().stream().collect(HashMap::new, (map, v) -> {
            if (map.containsKey(v.getSeverity())) {
                map.put(v.getSeverity(), map.get(v.getSeverity()) + 1);
            } else {
                map.put(v.getSeverity(), 1);
            }
        }, (map1, map2) -> map1.keySet().forEach(k -> map1.merge(k, map2.get(k), (a, b) -> a + b)));
        int maxCriticalVulnerabilities = preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_POLICY_MAX_CRITICAL_VULNERABILITIES);
        if (maxCriticalVulnerabilities != DISABLED && maxCriticalVulnerabilities < severityCounters.getOrDefault(VulnerabilitySeverity.Critical, 0)) {
            return false;
        }
        int maxHighVulnerabilities = preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_POLICY_MAX_HIGH_VULNERABILITIES);
        if (maxHighVulnerabilities != DISABLED && maxHighVulnerabilities < severityCounters.getOrDefault(VulnerabilitySeverity.High, 0)) {
            return false;
        }
        int maxMediumVulnerabilities = preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_POLICY_MAX_MEDIUM_VULNERABILITIES);
        if (maxMediumVulnerabilities != DISABLED && maxMediumVulnerabilities < severityCounters.getOrDefault(VulnerabilitySeverity.Medium, 0)) {
            return false;
        }
    }
    return true;
}
Also used : DockerComponentScanResult(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentScanResult) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) Autowired(org.springframework.beans.factory.annotation.Autowired) ManifestV2(com.epam.pipeline.entity.docker.ManifestV2) Vulnerability(com.epam.pipeline.entity.scan.Vulnerability) URLUtils(com.epam.pipeline.utils.URLUtils) StringUtils(org.apache.commons.lang3.StringUtils) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ToolDependency(com.epam.pipeline.entity.scan.ToolDependency) MessageHelper(com.epam.pipeline.common.MessageHelper) ListUtils(org.apache.commons.collections4.ListUtils) Map(java.util.Map) VulnerabilitySeverity(com.epam.pipeline.entity.scan.VulnerabilitySeverity) DateUtils(com.epam.pipeline.entity.utils.DateUtils) ClairService(com.epam.pipeline.manager.docker.scan.clair.ClairService) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Retrofit(retrofit2.Retrofit) Tool(com.epam.pipeline.entity.pipeline.Tool) List(java.util.List) ClairScanResult(com.epam.pipeline.manager.docker.scan.clair.ClairScanResult) Stream(java.util.stream.Stream) JacksonConverterFactory(retrofit2.converter.jackson.JacksonConverterFactory) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) StringPreference(com.epam.pipeline.manager.preference.AbstractSystemPreference.StringPreference) DockerClientFactory(com.epam.pipeline.manager.docker.DockerClientFactory) Call(retrofit2.Call) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ToolScanPolicy(com.epam.pipeline.entity.scan.ToolScanPolicy) MessageConstants(com.epam.pipeline.common.MessageConstants) ToolManager(com.epam.pipeline.manager.pipeline.ToolManager) ToolScanExternalServiceException(com.epam.pipeline.exception.ToolScanExternalServiceException) HashMap(java.util.HashMap) Response(retrofit2.Response) ClairScanRequest(com.epam.pipeline.manager.docker.scan.clair.ClairScanRequest) DockerRegistryManager(com.epam.pipeline.manager.docker.DockerRegistryManager) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) Service(org.springframework.stereotype.Service) DockerComponentScanService(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentScanService) ToolScanStatus(com.epam.pipeline.entity.pipeline.ToolScanStatus) DockerComponentLayerScanResult(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentLayerScanResult) AbstractSystemPreference(com.epam.pipeline.manager.preference.AbstractSystemPreference) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) Logger(org.slf4j.Logger) DockerComponentScanRequest(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentScanRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) DockerRegistry(com.epam.pipeline.entity.pipeline.DockerRegistry) URLEncoder(java.net.URLEncoder) OkHttpClient(okhttp3.OkHttpClient) ToolVersionScanResult(com.epam.pipeline.entity.scan.ToolVersionScanResult) DockerClient(com.epam.pipeline.manager.docker.DockerClient) ToolVersionScanResult(com.epam.pipeline.entity.scan.ToolVersionScanResult) HashMap(java.util.HashMap) VulnerabilitySeverity(com.epam.pipeline.entity.scan.VulnerabilitySeverity)

Example 5 with ToolVersionScanResult

use of com.epam.pipeline.entity.scan.ToolVersionScanResult in project cloud-pipeline by epam.

the class ToolManagerTest method testUpdateToolScanStatusWithFail.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testUpdateToolScanStatusWithFail() {
    Tool tool = generateTool(TEST_GROUP_ID1);
    tool.setToolGroupId(firstToolGroup.getId());
    toolManager.create(tool, true);
    String layerRef = "layerref";
    String digest = "digest";
    Date scanDate = new Date();
    ToolScanStatus status = ToolScanStatus.COMPLETED;
    toolManager.updateToolVersionScanStatus(tool.getId(), status, scanDate, LATEST_TAG, layerRef, digest);
    ToolVersionScanResult versionScan = toolManager.loadToolVersionScan(tool.getId(), LATEST_TAG).get();
    Assert.assertEquals(status, versionScan.getStatus());
    Assert.assertEquals(scanDate, versionScan.getScanDate());
    Assert.assertEquals(scanDate, versionScan.getSuccessScanDate());
    status = ToolScanStatus.FAILED;
    layerRef = "newlayerref";
    digest = "newdigest";
    Date newScanDate = new Date();
    toolManager.updateToolVersionScanStatus(tool.getId(), status, newScanDate, LATEST_TAG, layerRef, digest);
    Assert.assertEquals(1, toolManager.loadToolScanResult(tool).getToolVersionScanResults().values().size());
    versionScan = toolManager.loadToolVersionScan(tool.getId(), LATEST_TAG).get();
    Assert.assertEquals(newScanDate, versionScan.getScanDate());
    Assert.assertEquals(scanDate, versionScan.getSuccessScanDate());
}
Also used : ToolVersionScanResult(com.epam.pipeline.entity.scan.ToolVersionScanResult) Matchers.anyString(org.mockito.Matchers.anyString) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ToolVersionScanResult (com.epam.pipeline.entity.scan.ToolVersionScanResult)19 DockerClient (com.epam.pipeline.manager.docker.DockerClient)6 DockerRegistry (com.epam.pipeline.entity.pipeline.DockerRegistry)5 Tool (com.epam.pipeline.entity.pipeline.Tool)5 ToolScanStatus (com.epam.pipeline.entity.pipeline.ToolScanStatus)5 ToolScanExternalServiceException (com.epam.pipeline.exception.ToolScanExternalServiceException)5 List (java.util.List)5 Transactional (org.springframework.transaction.annotation.Transactional)5 ManifestV2 (com.epam.pipeline.entity.docker.ManifestV2)4 Vulnerability (com.epam.pipeline.entity.scan.Vulnerability)4 AbstractManagerTest (com.epam.pipeline.manager.AbstractManagerTest)4 Date (java.util.Date)4 Matchers.anyString (org.mockito.Matchers.anyString)4 MessageConstants (com.epam.pipeline.common.MessageConstants)3 MessageHelper (com.epam.pipeline.common.MessageHelper)3 ToolDependency (com.epam.pipeline.entity.scan.ToolDependency)3 ToolScanResult (com.epam.pipeline.entity.scan.ToolScanResult)3 VulnerabilitySeverity (com.epam.pipeline.entity.scan.VulnerabilitySeverity)3 DateUtils (com.epam.pipeline.entity.utils.DateUtils)3 DockerClientFactory (com.epam.pipeline.manager.docker.DockerClientFactory)3