Skip to content

Commit 8962a49

Browse files
authored
Register IngestGeoIpMetadata as a NamedXContent (elastic#123079) (elastic#123326)
1 parent 2137a66 commit 8962a49

File tree

7 files changed

+342
-215
lines changed

7 files changed

+342
-215
lines changed

docs/changelog/123079.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 123079
2+
summary: Register `IngestGeoIpMetadata` as a NamedXContent
3+
area: Ingest Node
4+
type: bug
5+
issues: []

modules/ingest-geoip/qa/full-cluster-restart/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies {
1818
javaRestTestImplementation(testArtifact(project(":qa:full-cluster-restart"), "javaRestTest"))
1919
}
2020

21-
buildParams.bwcVersions.withWireCompatible(v -> v.before("8.0.0")) { bwcVersion, baseName ->
21+
buildParams.bwcVersions.withWireCompatible(v -> v.onOrAfter("8.15.0")) { bwcVersion, baseName ->
2222
tasks.register(bwcTaskName(bwcVersion), StandaloneRestIntegTestTask) {
2323
usesBwcDistribution(bwcVersion)
2424
systemProperty("tests.old_cluster_version", bwcVersion)

modules/ingest-geoip/qa/full-cluster-restart/src/javaRestTest/java/org/elasticsearch/ingest/geoip/FullClusterRestartIT.java

Lines changed: 26 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,24 @@
1212

1313
import com.carrotsearch.randomizedtesting.annotations.Name;
1414

15-
import org.apache.http.util.EntityUtils;
1615
import org.elasticsearch.client.Request;
17-
import org.elasticsearch.client.RequestOptions;
1816
import org.elasticsearch.client.Response;
19-
import org.elasticsearch.client.WarningsHandler;
20-
import org.elasticsearch.common.settings.Settings;
21-
import org.elasticsearch.common.util.concurrent.ThreadContext;
22-
import org.elasticsearch.core.Nullable;
23-
import org.elasticsearch.rest.RestStatus;
2417
import org.elasticsearch.test.cluster.ElasticsearchCluster;
2518
import org.elasticsearch.test.cluster.FeatureFlag;
2619
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
27-
import org.elasticsearch.test.rest.ObjectPath;
2820
import org.elasticsearch.upgrades.FullClusterRestartUpgradeStatus;
2921
import org.elasticsearch.upgrades.ParameterizedFullClusterRestartTestCase;
3022
import org.junit.ClassRule;
3123
import org.junit.rules.RuleChain;
3224
import org.junit.rules.TestRule;
3325

34-
import java.io.IOException;
35-
import java.nio.charset.StandardCharsets;
36-
import java.util.ArrayList;
37-
import java.util.Base64;
38-
import java.util.HashSet;
3926
import java.util.List;
4027
import java.util.Map;
41-
import java.util.Objects;
28+
import java.util.Set;
4229
import java.util.concurrent.TimeUnit;
4330

31+
import static org.hamcrest.Matchers.equalTo;
32+
import static org.hamcrest.Matchers.hasSize;
4433
import static org.hamcrest.Matchers.is;
4534

4635
public class FullClusterRestartIT extends ParameterizedFullClusterRestartTestCase {
@@ -49,29 +38,16 @@ public class FullClusterRestartIT extends ParameterizedFullClusterRestartTestCas
4938

5039
private static final GeoIpHttpFixture fixture = new GeoIpHttpFixture(useFixture);
5140

52-
// e.g. use ./gradlew -Dtests.jvm.argline="-Dgeoip_test_with_security=false" ":modules:ingest-geoip:qa:full-cluster-restart:check"
53-
// to set this to false, if you so desire
54-
private static final boolean useSecurity = Boolean.parseBoolean(System.getProperty("geoip_test_with_security", "true"));
55-
5641
private static final ElasticsearchCluster cluster = ElasticsearchCluster.local()
5742
.distribution(DistributionType.DEFAULT)
5843
.version(getOldClusterTestVersion())
5944
.nodes(2)
6045
.setting("ingest.geoip.downloader.endpoint", () -> fixture.getAddress(), s -> useFixture)
61-
.setting("xpack.security.enabled", useSecurity ? "true" : "false")
46+
.setting("xpack.security.enabled", "false")
47+
// .setting("logger.org.elasticsearch.ingest.geoip", "TRACE")
6248
.feature(FeatureFlag.TIME_SERIES_MODE)
6349
.build();
6450

65-
@Override
66-
protected Settings restClientSettings() {
67-
Settings settings = super.restClientSettings();
68-
if (useSecurity) {
69-
String token = "Basic " + Base64.getEncoder().encodeToString("test_user:x-pack-test-password".getBytes(StandardCharsets.UTF_8));
70-
settings = Settings.builder().put(settings).put(ThreadContext.PREFIX + ".Authorization", token).build();
71-
}
72-
return settings;
73-
}
74-
7551
@ClassRule
7652
public static TestRule ruleChain = RuleChain.outerRule(fixture).around(cluster);
7753

@@ -84,195 +60,32 @@ protected ElasticsearchCluster getUpgradeCluster() {
8460
return cluster;
8561
}
8662

87-
public void testGeoIpSystemFeaturesMigration() throws Exception {
88-
final List<String> maybeSecurityIndex = useSecurity ? List.of(".security-7") : List.of();
89-
63+
@SuppressWarnings("unchecked")
64+
public void testGeoIpDatabaseConfigurations() throws Exception {
9065
if (isRunningAgainstOldCluster()) {
91-
Request enableDownloader = new Request("PUT", "/_cluster/settings");
92-
enableDownloader.setJsonEntity("""
93-
{"persistent": {"ingest.geoip.downloader.enabled": true}}
94-
""");
95-
assertOK(client().performRequest(enableDownloader));
96-
97-
Request putPipeline = new Request("PUT", "/_ingest/pipeline/geoip");
98-
putPipeline.setJsonEntity("""
66+
Request putConfiguration = new Request("PUT", "_ingest/ip_location/database/my-database-1");
67+
putConfiguration.setJsonEntity("""
9968
{
100-
"description": "Add geoip info",
101-
"processors": [{
102-
"geoip": {
103-
"field": "ip",
104-
"target_field": "geo",
105-
"database_file": "GeoLite2-Country.mmdb"
106-
}
107-
}]
69+
"name": "GeoIP2-Domain",
70+
"maxmind": {
71+
"account_id": "1234567"
72+
}
10873
}
10974
""");
110-
assertOK(client().performRequest(putPipeline));
111-
112-
// wait for the geo databases to all be loaded
113-
assertBusy(() -> testDatabasesLoaded(), 30, TimeUnit.SECONDS);
114-
115-
// the geoip index should be created
116-
assertBusy(() -> testCatIndices(List.of(".geoip_databases"), List.of()));
117-
assertBusy(() -> testIndexGeoDoc());
118-
119-
// before the upgrade, Kibana should work
120-
assertBusy(() -> testGetStarAsKibana(List.of("my-index-00001"), List.of()));
121-
122-
// as should a normal get *
123-
assertBusy(() -> testGetStar(List.of("my-index-00001"), List.of()));
124-
125-
// and getting data streams
126-
assertBusy(() -> testGetDatastreams());
127-
} else {
128-
// after the upgrade, but before the migration, Kibana should work
129-
assertBusy(() -> testGetStarAsKibana(List.of("my-index-00001"), maybeSecurityIndex));
130-
131-
// as should a normal get *
132-
assertBusy(() -> testGetStar(List.of("my-index-00001"), maybeSecurityIndex));
133-
134-
// and getting data streams
135-
assertBusy(() -> testGetDatastreams());
136-
137-
// migrate the system features and give the cluster a moment to settle
138-
Request migrateSystemFeatures = new Request("POST", "/_migration/system_features");
139-
assertOK(client().performRequest(migrateSystemFeatures));
140-
ensureHealth(request -> request.addParameter("wait_for_status", "yellow"));
141-
142-
assertBusy(() -> testCatIndices(List.of(".geoip_databases-reindexed-for-9", "my-index-00001"), maybeSecurityIndex));
143-
assertBusy(() -> testIndexGeoDoc());
144-
145-
// after the migration, Kibana should work
146-
assertBusy(() -> testGetStarAsKibana(List.of("my-index-00001"), maybeSecurityIndex));
147-
148-
// as should a normal get *
149-
assertBusy(() -> testGetStar(List.of("my-index-00001"), maybeSecurityIndex));
150-
151-
// and getting data streams
152-
assertBusy(() -> testGetDatastreams());
153-
154-
Request disableDownloader = new Request("PUT", "/_cluster/settings");
155-
disableDownloader.setJsonEntity("""
156-
{"persistent": {"ingest.geoip.downloader.enabled": false}}
157-
""");
158-
assertOK(client().performRequest(disableDownloader));
159-
160-
// the geoip index should be deleted
161-
assertBusy(() -> testCatIndices(List.of("my-index-00001"), maybeSecurityIndex));
162-
163-
Request enableDownloader = new Request("PUT", "/_cluster/settings");
164-
enableDownloader.setJsonEntity("""
165-
{"persistent": {"ingest.geoip.downloader.enabled": true}}
166-
""");
167-
assertOK(client().performRequest(enableDownloader));
168-
169-
// wait for the geo databases to all be loaded
170-
assertBusy(() -> testDatabasesLoaded(), 30, TimeUnit.SECONDS);
171-
172-
// the geoip index should be recreated
173-
assertBusy(() -> testCatIndices(List.of(".geoip_databases", "my-index-00001"), maybeSecurityIndex));
174-
assertBusy(() -> testIndexGeoDoc());
75+
assertOK(client().performRequest(putConfiguration));
17576
}
176-
}
177-
178-
@SuppressWarnings("unchecked")
179-
private void testDatabasesLoaded() throws IOException {
180-
Request getTaskState = new Request("GET", "/_cluster/state");
181-
ObjectPath state = ObjectPath.createFromResponse(assertOK(client().performRequest(getTaskState)));
182-
183-
List<?> tasks = state.evaluate("metadata.persistent_tasks.tasks");
184-
// Short-circuit to avoid using steams if the list is empty
185-
if (tasks.isEmpty()) {
186-
fail();
187-
}
188-
Map<String, Object> databases = (Map<String, Object>) tasks.stream().map(task -> {
189-
try {
190-
return ObjectPath.evaluate(task, "task.geoip-downloader.state.databases");
191-
} catch (IOException e) {
192-
return null;
193-
}
194-
}).filter(Objects::nonNull).findFirst().orElse(null);
195-
196-
assertNotNull(databases);
197-
198-
for (String name : List.of("GeoLite2-ASN.mmdb", "GeoLite2-City.mmdb", "GeoLite2-Country.mmdb")) {
199-
Object database = databases.get(name);
200-
assertNotNull(database);
201-
assertNotNull(ObjectPath.evaluate(database, "md5"));
202-
}
203-
}
204-
205-
private void testCatIndices(List<String> indexNames, @Nullable List<String> additionalIndexNames) throws IOException {
206-
Request catIndices = new Request("GET", "_cat/indices/*?s=index&h=index&expand_wildcards=all");
207-
// the cat APIs can sometimes 404, erroneously
208-
// see https://github.com/elastic/elasticsearch/issues/104371
209-
setIgnoredErrorResponseCodes(catIndices, RestStatus.NOT_FOUND);
210-
String response = EntityUtils.toString(assertOK(client().performRequest(catIndices)).getEntity());
211-
List<String> indices = List.of(response.trim().split("\\s+"));
212-
213-
if (additionalIndexNames != null && additionalIndexNames.isEmpty() == false) {
214-
indexNames = new ArrayList<>(indexNames); // recopy into a mutable list
215-
indexNames.addAll(additionalIndexNames);
216-
}
217-
218-
assertThat(new HashSet<>(indices), is(new HashSet<>(indexNames)));
219-
}
220-
221-
private void testIndexGeoDoc() throws IOException {
222-
Request putDoc = new Request("PUT", "/my-index-00001/_doc/my_id?pipeline=geoip");
223-
putDoc.setJsonEntity("""
224-
{"ip": "89.160.20.128"}
225-
""");
226-
assertOK(client().performRequest(putDoc));
227-
228-
Request getDoc = new Request("GET", "/my-index-00001/_doc/my_id");
229-
ObjectPath doc = ObjectPath.createFromResponse(assertOK(client().performRequest(getDoc)));
230-
assertNull(doc.evaluate("_source.tags"));
231-
assertEquals("Sweden", doc.evaluate("_source.geo.country_name"));
232-
}
233-
234-
private void testGetStar(List<String> indexNames, @Nullable List<String> additionalIndexNames) throws IOException {
235-
Request getStar = new Request("GET", "*?expand_wildcards=all");
236-
getStar.setOptions(
237-
RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE) // we don't care about warnings, just errors
238-
);
239-
Response response = assertOK(client().performRequest(getStar));
240-
241-
if (additionalIndexNames != null && additionalIndexNames.isEmpty() == false) {
242-
indexNames = new ArrayList<>(indexNames); // recopy into a mutable list
243-
indexNames.addAll(additionalIndexNames);
244-
}
245-
246-
Map<String, Object> map = responseAsMap(response);
247-
assertThat(map.keySet(), is(new HashSet<>(indexNames)));
248-
}
249-
250-
private void testGetStarAsKibana(List<String> indexNames, @Nullable List<String> additionalIndexNames) throws IOException {
251-
Request getStar = new Request("GET", "*?expand_wildcards=all");
252-
getStar.setOptions(
253-
RequestOptions.DEFAULT.toBuilder()
254-
.addHeader("X-elastic-product-origin", "kibana")
255-
.setWarningsHandler(WarningsHandler.PERMISSIVE) // we don't care about warnings, just errors
256-
);
257-
Response response = assertOK(client().performRequest(getStar));
258-
259-
if (additionalIndexNames != null && additionalIndexNames.isEmpty() == false) {
260-
indexNames = new ArrayList<>(indexNames); // recopy into a mutable list
261-
indexNames.addAll(additionalIndexNames);
262-
}
263-
264-
Map<String, Object> map = responseAsMap(response);
265-
assertThat(map.keySet(), is(new HashSet<>(indexNames)));
266-
}
267-
268-
private void testGetDatastreams() throws IOException {
269-
Request getStar = new Request("GET", "_data_stream");
270-
getStar.setOptions(
271-
RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE) // we don't care about warnings, just errors
272-
);
273-
Response response = client().performRequest(getStar);
274-
assertOK(response);
27577

276-
// note: we don't actually care about the response, just that there was one and that it didn't error out on us
78+
assertBusy(() -> {
79+
Request getConfiguration = new Request("GET", "_ingest/ip_location/database/my-database-1");
80+
Response response = assertOK(client().performRequest(getConfiguration));
81+
Map<String, Object> map = responseAsMap(response);
82+
assertThat(map.keySet(), equalTo(Set.of("databases")));
83+
List<Map<String, Object>> databases = (List<Map<String, Object>>) map.get("databases");
84+
assertThat(databases, hasSize(1));
85+
Map<String, Object> database = databases.get(0);
86+
assertThat(database.get("id"), is("my-database-1"));
87+
assertThat(database.get("version"), is(1));
88+
assertThat(database.get("database"), equalTo(Map.of("name", "GeoIP2-Domain", "maxmind", Map.of("account_id", "1234567"))));
89+
}, 30, TimeUnit.SECONDS);
27790
}
27891
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
import org.elasticsearch.gradle.testclusters.StandaloneRestIntegTestTask
11+
12+
apply plugin: 'elasticsearch.internal-java-rest-test'
13+
apply plugin: 'elasticsearch.bwc-test'
14+
15+
16+
dependencies {
17+
javaRestTestImplementation project(':test:fixtures:geoip-fixture')
18+
javaRestTestImplementation(testArtifact(project(":qa:full-cluster-restart"), "javaRestTest"))
19+
}
20+
21+
buildParams.bwcVersions.withWireCompatible(v -> v.before("8.0.0")) { bwcVersion, baseName ->
22+
tasks.register(bwcTaskName(bwcVersion), StandaloneRestIntegTestTask) {
23+
usesBwcDistribution(bwcVersion)
24+
systemProperty("tests.old_cluster_version", bwcVersion)
25+
}
26+
}

0 commit comments

Comments
 (0)