Whitelist node stats indices level parameter

When indices stats are requested via the node stats API, there is a
level parameter to request stats at the index, node, or shards
level. This parameter was not whitelisted when URL parsing was made
strict. This commit whitelists this parameter.

Additionally, there was some leniency in the parsing of this parameter
that has been removed.

Relates #21024
This commit is contained in:
Jason Tedor 2016-10-19 22:01:17 -04:00 committed by GitHub
parent e7655bbf80
commit 7a55cca122
5 changed files with 71 additions and 5 deletions

View File

@ -188,10 +188,11 @@ public class NodeIndicesStats implements Streamable, ToXContent {
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
String level = params.param("level", "node"); final String level = params.param("level", "node");
boolean isLevelValid = "node".equalsIgnoreCase(level) || "indices".equalsIgnoreCase(level) || "shards".equalsIgnoreCase(level); final boolean isLevelValid =
"indices".equalsIgnoreCase(level) || "node".equalsIgnoreCase(level) || "shards".equalsIgnoreCase(level);
if (!isLevelValid) { if (!isLevelValid) {
return builder; throw new IllegalArgumentException("level parameter must be one of [indices] or [node] or [shards] but was [" + level + "]");
} }
// "node" level // "node" level

View File

@ -32,6 +32,7 @@ import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestActions.NodesResponseRestListener; import org.elasticsearch.rest.action.RestActions.NodesResponseRestListener;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.Set; import java.util.Set;
import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.GET;
@ -114,8 +115,16 @@ public class RestNodesStatsAction extends BaseRestHandler {
return channel -> client.admin().cluster().nodesStats(nodesStatsRequest, new NodesResponseRestListener<>(channel)); return channel -> client.admin().cluster().nodesStats(nodesStatsRequest, new NodesResponseRestListener<>(channel));
} }
private final Set<String> RESPONSE_PARAMS = Collections.singleton("level");
@Override
protected Set<String> responseParams() {
return RESPONSE_PARAMS;
}
@Override @Override
public boolean canTripCircuitBreaker() { public boolean canTripCircuitBreaker() {
return false; return false;
} }
} }

View File

@ -0,0 +1,42 @@
/*
* 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.indices;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.test.ESTestCase;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.object.HasToString.hasToString;
public class NodeIndicesStatsTests extends ESTestCase {
public void testInvalidLevel() {
final NodeIndicesStats stats = new NodeIndicesStats();
final String level = randomAsciiOfLength(16);
final ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap("level", level));
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> stats.toXContent(null, params));
assertThat(
e,
hasToString(containsString("level parameter must be one of [indices] or [node] or [shards] but was [" + level + "]")));
}
}

View File

@ -52,8 +52,8 @@
}, },
"level": { "level": {
"type" : "enum", "type" : "enum",
"description": "Return indices stats aggregated at node, index or shard level", "description": "Return indices stats aggregated at index, node or shard level",
"options" : ["node", "indices", "shards"], "options" : ["indices", "node", "shards"],
"default" : "node" "default" : "node"
}, },
"types" : { "types" : {

View File

@ -6,3 +6,17 @@
- is_true: cluster_name - is_true: cluster_name
- is_true: nodes - is_true: nodes
---
"Nodes stats level":
- do:
cluster.state: {}
- set: { master_node: master }
- do:
nodes.stats:
metric: [ indices ]
level: "indices"
- is_true: nodes.$master.indices.indices