Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,55 @@ public void testGetReindexDescriptionStripsRemoteInfoSensitiveFields() throws Ex
+ "][remote_src] to [dest]";
assertThat(body.get("description"), equalTo(expectedDescription));
}

public void testTaskDescriptionExcludesSensitiveFields() throws Exception {
Request indexRequest = new Request("POST", "/task_api_src/_doc");
indexRequest.addParameter("refresh", "true");
indexRequest.setJsonEntity("{\"field\": \"value\"}");
client().performRequest(indexRequest);

String remoteHost = getRemoteHost();

Request reindexRequest = new Request("POST", "/_reindex");
reindexRequest.addParameter("wait_for_completion", "false");
reindexRequest.setJsonEntity(String.format(java.util.Locale.ROOT, """
{
"source": {
"remote": {
"host": "%s",
"username": "testuser",
"password": "testpass"
},
"index": "remote_src",
"query": {
"match_all": {}
}
},
"dest": {
"index": "dest"
}
}""", remoteHost));

Response reindexResponse = client().performRequest(reindexRequest);
String taskId = (String) entityAsMap(reindexResponse).get("task");
assertNotNull("reindex did not return a task id", taskId);

Request getTaskRequest = new Request("GET", "/_tasks/" + taskId);
getTaskRequest.addParameter("wait_for_completion", "true");
getTaskRequest.addParameter("timeout", "30s");
Response taskResponse = client().performRequest(getTaskRequest);
Map<String, Object> body = entityAsMap(taskResponse);

@SuppressWarnings("unchecked")
Map<String, Object> task = (Map<String, Object>) body.get("task");
String description = (String) task.get("description");

URI remoteUri = URI.create(remoteHost);
String expectedDescription = "reindex from [host="
+ remoteUri.getHost()
+ " port="
+ remoteUri.getPort()
+ "][remote_src] to [dest]";
assertThat(description, equalTo(expectedDescription));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ public class GetReindexResponse extends ActionResponse implements ToXContentObje
* group(2) = source indices
* group(3) = destination index
*/
private static final Pattern DESCRIPTION_PATTERN = Pattern.compile(
"(?s)^reindex from (?:\\[((?:scheme=\\S+ )?host=\\S+ port=\\d+(?:\\s+pathPrefix=\\S+)?) .+\\])?\\[([^\\]]*)].*to \\[([^\\]]*)]$"
);
private static final Pattern DESCRIPTION_PATTERN = Pattern.compile("(?s)^reindex from " +
// group(1): optional remote info
"(?:\\[((?:scheme=\\S+ )?host=\\S+ port=\\d+(?:\\s+pathPrefix=\\S+)?)(?: .+)?\\])?" +
// group(2): source indices
"\\[([^\\]]*)].*" +
// group(3): destination index
"to \\[([^\\]]*)]$");

public GetReindexResponse(final RelocatableReindexResult result) {
this.result = Objects.requireNonNull(result, "result is required");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,31 @@ public void testSanitizeDescriptionRemoteWithScript() {
equalTo(Optional.of("reindex from [host=example.com port=9200][source] to [dest]"))
);
}

public void testSanitizeDescriptionWithOnlyHostPort() {
assertThat(
sanitizeDescription("reindex from [host=example.com port=9200][source] to [dest]"),
equalTo(Optional.of("reindex from [host=example.com port=9200][source] to [dest]"))
);
}

public void testSanitizeDescriptionWithSchemeHostPortPathPrefix() {
assertThat(
sanitizeDescription("reindex from [scheme=https host=example.com port=9200 pathPrefix=/es][source] to [dest]"),
equalTo(Optional.of("reindex from [scheme=https host=example.com port=9200 pathPrefix=/es][source] to [dest]"))
);
}

public void testSanitizeDescriptionWithHostPortScript() {
assertThat(
sanitizeDescription(
"reindex from [host=example.com port=9200][source]"
+ " updated with Script{type=inline, lang='painless',"
+ " idOrCode='ctx._source.tag = 'host=localhost port=9200 username=admin password=secret'',"
+ " options={}, params={}}"
+ " to [dest]"
),
equalTo(Optional.of("reindex from [host=example.com port=9200][source] to [dest]"))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,12 @@ private RemoteInfo newRemoteInfo(String scheme, String prefixPath, String userna
}

public void testToString() {
assertEquals("host=testhost port=12344 query={ \"foo\" : \"bar\" }", newRemoteInfo("http", null, null, null).toString());
assertEquals("host=testhost port=12344", newRemoteInfo("http", null, null, null).toString());
assertEquals("host=testhost port=12344", newRemoteInfo("http", null, "testuser", null).toString());
assertEquals("host=testhost port=12344", newRemoteInfo("http", null, "testuser", "testpass").toString());
assertEquals("scheme=https host=testhost port=12344", newRemoteInfo("https", null, "testuser", "testpass").toString());
assertEquals(
"host=testhost port=12344 query={ \"foo\" : \"bar\" } username=testuser",
newRemoteInfo("http", null, "testuser", null).toString()
);
assertEquals(
"host=testhost port=12344 query={ \"foo\" : \"bar\" } username=testuser password=<<>>",
newRemoteInfo("http", null, "testuser", "testpass").toString()
);
assertEquals(
"scheme=https host=testhost port=12344 query={ \"foo\" : \"bar\" } username=testuser password=<<>>",
newRemoteInfo("https", null, "testuser", "testpass").toString()
);
assertEquals(
"scheme=https host=testhost port=12344 pathPrefix=prxy query={ \"foo\" : \"bar\" } username=testuser password=<<>>",
"scheme=https host=testhost port=12344 pathPrefix=prxy",
newRemoteInfo("https", "prxy", "testuser", "testpass").toString()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,6 @@ public String toString() {
if (pathPrefix != null) {
b.append(" pathPrefix=").append(pathPrefix);
}
b.append(" query=").append(query.utf8ToString());
if (username != null) {
b.append(" username=").append(username);
}
if (password != null) {
b.append(" password=<<>>");
}
return b.toString();
}

Expand Down
Loading