Search in sources :

Example 1 with PolicyStoreDTO

use of org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO in project carbon-identity-framework by wso2.

the class AbstractPolicyFinderModule method getActivePolicies.

@Override
public String[] getActivePolicies() {
    log.debug("Start retrieving active policies at : " + new Date());
    List<String> policies = new ArrayList<String>();
    String[] policyIdentifiers = getOrderedPolicyIdentifiers();
    if (policyIdentifiers != null) {
        for (String identifier : policyIdentifiers) {
            if (!isPolicyDeActivationSupport()) {
                PolicyStoreDTO data = EntitlementAdminEngine.getInstance().getPolicyDataStore().getPolicyData(identifier);
                if (data != null && data.isActive()) {
                    String policy = getPolicy(identifier);
                    if (policy != null) {
                        policies.add(policy);
                    }
                }
            } else {
                String policy = getPolicy(identifier);
                if (policy != null) {
                    policies.add(policy);
                }
            }
        }
    }
    log.debug("Finish retrieving active policies at : " + new Date());
    return policies.toArray(new String[policies.size()]);
}
Also used : PolicyStoreDTO(org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 2 with PolicyStoreDTO

use of org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO in project carbon-identity-framework by wso2.

the class EntitlementUtil method addPolicyToPDP.

/**
 * @param policyStoreDTO
 * @return
 */
public static void addPolicyToPDP(PolicyStoreDTO policyStoreDTO) throws EntitlementException {
    Registry registry;
    String policyPath;
    Collection policyCollection;
    Resource resource;
    Map.Entry<PolicyStoreManageModule, Properties> entry = EntitlementServiceComponent.getEntitlementConfig().getPolicyStore().entrySet().iterator().next();
    String policyStorePath = entry.getValue().getProperty("policyStorePath");
    if (policyStorePath == null) {
        policyStorePath = "/repository/identity/entitlement/policy/pdp/";
    }
    if (policyStoreDTO == null || policyStoreDTO.getPolicy() == null || policyStoreDTO.getPolicy().trim().length() == 0 || policyStoreDTO.getPolicyId() == null || policyStoreDTO.getPolicyId().trim().length() == 0) {
        return;
    }
    try {
        registry = EntitlementServiceComponent.getRegistryService().getGovernanceSystemRegistry();
        if (registry.resourceExists(policyStorePath)) {
            policyCollection = (Collection) registry.get(policyStorePath);
        } else {
            policyCollection = registry.newCollection();
        }
        registry.put(policyStorePath, policyCollection);
        policyPath = policyStorePath + policyStoreDTO.getPolicyId();
        if (registry.resourceExists(policyPath)) {
            resource = registry.get(policyPath);
        } else {
            resource = registry.newResource();
        }
        resource.setProperty("policyOrder", Integer.toString(policyStoreDTO.getPolicyOrder()));
        resource.setContent(policyStoreDTO.getPolicy());
        resource.setMediaType("application/xacml-policy+xml");
        resource.setProperty("active", String.valueOf(policyStoreDTO.isActive()));
        AttributeDTO[] attributeDTOs = policyStoreDTO.getAttributeDTOs();
        if (attributeDTOs != null) {
            setAttributesAsProperties(attributeDTOs, resource);
        }
        registry.put(policyPath, resource);
        // Enable published policies in PDP
        PAPPolicyStoreManager storeManager = EntitlementAdminEngine.getInstance().getPapPolicyStoreManager();
        if (storeManager.isExistPolicy(policyStoreDTO.getPolicyId())) {
            PolicyPublisher publisher = EntitlementAdminEngine.getInstance().getPolicyPublisher();
            String[] subscribers = new String[] { EntitlementConstants.PDP_SUBSCRIBER_ID };
            if (policyStoreDTO.isActive()) {
                publisher.publishPolicy(new String[] { policyStoreDTO.getPolicyId() }, null, EntitlementConstants.PolicyPublish.ACTION_ENABLE, false, 0, subscribers, null);
            } else {
                publisher.publishPolicy(new String[] { policyStoreDTO.getPolicyId() }, null, EntitlementConstants.PolicyPublish.ACTION_DISABLE, false, 0, subscribers, null);
            }
        }
    } catch (RegistryException e) {
        log.error(e);
        throw new EntitlementException("Error while adding policy to PDP", e);
    }
}
Also used : PAPPolicyStoreManager(org.wso2.carbon.identity.entitlement.pap.store.PAPPolicyStoreManager) Resource(org.wso2.carbon.registry.core.Resource) Registry(org.wso2.carbon.registry.core.Registry) Properties(java.util.Properties) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) AttributeDTO(org.wso2.carbon.identity.entitlement.dto.AttributeDTO) PolicyStoreManageModule(org.wso2.carbon.identity.entitlement.policy.store.PolicyStoreManageModule) PolicyPublisher(org.wso2.carbon.identity.entitlement.policy.publisher.PolicyPublisher) Collection(org.wso2.carbon.registry.core.Collection) Map(java.util.Map)

Example 3 with PolicyStoreDTO

use of org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO in project carbon-identity-framework by wso2.

the class EntitlementUtil method addFilesystemPolicy.

/**
 * This method persists a new XACML policy, which was read from filesystem,
 * in the registry
 *
 * @param policyDTO PolicyDTO object
 * @param registry  Registry
 * @param promote   where policy must be promote PDP or not
 * @return returns whether True/False
 * @throws org.wso2.carbon.identity.entitlement.EntitlementException throws if policy with same id is exist
 */
public static boolean addFilesystemPolicy(PolicyDTO policyDTO, Registry registry, boolean promote) throws EntitlementException {
    PAPPolicyStoreManager policyAdmin;
    AbstractPolicy policyObj;
    if (policyDTO.getPolicy() != null) {
        policyDTO.setPolicy(policyDTO.getPolicy().replaceAll(">\\s+<", "><"));
    }
    policyObj = getPolicy(policyDTO.getPolicy());
    if (policyObj != null) {
        PAPPolicyStore policyStore = new PAPPolicyStore(registry);
        policyAdmin = new PAPPolicyStoreManager();
        policyDTO.setPolicyId(policyObj.getId().toASCIIString());
        policyDTO.setActive(true);
        if (isPolicyExists(policyDTO.getPolicyId(), registry)) {
            return false;
        }
        policyDTO.setPromote(promote);
        PolicyVersionManager versionManager = EntitlementAdminEngine.getInstance().getVersionManager();
        try {
            String version = versionManager.createVersion(policyDTO);
            policyDTO.setVersion(version);
        } catch (EntitlementException e) {
            log.error("Policy versioning is not supported", e);
        }
        policyAdmin.addOrUpdatePolicy(policyDTO);
        PAPPolicyStoreReader reader = new PAPPolicyStoreReader(policyStore);
        policyDTO = reader.readPolicyDTO(policyDTO.getPolicyId());
        if (Boolean.parseBoolean(System.getProperty(ENHANCED_XACML_LOADING_SYSTEM_PROPERTY)) && promote) {
            EntitlementAdminEngine adminEngine = EntitlementAdminEngine.getInstance();
            adminEngine.getPolicyStoreManager().addPolicy(policyDTO);
        } else {
            PolicyStoreDTO policyStoreDTO = new PolicyStoreDTO();
            policyStoreDTO.setPolicyId(policyDTO.getPolicyId());
            policyStoreDTO.setPolicy(policyDTO.getPolicy());
            policyStoreDTO.setPolicyOrder(policyDTO.getPolicyOrder());
            policyStoreDTO.setAttributeDTOs(policyDTO.getAttributeDTOs());
            policyStoreDTO.setActive(policyDTO.isActive());
            policyStoreDTO.setSetActive(policyDTO.isActive());
            if (promote) {
                addPolicyToPDP(policyStoreDTO);
            }
            policyAdmin.addOrUpdatePolicy(policyDTO);
        }
        return true;
    } else {
        throw new EntitlementException("Invalid Entitlement Policy");
    }
}
Also used : PolicyStoreDTO(org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO) PAPPolicyStoreManager(org.wso2.carbon.identity.entitlement.pap.store.PAPPolicyStoreManager) PolicyVersionManager(org.wso2.carbon.identity.entitlement.policy.version.PolicyVersionManager) AbstractPolicy(org.wso2.balana.AbstractPolicy) PAPPolicyStoreReader(org.wso2.carbon.identity.entitlement.pap.store.PAPPolicyStoreReader) EntitlementAdminEngine(org.wso2.carbon.identity.entitlement.pap.EntitlementAdminEngine) PAPPolicyStore(org.wso2.carbon.identity.entitlement.pap.store.PAPPolicyStore)

Example 4 with PolicyStoreDTO

use of org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO in project carbon-identity-framework by wso2.

the class DefaultPolicyDataStore method getPolicyData.

@Override
public PolicyStoreDTO getPolicyData(String policyId) {
    PolicyStoreDTO dataDTO = new PolicyStoreDTO();
    try {
        Registry registry = getGovernanceRegistry();
        String path = policyDataCollection + policyId;
        if (registry.resourceExists(path)) {
            Resource resource = registry.get(path);
            String order = resource.getProperty("order");
            String active = resource.getProperty("active");
            if (order != null && order.trim().length() > 0) {
                dataDTO.setPolicyOrder(Integer.parseInt(order));
            }
            dataDTO.setActive(Boolean.parseBoolean(active));
        }
    } catch (RegistryException e) {
        if (log.isDebugEnabled()) {
            log.debug(e);
        }
    } catch (EntitlementException e) {
        log.error("Error while getting policy data for policyId: " + policyId, e);
    }
    return dataDTO;
}
Also used : PolicyStoreDTO(org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO) EntitlementException(org.wso2.carbon.identity.entitlement.EntitlementException) Resource(org.wso2.carbon.registry.core.Resource) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 5 with PolicyStoreDTO

use of org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO in project carbon-identity-framework by wso2.

the class PolicyStoreManager method getLightPolicies.

public PolicyDTO[] getLightPolicies() {
    List<PolicyDTO> policyDTOs = new ArrayList<PolicyDTO>();
    String[] policies = policyStoreStore.getOrderedPolicyIdentifiers();
    if (policies != null) {
        for (String policy : policies) {
            PolicyDTO policyDTO = new PolicyDTO();
            policyDTO.setPolicyId(policy);
            PolicyStoreDTO storeDTO = policyDataStore.getPolicyData(policy);
            policyDTO.setActive(storeDTO.isActive());
            policyDTO.setPolicyOrder(storeDTO.getPolicyOrder());
            policyDTOs.add(policyDTO);
        }
    }
    return policyDTOs.toArray(new PolicyDTO[policyDTOs.size()]);
}
Also used : PolicyStoreDTO(org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO) PolicyDTO(org.wso2.carbon.identity.entitlement.dto.PolicyDTO) ArrayList(java.util.ArrayList)

Aggregations

PolicyStoreDTO (org.wso2.carbon.identity.entitlement.dto.PolicyStoreDTO)12 EntitlementException (org.wso2.carbon.identity.entitlement.EntitlementException)7 Registry (org.wso2.carbon.registry.core.Registry)5 Resource (org.wso2.carbon.registry.core.Resource)5 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)5 ArrayList (java.util.ArrayList)4 PolicyDTO (org.wso2.carbon.identity.entitlement.dto.PolicyDTO)3 Collection (org.wso2.carbon.registry.core.Collection)3 Date (java.util.Date)2 AttributeDTO (org.wso2.carbon.identity.entitlement.dto.AttributeDTO)2 PAPPolicyStoreManager (org.wso2.carbon.identity.entitlement.pap.store.PAPPolicyStoreManager)2 Map (java.util.Map)1 Properties (java.util.Properties)1 AbstractPolicy (org.wso2.balana.AbstractPolicy)1 PolicyOrderComparator (org.wso2.carbon.identity.entitlement.PolicyOrderComparator)1 EntitlementAdminEngine (org.wso2.carbon.identity.entitlement.pap.EntitlementAdminEngine)1 PAPPolicyStore (org.wso2.carbon.identity.entitlement.pap.store.PAPPolicyStore)1 PAPPolicyStoreReader (org.wso2.carbon.identity.entitlement.pap.store.PAPPolicyStoreReader)1 PolicyPublisher (org.wso2.carbon.identity.entitlement.policy.publisher.PolicyPublisher)1 PolicyStoreManageModule (org.wso2.carbon.identity.entitlement.policy.store.PolicyStoreManageModule)1