|
1 | 1 | package com.utmstack.userauditor.checks; |
2 | 2 |
|
3 | 3 | import com.utmstack.userauditor.service.elasticsearch.Constants; |
| 4 | +import okhttp3.Credentials; |
4 | 5 | import okhttp3.OkHttpClient; |
5 | 6 | import okhttp3.Request; |
6 | 7 | import okhttp3.Response; |
7 | 8 | import org.springframework.util.Assert; |
8 | 9 |
|
| 10 | +import javax.net.ssl.SSLContext; |
| 11 | +import javax.net.ssl.TrustManager; |
| 12 | +import javax.net.ssl.X509TrustManager; |
| 13 | +import java.security.cert.X509Certificate; |
9 | 14 | import java.util.Objects; |
10 | 15 |
|
11 | 16 | public class ElasticsearchConnectionCheck { |
@@ -46,21 +51,49 @@ private void pingElasticsearch() { |
46 | 51 | try { |
47 | 52 | String elasticHost = System.getenv(Constants.ENV_ELASTICSEARCH_HOST); |
48 | 53 | String elasticPort = System.getenv(Constants.ENV_ELASTICSEARCH_PORT); |
| 54 | + String user = System.getenv(Constants.ENV_ELASTICSEARCH_USER); |
| 55 | + String password = System.getenv(Constants.ENV_ELASTICSEARCH_PASSWORD); |
49 | 56 |
|
50 | 57 | Assert.hasText(elasticHost, "Missing elasticsearch host configuration value"); |
51 | 58 | Assert.hasText(elasticPort, "Missing elasticsearch port configuration value"); |
| 59 | + Assert.hasText(user, "Missing elasticsearch user configuration value"); |
| 60 | + Assert.hasText(password, "Missing elasticsearch password configuration value"); |
52 | 61 |
|
53 | | - final String ELASTIC_URL = String.format("http://%1$s:%2$s", |
54 | | - System.getenv(Constants.ENV_ELASTICSEARCH_HOST), System.getenv(Constants.ENV_ELASTICSEARCH_PORT)); |
| 62 | + final String ELASTIC_URL = String.format("https://%1$s:%2$s", elasticHost, elasticPort); |
55 | 63 |
|
56 | | - OkHttpClient client = new OkHttpClient().newBuilder().build(); |
57 | | - Request rq = new Request.Builder().url(ELASTIC_URL).build(); |
| 64 | + OkHttpClient client = createTrustAllClient(); |
| 65 | + Request rq = new Request.Builder() |
| 66 | + .url(ELASTIC_URL) |
| 67 | + .header("Authorization", Credentials.basic(user, password)) |
| 68 | + .build(); |
58 | 69 | Response rs = client.newCall(rq).execute(); |
59 | 70 | Objects.requireNonNull(rs.body()).close(); |
60 | 71 | if (!rs.isSuccessful()) |
61 | | - throw new RuntimeException(); |
| 72 | + throw new RuntimeException("HTTP " + rs.code()); |
62 | 73 | } catch (Exception e) { |
63 | 74 | throw new RuntimeException(ctx + ": " + e.getLocalizedMessage()); |
64 | 75 | } |
65 | 76 | } |
| 77 | + |
| 78 | + private OkHttpClient createTrustAllClient() { |
| 79 | + try { |
| 80 | + TrustManager[] trustAllCerts = new TrustManager[]{ |
| 81 | + new X509TrustManager() { |
| 82 | + public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } |
| 83 | + public void checkClientTrusted(X509Certificate[] certs, String authType) {} |
| 84 | + public void checkServerTrusted(X509Certificate[] certs, String authType) {} |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 89 | + sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); |
| 90 | + |
| 91 | + return new OkHttpClient.Builder() |
| 92 | + .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0]) |
| 93 | + .hostnameVerifier((hostname, session) -> true) |
| 94 | + .build(); |
| 95 | + } catch (Exception e) { |
| 96 | + throw new RuntimeException("Failed to create SSL client: " + e.getMessage()); |
| 97 | + } |
| 98 | + } |
66 | 99 | } |
0 commit comments