Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compat/rest controller xpack #59703

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.elasticsearch.compat;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.node.DiscoveryNodes;
Expand Down Expand Up @@ -50,6 +52,7 @@
import java.util.function.Supplier;

public class RestCompatPlugin extends Plugin implements ActionPlugin {
Logger log = LogManager.getLogger(RestCompatPlugin.class);

@Override
public List<RestHandler> getRestHandlers(
Expand Down
24 changes: 21 additions & 3 deletions server/src/main/java/org/elasticsearch/action/ActionModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,11 @@
import org.elasticsearch.persistent.UpdatePersistentTaskStatusAction;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.ActionPlugin.ActionHandler;
import org.elasticsearch.plugins.RestRequestPlugin;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestHeaderDefinition;
import org.elasticsearch.rest.RestRequestFactory;
import org.elasticsearch.rest.action.RestFieldCapabilitiesAction;
import org.elasticsearch.rest.action.RestMainAction;
import org.elasticsearch.rest.action.admin.cluster.RestAddVotingConfigExclusionAction;
Expand Down Expand Up @@ -446,7 +448,8 @@ public class ActionModule extends AbstractModule {
public ActionModule(Settings settings, IndexNameExpressionResolver indexNameExpressionResolver,
IndexScopedSettings indexScopedSettings, ClusterSettings clusterSettings, SettingsFilter settingsFilter,
ThreadPool threadPool, List<ActionPlugin> actionPlugins, NodeClient nodeClient,
CircuitBreakerService circuitBreakerService, UsageService usageService, ClusterService clusterService) {
CircuitBreakerService circuitBreakerService, UsageService usageService, ClusterService clusterService,
List<RestRequestPlugin> restRequestPlugins) {
this.settings = settings;
this.indexNameExpressionResolver = indexNameExpressionResolver;
this.indexScopedSettings = indexScopedSettings;
Expand Down Expand Up @@ -477,10 +480,25 @@ public ActionModule(Settings settings, IndexNameExpressionResolver indexNameExpr
actionPlugins.stream().flatMap(p -> p.mappingRequestValidators().stream()).collect(Collectors.toList()));
indicesAliasesRequestRequestValidators = new RequestValidators<>(
actionPlugins.stream().flatMap(p -> p.indicesAliasesRequestValidators().stream()).collect(Collectors.toList()));

restController = new RestController(headers, restWrapper, nodeClient, circuitBreakerService, usageService);
RestRequestFactory restRequestFactory = getRestRequestFactory(restRequestPlugins);
restController = new RestController(headers, restWrapper, nodeClient, circuitBreakerService, usageService, restRequestFactory);
}

private RestRequestFactory getRestRequestFactory(List<RestRequestPlugin> restRequestPlugins) {
RestRequestFactory restRequestFactory = null;
for (RestRequestPlugin plugin : restRequestPlugins) {
RestRequestFactory newRestRequestFactory = plugin.getRestRequestFactory();
if (newRestRequestFactory != null) {
logger.debug("Using REST request factory from plugin " + plugin.getClass().getName());
if (restRequestFactory != null) {
//TODO maybe we could combine/chain them?
throw new IllegalArgumentException("Cannot have more than one plugin implementing a RestRequestPlugin");
}
restRequestFactory = newRestRequestFactory;
}
}
return restRequestFactory != null ? restRequestFactory : r -> r;
}

public Map<String, ActionHandler<?, ?>> getActions() {
return actions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ private void handleIncomingRequest(final HttpRequest httpRequest, final HttpChan
{
RestRequest innerRestRequest;
try {
//TODO another option would be to use a factory here, but it requires to modify all the subclasses..
innerRestRequest = RestRequest.request(xContentRegistry, httpRequest, httpChannel);
} catch (final RestRequest.ContentTypeHeaderException e) {
badRequestCause = ExceptionsHelper.useOrSuppress(badRequestCause, e);
Expand Down
4 changes: 3 additions & 1 deletion server/src/main/java/org/elasticsearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.PluginsService;
import org.elasticsearch.plugins.RepositoryPlugin;
import org.elasticsearch.plugins.RestRequestPlugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.plugins.SearchPlugin;
import org.elasticsearch.plugins.SystemIndexPlugin;
Expand Down Expand Up @@ -511,7 +512,8 @@ protected Node(final Environment initialEnvironment,

ActionModule actionModule = new ActionModule(settings, clusterModule.getIndexNameExpressionResolver(),
settingsModule.getIndexScopedSettings(), settingsModule.getClusterSettings(), settingsModule.getSettingsFilter(),
threadPool, pluginsService.filterPlugins(ActionPlugin.class), client, circuitBreakerService, usageService, clusterService);
threadPool, pluginsService.filterPlugins(ActionPlugin.class), client, circuitBreakerService, usageService, clusterService,
pluginsService.filterPlugins(RestRequestPlugin.class));
modules.add(actionModule);

final RestController restController = actionModule.getRestController();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ default UnaryOperator<RestHandler> getRestHandlerWrapper(ThreadContext threadCon
return null;
}

// RestRequestFactory getRestRequestFactory();

final class ActionHandler<Request extends ActionRequest, Response extends ActionResponse> {
private final ActionType<Response> action;
private final Class<? extends TransportAction<Request, Response>> transportAction;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.plugins;

import org.elasticsearch.rest.RestRequestFactory;

public interface RestRequestPlugin {
default RestRequestFactory getRestRequestFactory() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ public class RestController implements HttpServerTransport.Dispatcher {
/** Rest headers that are copied to internal requests made during a rest request. */
private final Set<RestHeaderDefinition> headersToCopy;
private final UsageService usageService;
private RestRequestFactory restRequestFactory;

public RestController(Set<RestHeaderDefinition> headersToCopy, UnaryOperator<RestHandler> handlerWrapper,
NodeClient client, CircuitBreakerService circuitBreakerService, UsageService usageService) {
NodeClient client, CircuitBreakerService circuitBreakerService, UsageService usageService,
RestRequestFactory restRequestFactory) {
this.headersToCopy = headersToCopy;
this.usageService = usageService;
this.restRequestFactory = restRequestFactory;
if (handlerWrapper == null) {
handlerWrapper = h -> h; // passthrough if no wrapper set
}
Expand Down Expand Up @@ -176,8 +179,10 @@ public void dispatchRequest(RestRequest request, RestChannel channel, ThreadCont
handleFavicon(request.method(), request.uri(), channel);
return;
}
RestRequest restRequest = restRequestFactory.createRestRequest(request);

try {
tryAllHandlers(request, channel, threadContext);
tryAllHandlers(restRequest, channel, threadContext);
} catch (Exception e) {
try {
channel.sendResponse(new BytesRestResponse(channel, e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ public static class BadParameterException extends RuntimeException {

public static class CompatibleApiHeadersCombinationException extends RuntimeException {

CompatibleApiHeadersCombinationException(String cause) {
public CompatibleApiHeadersCombinationException(String cause) {
super(cause);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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;

public interface RestRequestFactory {
RestRequest createRestRequest(RestRequest restRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void testSetupRestHandlerContainsKnownBuiltin() {
UsageService usageService = new UsageService();
ActionModule actionModule = new ActionModule(settings.getSettings(), new IndexNameExpressionResolver(),
settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), null, emptyList(), null,
null, usageService, null);
null, usageService, null, null);
actionModule.initRestHandlers(null);
// At this point the easiest way to confirm that a handler is loaded is to try to register another one on top of it and to fail
Exception e = expectThrows(IllegalArgumentException.class, () ->
Expand Down Expand Up @@ -148,7 +148,7 @@ public String getName() {
UsageService usageService = new UsageService();
ActionModule actionModule = new ActionModule(settings.getSettings(), new IndexNameExpressionResolver(),
settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool,
singletonList(dupsMainAction), null, null, usageService, null);
singletonList(dupsMainAction), null, null, usageService, null, null);
Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.initRestHandlers(null));
assertThat(e.getMessage(), startsWith("Cannot replace existing handler for [/] for method: GET"));
} finally {
Expand Down Expand Up @@ -182,7 +182,7 @@ public List<RestHandler> getRestHandlers(Settings settings, RestController restC
UsageService usageService = new UsageService();
ActionModule actionModule = new ActionModule(settings.getSettings(), new IndexNameExpressionResolver(),
settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool,
singletonList(registersFakeHandler), null, null, usageService, null);
singletonList(registersFakeHandler), null, null, usageService, null, null);
actionModule.initRestHandlers(null);
// At this point the easiest way to confirm that a handler is loaded is to try to register another one on top of it and to fail
Exception e = expectThrows(IllegalArgumentException.class, () ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void setup() {
inFlightRequestsBreaker = circuitBreakerService.getBreaker(CircuitBreaker.IN_FLIGHT_REQUESTS);

HttpServerTransport httpServerTransport = new TestHttpServerTransport();
restController = new RestController(Collections.emptySet(), null, null, circuitBreakerService, usageService);
restController = new RestController(Collections.emptySet(), null, null, circuitBreakerService, usageService, r -> r);
restController.registerHandler(RestRequest.Method.GET, "/",
(request, channel, client) -> channel.sendResponse(
new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY)));
Expand All @@ -110,7 +110,7 @@ public void testApplyRelevantHeaders() throws Exception {
final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
Set<RestHeaderDefinition> headers = new HashSet<>(Arrays.asList(new RestHeaderDefinition("header.1", true),
new RestHeaderDefinition("header.2", true)));
final RestController restController = new RestController(headers, null, null, circuitBreakerService, usageService);
final RestController restController = new RestController(headers, null, null, circuitBreakerService, usageService, r -> r);
Map<String, List<String>> restHeaders = new HashMap<>();
restHeaders.put("header.1", Collections.singletonList("true"));
restHeaders.put("header.2", Collections.singletonList("true"));
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testRequestWithDisallowedMultiValuedHeader() {
final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
Set<RestHeaderDefinition> headers = new HashSet<>(Arrays.asList(new RestHeaderDefinition("header.1", true),
new RestHeaderDefinition("header.2", false)));
final RestController restController = new RestController(headers, null, null, circuitBreakerService, usageService);
final RestController restController = new RestController(headers, null, null, circuitBreakerService, usageService, r -> r);
Map<String, List<String>> restHeaders = new HashMap<>();
restHeaders.put("header.1", Collections.singletonList("boo"));
restHeaders.put("header.2", List.of("foo", "bar"));
Expand All @@ -160,7 +160,7 @@ public void testRequestWithDisallowedMultiValuedHeaderButSameValues() {
final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
Set<RestHeaderDefinition> headers = new HashSet<>(Arrays.asList(new RestHeaderDefinition("header.1", true),
new RestHeaderDefinition("header.2", false)));
final RestController restController = new RestController(headers, null, null, circuitBreakerService, usageService);
final RestController restController = new RestController(headers, null, null, circuitBreakerService, usageService, r -> r);
Map<String, List<String>> restHeaders = new HashMap<>();
restHeaders.put("header.1", Collections.singletonList("boo"));
restHeaders.put("header.2", List.of("foo", "foo"));
Expand Down Expand Up @@ -214,7 +214,7 @@ public void testRegisterWithDeprecatedHandler() {
}

public void testRegisterSecondMethodWithDifferentNamedWildcard() {
final RestController restController = new RestController(null, null, null, circuitBreakerService, usageService);
final RestController restController = new RestController(null, null, null, circuitBreakerService, usageService, r -> r);

RestRequest.Method firstMethod = randomFrom(RestRequest.Method.values());
RestRequest.Method secondMethod =
Expand Down Expand Up @@ -247,7 +247,7 @@ public void testRestHandlerWrapper() throws Exception {
h -> {
assertSame(handler, h);
return (RestRequest request, RestChannel channel, NodeClient client) -> wrapperCalled.set(true);
}, null, circuitBreakerService, usageService);
}, null, circuitBreakerService, usageService, r -> r);
restController.registerHandler(RestRequest.Method.GET, "/wrapped", handler);
RestRequest request = testRestRequest("/wrapped", "{}", XContentType.JSON);
AssertingChannel channel = new AssertingChannel(request, true, RestStatus.BAD_REQUEST);
Expand Down Expand Up @@ -310,7 +310,7 @@ public void testDispatchRequiresContentTypeForRequestsWithContent() {
String content = randomAlphaOfLength((int) Math.round(BREAKER_LIMIT.getBytes() / inFlightRequestsBreaker.getOverhead()));
RestRequest request = testRestRequest("/", content, null);
AssertingChannel channel = new AssertingChannel(request, true, RestStatus.NOT_ACCEPTABLE);
restController = new RestController(Collections.emptySet(), null, null, circuitBreakerService, usageService);
restController = new RestController(Collections.emptySet(), null, null, circuitBreakerService, usageService, r -> r);
restController.registerHandler(RestRequest.Method.GET, "/",
(r, c, client) -> c.sendResponse(
new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testUnsupportedMethodResponseHttpHeader() throws Exception {
final Settings settings = Settings.EMPTY;
UsageService usageService = new UsageService();
RestController restController = new RestController(Collections.emptySet(),
null, null, circuitBreakerService, usageService);
null, null, circuitBreakerService, usageService, r -> r);

// A basic RestHandler handles requests to the endpoint
RestHandler restHandler = new RestHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class RestValidateQueryActionTests extends AbstractSearchTestCase {

private static UsageService usageService = new UsageService();
private static RestController controller = new RestController(emptySet(), null, client,
new NoneCircuitBreakerService(), usageService);
new NoneCircuitBreakerService(), usageService, r -> r);
private static RestValidateQueryAction action = new RestValidateQueryAction();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void setUpController() {
controller = new RestController(Collections.emptySet(), null,
nodeClient,
new NoneCircuitBreakerService(),
new UsageService());
new UsageService(), r -> r);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions x-pack/plugin/rest-compat/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
evaluationDependsOn(xpackModule('core'))

apply plugin: 'elasticsearch.esplugin'

esplugin {
name 'rest-compat'
description 'A plugin for Rest compat request features'
classname 'org.elasticsearch.compat.RestCompatRequestPlugin'
extendedPlugins = ['x-pack-core']
}

dependencies {
compileOnly project(path: xpackModule('core'), configuration: 'default')
testImplementation project(path: xpackModule('core'), configuration: 'testArtifacts')
}


integTest.enabled = false
Loading