Search in sources :

Example 11 with ToolVersionScanResult

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

the class TestUtils method generateScanResult.

public static void generateScanResult(int criticalVulnerabilitiesCount, int highVulnerabilitiesCount, int mediumVulnerabilitiesCount, ToolVersionScanResult versionScanResult) {
    List<Vulnerability> testVulnerabilities = IntStream.range(0, criticalVulnerabilitiesCount).mapToObj(i -> createVulnerability(VulnerabilitySeverity.Critical)).collect(Collectors.toList());
    testVulnerabilities.addAll(IntStream.range(0, highVulnerabilitiesCount).mapToObj(i -> createVulnerability(VulnerabilitySeverity.High)).collect(Collectors.toList()));
    testVulnerabilities.addAll(IntStream.range(0, mediumVulnerabilitiesCount).mapToObj(i -> createVulnerability(VulnerabilitySeverity.Medium)).collect(Collectors.toList()));
    versionScanResult.setVulnerabilities(testVulnerabilities);
    versionScanResult.setScanDate(new Date());
    versionScanResult.setSuccessScanDate(new Date());
    versionScanResult.setStatus(ToolScanStatus.COMPLETED);
}
Also used : IntStream(java.util.stream.IntStream) Date(java.util.Date) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ManifestV2(com.epam.pipeline.entity.docker.ManifestV2) Vulnerability(com.epam.pipeline.entity.scan.Vulnerability) Collectors(java.util.stream.Collectors) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) List(java.util.List) ToolVersionScanResult(com.epam.pipeline.entity.scan.ToolVersionScanResult) Map(java.util.Map) Optional(java.util.Optional) VulnerabilitySeverity(com.epam.pipeline.entity.scan.VulnerabilitySeverity) DockerClientFactory(com.epam.pipeline.manager.docker.DockerClientFactory) Assert(org.junit.Assert) Collections(java.util.Collections) DockerClient(com.epam.pipeline.manager.docker.DockerClient) ToolVersion(com.epam.pipeline.entity.docker.ToolVersion) ToolScanStatus(com.epam.pipeline.entity.pipeline.ToolScanStatus) Vulnerability(com.epam.pipeline.entity.scan.Vulnerability) Date(java.util.Date)

Example 12 with ToolVersionScanResult

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

the class AggregatingToolScanManager method convertResults.

private ToolVersionScanResult convertResults(ClairScanResult clairScanResult, DockerComponentScanResult compScanResult, Tool tool, String tag, String digest) {
    List<Vulnerability> vulnerabilities = Optional.ofNullable(clairScanResult).map(result -> ListUtils.emptyIfNull(result.getFeatures()).stream()).orElse(Stream.empty()).flatMap(f -> f.getVulnerabilities() != null ? f.getVulnerabilities().stream().map(v -> {
        Vulnerability vulnerability = new Vulnerability();
        vulnerability.setName(v.getName());
        vulnerability.setDescription(v.getDescription());
        vulnerability.setFixedBy(v.getFixedBy());
        vulnerability.setLink(v.getLink());
        vulnerability.setSeverity(v.getSeverity());
        vulnerability.setFeature(f.getName());
        vulnerability.setFeatureVersion(f.getVersion());
        return vulnerability;
    }) : Stream.empty()).collect(Collectors.toList());
    LOGGER.debug("Found: " + vulnerabilities.size() + " vulnerabilities for " + tool.getImage() + ":" + tag);
    // Concat dependencies from Clair and DockerCompScan
    List<ToolDependency> dependencies = Stream.concat(Optional.ofNullable(compScanResult).map(result -> ListUtils.emptyIfNull(result.getLayers()).stream()).orElse(Stream.empty()).flatMap(l -> l.getDependencies().stream().peek(dependency -> {
        dependency.setToolVersion(tag);
        dependency.setToolId(tool.getId());
    })), Optional.ofNullable(clairScanResult).map(result -> ListUtils.emptyIfNull(result.getFeatures()).stream()).orElse(Stream.empty()).map(f -> new ToolDependency(tool.getId(), tag, f.getName(), f.getVersion(), ToolDependency.Ecosystem.SYSTEM, null))).collect(Collectors.toList());
    LOGGER.debug("Found: " + dependencies.size() + " dependencies for " + tool.getImage() + ":" + tag);
    return new ToolVersionScanResult(tag, vulnerabilities, dependencies, ToolScanStatus.COMPLETED, clairScanResult.getName(), digest);
}
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) Vulnerability(com.epam.pipeline.entity.scan.Vulnerability) ToolDependency(com.epam.pipeline.entity.scan.ToolDependency)

Example 13 with ToolVersionScanResult

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

the class AggregatingToolScanManager method scanTool.

public ToolVersionScanResult scanTool(Tool tool, String tag, Boolean rescan) throws ToolScanExternalServiceException {
    DockerRegistry registry = dockerRegistryManager.load(tool.getRegistryId());
    Optional<ToolVersionScanResult> actualScan = rescan ? Optional.empty() : getActualScan(tool, tag, registry);
    return actualScan.isPresent() ? actualScan.get() : doScan(tool, tag, registry);
}
Also used : DockerRegistry(com.epam.pipeline.entity.pipeline.DockerRegistry) ToolVersionScanResult(com.epam.pipeline.entity.scan.ToolVersionScanResult)

Example 14 with ToolVersionScanResult

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

the class ToolScanScheduler method scheduledToolScan.

/**
 * A scheduled scan, that runs for all the registries, all tools and all tool versions, sends them to Tool Scanning
 * System and stores scanning results to the database.
 */
public void scheduledToolScan() {
    if (!preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_SCAN_ENABLED)) {
        LOGGER.info(messageHelper.getMessage(MessageConstants.ERROR_TOOL_SCAN_DISABLED));
        return;
    } else {
        LOGGER.info(messageHelper.getMessage(MessageConstants.INFO_TOOL_SCAN_SCHEDULED_STARTED));
    }
    boolean scanAllRegistries = preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_SCAN_ALL_REGISTRIES);
    List<DockerRegistry> registries = scanAllRegistries ? dockerRegistryDao.loadAllDockerRegistry() : dockerRegistryDao.loadDockerRegistriesWithSecurityScanEnabled();
    for (DockerRegistry registry : registries) {
        LOGGER.info(messageHelper.getMessage(MessageConstants.INFO_TOOL_SCAN_REGISTRY_STARTED, registry.getPath()));
        for (Tool tool : registry.getTools()) {
            DockerClient dockerClient = getDockerClient(registry, tool);
            try {
                List<String> versions = toolManager.loadTags(tool.getId());
                for (String version : versions) {
                    try {
                        ToolVersionScanResult result = toolScanManager.scanTool(tool, version, false);
                        toolManager.updateToolVulnerabilities(result.getVulnerabilities(), tool.getId(), version);
                        toolManager.updateToolDependencies(result.getDependencies(), tool.getId(), version);
                        toolManager.updateToolVersionScanStatus(tool.getId(), ToolScanStatus.COMPLETED, new Date(), version, result.getLastLayerRef(), result.getDigest());
                        updateToolVersion(tool, version, registry, dockerClient);
                    } catch (ToolScanExternalServiceException e) {
                        LOGGER.error(messageHelper.getMessage(MessageConstants.ERROR_TOOL_SCAN_FAILED, tool.getImage(), version), e);
                        toolManager.updateToolVersionScanStatus(tool.getId(), ToolScanStatus.FAILED, new Date(), version, null, null);
                    }
                }
            } catch (Exception e) {
                LOGGER.error(messageHelper.getMessage(MessageConstants.ERROR_TOOL_SCAN_FAILED, tool.getImage()), e);
                toolManager.updateToolVersionScanStatus(tool.getId(), ToolScanStatus.FAILED, new Date(), "latest", null, null);
            }
        }
    }
    LOGGER.info(messageHelper.getMessage(MessageConstants.INFO_TOOL_SCAN_SCHEDULED_DONE));
}
Also used : DockerRegistry(com.epam.pipeline.entity.pipeline.DockerRegistry) DockerClient(com.epam.pipeline.manager.docker.DockerClient) ToolVersionScanResult(com.epam.pipeline.entity.scan.ToolVersionScanResult) ToolScanExternalServiceException(com.epam.pipeline.exception.ToolScanExternalServiceException) Date(java.util.Date) ToolScanExternalServiceException(com.epam.pipeline.exception.ToolScanExternalServiceException) PipelineException(com.epam.pipeline.exception.PipelineException) Tool(com.epam.pipeline.entity.pipeline.Tool)

Example 15 with ToolVersionScanResult

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

the class ToolScanScheduler method forceScheduleScanTool.

/**
 * Schedule a Tool for security scan. Since a Tool's scan is a time costly operation, there's a queue for that.
 * A tool is added to that queue and will be processed in order. Once the tool is added to a queue, it's scanStatus
 * field is being set to {@link ToolScanStatus}.PENDING
 * @param registry a registry path, where tool is located
 * @param id Tool's id or image
 * @param version Tool's version (Docker tag)
 * @param rescan
 */
public Future<ToolVersionScanResult> forceScheduleScanTool(final String registry, final String id, final String version, final Boolean rescan) {
    if (!preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_SCAN_ENABLED)) {
        throw new IllegalArgumentException(messageHelper.getMessage(MessageConstants.ERROR_TOOL_SCAN_DISABLED));
    }
    Tool tool = toolManager.loadTool(registry, id);
    Optional<ToolVersionScanResult> toolVersionScanResult = toolManager.loadToolVersionScan(tool.getId(), version);
    ToolScanStatus curentStatus = toolVersionScanResult.map(ToolVersionScanResult::getStatus).orElse(ToolScanStatus.NOT_SCANNED);
    // The tool is already in the queue
    if (curentStatus != ToolScanStatus.PENDING) {
        String layerRef = toolVersionScanResult.map(ToolVersionScanResult::getLastLayerRef).orElse(null);
        String digest = toolVersionScanResult.map(ToolVersionScanResult::getDigest).orElse(null);
        toolManager.updateToolVersionScanStatus(tool.getId(), ToolScanStatus.PENDING, null, version, layerRef, digest);
        return forceScanExecutor.submit(new DelegatingSecurityContextCallable<>(() -> {
            LOGGER.info(messageHelper.getMessage(MessageConstants.INFO_TOOL_FORCE_SCAN_STARTED, tool.getImage()));
            try {
                ToolVersionScanResult scanResult = toolScanManager.scanTool(tool, version, rescan);
                toolManager.updateToolVulnerabilities(scanResult.getVulnerabilities(), tool.getId(), version);
                toolManager.updateToolDependencies(scanResult.getDependencies(), tool.getId(), version);
                toolManager.updateToolVersionScanStatus(tool.getId(), ToolScanStatus.COMPLETED, scanResult.getScanDate(), version, scanResult.getLastLayerRef(), scanResult.getDigest());
                return scanResult;
            } catch (Exception e) {
                toolManager.updateToolVersionScanStatus(tool.getId(), ToolScanStatus.FAILED, new Date(), version, null, null);
                LOGGER.error(messageHelper.getMessage(MessageConstants.ERROR_TOOL_SCAN_FAILED, tool.getImage()), e);
                throw new PipelineException(e);
            }
        }, SecurityContextHolder.getContext()));
    }
    return CompletableFuture.completedFuture(new ToolVersionScanResult(ToolScanStatus.PENDING, null, Collections.emptyList(), Collections.emptyList()));
}
Also used : PipelineException(com.epam.pipeline.exception.PipelineException) ToolVersionScanResult(com.epam.pipeline.entity.scan.ToolVersionScanResult) ToolScanStatus(com.epam.pipeline.entity.pipeline.ToolScanStatus) ToolScanExternalServiceException(com.epam.pipeline.exception.ToolScanExternalServiceException) PipelineException(com.epam.pipeline.exception.PipelineException) Date(java.util.Date) Tool(com.epam.pipeline.entity.pipeline.Tool)

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