Search in sources :

Example 1 with ConnectionResult

use of com.synopsys.integration.blackduck.configuration.ConnectionResult in project hub-detect by blackducksoftware.

the class ConnectivityChecker method determineConnectivity.

public ConnectivityResult determineConnectivity(DetectConfiguration detectConfiguration, DetectOptionManager detectOptionManager, DetectInfo detectInfo, Gson gson, ObjectMapper objectMapper, EventSystem eventSystem) throws DetectUserFriendlyException {
    logger.info("Detect will check if it can communicate with the Black Duck Server.");
    Slf4jIntLogger blackduckLogger = new Slf4jIntLogger(logger);
    BlackDuckServerConfig blackDuckServerConfig = detectOptionManager.createBlackduckServerConfig();
    logger.info("Attempting connection to the Black Duck server");
    ConnectionResult connectionResult = blackDuckServerConfig.attemptConnection(blackduckLogger);
    if (connectionResult.isFailure()) {
        logger.error("Failed to connect to the Black Duck server");
        logger.debug(String.format("The Black Duck server responded with a status code of %d", connectionResult.getHttpStatusCode()));
        return ConnectivityResult.failure(connectionResult.getErrorMessage().orElse("Could not reach the Black Duck server or the credentials were invalid."));
    }
    // TODO: Get a detailed reason of why canConnect failed.
    logger.info("Connection to the Black Duck server was successful");
    BlackDuckServicesFactory blackDuckServicesFactory = blackDuckServerConfig.createBlackDuckServicesFactory(gson, objectMapper, blackduckLogger);
    try {
        final BlackDuckService blackDuckService = blackDuckServicesFactory.createBlackDuckService();
        final CurrentVersionView currentVersion = blackDuckService.getResponse(ApiDiscovery.CURRENT_VERSION_LINK_RESPONSE);
        logger.info(String.format("Successfully connected to BlackDuck (version %s)!", currentVersion.getVersion()));
    } catch (IntegrationException e) {
        throw new DetectUserFriendlyException("Could not determine which version of Black Duck detect connected to.", e, ExitCodeType.FAILURE_HUB_CONNECTIVITY);
    }
    Map<String, String> additionalMetaData = detectConfiguration.getPhoneHomeProperties();
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    BlackDuckPhoneHomeHelper blackDuckPhoneHomeHelper = BlackDuckPhoneHomeHelper.createAsynchronousPhoneHomeHelper(blackDuckServicesFactory, executorService);
    PhoneHomeManager phoneHomeManager = new OnlinePhoneHomeManager(additionalMetaData, detectInfo, gson, eventSystem, blackDuckPhoneHomeHelper);
    return ConnectivityResult.success(blackDuckServicesFactory, phoneHomeManager, blackDuckServerConfig);
}
Also used : CurrentVersionView(com.synopsys.integration.blackduck.api.generated.response.CurrentVersionView) IntegrationException(com.synopsys.integration.exception.IntegrationException) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) OnlinePhoneHomeManager(com.blackducksoftware.integration.hub.detect.workflow.phonehome.OnlinePhoneHomeManager) PhoneHomeManager(com.blackducksoftware.integration.hub.detect.workflow.phonehome.PhoneHomeManager) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig) DetectUserFriendlyException(com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) OnlinePhoneHomeManager(com.blackducksoftware.integration.hub.detect.workflow.phonehome.OnlinePhoneHomeManager) ConnectionResult(com.synopsys.integration.blackduck.configuration.ConnectionResult) BlackDuckPhoneHomeHelper(com.synopsys.integration.blackduck.phonehome.BlackDuckPhoneHomeHelper) ExecutorService(java.util.concurrent.ExecutorService) BlackDuckService(com.synopsys.integration.blackduck.service.BlackDuckService)

Example 2 with ConnectionResult

use of com.synopsys.integration.blackduck.configuration.ConnectionResult in project hub-detect by blackducksoftware.

the class DefaultInteractiveMode method configure.

@Override
public void configure() {
    printWelcome();
    final Boolean connectToHub = askYesOrNo("Would you like to connect to a Black Duck server?");
    if (connectToHub == true) {
        boolean connected = false;
        boolean skipConnectionTest = false;
        while (!connected && !skipConnectionTest) {
            setPropertyFromQuestion(DetectProperty.BLACKDUCK_URL, "What is the Black Duck server url?");
            println("You can now configure Black Duck with either an API token -OR- a username and password. The API token must already exist on the Black Duck server, but it is the preferred approach to configure your connection.");
            final Boolean useApiToken = askYesOrNo("Would you like to use an existing API token?");
            if (useApiToken) {
                setPropertyFromQuestion(DetectProperty.BLACKDUCK_API_TOKEN, "What is the API token?");
            } else {
                setPropertyFromQuestion(DetectProperty.BLACKDUCK_USERNAME, "What is the username?");
                final Boolean setHubPassword = askYesOrNoWithMessage("Would you like to set the password?", "WARNING: If you choose to save the settings, this password will be stored in plain text. You can set this password as an environment variable BLACKDUCK_HUB_PASSWORD.");
                if (setHubPassword) {
                    setPropertyFromSecretQuestion(DetectProperty.BLACKDUCK_PASSWORD, "What is the password?");
                }
            }
            final Boolean useProxy = askYesOrNo("Would you like to configure a proxy?");
            if (useProxy) {
                setPropertyFromQuestion(DetectProperty.BLACKDUCK_PROXY_HOST, "What is the proxy host?");
                setPropertyFromQuestion(DetectProperty.BLACKDUCK_PROXY_PORT, "What is the proxy port?");
                setPropertyFromQuestion(DetectProperty.BLACKDUCK_PROXY_USERNAME, "What is the Black Duck username?");
                final Boolean setHubPassword = askYesOrNoWithMessage("Would you like to set the Black Duck password?", "WARNING: If you choose to save the settings, this password will be stored in plain text. You can set this password as an environment variable BLACKDUCK_PROXY_PASSWORD.");
                if (setHubPassword) {
                    setPropertyFromSecretQuestion(DetectProperty.BLACKDUCK_PROXY_PASSWORD, "What is the proxy password?");
                }
                final Boolean useNtlmProxy = askYesOrNo("Do you use a ntlm proxy?");
                if (useNtlmProxy) {
                    setPropertyFromQuestion(DetectProperty.BLACKDUCK_PROXY_NTLM_DOMAIN, "What is the ntlm proxy domain?");
                    setPropertyFromQuestion(DetectProperty.BLACKDUCK_PROXY_NTLM_WORKSTATION, "What is the ntlm proxy workstation?");
                }
            }
            final Boolean trustCert = askYesOrNo("Would you like to automatically trust certificates?");
            if (trustCert) {
                setProperty(DetectProperty.BLACKDUCK_TRUST_CERT, "true");
            }
            final Boolean testHub = askYesOrNo("Would you like to test the Black Duck connection now?");
            if (testHub) {
                ConnectionResult connectionAttempt = null;
                try {
                    detectOptionManager.applyInteractiveOptions(getInteractiveOptions());
                    BlackDuckServerConfig blackDuckServerConfig = detectOptionManager.createBlackduckServerConfig();
                    connectionAttempt = blackDuckServerConfig.attemptConnection(new SilentIntLogger());
                } catch (final Exception e) {
                    println("Failed to test connection.");
                    println(e.toString());
                    println("");
                }
                if (connectionAttempt != null && connectionAttempt.isSuccess()) {
                    connected = true;
                } else {
                    connected = false;
                    println("Failed to connect.");
                    println(connectionAttempt.getErrorMessage().orElse("Unknown reason."));
                    skipConnectionTest = !askYesOrNo("Would you like to retry entering Black Duck information?");
                }
            } else {
                skipConnectionTest = true;
            }
        }
        final Boolean customDetails = askYesOrNo("Would you like to provide a project name and version to use?");
        if (customDetails) {
            setPropertyFromQuestion(DetectProperty.DETECT_PROJECT_NAME, "What is the project name?");
            setPropertyFromQuestion(DetectProperty.DETECT_PROJECT_VERSION_NAME, "What is the project version?");
        }
    } else {
        setProperty(DetectProperty.BLACKDUCK_OFFLINE_MODE, "true");
    }
    final Boolean scan = askYesOrNo("Would you like run a CLI scan?");
    if (!scan) {
        setProperty(DetectProperty.DETECT_BLACKDUCK_SIGNATURE_SCANNER_DISABLED, "true");
    } else if (scan && connectToHub) {
        final Boolean upload = askYesOrNo("Would you like to upload CLI scan results to the Black Duck server?");
        if (!upload) {
            setProperty(DetectProperty.DETECT_BLACKDUCK_SIGNATURE_SCANNER_DRY_RUN, "true");
        }
    }
    if (scan) {
        final Boolean customScanner = askYesOrNo("Would you like to provide a custom scanner?");
        if (customScanner) {
            final Boolean downloadCustomScanner = askYesOrNo("Would you like to download the custom scanner?");
            if (downloadCustomScanner) {
                setPropertyFromQuestion(DetectProperty.DETECT_BLACKDUCK_SIGNATURE_SCANNER_HOST_URL, "What is the scanner host url?");
            } else {
                setPropertyFromQuestion(DetectProperty.DETECT_BLACKDUCK_SIGNATURE_SCANNER_OFFLINE_LOCAL_PATH, "What is the location of your offline scanner?");
            }
        }
    }
    performStandardOutflow();
}
Also used : SilentIntLogger(com.synopsys.integration.log.SilentIntLogger) ConnectionResult(com.synopsys.integration.blackduck.configuration.ConnectionResult) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig)

Aggregations

BlackDuckServerConfig (com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig)2 ConnectionResult (com.synopsys.integration.blackduck.configuration.ConnectionResult)2 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 OnlinePhoneHomeManager (com.blackducksoftware.integration.hub.detect.workflow.phonehome.OnlinePhoneHomeManager)1 PhoneHomeManager (com.blackducksoftware.integration.hub.detect.workflow.phonehome.PhoneHomeManager)1 CurrentVersionView (com.synopsys.integration.blackduck.api.generated.response.CurrentVersionView)1 BlackDuckPhoneHomeHelper (com.synopsys.integration.blackduck.phonehome.BlackDuckPhoneHomeHelper)1 BlackDuckService (com.synopsys.integration.blackduck.service.BlackDuckService)1 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)1 IntegrationException (com.synopsys.integration.exception.IntegrationException)1 SilentIntLogger (com.synopsys.integration.log.SilentIntLogger)1 Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)1 ExecutorService (java.util.concurrent.ExecutorService)1