org.wso2.balana.ctx.ResponseCtx Java Examples

The following examples show how to use org.wso2.balana.ctx.ResponseCtx. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EntitlementEngine.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluates the given XACML request and returns the Response
 *
 * @param requestCtx Balana Object model for request
 * @param xacmlRequest Balana Object model for request
 * @return ResponseCtx  Balana Object model for response
 */
public ResponseCtx evaluate(AbstractRequestCtx requestCtx, String xacmlRequest) {

    if (log.isDebugEnabled() && IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.XACML_REQUEST)) {
        log.debug("XACML Request : " + xacmlRequest);
    }

    ResponseCtx xacmlResponse;

    if ((xacmlResponse = (ResponseCtx) getFromCache(xacmlRequest, false)) != null) {
        if (log.isDebugEnabled() && IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.XACML_RESPONSE)) {
            log.debug("XACML Response : " + xacmlResponse);
        }
        return xacmlResponse;
    }

    xacmlResponse = pdp.evaluate(requestCtx);

    addToCache(xacmlRequest, xacmlResponse, false);

    if (log.isDebugEnabled() && IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.XACML_RESPONSE)) {
        log.debug("XACML Response : " + xacmlResponse);
    }
    return xacmlResponse;
}
 
Example #2
Source File: PolicySearch.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to get XACML decision
 *
 * @param requestAttributes XACML request attributes
 * @return whether permit or deny
 */
private boolean getResponse(List<AttributeDTO> requestAttributes) {

    ResponseCtx responseCtx;
    AbstractRequestCtx requestCtx = EntitlementUtil.createRequestContext(requestAttributes);

    responseCtx = EntitlementEngine.getInstance().evaluateByContext(requestCtx);

    if (responseCtx != null) {
        Set<AbstractResult> results = responseCtx.getResults();
        for (AbstractResult result : results) {
            if (result.getDecision() == AbstractResult.DECISION_PERMIT) {
                return true;
            }
        }
    }

    return false;
}
 
Example #3
Source File: PolicySearch.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to get XACML decision
 *
 * @param requestAttributes XACML request attributes
 * @return whether permit or deny
 */
private boolean getResponse(List<AttributeDTO> requestAttributes) {

    ResponseCtx responseCtx;
    AbstractRequestCtx requestCtx = EntitlementUtil.createRequestContext(requestAttributes);

    responseCtx = EntitlementEngine.getInstance().evaluateByContext(requestCtx);

    if (responseCtx != null) {
        Set<AbstractResult> results = responseCtx.getResults();
        for (AbstractResult result : results) {
            if (result.getDecision() == AbstractResult.DECISION_PERMIT) {
                return true;
            }
        }
    }

    return false;
}
 
Example #4
Source File: BasicTestV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0001() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0001.xml");
        log.info("Basic Test 0001 is started");

        for(int i = 1; i < 8 ; i++){
            
            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                                                        "request_0001_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                                    VERSION_DIRECTORY, "response_0001_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);    
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0001 is finished");
        }
    }
 
Example #5
Source File: JSONResponseWriter.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Returns <code>JsonObject</code> created by parsing the contents of a given
 * Balana <code>{@link ResponseCtx}</code>
 *
 * @param response <code>{@link ResponseCtx}</code>
 * @return <code>{@link JsonObject}</code> with parsed properties
 * @throws ResponseWriteException <code>{@link ResponseWriteException}</code>
 */
public static JsonObject write(ResponseCtx response) throws ResponseWriteException {

    JsonObject responseWrap = new JsonObject();

    //JsonObject jsonResponse = new JsonObject();
    JsonArray results = new JsonArray();

    Properties properties = EntitlementUtil.getPropertiesFromEntitlementConfig();
    if (properties != null) {
        if (Boolean.parseBoolean(properties.getProperty(PDPConstants.XACML_JSON_SHORT_FORM_ENABLED))) {
            xacmlJSONProfileShortFormEnable = true;
        }
    }
    //Loop all AbstractResult objects in ResponseCtx and add them as
    //Requests to JSON Response
    //There should be at least 1 request
    if (response.getResults().size() < 1) {
        throw new ResponseWriteException(40032, "XACML response should contain at least 1 Result");
    }

    for (AbstractResult result : response.getResults()) {
        /* AbstractResult type does not contain PolicyIdentifierList, as per XACML 3.0, the PolicyIdentifier is
        optional. Hence, Result type is not used. */
        results.add(abstractResultToJSONObject(result));
    }
    responseWrap.add(EntitlementEndpointConstants.RESPONSE, results);

    return responseWrap;
}
 
Example #6
Source File: BasicTestV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0002() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0002.xml");
        log.info("Basic Test 0002 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                                                        "request_0002_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                                    VERSION_DIRECTORY, "response_0002_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0002 is finished");
        }
    }
 
Example #7
Source File: BasicTestV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0003() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0003.xml");
        log.info("Basic Test 0003 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                                                        "request_0003_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                                    VERSION_DIRECTORY, "response_0003_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0003 is finished");
        }
    }
 
Example #8
Source File: BasicTestV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0004() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0004.xml");
        log.info("Basic Test 0004 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                                                        "request_0004_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                                    VERSION_DIRECTORY, "response_0004_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0003 is finished");
        }
    }
 
Example #9
Source File: BasicTestV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0005() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0005.xml");
        log.info("Basic Test 0005 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                                                        "request_0005_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                                    VERSION_DIRECTORY, "response_0005_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0003 is finished");
        }
    }
 
Example #10
Source File: TestMultipleRequestV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0001() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0014.xml");
        PDP pdp = getPDPNewInstance(policies);
        log.info("Basic Test 0014 is started");

        for (int i = 1; i < 2; i++) {

            if (i < 10) {
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0014_" + reqResNo + ".xml");
            if (request != null) {
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if (response != null) {
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0014_" + reqResNo + ".xml");
                    if (expectedResponseCtx != null) {
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null", false);
                    }
                } else {
                    assertFalse("Response received PDP is Null", false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0014 is finished");
        }
    }
 
Example #11
Source File: TestFunctionV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0001() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0006.xml");
        PDP pdp = getPDPNewInstance(policies);
        log.info("Basic Test 0006 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                                                        "request_0006_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                                    VERSION_DIRECTORY, "response_0006_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0006 is finished");
        }
    }
 
Example #12
Source File: TestFunctionV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0002() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0015.xml");
        PDP pdp = getPDPNewInstance(policies);
        log.info("Basic Test 0015 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0015_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0015_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0015 is finished");
        }
    }
 
Example #13
Source File: TestFunctionV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0003() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0016.xml");
        PDP pdp = getPDPNewInstance(policies);
        log.info("Basic Test 0016 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0016_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0016_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0016 is finished");
        }
    }
 
Example #14
Source File: TestFunctionV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0004() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0017.xml");
        PDP pdp = getPDPNewInstance(policies);
        log.info("Basic Test 0017 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0017_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0017_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0017 is finished");
        }
    }
 
Example #15
Source File: TestFunctionV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0005() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0018.xml");
        PDP pdp = getPDPNewInstance(policies);
        log.info("Basic Test 0018 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0018_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0018_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0018 is finished");
        }
    }
 
Example #16
Source File: TestXPathV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0001() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0007.xml");
        log.info("Basic Test 0007 is started");

        for (int i = 1; i < 4; i++) {

            if (i < 10) {
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0007_" + reqResNo + ".xml");
            if (request != null) {
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if (response != null) {
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0007_" + reqResNo + ".xml");
                    if (expectedResponseCtx != null) {
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null", false);
                    }
                } else {
                    assertFalse("Response received PDP is Null", false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0007 is finished");
        }
    }
 
Example #17
Source File: TestXPathV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0002() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0008.xml");
        log.info("Basic Test 0008 is started");

        for (int i = 1; i < 4; i++) {

            if (i < 10) {
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0008_" + reqResNo + ".xml");
            if (request != null) {
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if (response != null) {
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0008_" + reqResNo + ".xml");
                    if (expectedResponseCtx != null) {
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null", false);
                    }
                } else {
                    assertFalse("Response received PDP is Null", false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0008 is finished");
        }
    }
 
Example #18
Source File: TestXPathV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0003() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0009.xml");
        log.info("Basic Test 0009 is started");

        for (int i = 1; i < 4; i++) {

            if (i < 10) {
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0009_" + reqResNo + ".xml");
            if (request != null) {
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if (response != null) {
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0009_" + reqResNo + ".xml");
                    if (expectedResponseCtx != null) {
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null", false);
                    }
                } else {
                    assertFalse("Response received PDP is Null", false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0009 is finished");
        }
    }
 
Example #19
Source File: TestXPathV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0004() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0010.xml");
        log.info("Basic Test 0010 is started");

        for (int i = 1; i < 4; i++) {

            if (i < 10) {
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0010_" + reqResNo + ".xml");
            if (request != null) {
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if (response != null) {
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0010_" + reqResNo + ".xml");
                    if (expectedResponseCtx != null) {
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null", false);
                    }
                } else {
                    assertFalse("Response received PDP is Null", false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0010 is finished");
        }
    }
 
Example #20
Source File: TestXPathV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0005() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0011.xml");
        log.info("Basic Test 0011 is started");

        for (int i = 1; i < 4; i++) {

            if (i < 10) {
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0011_" + reqResNo + ".xml");
            if (request != null) {
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if (response != null) {
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0011_" + reqResNo + ".xml");
                    if (expectedResponseCtx != null) {
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null", false);
                    }
                } else {
                    assertFalse("Response received PDP is Null", false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0011 is finished");
        }
    }
 
Example #21
Source File: TestAlgorithmsV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0001() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0019.xml");
        PDP pdp = getPDPNewInstance(policies);
        log.info("Basic Test 0019 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                                                        "request_0019_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                                    VERSION_DIRECTORY, "response_0019_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0019 is finished");
        }
    }
 
Example #22
Source File: TestAlgorithmsV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testBasicTest0002() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0020.xml");
        PDP pdp = getPDPNewInstance(policies);
        log.info("Basic Test 0020 is started");

        for(int i = 1; i < 4 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0020_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0020_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Basic Test 0020 is finished");
        }
    }
 
Example #23
Source File: DecisionResource.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * API endpoint for evaluating XACML XML policies
 *
 * @return XML Policy result String
 */
@POST
@Path("pdp")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@ApiOperation(value = "Get response by evaluating JSON/XML XACML request", response = String.class)
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "XACML JSON/XML Response"),
        @ApiResponse(code = 40010, message = EntitlementEndpointConstants.ERROR_UNAUTHORIZED_MESSAGE,
                response = ExceptionBean.class),
        @ApiResponse(code = 40020, message = EntitlementEndpointConstants.ERROR_REQUEST_PARSE_MESSAGE,
                response = ExceptionBean.class),
        @ApiResponse(code = 40010, message = EntitlementEndpointConstants.ERROR_RESPONSE_READ_MESSAGE,
                response = ExceptionBean.class)
})
public String getDecision(@ApiParam(value = "Request Media Type", required = true)
                          @HeaderParam(EntitlementEndpointConstants.ACCEPT_HEADER) String format,
                          @ApiParam(value = "Authentication Type", required = true)
                          @HeaderParam(EntitlementEndpointConstants.AUTHENTICATION_TYPE_HEADER) String authMechanism,
                          @ApiParam(value = "Add HTTP Basic Authorization", required = true)
                          @HeaderParam(EntitlementEndpointConstants.AUTHORIZATION_HEADER) String authorization,
                          @ApiParam(value = "Response Media Type", required = true)
                          @HeaderParam(EntitlementEndpointConstants.CONTENT_TYPE_HEADER) String contentType,
                          @ApiParam(value = "XACML JSON/XML Request", required = true)
                                  String xacmlRequest) throws Exception {

    if (log.isDebugEnabled()) {
        log.debug("recieved :" + xacmlRequest);
    }
    EntitlementEngine entitlementEngine = EntitlementEngine.getInstance();

    if (contentType.equals(EntitlementEndpointConstants.APPLICATION_JSON)) {
        RequestCtx requestCtx = JSONRequestParser.parse(xacmlRequest);
        ResponseCtx responseCtx = entitlementEngine.evaluate(requestCtx, xacmlRequest);
        return gson.toJson(JSONResponseWriter.write(responseCtx));
    } else {
        return entitlementEngine.evaluate(xacmlRequest);
    }

}
 
Example #24
Source File: TestJSONResponseWriter.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteWithObligations() throws URISyntaxException {

    List<AttributeAssignment> assignments = new ArrayList<>();
    String content = "Error: Channel request is not WEB.";
    URI type = new URI("http://www.w3.org/2001/XMLSchema#string");
    URI attributeId = new URI("urn:oasis:names:tc:xacml:3.0:example:attribute:text");
    AttributeAssignment attributeAssignment = new AttributeAssignment(attributeId, type, null, content, null);
    assignments.add(attributeAssignment);

    List<ObligationResult> obligationResults = new ArrayList<>();
    ObligationResult obligationResult = new Obligation(assignments, new URI("channel_ko"));
    obligationResults.add(obligationResult);

    List<String> codes = new ArrayList<>();
    codes.add("urn:oasis:names:tc:xacml:1.0:status:ok");
    AbstractResult abstractResult = new Result(1, new Status(codes), obligationResults, null, null);

    ResponseCtx responseCtx = new ResponseCtx(abstractResult);

    JSONResponseWriter jsonResponseWriter = new JSONResponseWriter();
    try {
        JsonObject jsonObject = jsonResponseWriter.write(responseCtx);
        assertNotNull("Failed to build the XACML json response", jsonObject.toString());
        assertFalse("Failed to build the XACML json response", jsonObject.entrySet().isEmpty());
        for(Map.Entry<String, JsonElement> jsonElementEntry: jsonObject.entrySet()) {
            if (jsonElementEntry.getKey().equals("Response")) {
                JsonArray jsonArray = (JsonArray) jsonElementEntry.getValue();
                assertEquals("Failed to build the XACML json response with correct evaluation",
                        jsonArray.get(0).getAsJsonObject().get("Decision").getAsString(), "Deny");
            }
        }
    } catch (ResponseWriteException e) {
        assertNull("Failed to build the XACML response", e);
    }

}
 
Example #25
Source File: TestJSONResponseWriter.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteWithAdvices() throws URISyntaxException {

    List<AttributeAssignment> assignments = new ArrayList<>();
    String content = "Error: Channel request is not WEB.";
    URI type = new URI("http://www.w3.org/2001/XMLSchema#string");
    URI attributeId = new URI("urn:oasis:names:tc:xacml:3.0:example:attribute:text");
    AttributeAssignment attributeAssignment = new AttributeAssignment(attributeId, type, null, content, null);
    assignments.add(attributeAssignment);

    List<Advice> adviceResults = new ArrayList<>();
    Advice adviceResult = new Advice(new URI("channel_ko"), assignments);
    adviceResults.add(adviceResult);

    List<String> codes = new ArrayList<>();
    codes.add("urn:oasis:names:tc:xacml:1.0:status:ok");
    AbstractResult abstractResult = new Result(1, new Status(codes), null, adviceResults, null);

    ResponseCtx responseCtx = new ResponseCtx(abstractResult);

    JSONResponseWriter jsonResponseWriter = new JSONResponseWriter();
    try {
        JsonObject jsonObject = jsonResponseWriter.write(responseCtx);
        assertNotNull("Failed to build the XACML json response", jsonObject.toString());
        assertFalse("Failed to build the XACML json response", jsonObject.entrySet().isEmpty());
        for(Map.Entry<String, JsonElement> jsonElementEntry: jsonObject.entrySet()) {
            if (jsonElementEntry.getKey().equals("Response")) {
                JsonArray jsonArray = (JsonArray) jsonElementEntry.getValue();
                assertEquals("Failed to build the XACML json response with correct evaluation",
                        jsonArray.get(0).getAsJsonObject().get("Decision").getAsString(), "Deny");
            }
        }
    } catch (ResponseWriteException e) {
        assertNull("Failed to build the XACML json response", e);
    }

}
 
Example #26
Source File: AdvanceTestV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testAdvanceTest0003() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0003.xml");
        log.info("Advance Test 0003 is started. This test is for Jira COMMONS-97");

        for(int i = 1; i < 2 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                    "request_0003_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                            VERSION_DIRECTORY, "response_0003_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Advance Test 0003 is finished");
        }
    }
 
Example #27
Source File: AdvanceTestV3.java    From balana with Apache License 2.0 5 votes vote down vote up
public void testAdvanceTest0001() throws Exception {

        String reqResNo;
        Set<String> policies = new HashSet<String>();
        policies.add("TestPolicy_0002.xml");
        log.info("Advance Test 0002 is started. This test is for Jira IDENTITY-416");

        for(int i = 1; i < 2 ; i++){

            if(i < 10){
                reqResNo = "0" + i;
            } else {
                reqResNo = Integer.toString(i);
            }

            String request = TestUtil.createRequest(ROOT_DIRECTORY, VERSION_DIRECTORY,
                                                        "request_0002_" + reqResNo + ".xml");
            if(request != null){
                log.info("Request that is sent to the PDP :  " + request);
                ResponseCtx response = TestUtil.evaluate(getPDPNewInstance(policies), request);
                if(response != null){
                    log.info("Response that is received from the PDP :  " + response.encode());
                    ResponseCtx expectedResponseCtx = TestUtil.createResponse(ROOT_DIRECTORY,
                                    VERSION_DIRECTORY, "response_0002_" + reqResNo + ".xml");
                    if(expectedResponseCtx != null){
                        assertTrue(TestUtil.isMatching(response, expectedResponseCtx));
                    } else {
                        assertTrue("Response read from file is Null",false);
                    }
                } else {
                    assertFalse("Response received PDP is Null",false);
                }
            } else {
                assertTrue("Request read from file is Null", false);
            }

            log.info("Advance Test 0002 is finished");
        }
    }
 
Example #28
Source File: PDPController.java    From balana with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates the request which was created based on KMarket sample.
 *
 * @param request is going to be converted to XACML Request.
 * @return        result of the Policy Decision Point.
 * */
@PostMapping("/evaluate")
public ResponseObject evaluate(@RequestBody RequestObject request)
{
    int totalAmount = 0;
    Utilities.initData();
    Utilities.initBalana();

    totalAmount = Utilities.calculateTotal(request.getProductName(), request.getNumberOfProducts());
    String xacmlRequest = Utilities.createXACMLRequest(
            request.getUsername(), request.getProductName(), request.getNumberOfProducts(), totalAmount);

    PDP pdp = Utilities.getPDPNewInstance();
    String xacmlResponse = pdp.evaluate(xacmlRequest); //evaluates XACML request here.
    String responseMessage = "";

    try {
        ResponseCtx responseCtx = ResponseCtx.getInstance(Utilities.getXacmlResponse(xacmlResponse));
        AbstractResult result  = responseCtx.getResults().iterator().next();
        if(AbstractResult.DECISION_PERMIT == result.getDecision()){
            responseMessage = "\n" + request.getUsername() + " is authorized to perform this purchase\n\n";
        } else {
            //if it is not PERMIT, DENY is going to be returned to client user.
            responseMessage += "\n" + request.getUsername() + " is NOT authorized to perform this purchase\n";
            List<Advice> advices = result.getAdvices();
            for(Advice advice : advices){
                List<AttributeAssignment> assignments = advice.getAssignments();
                for(AttributeAssignment assignment : assignments){
                    responseMessage += "Advice :  " + assignment.getContent() +"\n\n";
                }
            }
        }
    } catch (ParsingException e) {
        e.printStackTrace();
    }
    return new ResponseObject(responseMessage);
}
 
Example #29
Source File: Main.java    From balana with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args){

        Console console;
        String userName = "none";
        String content = "foo";

        initBalana();
        
        if ((console = System.console()) != null){
            userName = console.readLine("Enter User name  [bob,  peter, alice] : ");
            if(userName == null || userName.trim().length() < 1 ){
                System.err.println("\nUser name can not be empty\n");
                return;
            }
        }

        String request = createXACMLRequest(userName, content);

        PDP pdp = getPDPNewInstance();

        System.out.println("\n======================== XACML Request ====================");
        System.out.println(request);
        System.out.println("===========================================================");

        String response = pdp.evaluate(request);

        System.out.println("\n======================== XACML Response ===================");
        System.out.println(response);
        System.out.println("===========================================================");

        try {
            ResponseCtx responseCtx = ResponseCtx.getInstance(getXacmlResponse(response));
            AbstractResult result  = responseCtx.getResults().iterator().next();
            if(AbstractResult.DECISION_PERMIT == result.getDecision()){
                System.out.println("\n" + userName + " is authorized to perform this access\n\n");
            } else {
                System.out.println("\n" + userName + " is NOT authorized to perform this access\n");
            }
        } catch (ParsingException e) {
            e.printStackTrace();
        }

    }
 
Example #30
Source File: EntitlementEngine.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * 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;

}