Search in sources :

Example 6 with Tool

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

the class ToolManager method delete.

/**
 * Deletes a Tool from the database and from Docker Registry
 * @param registry registry identifier
 * @param image Tool's image
 * @param hard flag determines if the real image from Docker Registry should be deleted
 * @return the deleted Tool entity
 */
@Transactional(propagation = Propagation.REQUIRED)
public Tool delete(String registry, final String image, boolean hard) {
    Tool tool = loadTool(registry, image);
    if (hard) {
        DockerRegistry dockerRegistry = dockerRegistryManager.load(tool.getRegistryId());
        List<String> tags = dockerRegistryManager.loadImageTags(dockerRegistry, image);
        for (String tag : tags) {
            Optional<ManifestV2> manifestOpt = dockerRegistryManager.deleteImage(dockerRegistry, tool.getImage(), tag);
            manifestOpt.ifPresent(manifest -> {
                dockerRegistryManager.deleteLayer(dockerRegistry, image, manifest.getConfig().getDigest());
                Collections.reverse(manifest.getLayers());
                for (ManifestV2.Config layer : manifest.getLayers()) {
                    dockerRegistryManager.deleteLayer(dockerRegistry, image, layer.getDigest());
                }
            });
        }
    }
    toolVulnerabilityDao.loadAllToolVersionScans(tool.getId()).values().forEach(versionScan -> deleteToolVersionScan(tool.getId(), versionScan.getVersion()));
    toolDao.deleteToolIcon(tool.getId());
    toolVersionManager.deleteToolVersions(tool.getId());
    toolDao.deleteTool(tool.getId());
    return tool;
}
Also used : DockerRegistry(com.epam.pipeline.entity.pipeline.DockerRegistry) ManifestV2(com.epam.pipeline.entity.docker.ManifestV2) Tool(com.epam.pipeline.entity.pipeline.Tool) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with Tool

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

the class ToolManager method changeOwner.

@Override
public AbstractSecuredEntity changeOwner(Long id, String owner) {
    Tool tool = toolDao.loadTool(id);
    tool.setOwner(owner);
    toolDao.updateTool(tool);
    return tool;
}
Also used : Tool(com.epam.pipeline.entity.pipeline.Tool)

Example 8 with Tool

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

the class ToolManager method loadByNameOrId.

/**
 * Tries to parse repository and image name from a string of pattern 'repo:5000/image'
 * @param identifier
 * @return
 */
@Override
public Tool loadByNameOrId(String identifier) {
    if (NumberUtils.isDigits(identifier)) {
        Tool imageTool = toolDao.loadTool(Long.parseLong(identifier));
        if (imageTool != null) {
            return imageTool;
        }
    }
    Matcher matcher = REPOSITORY_AND_IMAGE.matcher(identifier);
    String repository = "";
    String imageName;
    String imageWithTag;
    if (matcher.find()) {
        repository = matcher.group(1);
        imageWithTag = matcher.group(2);
        imageName = getImageWithoutTag(imageWithTag);
    } else {
        imageWithTag = identifier;
        imageName = getImageWithoutTag(identifier);
    }
    if (!imageAndTagAreValid(imageWithTag)) {
        throw new IllegalArgumentException(messageHelper.getMessage(MessageConstants.ERROR_INVALID_IMAGE_REPOSITORY, repository));
    }
    Tool imageTool = fetchTool(repository, imageName);
    if ((!repository.isEmpty() && !repository.equals(imageTool.getRegistry()))) {
        throw new IllegalArgumentException(messageHelper.getMessage(MessageConstants.ERROR_INVALID_IMAGE_REPOSITORY, repository));
    }
    // return value with tag
    imageTool.setImage(imageWithTag);
    return imageTool;
}
Also used : Matcher(java.util.regex.Matcher) Tool(com.epam.pipeline.entity.pipeline.Tool)

Example 9 with Tool

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

the class ToolManager method updateToolIcon.

/**
 * Updates an icon of a Tool, specified by ID
 * @param toolId an ID of a Tool, which icon to update
 * @param fileName a name of an icon file
 * @param image a byte content of a file
 * @return an ID of a newly created icon
 */
@Transactional(propagation = Propagation.REQUIRED)
public long updateToolIcon(long toolId, String fileName, byte[] image) {
    Tool tool = load(toolId);
    Assert.notNull(tool, messageHelper.getMessage(MessageConstants.ERROR_TOOL_NOT_FOUND, toolId));
    int allowedIconSize = preferenceManager.getPreference(SystemPreferences.MISC_MAX_TOOL_ICON_SIZE_KB) * KB_SIZE;
    Assert.isTrue(image.length <= allowedIconSize, messageHelper.getMessage(MessageConstants.ERROR_TOOL_ICON_TOO_LARGE, image.length, allowedIconSize));
    return toolDao.updateIcon(toolId, FilenameUtils.getName(fileName), image);
}
Also used : Tool(com.epam.pipeline.entity.pipeline.Tool) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with Tool

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

the class ToolManager method deleteToolVersion.

/**
 * Deletes Tool's version (a tag) from Docker Registry
 *
 * @param registry registry identifier
 * @param image Tool's image
 * @param version tag from registry
 * @return Tool entity, which version was deleted
 */
@Transactional(propagation = Propagation.REQUIRED)
public Tool deleteToolVersion(String registry, final String image, String version) {
    Tool tool = loadTool(registry, image);
    deleteToolVersionScan(tool.getId(), version);
    toolVersionManager.deleteToolVersion(tool.getId(), version);
    DockerRegistry dockerRegistry = dockerRegistryManager.load(tool.getRegistryId());
    dockerRegistryManager.deleteImage(dockerRegistry, tool.getImage(), version);
    return tool;
}
Also used : DockerRegistry(com.epam.pipeline.entity.pipeline.DockerRegistry) Tool(com.epam.pipeline.entity.pipeline.Tool) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Tool (com.epam.pipeline.entity.pipeline.Tool)72 Transactional (org.springframework.transaction.annotation.Transactional)28 DockerRegistry (com.epam.pipeline.entity.pipeline.DockerRegistry)24 Test (org.junit.Test)22 ToolGroup (com.epam.pipeline.entity.pipeline.ToolGroup)14 AbstractManagerTest (com.epam.pipeline.manager.AbstractManagerTest)11 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)9 DockerClient (com.epam.pipeline.manager.docker.DockerClient)9 List (java.util.List)9 PipelineConfiguration (com.epam.pipeline.entity.configuration.PipelineConfiguration)8 MessageHelper (com.epam.pipeline.common.MessageHelper)7 PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)7 ToolVersionScanResult (com.epam.pipeline.entity.scan.ToolVersionScanResult)7 ToolScanExternalServiceException (com.epam.pipeline.exception.ToolScanExternalServiceException)7 DockerRegistryManager (com.epam.pipeline.manager.docker.DockerRegistryManager)7 Optional (java.util.Optional)7 MessageConstants (com.epam.pipeline.common.MessageConstants)6 ManifestV2 (com.epam.pipeline.entity.docker.ManifestV2)6 ToolScanStatus (com.epam.pipeline.entity.pipeline.ToolScanStatus)6 DockerClientFactory (com.epam.pipeline.manager.docker.DockerClientFactory)6