Skip to content

Commit

Permalink
[GH-15909] Fix Jetty 9 SSL init (#15824)
Browse files Browse the repository at this point in the history
* [GH-15909] Fix Jetty 9 SSL init

* switch & add comment
  • Loading branch information
krasinski authored Jan 22, 2024
1 parent fba1e05 commit 6508c53
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions h2o-jetty-9/src/main/java/water/webserver/jetty9/Jetty9Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
Expand All @@ -33,6 +32,8 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.util.Collections;

class Jetty9Helper {
Expand Down Expand Up @@ -65,7 +66,8 @@ Server createJettyServer(String ip, int port) {

final ServerConnector connector;
if (isSecured) {
final SslContextFactory sslContextFactory = new SslContextFactory(config.jks);
final SslContextFactory sslContextFactory = getSslContextFactory();
sslContextFactory.setKeyStorePath(config.jks);
sslContextFactory.setKeyStorePassword(config.jks_pass);
if (config.jks_alias != null) {
sslContextFactory.setCertAlias(config.jks_alias);
Expand All @@ -84,6 +86,22 @@ Server createJettyServer(String ip, int port) {
return jettyServer;
}

/**
* A method which tries to get the proper SslContextFactory - for older Jetty 9 versions SslContextFactory,
* and for newer ones (which fail with the former one) SslContextFactory$Server
*/
protected static SslContextFactory getSslContextFactory() {
try {
return (SslContextFactory) Class.forName("org.eclipse.jetty.util.ssl.SslContextFactory$Server").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
try {
return (SslContextFactory) Class.forName("org.eclipse.jetty.util.ssl.SslContextFactory").newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
}

static HttpConfiguration makeHttpConfiguration(ConnectionConfiguration cfg) {
final HttpConfiguration httpConfiguration = new HttpConfiguration();
httpConfiguration.setSendServerVersion(false);
Expand Down

0 comments on commit 6508c53

Please sign in to comment.