001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.tomcat;
017:
018: import java.io.File;
019: import java.net.URI;
020: import java.net.URL;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import javax.transaction.TransactionManager;
026:
027: import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator;
028: import org.apache.geronimo.connector.outbound.connectiontracking.GeronimoTransactionListener;
029: import org.apache.geronimo.security.SecurityServiceImpl;
030: import org.apache.geronimo.security.credentialstore.CredentialStore;
031: import org.apache.geronimo.security.deploy.PrincipalInfo;
032: import org.apache.geronimo.security.jaas.ConfigurationEntryFactory;
033: import org.apache.geronimo.security.jaas.GeronimoLoginConfiguration;
034: import org.apache.geronimo.security.jaas.JaasLoginModuleUse;
035: import org.apache.geronimo.security.jaas.LoginModuleControlFlag;
036: import org.apache.geronimo.security.jaas.LoginModuleGBean;
037: import org.apache.geronimo.security.jacc.ApplicationPolicyConfigurationManager;
038: import org.apache.geronimo.security.jacc.mappingprovider.ApplicationPrincipalRoleConfigurationManager;
039: import org.apache.geronimo.security.jacc.mappingprovider.GeronimoPolicyConfigurationFactory;
040: import org.apache.geronimo.security.jacc.mappingprovider.GeronimoPolicy;
041: import org.apache.geronimo.security.jacc.ComponentPermissions;
042: import org.apache.geronimo.security.jacc.PrincipalRoleMapper;
043: import org.apache.geronimo.security.jacc.RunAsSource;
044: import org.apache.geronimo.security.realm.GenericSecurityRealm;
045: import org.apache.geronimo.system.serverinfo.BasicServerInfo;
046: import org.apache.geronimo.system.serverinfo.ServerInfo;
047: import org.apache.geronimo.testsupport.TestSupport;
048: import org.apache.geronimo.tomcat.connector.ConnectorGBean;
049: import org.apache.geronimo.tomcat.connector.Http11ConnectorGBean;
050: import org.apache.geronimo.tomcat.util.SecurityHolder;
051: import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
052:
053: /**
054: * @version $Rev: 613094 $ $Date: 2008-01-18 00:00:10 -0800 (Fri, 18 Jan 2008) $
055: */
056: public abstract class AbstractWebModuleTest extends TestSupport {
057:
058: protected ClassLoader cl;
059: protected final static String securityRealmName = "demo-properties-realm";
060: protected ConnectorGBean connector;
061: protected TomcatContainer container;
062: protected static int port = 8181;
063: private TransactionManager transactionManager;
064: private ConnectionTrackingCoordinator connectionTrackingCoordinator;
065:
066: protected static final String POLICY_CONTEXT_ID = "securetest";
067: private GeronimoLoginConfiguration loginConfiguration;
068:
069: protected TomcatWebAppContext setUpInsecureAppContext(
070: URI relativeWebAppRoot, URL configurationBaseURL,
071: SecurityHolder securityHolder, RunAsSource runAsSource,
072: ObjectRetriever tomcatRealm, ValveGBean valveChain)
073: throws Exception {
074:
075: TomcatWebAppContext app = new TomcatWebAppContext(cl, null,
076: null, new URL(configurationBaseURL, relativeWebAppRoot
077: .getPath()), securityHolder, null,
078: Collections.EMPTY_MAP, null, null, transactionManager,
079: connectionTrackingCoordinator, container, runAsSource,
080: tomcatRealm, null, valveChain, null, null, null, false,
081: null, false, null, null, null, null, null, null);
082: app.setContextPath("/test");
083: app.doStart();
084: return app;
085: }
086:
087: protected TomcatWebAppContext setUpSecureAppContext(
088: Map roleDesignates, Map principalRoleMap,
089: ComponentPermissions componentPermissions,
090: RealmGBean realm, SecurityHolder securityHolder,
091: CredentialStore credentialStore) throws Exception {
092: PrincipalRoleMapper roleMapper = new ApplicationPrincipalRoleConfigurationManager(
093: principalRoleMap);
094: Map contextIDToPermissionsMap = new HashMap();
095: contextIDToPermissionsMap.put(POLICY_CONTEXT_ID,
096: componentPermissions);
097: ApplicationPolicyConfigurationManager jacc = new ApplicationPolicyConfigurationManager(
098: contextIDToPermissionsMap, null, roleDesignates, cl,
099: credentialStore, roleMapper);
100: jacc.doStart();
101:
102: URL configurationBaseURL = new File(BASEDIR,
103: "src/test/resources/deployables/war3/WEB-INF/web.xml")
104: .toURL();
105: return setUpInsecureAppContext(new File(BASEDIR,
106: "src/test/resources/deployables/war3/").toURI(),
107: configurationBaseURL, securityHolder, jacc, realm, null);
108: }
109:
110: protected void setUpSecurity() throws Exception {
111: String domainName = "demo-properties-realm";
112:
113: ServerInfo serverInfo = new BasicServerInfo(".");
114:
115: new SecurityServiceImpl(cl, serverInfo,
116: GeronimoPolicyConfigurationFactory.class.getName(),
117: GeronimoPolicy.class.getName(), null, null, null, null);
118:
119: Map<String, Object> options = new HashMap<String, Object>();
120: options.put("usersURI", new File(BASEDIR,
121: "src/test/resources/data/users.properties").toURI()
122: .toString());
123: options.put("groupsURI", new File(BASEDIR,
124: "src/test/resources/data/groups.properties").toURI()
125: .toString());
126:
127: LoginModuleGBean loginModule = new LoginModuleGBean(
128: "org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule",
129: null, true, options, domainName, cl);
130:
131: JaasLoginModuleUse loginModuleUse = new JaasLoginModuleUse(
132: loginModule, null, LoginModuleControlFlag.REQUIRED);
133:
134: PrincipalInfo.PrincipalEditor principalEditor = new PrincipalInfo.PrincipalEditor();
135: principalEditor
136: .setAsText("metro,org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal");
137: GenericSecurityRealm realm = new GenericSecurityRealm(
138: domainName, loginModuleUse, true, serverInfo, cl, null);
139:
140: loginConfiguration = new GeronimoLoginConfiguration();
141: loginConfiguration.setConfigurations(Collections
142: .<ConfigurationEntryFactory> singleton(realm));
143: loginConfiguration.doStart();
144:
145: }
146:
147: protected void tearDownSecurity() throws Exception {
148: loginConfiguration.doStop();
149: }
150:
151: protected void init(String realmClass) throws Exception {
152: cl = this .getClass().getClassLoader();
153:
154: RealmGBean realm = null;
155: if (realmClass != null) {
156: Map initParams = new HashMap();
157: initParams
158: .put("userClassNames",
159: "org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal");
160: initParams
161: .put("roleClassNames",
162: "org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal");
163: realm = new RealmGBean(realmClass, initParams);
164: realm.doStart();
165: }
166:
167: //Default Host
168: Map initParams = new HashMap();
169: initParams.put("workDir", "work");
170: initParams.put("name", "localhost");
171: initParams.put("appBase", "");
172:
173: HostGBean host = new HostGBean(
174: "org.apache.catalina.core.StandardHost", initParams,
175: null, realm, null, null, null, null);
176: //Default Engine
177: initParams = Collections.singletonMap("name", "Geronimo");
178:
179: EngineGBean engine = new EngineGBean(
180: "org.apache.geronimo.tomcat.TomcatEngine", initParams,
181: host, Collections.singleton(host), realm, null, null,
182: null, null, null);
183: engine.doStart();
184:
185: ServerInfo serverInfo = new BasicServerInfo(".");
186: container = new TomcatContainer(cl, new File(BASEDIR,
187: "target/var/catalina").toString(), null, engine, null,
188: serverInfo, null, null);
189: container.doStart();
190:
191: connector = new Http11ConnectorGBean("HTTP", null, "localhost",
192: port++, container, serverInfo);
193: connector.doStart();
194:
195: TransactionManagerImpl transactionManager = new TransactionManagerImpl();
196: this .transactionManager = transactionManager;
197: connectionTrackingCoordinator = new ConnectionTrackingCoordinator();
198: transactionManager
199: .addTransactionAssociationListener(new GeronimoTransactionListener(
200: connectionTrackingCoordinator));
201: }
202:
203: protected void tearDown() throws Exception {
204: connector.doStop();
205: super.tearDown();
206: }
207: }
|