|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package geoip; |
| 11 | + |
| 12 | +import fixture.geoip.GeoIpHttpFixture; |
| 13 | + |
| 14 | +import org.elasticsearch.client.Request; |
| 15 | +import org.elasticsearch.client.RequestOptions; |
| 16 | +import org.elasticsearch.core.Booleans; |
| 17 | +import org.elasticsearch.ingest.geoip.GeoIpDownloader; |
| 18 | +import org.elasticsearch.ingest.geoip.GeoIpDownloaderTaskExecutor; |
| 19 | +import org.elasticsearch.tasks.Task; |
| 20 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 21 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 22 | +import org.elasticsearch.test.rest.ObjectPath; |
| 23 | +import org.junit.ClassRule; |
| 24 | +import org.junit.rules.RuleChain; |
| 25 | +import org.junit.rules.TestRule; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | +import java.util.stream.Collectors; |
| 34 | + |
| 35 | +import static org.hamcrest.Matchers.equalTo; |
| 36 | + |
| 37 | +public class GeoIpMultiProjectIT extends ESRestTestCase { |
| 38 | + // default true |
| 39 | + private static final boolean useFixture = Booleans.parseBoolean(System.getProperty("geoip_use_service", "false")) == false; |
| 40 | + |
| 41 | + public static final GeoIpHttpFixture fixture = new GeoIpHttpFixture(useFixture); |
| 42 | + |
| 43 | + public static final ElasticsearchCluster cluster = ElasticsearchCluster.local() |
| 44 | + .module("ingest-geoip") |
| 45 | + .module("reindex") // for database cleanup |
| 46 | + .module("test-multi-project") |
| 47 | + .setting("test.multi_project.enabled", "true") |
| 48 | + .setting(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey(), "true") |
| 49 | + .setting(GeoIpDownloader.ENDPOINT_SETTING.getKey(), fixture::getAddress, (k) -> useFixture) |
| 50 | + .build(); |
| 51 | + |
| 52 | + @ClassRule |
| 53 | + public static TestRule ruleChain = RuleChain.outerRule(fixture).around(cluster); |
| 54 | + |
| 55 | + @Override |
| 56 | + protected String getTestRestCluster() { |
| 57 | + return cluster.getHttpAddresses(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected boolean shouldConfigureProjects() { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + public void testGeoIpDownloader() throws Exception { |
| 66 | + String project1 = randomUniqueProjectId().id(); |
| 67 | + String project2 = randomUniqueProjectId().id(); |
| 68 | + createProject(project1); |
| 69 | + createProject(project2); |
| 70 | + |
| 71 | + // download databases for project1 |
| 72 | + putGeoIpPipeline(project1); |
| 73 | + assertBusy(() -> assertDatabases(project1, true), 30, TimeUnit.SECONDS); |
| 74 | + assertBusy(() -> assertDatabases(project2, false), 30, TimeUnit.SECONDS); |
| 75 | + |
| 76 | + // download databases for project2 |
| 77 | + putGeoIpPipeline(project2); |
| 78 | + assertBusy(() -> assertDatabases(project2, true), 30, TimeUnit.SECONDS); |
| 79 | + } |
| 80 | + |
| 81 | + private void putGeoIpPipeline(String projectId) throws IOException { |
| 82 | + Request putPipelineRequest = new Request("PUT", "/_ingest/pipeline/geoip-pipeline"); |
| 83 | + putPipelineRequest.setJsonEntity(""" |
| 84 | + { |
| 85 | + "processors" : [ |
| 86 | + { |
| 87 | + "geoip" : { |
| 88 | + "field" : "ip", |
| 89 | + "target_field" : "geo", |
| 90 | + "database_file" : "GeoLite2-Country.mmdb" |
| 91 | + } |
| 92 | + } |
| 93 | + ] |
| 94 | + } |
| 95 | + """); |
| 96 | + setRequestProjectId(projectId, putPipelineRequest); |
| 97 | + assertOK(client().performRequest(putPipelineRequest)); |
| 98 | + } |
| 99 | + |
| 100 | + private static Request setRequestProjectId(String projectId, Request request) { |
| 101 | + RequestOptions.Builder options = request.getOptions().toBuilder(); |
| 102 | + options.removeHeader(Task.X_ELASTIC_PROJECT_ID_HTTP_HEADER); |
| 103 | + options.addHeader(Task.X_ELASTIC_PROJECT_ID_HTTP_HEADER, projectId); |
| 104 | + request.setOptions(options); |
| 105 | + return request; |
| 106 | + } |
| 107 | + |
| 108 | + @SuppressWarnings("unchecked") |
| 109 | + private void assertDatabases(String projectId, boolean shouldDownload) throws IOException { |
| 110 | + Request getTaskState = new Request("GET", "/_cluster/state"); |
| 111 | + setRequestProjectId(projectId, getTaskState); |
| 112 | + |
| 113 | + ObjectPath state = ObjectPath.createFromResponse(assertOK(client().performRequest(getTaskState))); |
| 114 | + |
| 115 | + List<Map<String, ?>> tasks = state.evaluate("metadata.persistent_tasks.tasks"); |
| 116 | + // Short-circuit to avoid using steams if the list is empty |
| 117 | + if (tasks.isEmpty()) { |
| 118 | + fail("persistent tasks list is empty, expected at least one task for geoip-downloader"); |
| 119 | + } |
| 120 | + |
| 121 | + // verify project task id |
| 122 | + Set<Map<String, ?>> id = tasks.stream() |
| 123 | + .filter(task -> String.format("%s/geoip-downloader", projectId).equals(task.get("id"))) |
| 124 | + .collect(Collectors.toSet()); |
| 125 | + assertThat(id.size(), equalTo(1)); |
| 126 | + |
| 127 | + // verify database download |
| 128 | + Map<String, Object> databases = (Map<String, Object>) tasks.stream().map(task -> { |
| 129 | + try { |
| 130 | + return ObjectPath.evaluate(task, "task.geoip-downloader.state.databases"); |
| 131 | + } catch (IOException e) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + }).filter(Objects::nonNull).findFirst().orElse(null); |
| 135 | + |
| 136 | + if (shouldDownload) { |
| 137 | + // verify database downloaded |
| 138 | + assertNotNull(databases); |
| 139 | + for (String name : List.of("GeoLite2-ASN.mmdb", "GeoLite2-City.mmdb", "GeoLite2-Country.mmdb")) { |
| 140 | + Object database = databases.get(name); |
| 141 | + assertNotNull(database); |
| 142 | + assertNotNull(ObjectPath.evaluate(database, "md5")); |
| 143 | + } |
| 144 | + } else { |
| 145 | + // verify database not downloaded |
| 146 | + assertNull(databases); |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | +} |
0 commit comments