Search in sources :

Example 16 with EntitlementSubject

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

the class PrivilegeUtils method policyToPrivileges.

public static void policyToPrivileges(Policy policy, Set<IPrivilege> privileges) throws SSOException, PolicyException, EntitlementException {
    String policyName = policy.getName();
    if (policy.isReferralPolicy()) {
        Map<String, Set<String>> resources = getResources(policy);
        Set<String> referredRealms = getReferrals(policy);
        ReferralPrivilege rp = new ReferralPrivilege(policyName, resources, referredRealms);
        rp.setDescription(policy.getDescription());
        rp.setCreationDate(policy.getCreationDate());
        rp.setCreatedBy(policy.getCreatedBy());
        rp.setLastModifiedBy(policy.getLastModifiedBy());
        rp.setLastModifiedDate(policy.getLastModifiedDate());
        rp.setActive(policy.isActive());
        privileges.add(rp);
    } else {
        Set<Entitlement> entitlements = rulesToEntitlement(policy);
        EntitlementSubject eSubject = toEntitlementSubject(policy);
        EntitlementCondition eCondition = toEntitlementCondition(policy);
        Set<ResourceAttribute> resourceAttributesSet = toResourceAttributes(policy);
        if (entitlements.size() == 1) {
            privileges.add(createPrivilege(policyName, policyName, entitlements.iterator().next(), eSubject, eCondition, resourceAttributesSet, policy));
        } else {
            for (Entitlement e : entitlements) {
                String pName = policyName + "_" + e.getName();
                privileges.add(createPrivilege(pName, policyName, e, eSubject, eCondition, resourceAttributesSet, policy));
            }
        }
    }
}
Also used : EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) HashSet(java.util.HashSet) Set(java.util.Set) ReferralPrivilege(com.sun.identity.entitlement.ReferralPrivilege) Entitlement(com.sun.identity.entitlement.Entitlement) ResourceAttribute(com.sun.identity.entitlement.ResourceAttribute)

Example 17 with EntitlementSubject

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

the class PolicyConditionUpgrader method migrateSubjectConditions.

private void migrateSubjectConditions(Privilege privilege, MigrationReport migrationReport) throws UpgradeException, EntitlementException {
    if (privilege.getSubject() == null) {
        return;
    }
    if (privilege.getSubject() instanceof NoSubject) {
        return;
    }
    if (privilege.getSubject() instanceof LogicalSubject) {
        LogicalSubject logicalSubject = (LogicalSubject) privilege.getSubject();
        Set<EntitlementSubject> subjects = logicalSubject.getESubjects();
        Set<EntitlementSubject> migratedSubjects = new HashSet<EntitlementSubject>();
        for (EntitlementSubject subject : subjects) {
            if (subject instanceof NoSubject) {
                //pass this through directly
                migratedSubjects.add(subject);
            } else if (!(subject instanceof PolicySubject)) {
                //This should never happen due to check in initialise
                throw new UpgradeException("Cannot upgrade a subject condition that is not of PolicySubject type!");
            } else {
                migratedSubjects.add(migrateSubjectCondition((PolicySubject) subject, migrationReport));
            }
        }
        logicalSubject.setESubjects(migratedSubjects);
    } else if (privilege.getSubject() instanceof PolicySubject) {
        privilege.setSubject(migrateSubjectCondition((PolicySubject) privilege.getSubject(), migrationReport));
    } else {
        //This should never happen due to check in initialise
        throw new UpgradeException("Cannot upgrade a subject condition that is not of PolicySubject type!");
    }
}
Also used : EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) UpgradeException(org.forgerock.openam.upgrade.UpgradeException) PolicySubject(com.sun.identity.entitlement.opensso.PolicySubject) NoSubject(com.sun.identity.entitlement.NoSubject) LogicalSubject(com.sun.identity.entitlement.LogicalSubject) HashSet(java.util.HashSet)

Example 18 with EntitlementSubject

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

the class PolicyConditionUpgraderTest method shouldMigratePolicyWithSingleSubjectAndEnvironmentCondition.

@Test
public void shouldMigratePolicyWithSingleSubjectAndEnvironmentCondition() throws EntitlementException, UpgradeException {
    //Given
    Privilege policy = mock(Privilege.class);
    PolicySubject subject = mock(PolicySubject.class);
    PolicyCondition condition = mock(PolicyCondition.class);
    EntitlementSubject migratedSubject = mock(EntitlementSubject.class);
    EntitlementCondition migratedCondition = mock(EntitlementCondition.class);
    given(policy.getSubject()).willReturn(subject);
    given(policy.getCondition()).willReturn(condition);
    given(subject.getClassName()).willReturn("SUBJECT_CLASS_NAME");
    given(condition.getClassName()).willReturn("CONDITION_CLASS_NAME");
    given(conditionUpgradeMap.migrateSubjectCondition(eq("SUBJECT_CLASS_NAME"), eq(subject), Matchers.<MigrationReport>anyObject())).willReturn(migratedSubject);
    given(conditionUpgradeMap.migrateEnvironmentCondition(eq("CONDITION_CLASS_NAME"), eq(condition), Matchers.<MigrationReport>anyObject())).willReturn(migratedCondition);
    //When
    conditionUpgrader.dryRunPolicyUpgrade(policy);
    //Then
    ArgumentCaptor<EntitlementSubject> subjectCaptor = ArgumentCaptor.forClass(EntitlementSubject.class);
    verify(policy).setSubject(subjectCaptor.capture());
    assertThat(subjectCaptor.getValue()).isEqualTo(migratedSubject);
    ArgumentCaptor<EntitlementCondition> conditionCaptor = ArgumentCaptor.forClass(EntitlementCondition.class);
    verify(policy).setCondition(conditionCaptor.capture());
    assertThat(conditionCaptor.getValue()).isEqualTo(migratedCondition);
}
Also used : EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) PolicySubject(com.sun.identity.entitlement.opensso.PolicySubject) EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) PolicyCondition(com.sun.identity.entitlement.opensso.PolicyCondition) Privilege(com.sun.identity.entitlement.Privilege) Test(org.testng.annotations.Test)

Example 19 with EntitlementSubject

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

the class PolicyConditionUpgraderTest method shouldMigratePolicyWithAndSubjectCondition.

@SuppressWarnings("unchecked")
@Test
public void shouldMigratePolicyWithAndSubjectCondition() throws EntitlementException, UpgradeException {
    //Given
    Privilege policy = mock(Privilege.class);
    AndSubject andSubject = mock(AndSubject.class);
    Set<EntitlementSubject> andSubjects = new HashSet<EntitlementSubject>();
    PolicySubject subject1 = mock(PolicySubject.class);
    PolicySubject subject2 = mock(PolicySubject.class);
    andSubjects.add(subject1);
    andSubjects.add(subject2);
    EntitlementSubject migratedSubject1 = mock(EntitlementSubject.class);
    EntitlementSubject migratedSubject2 = mock(EntitlementSubject.class);
    given(policy.getSubject()).willReturn(andSubject);
    given(andSubject.getESubjects()).willReturn(andSubjects);
    given(subject1.getClassName()).willReturn("SUBJECT1_CLASS_NAME");
    given(subject2.getClassName()).willReturn("SUBJECT2_CLASS_NAME");
    given(conditionUpgradeMap.migrateSubjectCondition(eq("SUBJECT1_CLASS_NAME"), eq(subject1), Matchers.<MigrationReport>anyObject())).willReturn(migratedSubject1);
    given(conditionUpgradeMap.migrateSubjectCondition(eq("SUBJECT2_CLASS_NAME"), eq(subject2), Matchers.<MigrationReport>anyObject())).willReturn(migratedSubject2);
    //When
    conditionUpgrader.dryRunPolicyUpgrade(policy);
    //Then
    ArgumentCaptor<Set> subjectCaptor = ArgumentCaptor.forClass(Set.class);
    verify(andSubject).setESubjects(subjectCaptor.capture());
    assertThat(subjectCaptor.getValue()).hasSize(2).contains(migratedSubject1, migratedSubject2);
    verify(policy, never()).setSubject(Matchers.<EntitlementSubject>anyObject());
    verify(policy, never()).setCondition(Matchers.<EntitlementCondition>anyObject());
}
Also used : AndSubject(com.sun.identity.entitlement.AndSubject) EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) PolicySubject(com.sun.identity.entitlement.opensso.PolicySubject) HashSet(java.util.HashSet) Set(java.util.Set) Privilege(com.sun.identity.entitlement.Privilege) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 20 with EntitlementSubject

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

the class PolicyConditionUpgraderTest method shouldMigratePolicyWithNotSubjectCondition.

@Test
public void shouldMigratePolicyWithNotSubjectCondition() throws EntitlementException, UpgradeException {
    //Given
    Privilege policy = mock(Privilege.class);
    NotSubject notSubject = mock(NotSubject.class);
    Set<EntitlementSubject> notSubjects = new HashSet<EntitlementSubject>();
    PolicySubject subject = mock(PolicySubject.class);
    notSubjects.add(subject);
    EntitlementSubject migratedSubject = mock(EntitlementSubject.class);
    given(policy.getSubject()).willReturn(notSubject);
    given(notSubject.getESubjects()).willReturn(notSubjects);
    given(subject.getClassName()).willReturn("SUBJECT_CLASS_NAME");
    given(conditionUpgradeMap.migrateSubjectCondition(eq("SUBJECT_CLASS_NAME"), eq(subject), Matchers.<MigrationReport>anyObject())).willReturn(migratedSubject);
    //When
    conditionUpgrader.dryRunPolicyUpgrade(policy);
    //Then
    ArgumentCaptor<Set> subjectCaptor = ArgumentCaptor.forClass(Set.class);
    verify(notSubject).setESubjects(subjectCaptor.capture());
    assertThat(subjectCaptor.getValue()).hasSize(1).contains(migratedSubject);
    verify(policy, never()).setSubject(Matchers.<EntitlementSubject>anyObject());
    verify(policy, never()).setCondition(Matchers.<EntitlementCondition>anyObject());
}
Also used : EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) PolicySubject(com.sun.identity.entitlement.opensso.PolicySubject) HashSet(java.util.HashSet) Set(java.util.Set) Privilege(com.sun.identity.entitlement.Privilege) NotSubject(com.sun.identity.entitlement.NotSubject) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

EntitlementSubject (com.sun.identity.entitlement.EntitlementSubject)30 Privilege (com.sun.identity.entitlement.Privilege)17 HashSet (java.util.HashSet)15 Entitlement (com.sun.identity.entitlement.Entitlement)13 Test (org.testng.annotations.Test)13 EntitlementCondition (com.sun.identity.entitlement.EntitlementCondition)11 OrSubject (com.sun.identity.entitlement.OrSubject)9 PolicySubject (com.sun.identity.entitlement.opensso.PolicySubject)9 HashMap (java.util.HashMap)6 PrivilegeManager (com.sun.identity.entitlement.PrivilegeManager)5 ResourceAttribute (com.sun.identity.entitlement.ResourceAttribute)5 Set (java.util.Set)5 AuthenticatedUsers (org.forgerock.openam.entitlement.conditions.subject.AuthenticatedUsers)4 AndCondition (com.sun.identity.entitlement.AndCondition)3 IPrivilege (com.sun.identity.entitlement.IPrivilege)3 OrCondition (com.sun.identity.entitlement.OrCondition)3 ReferralPrivilege (com.sun.identity.entitlement.ReferralPrivilege)3 Subject (com.sun.identity.policy.interfaces.Subject)3 PrivilegeSubject (com.sun.identity.policy.plugins.PrivilegeSubject)3 Cookie (javax.ws.rs.core.Cookie)3