use of org.wso2.balana.ParsingException in project carbon-identity-framework by wso2.
the class EntitlementEngine method evaluate.
/**
* Evaluates the given XACML request and returns the Response that the EntitlementEngine will
* hand back to the PEP. PEP needs construct the XACML request before sending it to the
* EntitlementEngine
*
* @param xacmlRequest XACML request as String
* @return XACML response as String
* @throws org.wso2.balana.ParsingException throws
* @throws org.wso2.carbon.identity.entitlement.EntitlementException throws
*/
public String evaluate(String xacmlRequest) throws EntitlementException, ParsingException {
if (log.isDebugEnabled() && IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.XACML_REQUEST)) {
log.debug("XACML Request : " + xacmlRequest);
}
String xacmlResponse;
if ((xacmlResponse = (String) getFromCache(xacmlRequest, false)) != null) {
if (log.isDebugEnabled() && IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.XACML_RESPONSE)) {
log.debug("XACML Response : " + xacmlResponse);
}
return xacmlResponse;
}
Map<PIPExtension, Properties> extensions = EntitlementServiceComponent.getEntitlementConfig().getExtensions();
if (extensions != null && !extensions.isEmpty()) {
PolicyRequestBuilder policyRequestBuilder = new PolicyRequestBuilder();
Element xacmlRequestElement = policyRequestBuilder.getXacmlRequest(xacmlRequest);
AbstractRequestCtx requestCtx = RequestCtxFactory.getFactory().getRequestCtx(xacmlRequestElement);
Set<PIPExtension> pipExtensions = extensions.keySet();
for (PIPExtension pipExtension : pipExtensions) {
pipExtension.update(requestCtx);
}
ResponseCtx responseCtx = pdp.evaluate(requestCtx);
xacmlResponse = responseCtx.encode();
} else {
xacmlResponse = pdp.evaluate(xacmlRequest);
}
addToCache(xacmlRequest, xacmlResponse, false);
if (log.isDebugEnabled() && IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.XACML_RESPONSE)) {
log.debug("XACML Response : " + xacmlResponse);
}
return xacmlResponse;
}
use of org.wso2.balana.ParsingException in project carbon-identity-framework by wso2.
the class EntitlementUtil method getPolicy.
public static AbstractPolicy getPolicy(String policy) {
DocumentBuilder builder;
InputStream stream = null;
// now use the factory to create the document builder
try {
builder = getSecuredDocumentBuilder(true);
stream = new ByteArrayInputStream(policy.getBytes("UTF-8"));
Document doc = builder.parse(stream);
Element root = doc.getDocumentElement();
String name = root.getTagName();
// see what type of policy this is
if (name.equals("Policy")) {
return Policy.getInstance(root);
} else if (name.equals("PolicySet")) {
return PolicySet.getInstance(root, null);
} else {
// this isn't a root type that we know how to handle
throw new ParsingException("Unknown root document type: " + name);
}
} catch (Exception e) {
throw new IllegalArgumentException("Error while parsing start up policy", e);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
log.error("Error while closing input stream");
}
}
}
}
use of org.wso2.balana.ParsingException in project carbon-identity-framework by wso2.
the class PAPPolicyReader method handleDocument.
/**
* @param doc
* @return
* @throws org.wso2.balana.ParsingException
*/
private AbstractPolicy handleDocument(Document doc) throws ParsingException {
// handle the policy, if it's a known type
Element root = doc.getDocumentElement();
String name = root.getLocalName();
// see what type of policy this is
if (name.equals("Policy")) {
return Policy.getInstance(root);
} else if (name.equals("PolicySet")) {
return PolicySet.getInstance(root, policyFinder);
} else {
// this isn't a root type that we know how to handle
throw new ParsingException("Unknown root document type: " + name);
}
}
use of org.wso2.balana.ParsingException in project carbon-identity-framework by wso2.
the class EntitlementEngine method evaluateReturnResponseCtx.
/**
* Evaluates the given XACML request and returns the ResponseCtx Response that the EntitlementEngine will
* hand back to the PEP. PEP needs construct the XACML request before sending it to the
* EntitlementEngine
*
* @param xacmlRequest XACML request as String
* @return ResponseCtx response
* @throws org.wso2.balana.ParsingException throws
* @throws org.wso2.carbon.identity.entitlement.EntitlementException throws
* @throws javax.xml.parsers.ParserConfigurationException throws
* @throws org.xml.sax.SAXException throws
* @throws java.io.IOException throws
*/
public ResponseCtx evaluateReturnResponseCtx(String xacmlRequest) throws EntitlementException, ParsingException, ParserConfigurationException, SAXException, IOException {
if (log.isDebugEnabled() && IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.XACML_REQUEST)) {
log.debug("XACML Request : " + xacmlRequest);
}
String xacmlResponse;
ResponseCtx responseCtx;
if ((xacmlResponse = (String) getFromCache(xacmlRequest, false)) != null) {
if (log.isDebugEnabled() && IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.XACML_RESPONSE)) {
log.debug("XACML Response : " + xacmlResponse);
}
DocumentBuilderFactory documentBuilderFactory = IdentityUtil.getSecuredDocumentBuilderFactory();
Element node = documentBuilderFactory.newDocumentBuilder().parse(new ByteArrayInputStream(xacmlResponse.getBytes())).getDocumentElement();
return (ResponseCtx.getInstance(node));
}
Map<PIPExtension, Properties> extensions = EntitlementServiceComponent.getEntitlementConfig().getExtensions();
if (extensions != null && !extensions.isEmpty()) {
PolicyRequestBuilder policyRequestBuilder = new PolicyRequestBuilder();
Element xacmlRequestElement = policyRequestBuilder.getXacmlRequest(xacmlRequest);
AbstractRequestCtx requestCtx = RequestCtxFactory.getFactory().getRequestCtx(xacmlRequestElement);
Set<PIPExtension> pipExtensions = extensions.keySet();
for (PIPExtension pipExtension : pipExtensions) {
pipExtension.update(requestCtx);
}
responseCtx = pdp.evaluate(requestCtx);
} else {
responseCtx = pdp.evaluateReturnResponseCtx(xacmlRequest);
}
xacmlResponse = responseCtx.encode();
addToCache(xacmlRequest, xacmlResponse, false);
if (log.isDebugEnabled() && IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.XACML_RESPONSE)) {
log.debug("XACML Response : " + xacmlResponse);
}
return responseCtx;
}
use of org.wso2.balana.ParsingException in project carbon-identity-framework by wso2.
the class CarbonAttributeFinder method findAttribute.
/*
* (non-Javadoc)
*
* @see org.wso2.balana.finder.AttributeFinderModule#findAttribute(java.net.URI, java.net.URI,
* java.net.URI, java.net.URI, org.wso2.balana.EvaluationCtx, int)
*/
public EvaluationResult findAttribute(URI attributeType, URI attributeId, String issuer, URI category, EvaluationCtx context) {
List<AttributeValue> attrBag = new ArrayList<AttributeValue>();
// Get the list of attribute finders who are registered with this particular attribute.
List<PIPAttributeFinder> finders = null;
if (StringUtils.isNotBlank(category.toString())) {
finders = attrFinders.get(category.toString());
if (log.isDebugEnabled()) {
log.debug("No attribute designators defined for the category " + category.toString());
}
}
if (CollectionUtils.isEmpty(finders)) {
finders = attrFinders.get(attributeId.toString());
if (CollectionUtils.isEmpty(finders)) {
if (log.isDebugEnabled()) {
log.debug("No attribute designators defined for the attribute " + attributeId.toString());
}
return new EvaluationResult(BagAttribute.createEmptyBag(attributeType));
}
}
try {
for (Iterator iterator = finders.iterator(); iterator.hasNext(); ) {
PIPAttributeFinder pipAttributeFinder = (PIPAttributeFinder) iterator.next();
if (log.isDebugEnabled()) {
log.debug(String.format("Finding attributes with the PIP attribute handler %1$s", pipAttributeFinder.getClass()));
}
Set<String> attrs = null;
String key = null;
if (attributeFinderCache != null && !pipAttributeFinder.overrideDefaultCache()) {
key = "[" + attributeType.toString() + "][" + attributeId.toString() + "][" + category.toString() + "][" + encodeContext(context) + "]";
if (issuer != null) {
key += "[" + issuer + "]";
}
if (key != null) {
if (log.isDebugEnabled()) {
log.debug("Retrieving attributes from cache, tenantId: " + tenantId + ", key: " + key);
}
attrs = attributeFinderCache.getFromCache(tenantId, key);
}
}
if (attrs == null) {
attrs = pipAttributeFinder.getAttributeValues(attributeType, attributeId, category, issuer, context);
if (attributeFinderCache != null && key != null && !pipAttributeFinder.overrideDefaultCache()) {
attributeFinderCache.addToCache(tenantId, key, attrs);
}
}
if (attrs != null) {
for (Iterator iterAttr = attrs.iterator(); iterAttr.hasNext(); ) {
final String attr = (String) iterAttr.next();
AttributeValue attribute = EntitlementUtil.getAttributeValue(attr, attributeType.toString());
attrBag.add(attribute);
if (log.isDebugEnabled()) {
log.debug("Attribute added to the attributeBag: \'" + attr + "\'");
}
}
}
}
} catch (ParsingException e) {
log.error("Error while parsing attribute values from EvaluationCtx : ", e);
ArrayList<String> code = new ArrayList<String>();
code.add(Status.STATUS_MISSING_ATTRIBUTE);
Status status = new Status(code, "Error while parsing attribute values from EvaluationCtx : " + e.getMessage());
return new EvaluationResult(status);
} catch (ParseException e) {
e.printStackTrace();
log.error("Error while parsing attribute values from EvaluationCtx : ", e);
ArrayList<String> code = new ArrayList<String>();
code.add(Status.STATUS_MISSING_ATTRIBUTE);
Status status = new Status(code, "Error while parsing attribute values from EvaluationCtx : " + e.getMessage());
return new EvaluationResult(status);
} catch (URISyntaxException e) {
log.error("Error while parsing attribute values from EvaluationCtx : ", e);
ArrayList<String> code = new ArrayList<String>();
code.add(Status.STATUS_MISSING_ATTRIBUTE);
Status status = new Status(code, "Error while parsing attribute values from EvaluationCtx :" + e.getMessage());
return new EvaluationResult(status);
} catch (Exception e) {
log.error("Error while retrieving attribute values from PIP attribute finder : ", e);
ArrayList<String> code = new ArrayList<String>();
code.add(Status.STATUS_MISSING_ATTRIBUTE);
Status status = new Status(code, "Error while retrieving attribute values from PIP" + " attribute finder : " + e.getMessage());
return new EvaluationResult(status);
}
return new EvaluationResult(new BagAttribute(attributeType, attrBag));
}
Aggregations