Search in sources :

Example 16 with ConfigContextEntity

use of com.synopsys.integration.alert.database.configuration.ConfigContextEntity in project hub-alert by blackducksoftware.

the class DefaultRoleAccessor method updateRoleOperations.

private List<PermissionMatrixRelation> updateRoleOperations(RoleEntity roleEntity, PermissionMatrixModel permissionMatrix) {
    List<PermissionMatrixRelation> oldPermissionsForRole = permissionMatrixRepository.findAllByRoleId(roleEntity.getId());
    if (!oldPermissionsForRole.isEmpty()) {
        permissionMatrixRepository.deleteAll(oldPermissionsForRole);
    }
    List<PermissionMatrixRelation> matrixEntries = new ArrayList<>();
    Map<PermissionKey, Integer> permissions = permissionMatrix.getPermissions();
    for (Map.Entry<PermissionKey, Integer> permission : permissions.entrySet()) {
        PermissionKey permissionKey = permission.getKey();
        ConfigContextEntity dbContext = configContextRepository.findFirstByContext(permissionKey.getContext()).orElseThrow(() -> new AlertRuntimeException("Invalid context specified for permission"));
        RegisteredDescriptorEntity registeredDescriptor = registeredDescriptorRepository.findFirstByName(permissionKey.getDescriptorName()).orElseThrow(() -> new AlertRuntimeException("Invalid descriptor name specified for permission"));
        int accessOperations = permission.getValue();
        PermissionMatrixRelation permissionMatrixRelation = new PermissionMatrixRelation(roleEntity.getId(), dbContext.getId(), registeredDescriptor.getId(), accessOperations);
        matrixEntries.add(permissionMatrixRelation);
    }
    if (!matrixEntries.isEmpty()) {
        return permissionMatrixRepository.saveAll(matrixEntries);
    }
    return List.of();
}
Also used : PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) ArrayList(java.util.ArrayList) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) PermissionMatrixRelation(com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation) AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException) HashMap(java.util.HashMap) Map(java.util.Map) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity)

Example 17 with ConfigContextEntity

use of com.synopsys.integration.alert.database.configuration.ConfigContextEntity in project hub-alert by blackducksoftware.

the class DefaultRoleAccessorTest method testSuperSetRoles.

@Test
public void testSuperSetRoles() {
    RoleRepository roleRepository = Mockito.mock(RoleRepository.class);
    UserRoleRepository userRoleRepository = Mockito.mock(UserRoleRepository.class);
    PermissionMatrixRepository permissionMatrixRepository = Mockito.mock(PermissionMatrixRepository.class);
    RegisteredDescriptorRepository registeredDescriptorRepository = Mockito.mock(RegisteredDescriptorRepository.class);
    ConfigContextRepository configContextRepository = Mockito.mock(ConfigContextRepository.class);
    RoleEntity adminRole = new RoleEntity(DefaultUserRole.ALERT_ADMIN.name(), true);
    adminRole.setId(1L);
    RoleEntity userRole = new RoleEntity(DefaultUserRole.ALERT_USER.name(), true);
    userRole.setId(2L);
    Mockito.when(roleRepository.findRoleEntitiesByRoleNames(Mockito.anyCollection())).thenReturn(List.of(adminRole, userRole));
    Long contextId = 1L;
    String contextString = "PERMISSION";
    ConfigContextEntity contextEntity = new ConfigContextEntity(contextString);
    contextEntity.setId(contextId);
    Mockito.when(configContextRepository.findById(Mockito.eq(contextEntity.getId()))).thenReturn(Optional.of(contextEntity));
    Long descriptorId_1 = 1L;
    String descriptorName_1 = "key.1";
    Long descriptorId_2 = 2L;
    String descriptorName_2 = "key.2";
    Long descriptorId_3 = 3L;
    String descriptorName_3 = "key.3";
    RegisteredDescriptorEntity registeredDescriptorEntity_1 = new RegisteredDescriptorEntity(descriptorName_1, 1L);
    registeredDescriptorEntity_1.setId(descriptorId_1);
    Mockito.when(registeredDescriptorRepository.findById(Mockito.eq(registeredDescriptorEntity_1.getId()))).thenReturn(Optional.of(registeredDescriptorEntity_1));
    RegisteredDescriptorEntity registeredDescriptorEntity_2 = new RegisteredDescriptorEntity(descriptorName_2, 1L);
    registeredDescriptorEntity_2.setId(descriptorId_2);
    Mockito.when(registeredDescriptorRepository.findById(Mockito.eq(registeredDescriptorEntity_2.getId()))).thenReturn(Optional.of(registeredDescriptorEntity_2));
    RegisteredDescriptorEntity registeredDescriptorEntity_3 = new RegisteredDescriptorEntity(descriptorName_3, 1L);
    registeredDescriptorEntity_3.setId(descriptorId_3);
    Mockito.when(registeredDescriptorRepository.findById(Mockito.eq(registeredDescriptorEntity_3.getId()))).thenReturn(Optional.of(registeredDescriptorEntity_3));
    PermissionKey permission_1 = new PermissionKey(contextString, descriptorName_1);
    PermissionKey permission_2 = new PermissionKey(contextString, descriptorName_2);
    PermissionKey permission_3 = new PermissionKey(contextString, descriptorName_3);
    PermissionMatrixRelation adminRelation_1 = new PermissionMatrixRelation(adminRole.getId(), contextEntity.getId(), registeredDescriptorEntity_1.getId(), AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit());
    PermissionMatrixRelation adminRelation_3 = new PermissionMatrixRelation(adminRole.getId(), contextEntity.getId(), registeredDescriptorEntity_3.getId(), AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit());
    PermissionMatrixRelation userRelation_1 = new PermissionMatrixRelation(userRole.getId(), contextEntity.getId(), registeredDescriptorEntity_1.getId(), AccessOperation.READ.getBit());
    PermissionMatrixRelation userRelation_2 = new PermissionMatrixRelation(userRole.getId(), contextEntity.getId(), registeredDescriptorEntity_2.getId(), AccessOperation.READ.getBit() + AccessOperation.EXECUTE.getBit());
    List<Long> roleIds = List.of(adminRole.getId(), userRole.getId());
    Mockito.when(permissionMatrixRepository.findAllByRoleId(Mockito.eq(adminRole.getId()))).thenReturn(List.of(adminRelation_1, adminRelation_3));
    Mockito.when(permissionMatrixRepository.findAllByRoleId(Mockito.eq(userRole.getId()))).thenReturn(List.of(userRelation_1, userRelation_2));
    Mockito.when(permissionMatrixRepository.findAllByRoleIdIn(Mockito.eq(roleIds))).thenReturn(List.of(adminRelation_1, adminRelation_3, userRelation_1, userRelation_2));
    DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
    // order matters here.  The userRole has less privileges so we want to test that the more restrictive privileges don't overwrite the admin privileges.  We want a union of the permissions
    List<String> roles = List.of(adminRole.getRoleName(), userRole.getRoleName());
    PermissionMatrixModel matrixModel = authorizationUtility.mergePermissionsForRoles(roles);
    // admin read/write
    assertTrue(matrixModel.hasPermission(permission_1, AccessOperation.READ));
    assertTrue(matrixModel.hasPermission(permission_1, AccessOperation.WRITE));
    assertFalse(matrixModel.hasPermission(permission_1, AccessOperation.EXECUTE));
    // user read/execute
    assertTrue(matrixModel.hasPermission(permission_2, AccessOperation.READ));
    assertFalse(matrixModel.hasPermission(permission_2, AccessOperation.WRITE));
    assertTrue(matrixModel.hasPermission(permission_2, AccessOperation.EXECUTE));
    // admin read/write
    assertTrue(matrixModel.hasPermission(permission_3, AccessOperation.READ));
    assertTrue(matrixModel.hasPermission(permission_3, AccessOperation.WRITE));
    assertFalse(matrixModel.hasPermission(permission_3, AccessOperation.EXECUTE));
}
Also used : RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) PermissionMatrixRepository(com.synopsys.integration.alert.database.authorization.PermissionMatrixRepository) UserRoleRepository(com.synopsys.integration.alert.database.user.UserRoleRepository) RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) PermissionMatrixRelation(com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation) ConfigContextRepository(com.synopsys.integration.alert.database.configuration.repository.ConfigContextRepository) RegisteredDescriptorRepository(com.synopsys.integration.alert.database.configuration.repository.RegisteredDescriptorRepository) RoleRepository(com.synopsys.integration.alert.database.user.RoleRepository) UserRoleRepository(com.synopsys.integration.alert.database.user.UserRoleRepository) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) Test(org.junit.jupiter.api.Test)

Example 18 with ConfigContextEntity

use of com.synopsys.integration.alert.database.configuration.ConfigContextEntity in project hub-alert by blackducksoftware.

the class DefaultConfigurationModelConfigurationAccessorTest method getProviderConfigurationByNameTest.

@Test
public void getProviderConfigurationByNameTest() {
    final String providerConfigName = "provider-config-name-test";
    final String emptyProviderConfigName = "bad-config-name";
    final Long fieldId = 1L;
    final Long descriptorId = 4L;
    final Long configurationId = 6L;
    DefinedFieldEntity definedFieldEntity = new DefinedFieldEntity(fieldKey, false);
    definedFieldEntity.setId(fieldId);
    FieldValueEntity fieldValueEntity = new FieldValueEntity(2L, 3L, fieldValue);
    DescriptorConfigEntity descriptorConfigEntity = new DescriptorConfigEntity(descriptorId, 5L, DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp());
    descriptorConfigEntity.setId(configurationId);
    ConfigContextEntity configContextEntity = new ConfigContextEntity(configContextEnum.name());
    Mockito.when(definedFieldRepository.findFirstByKey(Mockito.any())).thenReturn(Optional.of(definedFieldEntity));
    Mockito.when(fieldValueRepository.findAllByFieldIdAndValue(fieldId, providerConfigName)).thenReturn(List.of(fieldValueEntity));
    Mockito.when(fieldValueRepository.findAllByFieldIdAndValue(fieldId, emptyProviderConfigName)).thenReturn(List.of());
    setupGetJobMocks(descriptorConfigEntity, configContextEntity, fieldValueEntity, definedFieldEntity);
    DefaultConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = new DefaultConfigurationModelConfigurationAccessor(null, definedFieldRepository, descriptorConfigRepository, configContextRepository, fieldValueRepository, encryptionUtility);
    Optional<ConfigurationModel> configurationModelOptional = configurationModelConfigurationAccessor.getProviderConfigurationByName(providerConfigName);
    Optional<ConfigurationModel> configurationModelProviderConfigsEmpty = configurationModelConfigurationAccessor.getProviderConfigurationByName(emptyProviderConfigName);
    assertTrue(configurationModelOptional.isPresent());
    assertFalse(configurationModelProviderConfigsEmpty.isPresent());
    ConfigurationModel configurationModel = configurationModelOptional.get();
    testConfigurationModel(configurationId, descriptorId, configurationModel);
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) DescriptorConfigEntity(com.synopsys.integration.alert.database.configuration.DescriptorConfigEntity) DefinedFieldEntity(com.synopsys.integration.alert.database.configuration.DefinedFieldEntity) FieldValueEntity(com.synopsys.integration.alert.database.configuration.FieldValueEntity) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) Test(org.junit.jupiter.api.Test)

Example 19 with ConfigContextEntity

use of com.synopsys.integration.alert.database.configuration.ConfigContextEntity in project hub-alert by blackducksoftware.

the class DescriptorConfigRepositoryTestIT method findByDescriptorIdTest.

@Test
public void findByDescriptorIdTest() {
    RegisteredDescriptorEntity descriptorEntity1 = new RegisteredDescriptorEntity(DESCRIPTOR_NAME, 1L);
    RegisteredDescriptorEntity descriptorEntity2 = new RegisteredDescriptorEntity(DESCRIPTOR_NAME + "Alt", 1L);
    RegisteredDescriptorEntity savedDescriptorEntity1 = registeredDescriptorRepository.save(descriptorEntity1);
    RegisteredDescriptorEntity savedDescriptorEntity2 = registeredDescriptorRepository.save(descriptorEntity2);
    ConfigContextEntity configContextEntity = new ConfigContextEntity(CONTEXT_NAME);
    ConfigContextEntity savedContextEntity = configContextRepository.save(configContextEntity);
    OffsetDateTime currentTime = DateUtils.createCurrentDateTimestamp();
    DescriptorConfigEntity descriptorConfigEntity1 = new DescriptorConfigEntity(savedDescriptorEntity1.getId(), savedContextEntity.getId(), currentTime, currentTime);
    DescriptorConfigEntity descriptorConfigEntity2 = new DescriptorConfigEntity(savedDescriptorEntity1.getId(), savedContextEntity.getId(), currentTime, currentTime);
    DescriptorConfigEntity descriptorConfigEntity3 = new DescriptorConfigEntity(savedDescriptorEntity2.getId(), savedContextEntity.getId(), currentTime, currentTime);
    descriptorConfigRepository.save(descriptorConfigEntity1);
    descriptorConfigRepository.save(descriptorConfigEntity2);
    descriptorConfigRepository.save(descriptorConfigEntity3);
    List<DescriptorConfigEntity> descriptorConfig1List = descriptorConfigRepository.findByDescriptorId(savedDescriptorEntity1.getId());
    assertEquals(2, descriptorConfig1List.size());
    List<DescriptorConfigEntity> descriptorConfig2List = descriptorConfigRepository.findByDescriptorId(savedDescriptorEntity2.getId());
    assertEquals(1, descriptorConfig2List.size());
}
Also used : OffsetDateTime(java.time.OffsetDateTime) DescriptorConfigEntity(com.synopsys.integration.alert.database.configuration.DescriptorConfigEntity) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 20 with ConfigContextEntity

use of com.synopsys.integration.alert.database.configuration.ConfigContextEntity in project hub-alert by blackducksoftware.

the class FieldContextRepositoryTestIT method findByFieldId.

@Test
public void findByFieldId() {
    DefinedFieldEntity definedFieldEntity1 = new DefinedFieldEntity(FIELD_KEY_1, Boolean.FALSE);
    DefinedFieldEntity definedFieldEntity2 = new DefinedFieldEntity(FIELD_KEY_2, Boolean.FALSE);
    DefinedFieldEntity savedFieldEntity1 = definedFieldRepository.save(definedFieldEntity1);
    DefinedFieldEntity savedFieldEntity2 = definedFieldRepository.save(definedFieldEntity2);
    assertEquals(2, definedFieldRepository.findAll().size());
    ConfigContextEntity configContextEntity1 = new ConfigContextEntity(CONTEXT_NAME);
    ConfigContextEntity configContextEntity2 = new ConfigContextEntity(CONTEXT_NAME + "2");
    ConfigContextEntity savedContextEntity1 = configContextRepository.save(configContextEntity1);
    ConfigContextEntity savedContextEntity2 = configContextRepository.save(configContextEntity2);
    assertEquals(2, configContextRepository.findAll().size());
    FieldContextRelation fieldContextRelation1 = new FieldContextRelation(savedFieldEntity1.getId(), savedContextEntity1.getId());
    FieldContextRelation fieldContextRelation2 = new FieldContextRelation(savedFieldEntity1.getId(), savedContextEntity2.getId());
    FieldContextRelation fieldContextRelation3 = new FieldContextRelation(savedFieldEntity2.getId(), savedContextEntity1.getId());
    fieldContextRepository.save(fieldContextRelation1);
    fieldContextRepository.save(fieldContextRelation2);
    fieldContextRepository.save(fieldContextRelation3);
    assertEquals(3, fieldContextRepository.findAll().size());
    List<FieldContextRelation> foundList1 = fieldContextRepository.findByFieldId(savedFieldEntity1.getId());
    assertEquals(2, foundList1.size());
    List<FieldContextRelation> foundList2 = fieldContextRepository.findByFieldId(savedFieldEntity2.getId());
    assertEquals(1, foundList2.size());
}
Also used : FieldContextRelation(com.synopsys.integration.alert.database.configuration.FieldContextRelation) DefinedFieldEntity(com.synopsys.integration.alert.database.configuration.DefinedFieldEntity) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigContextEntity (com.synopsys.integration.alert.database.configuration.ConfigContextEntity)25 Test (org.junit.jupiter.api.Test)20 RegisteredDescriptorEntity (com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity)17 DefinedFieldEntity (com.synopsys.integration.alert.database.configuration.DefinedFieldEntity)14 DescriptorConfigEntity (com.synopsys.integration.alert.database.configuration.DescriptorConfigEntity)13 FieldValueEntity (com.synopsys.integration.alert.database.configuration.FieldValueEntity)8 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)8 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)7 PermissionKey (com.synopsys.integration.alert.common.persistence.model.PermissionKey)5 PermissionMatrixRelation (com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation)5 OffsetDateTime (java.time.OffsetDateTime)5 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)4 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)4 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)3 DescriptorFieldRelation (com.synopsys.integration.alert.database.configuration.DescriptorFieldRelation)3 FieldContextRelation (com.synopsys.integration.alert.database.configuration.FieldContextRelation)3 ConfigContextRepository (com.synopsys.integration.alert.database.configuration.repository.ConfigContextRepository)3 RegisteredDescriptorRepository (com.synopsys.integration.alert.database.configuration.repository.RegisteredDescriptorRepository)3 RoleEntity (com.synopsys.integration.alert.database.user.RoleEntity)3 ArrayList (java.util.ArrayList)3