a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington/*
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * The contents of this file are subject to the terms
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * of the Common Development and Distribution License
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * (the License). You may not use this file except in
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * compliance with the License.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * You can obtain a copy of the License at
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * opensso/legal/CDDLv1.0.txt
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * See the License for the specific language governing
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * permission and limitations under the License.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * When distributing Covered Code, include this CDDL
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * Header Notice in each file and include the License file
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * at opensso/legal/CDDLv1.0.txt.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * If applicable, add the following below the CDDL Header,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * with the fields enclosed by brackets [] replaced by
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * your own identifying information:
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * $Id: DelegationIsAllowedSubResourceTest.java,v 1.3 2009/12/22 18:00:24 veiming Exp $
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell *
a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington * Portions Copyrighted 2014-2015 ForgeRock AS.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster */
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterpackage com.sun.identity.entitlement;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.iplanet.sso.SSOException;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.iplanet.sso.SSOToken;
ba07e74da87b2caf40d3397e50523632daeb4cacAndrew Forrestimport com.sun.identity.delegation.DelegationEvaluator;
abd4d5547d40141d956adbbd8ac2e0efd5f025e1Andrew Forrestimport com.sun.identity.delegation.DelegationEvaluatorImpl;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.delegation.DelegationPermission;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.opensso.OpenSSOUserSubject;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.opensso.SubjectUtils;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.util.AuthUtils;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.util.IdRepoUtils;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.idm.AMIdentity;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.idm.IdRepoException;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.security.AdminTokenAction;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.sm.SMSEntry;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.sm.SMSException;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.security.AccessController;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.Collections;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.HashMap;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.HashSet;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.Map;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.Set;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport javax.security.auth.Subject;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.testng.annotations.AfterClass;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.testng.annotations.BeforeClass;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.testng.annotations.Test;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster/**
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * @author dennis
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster */
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterpublic class DelegationIsAllowedSubResourceTest {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String APPL_NAME =
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "DelegationIsAllowedSubResourceTest";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String USER1 =
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "DelegationIsAllowedSubResourceTestUser1";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String PRIVILEGE_NAME =
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "DelegationIsAllowedSubResourceTestPrivilege";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String PRIVILEGE_NAME1 = PRIVILEGE_NAME + "1";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String DELEGATE_PRIVILEGE_NAME =
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "DelegationIsAllowedSubResourceTestDelegationPrivilege";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String DELEGATED_RESOURCE_BASE =
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "http://www.www.delegationisallowedsubresourcetest.com.com";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String DELEGATED_RESOURCE = DELEGATED_RESOURCE_BASE +
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "/user";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private SSOToken adminToken = (SSOToken) AccessController.doPrivileged(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster AdminTokenAction.getInstance());
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private Subject adminSubject = SubjectUtils.createSubject(adminToken);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private boolean migrated = EntitlementConfiguration.getInstance(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster adminSubject, "/").migratedToEntitlementService();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private AMIdentity user1;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @BeforeClass
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void setup()
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!migrated) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster return;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
1350b637af96e2390d247918800c02a7f531b47dJaco Jooste Application appl = new Application(APPL_NAME,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationTypeManager.getAppplicationType(adminSubject,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationTypeManager.URL_APPLICATION_TYPE_NAME));
74dca04245920444925c2544c591c3da5dad607eAndrew Forrest
74dca04245920444925c2544c591c3da5dad607eAndrew Forrest // Test disabled, unable to fix model change
74dca04245920444925c2544c591c3da5dad607eAndrew Forrest // Set<String> appResources = new HashSet<String>();
74dca04245920444925c2544c591c3da5dad607eAndrew Forrest // appResources.add(DELEGATED_RESOURCE_BASE);
74dca04245920444925c2544c591c3da5dad607eAndrew Forrest // appl.addResources(appResources);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster appl.setEntitlementCombiner(DenyOverride.class);
ba3008548cd047b233fcd32bb3c5d69926eed22fAndrew Forrest ApplicationManager.saveApplication(adminSubject, "/", appl);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster user1 = IdRepoUtils.createUser("/", USER1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster createDelegationPrivilege();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster createPrivilege();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @AfterClass
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void cleanup() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!migrated) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster return;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster PrivilegeManager pm = PrivilegeManager.getInstance("/", adminSubject);
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell pm.remove(PRIVILEGE_NAME1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationPrivilegeManager apm =
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationPrivilegeManager.getInstance("/", adminSubject);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster apm.removePrivilege(DELEGATE_PRIVILEGE_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster IdRepoUtils.deleteIdentity("/", user1);
ba3008548cd047b233fcd32bb3c5d69926eed22fAndrew Forrest ApplicationManager.deleteApplication(adminSubject, "/",
ba3008548cd047b233fcd32bb3c5d69926eed22fAndrew Forrest APPL_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private void createPrivilege() throws EntitlementException {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster PrivilegeManager pm = PrivilegeManager.getInstance("/", adminSubject);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Map<String, Boolean> actionValues = new HashMap<String, Boolean>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster actionValues.put("GET", Boolean.TRUE);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Entitlement entitlement = new Entitlement(APPL_NAME,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster DELEGATED_RESOURCE_BASE, actionValues);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster OpenSSOUserSubject subject = new OpenSSOUserSubject(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "id=isallowedtestdummy,ou=user," + SMSEntry.getRootSuffix());
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Privilege privilege1 = Privilege.getNewInstance();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster privilege1.setName(PRIVILEGE_NAME1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster privilege1.setEntitlement(entitlement);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster privilege1.setSubject(subject);
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell pm.add(privilege1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private void createDelegationPrivilege()
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throws SMSException, EntitlementException, SSOException,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster IdRepoException,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster InterruptedException {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationPrivilege ap = new ApplicationPrivilege(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster DELEGATE_PRIVILEGE_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster OpenSSOUserSubject sbj = new OpenSSOUserSubject();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster sbj.setID(user1.getUniversalId());
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<SubjectImplementation> subjects = new HashSet<SubjectImplementation>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster subjects.add(sbj);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ap.setSubject(subjects);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Map<String, Set<String>> appRes = new HashMap<String, Set<String>>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<String> res = new HashSet<String>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster appRes.put(APPL_NAME, res);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster res.add(DELEGATED_RESOURCE);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ap.setApplicationResources(appRes);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ap.setActionValues(ApplicationPrivilege.PossibleAction.READ);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationPrivilegeManager apm =
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationPrivilegeManager.getInstance("/", adminSubject);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster apm.addPrivilege(ap);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void test() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<String> actions = new HashSet<String>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster actions.add("READ");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster SSOToken token = AuthUtils.authenticate("/", USER1, USER1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster DelegationPermission dp = new DelegationPermission("/",
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "sunEntitlementService", "1.0", "application",
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "default/application/*",
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster actions, null);
ba07e74da87b2caf40d3397e50523632daeb4cacAndrew Forrest DelegationEvaluator de = new DelegationEvaluatorImpl();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!de.isAllowed(token, dp, Collections.EMPTY_MAP, true)) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "DelegationIsAllowedSubResourceTest.test: failed");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster}