This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Rest mutual auth fix #279
Merged
sidheart
merged 7 commits into
opendistro-for-elasticsearch:master
from
sidheart:rest-mutual-auth-fix
Jul 27, 2020
Merged
Rest mutual auth fix #279
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
af001b6
Add required mutual auth to gRPC Server/Client
d0b6804
Add full mutual auth to gRPC client/server and augment tests
f545e2c
Implement mutual auth for the REST endpoints
de2cb4d
Fix merge issue with WireHopperTest
28033f2
Fixing up PerformanceAnalyzerWebServerTest
8894129
Modify gradle.yml for testing
dea63c4
Remove info log flooding from testing gradle.yml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,22 +16,25 @@ | |
package com.amazon.opendistro.elasticsearch.performanceanalyzer; | ||
|
||
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.sun.net.httpserver.HttpServer; | ||
import com.sun.net.httpserver.HttpsConfigurator; | ||
import com.sun.net.httpserver.HttpsParameters; | ||
import com.sun.net.httpserver.HttpsServer; | ||
|
||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.security.KeyStore; | ||
import java.security.Security; | ||
import java.security.cert.X509Certificate; | ||
import java.util.concurrent.Executors; | ||
|
||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.HttpsURLConnection; | ||
import javax.net.ssl.KeyManagerFactory; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSession; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.X509TrustManager; | ||
import javax.net.ssl.SSLEngine; | ||
import javax.net.ssl.SSLParameters; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
|
@@ -40,8 +43,10 @@ public class PerformanceAnalyzerWebServer { | |
|
||
private static final Logger LOG = LogManager.getLogger(PerformanceAnalyzerWebServer.class); | ||
public static final int WEBSERVICE_DEFAULT_PORT = 9600; | ||
public static final String WEBSERVICE_PORT_CONF_NAME = "webservice-listener-port"; | ||
@VisibleForTesting | ||
public static final String WEBSERVICE_BIND_HOST_NAME = "webservice-bind-host"; | ||
@VisibleForTesting | ||
public static final String WEBSERVICE_PORT_CONF_NAME = "webservice-listener-port"; | ||
// Use system default for max backlog. | ||
private static final int INCOMING_QUEUE_LENGTH = 1; | ||
|
||
|
@@ -66,8 +71,34 @@ public static HttpServer createInternalServer(String portFromSetting, String hos | |
return null; | ||
} | ||
|
||
/** | ||
* ClientAuthConfigurator makes the server perform client authentication if the user has set up a | ||
* certificate authority | ||
*/ | ||
private static class ClientAuthConfigurator extends HttpsConfigurator { | ||
public ClientAuthConfigurator(SSLContext sslContext) { | ||
super(sslContext); | ||
} | ||
|
||
@Override | ||
public void configure(HttpsParameters params) { | ||
final SSLParameters sslParams = getSSLContext().getDefaultSSLParameters(); | ||
if (CertificateUtils.getTrustedCasFile() != null) { | ||
LOG.debug("Enabling client auth"); | ||
final SSLEngine sslEngine = getSSLContext().createSSLEngine(); | ||
sslParams.setNeedClientAuth(true); | ||
sslParams.setCipherSuites(sslEngine.getEnabledCipherSuites()); | ||
sslParams.setProtocols(sslEngine.getEnabledProtocols()); | ||
params.setSSLParameters(sslParams); | ||
} else { | ||
LOG.debug("Not enabling client auth"); | ||
super.configure(params); | ||
} | ||
} | ||
} | ||
|
||
private static HttpServer createHttpsServer(int readerPort, String bindHost) throws Exception { | ||
HttpsServer server = null; | ||
HttpsServer server; | ||
if (bindHost != null && !bindHost.trim().isEmpty()) { | ||
LOG.info("Binding to Interface: {}", bindHost); | ||
server = | ||
|
@@ -81,38 +112,30 @@ private static HttpServer createHttpsServer(int readerPort, String bindHost) thr | |
server = HttpsServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), readerPort), INCOMING_QUEUE_LENGTH); | ||
} | ||
|
||
TrustManager[] trustAllCerts = | ||
new TrustManager[] { | ||
new X509TrustManager() { | ||
|
||
public X509Certificate[] getAcceptedIssuers() { | ||
return null; | ||
} | ||
|
||
public void checkClientTrusted(X509Certificate[] certs, String authType) {} | ||
|
||
public void checkServerTrusted(X509Certificate[] certs, String authType) {} | ||
} | ||
}; | ||
|
||
HostnameVerifier allHostsValid = | ||
new HostnameVerifier() { | ||
public boolean verify(String hostname, SSLSession session) { | ||
return true; | ||
} | ||
}; | ||
|
||
// Install the all-trusting trust manager | ||
SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); | ||
|
||
KeyStore ks = CertificateUtils.createKeyStore(); | ||
KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509"); | ||
kmf.init(ks, CertificateUtils.IN_MEMORY_PWD.toCharArray()); | ||
sslContext.init(kmf.getKeyManagers(), trustAllCerts, null); | ||
sslContext.init(kmf.getKeyManagers(), CertificateUtils.getTrustManagers(true), null); | ||
server.setHttpsConfigurator(new ClientAuthConfigurator(sslContext)); | ||
|
||
|
||
// TODO ask ktkrg why this is necessary | ||
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. Do you plan to wrap up the TODO in this PR or in a follow up ? |
||
// Try to set HttpsURLConnection defaults, our webserver can still run even if this block fails | ||
try { | ||
LOG.debug("Setting default SSLSocketFactory..."); | ||
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); | ||
LOG.debug("Default SSLSocketFactory set successfully"); | ||
HostnameVerifier allHostsValid = (hostname, session) -> true; | ||
LOG.debug("Setting default HostnameVerifier..."); | ||
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); | ||
LOG.debug("Default HostnameVerifier set successfully"); | ||
} catch (Exception e) { // Usually AccessControlException | ||
LOG.warn("Exception while trying to set URLConnection defaults", e); | ||
} | ||
|
||
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); | ||
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); | ||
server.setHttpsConfigurator(new HttpsConfigurator(sslContext)); | ||
return server; | ||
} | ||
|
||
|
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
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.
nit: additional space. static class -> static class