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;
}
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));
}
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));
}
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);
}
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.");
}
Aggregations