Skip to content

Commit e45a330

Browse files
committed
Merge branch 'cassandra-4.1' into cassandra-5.0
2 parents dd88393 + c9717b4 commit e45a330

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
5.0.6
22
* Expose StorageService.dropPreparedStatements via JMX (CASSANDRA-20870)
33
* Sort SSTable TOC entries for determinism (CASSANDRA-20494)
4+
Merged from 4.1:
5+
* Redact security-sensitive information in system_views.settings (CASSANDRA-20856)
46
Merged from 4.0:
57
* Update Jackson to 2.19.2 (CASSANDRA-20848)
68
* Update commons-lang3 to 3.18.0 (CASSANDRA-20849)

src/java/org/apache/cassandra/config/EncryptionOptions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@ public String description()
7373
* truststore_passwords configurations as they are in plaintext format.
7474
*/
7575
public final ParameterizedClass ssl_context_factory;
76+
@Redacted
7677
public final String keystore;
7778
@Nullable
79+
@Redacted
7880
public final String keystore_password;
81+
@Redacted
7982
public final String truststore;
8083
@Nullable
84+
@Redacted
8185
public final String truststore_password;
8286
public final List<String> cipher_suites;
8387
protected String protocol;

src/java/org/apache/cassandra/config/Properties.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.cassandra.config;
1919

20+
import java.lang.annotation.Annotation;
2021
import java.lang.reflect.Constructor;
2122
import java.util.ArrayDeque;
2223
import java.util.Collection;
@@ -192,5 +193,25 @@ public Object get(Object object)
192193
throw e;
193194
}
194195
}
196+
197+
/**
198+
* If there is a hierarchy of settings, like
199+
* </p>
200+
* {@code a.b.c.{d,e,f,g,h}}
201+
* </p>
202+
* and we put e.g. {@link Redacted} on {@code c},
203+
* then all {@code d,e,f,g,h} will be redacted as well automatically.
204+
* This is handy for cases when we want to redact whole family of properties by one shot.
205+
*
206+
* @param aClass annotation to get
207+
* @return found annotation of given type on root or on leaf, null when not present.
208+
* @param <A> type of annotation
209+
*/
210+
@Override
211+
public <A extends Annotation> A getAnnotation(Class<A> aClass)
212+
{
213+
A annotation = root.getAnnotation(aClass);
214+
return annotation != null ? annotation : leaf.getAnnotation(aClass);
215+
}
195216
}
196217
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.cassandra.config;
20+
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
/**
27+
* Annotation supposed to be placed on any field in Config (and its subproperties in embedded configuration classes)
28+
* which will be redacted e.g. password or similar.
29+
* <p>
30+
* User querying system_views.settings will be shown redacted values on such fields.
31+
*/
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@Target({ ElementType.FIELD })
34+
public @interface Redacted
35+
{
36+
String REDACTED_STRING = "<REDACTED>";
37+
38+
/**
39+
* @return redacted value, defaults to {@code <REDACTED>}.
40+
*/
41+
String redactedValue() default REDACTED_STRING;
42+
}

src/java/org/apache/cassandra/db/virtual/SettingsTable.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.common.collect.ImmutableMap;
2727

2828
import org.apache.cassandra.config.Config;
29+
import org.apache.cassandra.config.Redacted;
2930
import org.apache.cassandra.config.DatabaseDescriptor;
3031
import org.apache.cassandra.config.Loader;
3132
import org.apache.cassandra.config.Properties;
@@ -89,13 +90,34 @@ public DataSet data()
8990
@VisibleForTesting
9091
String getValue(Property prop)
9192
{
93+
Redacted maybeCredential = prop.getAnnotation(Redacted.class);
94+
if (maybeCredential != null)
95+
return maybeCredential.redactedValue();
96+
9297
Object value = prop.get(config);
9398
if (value == null)
9499
return null;
95100

96101
if (value.getClass().isArray())
97102
return Arrays.asList((Object[]) value).toString();
98103

104+
if (value instanceof Map)
105+
{
106+
Map<String, Object> map = new HashMap<>();
107+
for (Map.Entry<String, Object> entry : ((Map<String, Object>) value).entrySet())
108+
{
109+
// this is done on best-effort basis as we do not have names in parameters
110+
// inherently under control as this is what a user is responsible for
111+
// when dealing with custom implementations
112+
if (entry.getKey().endsWith("_password") || entry.getKey().equals("password"))
113+
map.put(entry.getKey(), Redacted.REDACTED_STRING);
114+
else
115+
map.put(entry.getKey(), entry.getValue());
116+
}
117+
118+
return map.toString();
119+
}
120+
99121
return value.toString();
100122
}
101123

test/unit/org/apache/cassandra/db/virtual/SettingsTableTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
package org.apache.cassandra.db.virtual;
2020

21+
import java.util.LinkedHashMap;
2122
import java.util.List;
2223
import java.util.Map;
24+
import java.util.Set;
25+
import java.util.TreeMap;
2326
import java.util.stream.Collectors;
2427

2528
import com.google.common.collect.ImmutableList;
@@ -31,13 +34,18 @@
3134
import com.datastax.driver.core.ResultSet;
3235
import com.datastax.driver.core.Row;
3336
import org.apache.cassandra.config.Config;
37+
import org.apache.cassandra.config.Redacted;
38+
import org.apache.cassandra.config.DefaultLoader;
3439
import org.apache.cassandra.config.DurationSpec;
3540
import org.apache.cassandra.config.EncryptionOptions.ServerEncryptionOptions.InternodeEncryption;
3641
import org.apache.cassandra.config.ParameterizedClass;
42+
import org.apache.cassandra.config.TransparentDataEncryptionOptions;
3743
import org.apache.cassandra.cql3.CQLTester;
3844
import org.apache.cassandra.security.SSLFactory;
3945
import org.yaml.snakeyaml.introspector.Property;
4046

47+
import static java.util.stream.Collectors.toMap;
48+
4149
public class SettingsTableTest extends CQLTester
4250
{
4351
private static final String KS_NAME = "vts";
@@ -63,6 +71,16 @@ public void config()
6371
config.commitlog_sync_group_window = new DurationSpec.IntMillisecondsBound(0);
6472
config.credentials_update_interval = null;
6573
config.data_file_directories = new String[] {"/my/data/directory", "/another/data/directory"};
74+
75+
Map<String, String> params = new LinkedHashMap<>();
76+
params.put("keystore_password", "password");
77+
params.put("key_password", "password");
78+
params.put("keystore", "conf/.keystore");
79+
config.transparent_data_encryption_options = new TransparentDataEncryptionOptions(false,
80+
"AES/CBC/PKCS5Padding",
81+
"alias",
82+
new ParameterizedClass("SomeClass",
83+
params));
6684
table = new SettingsTable(KS_NAME, config);
6785
VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
6886
disablePreparedReuseForTest();
@@ -299,4 +317,40 @@ public void testTransparentEncryptionOptionsOverride() throws Throwable
299317
config.transparent_data_encryption_options.iv_length = 7;
300318
check(pre + "iv_length", "7");
301319
}
320+
321+
@Test
322+
public void testRedaction()
323+
{
324+
assertValue("transparent_data_encryption_options.key_provider.parameters",
325+
String.format("{keystore_password=%s, keystore=conf/.keystore, key_password=%s}",
326+
Redacted.REDACTED_STRING,
327+
Redacted.REDACTED_STRING));
328+
329+
Set<Map.Entry<String, Property>> entries = new DefaultLoader().flatten(Config.class)
330+
.entrySet()
331+
.stream()
332+
.filter(e -> e.getValue().getAnnotation(Redacted.class) != null)
333+
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e, r) -> e, TreeMap::new))
334+
.entrySet();
335+
336+
Assert.assertFalse(entries.isEmpty());
337+
338+
for (Map.Entry<String, Property> entry : entries)
339+
{
340+
logger.info("redacted {}", entry.getKey());
341+
assertValue(entry.getKey(), entry.getValue().getAnnotation(Redacted.class).redactedValue());
342+
}
343+
}
344+
345+
private void assertValue(String settingName, String expectedValue)
346+
{
347+
List<Row> all = executeNet(String.format("SELECT * from vts.settings WHERE name = '%s'", settingName)).all();
348+
Assert.assertFalse(all.isEmpty());
349+
Row row = all.get(0);
350+
String name = row.getString("name");
351+
String value = row.getString("value");
352+
353+
Assert.assertEquals(settingName, name);
354+
Assert.assertEquals(expectedValue, value);
355+
}
302356
}

0 commit comments

Comments
 (0)