Search in sources :

Example 1 with EntitlementCondition

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

the class PrivilegeUtils method privilegeToPolicy.

public static Policy privilegeToPolicy(String realm, Privilege privilege) throws PolicyException, SSOException, EntitlementException {
    Policy policy = new Policy(privilege.getName());
    policy.setDescription(privilege.getDescription());
    if (privilege.getEntitlement() != null) {
        Entitlement entitlement = privilege.getEntitlement();
        Set<Rule> rules = entitlementToRule(realm, entitlement);
        for (Rule rule : rules) {
            policy.addRule(rule);
        }
    }
    EntitlementSubject es = privilege.getSubject();
    if ((es != null) && (es != Privilege.NOT_SUBJECT)) {
        Subject sbj = eSubjectToEPSubject(es);
        policy.addSubject(getSubjectName(es), sbj, false);
    }
    EntitlementCondition ec = privilege.getCondition();
    if (ec != null) {
        Condition cond = eConditionToEPCondition(ec);
        policy.addCondition(getConditionName(ec), cond);
    }
    if (privilege.getResourceAttributes() != null) {
        Map<String, ResponseProvider> nrps = resourceAttributesToResponseProviders(privilege.getResourceAttributes());
        for (String rpName : nrps.keySet()) {
            ResponseProvider responseProvider = nrps.get(rpName);
            policy.addResponseProvider(rpName, responseProvider);
        }
    }
    policy.setCreatedBy(privilege.getCreatedBy());
    policy.setCreationDate(privilege.getCreationDate());
    policy.setLastModifiedBy(privilege.getLastModifiedBy());
    policy.setLastModifiedDate(privilege.getLastModifiedDate());
    return policy;
}
Also used : Policy(com.sun.identity.policy.Policy) EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) OrCondition(com.sun.identity.entitlement.OrCondition) AndCondition(com.sun.identity.entitlement.AndCondition) PrivilegeCondition(com.sun.identity.policy.plugins.PrivilegeCondition) Condition(com.sun.identity.policy.interfaces.Condition) EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) IDRepoResponseProvider(com.sun.identity.policy.plugins.IDRepoResponseProvider) ResponseProvider(com.sun.identity.policy.interfaces.ResponseProvider) Rule(com.sun.identity.policy.Rule) Entitlement(com.sun.identity.entitlement.Entitlement) EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) PrivilegeSubject(com.sun.identity.policy.plugins.PrivilegeSubject) Subject(com.sun.identity.policy.interfaces.Subject) OrSubject(com.sun.identity.entitlement.OrSubject)

Example 2 with EntitlementCondition

use of com.sun.identity.entitlement.EntitlementCondition 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 3 with EntitlementCondition

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

the class XACMLPrivilegeUtils method createEntitlementCondition.

static EntitlementCondition createEntitlementCondition(String dataType, String value) throws EntitlementException {
    if (dataType == null || value == null) {
        return null;
    }
    EntitlementCondition ec = null;
    int i = dataType.indexOf(XACMLConstants.JSON_CONDITION_DATATYPE);
    if (i != 0) {
        return null;
    }
    String className = null;
    if (dataType.length() > XACMLConstants.JSON_CONDITION_DATATYPE.length()) {
        className = dataType.substring(XACMLConstants.JSON_CONDITION_DATATYPE.length() + 1);
        Object ob = createDefaultObject(className);
        if (ob != null && ob instanceof EntitlementCondition) {
            ec = (EntitlementCondition) ob;
        } else {
            PrivilegeManager.debug.error("XACMLPrivilegeUtils." + "createEntitlementCondition()" + "not an EntitlementCondition");
        }
    }
    if (ec != null) {
        ec.setState(value);
        ec.validate();
    }
    return ec;
}
Also used : EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) JSONObject(org.json.JSONObject)

Example 4 with EntitlementCondition

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

the class PolicyConditionUpgrader method migrateEnvironmentConditions.

private void migrateEnvironmentConditions(Privilege privilege, MigrationReport migrationReport) throws UpgradeException, EntitlementException {
    if (privilege.getCondition() == null) {
        return;
    }
    if (privilege.getCondition() instanceof LogicalCondition) {
        LogicalCondition logicalCondition = (LogicalCondition) privilege.getCondition();
        Set<EntitlementCondition> conditions = logicalCondition.getEConditions();
        Set<EntitlementCondition> migratedConditions = new HashSet<EntitlementCondition>();
        for (EntitlementCondition condition : conditions) {
            if (!(condition instanceof PolicyCondition)) {
                //This should never happen due to check in initialise
                throw new UpgradeException("Cannot upgrade a environment condition that is not of PolicyCondition type!");
            }
            migratedConditions.add(migrateEnvironmentCondition((PolicyCondition) condition, migrationReport));
        }
        logicalCondition.setEConditions(migratedConditions);
    } else if (privilege.getCondition() instanceof PolicyCondition) {
        privilege.setCondition(migrateEnvironmentCondition((PolicyCondition) privilege.getCondition(), migrationReport));
    } else {
        //This should never happen due to check in initialise
        throw new UpgradeException("Cannot upgrade a environment condition that is not of PolicyCondition type!");
    }
}
Also used : UpgradeException(org.forgerock.openam.upgrade.UpgradeException) EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) PolicyCondition(com.sun.identity.entitlement.opensso.PolicyCondition) LogicalCondition(com.sun.identity.entitlement.LogicalCondition) HashSet(java.util.HashSet)

Example 5 with EntitlementCondition

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

the class PolicyConditionUpgraderTest method shouldMigratePolicyWithNotEnvironmentCondition.

@Test
public void shouldMigratePolicyWithNotEnvironmentCondition() throws EntitlementException, UpgradeException {
    //Given
    Privilege policy = mock(Privilege.class);
    NotCondition notCondition = mock(NotCondition.class);
    Set<EntitlementCondition> notConditions = new HashSet<EntitlementCondition>();
    PolicyCondition condition = mock(PolicyCondition.class);
    notConditions.add(condition);
    EntitlementCondition migratedCondition = mock(EntitlementCondition.class);
    given(policy.getCondition()).willReturn(notCondition);
    given(notCondition.getEConditions()).willReturn(notConditions);
    given(condition.getClassName()).willReturn("CONDITION_CLASS_NAME");
    given(conditionUpgradeMap.migrateEnvironmentCondition(eq("CONDITION_CLASS_NAME"), eq(condition), Matchers.<MigrationReport>anyObject())).willReturn(migratedCondition);
    //When
    conditionUpgrader.dryRunPolicyUpgrade(policy);
    //Then
    ArgumentCaptor<Set> conditionCaptor = ArgumentCaptor.forClass(Set.class);
    verify(notCondition).setEConditions(conditionCaptor.capture());
    assertThat(conditionCaptor.getValue()).hasSize(1).contains(migratedCondition);
    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) NotCondition(com.sun.identity.entitlement.NotCondition) PolicyCondition(com.sun.identity.entitlement.opensso.PolicyCondition) Privilege(com.sun.identity.entitlement.Privilege) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

EntitlementCondition (com.sun.identity.entitlement.EntitlementCondition)28 HashSet (java.util.HashSet)17 Test (org.testng.annotations.Test)14 Privilege (com.sun.identity.entitlement.Privilege)13 PolicyCondition (com.sun.identity.entitlement.opensso.PolicyCondition)10 Set (java.util.Set)10 AndCondition (com.sun.identity.entitlement.AndCondition)9 EntitlementSubject (com.sun.identity.entitlement.EntitlementSubject)9 OrCondition (com.sun.identity.entitlement.OrCondition)8 Entitlement (com.sun.identity.entitlement.Entitlement)7 HashMap (java.util.HashMap)6 ResourceAttribute (com.sun.identity.entitlement.ResourceAttribute)5 OrSubject (com.sun.identity.entitlement.OrSubject)4 NotCondition (com.sun.identity.entitlement.NotCondition)3 ReferralPrivilege (com.sun.identity.entitlement.ReferralPrivilege)3 Condition (com.sun.identity.policy.interfaces.Condition)3 PrivilegeCondition (com.sun.identity.policy.plugins.PrivilegeCondition)3 IPrivilege (com.sun.identity.entitlement.IPrivilege)2 LogicalCondition (com.sun.identity.entitlement.LogicalCondition)2 NumericAttributeCondition (com.sun.identity.entitlement.NumericAttributeCondition)2