hasnain-db commented on code in PR #2438: URL: https://github.com/apache/celeborn/pull/2438#discussion_r1555205907
########## common/src/main/java/org/apache/celeborn/common/network/ssl/SSLFactory.java: ########## @@ -0,0 +1,407 @@ +/* + * 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.common.network.ssl; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; + +import com.google.common.io.Files; +import io.netty.buffer.ByteBufAllocator; +import io.netty.handler.ssl.SslProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.celeborn.common.util.JavaUtils; + +/** + * SSLFactory to initialize and configure use of JSSE for SSL in Celeborn. + * + * <p>Note: code was initially copied from Apache Spark. + */ +public class SSLFactory { + private static final Logger logger = LoggerFactory.getLogger(SSLFactory.class); + + /** For a configuration specifying keystore/truststore files */ + private SSLContext jdkSslContext; + + private KeyManager[] keyManagers; + private TrustManager[] trustManagers; + private String requestedProtocol; + private String[] requestedCiphers; + + private SSLFactory(final Builder b) { + this.requestedProtocol = b.requestedProtocol; + this.requestedCiphers = b.requestedCiphers; + try { + initJdkSslContext(b); + } catch (Exception e) { + throw new RuntimeException("SSLFactory creation failed", e); + } + } + + private void initJdkSslContext(final Builder b) throws IOException, GeneralSecurityException { + this.keyManagers = + null != b.keyStore ? keyManagers(b.keyStore, b.keyPassword, b.keyStorePassword) : null; + this.trustManagers = + trustStoreManagers( + b.trustStore, b.trustStorePassword, + b.trustStoreReloadingEnabled, b.trustStoreReloadIntervalMs); + this.jdkSslContext = createSSLContext(requestedProtocol, keyManagers, trustManagers); + } + + public boolean hasKeyManagers() { + // nettyServerSslContext requires keys to be specified, this path applies when + // applies only when jks is being used without keyManagers + return null == jdkSslContext || null != keyManagers; + } + + /** + * If OpenSSL is requested, this will check if an implementation is available on the local host. + * If an implementation is not available it will fall back to the JDK {@link SslProvider}. + * + * @param b + * @return + */ + private SslProvider getSslProvider(Builder b) { + return SslProvider.JDK; + } + + public void destroy() { + if (trustManagers != null) { + for (int i = 0; i < trustManagers.length; i++) { + if (trustManagers[i] instanceof ReloadingX509TrustManager) { + try { + ((ReloadingX509TrustManager) trustManagers[i]).destroy(); + } catch (InterruptedException ex) { + logger.info("Interrupted while destroying trust manager: " + ex.toString(), ex); + } + } + } + trustManagers = null; + } + + keyManagers = null; + jdkSslContext = null; + requestedProtocol = null; + requestedCiphers = null; + } + + /** Builder class to construct instances of {@link SSLFactory} with specific options */ + public static class Builder { + private String requestedProtocol; + private String[] requestedCiphers; + private File keyStore; + private String keyStorePassword; + private String keyPassword; + private File trustStore; + private String trustStorePassword; + private boolean trustStoreReloadingEnabled; + private int trustStoreReloadIntervalMs; + + /** + * Sets the requested protocol, i.e., "TLSv1.2", "TLSv1.1", etc + * + * @param requestedProtocol The requested protocol + * @return The builder object + */ + public Builder requestedProtocol(String requestedProtocol) { + this.requestedProtocol = requestedProtocol == null ? "TLSv1.3" : requestedProtocol; + return this; + } + + /** + * Sets the requested cipher suites, i.e., "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", etc + * + * @param requestedCiphers The requested ciphers + * @return The builder object + */ + public Builder requestedCiphers(String[] requestedCiphers) { + this.requestedCiphers = requestedCiphers; + return this; + } + + /** + * Sets the Keystore and Keystore password + * + * @param keyStore The key store file to use + * @param keyStorePassword The password for the key store + * @return The builder object + */ + public Builder keyStore(File keyStore, String keyStorePassword) { + this.keyStore = keyStore; + this.keyStorePassword = keyStorePassword; + return this; + } + + /** + * Sets the key password + * + * @param keyPassword The password for the private key in the key store + * @return The builder object + */ + public Builder keyPassword(String keyPassword) { + this.keyPassword = keyPassword; + return this; + } + + /** + * Sets the trust-store, trust-store password, whether to use a Reloading TrustStore, and the + * trust-store reload interval, if enabled + * + * @param trustStore The trust store file to use + * @param trustStorePassword The password for the trust store + * @param trustStoreReloadingEnabled Whether trust store reloading is enabled + * @param trustStoreReloadIntervalMs The interval at which to reload the trust store file + * @return The builder object + */ + public Builder trustStore( + File trustStore, + String trustStorePassword, + boolean trustStoreReloadingEnabled, + int trustStoreReloadIntervalMs) { + this.trustStore = trustStore; + this.trustStorePassword = trustStorePassword; + this.trustStoreReloadingEnabled = trustStoreReloadingEnabled; + this.trustStoreReloadIntervalMs = trustStoreReloadIntervalMs; + return this; + } + + /** + * Builds our {@link SSLFactory} + * + * @return The built {@link SSLFactory} + */ + public SSLFactory build() { + return new SSLFactory(this); + } + } + + /** + * Returns an initialized {@link SSLContext} + * + * @param requestedProtocol The requested protocol to use + * @param keyManagers The list of key managers to use + * @param trustManagers The list of trust managers to use + * @return The built {@link SSLContext} + * @throws GeneralSecurityException + */ + private static SSLContext createSSLContext( + String requestedProtocol, KeyManager[] keyManagers, TrustManager[] trustManagers) + throws GeneralSecurityException { + SSLContext sslContext = SSLContext.getInstance(requestedProtocol); + sslContext.init(keyManagers, trustManagers, null); + return sslContext; + } + + /** + * Creates a new {@link SSLEngine}. Note that currently client auth is not supported + * + * @param isClient Whether the engine is used in a client context + * @param allocator The {@link ByteBufAllocator to use} + * @return A valid {@link SSLEngine}. + */ + public SSLEngine createSSLEngine(boolean isClient, ByteBufAllocator allocator) { + SSLEngine engine = createEngine(isClient, allocator); + engine.setUseClientMode(isClient); + engine.setWantClientAuth(true); Review Comment: > Unlike in spark, where clients do not have ssl cert, here we allow for ssl client to present their certificate - when available. Would be great to do this on Spark too. I had a PR to do this that I never got around to submitting as part of other feature tests, but I wasn't sure if it would have value in isolation beyond the other changes I had in mind (those proved infeasible). -- 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]
