Search in sources :

Example 1 with EntitlementEngine

use of org.wso2.carbon.identity.entitlement.pdp.EntitlementEngine 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 EntitlementEngine

use of org.wso2.carbon.identity.entitlement.pdp.EntitlementEngine in project carbon-identity-framework by wso2.

the class EntitlementEngineCache method getEntitlementCache.

private Cache<Integer, EntitlementEngine> getEntitlementCache() {
    Cache<Integer, EntitlementEngine> cache;
    CacheManager cacheManager = Caching.getCacheManagerFactory().getCacheManager(ENTITLEMENT_ENGINE_CACHE_MANAGER);
    if (cacheManager != null) {
        if (cacheBuilder == null) {
            Properties properties = EntitlementServiceComponent.getEntitlementConfig().getEngineProperties();
            String engineCachingInterval = properties.getProperty(PDPConstants.ENTITLEMENT_ENGINE_CACHING_INTERVAL);
            long entitlementEngineCachingInterval = DEFAULT_ENTITLEMENT_ENGINE_CACHING_INTERVAL;
            if (engineCachingInterval != null) {
                try {
                    entitlementEngineCachingInterval = Long.parseLong(engineCachingInterval);
                } catch (NumberFormatException e) {
                    log.warn("Invalid value for " + PDPConstants.ENTITLEMENT_ENGINE_CACHING_INTERVAL + ". Using " + "default value " + entitlementEngineCachingInterval + " seconds.");
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(PDPConstants.ENTITLEMENT_ENGINE_CACHING_INTERVAL + " not set. Using default value " + entitlementEngineCachingInterval + " seconds.");
                }
            }
            cacheManager.removeCache(ENTITLEMENT_ENGINE_CACHE);
            cacheBuilder = cacheManager.<Integer, EntitlementEngine>createCacheBuilder(ENTITLEMENT_ENGINE_CACHE).setExpiry(CacheConfiguration.ExpiryType.ACCESSED, new CacheConfiguration.Duration(TimeUnit.SECONDS, entitlementEngineCachingInterval)).setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration(TimeUnit.SECONDS, entitlementEngineCachingInterval));
            cache = cacheBuilder.build();
        } else {
            cache = cacheManager.getCache(ENTITLEMENT_ENGINE_CACHE);
        }
    } else {
        cache = Caching.getCacheManager().getCache(ENTITLEMENT_ENGINE_CACHE);
    }
    if (log.isDebugEnabled()) {
        log.debug("created authorization cache : " + cache);
    }
    return cache;
}
Also used : EntitlementEngine(org.wso2.carbon.identity.entitlement.pdp.EntitlementEngine) CacheManager(javax.cache.CacheManager) Properties(java.util.Properties) CacheConfiguration(javax.cache.CacheConfiguration)

Example 3 with EntitlementEngine

use of org.wso2.carbon.identity.entitlement.pdp.EntitlementEngine in project carbon-identity-framework by wso2.

the class EntitlementEngineCache method put.

public void put(int key, EntitlementEngine engine) {
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        carbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        carbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        getEntitlementCache().put(key, engine);
        if (log.isDebugEnabled()) {
            log.debug("Cache : " + ENTITLEMENT_ENGINE_CACHE + " is populated with new entry " + "with tenantId : " + key);
        }
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext)

Example 4 with EntitlementEngine

use of org.wso2.carbon.identity.entitlement.pdp.EntitlementEngine in project carbon-identity-framework by wso2.

the class EntitlementEngineCache method get.

public EntitlementEngine get(int key) {
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        carbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        carbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        EntitlementEngine entitlementEngine = getEntitlementCache().get(key);
        if (entitlementEngine != null) {
            if (log.isDebugEnabled()) {
                log.debug("Cache : " + ENTITLEMENT_ENGINE_CACHE + "  is HIT " + "for tenantId : " + key);
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Cache : " + ENTITLEMENT_ENGINE_CACHE + "  is MISSED " + "for tenantId : " + key);
            }
        }
        return entitlementEngine;
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : EntitlementEngine(org.wso2.carbon.identity.entitlement.pdp.EntitlementEngine) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext)

Example 5 with EntitlementEngine

use of org.wso2.carbon.identity.entitlement.pdp.EntitlementEngine in project carbon-identity-framework by wso2.

the class EntitlementAdminService method doTestRequestForGivenPolicies.

/**
 * Tests engine of PAP policy store
 *
 * @param xacmlRequest
 * @param policies     policy ids that is evaluated
 * @return
 * @throws EntitlementException
 */
public String doTestRequestForGivenPolicies(String xacmlRequest, String[] policies) throws EntitlementException {
    EntitlementEngine engine = EntitlementEngine.getInstance();
    PAPPolicyFinder papPolicyFinder = (PAPPolicyFinder) engine.getPapPolicyFinder().getModules().iterator().next();
    papPolicyFinder.setPolicyIds(Arrays.asList(policies));
    String response = EntitlementEngine.getInstance().test(xacmlRequest);
    papPolicyFinder.initPolicyIds();
    return response;
}
Also used : EntitlementEngine(org.wso2.carbon.identity.entitlement.pdp.EntitlementEngine) PAPPolicyFinder(org.wso2.carbon.identity.entitlement.pap.store.PAPPolicyFinder)

Aggregations

EntitlementEngine (org.wso2.carbon.identity.entitlement.pdp.EntitlementEngine)7 Properties (java.util.Properties)3 ResponseCtx (org.wso2.balana.ctx.ResponseCtx)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Element (org.w3c.dom.Element)2 AbstractRequestCtx (org.wso2.balana.ctx.AbstractRequestCtx)2 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)2 PIPExtension (org.wso2.carbon.identity.entitlement.pip.PIPExtension)2 PolicyRequestBuilder (org.wso2.carbon.identity.entitlement.policy.PolicyRequestBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 CacheConfiguration (javax.cache.CacheConfiguration)1 CacheManager (javax.cache.CacheManager)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 RequestCtx (org.wso2.balana.ctx.xacml3.RequestCtx)1 PAPPolicyFinder (org.wso2.carbon.identity.entitlement.pap.store.PAPPolicyFinder)1