Skip to content

Commit 901a53a

Browse files
authored
Limit headers stored by reindexing to security (#49267)
This is related to #42612. It adds a setting to configure what headers are stored by the persistent reindexing task for further requests. Additionally, it has the x-pack security module automatically configure this setting to ensure security works with reindexing.
1 parent 8eaa108 commit 901a53a

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

modules/reindex/src/main/java/org/elasticsearch/index/reindex/TransportStartReindexTaskAction.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
3030
import org.elasticsearch.cluster.service.ClusterService;
3131
import org.elasticsearch.common.UUIDs;
32+
import org.elasticsearch.common.collect.Tuple;
3233
import org.elasticsearch.common.inject.Inject;
3334
import org.elasticsearch.common.settings.Settings;
35+
import org.elasticsearch.common.util.concurrent.ThreadContext;
3436
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3537
import org.elasticsearch.persistent.PersistentTasksCustomMetaData;
3638
import org.elasticsearch.persistent.PersistentTasksService;
@@ -39,11 +41,15 @@
3941
import org.elasticsearch.threadpool.ThreadPool;
4042
import org.elasticsearch.transport.TransportService;
4143

44+
import java.util.List;
45+
import java.util.Map;
4246
import java.util.function.Predicate;
47+
import java.util.stream.Collectors;
4348

4449
public class TransportStartReindexTaskAction
4550
extends HandledTransportAction<StartReindexTaskAction.Request, StartReindexTaskAction.Response> {
4651

52+
private final List<String> headersToInclude;
4753
private final ThreadPool threadPool;
4854
private final PersistentTasksService persistentTasksService;
4955
private final ReindexValidator reindexValidator;
@@ -55,6 +61,7 @@ public TransportStartReindexTaskAction(Settings settings, Client client, Transpo
5561
ClusterService clusterService, PersistentTasksService persistentTasksService,
5662
AutoCreateIndex autoCreateIndex, NamedXContentRegistry xContentRegistry) {
5763
super(StartReindexTaskAction.NAME, transportService, actionFilters, StartReindexTaskAction.Request::new);
64+
this.headersToInclude = ReindexHeaders.REINDEX_INCLUDED_HEADERS.get(settings);
5865
this.threadPool = threadPool;
5966
this.reindexValidator = new ReindexValidator(settings, clusterService, indexNameExpressionResolver, autoCreateIndex);
6067
this.persistentTasksService = persistentTasksService;
@@ -72,9 +79,15 @@ protected void doExecute(Task task, StartReindexTaskAction.Request request, Acti
7279

7380
String generatedId = UUIDs.randomBase64UUID();
7481

82+
ThreadContext threadContext = threadPool.getThreadContext();
83+
Map<String, String> included = headersToInclude.stream()
84+
.map(header -> new Tuple<>(header, threadContext.getHeader(header)))
85+
.filter(t -> t.v2() != null)
86+
.collect(Collectors.toMap(Tuple::v1, Tuple::v2));
87+
7588
// In the current implementation, we only need to store task results if we do not wait for completion
7689
boolean storeTaskResult = request.getWaitForCompletion() == false;
77-
ReindexTaskParams job = new ReindexTaskParams(storeTaskResult, threadPool.getThreadContext().getHeaders());
90+
ReindexTaskParams job = new ReindexTaskParams(storeTaskResult, included);
7891

7992
ReindexTaskStateDoc reindexState = new ReindexTaskStateDoc(request.getReindexRequest());
8093
reindexIndexClient.createReindexTaskDoc(generatedId, reindexState, new ActionListener<>() {

server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import org.elasticsearch.http.HttpTransportSettings;
7676
import org.elasticsearch.index.IndexModule;
7777
import org.elasticsearch.index.IndexSettings;
78+
import org.elasticsearch.index.reindex.ReindexHeaders;
7879
import org.elasticsearch.indices.IndexingMemoryController;
7980
import org.elasticsearch.indices.IndicesQueryCache;
8081
import org.elasticsearch.indices.IndicesRequestCache;
@@ -462,7 +463,8 @@ public void apply(Settings value, Settings current, Settings previous) {
462463
TransportAddVotingConfigExclusionsAction.MAXIMUM_VOTING_CONFIG_EXCLUSIONS_SETTING,
463464
ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING,
464465
ClusterBootstrapService.UNCONFIGURED_BOOTSTRAP_TIMEOUT_SETTING,
465-
LagDetector.CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING);
466+
LagDetector.CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING,
467+
ReindexHeaders.REINDEX_INCLUDED_HEADERS);
466468

467469
static List<SettingUpgrader<?>> BUILT_IN_SETTING_UPGRADERS = Collections.emptyList();
468470

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.reindex;
21+
22+
import org.elasticsearch.common.settings.Setting;
23+
24+
import java.util.Collections;
25+
import java.util.List;
26+
import java.util.function.Function;
27+
28+
public class ReindexHeaders {
29+
30+
/**
31+
* A list of headers that should be extracted from the start reindex request and used on subsequent
32+
* requests when resilient reindexing is enabled. For example, any authorization headers required for
33+
* reindexing should be configured.
34+
*/
35+
public static final Setting<List<String>> REINDEX_INCLUDED_HEADERS = Setting.listSetting("reindex.request_headers.include",
36+
Collections.emptyList(), Function.identity(), Setting.Property.NodeScope);
37+
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/Security.java

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.elasticsearch.env.NodeEnvironment;
4343
import org.elasticsearch.http.HttpServerTransport;
4444
import org.elasticsearch.index.IndexModule;
45+
import org.elasticsearch.index.reindex.ReindexHeaders;
4546
import org.elasticsearch.indices.breaker.CircuitBreakerService;
4647
import org.elasticsearch.ingest.Processor;
4748
import org.elasticsearch.license.License;
@@ -112,6 +113,7 @@
112113
import org.elasticsearch.xpack.core.security.action.user.PutUserAction;
113114
import org.elasticsearch.xpack.core.security.action.user.SetEnabledAction;
114115
import org.elasticsearch.xpack.core.security.authc.AuthenticationFailureHandler;
116+
import org.elasticsearch.xpack.core.security.authc.AuthenticationField;
115117
import org.elasticsearch.xpack.core.security.authc.AuthenticationServiceField;
116118
import org.elasticsearch.xpack.core.security.authc.DefaultAuthenticationFailureHandler;
117119
import org.elasticsearch.xpack.core.security.authc.InternalRealmsSettings;
@@ -568,6 +570,8 @@ static Settings additionalSettings(final Settings settings, final boolean enable
568570
SecurityHttpSettings.overrideSettings(builder, settings);
569571
}
570572
builder.put(SecuritySettings.addUserSettings(settings));
573+
List<String> authHeaders = Arrays.asList(AuthenticationField.AUTHENTICATION_KEY, AuthenticationServiceField.RUN_AS_USER_HEADER);
574+
builder.put(ReindexHeaders.REINDEX_INCLUDED_HEADERS.getKey(), Strings.collectionToCommaDelimitedString(authHeaders));
571575
return builder.build();
572576
} else {
573577
return Settings.EMPTY;

0 commit comments

Comments
 (0)