use of com.sun.identity.entitlement.ConditionDecision in project OpenAM by OpenRock.
the class AMIdentityMembershipConditionTest method conditionShouldEvaluateToFalseWhenInvocatorPrincipalNotSet.
@Test
public void conditionShouldEvaluateToFalseWhenInvocatorPrincipalNotSet() throws EntitlementException {
//Given
String realm = "REALM";
Subject subject = new Subject();
String resourceName = "RESOURCE_NAME";
Map<String, Set<String>> env = new HashMap<String, Set<String>>();
condition.setState("{\"amIdentityName\": [\"IDENTITY_ONE\", \"IDENTITY_TWO\"]}");
//When
ConditionDecision decision = condition.evaluate(realm, subject, resourceName, env);
//Then
assertThat(decision.isSatisfied()).isFalse();
assertThat(decision.getAdvice()).isEmpty();
}
use of com.sun.identity.entitlement.ConditionDecision in project OpenAM by OpenRock.
the class AMIdentityMembershipConditionTest method conditionShouldEvaluateToTrueWhenUsingIdentitiesMatch.
@Test
public void conditionShouldEvaluateToTrueWhenUsingIdentitiesMatch() throws EntitlementException, IdRepoException, SSOException {
//Given
String realm = "REALM";
Subject subject = new Subject();
String resourceName = "RESOURCE_NAME";
Map<String, Set<String>> env = new HashMap<String, Set<String>>();
AMIdentity invocatorIdentity = mock(AMIdentity.class);
AMIdentity identity = invocatorIdentity;
env.put(INVOCATOR_PRINCIPAL_UUID, Collections.singleton("INVOCATOR_UUID"));
condition.setState("{\"amIdentityName\": [\"IDENTITY\"]}");
given(coreWrapper.getIdentity(adminToken, "INVOCATOR_UUID")).willReturn(invocatorIdentity);
given(coreWrapper.getIdentity(adminToken, "IDENTITY")).willReturn(identity);
//When
ConditionDecision decision = condition.evaluate(realm, subject, resourceName, env);
//Then
assertThat(decision.isSatisfied()).isTrue();
assertThat(decision.getAdvice()).isEmpty();
}
use of com.sun.identity.entitlement.ConditionDecision in project OpenAM by OpenRock.
the class AMIdentityMembershipConditionTest method conditionShouldEvaluateToFalseWhenInvocatorPrincipalIsEmpty.
@Test
public void conditionShouldEvaluateToFalseWhenInvocatorPrincipalIsEmpty() throws EntitlementException {
//Given
String realm = "REALM";
Subject subject = new Subject();
String resourceName = "RESOURCE_NAME";
Map<String, Set<String>> env = new HashMap<String, Set<String>>();
Set<String> invocatorUuids = new HashSet<String>();
env.put(INVOCATOR_PRINCIPAL_UUID, invocatorUuids);
condition.setState("{\"amIdentityName\": [\"IDENTITY_ONE\", \"IDENTITY_TWO\"]}");
//When
ConditionDecision decision = condition.evaluate(realm, subject, resourceName, env);
//Then
assertThat(decision.isSatisfied()).isFalse();
assertThat(decision.getAdvice()).isEmpty();
}
use of com.sun.identity.entitlement.ConditionDecision in project OpenAM by OpenRock.
the class PolicyCondition method evaluate.
/**
* Returns condition decision.
*
* @param realm Realm name.
* @param subject Subject to be evaluated.
* @param resourceName Resource name.
* @param environment Environment map.
* @return condition decision.
* @throws com.sun.identity.entitlement.EntitlementException if error occur.
*/
public ConditionDecision evaluate(String realm, Subject subject, String resourceName, Map<String, Set<String>> environment) throws EntitlementException {
try {
SSOToken token = (subject != null) ? getSSOToken(subject) : null;
Condition cond = getPolicyCondition();
com.sun.identity.policy.ConditionDecision dec = cond.getConditionDecision(token, environment);
return new ConditionDecision(dec.isAllowed(), dec.getAdvices(), dec.getTimeToLive());
} catch (SSOException ex) {
throw new EntitlementException(510, ex);
} catch (PolicyException ex) {
throw new EntitlementException(510, ex);
}
}
use of com.sun.identity.entitlement.ConditionDecision in project OpenAM by OpenRock.
the class CachingEntitlementCondition method evaluate.
/**
* First checks whether the backing condition has been evaluated already, if no, then evaluates the condition once
* and saves the result in the {@link PrivilegeEvaluatorContext#conditionDecisionCache}. If the result of the
* condition is already cached, then it returns the cached result.
* In case the context is not available for any reason, then the condition will be executed
*
* @param realm {@inheritDoc}
* @param subject {@inheritDoc}
* @param resourceName {@inheritDoc}
* @param environment {@inheritDoc}
* @return {@inheritDoc}
* @throws EntitlementException {@inheritDoc}
*/
public ConditionDecision evaluate(String realm, Subject subject, String resourceName, Map<String, Set<String>> environment) throws EntitlementException {
String classMethod = "CachingEntitlementCondition.evaluate() ";
PrivilegeEvaluatorContext context = PrivilegeEvaluatorContext.getCurrent();
if (context == null) {
DEBUG.warning(classMethod + "PrivilegeEvaluatorContext is not available, condition cache is discarded.");
return backingCondition.evaluate(realm, subject, resourceName, environment);
}
//context is shared across evaluator threads, so we can synchronize on it. Different privilege evaluations have
//different contexts as well.
final String cacheKey = backingCondition.getClass().getName() + getState();
synchronized (context) {
ConditionDecision cachedResult = context.getConditionDecisionCache().get(cacheKey);
if (cachedResult != null) {
if (DEBUG.messageEnabled()) {
DEBUG.message(classMethod + "returning cached condition decision");
}
return cachedResult;
}
ConditionDecision result = backingCondition.evaluate(realm, subject, resourceName, environment);
if (DEBUG.messageEnabled()) {
DEBUG.message(classMethod + "caching condition decision \"" + result.isSatisfied() + "\" for condition: " + cacheKey);
}
context.getConditionDecisionCache().put(cacheKey, result);
return result;
}
}
Aggregations