Skip to content

Commit

Permalink
IDENTITY-4663: Remove incorrect media type set for policy data
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Farasath Ahamed committed Aug 19, 2017
1 parent cf5a9c4 commit 53fb513
Showing 1 changed file with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
}
}

Expand All @@ -106,16 +101,15 @@ 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)) {
policyCollection = (Collection) registry.get(policyDataCollection);
} else {
policyCollection = registry.newCollection();
}
policyCollection.setMediaType(PDPConstants.REGISTRY_MEDIA_TYPE);

policyCollection.setProperty("globalPolicyCombiningAlgorithm", policyCombiningAlgorithm);
registry.put(policyDataCollection, policyCollection);

Expand All @@ -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");
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -190,10 +186,10 @@ public PolicyStoreDTO getPolicyData(String policyId) {
@Override
public PolicyStoreDTO[] getPolicyData() {

Registry registry = EntitlementServiceComponent.
getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId());

List<PolicyStoreDTO> policyStoreDTOs = new ArrayList<PolicyStoreDTO>();
try {
Registry registry = getGovernanceRegistry();
if (registry.resourceExists(policyDataCollection)) {
Collection collection = (Collection) registry.get(policyDataCollection);
String[] paths = collection.getChildren();
Expand All @@ -217,15 +213,16 @@ 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()]);
}

@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;
Expand All @@ -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()));
}
Expand All @@ -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)) {
Expand All @@ -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;
}
}

0 comments on commit 53fb513

Please sign in to comment.