User: starksm 
  Date: 02/03/22 19:04:51

  Modified:    src/main/org/jboss/security/plugins SecurityConfig.java
                        SecurityConfigMBean.java
  Added:       src/main/org/jboss/security/plugins DefaultLoginConfig.java
  Log:
  Update the SecurityConfig mbean to allow for multiple login configuration
  instances that may be combined.
  
  Revision  Changes    Path
  1.3       +92 -37    jboss/src/main/org/jboss/security/plugins/SecurityConfig.java
  
  Index: SecurityConfig.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/security/plugins/SecurityConfig.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SecurityConfig.java       9 Feb 2002 16:09:25 -0000       1.2
  +++ SecurityConfig.java       23 Mar 2002 03:04:51 -0000      1.3
  @@ -11,31 +11,42 @@
   import java.io.PrintWriter;
   import java.io.StringWriter;
   import java.net.URL;
  -import java.util.Properties;
  -import java.util.Iterator;
  +import java.util.Stack;
  +import javax.management.JMException;
  +import javax.management.MalformedObjectNameException;
   import javax.management.MBeanServer;
   import javax.management.ObjectName;
  -import javax.naming.Context;
  -import javax.naming.InitialContext;
  +import javax.security.auth.login.Configuration;
   
   import org.jboss.system.ServiceMBeanSupport;
   
   /** The SecurityConfigMBean implementation. 
    
    @author [EMAIL PROTECTED]
  - @version $Revision: 1.2 $
  + @version $Revision: 1.3 $
    */
  -public class SecurityConfig extends ServiceMBeanSupport implements 
SecurityConfigMBean
  +public class SecurityConfig extends ServiceMBeanSupport
  +   implements SecurityConfigMBean
   {
      // Constants -----------------------------------------------------
      
      // Attributes ----------------------------------------------------
  -   private MBeanServer server;
  -   private String authConf = "auth.conf";
  +   /** The default Configuration mbean name */
  +   private String loginConfigName;
  +   /** The stack of Configuration mbeans that are active */
  +   private Stack loginConfigStack = new Stack();
  +
  +   static class ConfigInfo
  +   {
  +      ObjectName name;
  +      Configuration config;
  +      ConfigInfo(ObjectName name, Configuration config)
  +      {
  +         this.name = name;
  +         this.config = config;
  +      }
  +   }
   
  -   // Static --------------------------------------------------------
  -   
  -   // Constructors --------------------------------------------------
      public SecurityConfig()
      {
      }
  @@ -47,44 +58,88 @@
   
      /** Get the resource path to the JAAS login configuration file to use.
       */
  -   public String getAuthConf()
  +   public String getLoginConfig()
      {
  -      return authConf;
  +      return loginConfigName;
      }
  -   
  +
      /** Set the resource path to the JAAS login configuration file to use.
       The default is "auth.conf".
       */
  -   public void setAuthConf(String authConf)
  +   public void setLoginConfig(String name) throws MalformedObjectNameException
      {
  -      this.authConf = authConf;
  +      this.loginConfigName = name;
      }
  -   
  -   // Public --------------------------------------------------------
  -   /** Start the service by locating the AuthConf resource in the classpath
  -    using the current thread context ClassLoader and set the
  -    "java.security.auth.login.config" system property to  location of the
  -    resource if this property has not already been set.
  -    @exception Exception thrown if the property cannot be set.
  +
  +   /** Start the configuration service by pushing the mbean given by the
  +    LoginConfig onto the configuration stack.
       */
      public void startService() throws Exception
      {
  -      // Set the JAAS login config file if not already set
  -      if( System.getProperty("java.security.auth.login.config") == null )
  +      pushLoginConfig(loginConfigName);
  +   }
  +
  +   /** Start the configuration service by poping the top of the
  +    configuration stack.
  +    */
  +   public void stopService() throws Exception
  +   {
  +      if( loginConfigStack.empty() == false )
  +         popLoginConfig();
  +   }
  +
  +   /** Push an mbean onto the login configuration stack and install its
  +    Configuration as the current instance.
  +    @see javax.security.auth.login.Configuration
  +    */
  +   public synchronized void pushLoginConfig(String objectName)
  +      throws JMException, MalformedObjectNameException
  +   {
  +      ObjectName name = new ObjectName(objectName);
  +      Configuration prevConfig = null;
  +      if( loginConfigStack.empty() == false )
  +      {
  +         ConfigInfo prevInfo = (ConfigInfo) loginConfigStack.peek();
  +         prevConfig = prevInfo.config;
  +      }
  +
  +      ConfigInfo info = installConfig(name, prevConfig);
  +      loginConfigStack.push(info);
  +   }
  +   /** Pop the current mbean from the login configuration stack and install
  +    the previous Configuration as the current instance.
  +    @see javax.security.auth.login.Configuration
  +    */
  +   public synchronized void popLoginConfig()
  +      throws JMException
  +   {
  +      ConfigInfo info = (ConfigInfo) loginConfigStack.pop();
  +      Configuration prevConfig = null;
  +      if( loginConfigStack.empty() == false )
         {
  -         ClassLoader loader = Thread.currentThread().getContextClassLoader();
  -         URL loginConfig = loader.getResource("auth.conf");
  -         if( loginConfig != null )
  -         {
  -            System.setProperty("java.security.auth.login.config", 
loginConfig.toExternalForm());
  -            if (log.isInfoEnabled())
  -               log.info("Using JAAS LoginConfig: "+loginConfig.toExternalForm());
  -         }
  -         else
  -         {
  -            log.warn("No auth.conf resource found");
  -         }
  +         ConfigInfo prevInfo = (ConfigInfo) loginConfigStack.peek();
  +         prevConfig = prevInfo.config;
         }
  +
  +      installConfig(info.name, prevConfig);
      }
   
  +   /** Obtain the Configuration from the named mbean using its getConfiguration
  +    operation and install it as the current Configuration.
  +
  +    @see Configuration.setConfiguration(javax.security.auth.login.Configuration)
  +    */
  +   private ConfigInfo installConfig(ObjectName name, Configuration prevConfig)
  +      throws JMException
  +   {
  +      MBeanServer server = super.getServer();
  +      Object[] args = {prevConfig};
  +      String[] signature = {"javax.security.auth.login.Configuration"};
  +      Configuration config = (Configuration) server.invoke(name,
  +         "getConfiguration", args, signature);
  +      Configuration.setConfiguration(config);
  +      ConfigInfo info = new ConfigInfo(name, config);
  +      log.debug("Installed JAAS Configuration service="+name+", config="+config);
  +      return info;
  +   }
   }
  
  
  
  1.2       +19 -8     
jboss/src/main/org/jboss/security/plugins/SecurityConfigMBean.java
  
  Index: SecurityConfigMBean.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/security/plugins/SecurityConfigMBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SecurityConfigMBean.java  18 Sep 2001 08:33:14 -0000      1.1
  +++ SecurityConfigMBean.java  23 Mar 2002 03:04:51 -0000      1.2
  @@ -6,24 +6,35 @@
    */
   package org.jboss.security.plugins;
   
  +import javax.management.JMException;
  +import javax.management.MalformedObjectNameException;
  +
   import org.jboss.system.ServiceMBean;
   
   /** A security configuration MBean. This establishes the JAAS and Java2
    security properties and related configuration.
   
  - Currently this only sets the "java.security.auth.login.config" system
  - property to the URL to the AuthConf attribute.
  + @see DefaultLoginConfig
  + @see javax.security.auth.login.Configuration
   
   @author [EMAIL PROTECTED]
  -@version $Revision: 1.1 $
  +@version $Revision: 1.2 $
   */
   public interface SecurityConfigMBean extends ServiceMBean
   {
  -   /** Get the resource path to the JAAS login configuration file to use.
  +   /** Get the name of the mbean that provides the default JAAS login configuration 
*/
  +   public String getLoginConfig();
  +   /** Set the name of the mbean that provides the default JAAS login configuration 
*/
  +   public void setLoginConfig(String objectName) throws 
MalformedObjectNameException;
  +   /** Push an mbean onto the login configuration stack and install its
  +    Configuration as the current instance.
  +    @see javax.security.auth.login.Configuration
       */
  -   public String getAuthConf();
  -   /** Set the resource path to the JAAS login configuration file to use.
  -    The default is "auth.conf".
  +   public void pushLoginConfig(String objectName) throws JMException, 
MalformedObjectNameException;
  +   /** Pop the current mbean from the login configuration stack and install
  +    the previous Configuration as the current instance.
  +    @see javax.security.auth.login.Configuration
       */
  -   public void setAuthConf(String authConf);
  +   public void popLoginConfig() throws JMException;
  +
   }
  
  
  
  1.1                  
jboss/src/main/org/jboss/security/plugins/DefaultLoginConfig.java
  
  Index: DefaultLoginConfig.java
  ===================================================================
  package org.jboss.security.plugins;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Method;
  import java.net.MalformedURLException;
  import java.net.URL;
  import javax.management.Attribute;
  import javax.management.AttributeList;
  import javax.management.AttributeNotFoundException;
  import javax.management.DynamicMBean;
  import javax.management.InvalidAttributeValueException;
  import javax.management.MBeanAttributeInfo;
  import javax.management.MBeanConstructorInfo;
  import javax.management.MBeanException;
  import javax.management.MBeanInfo;
  import javax.management.MBeanOperationInfo;
  import javax.management.ReflectionException;
  import javax.security.auth.login.Configuration;
  import javax.security.auth.login.AppConfigurationEntry;
  
  import org.jboss.logging.Logger;
  
  /** An mbean that uses the default JAAS login configuration file based
   implementation. 
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
   */
  public class DefaultLoginConfig implements DynamicMBean
  {
     private static Logger log = Logger.getLogger(DefaultLoginConfig.class);
     private String authConfig = "auth.conf";
     private Configuration theConfig;
  
     /** Creates a new instance of DefaultLoginConfig */
     public DefaultLoginConfig()
     {
     }
  
     /** Get the resource path to the JAAS login configuration file to use.
      */
     public String getAuthConfig()
     {
        return authConfig;
     }
  
     /** Set the resource path or URL to the JAAS login configuration file to use.
      The default is "auth.conf".
      */
     public void setAuthConfig(String authConfURL) throws MalformedURLException
     {
        this.authConfig = authConfURL;
        // Set the JAAS login config file if not already set
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        URL loginConfig = loader.getResource(authConfig);
        if( loginConfig != null )
        {
           System.setProperty("java.security.auth.login.config", 
loginConfig.toExternalForm());
           if (log.isInfoEnabled())
              log.info("Using JAAS LoginConfig: "+loginConfig.toExternalForm());
        }
        else
        {
           log.warn("Resource: "+authConfig+" not found");
        }
     }
  
     /** Return the Configuration instance managed by this mbean. This simply
      obtains the default Configuration by calling Configuration.getConfiguration.
      Note that this means this mbean must be the first pushed onto the config
      stack if it is used.
      @see javax.security.auth.login.Configuration
      */
     public Configuration getConfiguration(Configuration currentConfig)
     {
        if( theConfig == null )
        {
           theConfig = Configuration.getConfiguration();
           log.debug("theConfig set to: "+theConfig);
        }
        return theConfig;
     }
  
  // Begin DynamicMBean interfaces
     public Object getAttribute(String name)
        throws AttributeNotFoundException, MBeanException, ReflectionException
     {
        if( name.equals("AuthConfig") )
           return getAuthConfig();
        throw new AttributeNotFoundException(name+": is not an attribute");
     }
  
     public AttributeList getAttributes(String[] names)
     {
        AttributeList list = new AttributeList();
        for(int n = 0; n < names.length; n ++)
        {
           String name = names[n];
           try
           {
              Object value = getAttribute(name);
              Attribute attr = new Attribute(name, value);
              list.add(attr);
           }
           catch(Exception e)
           {
           }
        }
        return list;
     }
  
     public MBeanInfo getMBeanInfo()
     {
        Class c = getClass();
        MBeanAttributeInfo[] attrInfo = {
           new MBeanAttributeInfo("AuthConfig", "java.lang.String",
               "", true, true, false)
        };
        Constructor ctor = null;
        try
        {
           Class[] sig = {};
           ctor = c.getDeclaredConstructor(sig);
        }
        catch(Exception e)
        {
        }
        MBeanConstructorInfo[] ctorInfo = {
           new MBeanConstructorInfo("Default ctor", ctor)
        };
        Method getConfiguration = null;
        try
        {
           Class[] sig = {Configuration.class};
           getConfiguration = c.getDeclaredMethod("getConfiguration", sig);
        }
        catch(Exception e)
        {
        }
        MBeanOperationInfo[] opInfo = {
           new MBeanOperationInfo("Access the LoginConfiguration", getConfiguration)
        };
        MBeanInfo info = new MBeanInfo(c.getName(), "Default JAAS LoginConfig",
           attrInfo, ctorInfo, opInfo, null);
        return info;
     }
  
     public Object invoke(String method, Object[] args, String[] signature)
        throws MBeanException, ReflectionException
     {
        Object value = null;
        if( method.equals("getConfiguration") )
        {
           Configuration currentConfig = (Configuration) args[0];
           value = this.getConfiguration(currentConfig);
        }
        return value;
     }
  
     public void setAttribute(Attribute attribute)
        throws AttributeNotFoundException, InvalidAttributeValueException, 
MBeanException, ReflectionException
     {
        String name = attribute.getName();
        String value = (String) attribute.getValue();
        if( name.equals("AuthConfig") )
        {
           try
           {
              setAuthConfig(value);
           }
           catch(Exception e)
           {
              throw new MBeanException(e);
           }
        }
        else
           throw new AttributeNotFoundException(name+": is not an attribute");      
     }
  
     public AttributeList setAttributes(AttributeList attributeList)
     {
        AttributeList list = new AttributeList();
        for(int n = 0; n < attributeList.size(); n ++)
        {
           Attribute attr = (Attribute) attributeList.get(n);
           try
           {
              setAttribute(attr);
              list.add(attr);
           }
           catch(Exception e)
           {
           }
        }
        return list;
     }
  // End DynamicMBean interfaces
  
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to