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

Copy http headers to ThreadContext strictly #45945

Merged
merged 10 commits into from Oct 30, 2019
Expand Up @@ -43,6 +43,7 @@
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestHeaderDefinition;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.tasks.Task;
Expand Down Expand Up @@ -161,8 +162,9 @@ public List<ActionFilter> getActionFilters() {
}

@Override
public Collection<String> getRestHeaders() {
return Arrays.asList(TestFilter.AUTHORIZATION_HEADER, TestFilter.EXAMPLE_HEADER);
public Collection<RestHeaderDefinition> getRestHeaders() {
return Arrays.asList(new RestHeaderDefinition(TestFilter.AUTHORIZATION_HEADER, false),
new RestHeaderDefinition(TestFilter.EXAMPLE_HEADER, false));
}
}

Expand Down
Expand Up @@ -228,6 +228,7 @@
import org.elasticsearch.plugins.ActionPlugin.ActionHandler;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestHeaderDefinition;
import org.elasticsearch.rest.action.RestFieldCapabilitiesAction;
import org.elasticsearch.rest.action.RestMainAction;
import org.elasticsearch.rest.action.admin.cluster.RestAddVotingConfigExclusionAction;
Expand Down Expand Up @@ -387,9 +388,9 @@ public ActionModule(Settings settings, IndexNameExpressionResolver indexNameExpr
actionFilters = setupActionFilters(actionPlugins);
autoCreateIndex = new AutoCreateIndex(settings, clusterSettings, indexNameExpressionResolver);
destructiveOperations = new DestructiveOperations(settings, clusterSettings);
Set<String> headers = Stream.concat(
Set<RestHeaderDefinition> headers = Stream.concat(
actionPlugins.stream().flatMap(p -> p.getRestHeaders().stream()),
Stream.of(Task.X_OPAQUE_ID)
Stream.of(new RestHeaderDefinition(Task.X_OPAQUE_ID, false))
).collect(Collectors.toSet());
UnaryOperator<RestHandler> restWrapper = null;
for (ActionPlugin plugin : actionPlugins) {
Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestHeaderDefinition;

import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -91,7 +92,7 @@ default List<RestHandler> getRestHandlers(Settings settings, RestController rest
/**
* Returns headers which should be copied through rest requests on to internal requests.
*/
default Collection<String> getRestHeaders() {
default Collection<RestHeaderDefinition> getRestHeaders() {
return Collections.emptyList();
}

Expand Down
21 changes: 15 additions & 6 deletions server/src/main/java/org/elasticsearch/rest/RestController.java
Expand Up @@ -50,6 +50,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

import static org.elasticsearch.rest.BytesRestResponse.TEXT_CONTENT_TYPE;
import static org.elasticsearch.rest.RestStatus.BAD_REQUEST;
Expand All @@ -71,10 +72,10 @@ public class RestController implements HttpServerTransport.Dispatcher {
private final CircuitBreakerService circuitBreakerService;

/** Rest headers that are copied to internal requests made during a rest request. */
private final Set<String> headersToCopy;
private final Set<RestHeaderDefinition> headersToCopy;
private final UsageService usageService;

public RestController(Set<String> headersToCopy, UnaryOperator<RestHandler> handlerWrapper,
public RestController(Set<RestHeaderDefinition> headersToCopy, UnaryOperator<RestHandler> handlerWrapper,
NodeClient client, CircuitBreakerService circuitBreakerService, UsageService usageService) {
this.headersToCopy = headersToCopy;
this.usageService = usageService;
Expand Down Expand Up @@ -255,10 +256,18 @@ private void sendContentTypeErrorMessage(@Nullable List<String> contentTypeHeade
}

private void tryAllHandlers(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) throws Exception {
for (String key : headersToCopy) {
String httpHeader = request.header(key);
if (httpHeader != null) {
threadContext.putHeader(key, httpHeader);
for (RestHeaderDefinition restHeader : headersToCopy) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop iterator can be final.

final String name = restHeader.getName();
final List<String> headerValues = request.getAllHeaderValues(name);
if (headerValues != null) {
if (restHeader.isMultiValueAllowed() == false && new HashSet<>(headerValues).size() > 1) {
channel.sendResponse(
BytesRestResponse.
createSimpleErrorResponse(channel, BAD_REQUEST, "multiple values for single-valued header [" + name + "]."));
return;
} else {
threadContext.putHeader(name, headerValues.stream().distinct().collect(Collectors.joining(",")));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is effectively going to collect allocate a set to collect the header values twice in case multi-value headers are not allowed. Once to collect the header values into a set to see if its size is one, and then again (when the set is size one) to do the trivial join. Since we always need to collect the distinct values (either to check if we should fail the request, or put the header in the context), I think we should refactor this slightly to avoid collecting twice and all the allocations that come with it.

}
}
}
// error_trace cannot be used when we disable detailed errors
Expand Down
@@ -0,0 +1,46 @@
/*
* 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;

/**
* A definition for an http header that should be copied to the {@link org.elasticsearch.common.util.concurrent.ThreadContext} when
* reading the request on the rest layer.
*/
public final class RestHeaderDefinition {
private final String name;
/**
* This should be set to true only when the syntax of the value of the Header to copy is defined as a comma separated list of String
* values.
*/
private final boolean multiValueAllowed;

public RestHeaderDefinition(String name, boolean multiValueAllowed) {
this.name = name;
this.multiValueAllowed = multiValueAllowed;
}

public String getName() {
return name;
}

public boolean isMultiValueAllowed() {
return multiValueAllowed;
}
}
Expand Up @@ -105,7 +105,8 @@ public void setup() {

public void testApplyRelevantHeaders() throws Exception {
final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
Set<String> headers = new HashSet<>(Arrays.asList("header.1", "header.2"));
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);
Map<String, List<String>> restHeaders = new HashMap<>();
restHeaders.put("header.1", Collections.singletonList("true"));
Expand Down Expand Up @@ -138,6 +139,40 @@ public MethodHandlers next() {
assertNull(threadContext.getHeader("header.3"));
}

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);
Map<String, List<String>> restHeaders = new HashMap<>();
restHeaders.put("header.1", Collections.singletonList("boo"));
restHeaders.put("header.2", List.of("foo", "bar"));
RestRequest fakeRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(restHeaders).build();
AssertingChannel channel = new AssertingChannel(fakeRequest, false, RestStatus.BAD_REQUEST);
restController.dispatchRequest(fakeRequest, channel, threadContext);
assertTrue(channel.getSendResponseCalled());
}

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);
Map<String, List<String>> restHeaders = new HashMap<>();
restHeaders.put("header.1", Collections.singletonList("boo"));
restHeaders.put("header.2", List.of("foo", "foo"));
RestRequest fakeRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(restHeaders).withPath("/bar").build();
restController.registerHandler(RestRequest.Method.GET, "/bar", new RestHandler() {
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
}
});
AssertingChannel channel = new AssertingChannel(fakeRequest, false, RestStatus.OK);
restController.dispatchRequest(fakeRequest, channel, threadContext);
assertTrue(channel.getSendResponseCalled());
}

public void testRegisterAsDeprecatedHandler() {
RestController controller = mock(RestController.class);

Expand Down
Expand Up @@ -62,6 +62,7 @@
import org.elasticsearch.repositories.Repository;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestHeaderDefinition;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.threadpool.ExecutorBuilder;
Expand Down Expand Up @@ -152,8 +153,8 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
}

@Override
public Collection<String> getRestHeaders() {
List<String> headers = new ArrayList<>();
public Collection<RestHeaderDefinition> getRestHeaders() {
List<RestHeaderDefinition> headers = new ArrayList<>();
headers.addAll(super.getRestHeaders());
filterPlugins(ActionPlugin.class).stream().forEach(p -> headers.addAll(p.getRestHeaders()));
return headers;
Expand Down
Expand Up @@ -57,6 +57,7 @@
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestHeaderDefinition;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.threadpool.ExecutorBuilder;
import org.elasticsearch.threadpool.FixedExecutorBuilder;
Expand Down Expand Up @@ -621,14 +622,14 @@ public static List<Setting<?>> getSettings(List<SecurityExtension> securityExten
}

@Override
public Collection<String> getRestHeaders() {
Set<String> headers = new HashSet<>();
headers.add(UsernamePasswordToken.BASIC_AUTH_HEADER);
public Collection<RestHeaderDefinition> getRestHeaders() {
Set<RestHeaderDefinition> headers = new HashSet<>();
headers.add(new RestHeaderDefinition(UsernamePasswordToken.BASIC_AUTH_HEADER, false));
bizybot marked this conversation as resolved.
Show resolved Hide resolved
if (XPackSettings.AUDIT_ENABLED.get(settings)) {
headers.add(AuditTrail.X_FORWARDED_FOR_HEADER);
headers.add(new RestHeaderDefinition(AuditTrail.X_FORWARDED_FOR_HEADER, true));
}
if (AuthenticationServiceField.RUN_AS_ENABLED.get(settings)) {
headers.add(AuthenticationServiceField.RUN_AS_USER_HEADER);
headers.add(new RestHeaderDefinition(AuthenticationServiceField.RUN_AS_USER_HEADER, false));
}
return headers;
}
Expand Down
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.example.realm.CustomRealm;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestHeaderDefinition;
import org.elasticsearch.xpack.core.security.authc.RealmSettings;

import java.util.ArrayList;
Expand All @@ -22,8 +23,10 @@
public class SpiExtensionPlugin extends Plugin implements ActionPlugin {

@Override
public Collection<String> getRestHeaders() {
return Arrays.asList(CustomRealm.USER_HEADER, CustomRealm.PW_HEADER);
public Collection<RestHeaderDefinition> getRestHeaders() {
return Arrays.asList(
new RestHeaderDefinition(CustomRealm.USER_HEADER, false),
new RestHeaderDefinition(CustomRealm.PW_HEADER, false));
}

@Override
Expand Down