Skip to content

Commit 7af0d69

Browse files
jaymodekcm
authored andcommitted
Security: use default scroll keepalive (#33639)
Security previously hardcoded a default scroll keepalive of 10 seconds, but in some cases this is not enough time as there can be network issues or overloading of host machines. After this change, security will now use the default keepalive timeout, which is controllable using a setting and the default value is 5 minutes.
1 parent d4d253f commit 7af0d69

File tree

8 files changed

+21
-11
lines changed

8 files changed

+21
-11
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
*/
66
package org.elasticsearch.xpack.core.security;
77

8+
import org.apache.logging.log4j.LogManager;
9+
import org.apache.logging.log4j.Logger;
10+
import org.apache.logging.log4j.message.ParameterizedMessage;
811
import org.elasticsearch.action.ActionListener;
912
import org.elasticsearch.action.search.ClearScrollRequest;
1013
import org.elasticsearch.action.search.SearchRequest;
1114
import org.elasticsearch.action.search.SearchResponse;
1215
import org.elasticsearch.action.search.SearchScrollRequest;
1316
import org.elasticsearch.action.support.ContextPreservingActionListener;
1417
import org.elasticsearch.client.Client;
15-
import org.elasticsearch.common.unit.TimeValue;
1618
import org.elasticsearch.index.IndexNotFoundException;
1719
import org.elasticsearch.search.SearchHit;
1820

@@ -25,6 +27,7 @@
2527

2628
public final class ScrollHelper {
2729

30+
private static final Logger LOGGER = LogManager.getLogger(ScrollHelper.class);
2831
private ScrollHelper() {}
2932

3033
/**
@@ -35,13 +38,15 @@ public static <T> void fetchAllByEntity(Client client, SearchRequest request, fi
3538
Function<SearchHit, T> hitParser) {
3639
final List<T> results = new ArrayList<>();
3740
if (request.scroll() == null) { // we do scroll by default lets see if we can get rid of this at some point.
38-
request.scroll(TimeValue.timeValueSeconds(10L));
41+
throw new IllegalArgumentException("request must have scroll set");
3942
}
4043
final Consumer<SearchResponse> clearScroll = (response) -> {
4144
if (response != null && response.getScrollId() != null) {
4245
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
4346
clearScrollRequest.addScrollId(response.getScrollId());
44-
client.clearScroll(clearScrollRequest, ActionListener.wrap((r) -> {}, (e) -> {}));
47+
client.clearScroll(clearScrollRequest, ActionListener.wrap((r) -> {}, e ->
48+
LOGGER.warn(new ParameterizedMessage("clear scroll failed for scroll id [{}]", response.getScrollId()), e)
49+
));
4550
}
4651
};
4752
// This function is MADNESS! But it works, don't think about it too hard...

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118

119119
import static org.elasticsearch.action.support.TransportActions.isShardNotAvailableException;
120120
import static org.elasticsearch.gateway.GatewayService.STATE_NOT_RECOVERED_BLOCK;
121+
import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING;
121122
import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN;
122123
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
123124

@@ -846,7 +847,7 @@ public void findActiveTokensForRealm(String realmName, ActionListener<Collection
846847
);
847848

848849
final SearchRequest request = client.prepareSearch(SecurityIndexManager.SECURITY_INDEX_NAME)
849-
.setScroll(TimeValue.timeValueSeconds(10L))
850+
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
850851
.setQuery(boolQuery)
851852
.setVersion(false)
852853
.setSize(1000)

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/NativeUsersStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.common.component.AbstractComponent;
2929
import org.elasticsearch.common.settings.SecureString;
3030
import org.elasticsearch.common.settings.Settings;
31-
import org.elasticsearch.common.unit.TimeValue;
3231
import org.elasticsearch.common.util.concurrent.ThreadContext;
3332
import org.elasticsearch.common.xcontent.XContentType;
3433
import org.elasticsearch.index.IndexNotFoundException;
@@ -62,6 +61,7 @@
6261
import java.util.function.Consumer;
6362
import java.util.function.Supplier;
6463

64+
import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING;
6565
import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN;
6666
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
6767
import static org.elasticsearch.xpack.core.ClientHelper.stashWithOrigin;
@@ -139,7 +139,7 @@ public void getUsers(String[] userNames, final ActionListener<Collection<User>>
139139
final Supplier<ThreadContext.StoredContext> supplier = client.threadPool().getThreadContext().newRestorableContext(false);
140140
try (ThreadContext.StoredContext ignore = stashWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN)) {
141141
SearchRequest request = client.prepareSearch(SECURITY_INDEX_NAME)
142-
.setScroll(TimeValue.timeValueSeconds(10L))
142+
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
143143
.setQuery(query)
144144
.setSize(1000)
145145
.setFetchSource(true)

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/mapper/NativeRoleMappingStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.elasticsearch.common.bytes.BytesReference;
1717
import org.elasticsearch.common.component.AbstractComponent;
1818
import org.elasticsearch.common.settings.Settings;
19-
import org.elasticsearch.common.unit.TimeValue;
2019
import org.elasticsearch.common.util.concurrent.ThreadContext;
2120
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2221
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
@@ -56,6 +55,7 @@
5655
import static org.elasticsearch.action.DocWriteResponse.Result.CREATED;
5756
import static org.elasticsearch.action.DocWriteResponse.Result.DELETED;
5857
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
58+
import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING;
5959
import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN;
6060
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
6161
import static org.elasticsearch.xpack.core.ClientHelper.stashWithOrigin;
@@ -129,7 +129,7 @@ void loadMappings(ActionListener<List<ExpressionRoleMapping>> listener) {
129129
final Supplier<ThreadContext.StoredContext> supplier = client.threadPool().getThreadContext().newRestorableContext(false);
130130
try (ThreadContext.StoredContext ignore = stashWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN)) {
131131
SearchRequest request = client.prepareSearch(SECURITY_INDEX_NAME)
132-
.setScroll(TimeValue.timeValueSeconds(10L))
132+
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
133133
.setTypes(SECURITY_GENERIC_TYPE)
134134
.setQuery(query)
135135
.setSize(1000)

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.elasticsearch.common.component.AbstractComponent;
2525
import org.elasticsearch.common.io.stream.StreamInput;
2626
import org.elasticsearch.common.settings.Settings;
27-
import org.elasticsearch.common.unit.TimeValue;
2827
import org.elasticsearch.common.util.CollectionUtils;
2928
import org.elasticsearch.common.util.concurrent.ThreadContext;
3029
import org.elasticsearch.common.util.iterable.Iterables;
@@ -56,6 +55,7 @@
5655
import java.util.stream.Collectors;
5756

5857
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
58+
import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING;
5959
import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN;
6060
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
6161
import static org.elasticsearch.xpack.core.ClientHelper.stashWithOrigin;
@@ -115,7 +115,7 @@ public void getPrivileges(Collection<String> applications, Collection<String> na
115115
final Supplier<ThreadContext.StoredContext> supplier = client.threadPool().getThreadContext().newRestorableContext(false);
116116
try (ThreadContext.StoredContext ignore = stashWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN)) {
117117
SearchRequest request = client.prepareSearch(SECURITY_INDEX_NAME)
118-
.setScroll(TimeValue.timeValueSeconds(10L))
118+
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
119119
.setQuery(query)
120120
.setSize(1000)
121121
.setFetchSource(true)

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
6161
import static org.elasticsearch.index.query.QueryBuilders.existsQuery;
62+
import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING;
6263
import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN;
6364
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
6465
import static org.elasticsearch.xpack.core.ClientHelper.stashWithOrigin;
@@ -120,7 +121,7 @@ public void getRoleDescriptors(String[] names, final ActionListener<Collection<R
120121
final Supplier<ThreadContext.StoredContext> supplier = client.threadPool().getThreadContext().newRestorableContext(false);
121122
try (ThreadContext.StoredContext ignore = stashWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN)) {
122123
SearchRequest request = client.prepareSearch(SecurityIndexManager.SECURITY_INDEX_NAME)
123-
.setScroll(TimeValue.timeValueSeconds(10L))
124+
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
124125
.setQuery(query)
125126
.setSize(1000)
126127
.setFetchSource(true)

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/ScrollHelperIntegTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void testFetchAllByEntityWithBrokenScroll() {
7979
when(client.threadPool()).thenReturn(threadPool);
8080
when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY));
8181
SearchRequest request = new SearchRequest();
82+
request.scroll(TimeValue.timeValueHours(10L));
8283

8384
String scrollId = randomAlphaOfLength(5);
8485
SearchHit[] hits = new SearchHit[] {new SearchHit(1), new SearchHit(2)};

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/index/AuditTrailTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.client.Requests;
1616
import org.elasticsearch.client.ResponseException;
1717
import org.elasticsearch.common.settings.Settings;
18+
import org.elasticsearch.common.unit.TimeValue;
1819
import org.elasticsearch.index.query.QueryBuilders;
1920
import org.elasticsearch.search.SearchHit;
2021
import org.elasticsearch.test.SecurityIntegTestCase;
@@ -161,6 +162,7 @@ private Collection<Map<String, Object>> getAuditEvents() throws Exception {
161162
client.admin().indices().refresh(Requests.refreshRequest(indexName)).get();
162163

163164
final SearchRequest request = client.prepareSearch(indexName)
165+
.setScroll(TimeValue.timeValueMinutes(10L))
164166
.setTypes(IndexAuditTrail.DOC_TYPE)
165167
.setQuery(QueryBuilders.matchAllQuery())
166168
.setSize(1000)

0 commit comments

Comments
 (0)