Skip to content

Commit 7ec39ac

Browse files
authored
Settings: Fix setting groups to include secure settings (#25076)
This commit fixes the group methdos of Settings to properly include grouped secure settings. Previously the secure settings were included but without the group prefix being removed. closes #25069
1 parent 40a1334 commit 7ec39ac

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

core/src/main/java/org/elasticsearch/common/settings/Settings.java

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -507,35 +507,21 @@ public Map<String, Settings> getGroups(String settingPrefix, boolean ignoreNonGr
507507
}
508508

509509
private Map<String, Settings> getGroupsInternal(String settingPrefix, boolean ignoreNonGrouped) throws SettingsException {
510-
// we don't really care that it might happen twice
511-
Map<String, Map<String, String>> map = new LinkedHashMap<>();
512-
for (Object o : settings.keySet()) {
513-
String setting = (String) o;
514-
if (setting.startsWith(settingPrefix)) {
515-
String nameValue = setting.substring(settingPrefix.length());
516-
int dotIndex = nameValue.indexOf('.');
517-
if (dotIndex == -1) {
518-
if (ignoreNonGrouped) {
519-
continue;
520-
}
521-
throw new SettingsException("Failed to get setting group for [" + settingPrefix + "] setting prefix and setting ["
522-
+ setting + "] because of a missing '.'");
523-
}
524-
String name = nameValue.substring(0, dotIndex);
525-
String value = nameValue.substring(dotIndex + 1);
526-
Map<String, String> groupSettings = map.get(name);
527-
if (groupSettings == null) {
528-
groupSettings = new LinkedHashMap<>();
529-
map.put(name, groupSettings);
510+
Settings prefixSettings = getByPrefix(settingPrefix);
511+
Map<String, Settings> groups = new HashMap<>();
512+
for (String groupName : prefixSettings.names()) {
513+
Settings groupSettings = prefixSettings.getByPrefix(groupName + ".");
514+
if (groupSettings.isEmpty()) {
515+
if (ignoreNonGrouped) {
516+
continue;
530517
}
531-
groupSettings.put(value, get(setting));
518+
throw new SettingsException("Failed to get setting group for [" + settingPrefix + "] setting prefix and setting ["
519+
+ settingPrefix + groupName + "] because of a missing '.'");
532520
}
521+
groups.put(groupName, groupSettings);
533522
}
534-
Map<String, Settings> retVal = new LinkedHashMap<>();
535-
for (Map.Entry<String, Map<String, String>> entry : map.entrySet()) {
536-
retVal.put(entry.getKey(), new Settings(Collections.unmodifiableMap(entry.getValue()), secureSettings));
537-
}
538-
return Collections.unmodifiableMap(retVal);
523+
524+
return Collections.unmodifiableMap(groups);
539525
}
540526
/**
541527
* Returns group settings for the given setting prefix.

core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.hamcrest.Matchers.allOf;
3939
import static org.hamcrest.Matchers.arrayContaining;
4040
import static org.hamcrest.Matchers.contains;
41+
import static org.hamcrest.Matchers.containsInAnyOrder;
4142
import static org.hamcrest.Matchers.containsString;
4243
import static org.hamcrest.Matchers.equalTo;
4344
import static org.hamcrest.Matchers.hasToString;
@@ -525,6 +526,29 @@ public void testSecureSettingsPrefix() {
525526
assertTrue(prefixSettings.names().contains("foo"));
526527
}
527528

529+
public void testGroupPrefix() {
530+
MockSecureSettings secureSettings = new MockSecureSettings();
531+
secureSettings.setString("test.key1.foo", "somethingsecure");
532+
secureSettings.setString("test.key1.bar", "somethingsecure");
533+
secureSettings.setString("test.key2.foo", "somethingsecure");
534+
secureSettings.setString("test.key2.bog", "somethingsecure");
535+
Settings.Builder builder = Settings.builder();
536+
builder.put("test.key1.baz", "blah1");
537+
builder.put("test.key1.other", "blah2");
538+
builder.put("test.key2.baz", "blah3");
539+
builder.put("test.key2.else", "blah4");
540+
builder.setSecureSettings(secureSettings);
541+
Settings settings = builder.build();
542+
Map<String, Settings> groups = settings.getGroups("test");
543+
assertEquals(2, groups.size());
544+
Settings key1 = groups.get("key1");
545+
assertNotNull(key1);
546+
assertThat(key1.names(), containsInAnyOrder("foo", "bar", "baz", "other"));
547+
Settings key2 = groups.get("key2");
548+
assertNotNull(key2);
549+
assertThat(key2.names(), containsInAnyOrder("foo", "bog", "baz", "else"));
550+
}
551+
528552
public void testEmptyFilterMap() {
529553
Settings.Builder builder = Settings.builder();
530554
builder.put("a", "a1");

0 commit comments

Comments
 (0)