use of org.wso2.carbon.identity.entitlement.EntitlementException in project carbon-identity-framework by wso2.
the class EntitlementUtil method isPolicyExists.
/**
* This method checks whether there is a policy having the same policyId as the given policyId is in the registry
*
* @param policyId
* @param registry
* @return
* @throws EntitlementException
*/
public static boolean isPolicyExists(String policyId, Registry registry) throws EntitlementException {
PAPPolicyStoreReader policyReader = null;
policyReader = new PAPPolicyStoreReader(new PAPPolicyStore(registry));
return policyReader.isExistPolicy(policyId);
}
use of org.wso2.carbon.identity.entitlement.EntitlementException 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);
}
}
use of org.wso2.carbon.identity.entitlement.EntitlementException 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");
}
}
use of org.wso2.carbon.identity.entitlement.EntitlementException in project carbon-identity-framework by wso2.
the class EntitlementPolicyAdminService method orderPolicy.
public void orderPolicy(String policyId, int newOrder) throws EntitlementException {
PolicyDTO policyDTO = new PolicyDTO();
policyDTO.setPolicyId(policyId);
policyDTO.setPolicyOrder(newOrder);
PAPPolicyStoreManager storeManager = EntitlementAdminEngine.getInstance().getPapPolicyStoreManager();
if (storeManager.isExistPolicy(policyId)) {
storeManager.addOrUpdatePolicy(policyDTO);
}
publishToPDP(new String[] { policyDTO.getPolicyId() }, EntitlementConstants.PolicyPublish.ACTION_ORDER, null, false, newOrder);
}
use of org.wso2.carbon.identity.entitlement.EntitlementException in project carbon-identity-framework by wso2.
the class EntitlementPolicyAdminService method deleteSubscriber.
/**
* delete subscriber details from registry
*
* @param subscriberId subscriber id
* @throws EntitlementException throws, if fails
*/
public void deleteSubscriber(String subscriberId) throws EntitlementException {
PolicyPublisher publisher = EntitlementAdminEngine.getInstance().getPolicyPublisher();
publisher.deleteSubscriber(subscriberId);
}
Aggregations