Skip to content

Commit b40bdd3

Browse files
authored
Add network from MaxMind Geo ASN database (#61676)
This adds the network property from the MaxMind Geo ASN database. This enables analysis of IP data based on the subnets that MaxMind have previously identified for ASN networks. closes #60942
1 parent ce9e2fd commit b40bdd3

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

docs/reference/ingest/processors/geoip.asciidoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ and `location`. The fields actually added depend on what has been found and whic
4040
`country_iso_code`, `country_name` and `continent_name`. The fields actually added depend on what has been found and which properties
4141
were configured in `properties`.
4242
* If the GeoLite2 ASN database is used, then the following fields may be added under the `target_field`: `ip`,
43-
`asn`, and `organization_name`. The fields actually added depend on what has been found and which properties were configured
43+
`asn`, `organization_name` and `network`. The fields actually added depend on what has been found and which properties were configured
4444
in `properties`.
4545

46+
4647
Here is an example that uses the default city database and adds the geographical information to the `geoip` field based on the `ip` field:
4748

4849
[source,console]

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpProcessor.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.ingest.geoip;
2121

22+
import com.maxmind.db.Network;
2223
import com.maxmind.geoip2.exception.AddressNotFoundException;
2324
import com.maxmind.geoip2.model.AsnResponse;
2425
import com.maxmind.geoip2.model.CityResponse;
@@ -345,6 +346,7 @@ private Map<String, Object> retrieveAsnGeoData(InetAddress ipAddress) {
345346

346347
Integer asn = response.getAutonomousSystemNumber();
347348
String organization_name = response.getAutonomousSystemOrganization();
349+
Network network = response.getNetwork();
348350

349351
Map<String, Object> geoData = new HashMap<>();
350352
for (Property property : this.properties) {
@@ -362,6 +364,11 @@ private Map<String, Object> retrieveAsnGeoData(InetAddress ipAddress) {
362364
geoData.put("organization_name", organization_name);
363365
}
364366
break;
367+
case NETWORK:
368+
if (network != null) {
369+
geoData.put("network", network.toString());
370+
}
371+
break;
365372
}
366373
}
367374
return geoData;
@@ -376,7 +383,7 @@ public static final class Factory implements Processor.Factory {
376383
Property.CONTINENT_NAME, Property.COUNTRY_ISO_CODE
377384
));
378385
static final Set<Property> DEFAULT_ASN_PROPERTIES = Collections.unmodifiableSet(EnumSet.of(
379-
Property.IP, Property.ASN, Property.ORGANIZATION_NAME
386+
Property.IP, Property.ASN, Property.ORGANIZATION_NAME, Property.NETWORK
380387
));
381388

382389
private final Map<String, DatabaseReaderLazyLoader> databaseReaders;
@@ -464,7 +471,8 @@ enum Property {
464471
TIMEZONE,
465472
LOCATION,
466473
ASN,
467-
ORGANIZATION_NAME;
474+
ORGANIZATION_NAME,
475+
NETWORK;
468476

469477
static final EnumSet<Property> ALL_CITY_PROPERTIES = EnumSet.of(
470478
Property.IP, Property.COUNTRY_ISO_CODE, Property.COUNTRY_NAME, Property.CONTINENT_NAME,
@@ -475,7 +483,7 @@ enum Property {
475483
Property.IP, Property.CONTINENT_NAME, Property.COUNTRY_NAME, Property.COUNTRY_ISO_CODE
476484
);
477485
static final EnumSet<Property> ALL_ASN_PROPERTIES = EnumSet.of(
478-
Property.IP, Property.ASN, Property.ORGANIZATION_NAME
486+
Property.IP, Property.ASN, Property.ORGANIZATION_NAME, Property.NETWORK
479487
);
480488

481489
public static Property parseProperty(String databaseType, String value) {

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public void testBuildWithAsnDbAndCityFields() throws Exception {
189189
config.put("properties", Collections.singletonList(cityProperty));
190190
Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, null, config));
191191
assertThat(e.getMessage(), equalTo("[properties] illegal property value [" + cityProperty +
192-
"]. valid values are [IP, ASN, ORGANIZATION_NAME]"));
192+
"]. valid values are [IP, ASN, ORGANIZATION_NAME, NETWORK]"));
193193
}
194194

195195
public void testBuildNonExistingDbFile() throws Exception {

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,11 @@ public void testAsn() throws Exception {
209209
assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo(ip));
210210
@SuppressWarnings("unchecked")
211211
Map<String, Object> geoData = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
212-
assertThat(geoData.size(), equalTo(3));
212+
assertThat(geoData.size(), equalTo(4));
213213
assertThat(geoData.get("ip"), equalTo(ip));
214214
assertThat(geoData.get("asn"), equalTo(1136));
215215
assertThat(geoData.get("organization_name"), equalTo("KPN B.V."));
216+
assertThat(geoData.get("network"), equalTo("82.168.0.0/14"));
216217
}
217218

218219
public void testAddressIsNotInTheDatabase() throws Exception {

modules/ingest-geoip/src/yamlRestTest/resources/rest-api-spec/test/ingest_geoip/20_geoip_processor.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@
299299
index: test
300300
id: 1
301301
- match: { _source.field1: "82.171.64.0" }
302-
- length: { _source.geoip: 3 }
302+
- length: { _source.geoip: 4 }
303303
- match: { _source.geoip.ip: "82.171.64.0" }
304304
- match: { _source.geoip.asn: 1136 }
305305
- match: { _source.geoip.organization_name: "KPN B.V." }
306+
- match: { _source.geoip.network: "82.168.0.0/14" }

0 commit comments

Comments
 (0)