Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port support for commercial GeoIP2 databases from Logstash. #24889

Merged
merged 4 commits into from
Jun 13, 2017
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
public final class GeoIpProcessor extends AbstractProcessor {

public static final String TYPE = "geoip";
private static final String CITY_DB_TYPE = "GeoLite2-City";
private static final String COUNTRY_DB_TYPE = "GeoLite2-Country";
private static final String CITY_DB_TYPE = "-City";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is now a suffix, can you change the constant name to reflect that, eg CITY_DB_SUFFIX? And same with country as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we go, renamed in my branch.

private static final String COUNTRY_DB_TYPE = "-Country";

private final String field;
private final String targetField;
Expand Down Expand Up @@ -93,24 +93,23 @@ public void execute(IngestDocument ingestDocument) {
final InetAddress ipAddress = InetAddresses.forString(ip);

Map<String, Object> geoData;
switch (dbReader.getMetadata().getDatabaseType()) {
case CITY_DB_TYPE:
try {
geoData = retrieveCityGeoData(ipAddress);
} catch (AddressNotFoundRuntimeException e) {
geoData = Collections.emptyMap();
}
break;
case COUNTRY_DB_TYPE:
try {
geoData = retrieveCountryGeoData(ipAddress);
} catch (AddressNotFoundRuntimeException e) {
geoData = Collections.emptyMap();
}
break;
default:
throw new ElasticsearchParseException("Unsupported database type [" + dbReader.getMetadata().getDatabaseType()
+ "]", new IllegalStateException());
String databaseType = dbReader.getMetadata().getDatabaseType();

if (databaseType.endsWith(CITY_DB_TYPE)) {
try {
geoData = retrieveCityGeoData(ipAddress);
} catch (AddressNotFoundRuntimeException e) {
geoData = Collections.emptyMap();
}
} else if (databaseType.endsWith(COUNTRY_DB_TYPE)) {
try {
geoData = retrieveCountryGeoData(ipAddress);
} catch (AddressNotFoundRuntimeException e) {
geoData = Collections.emptyMap();
}
} else {
throw new ElasticsearchParseException("Unsupported database type [" + dbReader.getMetadata().getDatabaseType()
+ "]", new IllegalStateException());
}
if (geoData.isEmpty() == false) {
ingestDocument.setFieldValue(targetField, geoData);
Expand Down Expand Up @@ -299,9 +298,9 @@ public GeoIpProcessor create(Map<String, Processor.Factory> registry, String pro
}
}
} else {
if (CITY_DB_TYPE.equals(databaseType)) {
if (databaseType.endsWith(CITY_DB_TYPE)) {
properties = DEFAULT_CITY_PROPERTIES;
} else if (COUNTRY_DB_TYPE.equals(databaseType)) {
} else if (databaseType.endsWith(COUNTRY_DB_TYPE)) {
properties = DEFAULT_COUNTRY_PROPERTIES;
} else {
throw newConfigurationException(TYPE, processorTag, "database_file", "Unsupported database type ["
Expand Down Expand Up @@ -340,9 +339,9 @@ enum Property {

public static Property parseProperty(String databaseType, String value) {
Set<Property> validProperties = EnumSet.noneOf(Property.class);
if (CITY_DB_TYPE.equals(databaseType)) {
if (databaseType.endsWith(CITY_DB_TYPE)) {
validProperties = ALL_CITY_PROPERTIES;
} else if (COUNTRY_DB_TYPE.equals(databaseType)) {
} else if (databaseType.endsWith(COUNTRY_DB_TYPE)) {
validProperties = ALL_COUNTRY_PROPERTIES;
}

Expand Down