Skip to content

Commit 1d23c9c

Browse files
committed
Added trust store handling for remote EJB with SSL sample
1 parent 3ec0551 commit 1d23c9c

File tree

3 files changed

+87
-190
lines changed

3 files changed

+87
-190
lines changed

ejb/remote/roles-allowed-ssl/src/test/java/org/javaee7/ejb/remote/ssl/RemoteBeanTest.java

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
/** Copyright Payara Services Limited **/
22
package org.javaee7.ejb.remote.ssl;
33

4+
import static javax.naming.Context.SECURITY_PROTOCOL;
45
import static org.javaee7.ServerOperations.addUsersToContainerIdentityStore;
56
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
67
import static org.jboss.shrinkwrap.api.asset.EmptyAsset.INSTANCE;
78
import static org.junit.Assert.assertEquals;
89
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;
917

1018
import javax.naming.Context;
1119
import javax.naming.NamingException;
1220

1321
import org.javaee7.RemoteEJBContextFactory;
1422
import org.javaee7.RemoteEJBContextProvider;
15-
import org.javaee7.ejb.remote.ssl.Bean;
16-
import org.javaee7.ejb.remote.ssl.BeanRemote;
1723
import org.jboss.arquillian.container.test.api.Deployment;
1824
import org.jboss.arquillian.container.test.api.RunAsClient;
1925
import org.jboss.arquillian.junit.Arquillian;
26+
import org.jboss.arquillian.test.api.ArquillianResource;
2027
import org.jboss.shrinkwrap.api.Archive;
21-
import org.jboss.shrinkwrap.api.ShrinkWrap;
2228
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
2329
import org.jboss.shrinkwrap.api.spec.JavaArchive;
2430
import org.jboss.shrinkwrap.api.spec.WebArchive;
@@ -39,6 +45,9 @@
3945
*/
4046
@RunWith(Arquillian.class)
4147
public class RemoteBeanTest {
48+
49+
@ArquillianResource
50+
private URL base;
4251

4352
private RemoteEJBContextProvider remoteEJBContextProvider;
4453

@@ -66,7 +75,7 @@ public static Archive<EnterpriseArchive> deployment() {
6675
create(WebArchive.class, "test.war")
6776
);
6877

69-
System.out.println(archive.toString(true));
78+
System.out.println("\n**** Deploying archive: " + archive.toString(true) + " \n");
7079

7180
return archive;
7281
} catch (Exception e) {
@@ -94,8 +103,37 @@ public void callProtectedRemoteBean() throws NamingException {
94103

95104
// Obtain the JNDI naming context in a vendor specific way.
96105
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");
99137

100138
assertEquals("method", beanRemote.method());
101139
}

pom.xml

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,6 @@
7676
<enabled>false</enabled>
7777
</snapshots>
7878
</repository>
79-
80-
<repository>
81-
<id>ossrh</id>
82-
<name>Sonatype-snapshot</name>
83-
84-
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
85-
86-
<releases>
87-
<enabled>false</enabled>
88-
</releases>
89-
<snapshots>
90-
<enabled>true</enabled>
91-
<updatePolicy>always</updatePolicy>
92-
</snapshots>
93-
</repository>
9479
</repositories>
9580

9681

@@ -255,6 +240,12 @@
255240
<version>1.6.0</version>
256241
<scope>test</scope>
257242
</dependency>
243+
<dependency>
244+
<groupId>org.omnifaces</groupId>
245+
<artifactId>omniutils</artifactId>
246+
<version>0.10</version>
247+
<scope>test</scope>
248+
</dependency>
258249
</dependencies>
259250

260251

0 commit comments

Comments
 (0)