Search in sources :

Example 1 with BlackDuckServerConfig

use of com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig in project hub-alert by blackducksoftware.

the class BlackDuckSystemValidator method validate.

public boolean validate(BlackDuckProperties blackDuckProperties) {
    boolean valid = true;
    String configName = blackDuckProperties.getConfigName();
    logger.info("Validating Black Duck configuration '{}'...", configName);
    try {
        Optional<String> blackDuckUrlOptional = blackDuckProperties.getBlackDuckUrl();
        removeOldConfigMessages(blackDuckProperties, SystemMessageType.BLACKDUCK_PROVIDER_CONNECTIVITY, SystemMessageType.BLACKDUCK_PROVIDER_LOCALHOST, SystemMessageType.BLACKDUCK_PROVIDER_URL_MISSING);
        String errorMessage = String.format(MISSING_BLACKDUCK_URL_ERROR_W_CONFIG_FORMAT, configName);
        String urlMissingType = createProviderSystemMessageType(blackDuckProperties, SystemMessageType.BLACKDUCK_PROVIDER_URL_MISSING);
        boolean missingUrl = addSystemMessageForError(errorMessage, SystemMessageSeverity.WARNING, urlMissingType, blackDuckUrlOptional.isEmpty());
        if (missingUrl) {
            logger.error("  -> {}", String.format(MISSING_BLACKDUCK_CONFIG_ERROR_FORMAT, configName));
            valid = false;
        }
        if (blackDuckUrlOptional.isPresent()) {
            String blackDuckUrlString = blackDuckUrlOptional.get();
            Integer timeout = blackDuckProperties.getBlackDuckTimeout();
            logger.debug("  -> Black Duck configuration '{}' URL found validating: {}", configName, blackDuckUrlString);
            logger.debug("  -> Black Duck configuration '{}' Timeout: {}", configName, timeout);
            URL blackDuckUrl = new URL(blackDuckUrlString);
            String localhostMissingType = createProviderSystemMessageType(blackDuckProperties, SystemMessageType.BLACKDUCK_PROVIDER_LOCALHOST);
            boolean localHostError = addSystemMessageForError(String.format(BLACKDUCK_LOCALHOST_ERROR_FORMAT, configName), SystemMessageSeverity.WARNING, localhostMissingType, "localhost".equals(blackDuckUrl.getHost()));
            if (localHostError) {
                logger.warn("  -> {}", String.format(BLACKDUCK_LOCALHOST_ERROR_FORMAT, configName));
            }
            IntLogger intLogger = new Slf4jIntLogger(logger);
            BlackDuckServerConfig blackDuckServerConfig = blackDuckProperties.createBlackDuckServerConfig(intLogger);
            boolean canConnect = blackDuckServerConfig.canConnect(intLogger);
            if (canConnect) {
                logger.info("  -> Black Duck configuration '{}' is Valid!", configName);
            } else {
                String message = String.format("Can not connect to the Black Duck server with the configuration '%s'.", configName);
                connectivityWarning(blackDuckProperties, message);
                valid = false;
            }
        } else {
            String message = String.format("The Black Duck configuration '%s' is not valid.", configName);
            connectivityWarning(blackDuckProperties, message);
            valid = false;
        }
        BlackDuckApiTokenValidator blackDuckAPITokenValidator = new BlackDuckApiTokenValidator(blackDuckProperties);
        if (!blackDuckAPITokenValidator.isApiTokenValid()) {
            connectivityWarning(blackDuckProperties, BLACKDUCK_API_PERMISSION_FORMAT);
            valid = false;
        }
    } catch (MalformedURLException | IntegrationException | AlertRuntimeException ex) {
        logger.error("  -> Black Duck configuration '{}' is invalid; cause: {}", configName, ex.getMessage());
        logger.debug(String.format("  -> Black Duck configuration '%s' Stack Trace: ", configName), ex);
        valid = false;
    }
    return valid;
}
Also used : MalformedURLException(java.net.MalformedURLException) IntegrationException(com.synopsys.integration.exception.IntegrationException) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) IntLogger(com.synopsys.integration.log.IntLogger) URL(java.net.URL) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)

Example 2 with BlackDuckServerConfig

use of com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig in project hub-alert by blackducksoftware.

the class SystemValidatorTest method testValidateHubValidProviderWithProxy.

@Test
public void testValidateHubValidProviderWithProxy() throws Exception {
    ProxyManager proxyManager = Mockito.mock(ProxyManager.class);
    CredentialsBuilder builder = Credentials.newBuilder();
    builder.setUsername("AUser");
    builder.setPassword("aPassword");
    Credentials credentials = builder.build();
    ProxyInfoBuilder proxyBuilder = ProxyInfo.newBuilder();
    proxyBuilder.setHost("google.com");
    proxyBuilder.setPort(3218);
    proxyBuilder.setCredentials(credentials);
    proxyBuilder.setNtlmDomain(null);
    proxyBuilder.setNtlmWorkstation(null);
    ProxyInfo expectedProxyInfo = proxyBuilder.build();
    Mockito.when(proxyManager.createProxyInfoForHost(Mockito.anyString())).thenReturn(expectedProxyInfo);
    BlackDuckProperties blackDuckProperties = Mockito.mock(BlackDuckProperties.class);
    Mockito.when(blackDuckProperties.getBlackDuckUrl()).thenReturn(Optional.of("Black Duck URL"));
    Mockito.when(blackDuckProperties.getApiToken()).thenReturn("Black Duck API Token");
    Mockito.when(blackDuckProperties.getBlackDuckTimeout()).thenReturn(BlackDuckProperties.DEFAULT_TIMEOUT);
    BlackDuckServerConfig serverConfig = Mockito.mock(BlackDuckServerConfig.class);
    Mockito.when(serverConfig.canConnect(Mockito.any(IntLogger.class))).thenReturn(false);
    DefaultSystemMessageAccessor defaultSystemMessageUtility = Mockito.mock(DefaultSystemMessageAccessor.class);
    StatefulProvider statefulProvider = Mockito.mock(StatefulProvider.class);
    BlackDuckProvider provider = Mockito.mock(BlackDuckProvider.class);
    Mockito.when(provider.createStatefulProvider(Mockito.any())).thenReturn(statefulProvider);
    Mockito.when(statefulProvider.getProperties()).thenReturn(blackDuckProperties);
    BlackDuckSystemValidator blackDuckSystemValidator = new BlackDuckSystemValidator(defaultSystemMessageUtility);
    blackDuckSystemValidator.validate(blackDuckProperties);
    Mockito.verify(defaultSystemMessageUtility, Mockito.times(0)).addSystemMessage(Mockito.anyString(), Mockito.any(SystemMessageSeverity.class), Mockito.any(SystemMessageType.class));
}
Also used : BlackDuckProperties(com.synopsys.integration.alert.provider.blackduck.BlackDuckProperties) ProxyManager(com.synopsys.integration.alert.common.rest.proxy.ProxyManager) IntLogger(com.synopsys.integration.log.IntLogger) BlackDuckSystemValidator(com.synopsys.integration.alert.provider.blackduck.validator.BlackDuckSystemValidator) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig) ProxyInfo(com.synopsys.integration.rest.proxy.ProxyInfo) CredentialsBuilder(com.synopsys.integration.rest.credentials.CredentialsBuilder) SystemMessageSeverity(com.synopsys.integration.alert.common.enumeration.SystemMessageSeverity) SystemMessageType(com.synopsys.integration.alert.common.enumeration.SystemMessageType) DefaultSystemMessageAccessor(com.synopsys.integration.alert.database.system.DefaultSystemMessageAccessor) BlackDuckProvider(com.synopsys.integration.alert.provider.blackduck.BlackDuckProvider) Credentials(com.synopsys.integration.rest.credentials.Credentials) ProxyInfoBuilder(com.synopsys.integration.rest.proxy.ProxyInfoBuilder) StatefulProvider(com.synopsys.integration.alert.api.provider.state.StatefulProvider) Test(org.junit.jupiter.api.Test)

Example 3 with BlackDuckServerConfig

use of com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig in project hub-alert by blackducksoftware.

the class SystemValidatorTest method testvalidateBlackDuckProviderLocalhostURL.

@Test
public void testvalidateBlackDuckProviderLocalhostURL() throws Exception {
    long configId = 1L;
    ProxyManager proxyManager = Mockito.mock(ProxyManager.class);
    Mockito.when(proxyManager.createProxyInfoForHost(Mockito.anyString())).thenReturn(ProxyInfo.NO_PROXY_INFO);
    BlackDuckProperties blackDuckProperties = Mockito.mock(BlackDuckProperties.class);
    Mockito.when(blackDuckProperties.getBlackDuckUrl()).thenReturn(Optional.of("https://localhost:443"));
    Mockito.when(blackDuckProperties.getBlackDuckTimeout()).thenReturn(BlackDuckProperties.DEFAULT_TIMEOUT);
    Mockito.when(blackDuckProperties.getConfigName()).thenReturn(DEFAULT_CONFIG_NAME);
    Mockito.when(blackDuckProperties.getConfigId()).thenReturn(configId);
    BlackDuckServerConfig blackDuckServerConfig = Mockito.mock(BlackDuckServerConfig.class);
    Mockito.when(blackDuckProperties.createBlackDuckServerConfig(Mockito.any(IntLogger.class))).thenReturn(blackDuckServerConfig);
    StatefulProvider statefulProvider = Mockito.mock(StatefulProvider.class);
    BlackDuckProvider provider = Mockito.mock(BlackDuckProvider.class);
    Mockito.when(provider.createStatefulProvider(Mockito.any())).thenReturn(statefulProvider);
    Mockito.when(statefulProvider.getProperties()).thenReturn(blackDuckProperties);
    DefaultSystemMessageAccessor defaultSystemMessageUtility = Mockito.mock(DefaultSystemMessageAccessor.class);
    String localhostMessageType = BlackDuckSystemValidator.createProviderSystemMessageType(blackDuckProperties, SystemMessageType.BLACKDUCK_PROVIDER_LOCALHOST);
    BlackDuckSystemValidator blackDuckSystemValidator = new BlackDuckSystemValidator(defaultSystemMessageUtility);
    blackDuckSystemValidator.validate(blackDuckProperties);
    Mockito.verify(defaultSystemMessageUtility).addSystemMessage(Mockito.eq(String.format(BlackDuckSystemValidator.BLACKDUCK_LOCALHOST_ERROR_FORMAT, DEFAULT_CONFIG_NAME)), Mockito.eq(SystemMessageSeverity.WARNING), Mockito.eq(localhostMessageType));
}
Also used : BlackDuckProperties(com.synopsys.integration.alert.provider.blackduck.BlackDuckProperties) ProxyManager(com.synopsys.integration.alert.common.rest.proxy.ProxyManager) IntLogger(com.synopsys.integration.log.IntLogger) BlackDuckSystemValidator(com.synopsys.integration.alert.provider.blackduck.validator.BlackDuckSystemValidator) BlackDuckProvider(com.synopsys.integration.alert.provider.blackduck.BlackDuckProvider) DefaultSystemMessageAccessor(com.synopsys.integration.alert.database.system.DefaultSystemMessageAccessor) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig) StatefulProvider(com.synopsys.integration.alert.api.provider.state.StatefulProvider) Test(org.junit.jupiter.api.Test)

Example 4 with BlackDuckServerConfig

use of com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig in project hub-alert by blackducksoftware.

the class BlackDuckProviderService method setupBlackDuckServicesFactory.

private BlackDuckServicesFactory setupBlackDuckServicesFactory() {
    BlackDuckServerConfigBuilder blackDuckServerConfigBuilder = new BlackDuckServerConfigBuilder();
    blackDuckServerConfigBuilder.setUrl(blackDuckProviderUrl).setApiToken(blackDuckApiToken).setTimeoutInSeconds(blackDuckTimeout).setTrustCert(true);
    BlackDuckServerConfig blackDuckServerConfig = blackDuckServerConfigBuilder.build();
    return blackDuckServerConfig.createBlackDuckServicesFactory(intLogger);
}
Also used : BlackDuckServerConfigBuilder(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig)

Example 5 with BlackDuckServerConfig

use of com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig in project hub-alert by blackducksoftware.

the class BlackDuckGlobalFieldModelTestAction method testConfig.

@Override
public MessageResult testConfig(String configId, FieldModel fieldModel, FieldUtility registeredFieldValues) throws IntegrationException {
    Slf4jIntLogger intLogger = new Slf4jIntLogger(logger);
    String apiToken = registeredFieldValues.getStringOrEmpty(BlackDuckDescriptor.KEY_BLACKDUCK_API_KEY);
    String url = registeredFieldValues.getStringOrEmpty(BlackDuckDescriptor.KEY_BLACKDUCK_URL);
    String timeout = registeredFieldValues.getStringOrEmpty(BlackDuckDescriptor.KEY_BLACKDUCK_TIMEOUT);
    Long parsedConfigurationId = ProviderProperties.UNKNOWN_CONFIG_ID;
    if (StringUtils.isNotBlank(configId)) {
        try {
            parsedConfigurationId = Long.valueOf(configId);
        } catch (NumberFormatException ex) {
            throw new AlertException("Configuration id not valid.");
        }
    }
    BlackDuckProperties blackDuckProperties = blackDuckPropertiesFactory.createProperties(parsedConfigurationId, registeredFieldValues);
    BlackDuckServerConfigBuilder blackDuckServerConfigBuilder = blackDuckProperties.createServerConfigBuilderWithoutAuthentication(intLogger, NumberUtils.toInt(timeout, 300));
    blackDuckServerConfigBuilder.setApiToken(apiToken);
    blackDuckServerConfigBuilder.setUrl(url);
    validateBlackDuckConfiguration(blackDuckServerConfigBuilder);
    BlackDuckServerConfig blackDuckServerConfig = blackDuckServerConfigBuilder.build();
    ConnectionResult connectionResult = blackDuckServerConfig.attemptConnection(intLogger);
    if (connectionResult.isFailure()) {
        String failureMessage = connectionResult.getFailureMessage().orElse("");
        Exception errorException = connectionResult.getException().orElse(null);
        if (RestConstants.UNAUTHORIZED_401 == connectionResult.getHttpStatusCode()) {
            throw AlertFieldException.singleFieldError(String.format("Invalid credential(s) for: %s. %s", url, failureMessage), BlackDuckDescriptor.KEY_BLACKDUCK_API_KEY, "This API Key isn't valid, try a different one.");
        } else if (connectionResult.getHttpStatusCode() > 0) {
            // TODO why are we throwing a non-alert exception?
            HttpUrl connectionUrl = new HttpUrl(url);
            throw new IntegrationRestException(HttpMethod.GET, connectionUrl, connectionResult.getHttpStatusCode(), String.format("Could not connect to: %s", url), failureMessage, errorException);
        }
        throw new AlertException(String.format("Could not connect to: %s. %s", url, failureMessage), errorException);
    }
    BlackDuckApiTokenValidator blackDuckAPITokenValidator = new BlackDuckApiTokenValidator(blackDuckProperties);
    if (!blackDuckAPITokenValidator.isApiTokenValid()) {
        throw AlertFieldException.singleFieldError(BlackDuckDescriptor.KEY_BLACKDUCK_API_KEY, "User permission failed. Cannot read notifications from Black Duck.");
    }
    return new MessageResult("Successfully connected to BlackDuck server.");
}
Also used : IntegrationRestException(com.synopsys.integration.rest.exception.IntegrationRestException) BlackDuckServerConfigBuilder(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder) BlackDuckProperties(com.synopsys.integration.alert.provider.blackduck.BlackDuckProperties) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) AlertFieldException(com.synopsys.integration.alert.common.exception.AlertFieldException) IntegrationException(com.synopsys.integration.exception.IntegrationException) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) IntegrationRestException(com.synopsys.integration.rest.exception.IntegrationRestException) HttpUrl(com.synopsys.integration.rest.HttpUrl) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) ConnectionResult(com.synopsys.integration.rest.client.ConnectionResult) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) BlackDuckApiTokenValidator(com.synopsys.integration.alert.provider.blackduck.validator.BlackDuckApiTokenValidator)

Aggregations

BlackDuckServerConfig (com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig)7 BlackDuckProperties (com.synopsys.integration.alert.provider.blackduck.BlackDuckProperties)5 IntLogger (com.synopsys.integration.log.IntLogger)5 StatefulProvider (com.synopsys.integration.alert.api.provider.state.StatefulProvider)4 ProxyManager (com.synopsys.integration.alert.common.rest.proxy.ProxyManager)4 DefaultSystemMessageAccessor (com.synopsys.integration.alert.database.system.DefaultSystemMessageAccessor)4 BlackDuckProvider (com.synopsys.integration.alert.provider.blackduck.BlackDuckProvider)4 BlackDuckSystemValidator (com.synopsys.integration.alert.provider.blackduck.validator.BlackDuckSystemValidator)4 Test (org.junit.jupiter.api.Test)4 SystemMessageSeverity (com.synopsys.integration.alert.common.enumeration.SystemMessageSeverity)2 SystemMessageType (com.synopsys.integration.alert.common.enumeration.SystemMessageType)2 BlackDuckServerConfigBuilder (com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder)2 IntegrationException (com.synopsys.integration.exception.IntegrationException)2 Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)2 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)1 AlertRuntimeException (com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)1 AlertFieldException (com.synopsys.integration.alert.common.exception.AlertFieldException)1 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)1 BlackDuckApiTokenValidator (com.synopsys.integration.alert.provider.blackduck.validator.BlackDuckApiTokenValidator)1 HttpUrl (com.synopsys.integration.rest.HttpUrl)1