Search in sources :

Example 11 with ValueWrapper

use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.

the class DefaultEntityEventManager method addEventCache.

/**
 * Include event in transaction processing.
 *
 * @param eventId
 * @param transactionId
 */
@SuppressWarnings("unchecked")
private void addEventCache(UUID eventId, UUID transactionId) {
    Assert.notNull(eventId, "Event has to be asynchronous (~persisted).");
    LOG.trace("Add event [{}] into cache under transaction [{}].", eventId, transactionId);
    // 
    lock.lock();
    try {
        if (transactionId == null) {
            return;
        }
        // 
        ValueWrapper value = cacheManager.getValue(TRANSACTION_EVENT_CACHE_NAME, transactionId);
        // 
        Set<UUID> events = null;
        if (value == null) {
            events = new HashSet<>();
        } else {
            events = (Set<UUID>) value.get();
        }
        events.add(eventId);
        cacheManager.cacheValue(TRANSACTION_EVENT_CACHE_NAME, transactionId, events);
    } finally {
        lock.unlock();
    }
}
Also used : ValueWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper) UUID(java.util.UUID)

Example 12 with ValueWrapper

use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.

the class DefaultConfigurationService method getValue.

@Override
@Transactional(readOnly = true)
public String getValue(String key, String defaultValue) {
    ValueWrapper cachedValue = getCachedValue(key);
    if (cachedValue != null) {
        return (String) cachedValue.get();
    }
    // 
    LOG.debug("Reading configuration for key [{}]", key);
    String value = null;
    boolean confidential = true;
    // IdM configuration has higher priority than property file.
    IdmConfigurationDto config = getByCode(key);
    if (config != null) {
        if (config.isConfidential()) {
            value = confidentialStorage.get(config.getId(), getEntityClass(), CONFIDENTIAL_PROPERTY_VALUE, String.class);
            LOG.debug("Configuration value for key [{}] was found in confidential storage", config.getName());
        } else {
            value = config.getValue();
            confidential = false;
            LOG.trace("Configuration value for key [{}] was found in database.", key);
        }
    } else if (env != null) {
        // try to find value in property configuration
        value = env.getProperty(key);
        confidential = GuardedString.shouldBeGuarded(key);
    }
    // fill default value
    if (value == null) {
        // TODO: null vs. isEmpty?
        value = defaultValue;
    }
    LOG.debug("Resolved configuration value for key [{}] is [{}].", key, confidential ? GuardedString.SECRED_PROXY_STRING : value);
    setCachedValue(key, value);
    return value;
}
Also used : ValueWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper) IdmConfigurationDto(eu.bcvsolutions.idm.core.api.dto.IdmConfigurationDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with ValueWrapper

use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.

the class DefaultIdmRoleCompositionService method findAllSubRoles.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<IdmRoleCompositionDto> findAllSubRoles(UUID superiorId, BasePermission... permission) {
    Assert.notNull(superiorId, "Superior role identifier is required.");
    // 
    ValueWrapper value = cacheManager.getValue(ALL_SUB_ROLES_CACHE_NAME, superiorId);
    if (value != null) {
        // never null
        return (List) value.get();
    }
    // 
    List<IdmRoleCompositionDto> results = new ArrayList<>();
    findAllSubRoles(results, new ArrayList<>(), superiorId, permission);
    cacheManager.cacheValue(ALL_SUB_ROLES_CACHE_NAME, superiorId, results);
    // 
    return results;
}
Also used : ValueWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper) IdmRoleCompositionDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleCompositionDto) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 14 with ValueWrapper

use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.

the class DefaultAuthorizationManagerIntegrationTest method testCache.

@Test
@Transactional
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCache() {
    // create and login identity
    IdmIdentityDto identity = getHelper().createIdentity();
    UUID mockIdentity = UUID.randomUUID();
    // prepare role
    IdmRoleDto role = getHelper().createRole();
    IdmAuthorizationPolicyDto policy = getHelper().createBasePolicy(role.getId(), IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ);
    getHelper().createIdentityRole(identity, role);
    // 
    Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, identity.getId()));
    Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
    Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
    Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
    Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
    // 
    cacheManager.cacheValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity, new HashMap<>());
    cacheManager.cacheValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity, new HashMap<>());
    Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
    Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
    // 
    // without login
    Set<String> permissions = manager.getPermissions(role);
    Assert.assertTrue(permissions.isEmpty());
    // 
    try {
        getHelper().login(identity);
        // 
        // new entity is not supported with cache, but permissions are evaluated
        permissions = manager.getPermissions(new IdmRoleDto());
        Assert.assertEquals(2, permissions.size());
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
        Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
        // 
        // load from db
        permissions = manager.getPermissions(role);
        Assert.assertEquals(2, permissions.size());
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
        Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
        Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
        // load from cache
        permissions = manager.getPermissions(role);
        Assert.assertEquals(2, permissions.size());
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
        Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, policy.getId()));
        Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
        Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
        // check cache content - one
        ValueWrapper cacheValue = cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId());
        List<IdmAuthorizationPolicyDto> cachedPolicies = (List) ((Map) cacheValue.get()).get(role.getClass());
        Assert.assertEquals(1, cachedPolicies.size());
        Assert.assertEquals(BasePermissionEvaluator.class.getCanonicalName(), ((IdmAuthorizationPolicyDto) cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, cachedPolicies.get(0)).get()).getEvaluatorType());
        cacheValue = cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId());
        permissions = (Set) ((Map) cacheValue.get()).get(role.getId());
        Assert.assertEquals(2, permissions.size());
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
        // 
        // change policy => evict whole cache
        policy.setPermissions(IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ, IdmBasePermission.UPDATE);
        authorizationPolicyService.save(policy);
        Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, policy.getId()));
        Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
        Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
        Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
        Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
        // 
        cacheManager.cacheValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity, new HashMap<>());
        cacheManager.cacheValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity, new HashMap<>());
        permissions = manager.getPermissions(role);
        Assert.assertEquals(3, permissions.size());
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
        Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.UPDATE.getName())));
        Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
        Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
    } finally {
        // evict logged identity cache only
        logout();
    }
    // check cache is evicted only for logged identity
    Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
    Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
    Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
    Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
}
Also used : IdmIdentityRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ValueWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper) CoreGroupPermission(eu.bcvsolutions.idm.core.model.domain.CoreGroupPermission) IdmBasePermission(eu.bcvsolutions.idm.core.security.api.domain.IdmBasePermission) Map(java.util.Map) UuidEvaluator(eu.bcvsolutions.idm.core.security.evaluator.UuidEvaluator) IdmIdentityContractDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto) IdmRoleFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmRoleFilter) BasePermissionEvaluator(eu.bcvsolutions.idm.core.security.evaluator.BasePermissionEvaluator) Before(org.junit.Before) AuthorizationManager(eu.bcvsolutions.idm.core.security.api.service.AuthorizationManager) IdmIdentityRoleService(eu.bcvsolutions.idm.core.api.service.IdmIdentityRoleService) IdmIdentity(eu.bcvsolutions.idm.core.model.entity.IdmIdentity) IdmIdentityContractService(eu.bcvsolutions.idm.core.api.service.IdmIdentityContractService) Assert.assertNotNull(org.junit.Assert.assertNotNull) ContractState(eu.bcvsolutions.idm.core.api.domain.ContractState) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmCacheManager(eu.bcvsolutions.idm.core.api.service.IdmCacheManager) IdmRoleService(eu.bcvsolutions.idm.core.api.service.IdmRoleService) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) UUID(java.util.UUID) RoleConfiguration(eu.bcvsolutions.idm.core.api.config.domain.RoleConfiguration) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) List(java.util.List) AuthorizationEvaluatorDto(eu.bcvsolutions.idm.core.security.api.dto.AuthorizationEvaluatorDto) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) Assert.assertFalse(org.junit.Assert.assertFalse) IdmAuthorizationPolicyDto(eu.bcvsolutions.idm.core.api.dto.IdmAuthorizationPolicyDto) LocalDate(java.time.LocalDate) AbstractEvaluatorIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractEvaluatorIntegrationTest) IdmAuthorizationPolicyService(eu.bcvsolutions.idm.core.api.service.IdmAuthorizationPolicyService) IdmIdentityService(eu.bcvsolutions.idm.core.api.service.IdmIdentityService) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) AuthorizableType(eu.bcvsolutions.idm.core.security.api.dto.AuthorizableType) Transactional(org.springframework.transaction.annotation.Transactional) IdmRole(eu.bcvsolutions.idm.core.model.entity.IdmRole) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) ValueWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper) IdmAuthorizationPolicyDto(eu.bcvsolutions.idm.core.api.dto.IdmAuthorizationPolicyDto) BasePermissionEvaluator(eu.bcvsolutions.idm.core.security.evaluator.BasePermissionEvaluator) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) List(java.util.List) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) Test(org.junit.Test) AbstractEvaluatorIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractEvaluatorIntegrationTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with ValueWrapper

use of eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper in project CzechIdMng by bcvsolutions.

the class DefaultFormService method getCachedDefinition.

private IdmFormDefinitionDto getCachedDefinition(UUID definitionId) {
    String cacheKey = getCacheKey(definitionId);
    ValueWrapper value = cacheManager.getValue(FORM_DEFINITION_CACHE_NAME, cacheKey);
    if (value != null) {
        // never null
        return ((FormDefinitionCache) value.get()).getById(definitionId);
    }
    // 
    IdmFormDefinitionDto definition = formDefinitionService.get(definitionId);
    if (definition == null) {
        // definition not found => not cached
        return null;
    }
    FormDefinitionCache cachedDefinitions = new FormDefinitionCache();
    cachedDefinitions.putDefinition(definition);
    cacheManager.cacheValue(FORM_DEFINITION_CACHE_NAME, cacheKey, cachedDefinitions);
    // 
    return cachedDefinitions.getById(definitionId);
}
Also used : FormDefinitionCache(eu.bcvsolutions.idm.core.eav.api.domain.FormDefinitionCache) ValueWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)

Aggregations

ValueWrapper (eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper)19 UUID (java.util.UUID)7 Set (java.util.Set)5 IdmAuthorizationPolicyDto (eu.bcvsolutions.idm.core.api.dto.IdmAuthorizationPolicyDto)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 IdmAuthorizationPolicyService (eu.bcvsolutions.idm.core.api.service.IdmAuthorizationPolicyService)3 IdmCacheManager (eu.bcvsolutions.idm.core.api.service.IdmCacheManager)3 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)3 AuthorizableType (eu.bcvsolutions.idm.core.security.api.dto.AuthorizableType)3 AuthorizationEvaluatorDto (eu.bcvsolutions.idm.core.security.api.dto.AuthorizationEvaluatorDto)3 AuthorizationManager (eu.bcvsolutions.idm.core.security.api.service.AuthorizationManager)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 RoleConfiguration (eu.bcvsolutions.idm.core.api.config.domain.RoleConfiguration)2 ConfigurationMap (eu.bcvsolutions.idm.core.api.domain.ConfigurationMap)2 ContractState (eu.bcvsolutions.idm.core.api.domain.ContractState)2 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)2 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)2 IdmIdentityRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto)2