Skip to content

Commit

Permalink
Fix broken if statement in geoip processor (#79188)
Browse files Browse the repository at this point in the history
Relates to #79074
  • Loading branch information
martijnvg authored Oct 15, 2021
1 parent 1073186 commit 1c0dfad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ public void testUseGeoIpProcessorWithDownloadedDBs() throws Exception {
deleteDatabasesInConfigDirectory();
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/79074")
public void testStartWithNoDatabases() throws Exception {
assumeTrue("only test with fixture to have stable results", ENDPOINT != null);
putPipeline();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,11 @@ public Processor create(
}

DatabaseReaderLazyLoader lazyLoader = databaseRegistry.getDatabase(databaseFile);
if (lazyLoader == null && databaseRegistry.getAvailableDatabases().isEmpty() == false) {
if (useDatabaseUnavailableProcessor(lazyLoader, databaseRegistry.getAvailableDatabases())) {
return new DatabaseUnavailableProcessor(processorTag, description, databaseFile);
} else if (lazyLoader == null) {
throw newConfigurationException(TYPE, processorTag,
"database_file", "database file [" + databaseFile + "] doesn't exist");
} else if (lazyLoader == null && databaseRegistry.getAvailableDatabases().isEmpty()) {
return new DatabaseUnavailableProcessor(processorTag, description, databaseFile);
}
final String databaseType;
try {
Expand Down Expand Up @@ -439,7 +439,7 @@ public Processor create(
}
CheckedSupplier<DatabaseReaderLazyLoader, IOException> supplier = () -> {
DatabaseReaderLazyLoader loader = databaseRegistry.getDatabase(databaseFile);
if (loader == null && databaseRegistry.getAvailableDatabases().isEmpty() == false) {
if (useDatabaseUnavailableProcessor(loader, databaseRegistry.getAvailableDatabases())) {
return null;
} else if (loader == null) {
throw new ResourceNotFoundException("database file [" + databaseFile + "] doesn't exist");
Expand Down Expand Up @@ -479,6 +479,11 @@ public Processor create(
return new GeoIpProcessor(processorTag, description, ipField, supplier, isValid, targetField, properties, ignoreMissing,
firstOnly, databaseFile);
}

private static boolean useDatabaseUnavailableProcessor(DatabaseReaderLazyLoader loader, Set<String> availableDatabases) {
return loader == null && availableDatabases.isEmpty();
}

}

// Geoip2's AddressNotFoundException is checked and due to the fact that we need run their code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,18 @@ public void testUpdateDatabaseWhileIngesting() throws Exception {
assertThat(geoData.get("city_name"), equalTo("Linköping"));
}
{
// No databases are available, so assume that databases still need to be downloaded and therefor not fail:
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
databaseRegistry.removeStaleEntries(List.of("GeoLite2-City.mmdb"));
localDatabases.updateDatabase(geoIpConfigDir.resolve("GeoLite2-City.mmdb"), false);
processor.execute(ingestDocument);
Map<?, ?> geoData = (Map<?, ?>) ingestDocument.getSourceAndMetadata().get("geoip");
assertThat(geoData, nullValue());
}
{
// There are database available, but not the right one, so fail:
databaseRegistry.updateDatabase("GeoLite2-City-Test.mmdb", "md5", geoipTmpDir.resolve("GeoLite2-City-Test.mmdb"));
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
Exception e = expectThrows(ResourceNotFoundException.class, () -> processor.execute(ingestDocument));
assertThat(e.getMessage(), equalTo("database file [GeoLite2-City.mmdb] doesn't exist"));
}
Expand Down

0 comments on commit 1c0dfad

Please sign in to comment.