1 17 package org.apache.servicemix.jbi.security; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.URL ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 import javax.jbi.messaging.InOnly; 26 import javax.security.auth.Subject ; 27 import javax.security.auth.callback.Callback ; 28 import javax.security.auth.callback.CallbackHandler ; 29 import javax.security.auth.callback.NameCallback ; 30 import javax.security.auth.callback.PasswordCallback ; 31 import javax.security.auth.callback.UnsupportedCallbackException ; 32 import javax.security.auth.login.LoginContext ; 33 34 import junit.framework.TestCase; 35 36 import org.apache.servicemix.client.DefaultServiceMixClient; 37 import org.apache.servicemix.client.ServiceMixClient; 38 import org.apache.servicemix.jbi.container.JBIContainer; 39 import org.apache.servicemix.jbi.jaxp.StringSource; 40 import org.apache.servicemix.jbi.security.acl.AuthorizationMap; 41 import org.apache.servicemix.jbi.security.acl.impl.AuthorizationEntry; 42 import org.apache.servicemix.jbi.security.acl.impl.DefaultAuthorizationMap; 43 import org.apache.servicemix.tck.ReceiverComponent; 44 45 public class SecuredBrokerTest extends TestCase { 46 47 static { 48 String path = System.getProperty("java.security.auth.login.config"); 49 if (path == null) { 50 URL resource = PropertiesLoginModuleTest.class.getResource("login.properties"); 51 if (resource != null) { 52 path = new File (resource.getFile()).getAbsolutePath(); 53 System.setProperty("java.security.auth.login.config", path); 54 } 55 } 56 System.out.println("Path to login config: " + path); 57 } 58 59 protected JBIContainer jbi; 60 protected ReceiverComponent receiver; 61 62 protected void setUp() throws Exception { 63 jbi = new JBIContainer(); 64 jbi.setUseMBeanServer(false); 65 jbi.setEmbedded(true); 66 List entries = new ArrayList (); 67 entries.add(new AuthorizationEntry(ReceiverComponent.SERVICE, null, "programmers")); 68 AuthorizationMap map = new DefaultAuthorizationMap(entries); 69 SecuredBroker broker = new SecuredBroker(map); 70 jbi.setBroker(broker); 71 jbi.init(); 72 73 receiver = new ReceiverComponent(); 74 jbi.activateComponent(receiver, "receiver"); 75 76 jbi.start(); 77 } 78 79 protected void tearDown() throws Exception { 80 jbi.shutDown(); 81 } 82 83 protected Subject login(final String username, final String password) throws Exception { 84 LoginContext context = new LoginContext ("servicemix-domain", new CallbackHandler () { 85 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException { 86 for (int i = 0; i < callbacks.length; i++) { 87 if (callbacks[i] instanceof NameCallback ) { 88 ((NameCallback ) callbacks[i]).setName(username); 89 } else if (callbacks[i] instanceof PasswordCallback ) { 90 ((PasswordCallback ) callbacks[i]).setPassword(password.toCharArray()); 91 } else { 92 throw new UnsupportedCallbackException (callbacks[i]); 93 } 94 } 95 } 96 }); 97 context.login(); 98 return context.getSubject(); 99 } 100 101 public void testOk() throws Exception { 102 Subject subject = login("first", "secret"); 103 ServiceMixClient client = new DefaultServiceMixClient(jbi); 104 InOnly me = client.createInOnlyExchange(); 105 me.setService(ReceiverComponent.SERVICE); 106 me.getInMessage().setSecuritySubject(subject); 107 me.getInMessage().setContent(new StringSource("<hello>world</hello>")); 108 client.sendSync(me); 109 110 receiver.getMessageList().assertMessagesReceived(1); 111 } 112 113 public void testNOk() throws Exception { 114 Subject subject = login("second", "password"); 115 ServiceMixClient client = new DefaultServiceMixClient(jbi); 116 InOnly me = client.createInOnlyExchange(); 117 me.setService(ReceiverComponent.SERVICE); 118 me.getInMessage().setSecuritySubject(subject); 119 me.getInMessage().setContent(new StringSource("<hello>world</hello>")); 120 try { 121 client.sendSync(me); 122 fail("Should have thrown a SecurityException"); 123 } catch (SecurityException e) { 124 } 126 } 127 128 } 129 | Popular Tags |