Skip to content

Commit

Permalink
Jetty 10.0.x 4814 configuring connection factory (#4815)
Browse files Browse the repository at this point in the history
* Issue #4814 Configuring Connection Factory

Redo of this PR without Attributes improvements (moved to #4816).
Add a ConnectionFactory.Configuring interface to all connectors to be configured during doStart.
I have some concern about shared HttpConfigurations.

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* updates from review

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw authored Apr 29, 2020
1 parent 4a46266 commit 81c4663
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ public int getAcceptors()
@Override
protected void doStart() throws Exception
{
getConnectionFactories().stream()
.filter(ConnectionFactory.Configuring.class::isInstance)
.map(ConnectionFactory.Configuring.class::cast)
.forEach(configuring -> configuring.configure(this));

_shutdown = new Graceful.Shutdown(this)
{
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,16 @@ enum Detection
*/
Detection detect(ByteBuffer buffer);
}

/**
* A ConnectionFactory that can configure the connector.
*/
interface Configuring extends ConnectionFactory
{
/**
* Called during {@link Connector#start()}.
* @param connector The connector to configure
*/
void configure(Connector connector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;

public class SslConnectionFactory extends AbstractConnectionFactory implements ConnectionFactory.Detecting
public class SslConnectionFactory extends AbstractConnectionFactory implements ConnectionFactory.Detecting, ConnectionFactory.Configuring
{
private static final int TLS_ALERT_FRAME_TYPE = 0x15;
private static final int TLS_HANDSHAKE_FRAME_TYPE = 0x16;
Expand All @@ -42,6 +42,7 @@ public class SslConnectionFactory extends AbstractConnectionFactory implements C
private final String _nextProtocol;
private boolean _directBuffersForEncryption = false;
private boolean _directBuffersForDecryption = false;
private boolean _ensureSecureRequestCustomizer = true;

public SslConnectionFactory()
{
Expand Down Expand Up @@ -91,6 +92,21 @@ public String getNextProtocol()
return _nextProtocol;
}

public boolean isEnsureSecureRequestCustomizer()
{
return _ensureSecureRequestCustomizer;
}

/**
* @param ensureSecureRequestCustomizer True if this factory ensures that all {@link HttpConfiguration}s on
* associated {@link Connector}s have an {@link SecureRequestCustomizer} instance.
* @see ConnectionFactory.Configuring
*/
public void setEnsureSecureRequestCustomizer(boolean ensureSecureRequestCustomizer)
{
_ensureSecureRequestCustomizer = ensureSecureRequestCustomizer;
}

@Override
protected void doStart() throws Exception
{
Expand All @@ -104,6 +120,19 @@ protected void doStart() throws Exception
setInputBufferSize(session.getPacketBufferSize());
}

@Override
public void configure(Connector connector)
{
if (isEnsureSecureRequestCustomizer())
{
connector.getContainedBeans(HttpConfiguration.class).forEach(configuration ->
{
if (configuration.getCustomizer(SecureRequestCustomizer.class) == null)
configuration.addCustomizer(new SecureRequestCustomizer());
});
}
}

@Override
public Detection detect(ByteBuffer buffer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SocketCustomizationListener;
Expand Down Expand Up @@ -81,16 +80,16 @@ public void before() throws Exception
httpConfig.setSecureScheme("https");
httpConfig.setSecurePort(8443);
httpConfig.setOutputBufferSize(32768);
HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
httpsConfig.addCustomizer(new SecureRequestCustomizer());

SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(keystoreFile.getAbsolutePath());
sslContextFactory.setKeyStorePassword("storepwd");

SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString());
sslConnectionFactory.setEnsureSecureRequestCustomizer(true);
ServerConnector https = _connector = new ServerConnector(_server,
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
new HttpConnectionFactory(httpsConfig));
sslConnectionFactory,
new HttpConnectionFactory());
https.setPort(0);
https.setIdleTimeout(30000);

Expand Down

0 comments on commit 81c4663

Please sign in to comment.