From 53fb513c482a5604c264978e56cfe74292327c27 Mon Sep 17 00:00:00 2001 From: Farasath Ahamed Date: Sat, 19 Aug 2017 22:36:13 +0530 Subject: [PATCH] IDENTITY-4663: Remove incorrect media type set for policy data By removing incorrect media type set for policy data collection at /_system/governance/repository/identity/entitlement/policy/data we can make sure they will not be indexed by registry. --- .../policy/store/DefaultPolicyDataStore.java | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/components/entitlement/org.wso2.carbon.identity.entitlement/src/main/java/org/wso2/carbon/identity/entitlement/policy/store/DefaultPolicyDataStore.java b/components/entitlement/org.wso2.carbon.identity.entitlement/src/main/java/org/wso2/carbon/identity/entitlement/policy/store/DefaultPolicyDataStore.java index a1150156ef6e..c502c187164c 100644 --- a/components/entitlement/org.wso2.carbon.identity.entitlement/src/main/java/org/wso2/carbon/identity/entitlement/policy/store/DefaultPolicyDataStore.java +++ b/components/entitlement/org.wso2.carbon.identity.entitlement/src/main/java/org/wso2/carbon/identity/entitlement/policy/store/DefaultPolicyDataStore.java @@ -58,11 +58,10 @@ public void init(Properties properties) throws EntitlementException { @Override public PolicyCombiningAlgorithm getGlobalPolicyAlgorithm() { - Registry registry = EntitlementServiceComponent. - getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + String algorithm = null; try { - + Registry registry = getGovernanceRegistry(); if (registry.resourceExists(policyDataCollection)) { Collection collection = (Collection) registry.get(policyDataCollection); algorithm = collection.getProperty("globalPolicyCombiningAlgorithm"); @@ -89,13 +88,9 @@ public PolicyCombiningAlgorithm getGlobalPolicyAlgorithm() { return EntitlementUtil.getPolicyCombiningAlgorithm(algorithm); } - } catch (RegistryException e) { - if (log.isDebugEnabled()) { - log.debug(e); - } - } catch (EntitlementException e) { + } catch (RegistryException | EntitlementException e) { if (log.isDebugEnabled()) { - log.debug(e); + log.debug("Exception while getting Global Policy Algorithm from policy data store.", e); } } @@ -106,8 +101,7 @@ public PolicyCombiningAlgorithm getGlobalPolicyAlgorithm() { @Override public void setGlobalPolicyAlgorithm(String policyCombiningAlgorithm) throws EntitlementException { - Registry registry = EntitlementServiceComponent. - getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + Registry registry = getGovernanceRegistry(); try { Collection policyCollection; if (registry.resourceExists(policyDataCollection)) { @@ -115,7 +109,7 @@ public void setGlobalPolicyAlgorithm(String policyCombiningAlgorithm) throws Ent } else { policyCollection = registry.newCollection(); } - policyCollection.setMediaType(PDPConstants.REGISTRY_MEDIA_TYPE); + policyCollection.setProperty("globalPolicyCombiningAlgorithm", policyCombiningAlgorithm); registry.put(policyDataCollection, policyCollection); @@ -131,11 +125,10 @@ public void setGlobalPolicyAlgorithm(String policyCombiningAlgorithm) throws Ent @Override public String getGlobalPolicyAlgorithmName() { - Registry registry = EntitlementServiceComponent. - getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId()); String algorithm = null; try { + Registry registry = getGovernanceRegistry(); if (registry.resourceExists(policyDataCollection)) { Collection collection = (Collection) registry.get(policyDataCollection); algorithm = collection.getProperty("globalPolicyCombiningAlgorithm"); @@ -144,6 +137,8 @@ public String getGlobalPolicyAlgorithmName() { if (log.isDebugEnabled()) { log.debug(e); } + } catch (EntitlementException e) { + log.error("Error while getting Global Policy Combining Algorithm Name.", e); } // set default @@ -164,10 +159,9 @@ public String[] getAllGlobalPolicyAlgorithmNames() { @Override public PolicyStoreDTO getPolicyData(String policyId) { - Registry registry = EntitlementServiceComponent. - getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId()); PolicyStoreDTO dataDTO = new PolicyStoreDTO(); try { + Registry registry = getGovernanceRegistry(); String path = policyDataCollection + policyId; if (registry.resourceExists(path)) { Resource resource = registry.get(path); @@ -182,6 +176,8 @@ public PolicyStoreDTO getPolicyData(String policyId) { if (log.isDebugEnabled()) { log.debug(e); } + } catch (EntitlementException e) { + log.error("Error while getting policy data for policyId: " + policyId, e); } return dataDTO; } @@ -190,10 +186,10 @@ public PolicyStoreDTO getPolicyData(String policyId) { @Override public PolicyStoreDTO[] getPolicyData() { - Registry registry = EntitlementServiceComponent. - getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + List policyStoreDTOs = new ArrayList(); try { + Registry registry = getGovernanceRegistry(); if (registry.resourceExists(policyDataCollection)) { Collection collection = (Collection) registry.get(policyDataCollection); String[] paths = collection.getChildren(); @@ -217,6 +213,8 @@ public PolicyStoreDTO[] getPolicyData() { if (log.isDebugEnabled()) { log.debug(e); } + } catch (EntitlementException e) { + log.error("Error while getting all policy data.", e); } return policyStoreDTOs.toArray(new PolicyStoreDTO[policyStoreDTOs.size()]); } @@ -224,8 +222,7 @@ public PolicyStoreDTO[] getPolicyData() { @Override public void setPolicyData(String policyId, PolicyStoreDTO policyDataDTO) throws EntitlementException { - Registry registry = EntitlementServiceComponent. - getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + Registry registry = getGovernanceRegistry(); try { String path = policyDataCollection + policyId; Resource resource; @@ -234,7 +231,7 @@ public void setPolicyData(String policyId, PolicyStoreDTO policyDataDTO) throws } else { resource = registry.newCollection(); } - resource.setMediaType(PDPConstants.REGISTRY_MEDIA_TYPE); + if (policyDataDTO.isSetActive()) { resource.setProperty("active", Boolean.toString(policyDataDTO.isActive())); } @@ -254,8 +251,7 @@ public void setPolicyData(String policyId, PolicyStoreDTO policyDataDTO) throws @Override public void removePolicyData(String policyId) throws EntitlementException { - Registry registry = EntitlementServiceComponent. - getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + Registry registry = getGovernanceRegistry(); try { String path = policyDataCollection + policyId; if (registry.resourceExists(path)) { @@ -267,4 +263,16 @@ public void removePolicyData(String policyId) throws EntitlementException { } } + + private Registry getGovernanceRegistry() throws EntitlementException { + + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + Registry registry = EntitlementServiceComponent.getGovernanceRegistry(tenantId); + + if (registry == null) { + throw new EntitlementException("Unable to get governance registry for tenant: " + tenantId); + } + + return registry; + } }