maulin-vasavada commented on a change in pull request #1027:
URL: https://github.com/apache/cassandra/pull/1027#discussion_r695304581



##########
File path: 
examples/ssl-factory/src/org/apache/cassandra/security/K8SecretsSslContextFactory.java
##########
@@ -0,0 +1,260 @@
+/*
+ * 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.security;
+
+import java.util.Map;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Custom {@link ISslContextFactory} implementation based on Kubernetes 
Secrets. It allows the keystore and
+ * truststore paths to be configured from the K8 secrets via volumeMount and 
passwords via K8 secrets environment
+ * variables.
+ *
+ * When keystore or truststore is updated, this implementation can detect that 
based on updated K8 secrets
+ * environment variable ({@code KEYSTORE_UPDATED_TIMESTAMP_ENV_VAR} for the 
keystore and {@code
+ * TRUSTSTORE_UPDATED_TIMESTAMP_ENV_VAR} for the truststore. The values in 
those timestamp variables is expected to
+ * be numeric values. Most obvious choice might be to just use time in 
milliseconds but any other strategy would work
+ * as well, as far as the comparison of those values can be done in a 
consistent/predictable manner. Again, those
+ * values don't have to necessarily reflect actual file's update timestamps, 
using the actual file's timestamps is
+ * just one of the valid options to signal updates.
+ *
+ * Defaults:
+ * <pre>
+ *     keystore path = /etc/my-ssl-store/keystore
+ *     keystore password = cassandra
+ *     truststore path = /etc/my-ssl-store/truststore
+ *     truststore password = cassandra
+ * </pre>
+ *
+ * Customization: In order to customize the K8 secret configuration, override 
appropriate values in the below cassandra
+ * configuration. The similar configuration can be applied to {@code 
client_encryption_options}.
+ * <pre>
+ *     server_encryption_options:
+ *       internode_encryption: none
+ *       ssl_context_factory:
+ *         class_name: org.apache.cassandra.security.K8SecretsSslContextFactory
+ *         parameters:
+ *           KEYSTORE_PATH: /etc/my-ssl-store/keystore
+ *           KEYSTORE_PASSWORD_ENV_VAR: KEYSTORE_PASSWORD
+ *           KEYSTORE_UPDATED_TIMESTAMP_ENV_VAR: KEYSTORE_LAST_UPDATEDTIME
+ *           TRUSTSTORE_PATH: /etc/my-ssl-store/truststore
+ *           TRUSTSTORE_PASSWORD_ENV_VAR: TRUSTSTORE_PASSWORD
+ *           TRUSTSTORE_UPDATED_TIMESTAMP_ENV_VAR: TRUSTSTORE_LAST_UPDATEDTIME
+ * </pre>
+ *
+ * Below is the corresponding sample YAML configuration for K8 env.
+ * <pre>
+ * apiVersion: v1
+ * kind: Pod
+ * metadata:
+ *   name: my-pod
+ *   labels:
+ *     app: my-app
+ * spec:
+ *   containers:
+ *   - name: my-app
+ *     image: my-app:latest
+ *     imagePullPolicy: Always
+ *     env:
+ *       - name: KEYSTORE_PASSWORD
+ *         valueFrom:
+ *           secretKeyRef:
+ *             name: my-ssl-store
+ *             key: keystore-password
+ *       - name: KEYSTORE_LAST_UPDATEDTIME
+ *         valueFrom:
+ *           secretKeyRef:
+ *             name: my-ssl-store
+ *             key: keystore-last-updatedtime
+ *       - name: TRUSTSTORE_PASSWORD
+ *         valueFrom:
+ *           secretKeyRef:
+ *             name: my-ssl-store
+ *             key: truststore-password
+ *       - name: TRUSTSTORE_LAST_UPDATEDTIME
+ *         valueFrom:
+ *           secretKeyRef:
+ *             name: my-ssl-store
+ *             key: truststore-last-updatedtime
+ *     volumeMounts:
+ *     - name: my-ssl-store
+ *       mountPath: "/etc/my-ssl-store"
+ *       readOnly: true
+ *   volumes:
+ *   - name: my-ssl-store
+ *     secret:
+ *       secretName: my-ssl-store
+ *       items:
+ *         - key: cassandra_ssl_keystore
+ *           path: keystore
+ *         - key: cassandra_ssl_truststore
+ *           path: truststore
+ * </pre>
+ */
+public class K8SecretsSslContextFactory extends FileBasedSslContextFactory
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(K8SecretsSslContextFactory.class);
+
+    /**
+     * Use below config-keys to configure this factory.
+     */
+    public interface ConfigKeys {
+        String KEYSTORE_PATH = "KEYSTORE_PATH";
+        String KEYSTORE_PASSWORD_ENV_VAR = "KEYSTORE_PASSWORD_ENV_VAR";
+        String TRUSTSTORE_PATH = "TRUSTSTORE_PATH";
+        String TRUSTSTORE_PASSWORD_ENV_VAR = "TRUSTSTORE_PASSWORD_ENV_VAR";
+        String KEYSTORE_UPDATED_TIMESTAMP_ENV_VAR = 
"KEYSTORE_UPDATED_TIMESTAMP_ENV_VAR";
+        String TRUSTSTORE_UPDATED_TIMESTAMP_ENV_VAR = 
"TRUSTSTORE_UPDATED_TIMESTAMP_ENV_VAR";
+    }
+
+    public static final String DEFAULT_KEYSTORE_UPDATED_TIMESTAMP_ENV_VAR = 
"KEYSTORE_LAST_UPDATEDTIME";
+    public static final String DEFAULT_TRUSTSTORE_UPDATED_TIMESTAMP_ENV_VAR = 
"TRUSTSTORE_LAST_UPDATEDTIME";
+
+    public static final String DEFAULT_KEYSTORE_PASSWORD = "cassandra";
+    public static final String DEFAULT_TRUSTSTORE_PASSWORD = "cassandra";
+
+    @VisibleForTesting
+    static final String DEFAULT_KEYSTORE_PASSWORD_ENV_VAR_NAME = 
"KEYSTORE_PASSWORD";
+    @VisibleForTesting
+    static final String DEFAULT_TRUSTSTORE_PASSWORD_ENV_VAR_NAME = 
"TRUSTSTORE_PASSWORD";
+
+    private static final String KEYSTORE_PATH_VALUE = 
"/etc/my-ssl-store/keystore";
+    private static final String TRUSTSTORE_PATH_VALUE = 
"/etc/my-ssl-store/truststore";
+    private static final String KEYSTORE_PASSWORD_ENV_VAR_NAME = 
DEFAULT_KEYSTORE_PASSWORD_ENV_VAR_NAME;
+    private static final String KEYSTORE_UPDATED_TIMESTAMP_ENV_VAR_NAME = 
DEFAULT_KEYSTORE_UPDATED_TIMESTAMP_ENV_VAR;
+    private static final String TRUSTSTORE_PASSWORD_ENV_VAR_NAME = 
DEFAULT_TRUSTSTORE_PASSWORD_ENV_VAR_NAME;
+    private static final String TRUSTSTORE_UPDATED_TIMESTAMP_ENV_VAR_NAME = 
DEFAULT_TRUSTSTORE_UPDATED_TIMESTAMP_ENV_VAR;
+
+    private long keystoreLastUpdatedTime;
+    private long truststoreLastUpdatedTime;
+
+    public K8SecretsSslContextFactory()
+    {
+        keystore = KEYSTORE_PATH_VALUE;
+        keystore_password = getValueFromEnv(KEYSTORE_PASSWORD_ENV_VAR_NAME,
+                                            DEFAULT_KEYSTORE_PASSWORD);
+        truststore = TRUSTSTORE_PATH_VALUE;
+        truststore_password = getValueFromEnv(TRUSTSTORE_PASSWORD_ENV_VAR_NAME,
+                                              DEFAULT_TRUSTSTORE_PASSWORD);
+        keystoreLastUpdatedTime = System.currentTimeMillis();
+        truststoreLastUpdatedTime = keystoreLastUpdatedTime;
+    }
+
+    public K8SecretsSslContextFactory(Map<String, Object> parameters)
+    {
+        super(parameters);
+        keystore = getString(ConfigKeys.KEYSTORE_PATH, KEYSTORE_PATH_VALUE);
+        keystore_password = 
getValueFromEnv(getString(ConfigKeys.KEYSTORE_PASSWORD_ENV_VAR,
+                                                      
KEYSTORE_PASSWORD_ENV_VAR_NAME), DEFAULT_KEYSTORE_PASSWORD);
+        truststore = getString(ConfigKeys.TRUSTSTORE_PATH, 
TRUSTSTORE_PATH_VALUE);
+        truststore_password = 
getValueFromEnv(getString(ConfigKeys.TRUSTSTORE_PASSWORD_ENV_VAR,
+                                                        
TRUSTSTORE_PASSWORD_ENV_VAR_NAME), DEFAULT_TRUSTSTORE_PASSWORD);
+        keystoreLastUpdatedTime = System.currentTimeMillis();
+        truststoreLastUpdatedTime = keystoreLastUpdatedTime;
+    }
+
+    @Override
+    public synchronized void initHotReloading() {
+        // No-op
+    }
+
+    /**
+     * Checks environment variables for {@code 
K8_SECRET_KEYSTORE_UPDATED_TIMESTAMP_ENV_VAR} and {@code 
K8_SECRET_TRUSTSTORE_UPDATED_TIMESTAMP_ENV_VAR}
+     * and compares the values for those variables with the current 
timestamps. In case the environment variables are
+     * not valid (either they are not initialized yet, got removed or got 
corrupted in-flight), this method considers
+     * that nothing is changed.
+     * @return {@code true} if either of the timestamps (keystore or 
truststore) got updated;{@code false} otherwise
+     */
+    @Override
+    public boolean shouldReload()
+    {
+        return hasKeystoreUpdated() || hasTruststoreUpdated();
+    }
+
+    @VisibleForTesting
+    String getValueFromEnv(String envVarName, String defaultValue) {
+        String valueFromEnv = StringUtils.isEmpty(envVarName) ? null : 
System.getenv(envVarName);
+        return StringUtils.isEmpty(valueFromEnv) ? defaultValue : valueFromEnv;

Review comment:
       Good point. I think it should allow empty values for password for the 
truststore atleast. Let me see how to enable that.




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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to