Search in sources :

Example 1 with DetectUserFriendlyException

use of com.synopsys.integration.detect.configuration.DetectUserFriendlyException in project synopsys-detect by blackducksoftware.

the class NugetAirGapCreator method installThenCopy.

private void installThenCopy(File nugetFolder, String folderName, ThrowingBiFunction<File, String, File, DetectableException> installer) throws DetectUserFriendlyException {
    try {
        File inspectorFolder = new File(nugetFolder, folderName);
        File installTarget = installer.apply(inspectorFolder, null);
        FileUtils.copyDirectory(installTarget, inspectorFolder);
        FileUtils.deleteDirectory(installTarget);
    } catch (DetectableException | IOException e) {
        throw new DetectUserFriendlyException("An error occurred installing to the " + folderName + " inspector folder.", e, ExitCodeType.FAILURE_GENERAL_ERROR);
    }
}
Also used : DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) IOException(java.io.IOException) File(java.io.File) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException)

Example 2 with DetectUserFriendlyException

use of com.synopsys.integration.detect.configuration.DetectUserFriendlyException in project synopsys-detect by blackducksoftware.

the class CreateBdio2FilesOperation method createBdioFiles.

public List<UploadTarget> createBdioFiles(BdioCodeLocationResult bdioCodeLocationResult, File outputDirectory, NameVersion projectNameVersion) throws DetectUserFriendlyException {
    List<UploadTarget> uploadTargets = new ArrayList<>();
    for (BdioCodeLocation bdioCodeLocation : bdioCodeLocationResult.getBdioCodeLocations()) {
        String codeLocationName = bdioCodeLocation.getCodeLocationName();
        ExternalId externalId = bdioCodeLocation.getDetectCodeLocation().getExternalId();
        DependencyGraph dependencyGraph = bdioCodeLocation.getDetectCodeLocation().getDependencyGraph();
        // Bdio 2
        String detectVersion = detectInfo.getDetectVersion();
        SpdxCreator detectCreator = SpdxCreator.createToolSpdxCreator("Detect", detectVersion);
        BdioMetadata bdioMetadata = bdio2Factory.createBdioMetadata(codeLocationName, ZonedDateTime.now(), new Product.Builder().name(detectCreator.getIdentifier()).build());
        bdioMetadata.scanType(Bdio.ScanType.PACKAGE_MANAGER);
        Project bdio2Project = bdio2Factory.createProject(externalId, projectNameVersion.getName(), projectNameVersion.getVersion(), true);
        Bdio2Document bdio2Document = bdio2Factory.createBdio2Document(bdioMetadata, bdio2Project, dependencyGraph);
        Bdio2Writer bdio2Writer = new Bdio2Writer();
        File bdio2OutputFile = new File(outputDirectory, bdioCodeLocation.getBdioName() + ".bdio");
        try {
            OutputStream outputStream = new FileOutputStream(bdio2OutputFile);
            bdio2Writer.writeBdioDocument(outputStream, bdio2Document);
            logger.debug(String.format("BDIO Generated: %s", bdio2OutputFile.getAbsolutePath()));
            uploadTargets.add(UploadTarget.createDefault(projectNameVersion, codeLocationName, bdio2OutputFile));
        } catch (IOException e) {
            throw new DetectUserFriendlyException(e.getMessage(), e, ExitCodeType.FAILURE_GENERAL_ERROR);
        }
    }
    return uploadTargets;
}
Also used : BdioMetadata(com.blackducksoftware.bdio2.BdioMetadata) Bdio2Writer(com.synopsys.integration.blackduck.bdio2.util.Bdio2Writer) Bdio2Document(com.synopsys.integration.blackduck.bdio2.model.Bdio2Document) UploadTarget(com.synopsys.integration.blackduck.codelocation.upload.UploadTarget) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) Product(com.blackducksoftware.common.value.Product) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) IOException(java.io.IOException) SpdxCreator(com.synopsys.integration.bdio.model.SpdxCreator) BdioCodeLocation(com.synopsys.integration.detect.workflow.codelocation.BdioCodeLocation) Project(com.blackducksoftware.bdio2.model.Project) DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 3 with DetectUserFriendlyException

use of com.synopsys.integration.detect.configuration.DetectUserFriendlyException in project synopsys-detect by blackducksoftware.

the class UniversalStepRunner method generateAggregateBdio.

private BdioResult generateAggregateBdio(AggregateDecision aggregateDecision, UniversalToolsResult universalToolsResult, NameVersion projectNameVersion, String aggregateName) throws OperationException, DetectUserFriendlyException {
    DependencyGraph aggregateDependencyGraph;
    if (aggregateDecision.getAggregateMode() == AggregateMode.DIRECT) {
        aggregateDependencyGraph = operationFactory.aggregateDirect(universalToolsResult.getDetectCodeLocations());
    } else if (aggregateDecision.getAggregateMode() == AggregateMode.TRANSITIVE) {
        aggregateDependencyGraph = operationFactory.aggregateTransitive(universalToolsResult.getDetectCodeLocations());
    } else if (aggregateDecision.getAggregateMode() == AggregateMode.SUBPROJECT) {
        aggregateDependencyGraph = operationFactory.aggregateSubProject(universalToolsResult.getDetectCodeLocations());
    } else {
        throw new DetectUserFriendlyException(String.format("The %s property was set to an unsupported aggregation mode, will not aggregate at this time.", DetectProperties.DETECT_BOM_AGGREGATE_REMEDIATION_MODE.getKey()), ExitCodeType.FAILURE_GENERAL_ERROR);
    }
    boolean isBdio2 = operationFactory.calculateBdioOptions().isBdio2Enabled();
    String aggregateExtension = isBdio2 ? ".bdio" : ".jsonld";
    AggregateCodeLocation aggregateCodeLocation = operationFactory.createAggregateCodeLocation(aggregateDependencyGraph, projectNameVersion, aggregateName, aggregateExtension);
    if (isBdio2) {
        operationFactory.createAggregateBdio2File(aggregateCodeLocation);
    } else {
        operationFactory.createAggregateBdio1File(aggregateCodeLocation);
    }
    List<UploadTarget> uploadTargets = new ArrayList<>();
    Map<DetectCodeLocation, String> codeLocationNamesResult = new HashMap<>();
    universalToolsResult.getDetectCodeLocations().forEach(cl -> codeLocationNamesResult.put(cl, aggregateCodeLocation.getCodeLocationName()));
    // TODO: This doesn't seem right, it should just be the aggregate CL name right?
    if (aggregateCodeLocation.getAggregateDependencyGraph().getRootDependencies().size() > 0 || aggregateDecision.shouldUploadEmptyAggregate()) {
        uploadTargets.add(UploadTarget.createDefault(projectNameVersion, aggregateCodeLocation.getCodeLocationName(), aggregateCodeLocation.getAggregateFile()));
    } else {
        logger.warn("The aggregate contained no dependencies, will not upload aggregate at this time.");
    }
    return new BdioResult(uploadTargets, new DetectCodeLocationNamesResult(codeLocationNamesResult), isBdio2);
}
Also used : AggregateCodeLocation(com.synopsys.integration.detect.workflow.bdio.AggregateCodeLocation) UploadTarget(com.synopsys.integration.blackduck.codelocation.upload.UploadTarget) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BdioResult(com.synopsys.integration.detect.workflow.bdio.BdioResult) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) DetectCodeLocationNamesResult(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocationNamesResult) DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation)

Example 4 with DetectUserFriendlyException

use of com.synopsys.integration.detect.configuration.DetectUserFriendlyException in project synopsys-detect by blackducksoftware.

the class ArtifactoryDockerInspectorResolver method install.

private DockerInspectorInfo install() throws IntegrationException, IOException, DetectUserFriendlyException {
    Optional<File> airGapDockerFolder = airGapInspectorPaths.getDockerInspectorAirGapFile();
    // TODO: Handle null better.
    Optional<Path> providedJarPath = dockerDetectableOptions.getDockerInspectorPath();
    if (providedJarPath.isPresent()) {
        logger.info("Docker tool will attempt to use the provided docker inspector.");
        return findProvidedJar(providedJarPath.get());
    } else if (airGapDockerFolder.isPresent()) {
        logger.info("Docker tool will attempt to use the air gapped docker inspector.");
        Optional<DockerInspectorInfo> airGapInspector = findAirGapInspector();
        return airGapInspector.orElse(null);
    } else {
        logger.info("Docker tool will attempt to download or find docker inspector.");
        File dockerDirectory = directoryManager.getPermanentDirectory(DOCKER_SHARED_DIRECTORY_NAME);
        // TODO: Handle null better.
        String dockerVersion = dockerDetectableOptions.getDockerInspectorVersion().orElse("");
        File inspector = null;
        Optional<File> cachedInstall = installedToolLocator.locateTool(INSTALLED_TOOL_JSON_KEY);
        try {
            inspector = dockerInspectorInstaller.installJar(dockerDirectory, Optional.of(dockerVersion));
        } catch (Exception e) {
            if (!cachedInstall.isPresent()) {
                throw e;
            }
        }
        if (inspector == null) {
            if (cachedInstall.isPresent()) {
                logger.debug("Using docker inspector from previous install.");
                return new DockerInspectorInfo(cachedInstall.get());
            }
            return null;
        } else {
            installedToolManager.saveInstalledToolLocation(INSTALLED_TOOL_JSON_KEY, inspector.getAbsolutePath());
            return new DockerInspectorInfo(inspector);
        }
    }
}
Also used : Path(java.nio.file.Path) Optional(java.util.Optional) File(java.io.File) DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) IntegrationException(com.synopsys.integration.exception.IntegrationException) IOException(java.io.IOException) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) DockerInspectorInfo(com.synopsys.integration.detectable.detectables.docker.DockerInspectorInfo)

Example 5 with DetectUserFriendlyException

use of com.synopsys.integration.detect.configuration.DetectUserFriendlyException in project synopsys-detect by blackducksoftware.

the class BinaryUploadOperation method uploadBinaryScanFile.

public CodeLocationCreationData<BinaryScanBatchOutput> uploadBinaryScanFile(File binaryScanFile, BinaryScanUploadService binaryScanUploadService, NameVersion projectNameVersion) throws DetectUserFriendlyException {
    String codeLocationName = codeLocationNameManager.createBinaryScanCodeLocationName(binaryScanFile, projectNameVersion.getName(), projectNameVersion.getVersion());
    try {
        logger.info("Preparing to upload binary scan file: " + binaryScanFile.getAbsolutePath());
        BinaryScan binaryScan = new BinaryScan(binaryScanFile, projectNameVersion.getName(), projectNameVersion.getVersion(), codeLocationName);
        BinaryScanBatch binaryScanBatch = new BinaryScanBatch(binaryScan);
        CodeLocationCreationData<BinaryScanBatchOutput> codeLocationCreationData = binaryScanUploadService.uploadBinaryScan(binaryScanBatch);
        BinaryScanBatchOutput binaryScanBatchOutput = codeLocationCreationData.getOutput();
        // The throwExceptionForError() in BinaryScanBatchOutput has a bug, so doing that work here
        throwExceptionForError(binaryScanBatchOutput);
        logger.info("Successfully uploaded binary scan file: " + binaryScanFile.getAbsolutePath());
        statusEventPublisher.publishStatusSummary(new Status(STATUS_KEY, StatusType.SUCCESS));
        return codeLocationCreationData;
    } catch (IntegrationException e) {
        statusEventPublisher.publishStatusSummary(new Status(STATUS_KEY, StatusType.FAILURE));
        throw new DetectUserFriendlyException("Failed to upload binary scan file.", e, ExitCodeType.FAILURE_BLACKDUCK_CONNECTIVITY);
    }
}
Also used : Status(com.synopsys.integration.detect.workflow.status.Status) DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) IntegrationException(com.synopsys.integration.exception.IntegrationException) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) BinaryScanBatch(com.synopsys.integration.blackduck.codelocation.binaryscanner.BinaryScanBatch) BinaryScanBatchOutput(com.synopsys.integration.blackduck.codelocation.binaryscanner.BinaryScanBatchOutput) BinaryScan(com.synopsys.integration.blackduck.codelocation.binaryscanner.BinaryScan)

Aggregations

DetectUserFriendlyException (com.synopsys.integration.detect.configuration.DetectUserFriendlyException)33 File (java.io.File)14 IntegrationException (com.synopsys.integration.exception.IntegrationException)12 IOException (java.io.IOException)12 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)5 ExitCodeType (com.synopsys.integration.detect.configuration.enumeration.ExitCodeType)3 ArrayList (java.util.ArrayList)3 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)2 SpdxCreator (com.synopsys.integration.bdio.model.SpdxCreator)2 Bdio2Writer (com.synopsys.integration.blackduck.bdio2.util.Bdio2Writer)2 UploadTarget (com.synopsys.integration.blackduck.codelocation.upload.UploadTarget)2 BlackDuckRequestBuilder (com.synopsys.integration.blackduck.http.BlackDuckRequestBuilder)2 BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)2 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)2 ProductRunData (com.synopsys.integration.detect.lifecycle.run.data.ProductRunData)2 CustomFieldOperation (com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldOperation)2 CustomFieldView (com.synopsys.integration.detect.workflow.blackduck.project.customfields.CustomFieldView)2 SilentIntLogger (com.synopsys.integration.log.SilentIntLogger)2 Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)2 FileOutputStream (java.io.FileOutputStream)2