Search in sources :

Example 1 with OrCondition

use of com.sun.identity.entitlement.OrCondition in project OpenAM by OpenRock.

the class PrivilegeUtils method nConditionsToECondition.

private static EntitlementCondition nConditionsToECondition(Set nConditons) throws EntitlementException {
    Set<EntitlementCondition> ecSet = new HashSet<EntitlementCondition>();
    for (Object nConditionObj : nConditons) {
        Object[] nCondition = (Object[]) nConditionObj;
        EntitlementCondition ec = mapGenericCondition(nCondition);
        ecSet.add(ec);
    }
    if (ecSet.isEmpty()) {
        return null;
    }
    if (ecSet.size() == 1) {
        return ecSet.iterator().next();
    }
    Map<String, Set<EntitlementCondition>> cnEntcMap = new HashMap<String, Set<EntitlementCondition>>();
    for (EntitlementCondition ec : ecSet) {
        String key = (ec instanceof PolicyCondition) ? ((PolicyCondition) ec).getClassName() : ec.getClass().getName();
        Set<EntitlementCondition> values = cnEntcMap.get(key);
        if (values == null) {
            values = new HashSet<EntitlementCondition>();
            cnEntcMap.put(key, values);
        }
        values.add(ec);
    }
    Set<String> keySet = cnEntcMap.keySet();
    if (keySet.size() == 1) {
        Set<EntitlementCondition> values = cnEntcMap.get(keySet.iterator().next());
        return (values.size() == 1) ? values.iterator().next() : new OrCondition(values);
    }
    Set andSet = new HashSet();
    for (String key : keySet) {
        Set values = (Set) cnEntcMap.get(key);
        if (values.size() == 1) {
            andSet.add(values.iterator().next());
        } else {
            andSet.add(new OrCondition(values));
        }
    }
    return new AndCondition(andSet);
}
Also used : EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) AndCondition(com.sun.identity.entitlement.AndCondition) OrCondition(com.sun.identity.entitlement.OrCondition) HashSet(java.util.HashSet)

Example 2 with OrCondition

use of com.sun.identity.entitlement.OrCondition in project OpenAM by OpenRock.

the class PolicyConditionUpgraderTest method isPolicyWithOrEnvironmentConditionUpgradable.

@Test(dataProvider = "isPolicyWithOrEnvironmentConditionUpgradableDataProvider")
public void isPolicyWithOrEnvironmentConditionUpgradable(Class<? extends EntitlementCondition> con1, boolean con1InMap, Class<? extends EntitlementCondition> con2, boolean con2InMap, boolean expectedResult) {
    //Given
    Privilege policy = mock(Privilege.class);
    OrCondition orCondition = mock(OrCondition.class);
    Set<EntitlementCondition> orConditions = new HashSet<EntitlementCondition>();
    EntitlementCondition condition1 = mock(con1);
    EntitlementCondition condition2 = mock(con2);
    orConditions.add(condition1);
    orConditions.add(condition2);
    given(policy.getCondition()).willReturn(orCondition);
    given(orCondition.getEConditions()).willReturn(orConditions);
    if (condition1 instanceof PolicyCondition) {
        given(((PolicyCondition) condition1).getClassName()).willReturn("CONDITION1_CLASS_NAME");
    }
    if (condition2 instanceof PolicyCondition) {
        given(((PolicyCondition) condition2).getClassName()).willReturn("CONDITION2_CLASS_NAME");
    }
    given(conditionUpgradeMap.containsEnvironmentCondition("CONDITION1_CLASS_NAME")).willReturn(con1InMap);
    given(conditionUpgradeMap.containsEnvironmentCondition("CONDITION2_CLASS_NAME")).willReturn(con2InMap);
    //When
    boolean upgradable = conditionUpgrader.isPolicyUpgradable(policy);
    //Then
    assertThat(upgradable).isEqualTo(expectedResult);
}
Also used : EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) OrCondition(com.sun.identity.entitlement.OrCondition) PolicyCondition(com.sun.identity.entitlement.opensso.PolicyCondition) Privilege(com.sun.identity.entitlement.Privilege) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 3 with OrCondition

use of com.sun.identity.entitlement.OrCondition in project OpenAM by OpenRock.

the class PolicyConditionUpgraderTest method shouldMigratePolicyWithOrEnvironmentCondition.

@SuppressWarnings("unchecked")
@Test
public void shouldMigratePolicyWithOrEnvironmentCondition() throws EntitlementException, UpgradeException {
    //Given
    Privilege policy = mock(Privilege.class);
    OrCondition orCondition = mock(OrCondition.class);
    Set<EntitlementCondition> orConditions = new HashSet<EntitlementCondition>();
    PolicyCondition condition1 = mock(PolicyCondition.class);
    PolicyCondition condition2 = mock(PolicyCondition.class);
    orConditions.add(condition1);
    orConditions.add(condition2);
    EntitlementCondition migratedCondition1 = mock(EntitlementCondition.class);
    EntitlementCondition migratedCondition2 = mock(EntitlementCondition.class);
    given(policy.getCondition()).willReturn(orCondition);
    given(orCondition.getEConditions()).willReturn(orConditions);
    given(condition1.getClassName()).willReturn("CONDITION1_CLASS_NAME");
    given(condition2.getClassName()).willReturn("CONDITION2_CLASS_NAME");
    given(conditionUpgradeMap.migrateEnvironmentCondition(eq("CONDITION1_CLASS_NAME"), eq(condition1), Matchers.<MigrationReport>anyObject())).willReturn(migratedCondition1);
    given(conditionUpgradeMap.migrateEnvironmentCondition(eq("CONDITION2_CLASS_NAME"), eq(condition2), Matchers.<MigrationReport>anyObject())).willReturn(migratedCondition2);
    //When
    conditionUpgrader.dryRunPolicyUpgrade(policy);
    //Then
    ArgumentCaptor<Set> conditionsCaptor = ArgumentCaptor.forClass(Set.class);
    verify(orCondition).setEConditions(conditionsCaptor.capture());
    assertThat(conditionsCaptor.getValue()).hasSize(2).contains(migratedCondition1, migratedCondition2);
    verify(policy, never()).setSubject(Matchers.<EntitlementSubject>anyObject());
    verify(policy, never()).setCondition(Matchers.<EntitlementCondition>anyObject());
}
Also used : EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) HashSet(java.util.HashSet) Set(java.util.Set) OrCondition(com.sun.identity.entitlement.OrCondition) PolicyCondition(com.sun.identity.entitlement.opensso.PolicyCondition) Privilege(com.sun.identity.entitlement.Privilege) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with OrCondition

use of com.sun.identity.entitlement.OrCondition in project OpenAM by OpenRock.

the class JsonPolicyParserTest method shouldParseNestedOrConditions.

@Test
public void shouldParseNestedOrConditions() throws Exception {
    // Given
    // An OR condition containing a single OAuth2Scope condition
    String scope = "givenName";
    JsonValue content = buildJson(field("condition", object(field("type", "OR"), field("conditions", Collections.singletonList(object(field("type", "OAuth2Scope"), field("requiredScopes", array(scope))))))));
    // When
    Privilege result = parser.parsePolicy(POLICY_NAME, content);
    // Then
    assertThat(result.getCondition()).isInstanceOf(OrCondition.class);
    OrCondition or = (OrCondition) result.getCondition();
    assertThat(or.getEConditions()).hasSize(1);
    assertThat(or.getEConditions().iterator().next()).isInstanceOf(OAuth2ScopeCondition.class);
    OAuth2ScopeCondition oauth2Scope = (OAuth2ScopeCondition) or.getEConditions().iterator().next();
    assertThat(oauth2Scope.getRequiredScopes()).isEqualTo(Collections.singleton(scope));
}
Also used : OAuth2ScopeCondition(org.forgerock.openam.entitlement.conditions.environment.OAuth2ScopeCondition) JsonValue(org.forgerock.json.JsonValue) OrCondition(com.sun.identity.entitlement.OrCondition) OpenSSOPrivilege(com.sun.identity.entitlement.opensso.OpenSSOPrivilege) Privilege(com.sun.identity.entitlement.Privilege) Test(org.testng.annotations.Test)

Example 5 with OrCondition

use of com.sun.identity.entitlement.OrCondition in project OpenAM by OpenRock.

the class PrivilegeUtilsTest method testPrivilegeToXACMLPolicy.

@Test
public void testPrivilegeToXACMLPolicy() throws Exception {
    try {
        UnittestLog.logMessage("PrivilegeUtils.testPrivilegeToXACMLPolicy():" + " entered");
        Map<String, Boolean> actionValues = new HashMap<String, Boolean>();
        actionValues.put("GET", Boolean.TRUE);
        actionValues.put("POST", Boolean.FALSE);
        // The port is required for passing equals  test
        // opensso policy would add default port if port not specified
        String resourceName = "http://www.sun.com:80";
        Entitlement entitlement = new Entitlement(SERVICE_NAME, resourceName, actionValues);
        entitlement.setName("ent1");
        String user11 = "id=user11,ou=user," + ServiceManager.getBaseDN();
        String user12 = "id=user12,ou=user," + ServiceManager.getBaseDN();
        UserSubject ua1 = new OpenSSOUserSubject();
        ua1.setID(user11);
        UserSubject ua2 = new OpenSSOUserSubject();
        ua2.setID(user12);
        Set<EntitlementSubject> subjects = new HashSet<EntitlementSubject>();
        subjects.add(ua1);
        subjects.add(ua2);
        OrSubject os = new OrSubject(subjects);
        Set<EntitlementCondition> conditions = new HashSet<EntitlementCondition>();
        String startIp = "100.100.100.100";
        String endIp = "200.200.200.200";
        IPv4Condition ipc = new IPv4Condition();
        ipc.setStartIpAndEndIp(startIp, endIp);
        conditions.add(ipc);
        OrCondition oc = new OrCondition(conditions);
        AndCondition ac = new AndCondition(conditions);
        StaticAttributes sa1 = new StaticAttributes();
        Set<String> aValues = new HashSet<String>();
        aValues.add("a10");
        aValues.add("a20");
        sa1.setPropertyName("a");
        sa1.setPropertyValues(aValues);
        sa1.setPResponseProviderName("sa");
        StaticAttributes sa2 = new StaticAttributes();
        Set<String> bValues = new HashSet<String>();
        bValues.add("b10");
        bValues.add("b20");
        sa2.setPropertyName("b");
        sa2.setPropertyValues(bValues);
        sa2.setPResponseProviderName("sa");
        UserAttributes uat1 = new UserAttributes();
        uat1.setPropertyName("email");
        uat1.setPResponseProviderName("ua");
        UserAttributes uat2 = new UserAttributes();
        uat2.setPropertyName("uid");
        uat2.setPResponseProviderName("ua");
        Set<ResourceAttribute> ra = new HashSet<ResourceAttribute>();
        ra.add(sa1);
        ra.add(sa2);
        ra.add(uat1);
        ra.add(uat2);
        Privilege privilege = Privilege.getNewInstance();
        privilege.setName(PRIVILEGE_NAME);
        privilege.setEntitlement(entitlement);
        privilege.setSubject(ua1);
        privilege.setCondition(ipc);
        privilege.setResourceAttributes(ra);
        privilege.setCreatedBy("amadmin");
        privilege.setLastModifiedBy("amadmin");
        privilege.setCreationDate(System.currentTimeMillis());
        privilege.setLastModifiedDate(System.currentTimeMillis());
        UnittestLog.logMessage("PrivilegeUtils.testPrivilegeToXACMLPolicy():" + "Privilege=" + privilege.toString());
        UnittestLog.logMessage("PrivilegeUtils.testPrivilegeToXACMLPolicy():" + "converting to xacml policy");
        // TODO(jtb): not compiling
        String xacmlString = XACMLPrivilegeUtils.toXACML(privilege);
        UnittestLog.logMessage("xacml policy=" + xacmlString);
    } catch (Throwable t) {
        UnittestLog.logError("Throwable:", t);
        UnittestLog.logMessage("Throwable:" + t.getMessage());
        t.printStackTrace();
    }
}
Also used : EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) HashMap(java.util.HashMap) IPv4Condition(org.forgerock.openam.entitlement.conditions.environment.IPv4Condition) OpenSSOUserSubject(com.sun.identity.entitlement.opensso.OpenSSOUserSubject) StaticAttributes(com.sun.identity.entitlement.StaticAttributes) OrSubject(com.sun.identity.entitlement.OrSubject) AndCondition(com.sun.identity.entitlement.AndCondition) UserAttributes(com.sun.identity.entitlement.UserAttributes) EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) OpenSSOUserSubject(com.sun.identity.entitlement.opensso.OpenSSOUserSubject) UserSubject(com.sun.identity.entitlement.UserSubject) OrCondition(com.sun.identity.entitlement.OrCondition) Entitlement(com.sun.identity.entitlement.Entitlement) ResourceAttribute(com.sun.identity.entitlement.ResourceAttribute) Privilege(com.sun.identity.entitlement.Privilege) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

OrCondition (com.sun.identity.entitlement.OrCondition)6 EntitlementCondition (com.sun.identity.entitlement.EntitlementCondition)5 HashSet (java.util.HashSet)5 Test (org.testng.annotations.Test)5 Privilege (com.sun.identity.entitlement.Privilege)4 Set (java.util.Set)3 AndCondition (com.sun.identity.entitlement.AndCondition)2 EntitlementSubject (com.sun.identity.entitlement.EntitlementSubject)2 PolicyCondition (com.sun.identity.entitlement.opensso.PolicyCondition)2 HashMap (java.util.HashMap)2 Entitlement (com.sun.identity.entitlement.Entitlement)1 IPrivilege (com.sun.identity.entitlement.IPrivilege)1 OrSubject (com.sun.identity.entitlement.OrSubject)1 ResourceAttribute (com.sun.identity.entitlement.ResourceAttribute)1 StaticAttributes (com.sun.identity.entitlement.StaticAttributes)1 UserAttributes (com.sun.identity.entitlement.UserAttributes)1 UserSubject (com.sun.identity.entitlement.UserSubject)1 OpenSSOPrivilege (com.sun.identity.entitlement.opensso.OpenSSOPrivilege)1 OpenSSOUserSubject (com.sun.identity.entitlement.opensso.OpenSSOUserSubject)1 PolicyException (com.sun.identity.policy.PolicyException)1