1 22 package org.jboss.test.jca.securedejb; 23 24 import java.rmi.RemoteException ; 25 import java.sql.Connection ; 26 import java.sql.SQLException ; 27 import java.security.Principal ; 28 import javax.ejb.SessionBean ; 29 import javax.ejb.SessionContext ; 30 import javax.ejb.EJBException ; 31 import javax.naming.InitialContext ; 32 import javax.naming.directory.DirContext ; 33 import javax.sql.DataSource ; 34 35 import org.jboss.logging.Logger; 36 import org.jboss.test.jca.fs.DirContextFactory; 37 38 43 public class CallerIdentityBean implements SessionBean 44 { 45 static Logger log = Logger.getLogger(CallerIdentityBean.class); 46 private SessionContext ctx; 47 48 public void ejbCreate() 49 { 50 } 51 public void ejbActivate() 52 { 53 } 54 public void ejbPassivate() throws RemoteException 55 { 56 } 57 public void ejbRemove() throws RemoteException 58 { 59 } 60 public void setSessionContext(SessionContext ctx) throws RemoteException 61 { 62 this.ctx = ctx; 63 } 64 public void unsetSessionContext() throws RemoteException 65 { 66 this.ctx = null; 67 } 68 69 public void useCallerForAuth() 70 { 71 try 72 { 73 Principal caller = ctx.getCallerPrincipal(); 74 String name0 = caller.getName(); 75 boolean isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser"); 76 boolean isUseCallerForAuth = ctx.isCallerInRole("UseCallerForAuth"); 77 log.info("useCallerForAuth#0, caller="+caller 78 +", isCallerIdentityUser="+isCallerIdentityUser 79 +", isUseCallerForAuth="+isUseCallerForAuth); 80 InitialContext enc = new InitialContext (); 81 DataSource ds = (DataSource ) enc.lookup("java:comp/env/jdbc/CallerIdentityDS"); 82 testConnection(ds); 83 caller = ctx.getCallerPrincipal(); 84 String name1 = caller.getName(); 85 isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser"); 86 isUseCallerForAuth = ctx.isCallerInRole("UseCallerForAuth"); 87 log.info("useCallerForAuth#1, caller="+caller 88 +", isCallerIdentityUser="+isCallerIdentityUser 89 +", isUseCallerForAuth="+isUseCallerForAuth); 90 if( name0.equals(name1) == false ) 91 throw new EJBException (name0+" != "+name1); 92 if( isCallerIdentityUser == false || isUseCallerForAuth == false ) 93 throw new EJBException ("Lost CallerIdentityUser, UseCallerForAuth roles"); 94 } 95 catch(Exception e) 96 { 97 throw new EJBException (e); 98 } 99 } 100 101 public void useConfiguredForAuth() 102 { 103 try 104 { 105 Principal caller = ctx.getCallerPrincipal(); 106 String name0 = caller.getName(); 107 boolean isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser"); 108 boolean isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth"); 109 log.info("useConfiguredForAuth#0, caller="+caller 110 +", isCallerIdentityUser="+isCallerIdentityUser 111 +", isUseConfiguredForAuth="+isUseConfiguredForAuth); 112 InitialContext enc = new InitialContext (); 113 DataSource ds = (DataSource ) enc.lookup("java:comp/env/jdbc/ConfiguredIdentityDS"); 114 testConnection(ds); 115 caller = ctx.getCallerPrincipal(); 116 String name1 = caller.getName(); 117 isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser"); 118 isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth"); 119 log.info("useConfiguredForAuth#1, caller="+caller 120 +", isCallerIdentityUser="+isCallerIdentityUser 121 +", isUseConfiguredForAuth="+isUseConfiguredForAuth); 122 if( name0.equals(name1) == false ) 123 throw new EJBException (name0+" != "+name1); 124 if( isCallerIdentityUser == false || isUseConfiguredForAuth == false ) 125 throw new EJBException ("Lost CallerIdentityUser, UseConfiguredForAuth roles"); 126 127 ds = (DataSource ) enc.lookup("java:comp/env/jdbc/ConfiguredIdentityDS"); 129 for(int n = 0; n < 1000; n ++) 130 { 131 testConnection(ds); 132 } 133 caller = ctx.getCallerPrincipal(); 134 String name2 = caller.getName(); 135 isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser"); 136 isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth"); 137 log.info("useRunAsForAuthDS#2, caller="+caller 138 +", isCallerIdentityUser="+isCallerIdentityUser 139 +", isUseConfiguredForAuth="+isUseConfiguredForAuth); 140 if( name0.equals(name2) == false ) 141 throw new EJBException (name0+" != "+name2); 142 if( isCallerIdentityUser == false || isUseConfiguredForAuth == false ) 143 throw new EJBException ("Lost CallerIdentityUser, UseConfiguredForAuth roles"); 144 } 145 catch(Exception e) 146 { 147 throw new EJBException (e); 148 } 149 } 150 151 public void useRunAsForAuthDS() 152 { 153 try 154 { 155 Principal caller = ctx.getCallerPrincipal(); 156 String name0 = caller.getName(); 157 boolean isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser"); 158 boolean isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth"); 159 log.info("useRunAsForAuthDS#0, caller="+caller 160 +", isCallerIdentityUser="+isCallerIdentityUser 161 +", isUseConfiguredForAuth="+isUseConfiguredForAuth); 162 InitialContext enc = new InitialContext (); 163 DataSource ds = (DataSource ) enc.lookup("java:comp/env/jdbc/RunAsIdentityDS"); 164 testConnection(ds); 165 caller = ctx.getCallerPrincipal(); 166 String name1 = caller.getName(); 167 isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser"); 168 isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth"); 169 log.info("useRunAsForAuthDS#1, caller="+caller 170 +", isCallerIdentityUser="+isCallerIdentityUser 171 +", isUseConfiguredForAuth="+isUseConfiguredForAuth); 172 if( name0.equals(name1) == false ) 173 throw new EJBException (name0+" != "+name1); 174 if( isCallerIdentityUser == false || isUseConfiguredForAuth == false ) 175 throw new EJBException ("Lost CallerIdentityUser, UseConfiguredForAuth roles"); 176 177 ds = (DataSource ) enc.lookup("java:comp/env/jdbc/RunAsIdentityDS"); 179 for(int n = 0; n < 1000; n ++) 180 { 181 testConnection(ds); 182 } 183 caller = ctx.getCallerPrincipal(); 184 String name2 = caller.getName(); 185 isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser"); 186 isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth"); 187 log.info("useRunAsForAuthDS#2, caller="+caller 188 +", isCallerIdentityUser="+isCallerIdentityUser 189 +", isUseConfiguredForAuth="+isUseConfiguredForAuth); 190 if( name0.equals(name2) == false ) 191 throw new EJBException (name0+" != "+name2); 192 if( isCallerIdentityUser == false || isUseConfiguredForAuth == false ) 193 throw new EJBException ("Lost CallerIdentityUser, UseConfiguredForAuth roles"); 194 } 195 catch(Exception e) 196 { 197 throw new EJBException (e); 198 } 199 } 200 201 public void useRunAsForAuthFS() 202 { 203 try 204 { 205 Principal caller = ctx.getCallerPrincipal(); 206 String name0 = caller.getName(); 207 boolean isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser"); 208 boolean isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth"); 209 log.info("useRunAsForAuthFS#0, caller="+caller 210 +", isCallerIdentityUser="+isCallerIdentityUser 211 +", isUseConfiguredForAuth="+isUseConfiguredForAuth); 212 InitialContext enc = new InitialContext (); 213 DirContextFactory dcf = (DirContextFactory) enc.lookup("java:comp/env/jndi/RunAsIdentityFS"); 214 DirContext dc = dcf.getConnection(); 215 caller = ctx.getCallerPrincipal(); 216 dc.close(); 217 String name1 = caller.getName(); 218 isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser"); 219 isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth"); 220 log.info("useRunAsForAuthFS#1, caller="+caller 221 +", isCallerIdentityUser="+isCallerIdentityUser 222 +", isUseConfiguredForAuth="+isUseConfiguredForAuth); 223 if( name0.equals(name1) == false ) 224 throw new EJBException (name0+" != "+name1); 225 if( isCallerIdentityUser == false || isUseConfiguredForAuth == false ) 226 throw new EJBException ("Lost CallerIdentityUser, UseConfiguredForAuth roles"); 227 } 228 catch(Exception e) 229 { 230 throw new EJBException (e); 231 } 232 } 233 234 private void testConnection(DataSource ds) throws SQLException 235 { 236 Connection conn = ds.getConnection(); 237 conn.close(); 238 } 239 } 240 | Popular Tags |