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 23 import javax.jbi.messaging.InOnly; 24 import javax.security.auth.Subject ; 25 import javax.security.auth.callback.Callback ; 26 import javax.security.auth.callback.CallbackHandler ; 27 import javax.security.auth.callback.NameCallback ; 28 import javax.security.auth.callback.PasswordCallback ; 29 import javax.security.auth.callback.UnsupportedCallbackException ; 30 import javax.security.auth.login.LoginContext ; 31 import javax.xml.namespace.QName ; 32 33 import org.apache.servicemix.client.DefaultServiceMixClient; 34 import org.apache.servicemix.client.ServiceMixClient; 35 import org.apache.servicemix.jbi.jaxp.StringSource; 36 import org.apache.servicemix.tck.Receiver; 37 import org.apache.servicemix.tck.SpringTestSupport; 38 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 39 import org.springframework.context.support.AbstractXmlApplicationContext; 40 41 public class SpringSecuredBrokerTest extends SpringTestSupport { 42 43 static { 44 String path = System.getProperty("java.security.auth.login.config"); 45 if (path == null) { 46 URL resource = PropertiesLoginModuleTest.class.getResource("login.properties"); 47 if (resource != null) { 48 path = new File (resource.getFile()).getAbsolutePath(); 49 System.setProperty("java.security.auth.login.config", path); 50 } 51 } 52 System.err.println("Path to login config: " + path); 53 } 54 55 protected Receiver receiver1; 56 protected Receiver receiver2; 57 protected Receiver receiver3; 58 protected ServiceMixClient client; 59 60 protected void setUp() throws Exception { 61 super.setUp(); 62 receiver1 = (Receiver) jbi.getBean("receiver1"); 63 receiver2 = (Receiver) jbi.getBean("receiver2"); 64 receiver3 = (Receiver) jbi.getBean("receiver3"); 65 client = new DefaultServiceMixClient(jbi); 66 } 67 68 protected void tearDown() throws Exception { 69 super.tearDown(); 70 } 71 72 protected AbstractXmlApplicationContext createBeanFactory() { 73 return new ClassPathXmlApplicationContext("org/apache/servicemix/jbi/security/secure.xml"); 74 } 75 76 protected Subject login(final String username, final String password) throws Exception { 77 LoginContext context = new LoginContext ("servicemix-domain", new CallbackHandler () { 78 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException { 79 for (int i = 0; i < callbacks.length; i++) { 80 if (callbacks[i] instanceof NameCallback ) { 81 ((NameCallback ) callbacks[i]).setName(username); 82 } else if (callbacks[i] instanceof PasswordCallback ) { 83 ((PasswordCallback ) callbacks[i]).setPassword(password.toCharArray()); 84 } else { 85 throw new UnsupportedCallbackException (callbacks[i]); 86 } 87 } 88 } 89 }); 90 context.login(); 91 return context.getSubject(); 92 } 93 94 protected void send(String username, String password, QName service) throws Exception { 95 Subject subject = login(username, password); 96 InOnly me = client.createInOnlyExchange(); 97 me.setService(service); 98 me.getInMessage().setSecuritySubject(subject); 99 me.getInMessage().setContent(new StringSource("<hello>world</hello>")); 100 client.sendSync(me); 101 } 102 103 public void testAuthorizationsOnReceiver1() throws Exception { 104 QName service = new QName ("http://servicemix.org/example/1", "receiver1"); 105 send("first", "secret", service); 107 send("second", "password", service); 108 send("third", "another", service); 109 } 110 111 public void testAuthorizationsOnReceiver2() throws Exception { 112 QName service = new QName ("http://servicemix.org/example/1", "receiver2"); 113 send("first", "secret", service); 115 send("second", "password", service); 116 try { 117 send("third", "another", service); 118 fail("receiver2 is not available to testers"); 119 } catch (SecurityException e) { 120 } 121 } 122 123 public void testAuthorizationsOnReceiver3() throws Exception { 124 QName service = new QName ("http://servicemix.org/example/2", "receiver1"); 125 send("first", "secret", service); 127 try { 128 send("second", "password", service); 129 fail("receiver2 is not available to accounting"); 130 } catch (SecurityException e) { 131 } 132 try { 133 send("third", "another", service); 134 fail("receiver2 is not available to testers"); 135 } catch (SecurityException e) { 136 } 137 } 138 139 } 140 | Popular Tags |