Search in sources :

Example 1 with ToolVersion

use of com.epam.pipeline.entity.docker.ToolVersion in project cloud-pipeline by epam.

the class ToolVersionDaoTest method shouldCRUDWithSettings.

@Test
public void shouldCRUDWithSettings() {
    Long toolId = tool.getId();
    PipelineConfiguration pipelineConfiguration = new PipelineConfiguration();
    pipelineConfiguration.setCmdTemplate(TEST_CMD_TEMPLATE_1);
    pipelineConfiguration.setNodeCount(TEST_NODE_COUNT);
    pipelineConfiguration.setParameters(Collections.singletonMap(TEST_PARAMETER_NAME, new PipeConfValueVO(TEST_PARAMETER_VALUE)));
    ConfigurationEntry configurationEntry = new ConfigurationEntry(pipelineConfiguration);
    List<ConfigurationEntry> settings = Collections.singletonList(configurationEntry);
    toolVersion1.setSettings(settings);
    toolVersionDao.createToolVersionWithSettings(toolVersion1);
    ToolVersion actual = toolVersionDao.loadToolVersionWithSettings(toolId, TEST_VERSION).orElse(null);
    validateToolVersionSettings(actual, configurationEntry, TEST_VERSION, toolId, TEST_PARAMETER_NAME);
    pipelineConfiguration.setCmdTemplate(TEST_CMD_TEMPLATE_2);
    configurationEntry.setConfiguration(pipelineConfiguration);
    settings = Collections.singletonList(configurationEntry);
    toolVersion1.setSettings(settings);
    toolVersionDao.updateToolVersionWithSettings(toolVersion1);
    actual = toolVersionDao.loadToolVersionWithSettings(toolId, TEST_VERSION).orElse(null);
    validateToolVersionSettings(actual, configurationEntry, TEST_VERSION, toolId, TEST_PARAMETER_NAME);
    toolVersionDao.deleteToolVersions(toolId);
    actual = toolVersionDao.loadToolVersion(toolId, TEST_VERSION).orElse(null);
    assertThat(actual).isNull();
}
Also used : PipeConfValueVO(com.epam.pipeline.entity.configuration.PipeConfValueVO) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration) ToolVersion(com.epam.pipeline.entity.docker.ToolVersion) ConfigurationEntry(com.epam.pipeline.entity.configuration.ConfigurationEntry) Test(org.junit.Test) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest)

Example 2 with ToolVersion

use of com.epam.pipeline.entity.docker.ToolVersion in project cloud-pipeline by epam.

the class ToolVersionDaoTest method shouldCRUDForListOfVersions.

@Test
public void shouldCRUDForListOfVersions() {
    Long toolId = tool.getId();
    toolVersionDao.createToolVersion(toolVersion1);
    toolVersionDao.createToolVersion(toolVersion2);
    ToolVersion actual = toolVersionDao.loadToolVersion(toolId, TEST_VERSION).orElse(null);
    validateToolVersion(actual, TEST_DIGEST, TEST_SIZE, TEST_VERSION, TEST_LAST_MODIFIED_DATE, toolId);
    actual = toolVersionDao.loadToolVersion(toolId, TEST_VERSION_2).orElse(null);
    validateToolVersion(actual, TEST_DIGEST_2, TEST_SIZE, TEST_VERSION_2, TEST_LAST_MODIFIED_DATE, toolId);
    toolVersionDao.deleteToolVersions(toolId);
    actual = toolVersionDao.loadToolVersion(toolId, TEST_VERSION).orElse(null);
    assertThat(actual).isNull();
    actual = toolVersionDao.loadToolVersion(toolId, TEST_VERSION_2).orElse(null);
    assertThat(actual).isNull();
}
Also used : ToolVersion(com.epam.pipeline.entity.docker.ToolVersion) Test(org.junit.Test) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest)

Example 3 with ToolVersion

use of com.epam.pipeline.entity.docker.ToolVersion in project cloud-pipeline by epam.

the class AggregatingToolScanManagerTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    Whitebox.setInternalState(aggregatingToolScanManager, "preferenceManager", preferenceManager);
    when(preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_POLICY_DENY_NOT_SCANNED)).thenReturn(DENY_NOT_SCANNED);
    when(preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_POLICY_MAX_CRITICAL_VULNERABILITIES)).thenReturn(MAX_CRITICAL_VULNERABILITIES);
    when(preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_POLICY_MAX_HIGH_VULNERABILITIES)).thenReturn(MAX_HIGH_VULNERABILITIES);
    when(preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_POLICY_MAX_MEDIUM_VULNERABILITIES)).thenReturn(MAX_MEDIUM_VULNERABILITIES);
    when(preferenceManager.getPreference(SystemPreferences.DOCKER_SECURITY_TOOL_GRACE_HOURS)).thenReturn(0);
    // Dummy line, to shut up PMD
    Assert.assertNotNull(pipelineConfigurationManager);
    testUser.setAdmin(false);
    DockerRegistry testRegistry = new DockerRegistry();
    testTool = new Tool();
    testTool.setId(1L);
    testTool.setImage(TEST_IMAGE);
    ManifestV2 testManifest = new ManifestV2();
    testManifest.setLayers(Arrays.asList(new ManifestV2.Config(DIGEST_1, null), new ManifestV2.Config(DIGEST_2, null), new ManifestV2.Config(DIGEST_3, null)));
    toolScanResult.setLastLayerRef(DIGEST_1);
    toolScanResult.setScanDate(DateUtils.now());
    toolScanResult.setVulnerabilities(Collections.emptyList());
    ToolVersion attributes = new ToolVersion();
    attributes.setVersion(LATEST_VERSION);
    attributes.setDigest(DIGEST_3);
    ToolVersion actualAttr = new ToolVersion();
    actualAttr.setVersion(ACTUAL_SCANNED_VERSION);
    actualAttr.setDigest(DIGEST_3);
    actual.setLastLayerRef(aggregatingToolScanManager.getLayerName(TEST_IMAGE, ACTUAL_SCANNED_VERSION));
    actual.setScanDate(DateUtils.now());
    actual.setSuccessScanDate(DateUtils.now());
    actual.setDigest(DIGEST_3);
    ClairScanResult testScanResult = new ClairScanResult();
    feature = new ClairScanResult.ClairFeature();
    feature.setName("test");
    feature.setVersion("test1");
    clairVulnerability = new ClairScanResult.ClairVulnerability();
    clairVulnerability.setSeverity(VulnerabilitySeverity.Critical);
    clairVulnerability.setName(TEST_VULNERABILITY_NAME);
    clairVulnerability.setDescription(TEST_VULNERABILITY_DESCRIPTION);
    feature.setVulnerabilities(Collections.singletonList(clairVulnerability));
    testScanResult.setFeatures(Collections.singletonList(feature));
    DockerComponentScanResult dockerComponentScanResult = new DockerComponentScanResult();
    DockerComponentLayerScanResult layerScanResult = new DockerComponentLayerScanResult();
    testDependency = new ToolDependency(1, "latest", "test", "1.0", ToolDependency.Ecosystem.R_PKG, "R Package");
    layerScanResult.setDependencies(Collections.singletonList(testDependency));
    dockerComponentScanResult.setLayers(Collections.singletonList(layerScanResult));
    when(dataStorageApiService.getDataStorages()).thenReturn(Collections.emptyList());
    when(versionManager.getValidDockerImage(TEST_IMAGE)).thenReturn(TEST_IMAGE);
    when(authManager.getCurrentUser()).thenReturn(testUser);
    when(dockerRegistryManager.load(testTool.getRegistryId())).thenReturn(testRegistry);
    when(dockerClientFactory.getDockerClient(eq(testRegistry), anyString())).thenReturn(mockDockerClient);
    when(mockDockerClient.getManifest(any(), Mockito.anyString(), Mockito.anyString())).thenReturn(Optional.of(testManifest));
    when(mockDockerClient.getVersionAttributes(any(), eq(TEST_IMAGE), eq(LATEST_VERSION))).thenReturn(attributes);
    when(mockDockerClient.getVersionAttributes(any(), eq(TEST_IMAGE), eq(ACTUAL_SCANNED_VERSION))).thenReturn(actualAttr);
    when(clairService.scanLayer(any(ClairScanRequest.class))).then((Answer<MockCall<ClairScanRequest>>) invocation -> new MockCall<>((ClairScanRequest) invocation.getArguments()[0]));
    when(clairService.getScanResult(Mockito.anyString())).thenReturn(new MockCall<>(testScanResult));
    when(compScanService.scanLayer(any(DockerComponentScanRequest.class))).then((Answer<MockCall<DockerComponentScanRequest>>) invocation -> new MockCall<>((DockerComponentScanRequest) invocation.getArguments()[0]));
    when(compScanService.getScanResult(Mockito.anyString())).thenReturn(new MockCall<>(dockerComponentScanResult));
    when(messageHelper.getMessage(Mockito.anyString(), Mockito.any())).thenReturn("testMessage");
    when(messageHelper.getMessage(any(), any())).thenReturn("testMessage");
    when(toolManager.loadByNameOrId(TEST_IMAGE)).thenReturn(testTool);
    when(toolManager.loadToolVersionScan(testTool.getId(), LATEST_VERSION)).thenReturn(Optional.of(toolScanResult));
    when(toolManager.loadToolVersionScan(testTool.getId(), ACTUAL_SCANNED_VERSION)).thenReturn(Optional.of(actual));
    ToolVersion actual = new ToolVersion();
    actual.setDigest(DIGEST_3);
    when(toolVersionManager.loadToolVersion(testTool.getId(), ACTUAL_SCANNED_VERSION)).thenReturn(actual);
    ToolVersion old = new ToolVersion();
    old.setDigest(DIGEST_2);
    when(toolVersionManager.loadToolVersion(testTool.getId(), LATEST_VERSION)).thenReturn(old);
    when(toolManager.getTagFromImageName(Mockito.anyString())).thenReturn(LATEST_VERSION);
}
Also used : DockerComponentScanResult(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentScanResult) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) ManifestV2(com.epam.pipeline.entity.docker.ManifestV2) MockitoAnnotations(org.mockito.MockitoAnnotations) DataStorageApiService(com.epam.pipeline.manager.datastorage.DataStorageApiService) MessageHelper(com.epam.pipeline.common.MessageHelper) Matchers.eq(org.mockito.Matchers.eq) Spy(org.mockito.Spy) TestUtils(com.epam.pipeline.util.TestUtils) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) com.epam.pipeline.entity.scan(com.epam.pipeline.entity.scan) PipelineConfigurationManager(com.epam.pipeline.manager.pipeline.PipelineConfigurationManager) DateUtils(com.epam.pipeline.entity.utils.DateUtils) Request(okhttp3.Request) ClairService(com.epam.pipeline.manager.docker.scan.clair.ClairService) Matchers.any(org.mockito.Matchers.any) Tool(com.epam.pipeline.entity.pipeline.Tool) PreferenceDao(com.epam.pipeline.dao.preference.PreferenceDao) ClairScanResult(com.epam.pipeline.manager.docker.scan.clair.ClairScanResult) Whitebox(org.mockito.internal.util.reflection.Whitebox) DockerClientFactory(com.epam.pipeline.manager.docker.DockerClientFactory) ToolVersionManager(com.epam.pipeline.manager.docker.ToolVersionManager) Call(retrofit2.Call) java.util(java.util) Mock(org.mockito.Mock) ToolManager(com.epam.pipeline.manager.pipeline.ToolManager) ToolScanExternalServiceException(com.epam.pipeline.exception.ToolScanExternalServiceException) Response(retrofit2.Response) ClairScanRequest(com.epam.pipeline.manager.docker.scan.clair.ClairScanRequest) PipelineVersionManager(com.epam.pipeline.manager.pipeline.PipelineVersionManager) Matchers.anyString(org.mockito.Matchers.anyString) DockerRegistryManager(com.epam.pipeline.manager.docker.DockerRegistryManager) Answer(org.mockito.stubbing.Answer) DockerComponentScanService(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentScanService) ToolVersion(com.epam.pipeline.entity.docker.ToolVersion) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) DockerComponentLayerScanResult(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentLayerScanResult) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) DockerComponentScanRequest(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentScanRequest) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DockerRegistry(com.epam.pipeline.entity.pipeline.DockerRegistry) Mockito(org.mockito.Mockito) Callback(retrofit2.Callback) Preference(com.epam.pipeline.entity.preference.Preference) AuthManager(com.epam.pipeline.manager.security.AuthManager) Assert(org.junit.Assert) DockerClient(com.epam.pipeline.manager.docker.DockerClient) ManifestV2(com.epam.pipeline.entity.docker.ManifestV2) DockerComponentLayerScanResult(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentLayerScanResult) ClairScanResult(com.epam.pipeline.manager.docker.scan.clair.ClairScanResult) DockerRegistry(com.epam.pipeline.entity.pipeline.DockerRegistry) DockerComponentScanRequest(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentScanRequest) ClairScanRequest(com.epam.pipeline.manager.docker.scan.clair.ClairScanRequest) ToolVersion(com.epam.pipeline.entity.docker.ToolVersion) DockerComponentScanResult(com.epam.pipeline.manager.docker.scan.dockercompscan.DockerComponentScanResult) Tool(com.epam.pipeline.entity.pipeline.Tool) Before(org.junit.Before)

Example 4 with ToolVersion

use of com.epam.pipeline.entity.docker.ToolVersion in project cloud-pipeline by epam.

the class ToolVersionManagerTest method shouldUpdateToolVersion.

@Test
public void shouldUpdateToolVersion() {
    ToolVersion toolVersionWithSameVersion = ToolVersion.builder().digest(TEST_DIGEST_2).size(TEST_SIZE).version(TEST_VERSION).modificationDate(TEST_LAST_MODIFIED_DATE).toolId(1L).build();
    Optional<ToolVersion> toolVersion = Optional.ofNullable(this.toolVersion);
    when(toolVersionDao.loadToolVersion(TEST_TOOL_ID, TEST_VERSION)).thenReturn(toolVersion);
    when(dockerClient.getVersionAttributes(any(DockerRegistry.class), anyString(), anyString())).thenReturn(toolVersionWithSameVersion);
    doNothing().when(toolVersionDao).updateToolVersion(isA(ToolVersion.class));
    doThrow(getThrowable()).when(toolVersionDao).createToolVersion(any(ToolVersion.class));
    toolVersionManager.updateOrCreateToolVersion(TEST_TOOL_ID, TEST_VERSION, TEST_IMAGE, dockerRegistry, dockerClient);
    verify(toolVersionDao).updateToolVersion(toolVersionWithSameVersion);
}
Also used : DockerRegistry(com.epam.pipeline.entity.pipeline.DockerRegistry) ToolVersion(com.epam.pipeline.entity.docker.ToolVersion) Test(org.junit.Test) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest)

Example 5 with ToolVersion

use of com.epam.pipeline.entity.docker.ToolVersion in project cloud-pipeline by epam.

the class ToolScanSchedulerTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    Whitebox.setInternalState(toolScanScheduler, "dockerRegistryDao", dockerRegistryDao);
    Whitebox.setInternalState(toolScanScheduler, "toolManager", toolManager);
    Whitebox.setInternalState(toolScanScheduler, "messageHelper", messageHelper);
    Whitebox.setInternalState(toolScanScheduler, "authManager", authManager);
    Whitebox.setInternalState(toolScanScheduler, "scheduler", taskScheduler);
    when(dockerClientFactory.getDockerClient(Mockito.any(DockerRegistry.class), Mockito.anyString())).thenReturn(mockClient);
    when(mockClient.getImageTags(Mockito.anyString(), Mockito.anyString())).thenReturn(Collections.singletonList(LATEST_VERSION));
    ToolVersion toolVersion = new ToolVersion();
    toolVersion.setDigest("test_digest");
    toolVersion.setSize(DOCKER_SIZE);
    toolVersion.setVersion("test_version");
    when(mockClient.getVersionAttributes(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(toolVersion);
    vulnerability = new Vulnerability();
    vulnerability.setName("testVulnerability");
    vulnerability.setFixedBy("testFix");
    vulnerability.setFeature("testFeature");
    vulnerability.setSeverity(VulnerabilitySeverity.Critical);
    dependency = new ToolDependency(1, "latest", "test", "1.0", ToolDependency.Ecosystem.SYSTEM, null);
    when(toolScanManager.scanTool(Mockito.any(Tool.class), Mockito.anyString(), Mockito.anyBoolean())).thenReturn(new ToolVersionScanResult(LATEST_VERSION, Collections.singletonList(vulnerability), Collections.singletonList(dependency), ToolScanStatus.COMPLETED, TEST_LAYER_REF, TEST_LAYER_DIGEST));
    doNothing().when(toolVersionManager).updateOrCreateToolVersion(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), Mockito.any(DockerRegistry.class), Mockito.any(DockerClient.class));
    when(dockerRegistryManager.getImageToken(Mockito.any(DockerRegistry.class), Mockito.anyString())).thenReturn("token");
}
Also used : DockerRegistry(com.epam.pipeline.entity.pipeline.DockerRegistry) DockerClient(com.epam.pipeline.manager.docker.DockerClient) ToolVersion(com.epam.pipeline.entity.docker.ToolVersion) Tool(com.epam.pipeline.entity.pipeline.Tool) Before(org.junit.Before)

Aggregations

ToolVersion (com.epam.pipeline.entity.docker.ToolVersion)13 Test (org.junit.Test)5 DockerRegistry (com.epam.pipeline.entity.pipeline.DockerRegistry)4 Tool (com.epam.pipeline.entity.pipeline.Tool)4 Before (org.junit.Before)4 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)3 ManifestV2 (com.epam.pipeline.entity.docker.ManifestV2)3 DockerClient (com.epam.pipeline.manager.docker.DockerClient)3 Transactional (org.springframework.transaction.annotation.Transactional)2 MessageHelper (com.epam.pipeline.common.MessageHelper)1 PreferenceDao (com.epam.pipeline.dao.preference.PreferenceDao)1 AclTestDao (com.epam.pipeline.dao.util.AclTestDao)1 InstanceType (com.epam.pipeline.entity.cluster.InstanceType)1 ConfigurationEntry (com.epam.pipeline.entity.configuration.ConfigurationEntry)1 PipeConfValueVO (com.epam.pipeline.entity.configuration.PipeConfValueVO)1 PipelineConfiguration (com.epam.pipeline.entity.configuration.PipelineConfiguration)1 Preference (com.epam.pipeline.entity.preference.Preference)1 com.epam.pipeline.entity.scan (com.epam.pipeline.entity.scan)1 PipelineUser (com.epam.pipeline.entity.user.PipelineUser)1 DateUtils (com.epam.pipeline.entity.utils.DateUtils)1