1 22 package org.jboss.ejb.plugins; 23 24 import java.lang.reflect.Method ; 25 import java.security.CodeSource ; 26 import java.util.HashMap ; 27 28 import javax.security.auth.Subject ; 29 30 import org.jboss.ejb.Container; 31 import org.jboss.invocation.Invocation; 32 import org.jboss.metadata.BeanMetaData; 33 import org.jboss.mx.util.MBeanProxyExt; 34 import org.jboss.mx.util.MBeanServerLocator; 35 import org.jboss.security.AuthorizationManager; 36 import org.jboss.security.SecurityConstants; 37 import org.jboss.security.Util; 38 import org.jboss.security.authorization.AuthorizationContext; 39 import org.jboss.security.authorization.EJBResource; 40 import org.jboss.security.authorization.ResourceKeys; 41 import org.jboss.security.plugins.AuthorizationManagerServiceMBean; 42 43 45 52 public class SecurityAuthorizationInterceptor extends AbstractInterceptor 53 { 54 protected boolean trace = false; 55 protected String ejbName = null; 56 protected CodeSource ejbCS = null; 57 protected AuthorizationManagerServiceMBean authorizationManagerService = null; 58 protected String appSecurityDomain = null; 59 protected String defaultAuthorizationSecurityDomain = SecurityConstants.DEFAULT_EJB_APPLICATION_POLICY; 61 62 63 public SecurityAuthorizationInterceptor() 64 { 65 trace = log.isTraceEnabled(); 66 authorizationManagerService = (AuthorizationManagerServiceMBean) 67 MBeanProxyExt.create(AuthorizationManagerServiceMBean.class, 68 AuthorizationManagerServiceMBean.OBJECT_NAME, 69 MBeanServerLocator.locateJBoss()); 70 } 71 72 75 public void setContainer(Container container) 76 { 77 super.setContainer(container); 78 if (container != null) 79 { 80 BeanMetaData beanMetaData = container.getBeanMetaData(); 81 appSecurityDomain = container.getBeanMetaData().getApplicationMetaData().getSecurityDomain(); 82 ejbName = beanMetaData.getEjbName(); 83 ejbCS = container.getBeanClass().getProtectionDomain().getCodeSource(); 84 } 85 } 86 87 90 public Object invokeHome(Invocation mi) throws Exception 91 { 92 checkAuthorization(mi); 94 Object returnValue = getNext().invokeHome(mi); 95 return returnValue; 96 } 97 98 101 public Object invoke(Invocation mi) throws Exception 102 { 103 checkAuthorization(mi); 105 Object returnValue = getNext().invoke(mi); 106 return returnValue; 107 } 108 109 111 private void checkAuthorization(Invocation mi) 112 throws Exception 113 { 114 Method ejbMethod = mi.getMethod(); 115 if( ejbMethod== null ) 117 return; 118 Subject caller = SecurityActions.getContextSubject(); 120 121 AuthorizationManager authzManager = this.getAuthorizationManager(); 122 final HashMap map = new HashMap (); 123 map.put(ResourceKeys.EJB_NAME ,this.ejbName); 124 map.put(ResourceKeys.EJB_METHOD,ejbMethod); 125 map.put(ResourceKeys.EJB_PRINCIPAL, mi.getPrincipal()); 126 map.put(ResourceKeys.EJB_METHODINTERFACE, mi.getType().toInterfaceString()); 127 map.put(ResourceKeys.EJB_CODESOURCE, ejbCS); 128 map.put(ResourceKeys.CALLER_SUBJECT, caller); 129 map.put(ResourceKeys.AUTHORIZATION_MANAGER,authzManager); 130 map.put(ResourceKeys.RUNASIDENTITY, SecurityActions.peekRunAsIdentity()); 131 map.put(ResourceKeys.EJB_METHODROLES, container.getMethodPermissions(ejbMethod, mi.getType())); 132 EJBResource ejbResource = new EJBResource(map); 133 boolean isAuthorized = false; 134 try 135 { 136 int check = authzManager.authorize(ejbResource); 137 isAuthorized = (check == AuthorizationContext.PERMIT); 138 } 139 catch (Exception e) 140 { 141 isAuthorized = false; 142 if(trace) 143 log.trace("Error in authorization:",e); 144 else 145 log.error("Error in authorization:"+e.getLocalizedMessage()); 146 } 147 String msg = "Denied: caller=" + caller; 148 if(!isAuthorized) 149 throw new SecurityException (msg); 150 } 151 152 158 private AuthorizationManager getAuthorizationManager() throws Exception 159 { 160 String tempSecurityDomain = appSecurityDomain != null ? Util.unprefixSecurityDomain(appSecurityDomain) : 161 defaultAuthorizationSecurityDomain; 162 AuthorizationManager am = authorizationManagerService.getAuthorizationManager(tempSecurityDomain); 163 if(trace) 164 log.trace(am.toString()); 165 return am; 166 } 167 } 168 | Popular Tags |