Skip to content

Commit 7bc8ab3

Browse files
swallezl-trotta
authored andcommitted
Add insecure SSLContext
1 parent 7575d1a commit 7bc8ab3

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

java-client/src/main/java/co/elastic/clients/elasticsearch/_helpers/builders/ElasticsearchClientBuilderBase.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,20 @@ public ElasticsearchClientBuilderBase<T> apiKey(String apiKey) {
121121

122122
/**
123123
* Set the SSL context. See {@link co.elastic.clients.transport.TransportUtils} to create it
124-
* from certificate files or fingerprint.
124+
* from certificate files or a certificate fingerprint.
125+
*
126+
* @see co.elastic.clients.transport.TransportUtils
125127
*/
126128
public ElasticsearchClientBuilderBase<T> sslContext(SSLContext sslContext) {
127129
this.sslContext = sslContext;
128130
return this;
129131
}
130132

133+
public ElasticsearchClientBuilderBase<T> setHosts(List<URI> hosts) {
134+
this.hosts = hosts;
135+
return this;
136+
}
137+
131138
/**
132139
* Set the JSON mapper.
133140
*/

java-client/src/main/java/co/elastic/clients/transport/TransportUtils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.FileInputStream;
2727
import java.io.IOException;
2828
import java.io.InputStream;
29+
import java.security.GeneralSecurityException;
2930
import java.security.KeyManagementException;
3031
import java.security.KeyStore;
3132
import java.security.KeyStoreException;
@@ -144,4 +145,40 @@ public X509Certificate[] getAcceptedIssuers() {
144145
throw new RuntimeException(e);
145146
}
146147
}
148+
149+
/**
150+
* Returns an <b>insecure</b> SSLContext that will accept any server certificate.
151+
* <p>
152+
* <b>Use with care as it allows man-in-the-middle attacks.</b>
153+
*/
154+
public static SSLContext insecureSSLContext() {
155+
SSLContext result;
156+
157+
X509TrustManager trustManager = new X509TrustManager() {
158+
@Override
159+
public void checkClientTrusted(X509Certificate[] certs, String authType) {
160+
// Accept anything
161+
}
162+
163+
@Override
164+
public void checkServerTrusted(X509Certificate[] certs, String authType) {
165+
// Accept anything
166+
}
167+
168+
@Override
169+
public X509Certificate[] getAcceptedIssuers() {
170+
return new X509Certificate[0];
171+
}
172+
};
173+
174+
try {
175+
result = SSLContext.getInstance("SSL");
176+
result.init(null, new X509TrustManager[] { trustManager }, null);
177+
} catch (GeneralSecurityException e) {
178+
// An exception here means SSL is not supported, which is unlikely
179+
throw new RuntimeException(e);
180+
}
181+
182+
return result;
183+
}
147184
}

java-client/src/test/java/co/elastic/clients/transport/TransportUtilsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public void testCaCertificate() throws Exception {
7777
);
7878
}
7979

80+
@Test void testInsecureContext() throws Exception {
81+
checkConnection(TransportUtils.insecureSSLContext());
82+
}
83+
8084
private void checkConnection(SSLContext sslContext) throws Exception {
8185
var server = ElasticsearchTestServer.global();
8286
var esClient = ElasticsearchTestClient.createClient(server.url(), null, sslContext);

0 commit comments

Comments
 (0)