jonmeredith commented on a change in pull request #1027:
URL: https://github.com/apache/cassandra/pull/1027#discussion_r695188506



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

Review comment:
       Perhaps worth adding a link to the Kubernetes Secrets spec - I found 
https://kubernetes.io/docs/concepts/configuration/secret/ which is hopefully 
the right one.

##########
File path: 
src/java/org/apache/cassandra/security/FileBasedSslContextFactory.java
##########
@@ -0,0 +1,206 @@
+/*
+ * 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.io.File;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.security.KeyStore;
+import java.security.cert.X509Certificate;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.TrustManagerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Abstract implementation for {@link ISslContextFactory} using file based, 
standard keystore format with the ability
+ * to hot-reload the files upon file changes (detected by the {@code last 
modified timestamp}).
+ *
+ * {@code CAUTION:} While this is a useful abstraction, please be careful if 
you need to modify this class
+ * given possible custom implementations out there!
+ */
+abstract public class FileBasedSslContextFactory extends 
AbstractSslContextFactory
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(FileBasedSslContextFactory.class);
+
+    @VisibleForTesting
+    protected volatile boolean checkedExpiry = false;
+
+    /**
+     * List of files that trigger hot reloading of SSL certificates
+     */
+    protected volatile List<HotReloadableFile> hotReloadableFiles = new 
ArrayList<>();
+
+    protected String keystore;
+    protected String keystore_password;
+    protected String truststore;
+    protected String truststore_password;

Review comment:
       Why remove `final` on these? I might have missed some other review 
feedback. 

##########
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:
       Does this mean it isn't possible to set an empty environment variable 
value - for example an empty password if not desired? Sounds like `keytool` 
refuses, but [apparently it's 
possible](https://stackoverflow.com/questions/23629246/is-it-possible-to-create-jks-keystore-file-without-a-password)
 to do it programmatically.

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

Review comment:
       Are `KEYSTORE_LAST_UPDATEDTIME` and `TRUSTSTORE_LAST_UPDATEDTIME` 
propagated to the running Cassandra process when the secret is updated? I can't 
think of any other operating environment that externally modifies a programs 
environment variables after startup. I can see it being updated when a pod 
restarts, but that wouldn't be very useful for the implementation below.

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

Review comment:
       Naming question - is Kubernetes more accurately `K8S` rather than `K8`, 
so this would be `K8SSecretSslContextFactory`?




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