byteroll commented on code in PR #2515:
URL: https://github.com/apache/celeborn/pull/2515#discussion_r2898892477


##########
master/src/test/java/org/apache/celeborn/service/deploy/master/clustermeta/ha/SSLRatisMasterStatusSystemSuiteJ.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.celeborn.service.deploy.master.clustermeta.ha;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.security.KeyPair;
+import java.security.cert.X509Certificate;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.TrustManager;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import org.apache.celeborn.common.CelebornConf;
+import org.apache.celeborn.common.CelebornConf$;
+import org.apache.celeborn.common.network.ssl.SSLFactory;
+import org.apache.celeborn.common.network.ssl.SslSampleConfigs;
+import org.apache.celeborn.common.protocol.TransportModuleConstants;
+import org.apache.celeborn.common.util.Utils;
+
+public class SSLRatisMasterStatusSystemSuiteJ extends 
RatisMasterStatusSystemSuiteJ {
+
+  private static final CelebornConf confWithHostPreferred = new CelebornConf();
+
+  static {
+    confWithHostPreferred.set(CelebornConf$.MODULE$.NETWORK_BIND_PREFER_IP(), 
false);
+  }
+
+  private static class CertificateData {
+    final File file;
+    final KeyPair keyPair;
+    final X509Certificate cert;
+
+    // If caData is null, we are generating for CA - else for a cert which is 
using the ca
+    // from caData
+    CertificateData(CertificateData caData) throws Exception {
+      this.file = File.createTempFile("file", ".jks");
+      file.deleteOnExit();
+
+      this.keyPair = SslSampleConfigs.generateKeyPair("RSA");
+
+      // for both ca and cert, we are simply using the same machien as CN
+      String hostname = Utils.localHostName(confWithHostPreferred);
+      final String dn = "CN=" + hostname + ",O=MyCompany,C=US";
+
+      if (null != caData) {
+        this.cert =
+            SslSampleConfigs.generateCertificate(
+                dn,
+                keyPair,
+                365,
+                "SHA256withRSA",
+                false,
+                new String[] {hostname},
+                caData.keyPair,
+                caData.cert);
+        SslSampleConfigs.createKeyStore(
+            file, "password", "password", "cert", keyPair.getPrivate(), cert);
+      } else {
+        this.cert =
+            SslSampleConfigs.generateCertificate(
+                dn, keyPair, 365, "SHA256withRSA", true, null, null, null);
+        SslSampleConfigs.createTrustStore(file, "password", "ca", cert);
+      }
+    }
+  }
+
+  private static final AtomicReference<CertificateData> caData;
+
+  static {
+    try {
+      caData = new AtomicReference<>(new CertificateData(null));
+    } catch (Exception ex) {
+      throw new IllegalStateException("Unable to initialize", ex);
+    }
+  }
+
+  @BeforeClass
+  public static void init() throws Exception {
+
+    resetRaftServer(
+        configureSsl(caData.get(), configureServerConf(new CelebornConf(), 1)),
+        configureSsl(caData.get(), configureServerConf(new CelebornConf(), 2)),
+        configureSsl(caData.get(), configureServerConf(new CelebornConf(), 3)),
+        true);
+  }
+
+  static CelebornConf configureSsl(CertificateData ca, CelebornConf conf) 
throws Exception {
+    conf.set("celeborn.master.ha.ratis.raft.rpc.type", "GRPC");
+
+    CertificateData server = new CertificateData(ca);
+
+    final String module = TransportModuleConstants.RPC_SERVICE_MODULE;
+
+    conf.set("celeborn.ssl." + module + ".enabled", "true");
+    conf.set("celeborn.ssl." + module + ".keyStore", 
server.file.getAbsolutePath());
+
+    conf.set("celeborn.ssl." + module + ".keyStorePassword", "password");
+    conf.set("celeborn.ssl." + module + ".keyPassword", "password");
+    conf.set("celeborn.ssl." + module + ".privateKeyPassword", "password");
+    conf.set("celeborn.ssl." + module + ".protocol", "TLSv1.2");
+    conf.set("celeborn.ssl." + module + ".trustStore", 
ca.file.getAbsolutePath());
+    conf.set("celeborn.ssl." + module + ".trustStorePassword", "password");
+
+    return conf;
+  }
+
+  @Test
+  public void testSslEnabled() throws Exception {
+    assertTrue(isSslServer(RATISSERVER1.getRaftAddress(), 
RATISSERVER1.getRaftPort()));
+    assertTrue(isSslServer(RATISSERVER2.getRaftAddress(), 
RATISSERVER2.getRaftPort()));
+    assertTrue(isSslServer(RATISSERVER3.getRaftAddress(), 
RATISSERVER3.getRaftPort()));
+  }
+
+  // Validate if the server listening at the port is using TLS or not.
+  static boolean isSslServer(InetAddress address, int port) throws Exception {
+    try (SSLSocket socket = createSslSocket(address, port)) {
+      socket.setSoTimeout(5000);
+      socket.startHandshake();
+      // handshake succeeded, this will always return true in this case
+      return socket.getSession().isValid();
+    }
+  }
+
+  private static SSLSocket createSslSocket(InetAddress address, int port) 
throws Exception {
+    TrustManager trustStore = 
SSLFactory.defaultTrustManagers(caData.get().file, "password")[0];
+    SSLContext context = SSLContext.getInstance("TLS");

Review Comment:
   the exception stack
   ```
   java.net.SocketException: Broken pipe (Write failed)
   
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
        at 
sun.security.ssl.SSLSocketOutputRecord.encodeChangeCipherSpec(SSLSocketOutputRecord.java:233)
        at 
sun.security.ssl.OutputRecord.changeWriteCiphers(OutputRecord.java:188)
        at 
sun.security.ssl.ChangeCipherSpec$T10ChangeCipherSpecProducer.produce(ChangeCipherSpec.java:118)
        at 
sun.security.ssl.Finished$T12FinishedProducer.onProduceFinished(Finished.java:390)
        at 
sun.security.ssl.Finished$T12FinishedProducer.produce(Finished.java:374)
        at sun.security.ssl.SSLHandshake.produce(SSLHandshake.java:420)
        at 
sun.security.ssl.ServerHelloDone$ServerHelloDoneConsumer.consume(ServerHelloDone.java:182)
        at sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:376)
        at sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:479)
        at sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:457)
        at sun.security.ssl.TransportContext.dispatch(TransportContext.java:200)
        at sun.security.ssl.SSLTransport.decode(SSLTransport.java:155)
        at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1320)
        at 
sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1233)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:417)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:389)
        at 
org.apache.celeborn.service.deploy.master.clustermeta.ha.SSLRatisMasterStatusSystemSuiteJ.isSslServer(SSLRatisMasterStatusSystemSuiteJ.java:141)
   ```



-- 
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