1 22 package org.jboss.test.security.ejb; 23 24 import java.util.Set ; 25 import java.util.Iterator ; 26 import java.security.GeneralSecurityException ; 27 import java.security.Principal ; 28 import java.security.acl.Group ; 29 import javax.security.auth.Subject ; 30 import javax.security.jacc.PolicyContext ; 31 import javax.security.jacc.PolicyContextException ; 32 import javax.ejb.SessionContext ; 33 import javax.ejb.SessionBean ; 34 import javax.ejb.FinderException ; 35 import javax.naming.InitialContext ; 36 37 import org.jboss.security.SecurityAssociation; 38 import org.jboss.test.security.interfaces.StatelessSessionHome; 39 import org.jboss.test.security.interfaces.StatelessSession; 40 import org.jboss.test.security.interfaces.StatefulSessionHome; 41 import org.jboss.test.security.interfaces.EntityHome; 42 import org.jboss.test.security.interfaces.Entity; 43 import org.jboss.test.security.interfaces.StatefulSession; 44 45 54 public class SubjectSessionBean implements SessionBean 55 { 56 57 private static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container"; 58 59 private SessionContext context; 60 61 public void ejbCreate() 62 { 63 } 64 public void ejbActivate() 65 { 66 } 67 public void ejbPassivate() 68 { 69 } 70 public void ejbRemove() 71 { 72 } 73 public void setSessionContext(SessionContext context) 74 { 75 this.context = context; 76 } 77 78 84 public void validateCallerContext(String callerName, Set callerPrincipals) 85 throws GeneralSecurityException 86 { 87 Principal caller = context.getCallerPrincipal(); 88 String name = caller.getName(); 89 if( name.equals(callerName) == false ) 90 throw new GeneralSecurityException ("CallerPrincipal.name("+name+") != "+callerName); 91 92 validatePolicyContextSubject("enter", callerPrincipals); 93 validateSecurityAssociationSubject("enter", callerPrincipals); 94 95 InitialContext ctx = null; 96 try 97 { 98 ctx = new InitialContext (); 99 StatelessSessionHome home = (StatelessSessionHome) 100 ctx.lookup("java:comp/env/ejb/StatelessSession"); 101 StatelessSession bean = home.create(); 102 bean.echo("validateCallerContext"); 103 validatePolicyContextSubject("post stateless", callerPrincipals); 104 validateSecurityAssociationSubject("post stateless", callerPrincipals); 105 106 StatefulSessionHome home2 = (StatefulSessionHome) 107 ctx.lookup("java:comp/env/ejb/StatefulSession"); 108 StatefulSession bean2 = home2.create("validateCallerContext"); 109 bean2.echo("validateCallerContext"); 110 validatePolicyContextSubject("post stateful", callerPrincipals); 111 validateSecurityAssociationSubject("post stateful", callerPrincipals); 112 113 EntityHome home3 = (EntityHome) 114 ctx.lookup("java:comp/env/ejb/Entity"); 115 Entity bean3 = null; 116 try 117 { 118 bean3 = home3.findByPrimaryKey("validateCallerContext"); 119 } 120 catch(FinderException e) 121 { 122 bean3 = home3.create("validateCallerContext"); 123 } 124 bean3.echo("validateCallerContext"); 125 } 126 catch(Exception e) 127 { 128 GeneralSecurityException ex = new GeneralSecurityException ("Unexpected exception"); 129 ex.initCause(e); 130 throw ex; 131 } 132 validatePolicyContextSubject("exit", callerPrincipals); 133 validateSecurityAssociationSubject("exit", callerPrincipals); 134 } 135 136 140 protected void validatePolicyContextSubject(String ctx, Set callerPrincipals) 141 throws GeneralSecurityException 142 { 143 try 144 { 145 Subject caller = caller = (Subject ) PolicyContext.getContext(SUBJECT_CONTEXT_KEY); 146 if( contains(caller, callerPrincipals) == false ) 147 { 148 String msg = ctx+", PolicyContext subject: "+caller 149 +" does not contain expected principals: "+callerPrincipals; 150 throw new GeneralSecurityException (msg); 151 } 152 } 153 catch(PolicyContextException e) 154 { 155 156 } 157 } 158 162 protected void validateSecurityAssociationSubject(String ctx, Set callerPrincipals) 163 throws GeneralSecurityException 164 { 165 Subject caller = SecurityAssociation.getSubject(); 166 if( contains(caller, callerPrincipals) == false ) 167 { 168 String msg = ctx+", SecurityAssociation subject: "+caller 169 +" does not contain expected principals: "+callerPrincipals; 170 throw new GeneralSecurityException (msg); 171 } 172 } 173 protected boolean contains(Subject s, Set callerPrincipals) 174 { 175 Set gs = s.getPrincipals(Group .class); 176 Iterator iter = gs.iterator(); 177 while( iter.hasNext() ) 178 { 179 Group g = (Group ) iter.next(); 180 if( g.getName().equals("Roles") ) 181 { 182 Iterator citer = callerPrincipals.iterator(); 183 while( citer.hasNext() ) 184 { 185 Principal p = (Principal ) citer.next(); 186 if( g.isMember(p) == false ) 187 return false; 188 } 189 } 190 } 191 return true; 192 } 193 } 194 | Popular Tags |