tiennguyen-onehouse commented on code in PR #13861:
URL: https://github.com/apache/hudi/pull/13861#discussion_r2366830341


##########
hudi-sync/hudi-datahub-sync/src/main/java/org/apache/hudi/sync/datahub/config/TlsEnabledDataHubEmitterSupplier.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hudi.sync.datahub.config;
+
+import datahub.client.rest.RestEmitter;
+import datahub.shaded.org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
+import 
datahub.shaded.org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
+import org.apache.hudi.common.config.TypedProperties;
+import org.apache.hudi.sync.datahub.HoodieDataHubSyncException;
+import org.apache.http.ssl.SSLContextBuilder;
+import org.apache.http.ssl.SSLContexts;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.net.ssl.SSLContext;
+import java.io.FileInputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.security.KeyStore;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateFactory;
+
+/**
+ * Custom DataHub emitter supplier that supports TLS configuration with CA 
certificates.
+ * This class reads TLS configuration from Hudi properties and creates a 
RestEmitter
+ * with proper SSL/TLS context for secure communication with DataHub servers.
+ */
+public class TlsEnabledDataHubEmitterSupplier implements 
DataHubEmitterSupplier {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(TlsEnabledDataHubEmitterSupplier.class);
+
+  private final TypedProperties config;
+
+  public TlsEnabledDataHubEmitterSupplier(TypedProperties config) {
+    this.config = config;
+  }
+
+  @Override
+  public RestEmitter get() {
+    try {
+      String serverUrl = 
config.getString(DataHubSyncConfig.META_SYNC_DATAHUB_EMITTER_SERVER.key(), 
null);
+      if (serverUrl == null || serverUrl.isEmpty()) {
+        throw new IllegalArgumentException(
+            "DataHub server URL must be specified with " + 
DataHubSyncConfig.META_SYNC_DATAHUB_EMITTER_SERVER.key());
+      }
+
+      String token = 
config.getString(DataHubSyncConfig.META_SYNC_DATAHUB_EMITTER_TOKEN.key(), null);
+      String caCertPath = 
config.getString(DataHubSyncConfig.META_SYNC_DATAHUB_TLS_CA_CERT_PATH.key(), 
null);
+      String keystorePath = 
config.getString(DataHubSyncConfig.META_SYNC_DATAHUB_TLS_KEYSTORE_PATH.key(), 
null);
+      String keystorePassword = 
config.getString(DataHubSyncConfig.META_SYNC_DATAHUB_TLS_KEYSTORE_PASSWORD.key(),
 null);
+      String truststorePath = 
config.getString(DataHubSyncConfig.META_SYNC_DATAHUB_TLS_TRUSTSTORE_PATH.key(), 
null);
+      String truststorePassword = 
config.getString(DataHubSyncConfig.META_SYNC_DATAHUB_TLS_TRUSTSTORE_PASSWORD.key(),
 null);
+
+      LOG.info("Creating DataHub RestEmitter with TLS configuration for 
server: {}", serverUrl);
+
+      return RestEmitter.create(builder -> {
+        builder.server(serverUrl);
+        
+        if (token != null && !token.isEmpty()) {
+          builder.token(token);
+        }
+        
+        // Configure TLS/SSL context if any TLS configuration is provided
+        if (hasTlsConfiguration(caCertPath, keystorePath, truststorePath)) {
+          LOG.info("Configuring TLS for DataHub connection");
+          SSLContext sslContext = createSSLContext(caCertPath, keystorePath, 
keystorePassword, truststorePath, truststorePassword);
+          
+          builder.customizeHttpAsyncClient(httpClientBuilder -> {
+            ClientTlsStrategyBuilder tlsStrategyBuilder = 
ClientTlsStrategyBuilder.create();
+            tlsStrategyBuilder.setSslContext(sslContext);
+
+            PoolingAsyncClientConnectionManagerBuilder 
connectionManagerBuilder =
+                PoolingAsyncClientConnectionManagerBuilder.create();
+            
connectionManagerBuilder.setTlsStrategy(tlsStrategyBuilder.build());
+
+            
httpClientBuilder.setConnectionManager(connectionManagerBuilder.build());
+          });
+          LOG.info("Successfully configured TLS for DataHub connection");
+        }
+      });
+    } catch (Exception e) {
+      throw new HoodieDataHubSyncException("Failed to create TLS-enabled 
DataHub emitter", e);
+    }
+  }
+
+  private boolean hasTlsConfiguration(String caCertPath, String keystorePath, 
String truststorePath) {
+    return (caCertPath != null && !caCertPath.isEmpty())
+        || (keystorePath != null && !keystorePath.isEmpty())
+        || (truststorePath != null && !truststorePath.isEmpty());
+  }
+
+  private SSLContext createSSLContext(String caCertPath, String keystorePath, 
String keystorePassword, 
+                                      String truststorePath, String 
truststorePassword) throws HoodieDataHubSyncException {
+    try {
+      SSLContextBuilder sslContextBuilder = SSLContexts.custom();
+
+      // Configure client keystore for mutual TLS authentication
+      if (keystorePath != null && !keystorePath.isEmpty()) {
+        if (!Files.exists(Paths.get(keystorePath))) {
+          throw new HoodieDataHubSyncException("Keystore file not found: " + 
keystorePath);
+        }
+        if (keystorePassword == null || keystorePassword.isEmpty()) {
+          LOG.warn("No password provided for keystore {}. Using empty password 
- consider using password-protected keystores for better security.", 
keystorePath);
+        }
+        LOG.info("Loading keystore from: {}", keystorePath);
+        KeyStore keyStore = KeyStore.getInstance("PKCS12");
+        char[] keystorePasswordChars = (keystorePassword != null && 
!keystorePassword.isEmpty()) 
+            ? keystorePassword.toCharArray() : new char[0];
+        try (FileInputStream keystoreInputStream = new 
FileInputStream(keystorePath)) {
+          keyStore.load(keystoreInputStream, keystorePasswordChars);
+        }
+        sslContextBuilder.loadKeyMaterial(keyStore, keystorePasswordChars);
+      }
+
+      // Configure truststore or CA certificate for server certificate 
verification
+      if (truststorePath != null && !truststorePath.isEmpty()) {
+        if (!Files.exists(Paths.get(truststorePath))) {

Review Comment:
   yeah you got a point. We do use HadoopFSUtils in Hudi but since certs are 
gonna be local. I think this should be alright.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to