Skip to content

Commit 356f227

Browse files
Reindex-from-remote: Validate basic auth params
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, which requires both username and password. 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).
1 parent 434cb5a commit 356f227

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,52 @@ 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(
207+
"Validation Failed: 1: reindex from remote source included username but not password;",
208+
e.getMessage()
209+
);
210+
}
211+
212+
public void testReindexFromRemoteRejectsPasswordWithNoUsername() {
213+
ReindexRequest reindex = newRequest();
214+
reindex.setRemoteInfo(
215+
new RemoteInfo(
216+
randomAlphaOfLength(5),
217+
randomAlphaOfLength(5),
218+
between(1, Integer.MAX_VALUE),
219+
null,
220+
matchAll,
221+
null,
222+
new SecureString("password".toCharArray()),
223+
emptyMap(),
224+
RemoteInfo.DEFAULT_SOCKET_TIMEOUT,
225+
RemoteInfo.DEFAULT_CONNECT_TIMEOUT
226+
)
227+
);
228+
ActionRequestValidationException e = reindex.validate();
229+
assertEquals(
230+
"Validation Failed: 1: reindex from remote source included password but not username;",
231+
e.getMessage()
232+
);
233+
}
234+
189235
public void testNoSliceBuilderSetWithSlicedRequest() {
190236
ReindexRequest reindex = newRequest();
191237
reindex.getSearchRequest().source().slice(new SliceBuilder(0, 4));

0 commit comments

Comments
 (0)