Adopt settings cleanups from core (elastic/x-pack-elasticsearch#2605)

Relates to elastic/elasticsearch#26739

Original commit: elastic/x-pack-elasticsearch@dd13d099de
This commit is contained in:
Simon Willnauer 2017-09-25 12:27:07 +02:00 committed by GitHub
parent 1bb9c4fe71
commit e7b5702f50
15 changed files with 47 additions and 38 deletions

View File

@ -9,6 +9,10 @@ import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.common.http.HttpClient; import org.elasticsearch.xpack.common.http.HttpClient;
import org.elasticsearch.xpack.common.http.HttpMethod; import org.elasticsearch.xpack.common.http.HttpMethod;
import org.elasticsearch.xpack.common.http.HttpProxy; import org.elasticsearch.xpack.common.http.HttpProxy;
@ -18,6 +22,7 @@ import org.elasticsearch.xpack.common.http.Scheme;
import org.elasticsearch.xpack.common.http.auth.basic.BasicAuth; import org.elasticsearch.xpack.common.http.auth.basic.BasicAuth;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Collections; import java.util.Collections;
@ -68,7 +73,15 @@ public class JiraAccount {
if (Strings.isEmpty(this.password)) { if (Strings.isEmpty(this.password)) {
throw requiredSettingException(name, PASSWORD_SETTING); throw requiredSettingException(name, PASSWORD_SETTING);
} }
this.issueDefaults = Collections.unmodifiableMap(settings.getAsSettings(ISSUE_DEFAULTS_SETTING).getAsStructuredMap()); try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
builder.startObject();
settings.getAsSettings(ISSUE_DEFAULTS_SETTING).toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
this.issueDefaults = Collections.unmodifiableMap(XContentType.JSON.xContent()
.createParser(new NamedXContentRegistry(Collections.emptyList()), builder.bytes()).map());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
} }
public String getName() { public String getName() {

View File

@ -32,6 +32,7 @@ import org.elasticsearch.action.ActionListener;
import org.elasticsearch.bootstrap.BootstrapCheck; import org.elasticsearch.bootstrap.BootstrapCheck;
import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
import org.elasticsearch.watcher.FileChangesListener; import org.elasticsearch.watcher.FileChangesListener;
import org.elasticsearch.watcher.FileWatcher; import org.elasticsearch.watcher.FileWatcher;
@ -127,8 +128,8 @@ public class DnRoleMapper implements UserRoleMapper {
} }
} }
try (InputStream in = Files.newInputStream(path)) { try {
Settings settings = Settings.builder().loadFromStream(path.toString(), in).build(); Settings settings = Settings.builder().loadFromPath(path).build();
Map<DN, Set<String>> dnToRoles = new HashMap<>(); Map<DN, Set<String>> dnToRoles = new HashMap<>();
Set<String> roles = settings.names(); Set<String> roles = settings.names();
@ -163,7 +164,7 @@ public class DnRoleMapper implements UserRoleMapper {
logger.debug("[{}] role mappings found in file [{}] for realm [{}/{}]", dnToRoles.size(), path.toAbsolutePath(), realmType, logger.debug("[{}] role mappings found in file [{}] for realm [{}/{}]", dnToRoles.size(), path.toAbsolutePath(), realmType,
realmName); realmName);
return unmodifiableMap(dnToRoles); return unmodifiableMap(dnToRoles);
} catch (IOException | YAMLException e) { } catch (IOException | SettingsException e) {
throw new ElasticsearchException("could not read realm [" + realmType + "/" + realmName + "] role mappings file [" + throw new ElasticsearchException("could not read realm [" + realmType + "/" + realmName + "] role mappings file [" +
path.toAbsolutePath() + "]", e); path.toAbsolutePath() + "]", e);
} }

View File

@ -83,10 +83,8 @@ public final class RestrictedTrustConfig extends TrustConfig {
} }
private CertificateTrustRestrictions readTrustGroup(Path path) throws IOException { private CertificateTrustRestrictions readTrustGroup(Path path) throws IOException {
try (InputStream in = Files.newInputStream(path)) { Settings settings = Settings.builder().loadFromPath(path).build();
Settings settings = Settings.builder().loadFromStream(path.toString(), in).build(); final String[] trustNodeNames = settings.getAsArray(RESTRICTIONS_KEY_SUBJECT_NAME);
final String[] trustNodeNames = settings.getAsArray(RESTRICTIONS_KEY_SUBJECT_NAME); return new CertificateTrustRestrictions(Arrays.asList(trustNodeNames));
return new CertificateTrustRestrictions(Arrays.asList(trustNodeNames));
}
} }
} }

View File

@ -586,7 +586,7 @@ public class DocumentLevelSecurityTests extends SecurityIntegTestCase {
public void testChildrenAggregation() throws Exception { public void testChildrenAggregation() throws Exception {
assertAcked(client().admin().indices().prepareCreate("test") assertAcked(client().admin().indices().prepareCreate("test")
.setSettings("index.version.created", Version.V_5_6_0.id) .setSettings(Settings.builder().put("index.version.created", Version.V_5_6_0.id))
.addMapping("type1", "field1", "type=text", "field2", "type=text") .addMapping("type1", "field1", "type=text", "field2", "type=text")
.addMapping("type2", "_parent", "type=type1", "field3", "type=text,fielddata=true") .addMapping("type2", "_parent", "type=type1", "field3", "type=text,fielddata=true")
); );
@ -643,7 +643,7 @@ public class DocumentLevelSecurityTests extends SecurityIntegTestCase {
public void testParentChild_parentField() { public void testParentChild_parentField() {
assertAcked(prepareCreate("test") assertAcked(prepareCreate("test")
.setSettings("index.version.created", Version.V_5_6_0.id) .setSettings(Settings.builder().put("index.version.created", Version.V_5_6_0.id))
.addMapping("parent") .addMapping("parent")
.addMapping("child", "_parent", "type=parent", "field1", "type=text", "field2", "type=text", "field3", "type=text")); .addMapping("child", "_parent", "type=parent", "field1", "type=text", "field2", "type=text", "field3", "type=text"));
ensureGreen(); ensureGreen();

View File

@ -1221,7 +1221,7 @@ public class FieldLevelSecurityTests extends SecurityIntegTestCase {
public void testParentChild_parentField() { public void testParentChild_parentField() {
assertAcked(prepareCreate("test") assertAcked(prepareCreate("test")
.setSettings("index.version.created", Version.V_5_6_0.id) .setSettings(Settings.builder().put("index.version.created", Version.V_5_6_0.id))
.addMapping("parent") .addMapping("parent")
.addMapping("child", "_parent", "type=parent")); .addMapping("child", "_parent", "type=parent"));
ensureGreen(); ensureGreen();

View File

@ -214,7 +214,7 @@ public abstract class SecurityIntegTestCase extends ESIntegTestCase {
// Disable native ML autodetect_process as the c++ controller won't be available // Disable native ML autodetect_process as the c++ controller won't be available
.put(MachineLearning.AUTODETECT_PROCESS.getKey(), false); .put(MachineLearning.AUTODETECT_PROCESS.getKey(), false);
Settings customSettings = customSecuritySettingsSource.nodeSettings(nodeOrdinal); Settings customSettings = customSecuritySettingsSource.nodeSettings(nodeOrdinal);
builder.put(customSettings.getAsMap()); // handle secure settings separately builder.put(customSettings, false); // handle secure settings separately
Settings.Builder customBuilder = Settings.builder().put(customSettings); Settings.Builder customBuilder = Settings.builder().put(customSettings);
if (customBuilder.getSecureSettings() != null) { if (customBuilder.getSecureSettings() != null) {
SecuritySettingsSource.addSecureSettings(builder, secureSettings -> SecuritySettingsSource.addSecureSettings(builder, secureSettings ->

View File

@ -79,7 +79,7 @@ public class GraphTests extends XPackSingleNodeTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
assertAcked(client().admin().indices().prepareCreate("test") assertAcked(client().admin().indices().prepareCreate("test")
.setSettings(SETTING_NUMBER_OF_SHARDS, 2, SETTING_NUMBER_OF_REPLICAS, 0) .setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 2).put(SETTING_NUMBER_OF_REPLICAS, 0))
.addMapping("type", .addMapping("type",
"decade", "type=keyword", "decade", "type=keyword",
"people", "type=keyword", "people", "type=keyword",

View File

@ -92,16 +92,12 @@ public class MonitoringSettingsIntegTests extends MonitoringIntegTestCase {
MonitoringSettings.JOB_STATS_TIMEOUT}; MonitoringSettings.JOB_STATS_TIMEOUT};
for (Setting<?> setting : monitoringSettings) { for (Setting<?> setting : monitoringSettings) {
if (setting.isDynamic()) { if (setting.isDynamic()) {
Object updated = null;
if (setting.get(Settings.EMPTY) instanceof TimeValue) { if (setting.get(Settings.EMPTY) instanceof TimeValue) {
updated = newRandomTimeValue(); transientSettings.put(setting.getKey(), newRandomTimeValue().toString());
transientSettings.put(setting.getKey(), updated);
} else if (setting.get(Settings.EMPTY) instanceof Boolean) { } else if (setting.get(Settings.EMPTY) instanceof Boolean) {
updated = randomBoolean(); transientSettings.put(setting.getKey(), randomBoolean());
transientSettings.put(setting.getKey(), updated);
} else if (setting.get(Settings.EMPTY) instanceof List) { } else if (setting.get(Settings.EMPTY) instanceof List) {
updated = randomStringArray(); transientSettings.putArray(setting.getKey(), randomStringArray());
transientSettings.putArray(setting.getKey(), (String[]) updated);
} else { } else {
fail("unknown dynamic setting [" + setting + "]"); fail("unknown dynamic setting [" + setting + "]");
} }

View File

@ -216,7 +216,7 @@ public class JiraAccountTests extends ESTestCase {
for (Map.Entry<String, Object> setting : defaults.entrySet()) { for (Map.Entry<String, Object> setting : defaults.entrySet()) {
String key = "xpack.notification.jira.account." + name + "." + JiraAccount.ISSUE_DEFAULTS_SETTING + "." + setting.getKey(); String key = "xpack.notification.jira.account." + name + "." + JiraAccount.ISSUE_DEFAULTS_SETTING + "." + setting.getKey();
if (setting.getValue() instanceof String) { if (setting.getValue() instanceof String) {
builder.put(key, setting.getValue()); builder.put(key, setting.getValue().toString());
} else if (setting.getValue() instanceof Map) { } else if (setting.getValue() instanceof Map) {
builder.putProperties((Map) setting.getValue(), s -> key + "." + s); builder.putProperties((Map) setting.getValue(), s -> key + "." + s);
} }

View File

@ -226,17 +226,19 @@ public class SecurityTribeIT extends NativeRealmIntegTestCase {
} }
}; };
final Settings settingsTemplate = cluster2SettingsSource.nodeSettings(0); final Settings settingsTemplate = cluster2SettingsSource.nodeSettings(0);
Map<String, String> asMap = new HashMap<>(settingsTemplate.getAsMap());
asMap.remove(NodeEnvironment.MAX_LOCAL_STORAGE_NODES_SETTING.getKey());
Settings.Builder tribe1Defaults = Settings.builder(); Settings.Builder tribe1Defaults = Settings.builder();
Settings.Builder tribe2Defaults = Settings.builder(); Settings.Builder tribe2Defaults = Settings.builder();
for (Map.Entry<String, String> entry : asMap.entrySet()) { Settings tribeSettings = settingsTemplate.filter(k -> {
if (entry.getKey().startsWith("path.")) { if (k.equals(NodeEnvironment.MAX_LOCAL_STORAGE_NODES_SETTING.getKey())) {
continue; return false;
} else if (entry.getKey().equals("transport.tcp.port")) { } if (k.startsWith("path.")) {
continue; return false;
} else if (k.equals("transport.tcp.port")) {
return false;
} }
return true;
});
for (Map.Entry<String, String> entry : tribeSettings.getAsMap().entrySet()) {
tribe1Defaults.put("tribe.t1." + entry.getKey(), entry.getValue()); tribe1Defaults.put("tribe.t1." + entry.getKey(), entry.getValue());
tribe2Defaults.put("tribe.t2." + entry.getKey(), entry.getValue()); tribe2Defaults.put("tribe.t2." + entry.getKey(), entry.getValue());
} }
@ -255,7 +257,7 @@ public class SecurityTribeIT extends NativeRealmIntegTestCase {
Settings merged = Settings.builder() Settings merged = Settings.builder()
.put(internalCluster().getDefaultSettings()) .put(internalCluster().getDefaultSettings())
.put(asMap) .put(tribeSettings, false)
.put("tribe.t1.cluster.name", internalCluster().getClusterName()) .put("tribe.t1.cluster.name", internalCluster().getClusterName())
.put("tribe.t2.cluster.name", cluster2.getClusterName()) .put("tribe.t2.cluster.name", cluster2.getClusterName())
.put("tribe.blocks.write", false) .put("tribe.blocks.write", false)

View File

@ -102,7 +102,6 @@ public class ServerTransportFilterIntegrationTests extends SecurityIntegTestCase
// test that starting up a node works // test that starting up a node works
Settings.Builder nodeSettings = Settings.builder() Settings.Builder nodeSettings = Settings.builder()
.put()
.put("node.name", "my-test-node") .put("node.name", "my-test-node")
.put("network.host", "localhost") .put("network.host", "localhost")
.put("cluster.name", internalCluster().getClusterName()) .put("cluster.name", internalCluster().getClusterName())

View File

@ -95,7 +95,7 @@ public class DNSOnlyHostnameVerificationTests extends SecurityIntegTestCase {
public Settings nodeSettings(int nodeOrdinal) { public Settings nodeSettings(int nodeOrdinal) {
Settings defaultSettings = super.nodeSettings(nodeOrdinal); Settings defaultSettings = super.nodeSettings(nodeOrdinal);
Settings.Builder builder = Settings.builder() Settings.Builder builder = Settings.builder()
.put(defaultSettings.filter((s) -> s.startsWith("xpack.ssl.") == false).getAsMap()) .put(defaultSettings.filter((s) -> s.startsWith("xpack.ssl.") == false), false)
.put("transport.host", hostName); .put("transport.host", hostName);
Path keystorePath = nodeConfigPath(nodeOrdinal).resolve("keystore.jks"); Path keystorePath = nodeConfigPath(nodeOrdinal).resolve("keystore.jks");
try (OutputStream os = Files.newOutputStream(keystorePath)) { try (OutputStream os = Files.newOutputStream(keystorePath)) {

View File

@ -31,7 +31,7 @@ public class IPHostnameVerificationTests extends SecurityIntegTestCase {
protected Settings nodeSettings(int nodeOrdinal) { protected Settings nodeSettings(int nodeOrdinal) {
Settings settings = super.nodeSettings(nodeOrdinal); Settings settings = super.nodeSettings(nodeOrdinal);
Settings.Builder builder = Settings.builder() Settings.Builder builder = Settings.builder()
.put(settings.filter((s) -> s.startsWith("xpack.ssl.") == false).getAsMap()); .put(settings.filter((s) -> s.startsWith("xpack.ssl.") == false), false);
settings = builder.build(); settings = builder.build();
// The default Unicast test behavior is to use 'localhost' with the port number. For this test we need to use IP // The default Unicast test behavior is to use 'localhost' with the port number. For this test we need to use IP

View File

@ -38,7 +38,7 @@ public class CustomRealmIT extends ESIntegTestCase {
protected Settings externalClusterClientSettings() { protected Settings externalClusterClientSettings() {
return Settings.builder() return Settings.builder()
.put(ThreadContext.PREFIX + "." + CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER) .put(ThreadContext.PREFIX + "." + CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER)
.put(ThreadContext.PREFIX + "." + CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW) .put(ThreadContext.PREFIX + "." + CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW.toString())
.put(NetworkModule.TRANSPORT_TYPE_KEY, "security4") .put(NetworkModule.TRANSPORT_TYPE_KEY, "security4")
.build(); .build();
} }
@ -78,7 +78,7 @@ public class CustomRealmIT extends ESIntegTestCase {
.put("cluster.name", clusterName) .put("cluster.name", clusterName)
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString())
.put(ThreadContext.PREFIX + "." + CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER) .put(ThreadContext.PREFIX + "." + CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER)
.put(ThreadContext.PREFIX + "." + CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW) .put(ThreadContext.PREFIX + "." + CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW.toString())
.build(); .build();
try (TransportClient client = new PreBuiltXPackTransportClient(settings)) { try (TransportClient client = new PreBuiltXPackTransportClient(settings)) {
client.addTransportAddress(publishAddress); client.addTransportAddress(publishAddress);
@ -98,7 +98,7 @@ public class CustomRealmIT extends ESIntegTestCase {
.put("cluster.name", clusterName) .put("cluster.name", clusterName)
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString())
.put(ThreadContext.PREFIX + "." + CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER + randomAlphaOfLength(1)) .put(ThreadContext.PREFIX + "." + CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER + randomAlphaOfLength(1))
.put(ThreadContext.PREFIX + "." + CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW) .put(ThreadContext.PREFIX + "." + CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW.toString())
.build(); .build();
try (TransportClient client = new PreBuiltXPackTransportClient(settings)) { try (TransportClient client = new PreBuiltXPackTransportClient(settings)) {
client.addTransportAddress(publishAddress); client.addTransportAddress(publishAddress);

View File

@ -40,7 +40,7 @@ public class CustomRolesProviderIT extends ESIntegTestCase {
protected Settings externalClusterClientSettings() { protected Settings externalClusterClientSettings() {
return Settings.builder() return Settings.builder()
.put(ThreadContext.PREFIX + "." + CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER) .put(ThreadContext.PREFIX + "." + CustomRealm.USER_HEADER, CustomRealm.KNOWN_USER)
.put(ThreadContext.PREFIX + "." + CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW) .put(ThreadContext.PREFIX + "." + CustomRealm.PW_HEADER, CustomRealm.KNOWN_PW.toString())
.put(NetworkModule.TRANSPORT_TYPE_KEY, "security4") .put(NetworkModule.TRANSPORT_TYPE_KEY, "security4")
.build(); .build();
} }