|
1 | 1 | /** Copyright Payara Services Limited **/ |
2 | 2 | package org.javaee7.ejb.remote.ssl; |
3 | 3 |
|
| 4 | +import static javax.naming.Context.SECURITY_PROTOCOL; |
4 | 5 | import static org.javaee7.ServerOperations.addUsersToContainerIdentityStore; |
5 | 6 | import static org.jboss.shrinkwrap.api.ShrinkWrap.create; |
6 | 7 | import static org.jboss.shrinkwrap.api.asset.EmptyAsset.INSTANCE; |
7 | 8 | import static org.junit.Assert.assertEquals; |
8 | 9 | import static org.junit.Assume.assumeTrue; |
| 10 | +import static org.omnifaces.utils.security.Certificates.createTempJKSTrustStore; |
| 11 | +import static org.omnifaces.utils.security.Certificates.getCertificateChainFromServer; |
| 12 | +import static org.omnifaces.utils.security.Certificates.getHostFromCertificate; |
| 13 | +import static org.omnifaces.utils.security.Certificates.setSystemTrustStore; |
| 14 | + |
| 15 | +import java.net.URL; |
| 16 | +import java.security.cert.X509Certificate; |
9 | 17 |
|
10 | 18 | import javax.naming.Context; |
11 | 19 | import javax.naming.NamingException; |
12 | 20 |
|
13 | 21 | import org.javaee7.RemoteEJBContextFactory; |
14 | 22 | import org.javaee7.RemoteEJBContextProvider; |
15 | | -import org.javaee7.ejb.remote.ssl.Bean; |
16 | | -import org.javaee7.ejb.remote.ssl.BeanRemote; |
17 | 23 | import org.jboss.arquillian.container.test.api.Deployment; |
18 | 24 | import org.jboss.arquillian.container.test.api.RunAsClient; |
19 | 25 | import org.jboss.arquillian.junit.Arquillian; |
| 26 | +import org.jboss.arquillian.test.api.ArquillianResource; |
20 | 27 | import org.jboss.shrinkwrap.api.Archive; |
21 | | -import org.jboss.shrinkwrap.api.ShrinkWrap; |
22 | 28 | import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; |
23 | 29 | import org.jboss.shrinkwrap.api.spec.JavaArchive; |
24 | 30 | import org.jboss.shrinkwrap.api.spec.WebArchive; |
|
39 | 45 | */ |
40 | 46 | @RunWith(Arquillian.class) |
41 | 47 | public class RemoteBeanTest { |
| 48 | + |
| 49 | + @ArquillianResource |
| 50 | + private URL base; |
42 | 51 |
|
43 | 52 | private RemoteEJBContextProvider remoteEJBContextProvider; |
44 | 53 |
|
@@ -66,7 +75,7 @@ public static Archive<EnterpriseArchive> deployment() { |
66 | 75 | create(WebArchive.class, "test.war") |
67 | 76 | ); |
68 | 77 |
|
69 | | - System.out.println(archive.toString(true)); |
| 78 | + System.out.println("\n**** Deploying archive: " + archive.toString(true) + " \n"); |
70 | 79 |
|
71 | 80 | return archive; |
72 | 81 | } catch (Exception e) { |
@@ -94,8 +103,37 @@ public void callProtectedRemoteBean() throws NamingException { |
94 | 103 |
|
95 | 104 | // Obtain the JNDI naming context in a vendor specific way. |
96 | 105 | Context ejbRemoteContext = remoteEJBContextProvider.getContextWithCredentialsSet("u1", "p1"); |
97 | | - |
98 | | - BeanRemote beanRemote = (BeanRemote) ejbRemoteContext.lookup("java:global/test/Bean"); |
| 106 | + |
| 107 | + ejbRemoteContext.addToEnvironment(SECURITY_PROTOCOL, "ssl"); |
| 108 | + |
| 109 | + System.out.println("\n**** Quering server for its certificate at " + base.getHost() + ":" + "3920" + "\n"); |
| 110 | + |
| 111 | + // Get the certificate from the server, using the EJB SSL port |
| 112 | + X509Certificate[] serverCertificateChain = getCertificateChainFromServer(base.getHost(), 3920); |
| 113 | + |
| 114 | + for (X509Certificate certificate : serverCertificateChain) { |
| 115 | + System.out.println("\n**** Server presented certificate:" + certificate + " \n"); |
| 116 | + } |
| 117 | + |
| 118 | + // Create a trust store on disk containing the servers's certificates |
| 119 | + String trustStorePath = createTempJKSTrustStore(serverCertificateChain); |
| 120 | + |
| 121 | + System.out.println("\n**** Temp trust store with server certificates created at: " + trustStorePath + " \n"); |
| 122 | + |
| 123 | + // Set the newly created trust store as the system wide trust store |
| 124 | + setSystemTrustStore(trustStorePath); |
| 125 | + |
| 126 | + // Get the host name from the certificate the server presented, and use that for the host |
| 127 | + // to ultimately do our SSL request to. |
| 128 | + String host = getHostFromCertificate(serverCertificateChain); |
| 129 | + ejbRemoteContext.addToEnvironment("org.omg.CORBA.ORBInitialHost", host); |
| 130 | + |
| 131 | + System.out.println("\n**** Obtained host \"" + host + "\" from server certificate and will use that for request \n"); |
| 132 | + |
| 133 | + // Do the actual request to the server for our remote EJB |
| 134 | + BeanRemote beanRemote = (BeanRemote) ejbRemoteContext.lookup("java:global/my/myEJB/Bean"); |
| 135 | + |
| 136 | + System.out.println("\n**** Remote EJB obtained via SSL: " + beanRemote + " \n"); |
99 | 137 |
|
100 | 138 | assertEquals("method", beanRemote.method()); |
101 | 139 | } |
|
0 commit comments