maulin-vasavada commented on code in PR #3638:
URL: https://github.com/apache/cassandra/pull/3638#discussion_r1831484322


##########
src/java/org/apache/cassandra/utils/jmx/AbstractJmxSocketFactory.java:
##########
@@ -0,0 +1,161 @@
+/*
+ * 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.cassandra.utils.jmx;
+
+import java.net.InetAddress;
+import java.util.HashMap;
+import java.util.Map;
+import javax.net.ssl.SSLException;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.config.EncryptionOptions;
+import org.apache.cassandra.exceptions.ConfigurationException;
+
+import static 
org.apache.cassandra.config.CassandraRelevantProperties.COM_SUN_MANAGEMENT_JMXREMOTE_SSL;
+import static 
org.apache.cassandra.config.CassandraRelevantProperties.COM_SUN_MANAGEMENT_JMXREMOTE_SSL_ENABLED_CIPHER_SUITES;
+import static 
org.apache.cassandra.config.CassandraRelevantProperties.COM_SUN_MANAGEMENT_JMXREMOTE_SSL_ENABLED_PROTOCOLS;
+import static 
org.apache.cassandra.config.CassandraRelevantProperties.COM_SUN_MANAGEMENT_JMXREMOTE_SSL_NEED_CLIENT_AUTH;
+import static 
org.apache.cassandra.config.CassandraRelevantProperties.JAVAX_RMI_SSL_CLIENT_ENABLED_CIPHER_SUITES;
+import static 
org.apache.cassandra.config.CassandraRelevantProperties.JAVAX_RMI_SSL_CLIENT_ENABLED_PROTOCOLS;
+
+/**
+ * Abstracts out the most common workflow in setting up the SSL client and 
server socket factorires for JMX.
+ * First, it checks the system properties (see <a 
href="https://docs.oracle.com/en/java/javase/17/management/monitoring-and-management-using-jmx-technology.html#GUID-F08985BB-629A-4FBF-A0CB-8762DF7590E0";>Java
 Documentation</a> to read the SSL configuration.
+ * Next, it checks the provided {@code jmxEncryptionOptions} to read the SSL 
configuration.
+ * If none of them is enabled, it checks the provided {@code localOnly} flag 
to configure the JMX server socket
+ * factory for the local JMX connection.
+ */
+abstract public class AbstractJmxSocketFactory implements IJmxSocketFactory
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(AbstractJmxSocketFactory.class);
+
+    @Override
+    public Map<String, Object> configure(InetAddress serverAddress, boolean 
localOnly,
+                                         EncryptionOptions 
jmxEncryptionOptions) throws SSLException
+    {
+        Map<String, Object> env = new HashMap<>();
+        boolean jmxRemoteSslSystemConfigProvided = 
COM_SUN_MANAGEMENT_JMXREMOTE_SSL.getBoolean();
+        // We check for the enabled jmx_encryption_options here because in 
case of no configuration provided in cassandra.yaml
+        // it will default to empty/non-null encryption options. Hence, we 
consider it set only if 'enabled' flag is set to true
+        boolean jmxEncryptionOptionsProvided = jmxEncryptionOptions != null && 
jmxEncryptionOptions.getEnabled() != null
+                                               && 
jmxEncryptionOptions.getEnabled();
+        if ( jmxRemoteSslSystemConfigProvided && jmxEncryptionOptionsProvided )
+        {
+            throw new ConfigurationException("Please specify JMX SSL 
configuration in either cassandra-env.sh or " +
+                                             "cassandra.yaml, not in both 
locations");
+        }
+
+        if (jmxRemoteSslSystemConfigProvided)
+        {
+            logger.info("Enabling JMX SSL using environment file properties");
+            logger.warn("Consider using the jmx_encryption_options section of 
cassandra.yaml instead to prevent " +
+                        "sensitive information being exposed");
+            boolean requireClientAuth = 
COM_SUN_MANAGEMENT_JMXREMOTE_SSL_NEED_CLIENT_AUTH.getBoolean();
+            String[] protocols = null;
+            String protocolList = 
COM_SUN_MANAGEMENT_JMXREMOTE_SSL_ENABLED_PROTOCOLS.getString();
+            if (protocolList != null)
+            {
+                JAVAX_RMI_SSL_CLIENT_ENABLED_PROTOCOLS.setString(protocolList);
+                protocols = StringUtils.split(protocolList, ',');
+            }
+
+            String[] ciphers = null;
+            String cipherList = 
COM_SUN_MANAGEMENT_JMXREMOTE_SSL_ENABLED_CIPHER_SUITES.getString();
+            if (cipherList != null)
+            {
+                
JAVAX_RMI_SSL_CLIENT_ENABLED_CIPHER_SUITES.setString(cipherList);
+                ciphers = StringUtils.split(cipherList, ',');
+            }
+
+            configureSslClientSocketFactory(env, serverAddress);
+            configureSslServerSocketFactoryBasedOnSystemConfig(env, 
serverAddress, ciphers, protocols, requireClientAuth);
+        }
+        else if (jmxEncryptionOptionsProvided)
+        {
+            logger.info("Enabling JMX SSL using jmx_encryption_options from 
cassandra.yaml");
+            setJmxSystemProperties(jmxEncryptionOptions);
+            configureSslClientSocketFactory(env, serverAddress);
+            configureSslServerSocketFactoryBasedOnEncryptionOptions(env, 
serverAddress, jmxEncryptionOptions);
+        }
+        else if (localOnly)
+        {
+            configureLocalSocketFactory(env,serverAddress);
+        }
+
+        return env;
+    }
+
+    /**
+     * Configures the non-SSL socket factories for the local JMX.
+     * @param env output param containing the configured socket factories
+     * @param serverAddress the JMX server is bound to
+     */
+    abstract public void configureLocalSocketFactory(Map<String, Object> env, 
InetAddress serverAddress);
+
+    /**
+     * Configures SSL based client socket factory.
+     * @param env output param containing the configured socket factories
+     * @param serverAddress the JMX server is bound to
+     */
+    abstract public void configureSslClientSocketFactory(Map<String, Object> 
env, InetAddress serverAddress);
+
+    /**
+     * Configures SSL based server socket factory based on system config for 
key/trust stores.
+     * @param env output param containing the configured socket factories
+     * @param serverAddress the JMX server is bound to
+     * @param enabledCipherSuites for the SSL communication
+     * @param enabledProtocols for the SSL communication
+     * @param needClientAuth {@code true} if it requires the client-auth; 
{@code false} otherwise
+     */
+    abstract public void 
configureSslServerSocketFactoryBasedOnSystemConfig(Map<String, Object> env, 
InetAddress serverAddress,
+                                                                            
String[] enabledCipherSuites, String[] enabledProtocols,
+                                                                            
boolean needClientAuth);
+
+    /**
+     * Configures SSL based server socket factory based on provided 
encryption_options.
+     * @param env output param containing the configured socket factories
+     * @param serverAddress the JMX server is bound to
+     * @param jmxEncryptionOptions for the SSL communication
+     * @throws SSLException if fails to configure the SSL based server socket 
factory
+     */
+    abstract public void 
configureSslServerSocketFactoryBasedOnEncryptionOptions(Map<String, Object> 
env, InetAddress serverAddress,

Review Comment:
   Yes I did push. I was referring to these methods in the abstract class  -
   
![image](https://github.com/user-attachments/assets/4ef53b67-f0dd-4ec3-87e2-af4c4150ed77)
   



-- 
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: pr-unsubscr...@cassandra.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to