1 17 package org.apache.geronimo.tomcat; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.net.HttpURLConnection ; 23 import java.net.URL ; 24 import java.security.PermissionCollection ; 25 import java.security.Permissions ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import javax.management.ObjectName ; 33 import javax.security.jacc.WebResourcePermission ; 34 import javax.security.jacc.WebUserDataPermission ; 35 36 import org.apache.geronimo.security.deploy.DefaultPrincipal; 37 import org.apache.geronimo.security.deploy.PrincipalInfo; 38 import org.apache.geronimo.security.deploy.Role; 39 import org.apache.geronimo.security.deploy.Security; 40 import org.apache.geronimo.security.deployment.GeronimoSecurityBuilderImpl; 41 import org.apache.geronimo.security.jacc.ComponentPermissions; 42 import org.apache.geronimo.tomcat.util.SecurityHolder; 43 44 45 50 public class JACCSecurityTest extends AbstractWebModuleTest { 51 52 ObjectName appName = null; 53 54 59 public void testExplicitMapping() throws Exception { 60 61 Security securityConfig = new Security(); 62 securityConfig.setUseContextHandler(false); 63 64 DefaultPrincipal defaultPrincipal = new DefaultPrincipal(); 65 PrincipalInfo principalInfo = new PrincipalInfo("org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal", "izumi", false); 66 defaultPrincipal.setPrincipal(principalInfo); 67 68 securityConfig.setDefaultPrincipal(defaultPrincipal); 69 70 Role role = new Role(); 71 role.setRoleName("content-administrator"); 72 principalInfo = new PrincipalInfo("org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal", "it", false); 73 role.getPrincipals().add(principalInfo); 74 75 securityConfig.getRoleMappings().put(role.getRoleName(), role); 76 77 Map roleDesignates = new HashMap (); 78 Map principalRoleMap = new HashMap (); 79 buildPrincipalRoleMap(securityConfig, roleDesignates, principalRoleMap); 80 81 PermissionCollection uncheckedPermissions = new Permissions (); 82 83 PermissionCollection excludedPermissions = new Permissions (); 84 excludedPermissions.add(new WebResourcePermission ("/auth/login.html", "")); 85 excludedPermissions.add(new WebUserDataPermission ("/auth/login.html", "")); 86 87 Map rolePermissions = new HashMap (); 88 PermissionCollection permissions = new Permissions (); 89 permissions.add(new WebUserDataPermission ("/protected/*", "")); 90 permissions.add(new WebResourcePermission ("/protected/*", "")); 91 rolePermissions.put("content-administrator", permissions); 92 rolePermissions.put("auto-administrator", permissions); 93 94 ComponentPermissions componentPermissions = new ComponentPermissions(excludedPermissions, uncheckedPermissions, rolePermissions); 95 96 startWebApp(roleDesignates, principalRoleMap, componentPermissions, 97 defaultPrincipal, permissions); 98 99 HttpURLConnection connection = (HttpURLConnection ) new URL ("http://localhost:8181/test/protected/hello.txt").openConnection(); 101 connection.setInstanceFollowRedirects(false); 102 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 103 104 BufferedReader reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 106 assertEquals("<!-- Login Page -->", reader.readLine()); 107 reader.close(); 108 109 String cookie = connection.getHeaderField("Set-Cookie"); 110 cookie = cookie.substring(0, cookie.lastIndexOf(';')); 111 String location = "http://localhost:8181/test/protected/j_security_check?j_username=alan&j_password=starcraft"; 112 113 connection = (HttpURLConnection ) new URL (location).openConnection(); 114 connection.setRequestMethod("POST"); 115 connection.setRequestProperty("Referer", "http://localhost:8181/test/auth/logon.html?param=test"); 116 connection.setRequestProperty("Cookie", cookie); 117 connection.setInstanceFollowRedirects(false); 118 assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode()); 119 120 connection = (HttpURLConnection ) new URL ("http://localhost:8181/test/protected/hello.txt").openConnection(); 121 connection.setRequestProperty("Cookie", cookie); 122 connection.setInstanceFollowRedirects(false); 123 reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 124 125 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 126 assertEquals("Hello World", reader.readLine()); 127 connection.disconnect(); 128 129 connection = (HttpURLConnection ) new URL ("http://localhost:8181/test/protected/hello.txt").openConnection(); 131 connection.setInstanceFollowRedirects(false); 132 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 133 134 cookie = connection.getHeaderField("Set-Cookie"); 135 cookie = cookie.substring(0, cookie.lastIndexOf(';')); 136 137 reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 139 assertEquals("<!-- Login Page -->", reader.readLine()); 140 reader.close(); 141 142 location = "http://localhost:8181/test/protected/j_security_check?j_username=izumi&j_password=violin"; 143 144 connection = (HttpURLConnection ) new URL (location).openConnection(); 145 connection.setRequestMethod("POST"); 146 connection.setRequestProperty("Cookie", cookie); 147 connection.setInstanceFollowRedirects(false); 148 assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode()); 149 150 try { 151 connection = (HttpURLConnection ) new URL ("http://localhost:8181/test/protected/hello.txt").openConnection(); 152 connection.setRequestProperty("Cookie", cookie); 153 connection.setInstanceFollowRedirects(false); 154 reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 155 156 fail("Should throw an IOException for HTTP 403 response"); 157 } catch (IOException e) { 158 } 159 160 assertEquals(HttpURLConnection.HTTP_FORBIDDEN, connection.getResponseCode()); 161 connection.disconnect(); 162 163 164 stopWebApp(); 165 } 166 167 protected TomcatWebAppContext startWebApp( 168 Map roleDesignates, 169 Map principalRoleMap, 170 ComponentPermissions componentPermissions, 171 DefaultPrincipal defaultPrincipal, 172 PermissionCollection checked) throws Exception { 173 174 SecurityHolder securityHolder = new SecurityHolder(); 175 securityHolder.setSecurity(true); 176 securityHolder.setChecked(checked); 177 securityHolder.setExcluded(componentPermissions.getExcludedPermissions()); 178 securityHolder.setPolicyContextID(POLICY_CONTEXT_ID); 179 securityHolder.setDefaultPrincipal(defaultPrincipal); 180 securityHolder.setSecurityRealm(securityRealmName); 181 return setUpSecureAppContext(roleDesignates, 182 principalRoleMap, 183 componentPermissions, 184 null, 185 securityHolder); 186 } 187 188 protected void stopWebApp() throws Exception { 189 } 190 191 public void buildPrincipalRoleMap(Security security, Map roleDesignates, Map principalRoleMap) { 192 Map roleToPrincipalMap = new HashMap (); 193 GeronimoSecurityBuilderImpl.buildRolePrincipalMap(security, roleDesignates, roleToPrincipalMap, getClass().getClassLoader()); 194 invertMap(roleToPrincipalMap, principalRoleMap); 195 } 196 197 private static Map invertMap(Map roleToPrincipalMap, Map principalRoleMapping) { 198 for (Iterator roles = roleToPrincipalMap.entrySet().iterator(); roles.hasNext();) { 199 Map.Entry entry = (Map.Entry ) roles.next(); 200 String role = (String ) entry.getKey(); 201 Set principals = (Set ) entry.getValue(); 202 for (Iterator iter = principals.iterator(); iter.hasNext();) { 203 java.security.Principal principal = (java.security.Principal ) iter.next(); 204 205 HashSet roleSet = (HashSet ) principalRoleMapping.get(principal); 206 if (roleSet == null) { 207 roleSet = new HashSet (); 208 principalRoleMapping.put(principal, roleSet); 209 } 210 roleSet.add(role); 211 } 212 } 213 return principalRoleMapping; 214 } 215 216 protected void setUp() throws Exception { 217 super.setUp(); 218 super.init("org.apache.geronimo.tomcat.realm.TomcatGeronimoRealm"); 219 setUpSecurity(); 220 } 221 222 protected void tearDown() throws Exception { 223 tearDownSecurity(); 224 super.tearDown(); 225 } 226 227 } 228 | Popular Tags |