Search in sources :

Example 6 with Slf4jIntLogger

use of com.synopsys.integration.log.Slf4jIntLogger in project hub-alert by blackducksoftware.

the class BlackDuckPhoneHomeHandler method populatePhoneHomeData.

@Override
public PhoneHomeRequestBodyBuilder populatePhoneHomeData(ConfigurationModel configurationModel, NameVersion alertArtifactInfo) {
    String registrationId = null;
    String blackDuckUrl = PhoneHomeRequestBody.UNKNOWN_FIELD_VALUE;
    String blackDuckVersion = PhoneHomeRequestBody.UNKNOWN_FIELD_VALUE;
    try {
        descriptorAccessor.getRegisteredDescriptorById(configurationModel.getDescriptorId());
        StatefulProvider statefulProvider = provider.createStatefulProvider(configurationModel);
        BlackDuckProperties blackDuckProperties = (BlackDuckProperties) statefulProvider.getProperties();
        BlackDuckHttpClient blackDuckHttpClient = blackDuckProperties.createBlackDuckHttpClient(logger);
        BlackDuckServicesFactory blackDuckServicesFactory = blackDuckProperties.createBlackDuckServicesFactory(blackDuckHttpClient, new Slf4jIntLogger(logger));
        BlackDuckRegistrationService blackDuckRegistrationService = blackDuckServicesFactory.createBlackDuckRegistrationService();
        BlackDuckServerData blackDuckServerData = blackDuckRegistrationService.getBlackDuckServerData();
        blackDuckVersion = blackDuckServerData.getVersion();
        registrationId = blackDuckServerData.getRegistrationKey().orElse(null);
        blackDuckUrl = blackDuckProperties.getBlackDuckUrl().orElse(PhoneHomeRequestBody.UNKNOWN_FIELD_VALUE);
    } catch (IntegrationException ignored) {
    // ignoring this exception
    }
    // We must check if the reg id is blank because of an edge case in which Black Duck can authenticate (while the webserver is coming up) without registration
    if (StringUtils.isBlank(registrationId)) {
        registrationId = PhoneHomeRequestBody.UNKNOWN_FIELD_VALUE;
    }
    PhoneHomeRequestBodyBuilder phoneHomeBuilder = new PhoneHomeRequestBodyBuilder(registrationId, blackDuckUrl, alertArtifactInfo, UniquePhoneHomeProduct.BLACK_DUCK, blackDuckVersion);
    return phoneHomeBuilder;
}
Also used : PhoneHomeRequestBodyBuilder(com.synopsys.integration.phonehome.request.PhoneHomeRequestBodyBuilder) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) IntegrationException(com.synopsys.integration.exception.IntegrationException) BlackDuckHttpClient(com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) BlackDuckServerData(com.synopsys.integration.blackduck.service.model.BlackDuckServerData) StatefulProvider(com.synopsys.integration.alert.api.provider.state.StatefulProvider) BlackDuckRegistrationService(com.synopsys.integration.blackduck.service.dataservice.BlackDuckRegistrationService)

Example 7 with Slf4jIntLogger

use of com.synopsys.integration.log.Slf4jIntLogger 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)

Example 8 with Slf4jIntLogger

use of com.synopsys.integration.log.Slf4jIntLogger in project hub-alert by blackducksoftware.

the class BlackDuckCacheHttpClientCache method retrieveOrCreateBlackDuckCacheHttpClient.

public BlackDuckHttpClient retrieveOrCreateBlackDuckCacheHttpClient(Long blackDuckConfigId) throws IntegrationException {
    BlackDuckHttpClient cachedClient = httpClientCache.get(blackDuckConfigId);
    if (null != cachedClient) {
        return cachedClient;
    }
    BlackDuckProperties properties = blackDuckPropertiesFactory.createProperties(blackDuckConfigId).orElseThrow(() -> new AlertConfigurationException(String.format("No BlackDuck configuration with id %s", blackDuckConfigId)));
    BlackDuckHttpClient blackDuckCacheClient = properties.createBlackDuckCacheClient(new Slf4jIntLogger(logger));
    httpClientCache.put(blackDuckConfigId, blackDuckCacheClient);
    return blackDuckCacheClient;
}
Also used : Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) BlackDuckHttpClient(com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)

Example 9 with Slf4jIntLogger

use of com.synopsys.integration.log.Slf4jIntLogger in project hub-alert by blackducksoftware.

the class BlackDuckIssueTrackerCallbackEventHandler method createBlackDuckServicesFactory.

private Optional<BlackDuckServicesFactory> createBlackDuckServicesFactory(BlackDuckProperties blackDuckProperties) {
    IntLogger intLogger = new Slf4jIntLogger(logger);
    try {
        BlackDuckHttpClient blackDuckHttpClient = blackDuckProperties.createBlackDuckHttpClient(intLogger);
        BlackDuckServicesFactory blackDuckServicesFactory = blackDuckProperties.createBlackDuckServicesFactory(blackDuckHttpClient, intLogger);
        return Optional.of(blackDuckServicesFactory);
    } catch (AlertException e) {
        logger.error("Failed to create a BlackDuck http client", e);
        return Optional.empty();
    }
}
Also used : Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) BlackDuckHttpClient(com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) IntLogger(com.synopsys.integration.log.IntLogger) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 10 with Slf4jIntLogger

use of com.synopsys.integration.log.Slf4jIntLogger in project hub-alert by blackducksoftware.

the class BlackDuckDataSyncTask method runProviderTask.

@Override
public void runProviderTask() {
    try {
        BlackDuckProperties providerProperties = getProviderProperties();
        Optional<BlackDuckHttpClient> optionalBlackDuckHttpClient = providerProperties.createBlackDuckHttpClientAndLogErrors(logger);
        if (optionalBlackDuckHttpClient.isPresent()) {
            BlackDuckHttpClient blackDuckHttpClient = optionalBlackDuckHttpClient.get();
            BlackDuckServicesFactory blackDuckServicesFactory = providerProperties.createBlackDuckServicesFactory(blackDuckHttpClient, new Slf4jIntLogger(logger));
            ProjectUsersService projectUsersService = blackDuckServicesFactory.createProjectUsersService();
            BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
            ApiDiscovery apiDiscovery = blackDuckServicesFactory.getApiDiscovery();
            List<ProjectView> projectViews = blackDuckApiClient.getAllResponses(apiDiscovery.metaProjectsLink());
            Map<ProjectView, ProviderProject> blackDuckToAlertProjects = mapBlackDuckProjectsToAlertProjects(projectViews, blackDuckApiClient);
            Map<ProviderProject, Set<String>> projectToEmailAddresses = getEmailsPerProject(blackDuckToAlertProjects, projectUsersService);
            Set<String> allRelevantBlackDuckUsers = getAllActiveBlackDuckUserEmailAddresses(blackDuckApiClient, apiDiscovery);
            blackDuckDataAccessor.updateProjectAndUserData(providerProperties.getConfigId(), projectToEmailAddresses, allRelevantBlackDuckUsers);
        } else {
            logger.error("Missing BlackDuck global configuration.");
        }
    } catch (IntegrationException | AlertRuntimeException e) {
        logger.error(String.format("Could not retrieve the current data from the BlackDuck server: %s", e.getMessage()), e);
    }
}
Also used : ProjectUsersService(com.synopsys.integration.blackduck.service.dataservice.ProjectUsersService) BlackDuckProperties(com.synopsys.integration.alert.provider.blackduck.BlackDuckProperties) Set(java.util.Set) IntegrationException(com.synopsys.integration.exception.IntegrationException) BlackDuckHttpClient(com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) ApiDiscovery(com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery) AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException) ProjectView(com.synopsys.integration.blackduck.api.generated.view.ProjectView) ProviderProject(com.synopsys.integration.alert.common.persistence.model.ProviderProject)

Aggregations

Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)13 BlackDuckHttpClient (com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient)8 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)6 IntegrationException (com.synopsys.integration.exception.IntegrationException)4 IntLogger (com.synopsys.integration.log.IntLogger)4 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)3 BlackDuckProperties (com.synopsys.integration.alert.provider.blackduck.BlackDuckProperties)3 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)2 AlertRuntimeException (com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)2 StatefulProvider (com.synopsys.integration.alert.api.provider.state.StatefulProvider)2 BlackDuckServerConfig (com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig)2 JiraHttpClient (com.synopsys.integration.jira.common.rest.JiraHttpClient)2 AlertFieldException (com.synopsys.integration.alert.common.exception.AlertFieldException)1 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)1 ProviderProject (com.synopsys.integration.alert.common.persistence.model.ProviderProject)1 BlackDuckApiTokenValidator (com.synopsys.integration.alert.provider.blackduck.validator.BlackDuckApiTokenValidator)1 ApiDiscovery (com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery)1 ProjectView (com.synopsys.integration.blackduck.api.generated.view.ProjectView)1 BlackDuckServerConfigBuilder (com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder)1 BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)1