Skip to content

Avoid calling real maxmind endpoint from EnterpriseGeoIpDownloader (#111121) #111171

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

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -26,7 +26,6 @@
import org.elasticsearch.common.settings.MockSecureSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.ingest.EnterpriseGeoIpTask;
import org.elasticsearch.ingest.geoip.direct.DatabaseConfiguration;
Expand Down Expand Up @@ -54,13 +53,12 @@
public class EnterpriseGeoIpDownloaderIT extends ESIntegTestCase {

private static final String DATABASE_TYPE = "GeoIP2-City";
private static final boolean useFixture = Booleans.parseBoolean(System.getProperty("geoip_use_service", "false")) == false;

@ClassRule
public static final EnterpriseGeoIpHttpFixture fixture = new EnterpriseGeoIpHttpFixture(useFixture, DATABASE_TYPE);
public static final EnterpriseGeoIpHttpFixture fixture = new EnterpriseGeoIpHttpFixture(DATABASE_TYPE);

protected String getEndpoint() {
return useFixture ? fixture.getAddress() : null;
return fixture.getAddress();
}

@Override
Expand All @@ -71,11 +69,9 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
builder.setSecureSettings(secureSettings)
.put(super.nodeSettings(nodeOrdinal, otherSettings))
.put(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey(), true);
if (getEndpoint() != null) {
// note: this is using the enterprise fixture for the regular downloader, too, as
// a slightly hacky way of making the regular downloader not actually download any files
builder.put(GeoIpDownloader.ENDPOINT_SETTING.getKey(), getEndpoint());
}
// note: this is using the enterprise fixture for the regular downloader, too, as
// a slightly hacky way of making the regular downloader not actually download any files
builder.put(GeoIpDownloader.ENDPOINT_SETTING.getKey(), getEndpoint());
return builder.build();
}

Expand All @@ -94,9 +90,7 @@ public void testEnterpriseDownloaderTask() throws Exception {
* was updated with information from the database.
* Note that the "enterprise database" is actually just a geolite database being loaded by the GeoIpHttpFixture.
*/
if (getEndpoint() != null) {
EnterpriseGeoIpDownloader.DEFAULT_MAXMIND_ENDPOINT = getEndpoint();
}
EnterpriseGeoIpDownloader.DEFAULT_MAXMIND_ENDPOINT = getEndpoint();
final String pipelineName = "enterprise_geoip_pipeline";
final String indexName = "enterprise_geoip_test_index";
final String sourceField = "ip";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@
public class EnterpriseGeoIpHttpFixture extends ExternalResource {

private final Path source;
private final boolean enabled;
private final String[] databaseTypes;
private HttpServer server;

/*
* The values in databaseTypes must be in DatabaseConfiguration.MAXMIND_NAMES, and must be one of the databases copied in the
* copyFiles method of thisi class.
*/
public EnterpriseGeoIpHttpFixture(boolean enabled, String... databaseTypes) {
this.enabled = enabled;
public EnterpriseGeoIpHttpFixture(String... databaseTypes) {
this.databaseTypes = databaseTypes;
try {
this.source = Files.createTempDirectory("source");
Expand All @@ -56,28 +54,26 @@ public String getAddress() {

@Override
protected void before() throws Throwable {
if (enabled) {
copyFiles();
this.server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
copyFiles();
this.server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);

// for expediency reasons, it is handy to have this test fixture be able to serve the dual purpose of actually stubbing
// out the download protocol for downloading files from maxmind (see the looped context creation after this stanza), as
// we as to serve an empty response for the geoip.elastic.co service here
this.server.createContext("/", exchange -> {
String response = "[]"; // an empty json array
exchange.sendResponseHeaders(200, response.length());
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.getBytes(StandardCharsets.UTF_8));
}
});

// register the file types for the download fixture
for (String databaseType : databaseTypes) {
createContextForEnterpriseDatabase(databaseType);
// for expediency reasons, it is handy to have this test fixture be able to serve the dual purpose of actually stubbing
// out the download protocol for downloading files from maxmind (see the looped context creation after this stanza), as
// we as to serve an empty response for the geoip.elastic.co service here
this.server.createContext("/", exchange -> {
String response = "[]"; // an empty json array
exchange.sendResponseHeaders(200, response.length());
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.getBytes(StandardCharsets.UTF_8));
}
});

server.start();
// register the file types for the download fixture
for (String databaseType : databaseTypes) {
createContextForEnterpriseDatabase(databaseType);
}

server.start();
}

private void createContextForEnterpriseDatabase(String databaseType) {
Expand Down Expand Up @@ -108,9 +104,7 @@ private void createContextForEnterpriseDatabase(String databaseType) {

@Override
protected void after() {
if (enabled) {
server.stop(0);
}
server.stop(0);
}

private void copyFiles() throws Exception {
Expand Down