Home » geronimo-2.2-source-release » org.apache.geronimo.security.network.protocol » [javadoc | source]

    1   /**
    2    *
    3    * Copyright 2004 The Apache Software Foundation
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package org.apache.geronimo.security.network.protocol;
   19   
   20   import java.io.File;
   21   import java.io.IOException;
   22   import java.net.URI;
   23   import java.security.AccessController;
   24   import java.security.PrivilegedExceptionAction;
   25   import java.util.Collections;
   26   import java.util.Properties;
   27   
   28   import javax.management.ObjectName;
   29   import javax.security.auth.Subject;
   30   import javax.security.auth.login.Configuration;
   31   import javax.security.auth.login.LoginContext;
   32   
   33   import org.activeio.AcceptListener;
   34   import org.activeio.AsynchChannelServer;
   35   import org.activeio.Channel;
   36   import org.activeio.Packet;
   37   import org.activeio.RequestChannel;
   38   import org.activeio.RequestListener;
   39   import org.activeio.adapter.AsynchChannelToClientRequestChannel;
   40   import org.activeio.adapter.AsynchChannelToServerRequestChannel;
   41   import org.activeio.adapter.AsynchToSynchChannelAdapter;
   42   import org.activeio.adapter.SynchToAsynchChannelAdapter;
   43   import org.activeio.adapter.SynchToAsynchChannelServerAdapter;
   44   import org.activeio.filter.PacketAggregatingAsynchChannel;
   45   import org.activeio.net.SocketSynchChannelFactory;
   46   import org.activeio.packet.ByteArrayPacket;
   47   import org.apache.commons.logging.Log;
   48   import org.apache.commons.logging.LogFactory;
   49   import org.apache.geronimo.gbean.GBeanData;
   50   import org.apache.geronimo.security.AbstractTest;
   51   import org.apache.geronimo.security.jaas.LoginModuleGBean;
   52   import org.apache.geronimo.security.jaas.JaasLoginModuleUse;
   53   import org.apache.geronimo.security.realm.GenericSecurityRealm;
   54   import org.apache.geronimo.system.serverinfo.ServerInfo;
   55   
   56   import com.sun.security.auth.login.ConfigFile;
   57   
   58   
   59   /**
   60    * @version $Rev: 165344 $ $Date: 2005-04-29 14:19:43 -0700 (Fri, 29 Apr 2005) $
   61    */
   62   public class SubjectCarryingProtocolTest extends AbstractTest implements RequestListener {
   63   
   64       final static private Log log = LogFactory.getLog(SubjectCarryingProtocolTest.class);
   65   
   66       protected ObjectName serverInfo;
   67       protected ObjectName testCE;
   68       protected ObjectName testRealm;
   69   
   70       private Subject clientSubject;
   71       private Subject serverSubject;
   72       private URI serverURI;
   73       private AsynchChannelServer server;
   74   
   75       public void testNothing() throws Exception {
   76       }
   77   
   78       /*
   79        * Enable this test again once its working.
   80        */
   81       public void disabledtest() throws Exception {
   82   
   83           SocketSynchChannelFactory factory = new SocketSynchChannelFactory();
   84           final RequestChannel channel =
   85               new AsynchChannelToClientRequestChannel(
   86                   AsynchToSynchChannelAdapter.adapt(
   87                       new SubjectCarryingChannel(
   88                           new PacketAggregatingAsynchChannel(
   89                               SynchToAsynchChannelAdapter.adapt(
   90                                    factory.openSynchChannel(serverURI))))));
   91           try {
   92               channel.start();
   93   	        Subject.doAs(clientSubject, new PrivilegedExceptionAction() {
   94   	            public Object run() throws Exception {
   95   
   96   	                Subject subject = Subject.getSubject(AccessController.getContext());
   97   	                String p = subject.getPrincipals().iterator().next().toString();
   98   	                log.info("Sending request as: "+p);
   99   
  100                       Packet request = new ByteArrayPacket("whoami".getBytes());
  101                       Packet response = channel.request(request, 1000*5*1000);
  102   
  103                       assertNotNull(response);
  104                       assertEquals( p, new String(response.sliceAsBytes()) );
  105                       return null;
  106   	            }
  107   	        });
  108           } finally {
  109               channel.dispose();
  110           }
  111       }
  112   
  113   
  114       public void setUp() throws Exception {
  115           super.setUp();
  116   
  117           GBeanData gbean;
  118   
  119           serverInfo = new ObjectName("geronimo.system:role=ServerInfo");
  120           gbean = new GBeanData(serverInfo, ServerInfo.GBEAN_INFO);
  121           gbean.setAttribute("baseDirectory", ".");
  122           kernel.loadGBean(gbean, ServerInfo.class.getClassLoader());
  123           kernel.startGBean(serverInfo);
  124   
  125           testCE = new ObjectName("geronimo.security:type=LoginModule,name=properties");
  126           gbean = new GBeanData(testCE, LoginModuleGBean.getGBeanInfo());
  127           gbean.setAttribute("loginModuleClass", "org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule");
  128           gbean.setAttribute("serverSide", new Boolean(true));
  129           Properties props = new Properties();
  130           props.put("usersURI", new File(new File("."), "src/test-data/data/users.properties").toURI().toString());
  131           props.put("groupsURI", new File(new File("."), "src/test-data/data/groups.properties").toURI().toString());
  132           gbean.setAttribute("options", props);
  133           gbean.setAttribute("loginDomainName", "PropertiesDomain");
  134           kernel.loadGBean(gbean, LoginModuleGBean.class.getClassLoader());
  135   
  136           ObjectName testUseName = new ObjectName("geronimo.security:type=LoginModuleUse,name=properties");
  137           gbean = new GBeanData(testUseName, JaasLoginModuleUse.getGBeanInfo());
  138           gbean.setAttribute("controlFlag", "REQUIRED");
  139           gbean.setReferencePattern("LoginModule", testCE);
  140           kernel.loadGBean(gbean, JaasLoginModuleUse.class.getClassLoader());
  141   
  142           testRealm = new ObjectName("geronimo.security:type=SecurityRealm,realm=properties-realm");
  143           gbean = new GBeanData(testRealm, GenericSecurityRealm.getGBeanInfo());
  144           gbean.setAttribute("realmName", "properties-realm");
  145   //        props = new Properties();
  146   //        props.setProperty("LoginModule.1.REQUIRED","geronimo.security:type=LoginModule,name=properties");
  147   //        gbean.setAttribute("loginModuleConfiguration", props);
  148           gbean.setReferencePattern("LoginModuleConfiguration", testUseName);
  149           gbean.setReferencePatterns("ServerInfo", Collections.singleton(serverInfo));
  150           kernel.loadGBean(gbean, GenericSecurityRealm.class.getClassLoader());
  151   
  152           kernel.startGBean(testCE);
  153           kernel.startGBean(testUseName);
  154           kernel.startGBean(testRealm);
  155   
  156           LoginContext context = new LoginContext("properties", new AbstractTest.UsernamePasswordCallback("alan", "starcraft"));
  157           context.login();
  158           clientSubject = context.getSubject();
  159   
  160           context = new LoginContext("properties", new AbstractTest.UsernamePasswordCallback("izumi", "violin"));
  161           context.login();
  162           serverSubject = context.getSubject();
  163   
  164           SocketSynchChannelFactory factory = new SocketSynchChannelFactory();
  165           server = new SynchToAsynchChannelServerAdapter(
  166                   factory.bindSynchChannel(new URI("tcp://localhost:0")));
  167   
  168           server.setAcceptListener(new AcceptListener() {
  169               public void onAccept(Channel channel) {
  170                   RequestChannel requestChannel=null;
  171                   try {
  172   
  173                       requestChannel =
  174                           new AsynchChannelToServerRequestChannel(
  175   	                        new SubjectCarryingChannel(
  176   	                            new PacketAggregatingAsynchChannel(
  177   	                                SynchToAsynchChannelAdapter.adapt(channel))));
  178   
  179                       requestChannel.setRequestListener(SubjectCarryingProtocolTest.this);
  180                       requestChannel.start();
  181   
  182                   } catch (IOException e) {
  183                       log.info("Failed to accept connection.", e);
  184                       if( requestChannel!=null )
  185                           requestChannel.dispose();
  186                       else
  187                           channel.dispose();
  188                   }
  189               }
  190               public void onAcceptError(IOException error) {
  191                   log.info("Accept Failed: "+error);
  192               }
  193           });
  194   
  195           server.start();
  196           serverURI = server.getConnectURI();
  197   
  198       }
  199   
  200       public void tearDown() throws Exception {
  201           server.dispose();
  202   
  203           kernel.stopGBean(testRealm);
  204           kernel.stopGBean(testCE);
  205           kernel.stopGBean(serverInfo);
  206           kernel.unloadGBean(testCE);
  207           kernel.unloadGBean(testRealm);
  208           kernel.unloadGBean(serverInfo);
  209           super.tearDown();
  210           Configuration.setConfiguration(new ConfigFile());
  211       }
  212   
  213       public Packet onRequest(Packet packet) {
  214   
  215           String p="";
  216           try {
  217               SubjectContext ctx = (SubjectContext)packet.narrow(SubjectContext.class);
  218   	        Subject subject = ctx.getSubject();
  219   	        p = subject.getPrincipals().iterator().next().toString();
  220   	        log.info("Received request as: "+p);
  221           } catch ( Exception e ) {
  222               e.printStackTrace();
  223           }
  224           return new ByteArrayPacket(p.getBytes());
  225       }
  226   
  227       public void onRquestError(IOException arg) {
  228       }
  229   
  230   
  231   }

Home » geronimo-2.2-source-release » org.apache.geronimo.security.network.protocol » [javadoc | source]