From cfe3d9f611a328cfffc18b445b3aecb016349514 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Wed, 29 Aug 2018 15:56:13 -0400 Subject: [PATCH] Apply settings filter to get cluster settings API (#33247) Some settings have filters applied to them and we use this in logs and the get nodes info API. For consistency, we should apply this in the get cluster settings API too. --- .../cluster/RestClusterGetSettingsAction.java | 79 ++++++++++++++++--- .../RestClusterGetSettingsActionTests.java | 74 +++++++++++++++++ 2 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/rest/action/admin/cluster/RestClusterGetSettingsActionTests.java diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterGetSettingsAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterGetSettingsAction.java index 5c3be8f4347f2..cedc7134f8b4d 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterGetSettingsAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterGetSettingsAction.java @@ -63,7 +63,9 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC return channel -> client.admin().cluster().state(clusterStateRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(ClusterStateResponse response, XContentBuilder builder) throws Exception { - return new BytesRestResponse(RestStatus.OK, renderResponse(response.getState(), renderDefaults, builder, request)); + return new BytesRestResponse( + RestStatus.OK, + renderResponse(response.getState(), renderDefaults, settingsFilter, clusterSettings, settings, builder, request)); } }); } @@ -78,25 +80,76 @@ public boolean canTripCircuitBreaker() { return false; } - private XContentBuilder renderResponse(ClusterState state, boolean renderDefaults, XContentBuilder builder, ToXContent.Params params) + static XContentBuilder renderResponse( + final ClusterState state, + final boolean renderDefaults, + final SettingsFilter settingsFilter, + final ClusterSettings clusterSettings, + final Settings settings, + final XContentBuilder builder, + final ToXContent.Params params) throws IOException { + final ClusterGetSettingsResponse response = + new ClusterGetSettingsResponse(state, renderDefaults, settingsFilter, clusterSettings, settings); builder.startObject(); + { + builder.startObject("persistent"); + { + response.persistentSettings().toXContent(builder, params); + } + builder.endObject(); - builder.startObject("persistent"); - state.metaData().persistentSettings().toXContent(builder, params); - builder.endObject(); + builder.startObject("transient"); + { + response.transientSettings().toXContent(builder, params); + } + builder.endObject(); - builder.startObject("transient"); - state.metaData().transientSettings().toXContent(builder, params); + if (renderDefaults) { + builder.startObject("defaults"); + { + response.defaultSettings().toXContent(builder, params); + } + builder.endObject(); + } + } builder.endObject(); + return builder; + } - if (renderDefaults) { - builder.startObject("defaults"); - settingsFilter.filter(clusterSettings.diff(state.metaData().settings(), this.settings)).toXContent(builder, params); - builder.endObject(); + static final class ClusterGetSettingsResponse { + + private final Settings persistentSettings; + + Settings persistentSettings() { + return persistentSettings; + } + + private final Settings transientSettings; + + Settings transientSettings() { + return transientSettings; + } + + private final Settings defaultSettings; + + Settings defaultSettings() { + return defaultSettings; + } + + ClusterGetSettingsResponse( + final ClusterState state, + final boolean renderDefaults, + final SettingsFilter settingsFilter, + final ClusterSettings clusterSettings, + final Settings settings) { + this.persistentSettings = settingsFilter.filter(state.metaData().persistentSettings()); + this.transientSettings = settingsFilter.filter(state.metaData().transientSettings()); + this.defaultSettings = renderDefaults ? + settingsFilter.filter(clusterSettings.diff(state.metaData().settings(), settings)) : + Settings.EMPTY; } - builder.endObject(); - return builder; } + } diff --git a/core/src/test/java/org/elasticsearch/rest/action/admin/cluster/RestClusterGetSettingsActionTests.java b/core/src/test/java/org/elasticsearch/rest/action/admin/cluster/RestClusterGetSettingsActionTests.java new file mode 100644 index 0000000000000..f59f484b47f37 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/rest/action/admin/cluster/RestClusterGetSettingsActionTests.java @@ -0,0 +1,74 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch 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.elasticsearch.rest.action.admin.cluster; + +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.common.settings.ClusterSettings; +import org.elasticsearch.common.settings.Setting; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.settings.SettingsFilter; +import org.elasticsearch.test.ESTestCase; + +import java.util.Collections; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class RestClusterGetSettingsActionTests extends ESTestCase { + + public void testFilterPersistentSettings() { + runTestFilterSettingsTest( + MetaData.Builder::persistentSettings, + RestClusterGetSettingsAction.ClusterGetSettingsResponse::persistentSettings); + } + + public void testFilterTransientSettings() { + runTestFilterSettingsTest( + MetaData.Builder::transientSettings, + RestClusterGetSettingsAction.ClusterGetSettingsResponse::transientSettings); + } + + private void runTestFilterSettingsTest( + final BiConsumer md, + final Function s) { + final MetaData.Builder mdBuilder = new MetaData.Builder(); + final Settings settings = Settings.builder().put("foo.filtered", "bar").put("foo.non_filtered", "baz").build(); + md.accept(mdBuilder, settings); + final ClusterState.Builder builder = new ClusterState.Builder(ClusterState.EMPTY_STATE).metaData(mdBuilder); + final SettingsFilter filter = new SettingsFilter(Settings.EMPTY, Collections.singleton("foo.filtered")); + final Setting.Property[] properties = {Setting.Property.Dynamic, Setting.Property.Filtered, Setting.Property.NodeScope}; + final Set> settingsSet = Stream.concat( + ClusterSettings.BUILT_IN_CLUSTER_SETTINGS.stream(), + Stream.concat( + Stream.of(Setting.simpleString("foo.filtered", properties)), + Stream.of(Setting.simpleString("foo.non_filtered", properties)))) + .collect(Collectors.toSet()); + final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, settingsSet); + final RestClusterGetSettingsAction.ClusterGetSettingsResponse response = + new RestClusterGetSettingsAction.ClusterGetSettingsResponse( + builder.build(), randomBoolean(), filter, clusterSettings, Settings.EMPTY); + assertFalse(s.apply(response).hasValue("foo.filtered")); + assertTrue(s.apply(response).hasValue("foo.non_filtered")); + } + +}