Skip to content

Commit

Permalink
Set User-Agent in http request
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <[email protected]>
  • Loading branch information
heemin32 committed May 10, 2023
1 parent 91c609b commit 6b49244
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.CharBuffer;
import java.security.AccessController;
import java.security.PrivilegedAction;
Expand All @@ -25,6 +26,7 @@
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.geospatial.shared.Constants;

/**
* Ip2Geo datasource manifest file object
Expand All @@ -39,7 +41,7 @@ public class DatasourceManifest {
private static final ParseField DB_NAME_FIELD = new ParseField("db_name");
private static final ParseField SHA256_HASH_FIELD = new ParseField("sha256_hash");
private static final ParseField VALID_FOR_IN_DAYS_FIELD = new ParseField("valid_for_in_days");
private static final ParseField UPDATED_AT_FIELD = new ParseField("updated_at");
private static final ParseField UPDATED_AT_FIELD = new ParseField("updated_at_in_epoch_milli");
private static final ParseField PROVIDER_FIELD = new ParseField("provider");

/**
Expand Down Expand Up @@ -114,16 +116,21 @@ public static class Builder {
public static DatasourceManifest build(final URL url) {
SpecialPermission.check();
return AccessController.doPrivileged((PrivilegedAction<DatasourceManifest>) () -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
CharBuffer charBuffer = CharBuffer.allocate(MANIFEST_FILE_MAX_BYTES);
reader.read(charBuffer);
charBuffer.flip();
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.IGNORE_DEPRECATIONS,
charBuffer.toString()
);
return PARSER.parse(parser, null);
try {
URLConnection con = url.openConnection();
con.addRequestProperty("User-Agent", Constants.USER_AGENT);
InputStreamReader inputStreamReader = new InputStreamReader(con.getInputStream());
try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
CharBuffer charBuffer = CharBuffer.allocate(MANIFEST_FILE_MAX_BYTES);
reader.read(charBuffer);
charBuffer.flip();
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.IGNORE_DEPRECATIONS,
charBuffer.toString()
);
return PARSER.parse(parser, null);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
Expand Down Expand Up @@ -52,6 +53,7 @@
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.geospatial.shared.Constants;
import org.opensearch.geospatial.shared.StashedThreadContext;
import org.opensearch.index.query.QueryBuilders;

Expand Down Expand Up @@ -138,7 +140,9 @@ public CSVParser getDatabaseReader(final DatasourceManifest manifest) {
return AccessController.doPrivileged((PrivilegedAction<CSVParser>) () -> {
try {
URL zipUrl = new URL(manifest.getUrl());
ZipInputStream zipIn = new ZipInputStream(zipUrl.openStream());
URLConnection con = zipUrl.openConnection();
con.addRequestProperty("User-Agent", Constants.USER_AGENT);
ZipInputStream zipIn = new ZipInputStream(con.getInputStream());
ZipEntry zipEntry = zipIn.getNextEntry();
while (zipEntry != null) {
if (zipEntry.getName().equalsIgnoreCase(manifest.getDbName()) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public class Ip2GeoSettings {
*/
public static final Setting<String> DATASOURCE_ENDPOINT = Setting.simpleString(
"plugins.geospatial.ip2geo.datasource.endpoint",
// TODO: This value is not correct. Update it later once CDN server is ready.
"https://geoip.maps.opensearch.org/v1/geolite-2/manifest.json",
"https://geoip.maps.opensearch.org/v1/geolite2-city/manifest.json",
new DatasourceEndpointValidator(),
Setting.Property.NodeScope,
Setting.Property.Dynamic
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/opensearch/geospatial/shared/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.geospatial.shared;

import java.util.Locale;

import org.opensearch.Version;

public class Constants {
public static final String USER_AGENT = String.format(Locale.ROOT, "OpenSearch/%s vanilla", Version.CURRENT.toString());
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testPrepareRequestDefaultValue() {
verifyingClient.setExecuteLocallyVerifier((actionResponse, actionRequest) -> {
assertTrue(actionRequest instanceof PutDatasourceRequest);
PutDatasourceRequest putDatasourceRequest = (PutDatasourceRequest) actionRequest;
assertEquals("https://geoip.maps.opensearch.org/v1/geolite-2/manifest.json", putDatasourceRequest.getEndpoint());
assertEquals("https://geoip.maps.opensearch.org/v1/geolite2-city/manifest.json", putDatasourceRequest.getEndpoint());
assertEquals(TimeValue.timeValueDays(3), putDatasourceRequest.getUpdateInterval());
assertEquals(datasourceName, putDatasourceRequest.getName());
isExecuted.set(true);
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/ip2geo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"db_name": "sample_valid.csv",
"sha256_hash": "safasdfaskkkesadfasdf",
"valid_for_in_days": 30,
"updated_at": 3134012341236,
"updated_at_in_epoch_milli": 3134012341236,
"provider": "sample_provider"
}
2 changes: 1 addition & 1 deletion src/test/resources/ip2geo/manifest_invalid_url.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"db_name": "sample_valid.csv",
"sha256_hash": "safasdfaskkkesadfasdf",
"valid_for_in_days": 30,
"updated_at": 3134012341236,
"updated_at_in_epoch_milli": 3134012341236,
"provider": "sample_provider"
}
2 changes: 1 addition & 1 deletion src/test/resources/ip2geo/manifest_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"db_name": "sample_valid.csv",
"sha256_hash": "safasdfaskkkesadfasdf",
"valid_for_in_days": 30,
"updated_at": 3134012341236,
"updated_at_in_epoch_milli": 3134012341236,
"provider": "maxmind"
}

0 comments on commit 6b49244

Please sign in to comment.