Search in sources :

Example 1 with CodeLocationCreationService

use of com.synopsys.integration.blackduck.codelocation.CodeLocationCreationService in project hub-detect by blackducksoftware.

the class BlackduckPostActions method perform.

public void perform(BlackduckReportOptions blackduckReportOptions, PolicyCheckOptions policyCheckOptions, CodeLocationWaitData codeLocationWaitData, ProjectVersionWrapper projectVersionWrapper, long timeoutInSeconds) throws DetectUserFriendlyException {
    try {
        ProjectView projectView = projectVersionWrapper.getProjectView();
        ProjectVersionView projectVersionView = projectVersionWrapper.getProjectVersionView();
        if (policyCheckOptions.shouldPerformPolicyCheck() || blackduckReportOptions.shouldGenerateAnyReport()) {
            logger.info("Detect must wait for bom tool calculations to finish.");
            CodeLocationCreationService codeLocationCreationService = blackDuckServicesFactory.createCodeLocationCreationService();
            List<CodeLocationWaitResult> results = new ArrayList<>();
            if (codeLocationWaitData.hasBdioResults()) {
                CodeLocationWaitResult result = codeLocationCreationService.waitForCodeLocations(codeLocationWaitData.getBdioUploadRange(), codeLocationWaitData.getBdioUploadCodeLocationNames(), timeoutInSeconds);
                results.add(result);
            }
            if (codeLocationWaitData.hasScanResults()) {
                CodeLocationWaitResult result = codeLocationCreationService.waitForCodeLocations(codeLocationWaitData.getSignatureScanRange(), codeLocationWaitData.getSignatureScanCodeLocationNames(), timeoutInSeconds);
                results.add(result);
            }
            if (codeLocationWaitData.hasBinaryScanResults()) {
                CodeLocationWaitResult result = codeLocationCreationService.waitForCodeLocations(codeLocationWaitData.getBinaryScanRange(), codeLocationWaitData.getBinaryScanCodeLocationNames(), timeoutInSeconds);
                results.add(result);
            }
            for (CodeLocationWaitResult result : results) {
                if (result.getStatus() == CodeLocationWaitResult.Status.PARTIAL) {
                    throw new DetectUserFriendlyException(result.getErrorMessage().orElse("Timed out waiting for code locations to finish on the Black Duck server."), ExitCodeType.FAILURE_TIMEOUT);
                }
            }
        }
        if (policyCheckOptions.shouldPerformPolicyCheck()) {
            logger.info("Detect will check policy for violations.");
            PolicyChecker policyChecker = new PolicyChecker(eventSystem);
            policyChecker.checkPolicy(policyCheckOptions.getSeveritiesToFailPolicyCheck(), blackDuckServicesFactory.createProjectService(), projectVersionView);
        }
        if (blackduckReportOptions.shouldGenerateAnyReport()) {
            ReportService reportService = blackDuckServicesFactory.createReportService(timeoutInSeconds);
            if (blackduckReportOptions.shouldGenerateRiskReport()) {
                logger.info("Creating risk report pdf");
                File reportDirectory = new File(blackduckReportOptions.getRiskReportPdfPath());
                File createdPdf = reportService.createReportPdfFile(reportDirectory, projectView, projectVersionView);
                logger.info(String.format("Created risk report pdf: %s", createdPdf.getCanonicalPath()));
            }
            if (blackduckReportOptions.shouldGenerateNoticesReport()) {
                logger.info("Creating notices report");
                File noticesDirectory = new File(blackduckReportOptions.getNoticesReportPath());
                final File noticesFile = reportService.createNoticesReportFile(noticesDirectory, projectView, projectVersionView);
                logger.info(String.format("Created notices report: %s", noticesFile.getCanonicalPath()));
            }
        }
    } catch (final DetectUserFriendlyException e) {
        throw e;
    } catch (final IllegalArgumentException e) {
        throw new DetectUserFriendlyException(String.format("Your Black Duck configuration is not valid: %s", e.getMessage()), e, ExitCodeType.FAILURE_HUB_CONNECTIVITY);
    } catch (final IntegrationRestException e) {
        throw new DetectUserFriendlyException(e.getMessage(), e, ExitCodeType.FAILURE_HUB_CONNECTIVITY);
    } catch (final BlackDuckTimeoutExceededException e) {
        throw new DetectUserFriendlyException(e.getMessage(), e, ExitCodeType.FAILURE_TIMEOUT);
    } catch (final Exception e) {
        throw new DetectUserFriendlyException(String.format("There was a problem: %s", e.getMessage()), e, ExitCodeType.FAILURE_GENERAL_ERROR);
    }
}
Also used : IntegrationRestException(com.synopsys.integration.rest.exception.IntegrationRestException) CodeLocationCreationService(com.synopsys.integration.blackduck.codelocation.CodeLocationCreationService) CodeLocationWaitResult(com.synopsys.integration.blackduck.codelocation.CodeLocationWaitResult) BlackDuckTimeoutExceededException(com.synopsys.integration.blackduck.exception.BlackDuckTimeoutExceededException) ArrayList(java.util.ArrayList) BlackDuckTimeoutExceededException(com.synopsys.integration.blackduck.exception.BlackDuckTimeoutExceededException) DetectUserFriendlyException(com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException) IntegrationRestException(com.synopsys.integration.rest.exception.IntegrationRestException) DetectUserFriendlyException(com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException) ReportService(com.synopsys.integration.blackduck.service.ReportService) ProjectVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView) ProjectView(com.synopsys.integration.blackduck.api.generated.view.ProjectView) File(java.io.File)

Example 2 with CodeLocationCreationService

use of com.synopsys.integration.blackduck.codelocation.CodeLocationCreationService in project hub-detect by blackducksoftware.

the class BlackDuckSignatureScannerTool method runScanTool.

public SignatureScannerToolResult runScanTool(NameVersion projectNameVersion, Optional<File> dockerTar) throws DetectUserFriendlyException {
    DetectConfiguration detectConfiguration = detectContext.getBean(DetectConfiguration.class);
    DetectConfigurationFactory detectConfigurationFactory = detectContext.getBean(DetectConfigurationFactory.class);
    ConnectionManager connectionManager = detectContext.getBean(ConnectionManager.class);
    ConnectivityManager connectivityManager = detectContext.getBean(ConnectivityManager.class);
    DirectoryManager directoryManager = detectContext.getBean(DirectoryManager.class);
    Optional<BlackDuckServerConfig> hubServerConfig = Optional.empty();
    if (connectivityManager.isDetectOnline() && connectivityManager.getBlackDuckServerConfig().isPresent()) {
        hubServerConfig = connectivityManager.getBlackDuckServerConfig();
    }
    logger.info("Will run the signature scanner tool.");
    final String offlineLocalScannerInstallPath = detectConfiguration.getProperty(DetectProperty.DETECT_BLACKDUCK_SIGNATURE_SCANNER_OFFLINE_LOCAL_PATH, PropertyAuthority.None);
    final String onlineLocalScannerInstallPath = detectConfiguration.getProperty(DetectProperty.DETECT_BLACKDUCK_SIGNATURE_SCANNER_LOCAL_PATH, PropertyAuthority.None);
    String localScannerInstallPath = "";
    if (StringUtils.isNotBlank(offlineLocalScannerInstallPath)) {
        localScannerInstallPath = offlineLocalScannerInstallPath;
        logger.debug("Determined offline local scanner path: " + localScannerInstallPath);
    } else if (StringUtils.isNotBlank(onlineLocalScannerInstallPath)) {
        localScannerInstallPath = onlineLocalScannerInstallPath;
        logger.debug("Determined online local scanner path: " + localScannerInstallPath);
    }
    final String userProvidedScannerInstallUrl = detectConfiguration.getProperty(DetectProperty.DETECT_BLACKDUCK_SIGNATURE_SCANNER_HOST_URL, PropertyAuthority.None);
    BlackDuckSignatureScannerOptions blackDuckSignatureScannerOptions = detectConfigurationFactory.createBlackDuckSignatureScannerOptions();
    final ExecutorService executorService = Executors.newFixedThreadPool(blackDuckSignatureScannerOptions.getParrallelProcessors());
    IntEnvironmentVariables intEnvironmentVariables = new IntEnvironmentVariables();
    ScanBatchRunnerFactory scanBatchRunnerFactory = new ScanBatchRunnerFactory(intEnvironmentVariables, executorService);
    ScanBatchRunner scanBatchRunner;
    File installDirectory = directoryManager.getPermanentDirectory();
    if (hubServerConfig.isPresent() && StringUtils.isBlank(userProvidedScannerInstallUrl) && StringUtils.isBlank(localScannerInstallPath)) {
        logger.debug("Signature scanner will use the hub server to download/update the scanner - this is the most likely situation.");
        scanBatchRunner = scanBatchRunnerFactory.withHubInstall(hubServerConfig.get());
    } else {
        if (StringUtils.isNotBlank(userProvidedScannerInstallUrl)) {
            logger.debug("Signature scanner will use the provided url to download/update the scanner.");
            scanBatchRunner = scanBatchRunnerFactory.withUserProvidedUrl(userProvidedScannerInstallUrl, connectionManager);
        } else {
            logger.debug("Signature scanner either given an existing path for the scanner or is offline - either way, we won't attempt to manage the install.");
            if (StringUtils.isNotBlank(localScannerInstallPath)) {
                logger.debug("Using provided path: " + localScannerInstallPath);
                installDirectory = new File(localScannerInstallPath);
            } else {
                logger.debug("Using default scanner path.");
            }
            scanBatchRunner = scanBatchRunnerFactory.withoutInstall(installDirectory);
        }
    }
    logger.debug("Determined install directory: " + installDirectory.getAbsolutePath());
    try {
        if (hubServerConfig.isPresent()) {
            logger.debug("Signature scan is online.");
            CodeLocationCreationService codeLocationCreationService = connectivityManager.getBlackDuckServicesFactory().get().createCodeLocationCreationService();
            OnlineBlackDuckSignatureScanner blackDuckSignatureScanner = detectContext.getBean(OnlineBlackDuckSignatureScanner.class, signatureScannerOptions, scanBatchRunner, codeLocationCreationService, hubServerConfig.get());
            CodeLocationCreationData<ScanBatchOutput> codeLocationCreationData = blackDuckSignatureScanner.performOnlineScan(projectNameVersion, installDirectory, dockerTar.orElse(null));
            return SignatureScannerToolResult.createOnlineResult(codeLocationCreationData);
        } else {
            logger.debug("Signature scan is offline.");
            OfflineBlackDuckSignatureScanner blackDuckSignatureScanner = detectContext.getBean(OfflineBlackDuckSignatureScanner.class, signatureScannerOptions, scanBatchRunner);
            ScanBatchOutput scanBatchOutput = blackDuckSignatureScanner.performScanActions(projectNameVersion, installDirectory, dockerTar.orElse(null));
            return SignatureScannerToolResult.createOfflineResult(scanBatchOutput);
        }
    } catch (IOException | InterruptedException | IntegrationException e) {
        logger.info("Signature scan failed!");
        logger.debug("Signature scan error!", e);
        return SignatureScannerToolResult.createFailureResult();
    } finally {
        executorService.shutdownNow();
    }
}
Also used : CodeLocationCreationService(com.synopsys.integration.blackduck.codelocation.CodeLocationCreationService) IntegrationException(com.synopsys.integration.exception.IntegrationException) ConnectivityManager(com.blackducksoftware.integration.hub.detect.workflow.ConnectivityManager) DirectoryManager(com.blackducksoftware.integration.hub.detect.workflow.file.DirectoryManager) IOException(java.io.IOException) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig) ScanBatchRunner(com.synopsys.integration.blackduck.codelocation.signaturescanner.ScanBatchRunner) DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) IntEnvironmentVariables(com.synopsys.integration.util.IntEnvironmentVariables) ConnectionManager(com.blackducksoftware.integration.hub.detect.configuration.ConnectionManager) DetectConfigurationFactory(com.blackducksoftware.integration.hub.detect.configuration.DetectConfigurationFactory) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File) ScanBatchOutput(com.synopsys.integration.blackduck.codelocation.signaturescanner.ScanBatchOutput)

Aggregations

CodeLocationCreationService (com.synopsys.integration.blackduck.codelocation.CodeLocationCreationService)2 File (java.io.File)2 ConnectionManager (com.blackducksoftware.integration.hub.detect.configuration.ConnectionManager)1 DetectConfiguration (com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration)1 DetectConfigurationFactory (com.blackducksoftware.integration.hub.detect.configuration.DetectConfigurationFactory)1 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 ConnectivityManager (com.blackducksoftware.integration.hub.detect.workflow.ConnectivityManager)1 DirectoryManager (com.blackducksoftware.integration.hub.detect.workflow.file.DirectoryManager)1 ProjectVersionView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView)1 ProjectView (com.synopsys.integration.blackduck.api.generated.view.ProjectView)1 CodeLocationWaitResult (com.synopsys.integration.blackduck.codelocation.CodeLocationWaitResult)1 ScanBatchOutput (com.synopsys.integration.blackduck.codelocation.signaturescanner.ScanBatchOutput)1 ScanBatchRunner (com.synopsys.integration.blackduck.codelocation.signaturescanner.ScanBatchRunner)1 BlackDuckServerConfig (com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig)1 BlackDuckTimeoutExceededException (com.synopsys.integration.blackduck.exception.BlackDuckTimeoutExceededException)1 ReportService (com.synopsys.integration.blackduck.service.ReportService)1 IntegrationException (com.synopsys.integration.exception.IntegrationException)1 IntegrationRestException (com.synopsys.integration.rest.exception.IntegrationRestException)1 IntEnvironmentVariables (com.synopsys.integration.util.IntEnvironmentVariables)1 IOException (java.io.IOException)1