Skip to content

Commit 9d204ed

Browse files
Reindex-from-remote: Validate basic auth params (#136501) (#136552)
This fixes a bug in the reindex API where it did not correctly validate the request parameters for authenticating with a remote source using basic auth. These require both username and password, and providing only one or the other is a user error. 1. Prior to this change, a reindex request which set `source.remote.username` but not `source.remote.password` would result in a response with HTTP status code 500 (Internal Server Error). This will now result in a response with HTTP status code 400 (Bad Request). 2. Prior to this change, a reindex request which set `source.remote.password` but not `source.remote.username` would normally result in a response with HTTP status code 401 (Unuauthorized). (If the remote cluster does not require authentication, or if an API key or some other form of authentication is provided, the request would succeed, with the password silently ignored.) This will now result in a response with HTTP status code 400 (Bad Request). The new behaviour more correctly indicates to the user that there is an error in their request. Closes #135925
1 parent 98e02b2 commit 9d204ed

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

docs/changelog/136501.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 136501
2+
summary: "Reindex-from-remote: Validate basic auth params"
3+
area: Indices APIs
4+
type: bug
5+
issues:
6+
- 135925

server/src/main/java/org/elasticsearch/index/reindex/ReindexRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ public ActionRequestValidationException validate() {
116116
if (getSlices() == AbstractBulkByScrollRequest.AUTO_SLICES || getSlices() > 1) {
117117
e = addValidationError("reindex from remote sources doesn't support slices > 1 but was [" + getSlices() + "]", e);
118118
}
119+
if (getRemoteInfo().getUsername() != null && getRemoteInfo().getPassword() == null) {
120+
e = addValidationError("reindex from remote source included username but not password", e);
121+
}
122+
if (getRemoteInfo().getPassword() != null && getRemoteInfo().getUsername() == null) {
123+
e = addValidationError("reindex from remote source included password but not username", e);
124+
}
119125
}
120126
return e;
121127
}

server/src/test/java/org/elasticsearch/index/reindex/ReindexRequestTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,46 @@ public void testReindexFromRemoteDoesNotSupportSlices() {
186186
);
187187
}
188188

189+
public void testReindexFromRemoteRejectsUsernameWithNoPassword() {
190+
ReindexRequest reindex = newRequest();
191+
reindex.setRemoteInfo(
192+
new RemoteInfo(
193+
randomAlphaOfLength(5),
194+
randomAlphaOfLength(5),
195+
between(1, Integer.MAX_VALUE),
196+
null,
197+
matchAll,
198+
"user",
199+
null,
200+
emptyMap(),
201+
RemoteInfo.DEFAULT_SOCKET_TIMEOUT,
202+
RemoteInfo.DEFAULT_CONNECT_TIMEOUT
203+
)
204+
);
205+
ActionRequestValidationException e = reindex.validate();
206+
assertEquals("Validation Failed: 1: reindex from remote source included username but not password;", e.getMessage());
207+
}
208+
209+
public void testReindexFromRemoteRejectsPasswordWithNoUsername() {
210+
ReindexRequest reindex = newRequest();
211+
reindex.setRemoteInfo(
212+
new RemoteInfo(
213+
randomAlphaOfLength(5),
214+
randomAlphaOfLength(5),
215+
between(1, Integer.MAX_VALUE),
216+
null,
217+
matchAll,
218+
null,
219+
new SecureString("password".toCharArray()),
220+
emptyMap(),
221+
RemoteInfo.DEFAULT_SOCKET_TIMEOUT,
222+
RemoteInfo.DEFAULT_CONNECT_TIMEOUT
223+
)
224+
);
225+
ActionRequestValidationException e = reindex.validate();
226+
assertEquals("Validation Failed: 1: reindex from remote source included password but not username;", e.getMessage());
227+
}
228+
189229
public void testNoSliceBuilderSetWithSlicedRequest() {
190230
ReindexRequest reindex = newRequest();
191231
reindex.getSearchRequest().source().slice(new SliceBuilder(0, 4));

0 commit comments

Comments
 (0)