1 17 package org.apache.geronimo.jetty; 18 19 import java.io.File ; 20 import java.net.URI ; 21 import java.security.PermissionCollection ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.Set ; 28 import javax.management.ObjectName ; 29 30 import junit.framework.TestCase; 31 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator; 32 import org.apache.geronimo.gbean.GBeanData; 33 import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContext; 34 import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContextImpl; 35 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 36 import org.apache.geronimo.jetty.connector.HTTPConnector; 37 import org.apache.geronimo.kernel.KernelFactory; 38 import org.apache.geronimo.kernel.Kernel; 39 import org.apache.geronimo.kernel.management.State; 40 import org.apache.geronimo.security.SecurityServiceImpl; 41 import org.apache.geronimo.security.jacc.ComponentPermissions; 42 import org.apache.geronimo.security.jacc.ApplicationPolicyConfigurationManager; 43 import org.apache.geronimo.security.deploy.Principal; 44 import org.apache.geronimo.security.deploy.DefaultPrincipal; 45 import org.apache.geronimo.security.jaas.GeronimoLoginConfiguration; 46 import org.apache.geronimo.security.jaas.JaasLoginService; 47 import org.apache.geronimo.security.jaas.LoginModuleGBean; 48 import org.apache.geronimo.security.jaas.JaasLoginModuleUse; 49 import org.apache.geronimo.security.realm.GenericSecurityRealm; 50 import org.apache.geronimo.system.serverinfo.ServerInfo; 51 import org.apache.geronimo.transaction.context.OnlineUserTransaction; 52 import org.apache.geronimo.transaction.context.TransactionContextManager; 53 import org.apache.geronimo.transaction.manager.TransactionManagerImpl; 54 import org.mortbay.jetty.servlet.FormAuthenticator; 55 56 57 60 public class AbstractWebModuleTest extends TestCase { 61 protected Kernel kernel; 62 private GBeanData container; 63 private ObjectName containerName; 64 private ObjectName connectorName; 65 private GBeanData connector; 66 private ObjectName webModuleName; 67 private ObjectName tmName; 68 private ObjectName ctcName; 69 private GBeanData tm; 70 private GBeanData ctc; 71 private ObjectName tcmName; 72 private GBeanData tcm; 73 private ClassLoader cl; 74 private J2eeContext moduleContext = new J2eeContextImpl("jetty.test", "test", "null", NameFactory.WEB_MODULE, "jettyTest", null, null); 75 private GBeanData loginConfigurationGBean; 76 protected ObjectName loginConfigurationName; 77 private GBeanData securityServiceGBean; 78 protected ObjectName securityServiceName; 79 private ObjectName loginServiceName; 80 private GBeanData loginServiceGBean; 81 protected GBeanData propertiesLMGBean; 82 protected ObjectName propertiesLMName; 83 protected ObjectName propertiesRealmName; 84 private GBeanData propertiesRealmGBean; 85 private ObjectName serverInfoName; 86 private GBeanData serverInfoGBean; 87 protected final static String securityRealmName = "demo-properties-realm"; 88 89 public void testDummy() throws Exception { 90 } 91 92 protected void setUpStaticContentServlet() throws Exception { 93 GBeanData staticContentServletGBeanData = new GBeanData(JettyServletHolder.GBEAN_INFO); 94 staticContentServletGBeanData.setAttribute("servletName", "default"); 95 staticContentServletGBeanData.setAttribute("servletClass", "org.mortbay.jetty.servlet.Default"); 96 Map staticContentServletInitParams = new HashMap (); 97 staticContentServletInitParams.put("acceptRanges", "true"); 98 staticContentServletInitParams.put("dirAllowed", "true"); 99 staticContentServletInitParams.put("putAllowed", "false"); 100 staticContentServletInitParams.put("delAllowed", "false"); 101 staticContentServletInitParams.put("redirectWelcome", "false"); 102 staticContentServletInitParams.put("minGzipLength", "8192"); 103 staticContentServletGBeanData.setAttribute("initParams", staticContentServletInitParams); 104 staticContentServletGBeanData.setAttribute("loadOnStartup", new Integer (0)); 105 staticContentServletGBeanData.setAttribute("servletMappings", Collections.singleton(new String ("/"))); 106 ObjectName staticContentServletObjectName = NameFactory.getComponentName(null, null, null, NameFactory.WEB_MODULE, null, (String ) staticContentServletGBeanData.getAttribute("servletName"), NameFactory.SERVLET, moduleContext); 107 staticContentServletGBeanData.setName(staticContentServletObjectName); 108 staticContentServletGBeanData.setReferencePattern("JettyServletRegistration", webModuleName); 109 110 start(staticContentServletGBeanData); 111 } 112 113 protected void setUpInsecureAppContext() throws Exception { 114 GBeanData app = new GBeanData(webModuleName, JettyWebAppContext.GBEAN_INFO); 115 app.setAttribute("uri", URI.create("war1/")); 116 app.setAttribute("componentContext", Collections.EMPTY_MAP); 117 OnlineUserTransaction userTransaction = new OnlineUserTransaction(); 118 app.setAttribute("userTransaction", userTransaction); 119 app.setAttribute("webClassPath", new URI []{}); 121 app.setAttribute("contextPriorityClassLoader", Boolean.FALSE); 122 app.setAttribute("configurationBaseUrl", new File ("src/test-resources/deployables/").toURL()); 123 app.setReferencePattern("TransactionContextManager", tcmName); 124 app.setReferencePattern("TrackedConnectionAssociator", ctcName); 125 app.setReferencePattern("JettyContainer", containerName); 126 127 app.setAttribute("contextPath", "/test"); 128 129 start(app); 130 } 131 132 protected void setUpSecureAppContext(Map roleDesignates, Map principalRoleMap, ComponentPermissions componentPermissions, DefaultPrincipal defaultPrincipal, PermissionCollection checked, Set securityRoles) throws Exception { 133 ObjectName jaccBeanName = NameFactory.getComponentName(null, null, null, null, "foo", NameFactory.JACC_MANAGER, moduleContext); 134 GBeanData jaccBeanData = new GBeanData(jaccBeanName, ApplicationPolicyConfigurationManager.GBEAN_INFO); 135 Map contextIDToPermissionsMap = new HashMap (); 136 contextIDToPermissionsMap.put("TEST", componentPermissions); 137 jaccBeanData.setAttribute("contextIdToPermissionsMap", contextIDToPermissionsMap); 138 jaccBeanData.setAttribute("principalRoleMap", principalRoleMap); 139 jaccBeanData.setAttribute("roleDesignates", roleDesignates); 140 start(jaccBeanData); 141 142 GBeanData app = new GBeanData(webModuleName, JettyWebAppContext.GBEAN_INFO); 143 app.setAttribute("securityRealmName", "demo-properties-realm"); 144 app.setAttribute("defaultPrincipal", defaultPrincipal); 145 app.setAttribute("checkedPermissions", checked); 146 app.setAttribute("excludedPermissions", componentPermissions.getExcludedPermissions()); 147 app.setReferencePattern("RoleDesignateSource", jaccBeanName); 148 149 FormAuthenticator formAuthenticator = new FormAuthenticator(); 150 formAuthenticator.setLoginPage("/auth/logon.html?param=test"); 151 formAuthenticator.setErrorPage("/auth/logonError.html?param=test"); 152 app.setAttribute("realmName", "Test JAAS Realm"); 153 app.setAttribute("authenticator", formAuthenticator); 154 app.setAttribute("policyContextID", "TEST"); 155 app.setAttribute("uri", URI.create("war3/")); 156 app.setAttribute("componentContext", Collections.EMPTY_MAP); 157 158 OnlineUserTransaction userTransaction = new OnlineUserTransaction(); 159 app.setAttribute("userTransaction", userTransaction); 160 app.setAttribute("webClassPath", new URI []{}); 162 app.setAttribute("contextPriorityClassLoader", Boolean.FALSE); 163 app.setAttribute("configurationBaseUrl", new File ("src/test-resources/deployables/").toURL()); 164 app.setReferencePattern("TransactionContextManager", tcmName); 165 app.setReferencePattern("TrackedConnectionAssociator", ctcName); 166 app.setReferencePattern("JettyContainer", containerName); 167 168 app.setAttribute("contextPath", "/test"); 169 170 start(app); 171 } 172 173 protected void setUpSecurity() throws Exception { 174 175 loginConfigurationName = new ObjectName ("geronimo.security:type=LoginConfiguration"); 176 loginConfigurationGBean = new GBeanData(loginConfigurationName, GeronimoLoginConfiguration.getGBeanInfo()); 177 Set configurations = new HashSet (); 178 configurations.add(new ObjectName ("geronimo.server:j2eeType=SecurityRealm,*")); 179 configurations.add(new ObjectName ("geronimo.server:j2eeType=ConfigurationEntry,*")); 180 loginConfigurationGBean.setReferencePatterns("Configurations", configurations); 181 182 serverInfoName = new ObjectName ("geronimo.system:name=ServerInfo"); 183 serverInfoGBean = new GBeanData(serverInfoName, ServerInfo.GBEAN_INFO); 184 serverInfoGBean.setAttribute("baseDirectory", "."); 185 186 securityServiceName = new ObjectName ("geronimo.server:j2eeType=SecurityService"); 187 securityServiceGBean = new GBeanData(securityServiceName, SecurityServiceImpl.GBEAN_INFO); 188 securityServiceGBean.setReferencePattern("ServerInfo", serverInfoName); 189 securityServiceGBean.setAttribute("policyConfigurationFactory", "org.apache.geronimo.security.jacc.GeronimoPolicyConfigurationFactory"); 190 securityServiceGBean.setAttribute("policyProvider", "org.apache.geronimo.security.jacc.GeronimoPolicy"); 191 192 loginServiceName = JaasLoginService.OBJECT_NAME; 193 loginServiceGBean = new GBeanData(loginServiceName, JaasLoginService.GBEAN_INFO); 194 loginServiceGBean.setReferencePattern("Realms", new ObjectName ("geronimo.server:j2eeType=SecurityRealm,*")); 195 loginServiceGBean.setAttribute("algorithm", "HmacSHA1"); 197 loginServiceGBean.setAttribute("password", "secret"); 198 199 propertiesLMName = new ObjectName ("geronimo.security:type=LoginModule,name=demo-properties-login"); 200 propertiesLMGBean = new GBeanData(propertiesLMName, LoginModuleGBean.GBEAN_INFO); 201 propertiesLMGBean.setAttribute("loginModuleClass", "org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule"); 202 propertiesLMGBean.setAttribute("serverSide", Boolean.TRUE); 203 Properties options = new Properties (); 204 options.setProperty("usersURI", "src/test-resources/data/users.properties"); 205 options.setProperty("groupsURI", "src/test-resources/data/groups.properties"); 206 propertiesLMGBean.setAttribute("options", options); 207 propertiesLMGBean.setAttribute("loginDomainName", "demo-properties-realm"); 209 210 ObjectName testUseName = new ObjectName ("geronimo.security:type=LoginModuleUse,name=properties"); 211 GBeanData lmUseGBean = new GBeanData(testUseName, JaasLoginModuleUse.getGBeanInfo()); 212 lmUseGBean.setAttribute("controlFlag", "REQUIRED"); 213 lmUseGBean.setReferencePattern("LoginModule", propertiesLMName); 214 215 propertiesRealmName = new ObjectName ("geronimo.server:j2eeType=SecurityRealm,name=demo-properties-realm"); 216 propertiesRealmGBean = new GBeanData(propertiesRealmName, GenericSecurityRealm.GBEAN_INFO); 217 propertiesRealmGBean.setReferencePattern("ServerInfo", serverInfoName); 218 propertiesRealmGBean.setAttribute("realmName", "demo-properties-realm"); 219 propertiesRealmGBean.setReferencePattern("LoginModuleConfiguration", testUseName); 223 Principal.PrincipalEditor principalEditor = new Principal.PrincipalEditor(); 224 principalEditor.setAsText("metro=org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal"); 225 propertiesRealmGBean.setAttribute("defaultPrincipal", principalEditor.getValue()); 226 227 start(loginConfigurationGBean); 228 start(serverInfoGBean); 229 start(securityServiceGBean); 230 start(loginServiceGBean); 231 start(propertiesLMGBean); 232 start(lmUseGBean); 233 start(propertiesRealmGBean); 234 235 } 236 237 protected void tearDownSecurity() throws Exception { 238 stop(propertiesRealmName); 239 stop(propertiesLMName); 240 stop(loginServiceName); 241 stop(securityServiceName); 242 stop(serverInfoName); 243 stop(loginConfigurationName); 244 } 245 246 private void start(GBeanData gbeanData) throws Exception { 247 kernel.loadGBean(gbeanData, cl); 248 kernel.startGBean(gbeanData.getName()); 249 if (kernel.getGBeanState(gbeanData.getName()) != State.RUNNING_INDEX) { 250 fail("gbean not started: " + gbeanData.getName()); 251 } 252 } 253 254 private void stop(ObjectName name) throws Exception { 255 kernel.stopGBean(name); 256 kernel.unloadGBean(name); 257 } 258 259 protected void setUp() throws Exception { 260 cl = this.getClass().getClassLoader(); 261 containerName = NameFactory.getWebComponentName(null, null, null, null, "jettyContainer", "WebResource", moduleContext); 262 connectorName = NameFactory.getWebComponentName(null, null, null, null, "jettyConnector", "WebResource", moduleContext); 263 webModuleName = NameFactory.getModuleName(null, null, null, null, "testModule", moduleContext); 264 265 tmName = NameFactory.getComponentName(null, null, null, null, null, "TransactionManager", NameFactory.JTA_RESOURCE, moduleContext); 266 tcmName = NameFactory.getComponentName(null, null, null, null, null, "TransactionContextManager", NameFactory.JTA_RESOURCE, moduleContext); 267 ctcName = new ObjectName ("geronimo.test:role=ConnectionTrackingCoordinator"); 268 269 kernel = KernelFactory.newInstance().createKernel("test.kernel"); 270 kernel.boot(); 271 container = new GBeanData(containerName, JettyContainerImpl.GBEAN_INFO); 272 273 connector = new GBeanData(connectorName, HTTPConnector.GBEAN_INFO); 274 connector.setAttribute("port", new Integer (5678)); 275 connector.setReferencePattern("JettyContainer", containerName); 276 277 start(container); 278 start(connector); 279 280 tm = new GBeanData(tmName, TransactionManagerImpl.GBEAN_INFO); 281 Set patterns = new HashSet (); 282 patterns.add(ObjectName.getInstance("geronimo.server:j2eeType=JCAManagedConnectionFactory,*")); 283 tm.setAttribute("defaultTransactionTimeoutSeconds", new Integer (10)); 284 tm.setReferencePatterns("ResourceManagers", patterns); 285 start(tm); 286 tcm = new GBeanData(tcmName, TransactionContextManager.GBEAN_INFO); 287 tcm.setReferencePattern("TransactionManager", tmName); 288 start(tcm); 289 ctc = new GBeanData(ctcName, ConnectionTrackingCoordinator.GBEAN_INFO); 290 start(ctc); 291 } 292 293 protected void tearDown() throws Exception { 294 stop(ctcName); 295 stop(tmName); 296 stop(connectorName); 297 stop(containerName); 298 kernel.shutdown(); 299 } 300 } 301 | Popular Tags |