package net.hzvtc.config; import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.json.JsonpMapper; import co.elastic.clients.json.jackson.JacksonJsonpMapper; import co.elastic.clients.transport.ElasticsearchTransport; import co.elastic.clients.transport.rest_client.RestClientTransport; import java.io.IOException; import java.io.InputStream; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import net.hzvtc.config.JacksonConfiguration; import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.apache.http.message.BasicHeader; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.SSLContexts; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.util.StringUtils; @Configuration public class ESClientConfig { private static final Logger log = LoggerFactory.getLogger(ESClientConfig.class); @Value(value="${spring.elastic.hosts}") private String hosts; @Value(value="${spring.elastic.http}") private String http; @Value(value="${spring.elastic.apiKey}") private String apiKey; @Value(value="${spring.elastic.username}") private String username; @Value(value="${spring.elastic.password}") private String passwd; private static String certName; @Value(value="${spring.elastic.certName}") public void setAppKey(String certName) { ESClientConfig.certName = certName; } private HttpHost[] toHttpHost() { if (!StringUtils.hasLength((String)this.hosts)) { throw new RuntimeException("invalid elasticsearch configuration"); } String[] hostArray = this.hosts.split(","); HttpHost[] httpHosts = new HttpHost[hostArray.length]; for (int i = 0; i < hostArray.length; ++i) { HttpHost httpHost; String[] strings = hostArray[i].split(":"); httpHosts[i] = httpHost = new HttpHost(strings[0], Integer.parseInt(strings[1]), "https"); } return httpHosts; } @Bean(value={"clientByPasswd"}) public ElasticsearchClient clientByPasswd() { ElasticsearchTransport transport = ESClientConfig.getElasticsearchTransport(this.username, this.passwd, this.toHttpHost()); return new ElasticsearchClient(transport); } private static SSLContext buildSSLContext() { ClassPathResource resource = new ClassPathResource(certName); SSLContext sslContext = null; try { Certificate trustedCa; CertificateFactory factory = CertificateFactory.getInstance("X.509"); try (InputStream is = resource.getInputStream();){ trustedCa = factory.generateCertificate(is); } KeyStore trustStore = KeyStore.getInstance("pkcs12"); trustStore.load(null, null); trustStore.setCertificateEntry("ca", trustedCa); SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial(trustStore, null); sslContext = sslContextBuilder.build(); } catch (IOException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException e) { log.error("ES\u8fde\u63a5\u8ba4\u8bc1\u5931\u8d25", (Throwable)e); } return sslContext; } private static ElasticsearchTransport getElasticsearchTransport(String username, String passwd, HttpHost ... hosts) { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, (Credentials)new UsernamePasswordCredentials(username, passwd)); RestClientBuilder.HttpClientConfigCallback callback = arg_0 -> ESClientConfig.lambda$getElasticsearchTransport$0((CredentialsProvider)credentialsProvider, arg_0); RestClient client = RestClient.builder((HttpHost[])hosts).setHttpClientConfigCallback(callback).build(); return new RestClientTransport(client, (JsonpMapper)new JacksonJsonpMapper(JacksonConfiguration.createObjectMapper())); } private static ElasticsearchTransport getElasticsearchTransport(String apiKey, HttpHost ... hosts) { Header[] headers = new Header[]{new BasicHeader("Authorization", "ApiKey " + apiKey)}; RestClientBuilder.HttpClientConfigCallback callback = httpAsyncClientBuilder -> httpAsyncClientBuilder.setSSLContext(ESClientConfig.buildSSLContext()).setSSLHostnameVerifier((HostnameVerifier)NoopHostnameVerifier.INSTANCE); RestClient client = RestClient.builder((HttpHost[])hosts).setHttpClientConfigCallback(callback).setDefaultHeaders(headers).build(); return new RestClientTransport(client, (JsonpMapper)new JacksonJsonpMapper()); } public void setHosts(String hosts) { this.hosts = hosts; } public void setHttp(String http) { this.http = http; } public void setApiKey(String apiKey) { this.apiKey = apiKey; } public void setUsername(String username) { this.username = username; } public void setPasswd(String passwd) { this.passwd = passwd; } private static /* synthetic */ HttpAsyncClientBuilder lambda$getElasticsearchTransport$0(CredentialsProvider credentialsProvider, HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setSSLContext(ESClientConfig.buildSSLContext()).setSSLHostnameVerifier((HostnameVerifier)NoopHostnameVerifier.INSTANCE).setDefaultCredentialsProvider(credentialsProvider); } }