Skip to content

Commit 442c4f7

Browse files
committed
Assert that REST params are consumed iff supported
REST APIs which declare their supported parameters must consume exactly those parameters: consuming an unsupported parameter means that requests including that parameter will be rejected, whereas failing to consume a supported parameter means that this parameter has no effect and should be removed. This commit adds an assertion to verify that we are consuming the correct parameters. Closes elastic#113854
1 parent d3dbbb2 commit 442c4f7

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

modules/data-streams/src/main/java/org/elasticsearch/datastreams/rest/RestGetDataStreamsAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.action.datastreams.GetDataStreamAction;
1212
import org.elasticsearch.action.support.IndicesOptions;
1313
import org.elasticsearch.client.internal.node.NodeClient;
14+
import org.elasticsearch.cluster.metadata.DataStream;
1415
import org.elasticsearch.cluster.metadata.DataStreamLifecycle;
1516
import org.elasticsearch.common.Strings;
1617
import org.elasticsearch.common.util.set.Sets;
@@ -35,14 +36,14 @@ public class RestGetDataStreamsAction extends BaseRestHandler {
3536
Set.of(
3637
"name",
3738
"include_defaults",
38-
"timeout",
3939
"master_timeout",
4040
IndicesOptions.WildcardOptions.EXPAND_WILDCARDS,
4141
IndicesOptions.ConcreteTargetOptions.IGNORE_UNAVAILABLE,
4242
IndicesOptions.WildcardOptions.ALLOW_NO_INDICES,
4343
IndicesOptions.GatekeeperOptions.IGNORE_THROTTLED,
4444
"verbose"
45-
)
45+
),
46+
DataStream.isFailureStoreFeatureFlagEnabled() ? Set.of(IndicesOptions.FailureStoreOptions.FAILURE_STORE) : Set.of()
4647
)
4748
);
4849

server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.common.settings.Setting.Property;
1717
import org.elasticsearch.common.util.set.Sets;
1818
import org.elasticsearch.core.CheckedConsumer;
19+
import org.elasticsearch.core.Nullable;
1920
import org.elasticsearch.core.RefCounted;
2021
import org.elasticsearch.core.Releasable;
2122
import org.elasticsearch.core.RestApiVersion;
@@ -104,6 +105,8 @@ public final void handleRequest(RestRequest request, RestChannel channel, NodeCl
104105
// prepare the request for execution; has the side effect of touching the request parameters
105106
try (var action = prepareRequest(request, client)) {
106107

108+
assert assertConsumesSupportedParams(supported, request);
109+
107110
// validate unconsumed params, but we must exclude params used to format the response
108111
// use a sorted set so the unconsumed parameters appear in a reliable sorted order
109112
final SortedSet<String> unconsumedParams = request.unconsumedParams()
@@ -148,6 +151,20 @@ public void close() {
148151
}
149152
}
150153

154+
private boolean assertConsumesSupportedParams(@Nullable Set<String> supported, RestRequest request) {
155+
if (supported != null) {
156+
final var supportedAndCommon = new TreeSet<>(supported);
157+
supportedAndCommon.add("error_trace");
158+
supportedAndCommon.addAll(ALWAYS_SUPPORTED);
159+
supportedAndCommon.removeAll(RestRequest.INTERNAL_MARKER_REQUEST_PARAMETERS);
160+
final var consumed = new TreeSet<>(request.consumedParams());
161+
consumed.removeAll(RestRequest.INTERNAL_MARKER_REQUEST_PARAMETERS);
162+
assert supportedAndCommon.equals(consumed)
163+
: getName() + ": consumed params " + consumed + " while supporting " + supportedAndCommon;
164+
}
165+
return true;
166+
}
167+
151168
protected static String unrecognized(RestRequest request, Set<String> invalids, Set<String> candidates, String detail) {
152169
StringBuilder message = new StringBuilder().append("request [")
153170
.append(request.path())

0 commit comments

Comments
 (0)