[jboss-cvs] jbosssx/src/main/org/jboss/security/authorization/modules/ejb ...

Anil Saldhana anil.saldhana at jboss.com
Tue Jul 25 13:11:54 EDT 2006


  User: asaldhana
  Date: 06/07/25 13:11:54

  Added:       src/main/org/jboss/security/authorization/modules/ejb 
                        EJBPolicyModuleDelegate.java
  Log:
  JBAS-3324: Policy Module Delgate for the EJB layer with the default behavior	
  
  Revision  Changes    Path
  1.1      date: 2006/07/25 17:11:54;  author: asaldhana;  state: Exp;jbosssx/src/main/org/jboss/security/authorization/modules/ejb/EJBPolicyModuleDelegate.java
  
  Index: EJBPolicyModuleDelegate.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.security.authorization.modules.ejb;
  
  import java.lang.reflect.Method;
  import java.security.Principal;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  
  import org.jboss.logging.Logger;
  import org.jboss.security.AnybodyPrincipal;
  import org.jboss.security.AuthorizationManager;
  import org.jboss.security.RunAsIdentity;
  import org.jboss.security.SecurityRoleRef;
  import org.jboss.security.SimplePrincipal;
  import org.jboss.security.authorization.AuthorizationContext;
  import org.jboss.security.authorization.PolicyRegistration;
  import org.jboss.security.authorization.Resource;
  import org.jboss.security.authorization.ResourceKeys;
  import org.jboss.security.authorization.modules.AuthorizationModuleHelper;
   
  
  //$Id: EJBPolicyModuleDelegate.java,v 1.1 2006/07/25 17:11:54 asaldhana Exp $
  
  /**
   *  Authorization Module delegate that deals with the authorization decisions
   *  for the EJB Layer (Default Behavior)
   *  @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
   *  @since  Jul 6, 2006 
   *  @version $Revision: 1.1 $
   */
  public class EJBPolicyModuleDelegate extends AuthorizationModuleHelper
  {  
     private String ejbName = null;
     private Method ejbMethod = null; 
     private Principal ejbPrincipal = null;
     private Set methodRoles = null; 
     private String methodInterface = null; 
     private RunAsIdentity callerRunAsIdentity = null;
     private String roleName = null; 
     private Boolean roleRefCheck = Boolean.FALSE;
     private Set securityRoleReferences = null;
     
     public EJBPolicyModuleDelegate()
     {
        log = Logger.getLogger(getClass());
        trace = log.isTraceEnabled();
     }
     
     /**
      * @see AuthorizationModuleHelper#authorize(Resource)
      */
     public int authorize(Resource resource)
     {
        //Get the contextual map
        Map map = resource.getMap();
        if(map == null)
           throw new IllegalStateException("Map from the Resource is null");
      
        if(map.size() == 0)
           throw new IllegalStateException("Map from the Resource is size zero"); 
        PolicyRegistration pr = (PolicyRegistration)map.get("authorizationManager");
        if(pr != null)
          this.authzManager = pr;
        //Populate local variables from the resource 
        this.ejbMethod = (Method)map.get(ResourceKeys.EJB_METHOD); 
        this.ejbName = (String)map.get(ResourceKeys.EJB_NAME); 
        this.ejbPrincipal = (Principal)map.get(ResourceKeys.EJB_PRINCIPAL);
        this.methodInterface = (String)map.get(ResourceKeys.EJB_METHODINTERFACE);
        this.methodRoles = (Set)map.get(ResourceKeys.EJB_METHODROLES);
        this.callerRunAsIdentity = (RunAsIdentity)map.get(ResourceKeys.RUNASIDENTITY);
        this.roleName = (String)map.get(ResourceKeys.ROLENAME);
        this.roleRefCheck = (Boolean)map.get(ResourceKeys.ROLEREF_PERM_CHECK);
        this.securityRoleReferences = (Set)map.get(ResourceKeys.SECURITY_ROLE_REFERENCES);
        
        if(this.roleRefCheck == Boolean.TRUE)
           return checkRoleRef();
        else
           return process();
     }
  
     /**
      * @see AuthorizationModuleHelper#setPolicyRegistrationManager(PolicyRegistration)
      */
     public void setPolicyRegistrationManager(PolicyRegistration authzM)
     {  
        this.authzManager =  authzM;
        if(this.authzManager instanceof AuthorizationManager == false)
           throw new IllegalStateException(authzManager + 
                   " is not an instanceof AuthorizationManager"); 
     }
     
     //Private Methods
     /**
      * Process the request
      * @param request
      * @param sc
      * @return
      */
     private int process() 
     {  
        boolean allowed = true;
        
        //Get the method permissions  
        if (methodRoles == null)
        {
           String method = this.ejbMethod.getName();
           String msg = "No method permissions assigned to method=" + method
              + ", interface=" + methodInterface;
           if(trace)
              log.trace("Exception:"+msg); 
           allowed = false;
        }
        else if (trace)
        {
           log.trace("method=" + this.ejbMethod + ", interface=" + this.methodInterface
              + ", requiredRoles=" + methodRoles);
        }
  
        // Check if the caller is allowed to access the method
        if (methodRoles.contains(AnybodyPrincipal.ANYBODY_PRINCIPAL) == false)
        {
           // The caller is using a the caller identity
           if (callerRunAsIdentity == null)
           { 
              AuthorizationManager am = (AuthorizationManager)authzManager;
              
              // Now actually check if the current caller has one of the required method roles
              if (am.doesUserHaveRole(ejbPrincipal, methodRoles) == false)
              {
                 Set userRoles = am.getUserRoles(ejbPrincipal);
                 String method = this.ejbMethod.getName(); 
                 String msg = "Insufficient method permissions, principal=" + ejbPrincipal
                    + ", ejbName=" + this.ejbName
                    + ", method=" + method + ", interface=" + this.methodInterface
                    + ", requiredRoles=" + methodRoles + ", principalRoles=" + userRoles;
                 if(trace)
                    log.trace("Exception:"+msg); 
                 allowed = false;
              }
           }
  
           // The caller is using a run-as identity
           else
           {
              // Check that the run-as role is in the set of method roles
              if (callerRunAsIdentity.doesUserHaveRole(methodRoles) == false)
              {
                 String method = this.ejbMethod.getName(); 
                 String msg = "Insufficient method permissions, principal=" + ejbPrincipal
                 + ", ejbName=" + this.ejbName
                 + ", method=" + method + ", interface=" + this.methodInterface
                 + ", requiredRoles=" + methodRoles + ", runAsRoles=" 
                 + callerRunAsIdentity.getRunAsRoles();
                 if(trace)
                    log.trace("Exception:"+msg); 
                 allowed = false;
              }
           }
        } 
        return allowed ? AuthorizationContext.PERMIT : AuthorizationContext.DENY;
     } 
     
     private int checkRoleRef()
     {
        AuthorizationManager am = (AuthorizationManager)authzManager;
        //Check the caller of this beans run-as identity 
        if (ejbPrincipal == null && callerRunAsIdentity == null)
        {
           if(trace)
              log.trace("ejbPrincipal = null,callerRunAsIdentity = null => DENY" );
           return AuthorizationContext.DENY;
        } 
  
        // Map the role name used by Bean Provider to the security role
        // link in the deployment descriptor. The EJB 1.1 spec requires
        // the security role refs in the descriptor but for backward
        // compability we're not enforcing this requirement.
        //
        // TODO (2.3): add a conditional check using jboss.xml <enforce-ejb-restrictions> element
        //             which will throw an exception in case no matching
        //             security ref is found. 
        boolean matchFound = false;
        Iterator it = this.securityRoleReferences.iterator();
        while ( it.hasNext())
        {
           SecurityRoleRef meta = (SecurityRoleRef) it.next();
           if (meta.getName().equals(roleName))
           {
              roleName = meta.getLink();
              matchFound = true;
              break;
           }
        }
  
        if (!matchFound)
           log.warn("no match found for security role " + roleName +
           " in the deployment descriptor for ejb " + this.ejbName);
  
        HashSet set = new HashSet();
        set.add(new SimplePrincipal(roleName));
  
        boolean allowed = false;
        if (callerRunAsIdentity == null)
           allowed = am.doesUserHaveRole(ejbPrincipal, set);
        else
           allowed = this.callerRunAsIdentity.doesUserHaveRole(set);
        
        return allowed ? AuthorizationContext.PERMIT : AuthorizationContext.DENY;
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list