1 17 18 package org.apache.geronimo.security.network.protocol; 19 20 import org.activeio.AcceptListener; 24 import org.activeio.AsyncChannelServer; 25 import org.activeio.Channel; 26 import org.activeio.Packet; 27 import org.activeio.RequestChannel; 28 import org.activeio.RequestListener; 29 import org.activeio.adapter.AsyncChannelToClientRequestChannel; 30 import org.activeio.adapter.AsyncChannelToServerRequestChannel; 31 import org.activeio.adapter.AsyncToSyncChannel; 32 import org.activeio.adapter.SyncToAsyncChannel; 33 import org.activeio.adapter.SyncToAsyncChannelServer; 34 import org.activeio.filter.PacketAggregatingAsyncChannel; 35 import org.activeio.net.SocketSyncChannelFactory; 36 import org.activeio.packet.ByteArrayPacket; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.apache.geronimo.gbean.AbstractName; 40 import org.apache.geronimo.gbean.GBeanData; 41 import org.apache.geronimo.security.AbstractTest; 42 import org.apache.geronimo.security.jaas.JaasLoginModuleUse; 43 import org.apache.geronimo.security.jaas.LoginModuleGBean; 44 import org.apache.geronimo.security.realm.GenericSecurityRealm; 45 46 import javax.security.auth.Subject ; 47 import javax.security.auth.login.Configuration ; 48 import javax.security.auth.login.LoginContext ; 49 import java.io.File ; 50 import java.io.IOException ; 51 import java.net.URI ; 52 import java.security.AccessController ; 53 import java.security.PrivilegedExceptionAction ; 54 import java.util.Properties ; 55 56 57 60 public class SubjectCarryingProtocolTest extends AbstractTest implements RequestListener { 61 62 final static private Log log = LogFactory.getLog(SubjectCarryingProtocolTest.class); 63 64 protected AbstractName testCE; 65 protected AbstractName testRealm; 66 67 private Subject clientSubject; 68 private Subject serverSubject; 69 private URI serverURI; 70 private AsyncChannelServer server; 71 72 public void testNothing() throws Exception { 73 } 74 75 78 public void disabledtest() throws Exception { 79 80 SocketSyncChannelFactory factory = new SocketSyncChannelFactory(); 81 final RequestChannel channel = 82 new AsyncChannelToClientRequestChannel( 83 AsyncToSyncChannel.adapt( 84 new SubjectCarryingChannel( 85 new PacketAggregatingAsyncChannel( 86 SyncToAsyncChannel.adapt( 87 factory.openSyncChannel(serverURI)))))); 88 try { 89 channel.start(); 90 Subject.doAs(clientSubject, new PrivilegedExceptionAction () { 91 public Object run() throws Exception { 92 93 Subject subject = Subject.getSubject(AccessController.getContext()); 94 String p = subject.getPrincipals().iterator().next().toString(); 95 log.info("Sending request as: "+p); 96 97 Packet request = new ByteArrayPacket("whoami".getBytes()); 98 Packet response = channel.request(request, 1000*5*1000); 99 100 assertNotNull(response); 101 assertEquals( p, new String (response.sliceAsBytes()) ); 102 return null; 103 } 104 }); 105 } finally { 106 channel.dispose(); 107 } 108 } 109 110 111 public void setUp() throws Exception { 112 needServerInfo = true; 113 super.setUp(); 114 115 GBeanData gbean; 116 117 gbean = buildGBeanData ("name", "PropertiesLoginModule", LoginModuleGBean.getGBeanInfo()); 118 testCE = gbean.getAbstractName(); 119 gbean.setAttribute("loginModuleClass", "org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule"); 120 gbean.setAttribute("serverSide", new Boolean (true)); 121 Properties props = new Properties (); 122 props.put("usersURI", new File (BASEDIR, "src/test/data/data/users.properties").toURI().toString()); 123 props.put("groupsURI", new File (BASEDIR, "src/test/data/data/groups.properties").toURI().toString()); 124 gbean.setAttribute("options", props); 125 gbean.setAttribute("loginDomainName", "PropertiesDomain"); 126 kernel.loadGBean(gbean, LoginModuleGBean.class.getClassLoader()); 127 128 gbean = buildGBeanData("name", "PropertiesLoginModuleUse", JaasLoginModuleUse.getGBeanInfo()); 129 AbstractName testUseName = gbean.getAbstractName(); 130 gbean.setAttribute("controlFlag", "REQUIRED"); 131 gbean.setReferencePattern("LoginModule", testCE); 132 kernel.loadGBean(gbean, JaasLoginModuleUse.class.getClassLoader()); 133 134 gbean = buildGBeanData("name", "PropertiesSecurityRealm", GenericSecurityRealm.getGBeanInfo()); 135 testRealm = gbean.getAbstractName(); 136 gbean.setAttribute("realmName", "properties-realm"); 137 gbean.setReferencePattern("LoginModuleConfiguration", testUseName); 138 gbean.setReferencePattern("ServerInfo", serverInfo); 139 kernel.loadGBean(gbean, GenericSecurityRealm.class.getClassLoader()); 140 141 kernel.startGBean(testCE); 142 kernel.startGBean(testUseName); 143 kernel.startGBean(testRealm); 144 145 LoginContext context = new LoginContext ("properties", new AbstractTest.UsernamePasswordCallback("alan", "starcraft")); 146 context.login(); 147 clientSubject = context.getSubject(); 148 149 context = new LoginContext ("properties", new AbstractTest.UsernamePasswordCallback("izumi", "violin")); 150 context.login(); 151 serverSubject = context.getSubject(); 152 153 SocketSyncChannelFactory factory = new SocketSyncChannelFactory(); 154 server = new SyncToAsyncChannelServer( 155 factory.bindSyncChannel(new URI ("tcp://localhost:0"))); 156 157 server.setAcceptListener(new AcceptListener() { 158 public void onAccept(Channel channel) { 159 RequestChannel requestChannel=null; 160 try { 161 162 requestChannel = 163 new AsyncChannelToServerRequestChannel( 164 new SubjectCarryingChannel( 165 new PacketAggregatingAsyncChannel( 166 SyncToAsyncChannel.adapt(channel)))); 167 168 requestChannel.setRequestListener(SubjectCarryingProtocolTest.this); 169 requestChannel.start(); 170 171 } catch (IOException e) { 172 log.info("Failed to accept connection.", e); 173 if( requestChannel!=null ) 174 requestChannel.dispose(); 175 else 176 channel.dispose(); 177 } 178 } 179 public void onAcceptError(IOException error) { 180 log.info("Accept Failed: "+error); 181 } 182 }); 183 184 server.start(); 185 serverURI = server.getConnectURI(); 186 187 } 188 189 public void tearDown() throws Exception { 190 server.dispose(); 191 192 kernel.stopGBean(testRealm); 193 kernel.stopGBean(testCE); 194 kernel.stopGBean(serverInfo); 195 kernel.unloadGBean(testCE); 196 kernel.unloadGBean(testRealm); 197 kernel.unloadGBean(serverInfo); 198 super.tearDown(); 199 } 201 202 public Packet onRequest(Packet packet) { 203 204 String p=""; 205 try { 206 SubjectContext ctx = (SubjectContext)packet.narrow(SubjectContext.class); 207 Subject subject = ctx.getSubject(); 208 p = subject.getPrincipals().iterator().next().toString(); 209 log.info("Received request as: "+p); 210 } catch ( Exception e ) { 211 e.printStackTrace(); 212 } 213 return new ByteArrayPacket(p.getBytes()); 214 } 215 216 public void onRquestError(IOException arg) { 217 } 218 219 220 } 221 | Popular Tags |