|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.elasticsearch.util; |
| 19 | + |
| 20 | +import org.apache.seatunnel.api.configuration.ReadonlyConfig; |
| 21 | +import org.apache.seatunnel.connectors.seatunnel.elasticsearch.config.ElasticsearchBaseOptions; |
| 22 | + |
| 23 | +import org.apache.http.auth.AuthScope; |
| 24 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 25 | +import org.apache.http.client.CredentialsProvider; |
| 26 | +import org.apache.http.conn.ssl.NoopHostnameVerifier; |
| 27 | +import org.apache.http.conn.ssl.TrustAllStrategy; |
| 28 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 29 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 30 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 31 | +import org.apache.http.impl.client.HttpClients; |
| 32 | +import org.apache.http.ssl.SSLContexts; |
| 33 | + |
| 34 | +import lombok.extern.slf4j.Slf4j; |
| 35 | + |
| 36 | +import javax.net.ssl.SSLContext; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.security.GeneralSecurityException; |
| 40 | +import java.util.Optional; |
| 41 | + |
| 42 | +@Slf4j |
| 43 | +public class HttpClientUtil { |
| 44 | + |
| 45 | + public CloseableHttpClient getHttpClient(ReadonlyConfig config) { |
| 46 | + // 1. Initialize credentials provider |
| 47 | + CredentialsProvider credsProvider = new BasicCredentialsProvider(); |
| 48 | + Optional<String> username = config.getOptional(ElasticsearchBaseOptions.USERNAME); |
| 49 | + Optional<String> password = config.getOptional(ElasticsearchBaseOptions.PASSWORD); |
| 50 | + |
| 51 | + // 2. Configure SSL |
| 52 | + boolean tlsVerifyCertificate = config.get(ElasticsearchBaseOptions.TLS_VERIFY_CERTIFICATE); |
| 53 | + boolean tlsVerifyHostnames = config.get(ElasticsearchBaseOptions.TLS_VERIFY_HOSTNAME); |
| 54 | + |
| 55 | + SSLContext sslContext = buildSSLContext(config, tlsVerifyCertificate); |
| 56 | + |
| 57 | + // 3. Set credentials if provided |
| 58 | + username.ifPresent( |
| 59 | + u -> |
| 60 | + password.ifPresent( |
| 61 | + p -> |
| 62 | + credsProvider.setCredentials( |
| 63 | + new AuthScope( |
| 64 | + AuthScope.ANY_HOST, AuthScope.ANY_PORT), |
| 65 | + new UsernamePasswordCredentials(u, p)))); |
| 66 | + |
| 67 | + // 4. Build HttpClient |
| 68 | + HttpClientBuilder httpClientBuilder = |
| 69 | + HttpClients.custom().setDefaultCredentialsProvider(credsProvider); |
| 70 | + |
| 71 | + if (sslContext != null) { |
| 72 | + httpClientBuilder.setSSLContext(sslContext); |
| 73 | + } |
| 74 | + |
| 75 | + if (!tlsVerifyHostnames) { |
| 76 | + httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); |
| 77 | + } |
| 78 | + |
| 79 | + return httpClientBuilder.build(); |
| 80 | + } |
| 81 | + |
| 82 | + private SSLContext buildSSLContext(ReadonlyConfig config, boolean tlsVerifyCertificate) { |
| 83 | + try { |
| 84 | + if (tlsVerifyCertificate) { |
| 85 | + Optional<String> keystorePath = |
| 86 | + config.getOptional(ElasticsearchBaseOptions.TLS_KEY_STORE_PATH); |
| 87 | + Optional<String> keystorePassword = |
| 88 | + config.getOptional(ElasticsearchBaseOptions.TLS_KEY_STORE_PASSWORD); |
| 89 | + Optional<String> truststorePath = |
| 90 | + config.getOptional(ElasticsearchBaseOptions.TLS_TRUST_STORE_PATH); |
| 91 | + Optional<String> truststorePassword = |
| 92 | + config.getOptional(ElasticsearchBaseOptions.TLS_TRUST_STORE_PASSWORD); |
| 93 | + |
| 94 | + return SSLUtils.buildSSLContext( |
| 95 | + keystorePath, keystorePassword, truststorePath, truststorePassword) |
| 96 | + .orElseThrow(() -> new RuntimeException("Failed to build SSLContext")); |
| 97 | + } else { |
| 98 | + log.warn( |
| 99 | + "TLS certificate verification is disabled. This is not secure for production environments."); |
| 100 | + return SSLContexts.custom().loadTrustMaterial(new TrustAllStrategy()).build(); |
| 101 | + } |
| 102 | + } catch (GeneralSecurityException | IOException e) { |
| 103 | + throw new RuntimeException("Failed to initialize SSLContext", e); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments