|
@@ -9,33 +9,27 @@ package org.elasticsearch.xpack.transform.integration;
|
|
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
-import org.apache.logging.log4j.Level;
|
|
|
import org.elasticsearch.client.Request;
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
|
import org.elasticsearch.client.Response;
|
|
|
-import org.elasticsearch.client.ResponseException;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.settings.SecureString;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.util.concurrent.ThreadContext;
|
|
|
import org.elasticsearch.common.xcontent.support.XContentMapValues;
|
|
|
import org.elasticsearch.rest.RestStatus;
|
|
|
-import org.elasticsearch.test.rest.ESRestTestCase;
|
|
|
import org.elasticsearch.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.xpack.core.transform.TransformField;
|
|
|
import org.elasticsearch.xpack.core.transform.transforms.DestAlias;
|
|
|
import org.elasticsearch.xpack.core.transform.transforms.SettingsConfig;
|
|
|
-import org.elasticsearch.xpack.core.transform.transforms.persistence.TransformInternalIndexConstants;
|
|
|
+import org.elasticsearch.xpack.transform.integration.common.TransformCommonRestTestCase;
|
|
|
import org.junit.After;
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.time.Instant;
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
-import java.util.Locale;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -44,10 +38,9 @@ import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
|
|
|
-public abstract class TransformRestTestCase extends ESRestTestCase {
|
|
|
+public abstract class TransformRestTestCase extends TransformCommonRestTestCase {
|
|
|
|
|
|
protected static final String TEST_PASSWORD = "x-pack-test-password";
|
|
|
- private static final String SECONDARY_AUTH_KEY = "es-secondary-authorization";
|
|
|
protected static final SecureString TEST_PASSWORD_SECURE_STRING = new SecureString(TEST_PASSWORD.toCharArray());
|
|
|
private static final String BASIC_AUTH_VALUE_SUPER_USER = basicAuthHeaderValue("x_pack_rest_user", TEST_PASSWORD_SECURE_STRING);
|
|
|
|
|
@@ -538,7 +531,7 @@ public abstract class TransformRestTestCase extends ESRestTestCase {
|
|
|
|
|
|
RequestOptions.Builder options = request.getOptions().toBuilder();
|
|
|
if (authHeader != null) {
|
|
|
- options.addHeader("Authorization", authHeader);
|
|
|
+ options.addHeader(AUTH_KEY, authHeader);
|
|
|
}
|
|
|
if (secondaryAuthHeader != null) {
|
|
|
options.addHeader(SECONDARY_AUTH_KEY, secondaryAuthHeader);
|
|
@@ -563,10 +556,6 @@ public abstract class TransformRestTestCase extends ESRestTestCase {
|
|
|
}, 30, TimeUnit.SECONDS);
|
|
|
}
|
|
|
|
|
|
- void refreshIndex(String index) throws IOException {
|
|
|
- assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
|
|
|
- }
|
|
|
-
|
|
|
@SuppressWarnings("unchecked")
|
|
|
protected static List<Map<String, Object>> getTransforms(List<Map<String, String>> expectedErrors) throws IOException {
|
|
|
Request request = new Request("GET", getTransformEndpoint() + "_all");
|
|
@@ -688,73 +677,4 @@ public abstract class TransformRestTestCase extends ESRestTestCase {
|
|
|
int actual = (Integer) ((List<?>) XContentMapValues.extractValue(field, searchResult)).get(0);
|
|
|
assertEquals(expected, actual);
|
|
|
}
|
|
|
-
|
|
|
- protected static String getTransformEndpoint() {
|
|
|
- return TransformField.REST_BASE_PATH_TRANSFORMS;
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- private void logAudits() throws Exception {
|
|
|
- logger.info("writing audit messages to the log");
|
|
|
- Request searchRequest = new Request("GET", TransformInternalIndexConstants.AUDIT_INDEX + "/_search?ignore_unavailable=true");
|
|
|
- searchRequest.setJsonEntity("""
|
|
|
- {
|
|
|
- "size": 100,
|
|
|
- "sort": [ { "timestamp": { "order": "asc" } } ]
|
|
|
- }""");
|
|
|
-
|
|
|
- assertBusy(() -> {
|
|
|
- try {
|
|
|
- refreshIndex(TransformInternalIndexConstants.AUDIT_INDEX_PATTERN);
|
|
|
- Response searchResponse = client().performRequest(searchRequest);
|
|
|
-
|
|
|
- Map<String, Object> searchResult = entityAsMap(searchResponse);
|
|
|
- List<Map<String, Object>> searchHits = (List<Map<String, Object>>) XContentMapValues.extractValue(
|
|
|
- "hits.hits",
|
|
|
- searchResult
|
|
|
- );
|
|
|
-
|
|
|
- for (Map<String, Object> hit : searchHits) {
|
|
|
- Map<String, Object> source = (Map<String, Object>) XContentMapValues.extractValue("_source", hit);
|
|
|
- String level = (String) source.getOrDefault("level", "info");
|
|
|
- logger.log(
|
|
|
- Level.getLevel(level.toUpperCase(Locale.ROOT)),
|
|
|
- "Transform audit: [{}] [{}] [{}] [{}]",
|
|
|
- Instant.ofEpochMilli((long) source.getOrDefault("timestamp", 0)),
|
|
|
- source.getOrDefault("transform_id", "n/a"),
|
|
|
- source.getOrDefault("message", "n/a"),
|
|
|
- source.getOrDefault("node_name", "n/a")
|
|
|
- );
|
|
|
- }
|
|
|
- } catch (ResponseException e) {
|
|
|
- // see gh#54810, wrap temporary 503's as assertion error for retry
|
|
|
- if (e.getResponse().getStatusLine().getStatusCode() != 503) {
|
|
|
- throw e;
|
|
|
- }
|
|
|
- throw new AssertionError("Failed to retrieve audit logs", e);
|
|
|
- }
|
|
|
- }, 5, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- protected List<String> getTransformTasks() throws IOException {
|
|
|
- final Request tasksRequest = new Request("GET", "/_tasks");
|
|
|
- tasksRequest.addParameter("actions", TransformField.TASK_NAME + "*");
|
|
|
- Map<String, Object> tasksResponse = entityAsMap(client().performRequest(tasksRequest));
|
|
|
-
|
|
|
- Map<String, Object> nodes = (Map<String, Object>) tasksResponse.get("nodes");
|
|
|
- if (nodes == null) {
|
|
|
- return List.of();
|
|
|
- }
|
|
|
-
|
|
|
- List<String> foundTasks = new ArrayList<>();
|
|
|
- for (Map.Entry<String, Object> node : nodes.entrySet()) {
|
|
|
- Map<String, Object> nodeInfo = (Map<String, Object>) node.getValue();
|
|
|
- Map<String, Object> tasks = (Map<String, Object>) nodeInfo.get("tasks");
|
|
|
- if (tasks != null) {
|
|
|
- foundTasks.addAll(tasks.keySet());
|
|
|
- }
|
|
|
- }
|
|
|
- return foundTasks;
|
|
|
- }
|
|
|
}
|