Search in sources :

Example 51 with PermissionKey

use of com.synopsys.integration.alert.common.persistence.model.PermissionKey in project hub-alert by blackducksoftware.

the class EmailGlobalConfigurationActionTest method testCreateForbidden.

@Test
public void testCreateForbidden() {
    AuthenticationTestUtils authenticationTestUtils = new AuthenticationTestUtils();
    DescriptorKey descriptorKey = ChannelKeys.EMAIL;
    PermissionKey permissionKey = new PermissionKey(ConfigContextEnum.GLOBAL.name(), descriptorKey.getUniversalKey());
    Map<PermissionKey, Integer> permissions = Map.of(permissionKey, AuthenticationTestUtils.NO_PERMISSIONS);
    AuthorizationManager authorizationManager = authenticationTestUtils.createAuthorizationManagerWithCurrentUserSet("admin", "admin", () -> new PermissionMatrixModel(permissions));
    EmailGlobalConfigurationValidator validator = new EmailGlobalConfigurationValidator();
    EmailGlobalConfigAccessor emailGlobalConfigAccessor = Mockito.mock(EmailGlobalConfigAccessor.class);
    EmailGlobalConfigModel model = new EmailGlobalConfigModel();
    model.setSmtpHost("host");
    model.setSmtpFrom("from");
    model.setSmtpAuth(true);
    model.setSmtpUsername("user");
    model.setSmtpPassword("password");
    EmailGlobalCrudActions configActions = new EmailGlobalCrudActions(authorizationManager, emailGlobalConfigAccessor, validator);
    ActionResponse<EmailGlobalConfigModel> response = configActions.create(model);
    assertEquals(HttpStatus.FORBIDDEN, response.getHttpStatus());
}
Also used : EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) EmailGlobalConfigurationValidator(com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) EmailGlobalConfigAccessor(com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor) AuthenticationTestUtils(com.synopsys.integration.alert.test.common.AuthenticationTestUtils) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Test(org.junit.jupiter.api.Test)

Example 52 with PermissionKey

use of com.synopsys.integration.alert.common.persistence.model.PermissionKey in project hub-alert by blackducksoftware.

the class EmailGlobalConfigurationActionTest method testUpdateForbidden.

@Test
public void testUpdateForbidden() {
    AuthenticationTestUtils authenticationTestUtils = new AuthenticationTestUtils();
    DescriptorKey descriptorKey = ChannelKeys.EMAIL;
    PermissionKey permissionKey = new PermissionKey(ConfigContextEnum.GLOBAL.name(), descriptorKey.getUniversalKey());
    Map<PermissionKey, Integer> permissions = Map.of(permissionKey, AuthenticationTestUtils.NO_PERMISSIONS);
    AuthorizationManager authorizationManager = authenticationTestUtils.createAuthorizationManagerWithCurrentUserSet("admin", "admin", () -> new PermissionMatrixModel(permissions));
    UUID configId = UUID.randomUUID();
    EmailGlobalConfigurationValidator validator = new EmailGlobalConfigurationValidator();
    EmailGlobalConfigAccessor emailGlobalConfigAccessor = Mockito.mock(EmailGlobalConfigAccessor.class);
    EmailGlobalConfigModel model = new EmailGlobalConfigModel();
    model.setSmtpHost("host");
    model.setSmtpFrom("from");
    model.setSmtpAuth(true);
    model.setSmtpUsername("user");
    model.setSmtpPassword("password");
    EmailGlobalCrudActions configActions = new EmailGlobalCrudActions(authorizationManager, emailGlobalConfigAccessor, validator);
    ActionResponse<EmailGlobalConfigModel> response = configActions.update(model);
    assertEquals(HttpStatus.FORBIDDEN, response.getHttpStatus());
}
Also used : EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) EmailGlobalConfigurationValidator(com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) EmailGlobalConfigAccessor(com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor) AuthenticationTestUtils(com.synopsys.integration.alert.test.common.AuthenticationTestUtils) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 53 with PermissionKey

use of com.synopsys.integration.alert.common.persistence.model.PermissionKey in project hub-alert by blackducksoftware.

the class JiraServerGlobalCrudActionsTestIT method init.

@BeforeEach
public void init() {
    AuthenticationTestUtils authenticationTestUtils = new AuthenticationTestUtils();
    DescriptorKey descriptorKey = ChannelKeys.JIRA_SERVER;
    PermissionKey permissionKey = new PermissionKey(ConfigContextEnum.GLOBAL.name(), descriptorKey.getUniversalKey());
    Map<PermissionKey, Integer> permissions = Map.of(permissionKey, AuthenticationTestUtils.FULL_PERMISSIONS);
    authorizationManager = authenticationTestUtils.createAuthorizationManagerWithCurrentUserSet("admin", "admin", () -> new PermissionMatrixModel(permissions));
}
Also used : PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) AuthenticationTestUtils(com.synopsys.integration.alert.test.common.AuthenticationTestUtils) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 54 with PermissionKey

use of com.synopsys.integration.alert.common.persistence.model.PermissionKey in project hub-alert by blackducksoftware.

the class AuthorizationManager method currentUserHasPermission.

private boolean currentUserHasPermission(AccessOperation operation, String context, String descriptorKey) {
    PermissionKey permissionKey = new PermissionKey(context, descriptorKey);
    Collection<String> roleNames = getCurrentUserRoleNames();
    boolean hasPermission = roleNames.stream().anyMatch(name -> permissionCache.containsKey(name) && permissionCache.get(name).hasPermission(permissionKey, operation));
    if (!hasPermission) {
        logger.debug(String.format("User %s does not have permission: %s", getCurrentUserName().orElse("UNKNOWN"), operation.name()));
    }
    return hasPermission;
}
Also used : PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey)

Example 55 with PermissionKey

use of com.synopsys.integration.alert.common.persistence.model.PermissionKey in project hub-alert by blackducksoftware.

the class AuthorizationManager method getOperations.

public final Set<Integer> getOperations(ConfigContextEnum context, DescriptorKey descriptorKey) {
    PermissionKey permissionKey = new PermissionKey(context.name(), descriptorKey.getUniversalKey());
    Collection<String> roleNames = getCurrentUserRoleNames();
    return roleNames.stream().filter(permissionCache::containsKey).map(permissionCache::get).map(object -> object.getOperations(permissionKey)).collect(Collectors.toSet());
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) HashSet(java.util.HashSet) AlertForbiddenOperationException(com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) Map(java.util.Map) UserDetails(org.springframework.security.core.userdetails.UserDetails) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Logger(org.slf4j.Logger) AccessOperation(com.synopsys.integration.alert.common.enumeration.AccessOperation) Collection(java.util.Collection) Set(java.util.Set) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) Collectors(java.util.stream.Collectors) GrantedAuthority(org.springframework.security.core.GrantedAuthority) RoleAccessor(com.synopsys.integration.alert.common.descriptor.accessor.RoleAccessor) Component(org.springframework.stereotype.Component) Optional(java.util.Optional) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Authentication(org.springframework.security.core.Authentication) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey)

Aggregations

PermissionKey (com.synopsys.integration.alert.common.persistence.model.PermissionKey)56 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)49 AuthenticationTestUtils (com.synopsys.integration.alert.test.common.AuthenticationTestUtils)43 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)42 Test (org.junit.jupiter.api.Test)38 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)35 ChannelKey (com.synopsys.integration.alert.descriptor.api.model.ChannelKey)23 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)16 EmailGlobalConfigurationValidator (com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator)12 EmailGlobalConfigModel (com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel)12 EmailGlobalConfigAccessor (com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor)10 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)9 ValidationResponseModel (com.synopsys.integration.alert.common.rest.model.ValidationResponseModel)6 PermissionMatrixRelation (com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation)5 ConfigContextEntity (com.synopsys.integration.alert.database.configuration.ConfigContextEntity)5 RegisteredDescriptorEntity (com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity)5 HashMap (java.util.HashMap)4 UUID (java.util.UUID)4 RoleEntity (com.synopsys.integration.alert.database.user.RoleEntity)3 Map (java.util.Map)3