|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.action; |
| 9 | + |
| 10 | +import org.elasticsearch.action.ActionType; |
| 11 | +import org.elasticsearch.action.support.ActionFilters; |
| 12 | +import org.elasticsearch.action.support.TransportAction; |
| 13 | +import org.elasticsearch.client.internal.Client; |
| 14 | +import org.elasticsearch.client.internal.node.NodeClient; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.common.util.CollectionUtils; |
| 17 | +import org.elasticsearch.core.Tuple; |
| 18 | +import org.elasticsearch.ingest.common.IngestCommonPlugin; |
| 19 | +import org.elasticsearch.injection.guice.Inject; |
| 20 | +import org.elasticsearch.license.LicenseService; |
| 21 | +import org.elasticsearch.license.XPackLicenseState; |
| 22 | +import org.elasticsearch.plugins.Plugin; |
| 23 | +import org.elasticsearch.protocol.xpack.XPackInfoRequest; |
| 24 | +import org.elasticsearch.protocol.xpack.XPackInfoResponse; |
| 25 | +import org.elasticsearch.reindex.ReindexPlugin; |
| 26 | +import org.elasticsearch.test.AbstractMultiClustersTestCase; |
| 27 | +import org.elasticsearch.transport.TransportService; |
| 28 | +import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; |
| 29 | +import org.elasticsearch.xpack.core.XPackSettings; |
| 30 | +import org.elasticsearch.xpack.core.action.TransportXPackInfoAction; |
| 31 | +import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; |
| 32 | +import org.elasticsearch.xpack.core.action.XPackInfoFeatureResponse; |
| 33 | +import org.elasticsearch.xpack.core.enrich.EnrichPolicy; |
| 34 | +import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction; |
| 35 | +import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction; |
| 36 | +import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; |
| 37 | +import org.elasticsearch.xpack.enrich.EnrichPlugin; |
| 38 | +import org.elasticsearch.xpack.esql.EsqlTestUtils; |
| 39 | +import org.elasticsearch.xpack.esql.plan.logical.Enrich; |
| 40 | +import org.junit.After; |
| 41 | +import org.junit.Before; |
| 42 | + |
| 43 | +import java.nio.file.Path; |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.Collection; |
| 46 | +import java.util.Collections; |
| 47 | +import java.util.List; |
| 48 | +import java.util.Map; |
| 49 | +import java.util.Set; |
| 50 | +import java.util.concurrent.TimeUnit; |
| 51 | + |
| 52 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 53 | +import static org.hamcrest.Matchers.containsString; |
| 54 | + |
| 55 | +public abstract class AbstractEnrichBasedCrossClusterTestCase extends AbstractMultiClustersTestCase { |
| 56 | + |
| 57 | + public static String REMOTE_CLUSTER_1 = "c1"; |
| 58 | + public static String REMOTE_CLUSTER_2 = "c2"; |
| 59 | + |
| 60 | + /** |
| 61 | + * subclasses should override if they don't want enrich policies wiped after each test method run |
| 62 | + */ |
| 63 | + protected boolean tolerateErrorsWhenWipingEnrichPolicies() { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected List<String> remoteClusterAlias() { |
| 69 | + return List.of(REMOTE_CLUSTER_1, REMOTE_CLUSTER_2); |
| 70 | + } |
| 71 | + |
| 72 | + protected Collection<String> allClusters() { |
| 73 | + return CollectionUtils.appendToCopy(remoteClusterAlias(), LOCAL_CLUSTER); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected Collection<Class<? extends Plugin>> nodePlugins(String clusterAlias) { |
| 78 | + List<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins(clusterAlias)); |
| 79 | + plugins.add(CrossClustersEnrichIT.LocalStateEnrich.class); |
| 80 | + plugins.add(IngestCommonPlugin.class); |
| 81 | + plugins.add(ReindexPlugin.class); |
| 82 | + return plugins; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected Settings nodeSettings() { |
| 87 | + return Settings.builder().put(super.nodeSettings()).put(XPackSettings.SECURITY_ENABLED.getKey(), false).build(); |
| 88 | + } |
| 89 | + |
| 90 | + static final EnrichPolicy hostPolicy = new EnrichPolicy("match", null, List.of("hosts"), "ip", List.of("ip", "os")); |
| 91 | + static final EnrichPolicy vendorPolicy = new EnrichPolicy("match", null, List.of("vendors"), "os", List.of("os", "vendor")); |
| 92 | + |
| 93 | + @Before |
| 94 | + public void setupHostsEnrich() { |
| 95 | + // the hosts policy are identical on every node |
| 96 | + Map<String, String> allHosts = Map.of( |
| 97 | + "192.168.1.2", |
| 98 | + "Windows", |
| 99 | + "192.168.1.3", |
| 100 | + "MacOS", |
| 101 | + "192.168.1.4", |
| 102 | + "Linux", |
| 103 | + "192.168.1.5", |
| 104 | + "Android", |
| 105 | + "192.168.1.6", |
| 106 | + "iOS", |
| 107 | + "192.168.1.7", |
| 108 | + "Windows", |
| 109 | + "192.168.1.8", |
| 110 | + "MacOS", |
| 111 | + "192.168.1.9", |
| 112 | + "Linux", |
| 113 | + "192.168.1.10", |
| 114 | + "Linux", |
| 115 | + "192.168.1.11", |
| 116 | + "Windows" |
| 117 | + ); |
| 118 | + for (String cluster : allClusters()) { |
| 119 | + Client client = client(cluster); |
| 120 | + client.admin().indices().prepareCreate("hosts").setMapping("ip", "type=ip", "os", "type=keyword").get(); |
| 121 | + for (Map.Entry<String, String> h : allHosts.entrySet()) { |
| 122 | + client.prepareIndex("hosts").setSource("ip", h.getKey(), "os", h.getValue()).get(); |
| 123 | + } |
| 124 | + client.admin().indices().prepareRefresh("hosts").get(); |
| 125 | + client.execute(PutEnrichPolicyAction.INSTANCE, new PutEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, "hosts", hostPolicy)) |
| 126 | + .actionGet(); |
| 127 | + client.execute(ExecuteEnrichPolicyAction.INSTANCE, new ExecuteEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, "hosts")) |
| 128 | + .actionGet(); |
| 129 | + assertAcked(client.admin().indices().prepareDelete("hosts")); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Before |
| 134 | + public void setupVendorPolicy() { |
| 135 | + var localVendors = Map.of("Windows", "Microsoft", "MacOS", "Apple", "iOS", "Apple", "Android", "Samsung", "Linux", "Redhat"); |
| 136 | + var c1Vendors = Map.of("Windows", "Microsoft", "MacOS", "Apple", "iOS", "Apple", "Android", "Google", "Linux", "Suse"); |
| 137 | + var c2Vendors = Map.of("Windows", "Microsoft", "MacOS", "Apple", "iOS", "Apple", "Android", "Sony", "Linux", "Ubuntu"); |
| 138 | + var vendors = Map.of(LOCAL_CLUSTER, localVendors, REMOTE_CLUSTER_1, c1Vendors, REMOTE_CLUSTER_2, c2Vendors); |
| 139 | + for (Map.Entry<String, Map<String, String>> e : vendors.entrySet()) { |
| 140 | + Client client = client(e.getKey()); |
| 141 | + client.admin().indices().prepareCreate("vendors").setMapping("os", "type=keyword", "vendor", "type=keyword").get(); |
| 142 | + for (Map.Entry<String, String> v : e.getValue().entrySet()) { |
| 143 | + client.prepareIndex("vendors").setSource("os", v.getKey(), "vendor", v.getValue()).get(); |
| 144 | + } |
| 145 | + client.admin().indices().prepareRefresh("vendors").get(); |
| 146 | + client.execute(PutEnrichPolicyAction.INSTANCE, new PutEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, "vendors", vendorPolicy)) |
| 147 | + .actionGet(); |
| 148 | + client.execute(ExecuteEnrichPolicyAction.INSTANCE, new ExecuteEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, "vendors")) |
| 149 | + .actionGet(); |
| 150 | + assertAcked(client.admin().indices().prepareDelete("vendors")); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Before |
| 155 | + public void setupEventsIndices() { |
| 156 | + record Event(long timestamp, String user, String host) { |
| 157 | + |
| 158 | + } |
| 159 | + List<Event> e0 = List.of( |
| 160 | + new Event(1, "matthew", "192.168.1.3"), |
| 161 | + new Event(2, "simon", "192.168.1.5"), |
| 162 | + new Event(3, "park", "192.168.1.2"), |
| 163 | + new Event(4, "andrew", "192.168.1.7"), |
| 164 | + new Event(5, "simon", "192.168.1.20"), |
| 165 | + new Event(6, "kevin", "192.168.1.2"), |
| 166 | + new Event(7, "akio", "192.168.1.5"), |
| 167 | + new Event(8, "luke", "192.168.1.2"), |
| 168 | + new Event(9, "jack", "192.168.1.4") |
| 169 | + ); |
| 170 | + List<Event> e1 = List.of( |
| 171 | + new Event(1, "andres", "192.168.1.2"), |
| 172 | + new Event(2, "sergio", "192.168.1.6"), |
| 173 | + new Event(3, "kylian", "192.168.1.8"), |
| 174 | + new Event(4, "andrew", "192.168.1.9"), |
| 175 | + new Event(5, "jack", "192.168.1.3"), |
| 176 | + new Event(6, "kevin", "192.168.1.4"), |
| 177 | + new Event(7, "akio", "192.168.1.7"), |
| 178 | + new Event(8, "kevin", "192.168.1.21"), |
| 179 | + new Event(9, "andres", "192.168.1.8") |
| 180 | + ); |
| 181 | + List<Event> e2 = List.of( |
| 182 | + new Event(1, "park", "192.168.1.25"), |
| 183 | + new Event(2, "akio", "192.168.1.5"), |
| 184 | + new Event(3, "park", "192.168.1.2"), |
| 185 | + new Event(4, "kevin", "192.168.1.3") |
| 186 | + ); |
| 187 | + for (var c : Map.of(LOCAL_CLUSTER, e0, REMOTE_CLUSTER_1, e1, REMOTE_CLUSTER_2, e2).entrySet()) { |
| 188 | + Client client = client(c.getKey()); |
| 189 | + client.admin() |
| 190 | + .indices() |
| 191 | + .prepareCreate("events") |
| 192 | + .setMapping("timestamp", "type=long", "user", "type=keyword", "host", "type=ip") |
| 193 | + .get(); |
| 194 | + for (var e : c.getValue()) { |
| 195 | + client.prepareIndex("events").setSource("timestamp", e.timestamp, "user", e.user, "host", e.host).get(); |
| 196 | + } |
| 197 | + client.admin().indices().prepareRefresh("events").get(); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @After |
| 202 | + public void wipeEnrichPolicies() { |
| 203 | + for (String cluster : allClusters()) { |
| 204 | + cluster(cluster).wipe(Set.of()); |
| 205 | + for (String policy : List.of("hosts", "vendors")) { |
| 206 | + if (tolerateErrorsWhenWipingEnrichPolicies()) { |
| 207 | + try { |
| 208 | + client(cluster).execute( |
| 209 | + DeleteEnrichPolicyAction.INSTANCE, |
| 210 | + new DeleteEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, policy) |
| 211 | + ); |
| 212 | + } catch (Exception e) { |
| 213 | + assertThat(e.getMessage(), containsString("Cluster is already closed")); |
| 214 | + } |
| 215 | + |
| 216 | + } else { |
| 217 | + client(cluster).execute( |
| 218 | + DeleteEnrichPolicyAction.INSTANCE, |
| 219 | + new DeleteEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, policy) |
| 220 | + ); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + static String enrichHosts(Enrich.Mode mode) { |
| 227 | + return EsqlTestUtils.randomEnrichCommand("hosts", mode, hostPolicy.getMatchField(), hostPolicy.getEnrichFields()); |
| 228 | + } |
| 229 | + |
| 230 | + static String enrichVendors(Enrich.Mode mode) { |
| 231 | + return EsqlTestUtils.randomEnrichCommand("vendors", mode, vendorPolicy.getMatchField(), vendorPolicy.getEnrichFields()); |
| 232 | + } |
| 233 | + |
| 234 | + protected EsqlQueryResponse runQuery(String query, Boolean ccsMetadataInResponse) { |
| 235 | + EsqlQueryRequest request = EsqlQueryRequest.syncEsqlQueryRequest(); |
| 236 | + request.query(query); |
| 237 | + request.pragmas(AbstractEsqlIntegTestCase.randomPragmas()); |
| 238 | + if (randomBoolean()) { |
| 239 | + request.profile(true); |
| 240 | + } |
| 241 | + if (ccsMetadataInResponse != null) { |
| 242 | + request.includeCCSMetadata(ccsMetadataInResponse); |
| 243 | + } |
| 244 | + return client(LOCAL_CLUSTER).execute(EsqlQueryAction.INSTANCE, request).actionGet(30, TimeUnit.SECONDS); |
| 245 | + } |
| 246 | + |
| 247 | + public static Tuple<Boolean, Boolean> randomIncludeCCSMetadata() { |
| 248 | + return switch (randomIntBetween(1, 3)) { |
| 249 | + case 1 -> new Tuple<>(Boolean.TRUE, Boolean.TRUE); |
| 250 | + case 2 -> new Tuple<>(Boolean.FALSE, Boolean.FALSE); |
| 251 | + case 3 -> new Tuple<>(null, Boolean.FALSE); |
| 252 | + default -> throw new AssertionError("should not get here"); |
| 253 | + }; |
| 254 | + } |
| 255 | + |
| 256 | + public static class LocalStateEnrich extends LocalStateCompositeXPackPlugin { |
| 257 | + public LocalStateEnrich(final Settings settings, final Path configPath) throws Exception { |
| 258 | + super(settings, configPath); |
| 259 | + |
| 260 | + plugins.add(new EnrichPlugin(settings) { |
| 261 | + @Override |
| 262 | + protected XPackLicenseState getLicenseState() { |
| 263 | + return this.getLicenseState(); |
| 264 | + } |
| 265 | + }); |
| 266 | + } |
| 267 | + |
| 268 | + public static class EnrichTransportXPackInfoAction extends TransportXPackInfoAction { |
| 269 | + @Inject |
| 270 | + public EnrichTransportXPackInfoAction( |
| 271 | + TransportService transportService, |
| 272 | + ActionFilters actionFilters, |
| 273 | + LicenseService licenseService, |
| 274 | + NodeClient client |
| 275 | + ) { |
| 276 | + super(transportService, actionFilters, licenseService, client); |
| 277 | + } |
| 278 | + |
| 279 | + @Override |
| 280 | + protected List<ActionType<XPackInfoFeatureResponse>> infoActions() { |
| 281 | + return Collections.singletonList(XPackInfoFeatureAction.ENRICH); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + protected Class<? extends TransportAction<XPackInfoRequest, XPackInfoResponse>> getInfoAction() { |
| 287 | + return CrossClustersQueriesWithInvalidLicenseIT.LocalStateEnrich.EnrichTransportXPackInfoAction.class; |
| 288 | + } |
| 289 | + } |
| 290 | +} |
0 commit comments