You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2021/01/09 01:36:22 UTC

[GitHub] [incubator-pinot] mcvsubbu commented on a change in pull request #6418: TLS-support for client-pinot and pinot-internode connections

mcvsubbu commented on a change in pull request #6418:
URL: https://github.com/apache/incubator-pinot/pull/6418#discussion_r554246048



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/transport/TlsConfig.java
##########
@@ -0,0 +1,79 @@
+/**
+ * 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.pinot.core.transport;
+
+/**
+ * Container object for TLS/SSL configuration of pinot clients and servers (netty, grizzly, etc.)
+ */
+public class TlsConfig {
+  private boolean _enabled;
+  private boolean _clientAuth;

Review comment:
       I think this indicates whether or not client auth should be done? Can we call it `_clientAuthEnabled`?

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/util/TlsUtils.java
##########
@@ -0,0 +1,182 @@
+/**
+ * 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.pinot.core.util;
+
+import com.google.common.base.Preconditions;
+import java.io.FileInputStream;
+import java.security.GeneralSecurityException;
+import java.security.KeyStore;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import org.apache.pinot.core.transport.TlsConfig;
+import org.apache.pinot.spi.env.PinotConfiguration;
+
+
+/**
+ * Utility class for shared TLS configuration logic
+ */
+public final class TlsUtils {
+  private static final String ENABLED = "enabled";
+  private static final String CLIENT_AUTH = "client.auth";
+  private static final String KEYSTORE_PATH = "keystore.path";
+  private static final String KEYSTORE_PASSWORD = "keystore.password";
+  private static final String TRUSTSTORE_PATH = "truststore.path";
+  private static final String TRUSTSTORE_PASSWORD = "truststore.password";
+
+  private TlsUtils() {
+    // left blank
+  }
+
+  /**
+   * Extract a TlsConfig instance from a namespaced set of configuration keys.
+   *
+   * @param pinotConfig pinot configuration
+   * @param prefix namespace prefix
+   *
+   * @return TlsConfig instance
+   */
+  public static TlsConfig extractTlsConfig(PinotConfiguration pinotConfig, String prefix) {
+    return extractTlsConfig(new TlsConfig(), pinotConfig, prefix);
+  }
+
+  /**
+   * Extract a TlsConfig instance from a namespaced set of configuration keys, with defaults pulled from an alternative
+   * namespace
+   *
+   * @param pinotConfig pinot configuration
+   * @param prefix namespace prefix
+   * @param prefixDefaults namespace prefix for defaults
+   *
+   * @return TlsConfig instance
+   */
+  public static TlsConfig extractTlsConfig(PinotConfiguration pinotConfig, String prefix, String prefixDefaults) {
+    return extractTlsConfig(extractTlsConfig(pinotConfig, prefixDefaults), pinotConfig, prefix);
+  }
+
+  /**
+   * Create a KeyManagerFactory instance from a given TlsConfig.
+   *
+   * @param tlsConfig TLS config
+   *
+   * @return KeyManagerFactory
+   */
+  public static KeyManagerFactory createKeyManagerFactory(TlsConfig tlsConfig) {
+    Preconditions.checkNotNull(tlsConfig.getKeyStorePath(), "key store path is null");
+    Preconditions.checkNotNull(tlsConfig.getKeyStorePassword(), "key store password is null");
+
+    try {
+      KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
+      try (FileInputStream is = new FileInputStream(tlsConfig.getKeyStorePath())) {
+        keyStore.load(is, tlsConfig.getKeyStorePassword().toCharArray());
+      }
+
+      KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+      keyManagerFactory.init(keyStore, tlsConfig.getKeyStorePassword().toCharArray());
+
+      return keyManagerFactory;
+
+    } catch (Exception e) {
+      throw new RuntimeException(String.format("Could not create key manager factory '%s'",
+          tlsConfig.getKeyStorePath()), e);
+    }
+  }
+
+  /**
+   * Create a TrustManagerFactory instance from a given TlsConfig.
+   *
+   * @param tlsConfig TLS config
+   *
+   * @return TrustManagerFactory
+   */
+  public static TrustManagerFactory createTrustManagerFactory(TlsConfig tlsConfig) {
+    Preconditions.checkNotNull(tlsConfig.getTrustStorePath(), "trust store path is null");
+    Preconditions.checkNotNull(tlsConfig.getTrustStorePassword(), "trust store password is null");
+
+    try {
+      KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
+      try (FileInputStream is = new FileInputStream(tlsConfig.getTrustStorePath())) {
+        keyStore.load(is, tlsConfig.getTrustStorePassword().toCharArray());
+      }
+
+      TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+      trustManagerFactory.init(keyStore);
+
+      return trustManagerFactory;
+    } catch (Exception e) {
+      throw new RuntimeException(String.format("Could not create trust manager factory '%s'",
+          tlsConfig.getTrustStorePath()), e);
+    }
+  }
+
+  /**
+   * Installs a default TLS socket factory for all HttpsURLConnection instances based on a given TlsConfig (1 or 2-way)
+   *
+   * @param tlsConfig TLS config
+   */
+  public static void installDefaultSSLSocketFactory(TlsConfig tlsConfig) {
+    KeyManager[] keyManagers = null;
+    if (tlsConfig.getKeyStorePath() != null) {
+      keyManagers = createKeyManagerFactory(tlsConfig).getKeyManagers();
+    }
+
+    TrustManager[] trustManagers = null;
+    if (tlsConfig.getTrustStorePath() != null) {
+      trustManagers = createTrustManagerFactory(tlsConfig).getTrustManagers();
+    }
+
+    try {
+      SSLContext sc = SSLContext.getInstance("SSL");
+      sc.init(keyManagers, trustManagers, new java.security.SecureRandom());
+      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
+    } catch (GeneralSecurityException ignore) {
+      // ignore

Review comment:
       Why ignore this exception?

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/utils/CommonConstants.java
##########
@@ -177,6 +177,12 @@
     public static final String CONFIG_OF_BROKER_GROUPBY_TRIM_THRESHOLD = "pinot.broker.groupby.trim.threshold";
     public static final int DEFAULT_BROKER_GROUPBY_TRIM_THRESHOLD = 1_000_000;
 
+    public static final String CONFIG_OF_BROKER_CLIENT_PROTOCOL = "pinot.broker.client.protocol";

Review comment:
       In order to support backward compat and migration, you should introduce config so that both http and https can be supported, both for client and netty. The way installations can migrate brokers by enabling both http and https, changing clients to https, ad then disabling https on the broker port.
   
   For netty is a bit trickier. We need to upgrade the servers to support both, change the broker to start using https, and then disable the servers to use http.

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/broker/BrokerAdminApiApplication.java
##########
@@ -58,20 +69,56 @@ protected void configure() {
     registerClasses(io.swagger.jaxrs.listing.SwaggerSerializers.class);
   }
 
-  public void start(int httpPort) {
-    Preconditions.checkArgument(httpPort > 0);
-    _baseUri = URI.create("http://0.0.0.0:" + httpPort + "/");
-    _httpServer = GrizzlyHttpServerFactory.createHttpServer(_baseUri, this);
+  public void start(PinotConfiguration brokerConf) {
+    int brokerQueryPort = brokerConf.getProperty(CommonConstants.Helix.KEY_OF_BROKER_QUERY_PORT,

Review comment:
       Dont you want to support backward compatibility and migration path? You should take two ports, and start http as well as https service, so that installations can upgrade without down time

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/broker/BrokerAdminApiApplication.java
##########
@@ -58,20 +69,56 @@ protected void configure() {
     registerClasses(io.swagger.jaxrs.listing.SwaggerSerializers.class);
   }
 
-  public void start(int httpPort) {
-    Preconditions.checkArgument(httpPort > 0);
-    _baseUri = URI.create("http://0.0.0.0:" + httpPort + "/");
-    _httpServer = GrizzlyHttpServerFactory.createHttpServer(_baseUri, this);
+  public void start(PinotConfiguration brokerConf) {
+    int brokerQueryPort = brokerConf.getProperty(CommonConstants.Helix.KEY_OF_BROKER_QUERY_PORT,
+        CommonConstants.Helix.DEFAULT_BROKER_QUERY_PORT);
+
+    Preconditions.checkArgument(brokerQueryPort > 0, "broker client port must be > 0");
+    _baseUri = URI.create(String.format("%s://0.0.0.0:%d/", getBrokerClientProtocol(brokerConf), brokerQueryPort));
+    _httpServer = buildHttpsServer(brokerConf);
     setupSwagger();
   }
 
+  private HttpServer buildHttpsServer(PinotConfiguration brokerConf) {
+    boolean isSecure = CommonConstants.HTTPS_PROTOCOL.equals(getBrokerClientProtocol(brokerConf));
+
+    TlsConfig tlsConfig = TlsUtils.extractTlsConfig(brokerConf, CommonConstants.Broker.BROKER_CLIENT_TLS_PREFIX, CommonConstants.Broker.BROKER_TLS_PREFIX);

Review comment:
       can u move this below line 88?

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/util/TlsUtils.java
##########
@@ -0,0 +1,182 @@
+/**
+ * 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.pinot.core.util;
+
+import com.google.common.base.Preconditions;
+import java.io.FileInputStream;
+import java.security.GeneralSecurityException;
+import java.security.KeyStore;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import org.apache.pinot.core.transport.TlsConfig;
+import org.apache.pinot.spi.env.PinotConfiguration;
+
+
+/**
+ * Utility class for shared TLS configuration logic
+ */
+public final class TlsUtils {
+  private static final String ENABLED = "enabled";
+  private static final String CLIENT_AUTH = "client.auth";
+  private static final String KEYSTORE_PATH = "keystore.path";
+  private static final String KEYSTORE_PASSWORD = "keystore.password";
+  private static final String TRUSTSTORE_PATH = "truststore.path";
+  private static final String TRUSTSTORE_PASSWORD = "truststore.password";
+
+  private TlsUtils() {
+    // left blank
+  }
+
+  /**
+   * Extract a TlsConfig instance from a namespaced set of configuration keys.
+   *
+   * @param pinotConfig pinot configuration
+   * @param prefix namespace prefix
+   *
+   * @return TlsConfig instance
+   */
+  public static TlsConfig extractTlsConfig(PinotConfiguration pinotConfig, String prefix) {
+    return extractTlsConfig(new TlsConfig(), pinotConfig, prefix);
+  }
+
+  /**
+   * Extract a TlsConfig instance from a namespaced set of configuration keys, with defaults pulled from an alternative
+   * namespace
+   *
+   * @param pinotConfig pinot configuration
+   * @param prefix namespace prefix
+   * @param prefixDefaults namespace prefix for defaults
+   *
+   * @return TlsConfig instance
+   */
+  public static TlsConfig extractTlsConfig(PinotConfiguration pinotConfig, String prefix, String prefixDefaults) {
+    return extractTlsConfig(extractTlsConfig(pinotConfig, prefixDefaults), pinotConfig, prefix);
+  }
+
+  /**
+   * Create a KeyManagerFactory instance from a given TlsConfig.
+   *
+   * @param tlsConfig TLS config
+   *
+   * @return KeyManagerFactory
+   */
+  public static KeyManagerFactory createKeyManagerFactory(TlsConfig tlsConfig) {
+    Preconditions.checkNotNull(tlsConfig.getKeyStorePath(), "key store path is null");

Review comment:
       Shouldn't these checks be done only if TlsConfig has enabled ?

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/broker/helix/HelixBrokerStarter.java
##########
@@ -238,14 +240,16 @@ public void start()
     // Initialize FunctionRegistry before starting the broker request handler
     FunctionRegistry.init();
     TableCache tableCache = new TableCache(_propertyStore, caseInsensitive);
+    // Configure TLS

Review comment:
       ```suggestion
       // Configure TLS for netty connection to server
   ```




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org