Search in sources :

Example 1 with FieldUtility

use of com.synopsys.integration.alert.common.persistence.accessor.FieldUtility in project hub-alert by blackducksoftware.

the class AzureBoardsCustomFunctionAction method createActionResponse.

@Override
public ActionResponse<OAuthEndpointResponse> createActionResponse(FieldModel fieldModel, HttpServletContentWrapper servletContentWrapper) {
    try {
        Optional<FieldModel> savedFieldModel = saveIfValid(fieldModel);
        if (!savedFieldModel.isPresent()) {
            return createErrorResponse("The configuration is invalid. Please test the configuration.");
        }
        FieldUtility fieldUtility = createFieldAccessor(savedFieldModel.get());
        Optional<String> clientId = fieldUtility.getString(AzureBoardsDescriptor.KEY_CLIENT_ID);
        if (!clientId.isPresent()) {
            return createErrorResponse("App ID not found.");
        }
        Optional<String> alertServerUrl = alertWebServerUrlManager.getServerUrl();
        if (!alertServerUrl.isPresent()) {
            return createErrorResponse("Could not determine the alert server url for the callback.");
        }
        String requestKey = oAuthRequestValidator.generateRequestKey();
        // since we have only one OAuth channel now remove any other requests.
        // if we have more OAuth clients then the "remove requests" will have to be removed from here.
        // beginning authentication process create the request id at the start.
        oAuthRequestValidator.removeRequestsOlderThan5MinutesAgo();
        oAuthRequestValidator.addAuthorizationRequest(requestKey);
        logger.info("OAuth authorization request created: {}", requestKey);
        String authUrl = createAuthURL(clientId.get(), requestKey);
        logger.debug("Authenticating Azure OAuth URL: {}", authUrl);
        return new ActionResponse<>(HttpStatus.OK, new OAuthEndpointResponse(isAuthenticated(fieldUtility), authUrl, "Authenticating..."));
    } catch (Exception ex) {
        logger.error("Error activating Azure Boards", ex);
        return createErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "Error activating azure oauth.");
    }
}
Also used : OAuthEndpointResponse(com.synopsys.integration.alert.common.descriptor.config.field.endpoint.oauth.OAuthEndpointResponse) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) FieldModel(com.synopsys.integration.alert.common.rest.model.FieldModel) FieldUtility(com.synopsys.integration.alert.common.persistence.accessor.FieldUtility) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse)

Example 2 with FieldUtility

use of com.synopsys.integration.alert.common.persistence.accessor.FieldUtility in project hub-alert by blackducksoftware.

the class AzureOAuthCallbackController method oauthCallback.

@GetMapping
public ResponseEntity<String> oauthCallback(HttpServletRequest request) {
    logger.debug("Azure OAuth callback method called");
    if (!authorizationManager.hasExecutePermission(ConfigContextEnum.GLOBAL.name(), ChannelKeys.AZURE_BOARDS.getUniversalKey())) {
        logger.debug("Azure OAuth callback user does not have permission to call the controller.");
        return responseFactory.createForbiddenResponse();
    }
    String state = request.getParameter("state");
    String oAuthRequestId = oAuthRequestValidator.parseRequestIdString(state);
    try {
        String requestURI = request.getRequestURI();
        String requestQueryString = request.getQueryString();
        logger.debug("Request URI {}?{}", requestURI, requestQueryString);
        String authorizationCode = request.getParameter("code");
        if (!oAuthRequestValidator.hasRequestKey(state)) {
            logger.info("OAuth request with id {}: not found.", oAuthRequestId);
        } else {
            logger.info("OAuth request with id {}: Processing...", oAuthRequestId);
            oAuthRequestValidator.removeAuthorizationRequest(state);
            FieldUtility fieldUtility = createFieldAccessor();
            if (fieldUtility.getFields().isEmpty()) {
                logger.error("OAuth request with id {}: Azure oauth callback: Channel global configuration missing", oAuthRequestId);
            } else {
                if (StringUtils.isBlank(authorizationCode)) {
                    logger.error("OAuth request with id {}: Azure oauth callback: Authorization code isn't valid. Stop processing", oAuthRequestId);
                } else {
                    String oAuthRedirectUri = azureRedirectUrlCreator.createOAuthRedirectUri();
                    AzureBoardsProperties properties = AzureBoardsProperties.fromFieldAccessor(azureBoardsCredentialDataStoreFactory, oAuthRedirectUri, fieldUtility);
                    testOAuthConnection(properties, authorizationCode, oAuthRequestId);
                }
            }
        }
    } catch (Exception ex) {
        // catch any exceptions so the redirect back to the UI happens and doesn't display the URL with the authorization code to the user.
        logger.error("OAuth request with id {}: Azure OAuth callback error occurred", oAuthRequestId, ex);
    }
    // redirect back to the global channel configuration URL in the Alert UI.
    return responseFactory.createFoundRedirectResponse(azureRedirectUrlCreator.createUIRedirectLocation());
}
Also used : FieldUtility(com.synopsys.integration.alert.common.persistence.accessor.FieldUtility) AzureBoardsProperties(com.synopsys.integration.alert.channel.azure.boards.AzureBoardsProperties) HttpServiceException(com.synopsys.integration.azure.boards.common.http.HttpServiceException) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with FieldUtility

use of com.synopsys.integration.alert.common.persistence.accessor.FieldUtility in project hub-alert by blackducksoftware.

the class LdapManagerTest method testAuthenticationTypeDigest.

@Test
public void testAuthenticationTypeDigest() throws Exception {
    final String authenticationType = "digest";
    ConfigurationModel configurationModel = createConfigurationModel();
    configurationModel.getField(AuthenticationDescriptor.KEY_LDAP_AUTHENTICATION_TYPE).get().setFieldValue(authenticationType);
    DefaultConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = Mockito.mock(DefaultConfigurationModelConfigurationAccessor.class);
    Mockito.when(configurationModelConfigurationAccessor.getConfigurationsByDescriptorKey(Mockito.any(DescriptorKey.class))).thenReturn(List.of(configurationModel));
    UserManagementAuthoritiesPopulator authoritiesPopulator = Mockito.mock(UserManagementAuthoritiesPopulator.class);
    LdapManager ldapManager = new LdapManager(AUTHENTICATION_DESCRIPTOR_KEY, configurationModelConfigurationAccessor, authoritiesPopulator, LDAP_USER_CONTEXT_MAPPER);
    ldapManager.getAuthenticationProvider();
    FieldUtility updatedProperties = ldapManager.getCurrentConfiguration();
    assertEquals(authenticationType, updatedProperties.getField(AuthenticationDescriptor.KEY_LDAP_AUTHENTICATION_TYPE).flatMap(ConfigurationFieldModel::getFieldValue).orElse(null));
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) UserManagementAuthoritiesPopulator(com.synopsys.integration.alert.component.authentication.security.UserManagementAuthoritiesPopulator) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AuthenticationDescriptorKey(com.synopsys.integration.alert.component.authentication.descriptor.AuthenticationDescriptorKey) FieldUtility(com.synopsys.integration.alert.common.persistence.accessor.FieldUtility) DefaultConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.database.api.DefaultConfigurationModelConfigurationAccessor) Test(org.junit.jupiter.api.Test)

Example 4 with FieldUtility

use of com.synopsys.integration.alert.common.persistence.accessor.FieldUtility in project hub-alert by blackducksoftware.

the class LdapManagerTest method testAuthenticationTypeSimple.

@Test
public void testAuthenticationTypeSimple() throws Exception {
    final String authenticationType = "simple";
    ConfigurationModel configurationModel = createConfigurationModel();
    configurationModel.getField(AuthenticationDescriptor.KEY_LDAP_AUTHENTICATION_TYPE).get().setFieldValue(authenticationType);
    DefaultConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = Mockito.mock(DefaultConfigurationModelConfigurationAccessor.class);
    Mockito.when(configurationModelConfigurationAccessor.getConfigurationsByDescriptorKey(Mockito.any(DescriptorKey.class))).thenReturn(List.of(configurationModel));
    UserManagementAuthoritiesPopulator authoritiesPopulator = Mockito.mock(UserManagementAuthoritiesPopulator.class);
    LdapManager ldapManager = new LdapManager(AUTHENTICATION_DESCRIPTOR_KEY, configurationModelConfigurationAccessor, authoritiesPopulator, LDAP_USER_CONTEXT_MAPPER);
    ldapManager.getAuthenticationProvider();
    FieldUtility updatedProperties = ldapManager.getCurrentConfiguration();
    assertEquals(authenticationType, updatedProperties.getField(AuthenticationDescriptor.KEY_LDAP_AUTHENTICATION_TYPE).flatMap(ConfigurationFieldModel::getFieldValue).orElse(null));
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) UserManagementAuthoritiesPopulator(com.synopsys.integration.alert.component.authentication.security.UserManagementAuthoritiesPopulator) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AuthenticationDescriptorKey(com.synopsys.integration.alert.component.authentication.descriptor.AuthenticationDescriptorKey) FieldUtility(com.synopsys.integration.alert.common.persistence.accessor.FieldUtility) DefaultConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.database.api.DefaultConfigurationModelConfigurationAccessor) Test(org.junit.jupiter.api.Test)

Example 5 with FieldUtility

use of com.synopsys.integration.alert.common.persistence.accessor.FieldUtility in project hub-alert by blackducksoftware.

the class PolicyNotificationFilterCustomFunctionAction method createBlackDuckProperties.

private Optional<BlackDuckProperties> createBlackDuckProperties(FieldModel fieldModel) {
    FieldUtility fieldUtility = fieldModelConverter.convertToFieldAccessor(fieldModel);
    Long providerConfigId = fieldUtility.getLong(ProviderDescriptor.KEY_PROVIDER_CONFIG_ID).orElse(null);
    if (null == providerConfigId) {
        return Optional.empty();
    }
    return configurationModelConfigurationAccessor.getConfigurationById(providerConfigId).map(blackDuckPropertiesFactory::createProperties);
}
Also used : FieldUtility(com.synopsys.integration.alert.common.persistence.accessor.FieldUtility)

Aggregations

FieldUtility (com.synopsys.integration.alert.common.persistence.accessor.FieldUtility)25 Test (org.junit.jupiter.api.Test)15 ConfigurationFieldModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel)13 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)11 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)10 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)8 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)7 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)6 FieldModelTestAction (com.synopsys.integration.alert.common.action.FieldModelTestAction)4 EmailChannelMessagingService (com.synopsys.integration.alert.channel.email.distribution.EmailChannelMessagingService)3 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)3 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)3 AlertFieldStatus (com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus)3 AlertFieldException (com.synopsys.integration.alert.common.exception.AlertFieldException)3 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)3 DescriptorAccessor (com.synopsys.integration.alert.common.persistence.accessor.DescriptorAccessor)3 IntegrationException (com.synopsys.integration.exception.IntegrationException)3 ProviderProjectExistencePopulator (com.synopsys.integration.alert.api.provider.ProviderProjectExistencePopulator)2 AbstractJobResourceActions (com.synopsys.integration.alert.common.action.api.AbstractJobResourceActions)2 ConfigurationFieldModelConverter (com.synopsys.integration.alert.common.persistence.util.ConfigurationFieldModelConverter)2