Move away from `Settings#getAsMap()` (elastic/x-pack-elasticsearch#2661)

Relates to elastic/elasticsearch#26845

Original commit: elastic/x-pack-elasticsearch@0323ea07a5
This commit is contained in:
Simon Willnauer 2017-10-04 01:21:59 -06:00 committed by GitHub
parent 8268cecb80
commit f5864c7291
17 changed files with 62 additions and 89 deletions

View File

@ -7,6 +7,7 @@ package org.elasticsearch.xpack.notification.email;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.SpecialPermission;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.common.unit.TimeValue;
@ -237,8 +238,8 @@ public class Account {
replace(builder, "use_rset", "userset");
settings = builder.build();
Properties props = new Properties();
for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
props.setProperty(SMTP_SETTINGS_PREFIX + entry.getKey(), entry.getValue());
for (String key : settings.keySet()) {
props.setProperty(SMTP_SETTINGS_PREFIX + key, settings.get(key));
}
return props;
}

View File

@ -212,7 +212,7 @@ public class Security implements ActionPlugin, IngestPlugin, NetworkPlugin, Clus
static final Setting<List<String>> AUDIT_OUTPUTS_SETTING =
Setting.listSetting(setting("audit.outputs"),
s -> s.getAsMap().containsKey(setting("audit.outputs")) ?
s -> s.keySet().contains(setting("audit.outputs")) ?
Collections.emptyList() : Collections.singletonList(LoggingAuditTrail.NAME),
Function.identity(), Property.NodeScope);
@ -680,7 +680,6 @@ public class Security implements ActionPlugin, IngestPlugin, NetworkPlugin, Clus
return;
}
final Map<String, String> settingsMap = settings.getAsMap();
for (Map.Entry<String, Settings> tribeSettings : tribesSettings.entrySet()) {
String tribePrefix = "tribe." + tribeSettings.getKey() + ".";
@ -701,12 +700,11 @@ public class Security implements ActionPlugin, IngestPlugin, NetworkPlugin, Clus
}
// we passed all the checks now we need to copy in all of the x-pack security settings
for (Map.Entry<String, String> entry : settingsMap.entrySet()) {
String key = entry.getKey();
if (key.startsWith("xpack.security.")) {
settingsBuilder.put(tribePrefix + key, entry.getValue());
}
settings.keySet().forEach(k -> {
if (k.startsWith("xpack.security.")) {
settingsBuilder.copy(tribePrefix + k, k, settings);
}
});
}
Map<String, Settings> realmsSettings = settings.getGroups(setting("authc.realms"), true);

View File

@ -837,14 +837,14 @@ public class IndexAuditTrail extends AbstractComponent implements AuditTrail, Cl
// Filter out forbidden settings:
Settings.Builder builder = Settings.builder();
for (Map.Entry<String, String> entry : newSettings.getAsMap().entrySet()) {
String name = "index." + entry.getKey();
builder.put(newSettings.filter(k -> {
String name = "index." + k;
if (FORBIDDEN_INDEX_SETTING.equals(name)) {
logger.warn("overriding the default [{}} setting is forbidden. ignoring...", name);
continue;
}
builder.put(name, entry.getValue());
return false;
}
return true;
}));
return builder.build();
}

View File

@ -103,7 +103,7 @@ public abstract class AbstractAdLdapRealmTestCase extends SecurityIntegTestCase
roleMappings = realmConfig.selectRoleMappings(ESTestCase::randomBoolean);
useGlobalSSL = randomBoolean();
ESLoggerFactory.getLogger("test").info("running test with realm configuration [{}], with direct group to role mapping [{}]. " +
"Settings [{}]", realmConfig, realmConfig.mapGroupsAsRoles, realmConfig.settings.getAsMap());
"Settings [{}]", realmConfig, realmConfig.mapGroupsAsRoles, realmConfig.settings);
}
@AfterClass
@ -119,11 +119,7 @@ public abstract class AbstractAdLdapRealmTestCase extends SecurityIntegTestCase
if (useGlobalSSL) {
// don't use filter since it returns a prefixed secure setting instead of mock!
Settings settingsToAdd = super.nodeSettings(nodeOrdinal);
for (Map.Entry<String, String> settingsEntry : settingsToAdd.getAsMap().entrySet()) {
if (settingsEntry.getKey().startsWith("xpack.ssl.") == false) {
builder.put(settingsEntry.getKey(), settingsEntry.getValue());
}
}
builder.put(settingsToAdd.filter(k -> k.startsWith("xpack.ssl.") == false), false);
MockSecureSettings mockSecureSettings = (MockSecureSettings) Settings.builder().put(settingsToAdd).getSecureSettings();
if (mockSecureSettings != null) {
MockSecureSettings filteredSecureSettings = new MockSecureSettings();

View File

@ -36,7 +36,7 @@ public class MultipleAdRealmTests extends AbstractAdLdapRealmTestCase {
secondaryRealmConfig = randomFrom(configs);
ESLoggerFactory.getLogger("test")
.info("running test with secondary realm configuration [{}], with direct group to role mapping [{}]. Settings [{}]",
secondaryRealmConfig, secondaryRealmConfig.mapGroupsAsRoles, secondaryRealmConfig.settings.getAsMap());
secondaryRealmConfig, secondaryRealmConfig.mapGroupsAsRoles, secondaryRealmConfig.settings);
// It's easier to test 2 realms when using file based role mapping, and for the purposes of
// this test, there's no need to test native mappings.
@ -51,9 +51,9 @@ public class MultipleAdRealmTests extends AbstractAdLdapRealmTestCase {
Path store = getDataPath(TESTNODE_KEYSTORE);
final List<RoleMappingEntry> secondaryRoleMappings = secondaryRealmConfig.selectRoleMappings(() -> true);
final Settings secondarySettings = super.buildRealmSettings(secondaryRealmConfig, secondaryRoleMappings, store);
secondarySettings.getAsMap().forEach((name, value) -> {
name = name.replace(XPACK_SECURITY_AUTHC_REALMS_EXTERNAL, XPACK_SECURITY_AUTHC_REALMS_EXTERNAL + "2");
builder.put(name, value);
secondarySettings.keySet().forEach(name -> {
String newName = name.replace(XPACK_SECURITY_AUTHC_REALMS_EXTERNAL, XPACK_SECURITY_AUTHC_REALMS_EXTERNAL + "2");
builder.copy(newName, name, secondarySettings);
});
return builder.build();

View File

@ -160,16 +160,17 @@ public abstract class TribeTransportTestCase extends ESIntegTestCase {
assertAcked(cluster2.client().admin().indices().prepareCreate("test2").get());
ensureYellow(internalCluster());
ensureYellow(cluster2);
Map<String,String> asMap = internalCluster().getDefaultSettings().getAsMap();
// Map<String,String> asMap = internalCluster().getDefaultSettings().getAsMap();
Settings.Builder tribe1Defaults = Settings.builder();
Settings.Builder tribe2Defaults = Settings.builder();
for (Map.Entry<String, String> entry : asMap.entrySet()) {
if (entry.getKey().startsWith("path.")) {
continue;
}
tribe1Defaults.put("tribe.t1." + entry.getKey(), entry.getValue());
tribe2Defaults.put("tribe.t2." + entry.getKey(), entry.getValue());
internalCluster().getDefaultSettings().keySet().forEach(k -> {
if (k.startsWith("path.") == false) {
tribe1Defaults.copy(k, internalCluster().getDefaultSettings());
tribe2Defaults.copy(k, internalCluster().getDefaultSettings());
}
});
tribe1Defaults.normalizePrefix("tribe.t1.");
tribe2Defaults.normalizePrefix("tribe.t2.");
// give each tribe it's unicast hosts to connect to
tribe1Defaults.putArray("tribe.t1." + UnicastZenPing.DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey(),
getUnicastHosts(internalCluster().client()));

View File

@ -193,10 +193,10 @@ public class ExportersTests extends ESTestCase {
exporters.start();
assertThat(settingsHolder.get(), notNullValue());
Map<String, String> settings = settingsHolder.get().getAsMap();
Settings settings = settingsHolder.get();
assertThat(settings.size(), is(2));
assertThat(settings, hasEntry("_name0.type", "_type"));
assertThat(settings, hasEntry("_name1.type", "_type"));
assertEquals(settings.get("_name0.type"), "_type");
assertEquals(settings.get("_name1.type"), "_type");
Settings update = Settings.builder()
.put("xpack.monitoring.exporters._name0.foo", "bar")
@ -204,12 +204,12 @@ public class ExportersTests extends ESTestCase {
.build();
clusterSettings.applySettings(update);
assertThat(settingsHolder.get(), notNullValue());
settings = settingsHolder.get().getAsMap();
settings = settingsHolder.get();
assertThat(settings.size(), is(4));
assertThat(settings, hasEntry("_name0.type", "_type"));
assertThat(settings, hasEntry("_name0.foo", "bar"));
assertThat(settings, hasEntry("_name1.type", "_type"));
assertThat(settings, hasEntry("_name1.foo", "bar"));
assertEquals(settings.get("_name0.type"), "_type");
assertEquals(settings.get("_name0.foo"), "bar");
assertEquals(settings.get("_name1.type"), "_type");
assertEquals(settings.get("_name1.foo"), "bar");
}
public void testExporterBlocksOnClusterState() {

View File

@ -154,8 +154,8 @@ public class PagerDutyAccountsTests extends ESTestCase {
private void addAccountSettings(String name, Settings.Builder builder) {
builder.put("xpack.notification.pagerduty.account." + name + ".service_api_key", randomAlphaOfLength(50));
Settings defaults = SlackMessageDefaultsTests.randomSettings();
for (Map.Entry<String, String> setting : defaults.getAsMap().entrySet()) {
builder.put("xpack.notification.pagerduty.message_defaults." + setting.getKey(), setting.getValue());
for (String setting : defaults.keySet()) {
builder.copy("xpack.notification.pagerduty.message_defaults." + setting, setting, defaults);
}
}
}

View File

@ -138,8 +138,8 @@ public class SlackAccountsTests extends ESTestCase {
private void addAccountSettings(String name, Settings.Builder builder) {
builder.put("xpack.notification.slack.account." + name + ".url", "https://hooks.slack.com/services/" + randomAlphaOfLength(50));
Settings defaults = SlackMessageDefaultsTests.randomSettings();
for (Map.Entry<String, String> setting : defaults.getAsMap().entrySet()) {
builder.put("xpack.notification.slack.message_defaults." + setting.getKey(), setting.getValue());
for (String setting : defaults.keySet()) {
builder.copy("xpack.notification.slack.message_defaults." + setting, setting, defaults);
}
}
}

View File

@ -238,10 +238,10 @@ public class SecurityTribeIT extends NativeRealmIntegTestCase {
}
return true;
});
for (Map.Entry<String, String> entry : tribeSettings.getAsMap().entrySet()) {
tribe1Defaults.put("tribe.t1." + entry.getKey(), entry.getValue());
tribe2Defaults.put("tribe.t2." + entry.getKey(), entry.getValue());
}
tribe1Defaults.put(tribeSettings, false);
tribe1Defaults.normalizePrefix("tribe.t1.");
tribe2Defaults.put(tribeSettings, false);
tribe2Defaults.normalizePrefix("tribe.t2.");
// TODO: rethink how these settings are generated for tribes once we support more than just string settings...
MockSecureSettings secureSettingsTemplate =
(MockSecureSettings) Settings.builder().put(settingsTemplate).getSecureSettings();

View File

@ -290,7 +290,7 @@ public class IndexAuditTrailTests extends SecurityIntegTestCase {
}
Settings settings = builder.put(settings(rollover, includes, excludes)).build();
logger.info("--> settings: [{}]", settings.getAsMap().toString());
logger.info("--> settings: [{}]", settings);
DiscoveryNode localNode = mock(DiscoveryNode.class);
when(localNode.getHostAddress()).thenReturn(remoteAddress.getAddress());
when(localNode.getHostName()).thenReturn(remoteAddress.getAddress());

View File

@ -397,9 +397,10 @@ public class LdapUserSearchSessionFactoryTests extends LdapTestCase {
.build();
Settings.Builder builder = Settings.builder()
.put(globalSettings);
for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
builder.put("xpack.security.authc.realms.ldap." + entry.getKey(), entry.getValue());
}
settings.keySet().forEach(k -> {
builder.copy("xpack.security.authc.realms.ldap." + k, k, settings);
});
Settings fullSettings = builder.build();
sslService = new SSLService(fullSettings, new Environment(fullSettings));
RealmConfig config = new RealmConfig("ad-as-ldap-test", settings, globalSettings, new Environment(globalSettings), new ThreadContext(globalSettings));

View File

@ -144,16 +144,10 @@ public class PkiAuthenticationTests extends SecurityIntegTestCase {
private TransportClient createTransportClient(Settings additionalSettings) {
Settings clientSettings = transportClientSettings();
if (additionalSettings.getByPrefix("xpack.ssl.").isEmpty() == false) {
Settings.Builder builder = Settings.builder();
for (Entry<String, String> entry : clientSettings.getAsMap().entrySet()) {
if (entry.getKey().startsWith("xpack.ssl.") == false) {
builder.put(entry.getKey(), entry.getValue());
}
}
clientSettings = builder.build();
clientSettings = clientSettings.filter(k -> k.startsWith("xpack.ssl.") == false);
}
Settings.Builder builder = Settings.builder().put(clientSettings)
Settings.Builder builder = Settings.builder().put(clientSettings, false)
.put(additionalSettings)
.put("cluster.name", internalCluster().getClusterName());
builder.remove(Security.USER_SETTING.getKey());

View File

@ -69,15 +69,7 @@ public class IPHostnameVerificationTests extends SecurityIntegTestCase {
@Override
protected Settings transportClientSettings() {
Settings clientSettings = super.transportClientSettings();
Settings.Builder builder = Settings.builder();
for (Entry<String, String> entry : clientSettings.getAsMap().entrySet()) {
if (entry.getKey().startsWith("xpack.ssl.") == false) {
builder.put(entry.getKey(), entry.getValue());
}
}
clientSettings = builder.build();
return Settings.builder().put(clientSettings)
return Settings.builder().put(clientSettings.filter(k -> k.startsWith("xpack.ssl.") == false))
.put("xpack.ssl.verification_mode", "certificate")
.put("xpack.ssl.keystore.path", keystore.toAbsolutePath())
.put("xpack.ssl.keystore.password", "testnode-ip-only")

View File

@ -35,11 +35,7 @@ public class SslHostnameVerificationTests extends SecurityIntegTestCase {
protected Settings nodeSettings(int nodeOrdinal) {
Settings settings = super.nodeSettings(nodeOrdinal);
Settings.Builder settingsBuilder = Settings.builder();
for (Entry<String, String> entry : settings.getAsMap().entrySet()) {
if (entry.getKey().startsWith("xpack.ssl.") == false) {
settingsBuilder.put(entry.getKey(), entry.getValue());
}
}
settingsBuilder.put(settings.filter(k -> k.startsWith("xpack.ssl.") == false), false);
Path keystore;
try {
/*
@ -71,12 +67,7 @@ public class SslHostnameVerificationTests extends SecurityIntegTestCase {
Settings settings = super.transportClientSettings();
// remove all ssl settings
Settings.Builder builder = Settings.builder();
for (Entry<String, String> entry : settings.getAsMap().entrySet()) {
String key = entry.getKey();
if (key.startsWith("xpack.ssl.") == false) {
builder.put(key, entry.getValue());
}
}
builder.put(settings.filter( k -> k.startsWith("xpack.ssl.") == false), false);
builder.put("xpack.ssl.verification_mode", "certificate")
.put("xpack.ssl.keystore.path", keystore.toAbsolutePath()) // settings for client keystore

View File

@ -32,6 +32,8 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
@ -223,11 +225,11 @@ public class ExecutableJiraActionTests extends ESTestCase {
}
public void testExecutionFieldsStringArrays() throws Exception {
Map<String, String> defaults = Settings.builder()
Settings build = Settings.builder()
.putArray("k0", "a", "b", "c")
.put("k1", "v1")
.build()
.getAsMap();
.build();
Map<String, String> defaults = build.keySet().stream().collect(Collectors.toMap(Function.identity(), k -> build.get(k)));
Map<String, Object> fields = new HashMap<>();
fields.put("k2", "v2");
@ -241,11 +243,10 @@ public class ExecutableJiraActionTests extends ESTestCase {
}
public void testExecutionFieldsStringArraysNotOverridden() throws Exception {
Map<String, String> defaults = Settings.builder()
Settings build = Settings.builder()
.putArray("k0", "a", "b", "c")
.build()
.getAsMap();
.build();
Map<String, String> defaults = build.keySet().stream().collect(Collectors.toMap(Function.identity(), k -> build.get(k)));
Map<String, Object> fields = new HashMap<>();
fields.put("k1", "v1");
fields.put("k0", new String[]{"d", "e", "f"}); // should not be overridden byt the defaults

View File

@ -69,9 +69,7 @@ public class OpenLdapUserSearchSessionFactoryTests extends ESTestCase {
.build(), globalSettings, new Environment(globalSettings), new ThreadContext(globalSettings));
Settings.Builder builder = Settings.builder()
.put(globalSettings);
for (Map.Entry<String, String> entry : config.settings().getAsMap().entrySet()) {
builder.put("xpack.security.authc.realms.ldap." + entry.getKey(), entry.getValue());
}
builder.put(Settings.builder().put(config.settings()).normalizePrefix("xpack.security.authc.realms.ldap.").build());
Settings settings = builder.build();
SSLService sslService = new SSLService(settings, new Environment(settings));