[jboss-cvs] JBossAS SVN: r70789 - in branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation: servlets and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Mar 12 19:38:52 EDT 2008


Author: bdecoste
Date: 2008-03-12 19:38:52 -0400 (Wed, 12 Mar 2008)
New Revision: 70789

Modified:
   branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/Client.java
   branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/CustomLoginModule.java
   branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/CustomPrincipalImpl.java
   branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/SessionBean.java
   branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/SessionRemote.java
   branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/servlets/EJBServlet.java
   branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/unit/JaccPropagationTestCase.java
Log:
SA propagation test - work in progress

Modified: branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/Client.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/Client.java	2008-03-12 21:24:11 UTC (rev 70788)
+++ branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/Client.java	2008-03-12 23:38:52 UTC (rev 70789)
@@ -21,15 +21,10 @@
  */
 package org.jboss.ejb3.test.jaccpropagation;
 
+import javax.naming.InitialContext;
 import javax.security.auth.Subject;
 import javax.security.auth.login.LoginContext;
 
-import javax.naming.InitialContext;
-
-import org.jboss.ejb3.test.jaccpropagation.CustomPrincipalImpl;
-import org.jboss.ejb3.test.jaccpropagation.SessionRemote;
-import org.jboss.ejb3.test.jaccpropagation.AppCallbackHandler;
-
 import org.jboss.logging.Logger;
 import org.jboss.security.SecurityAssociation;
 
@@ -43,7 +38,7 @@
    public static final String JAAS_MODE = "JAAS";
    public static final String SA_MODE = "SA";
    
-   public static String processSecurityAssociationRequest() throws Exception
+   public String processSecurityAssociationRequest() throws Exception
    {
       String result = "";
       
@@ -52,46 +47,44 @@
       
       log.info("!!! initial principal " + SecurityAssociation.getPrincipal());
       log.info("!!! initial subject " + SecurityAssociation.getSubject());
+      
+      session.testCustomPrincipal();
   
-      CustomPrincipalImpl customPrincipal = new CustomPrincipalImpl("somebody");
+      CustomPrincipalImpl customPrincipal = (CustomPrincipalImpl)SecurityAssociation.getPrincipal();
       customPrincipal.setCustomRole("CustomRole1");
       Object credential = "password".toCharArray();
       
-      java.util.Set<java.security.Principal> principalsSet = new java.util.HashSet<java.security.Principal>();
-      principalsSet.add(customPrincipal);
+      Subject newSubject = new Subject(); 
       
-      Subject activeSubject = SecurityAssociation.getSubject();
-      Subject newSubject = new Subject(); //false, principalsSet, activeSubject.getPublicCredentials(), activeSubject.getPrivateCredentials());
-      
-      log.info("!!! newSubject " + newSubject);
-      
       SecurityAssociation.pushSubjectContext(newSubject, customPrincipal, credential);
       
       log.info("!!! before principal " + SecurityAssociation.getPrincipal());
-      log.info("!!! before subject " + SecurityAssociation.getSubject() + " " + SecurityAssociation.getSubject().getPrincipals());
+      log.info("!!! before subject " + SecurityAssociation.getSubject());
       
-      result = session.testCustomPrincipal();
+      result = session.testCustomPrincipal1();
       
       log.info("!!! after principal " + SecurityAssociation.getPrincipal());
       log.info("!!! after subject " + SecurityAssociation.getSubject());
       
-      activeSubject = SecurityAssociation.getSubject();
-
       customPrincipal.setCustomRole("CustomRole2");
-      
-      newSubject = new Subject(); //false, principalsSet, activeSubject.getPublicCredentials(), activeSubject.getPrivateCredentials());
-      
-      log.info("!!! newSubject " + newSubject);
         
       SecurityAssociation.pushSubjectContext(newSubject, customPrincipal, credential);
 
       log.info("!!! before principal " + SecurityAssociation.getPrincipal());
       log.info("!!! before subject " + SecurityAssociation.getSubject());
       
-      result = session.testCustomPrincipal();
+      result = session.testCustomPrincipal2();
       
       log.info("!!! after principal " + SecurityAssociation.getPrincipal());
       log.info("!!! after subject " + SecurityAssociation.getSubject());
+      
+      // set "org.jboss.security.SecurityAssociation.ThreadLocal" property to true to use ThreadLocal 
+      // vs. default InheritableThreadLocal
+      EJBInvoker invoker = new EJBInvoker();
+      new Thread(invoker).start();
+      Thread.sleep(1000);
+      if (invoker.getException() != null)
+         throw invoker.getException();
 
       return result;
    }
@@ -123,4 +116,39 @@
 
       return result;
    }
+   
+   public class EJBInvoker implements Runnable
+   {
+      protected Exception exception = null;
+      
+      public EJBInvoker()
+      {
+      }
+      
+      public void run()
+      {
+         try
+         {
+            InitialContext ctx = new InitialContext();
+            SessionRemote session = (SessionRemote)ctx.lookup("SessionBean/remote");
+
+            log.info("!!! threaded before principal " + SecurityAssociation.getPrincipal() );
+            log.info("!!! threaded before subject " + SecurityAssociation.getSubject());
+            
+            session.testCustomPrincipal2();
+            
+            log.info("!!! threaded after principal " + SecurityAssociation.getPrincipal());
+            log.info("!!! threaded after subject " + SecurityAssociation.getSubject());
+         }
+         catch (Exception e)
+         {
+            exception = e;
+         }
+      }
+      
+      public Exception getException()
+      {
+         return exception;
+      }
+   }
 }

Modified: branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/CustomLoginModule.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/CustomLoginModule.java	2008-03-12 21:24:11 UTC (rev 70788)
+++ branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/CustomLoginModule.java	2008-03-12 23:38:52 UTC (rev 70789)
@@ -21,6 +21,7 @@
  */
 package org.jboss.ejb3.test.jaccpropagation;
 
+import java.lang.reflect.Constructor;
 import java.security.Principal;
 import java.security.acl.Group;
 import java.util.Map;
@@ -34,6 +35,8 @@
 import javax.security.auth.login.LoginException;
 
 import org.jboss.logging.Logger;
+import org.jboss.security.SecurityAssociation;
+import org.jboss.security.SimplePrincipal;
 import org.jboss.security.auth.spi.UsersRolesLoginModule;
 
 import org.jboss.security.auth.callback.SecurityAssociationCallback;
@@ -53,19 +56,27 @@
       NameCallback nc = new NameCallback("User name: ", "guest");
 
       Callback[] callbacks = {nc, pc, sc};
+      
+      Principal p = null;
 
       try
       {
          callbackHandler.handle(callbacks);
          
-         return sc.getPrincipal();
+         p = sc.getPrincipal();
+         
+         if (p instanceof SimplePrincipal)
+         {
+            p = new CustomPrincipalImpl(p.getName());
+            SecurityAssociation.setPrincipal(p);
+         }
       }
       catch (Exception e)
       {
          e.printStackTrace();
       }
-      
-      return null;
+        
+      return p;
    }
    
    public void initialize(Subject subject, CallbackHandler callbackHandler,
@@ -77,28 +88,44 @@
    public boolean login() throws LoginException
    {
       boolean success = super.login();
-  
       return success;
    }
    
    protected Principal createIdentity(String username)
       throws Exception
    { 
-      Principal principal = super.createIdentity(username);
-        
-      return principal;
+	   Principal p = null;
+      if( principalClassName == null )
+      {
+         p = new SimplePrincipal(username);
+      }
+      else
+      {
+         ClassLoader loader = Thread.currentThread().getContextClassLoader();
+         Class clazz = loader.loadClass(principalClassName);
+         Class[] ctorSig = {String.class};
+         Constructor ctor = clazz.getConstructor(ctorSig);
+         Object[] ctorArgs = {username};
+         p = (Principal) ctor.newInstance(ctorArgs);
+      }
+      
+      return p;
    }
    
    protected Principal getIdentity()
    { 
+      Principal p = null;
+      
       if (getCallbackPrincipal() != null)
       {
-         return getCallbackPrincipal();
+         p = getCallbackPrincipal();
       }
       else
       {
-         return super.getIdentity();
+         p = super.getIdentity();
       }
+      
+      return p;
    }
    
    public boolean commit() throws LoginException
@@ -125,10 +152,12 @@
          if (group.getName().equals("Roles"))
          {
             CustomPrincipalImpl customPrincipal = (CustomPrincipalImpl)getIdentity();
-            group.addMember(new CustomPrincipalImpl(customPrincipal.getCustomRole()));
+            if (customPrincipal.getCustomRole() != null && customPrincipal.getCustomRole().length() > 0){
+               group.addMember(new CustomPrincipalImpl(customPrincipal.getCustomRole()));
+            }
          }
       }
-      
+        
       return groups;
    }
    
@@ -147,7 +176,7 @@
    protected Group createGroup(String name, Set principals)
    {
       Group group = super.createGroup(name, principals);
-      
+       
       return group;
    }
 }

Modified: branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/CustomPrincipalImpl.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/CustomPrincipalImpl.java	2008-03-12 21:24:11 UTC (rev 70788)
+++ branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/CustomPrincipalImpl.java	2008-03-12 23:38:52 UTC (rev 70789)
@@ -30,9 +30,9 @@
 public class CustomPrincipalImpl
    implements Principal, Serializable
 {
-   private String name;
+   private String name = "";
    
-   private String customRole;
+   private String customRole = "";
 
    public CustomPrincipalImpl(String name)
    {

Modified: branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/SessionBean.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/SessionBean.java	2008-03-12 21:24:11 UTC (rev 70788)
+++ branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/SessionBean.java	2008-03-12 23:38:52 UTC (rev 70789)
@@ -27,8 +27,10 @@
 import java.util.Set;
 import java.util.Iterator;
 
+import javax.annotation.Resource;
 import javax.annotation.security.RolesAllowed;
 import javax.ejb.Remote;
+import javax.ejb.SessionContext;
 import javax.ejb.Stateless;
 
 import javax.security.auth.Subject;
@@ -49,12 +51,32 @@
 {
    private static final Logger log = Logger.getLogger(SessionBean.class);
    
-   @RolesAllowed({"CustomRole1", "CustomRole2"})
+   @Resource
+   protected SessionContext sessionContext;
+   
+   @RolesAllowed({"allowed"})
    public String testCustomPrincipal() throws Exception
    {
+      return test();
+   }
+   
+   @RolesAllowed({"CustomRole1"})
+   public String testCustomPrincipal1() throws Exception
+   {
+      return test();
+   }
+   
+   @RolesAllowed({"CustomRole2"})
+   public String testCustomPrincipal2() throws Exception
+   {
+      return test();
+   }
+   
+   protected String test() throws Exception
+   {
       String result = "";
       
-      log.info("------------- testCustomPrincipal ...");
+      log.info("------------- testCustomPrincipal -------------");
       
       Principal principal = SecurityAssociation.getPrincipal();
       if (principal != null)

Modified: branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/SessionRemote.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/SessionRemote.java	2008-03-12 21:24:11 UTC (rev 70788)
+++ branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/SessionRemote.java	2008-03-12 23:38:52 UTC (rev 70789)
@@ -27,5 +27,9 @@
 public interface SessionRemote
 {
    String testCustomPrincipal() throws Exception;
+   
+   String testCustomPrincipal1() throws Exception;
+   
+   String testCustomPrincipal2() throws Exception;
 }
 

Modified: branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/servlets/EJBServlet.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/servlets/EJBServlet.java	2008-03-12 21:24:11 UTC (rev 70788)
+++ branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/servlets/EJBServlet.java	2008-03-12 23:38:52 UTC (rev 70789)
@@ -60,10 +60,12 @@
       {
          String mode =  request.getParameter("mode");
          
+         Client client = new Client();
+         
          if (mode.equals(Client.JAAS_MODE))
-            result = Client.processJaasRequest();
+            result = client.processJaasRequest();
          else
-            result = Client.processSecurityAssociationRequest();
+            result = client.processSecurityAssociationRequest();
       }
       catch (Exception e)
       {

Modified: branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/unit/JaccPropagationTestCase.java
===================================================================
--- branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/unit/JaccPropagationTestCase.java	2008-03-12 21:24:11 UTC (rev 70788)
+++ branches/JBPAPP_4_2_0_GA_CP/ejb3/src/test/org/jboss/ejb3/test/jaccpropagation/unit/JaccPropagationTestCase.java	2008-03-12 23:38:52 UTC (rev 70789)
@@ -104,7 +104,8 @@
    
    public void atestRemoteSAPropagation() throws Exception
    {
-      String result = Client.processSecurityAssociationRequest();
+      Client client = new Client();
+      String result = client.processSecurityAssociationRequest();
       
       System.out.println(result);
       




More information about the jboss-cvs-commits mailing list