Search in sources :

Example 1 with ParsingException

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;
}
Also used : AbstractRequestCtx(org.wso2.balana.ctx.AbstractRequestCtx) PIPExtension(org.wso2.carbon.identity.entitlement.pip.PIPExtension) Element(org.w3c.dom.Element) Properties(java.util.Properties) ResponseCtx(org.wso2.balana.ctx.ResponseCtx) PolicyRequestBuilder(org.wso2.carbon.identity.entitlement.policy.PolicyRequestBuilder)

Example 2 with ParsingException

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");
            }
        }
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) ParsingException(org.wso2.balana.ParsingException) IOException(java.io.IOException) Document(org.w3c.dom.Document) URISyntaxException(java.net.URISyntaxException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) ParseException(java.text.ParseException) SAXException(org.xml.sax.SAXException) ParsingException(org.wso2.balana.ParsingException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 3 with ParsingException

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);
    }
}
Also used : Element(org.w3c.dom.Element) ParsingException(org.wso2.balana.ParsingException)

Example 4 with ParsingException

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;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) AbstractRequestCtx(org.wso2.balana.ctx.AbstractRequestCtx) ByteArrayInputStream(java.io.ByteArrayInputStream) PIPExtension(org.wso2.carbon.identity.entitlement.pip.PIPExtension) Element(org.w3c.dom.Element) Properties(java.util.Properties) ResponseCtx(org.wso2.balana.ctx.ResponseCtx) PolicyRequestBuilder(org.wso2.carbon.identity.entitlement.policy.PolicyRequestBuilder)

Example 5 with ParsingException

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));
}
Also used : Status(org.wso2.balana.ctx.Status) AttributeValue(org.wso2.balana.attr.AttributeValue) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) EvaluationResult(org.wso2.balana.cond.EvaluationResult) TransformerException(javax.xml.transform.TransformerException) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) ParsingException(org.wso2.balana.ParsingException) BagAttribute(org.wso2.balana.attr.BagAttribute) ParsingException(org.wso2.balana.ParsingException) Iterator(java.util.Iterator) ParseException(java.text.ParseException)

Aggregations

Element (org.w3c.dom.Element)5 ParsingException (org.wso2.balana.ParsingException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URISyntaxException (java.net.URISyntaxException)2 ParseException (java.text.ParseException)2 Properties (java.util.Properties)2 AbstractRequestCtx (org.wso2.balana.ctx.AbstractRequestCtx)2 ResponseCtx (org.wso2.balana.ctx.ResponseCtx)2 PIPExtension (org.wso2.carbon.identity.entitlement.pip.PIPExtension)2 PolicyRequestBuilder (org.wso2.carbon.identity.entitlement.policy.PolicyRequestBuilder)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 Document (org.w3c.dom.Document)1 AttributeValue (org.wso2.balana.attr.AttributeValue)1