|
| 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 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.ingest.geoip; |
| 10 | + |
| 11 | +import com.maxmind.geoip2.DatabaseReader; |
| 12 | +import org.apache.lucene.search.TotalHits; |
| 13 | +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; |
| 14 | +import org.elasticsearch.action.search.SearchResponse; |
| 15 | +import org.elasticsearch.common.SuppressForbidden; |
| 16 | +import org.elasticsearch.common.settings.Settings; |
| 17 | +import org.elasticsearch.index.query.BoolQueryBuilder; |
| 18 | +import org.elasticsearch.index.query.MatchQueryBuilder; |
| 19 | +import org.elasticsearch.index.query.RangeQueryBuilder; |
| 20 | +import org.elasticsearch.index.reindex.ReindexPlugin; |
| 21 | +import org.elasticsearch.persistent.PersistentTaskParams; |
| 22 | +import org.elasticsearch.persistent.PersistentTasksCustomMetadata; |
| 23 | +import org.elasticsearch.plugins.Plugin; |
| 24 | +import org.elasticsearch.search.SearchHit; |
| 25 | +import org.elasticsearch.search.sort.SortOrder; |
| 26 | +import org.elasticsearch.test.ESIntegTestCase.ClusterScope; |
| 27 | +import org.elasticsearch.test.ESIntegTestCase.Scope; |
| 28 | + |
| 29 | +import java.io.BufferedOutputStream; |
| 30 | +import java.io.ByteArrayInputStream; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.InputStream; |
| 33 | +import java.io.OutputStream; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Collection; |
| 39 | +import java.util.Iterator; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Set; |
| 42 | +import java.util.concurrent.TimeUnit; |
| 43 | +import java.util.zip.GZIPInputStream; |
| 44 | + |
| 45 | +import static java.nio.file.StandardOpenOption.CREATE; |
| 46 | +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; |
| 47 | +import static java.nio.file.StandardOpenOption.WRITE; |
| 48 | + |
| 49 | +@ClusterScope(scope = Scope.TEST, maxNumDataNodes = 1) |
| 50 | +public class GeoIpDownloaderIT extends AbstractGeoIpIT { |
| 51 | + |
| 52 | + @Override |
| 53 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 54 | + return Arrays.asList(ReindexPlugin.class, IngestGeoIpPlugin.class, GeoIpProcessorNonIngestNodeIT.IngestGeoIpSettingsPlugin.class); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 59 | + Settings.Builder settings = Settings.builder() |
| 60 | + .put(super.nodeSettings(nodeOrdinal)) |
| 61 | + .put(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey(), false); |
| 62 | + String endpoint = System.getProperty("geoip_endpoint"); |
| 63 | + if (endpoint != null) { |
| 64 | + settings.put(GeoIpDownloader.ENDPOINT_SETTING.getKey(), endpoint); |
| 65 | + } |
| 66 | + return settings.build(); |
| 67 | + } |
| 68 | + |
| 69 | + public void testGeoIpDatabasesDownload() throws Exception { |
| 70 | + ClusterUpdateSettingsResponse settingsResponse = client().admin().cluster() |
| 71 | + .prepareUpdateSettings() |
| 72 | + .setPersistentSettings(Settings.builder().put(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey(), true)) |
| 73 | + .get(); |
| 74 | + assertTrue(settingsResponse.isAcknowledged()); |
| 75 | + assertBusy(() -> { |
| 76 | + PersistentTasksCustomMetadata.PersistentTask<PersistentTaskParams> task = getTask(); |
| 77 | + assertNotNull(task); |
| 78 | + GeoIpTaskState state = (GeoIpTaskState) task.getState(); |
| 79 | + assertNotNull(state); |
| 80 | + assertEquals(Set.of("GeoLite2-ASN.mmdb", "GeoLite2-City.mmdb", "GeoLite2-Country.mmdb"), state.getDatabases().keySet()); |
| 81 | + }, 2, TimeUnit.MINUTES); |
| 82 | + |
| 83 | + GeoIpTaskState state = (GeoIpTaskState) getTask().getState(); |
| 84 | + for (String id : List.of("GeoLite2-ASN.mmdb", "GeoLite2-City.mmdb", "GeoLite2-Country.mmdb")) { |
| 85 | + assertBusy(() -> { |
| 86 | + GeoIpTaskState.Metadata metadata = state.get(id); |
| 87 | + BoolQueryBuilder queryBuilder = new BoolQueryBuilder() |
| 88 | + .filter(new MatchQueryBuilder("name", id)) |
| 89 | + .filter(new RangeQueryBuilder("chunk") |
| 90 | + .from(metadata.getFirstChunk()) |
| 91 | + .to(metadata.getLastChunk(), true)); |
| 92 | + int size = metadata.getLastChunk() - metadata.getFirstChunk() + 1; |
| 93 | + SearchResponse res = client().prepareSearch(GeoIpDownloader.DATABASES_INDEX) |
| 94 | + .setSize(size) |
| 95 | + .setQuery(queryBuilder) |
| 96 | + .addSort("chunk", SortOrder.ASC) |
| 97 | + .get(); |
| 98 | + TotalHits totalHits = res.getHits().getTotalHits(); |
| 99 | + assertEquals(TotalHits.Relation.EQUAL_TO, totalHits.relation); |
| 100 | + assertEquals(size, totalHits.value); |
| 101 | + assertEquals(size, res.getHits().getHits().length); |
| 102 | + |
| 103 | + List<byte[]> data = new ArrayList<>(); |
| 104 | + |
| 105 | + for (SearchHit hit : res.getHits().getHits()) { |
| 106 | + data.add((byte[]) hit.getSourceAsMap().get("data")); |
| 107 | + } |
| 108 | + |
| 109 | + GZIPInputStream stream = new GZIPInputStream(new MultiByteArrayInputStream(data)); |
| 110 | + Path tempFile = createTempFile(); |
| 111 | + try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tempFile, TRUNCATE_EXISTING, WRITE, CREATE))) { |
| 112 | + stream.transferTo(os); |
| 113 | + } |
| 114 | + |
| 115 | + parseDatabase(tempFile); |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @SuppressForbidden(reason = "Maxmind API requires java.io.File") |
| 121 | + private void parseDatabase(Path tempFile) throws IOException { |
| 122 | + try (DatabaseReader databaseReader = new DatabaseReader.Builder(tempFile.toFile()).build()) { |
| 123 | + assertNotNull(databaseReader.getMetadata()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private PersistentTasksCustomMetadata.PersistentTask<PersistentTaskParams> getTask() { |
| 128 | + return PersistentTasksCustomMetadata.getTaskWithId(clusterService().state(), GeoIpDownloader.GEOIP_DOWNLOADER); |
| 129 | + } |
| 130 | + |
| 131 | + private static class MultiByteArrayInputStream extends InputStream { |
| 132 | + |
| 133 | + private final Iterator<byte[]> data; |
| 134 | + private ByteArrayInputStream current; |
| 135 | + |
| 136 | + private MultiByteArrayInputStream(List<byte[]> data) { |
| 137 | + this.data = data.iterator(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public int read() { |
| 142 | + if (current == null) { |
| 143 | + if (data.hasNext() == false) { |
| 144 | + return -1; |
| 145 | + } |
| 146 | + |
| 147 | + current = new ByteArrayInputStream(data.next()); |
| 148 | + } |
| 149 | + int read = current.read(); |
| 150 | + if (read == -1) { |
| 151 | + current = null; |
| 152 | + return read(); |
| 153 | + } |
| 154 | + return read; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public int read(byte[] b, int off, int len) throws IOException { |
| 159 | + if (current == null) { |
| 160 | + if (data.hasNext() == false) { |
| 161 | + return -1; |
| 162 | + } |
| 163 | + |
| 164 | + current = new ByteArrayInputStream(data.next()); |
| 165 | + } |
| 166 | + int read = current.read(b, off, len); |
| 167 | + if (read == -1) { |
| 168 | + current = null; |
| 169 | + return read(b, off, len); |
| 170 | + } |
| 171 | + return read; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments