-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nio http transport to security plugin #32018
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
26ea13e
WIP
Tim-Brooks 3f0fee0
Extract exception handler
Tim-Brooks b8b84d7
WIP
Tim-Brooks 9d5887a
Enable tests
Tim-Brooks 9977af6
Fix checkstyle
Tim-Brooks a0fbd07
Fix forbidden
Tim-Brooks 63ae74f
Issue from review
Tim-Brooks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...gin/security/src/main/java/org/elasticsearch/xpack/security/transport/SSLEngineUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.security.transport; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.handler.ssl.SslHandler; | ||
import org.elasticsearch.http.HttpChannel; | ||
import org.elasticsearch.http.netty4.Netty4HttpChannel; | ||
import org.elasticsearch.http.nio.NioHttpChannel; | ||
import org.elasticsearch.nio.SocketChannelContext; | ||
import org.elasticsearch.transport.TcpChannel; | ||
import org.elasticsearch.transport.netty4.Netty4TcpChannel; | ||
import org.elasticsearch.transport.nio.NioTcpChannel; | ||
import org.elasticsearch.xpack.security.transport.nio.SSLChannelContext; | ||
|
||
import javax.net.ssl.SSLEngine; | ||
|
||
public class SSLEngineUtils { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a private constructor so no one instantiates this class |
||
|
||
public static SSLEngine getSSLEngine(HttpChannel httpChannel) { | ||
if (httpChannel instanceof Netty4HttpChannel) { | ||
Channel nettyChannel = ((Netty4HttpChannel) httpChannel).getNettyChannel(); | ||
SslHandler handler = nettyChannel.pipeline().get(SslHandler.class); | ||
assert handler != null : "Must have SslHandler"; | ||
return handler.engine(); | ||
} else if (httpChannel instanceof NioHttpChannel) { | ||
SocketChannelContext context = ((NioHttpChannel) httpChannel).getContext(); | ||
assert context instanceof SSLChannelContext : "Must be SSLChannelContext.class, found: " + context.getClass(); | ||
return ((SSLChannelContext) context).getSSLEngine(); | ||
} else { | ||
throw new AssertionError("Unknown channel class type: " + httpChannel.getClass()); | ||
} | ||
} | ||
|
||
public static SSLEngine getSSLEngine(TcpChannel tcpChannel) { | ||
if (tcpChannel instanceof Netty4TcpChannel) { | ||
Channel nettyChannel = ((Netty4TcpChannel) tcpChannel).getNettyChannel(); | ||
SslHandler handler = nettyChannel.pipeline().get(SslHandler.class); | ||
assert handler != null : "Must have SslHandler"; | ||
return handler.engine(); | ||
} else if (tcpChannel instanceof NioTcpChannel) { | ||
SocketChannelContext context = ((NioTcpChannel) tcpChannel).getContext(); | ||
assert context instanceof SSLChannelContext : "Must be SSLChannelContext.class, found: " + context.getClass(); | ||
return ((SSLChannelContext) context).getSSLEngine(); | ||
} else { | ||
throw new AssertionError("Unknown channel class type: " + tcpChannel.getClass()); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...rc/main/java/org/elasticsearch/xpack/security/transport/SecurityHttpExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.security.transport; | ||
|
||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.message.ParameterizedMessage; | ||
import org.elasticsearch.common.component.Lifecycle; | ||
import org.elasticsearch.common.network.CloseableChannel; | ||
import org.elasticsearch.http.HttpChannel; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
import static org.elasticsearch.xpack.core.security.transport.SSLExceptionHelper.isCloseDuringHandshakeException; | ||
import static org.elasticsearch.xpack.core.security.transport.SSLExceptionHelper.isNotSslRecordException; | ||
import static org.elasticsearch.xpack.core.security.transport.SSLExceptionHelper.isReceivedCertificateUnknownException; | ||
|
||
public class SecurityHttpExceptionHandler implements BiConsumer<HttpChannel, Exception> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make it final |
||
|
||
private final Lifecycle lifecycle; | ||
private final Logger logger; | ||
private final BiConsumer<HttpChannel, Exception> fallback; | ||
|
||
public SecurityHttpExceptionHandler(Logger logger, Lifecycle lifecycle, BiConsumer<HttpChannel, Exception> fallback) { | ||
this.lifecycle = lifecycle; | ||
this.logger = logger; | ||
this.fallback = fallback; | ||
} | ||
|
||
public void accept(HttpChannel channel, Exception e) { | ||
if (!lifecycle.started()) { | ||
return; | ||
} | ||
|
||
if (isNotSslRecordException(e)) { | ||
if (logger.isTraceEnabled()) { | ||
logger.trace(new ParameterizedMessage("received plaintext http traffic on a https channel, closing connection {}", | ||
channel), e); | ||
} else { | ||
logger.warn("received plaintext http traffic on a https channel, closing connection {}", channel); | ||
} | ||
CloseableChannel.closeChannel(channel); | ||
} else if (isCloseDuringHandshakeException(e)) { | ||
if (logger.isTraceEnabled()) { | ||
logger.trace(new ParameterizedMessage("connection {} closed during ssl handshake", channel), e); | ||
} else { | ||
logger.warn("connection {} closed during ssl handshake", channel); | ||
} | ||
CloseableChannel.closeChannel(channel); | ||
} else if (isReceivedCertificateUnknownException(e)) { | ||
if (logger.isTraceEnabled()) { | ||
logger.trace(new ParameterizedMessage("http client did not trust server's certificate, closing connection {}", | ||
channel), e); | ||
} else { | ||
logger.warn("http client did not trust this server's certificate, closing connection {}", channel); | ||
} | ||
CloseableChannel.closeChannel(channel); | ||
} else { | ||
fallback.accept(channel, e); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...curity/src/main/java/org/elasticsearch/xpack/security/transport/SecurityHttpSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.security.transport; | ||
|
||
import org.elasticsearch.common.settings.Settings; | ||
|
||
import static org.elasticsearch.http.HttpTransportSettings.SETTING_HTTP_COMPRESSION; | ||
import static org.elasticsearch.xpack.core.XPackSettings.HTTP_SSL_ENABLED; | ||
|
||
public final class SecurityHttpSettings { | ||
|
||
private SecurityHttpSettings() {} | ||
|
||
public static void overrideSettings(Settings.Builder settingsBuilder, Settings settings) { | ||
if (HTTP_SSL_ENABLED.get(settings) && SETTING_HTTP_COMPRESSION.exists(settings) == false) { | ||
settingsBuilder.put(SETTING_HTTP_COMPRESSION.getKey(), false); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we move extractClientCertificates into SSLEngineUtils and just have a single method call here?