forked from opensearch-project/geospatial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add denylist ip config for datasource endpoint
Signed-off-by: Heemin Kim <heemin@amazon.com>
- Loading branch information
Showing
17 changed files
with
304 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/org/opensearch/geospatial/ip2geo/common/URLDenyListChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.ip2geo.common; | ||
|
||
import java.net.InetAddress; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.UnknownHostException; | ||
import java.util.List; | ||
|
||
import org.opensearch.common.SuppressForbidden; | ||
import org.opensearch.common.settings.ClusterSettings; | ||
|
||
import inet.ipaddr.IPAddressString; | ||
|
||
/** | ||
* A class to check url against a deny-list | ||
*/ | ||
public class URLDenyListChecker { | ||
private final ClusterSettings clusterSettings; | ||
|
||
public URLDenyListChecker(final ClusterSettings clusterSettings) { | ||
this.clusterSettings = clusterSettings; | ||
} | ||
|
||
/** | ||
* Convert String to URL after verifying the url is not on a deny-list | ||
* | ||
* @param url value to validate and convert to URL | ||
* @return value in URL type | ||
* @throws UnknownHostException | ||
* @throws MalformedURLException | ||
*/ | ||
public URL toUrlIfNotInDenyList(final String url) { | ||
try { | ||
return toUrlIfNotInDenyList(url, clusterSettings.get(Ip2GeoSettings.DATASOURCE_ENDPOINT_DENYLIST)); | ||
} catch (UnknownHostException e) { | ||
throw new IllegalArgumentException("host is unknown"); | ||
} catch (MalformedURLException e) { | ||
throw new IllegalArgumentException("url is malformed"); | ||
} | ||
} | ||
|
||
@SuppressForbidden(reason = "Need to connect to http endpoint to read GeoIP database file") | ||
private URL toUrlIfNotInDenyList(final String url, final List<String> denyList) throws UnknownHostException, MalformedURLException { | ||
URL urlToReturn = new URL(url); | ||
if (isInDenyList(new IPAddressString(InetAddress.getByName(urlToReturn.getHost()).getHostAddress()), denyList)) { | ||
throw new IllegalArgumentException( | ||
"given endpoint is blocked by deny list in cluster setting " + Ip2GeoSettings.DATASOURCE_ENDPOINT_DENYLIST.getKey() | ||
); | ||
} | ||
return urlToReturn; | ||
} | ||
|
||
private boolean isInDenyList(final IPAddressString url, final List<String> denyList) { | ||
return denyList.stream().map(cidr -> new IPAddressString(cidr)).anyMatch(cidr -> cidr.contains(url)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/test/java/org/opensearch/geospatial/ClusterSettingHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial; | ||
|
||
import org.opensearch.cluster.ClusterName; | ||
import org.opensearch.common.network.NetworkModule; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.env.Environment; | ||
import org.opensearch.geospatial.plugin.GeospatialPlugin; | ||
import org.opensearch.node.MockNode; | ||
import org.opensearch.node.Node; | ||
import org.opensearch.plugins.Plugin; | ||
import org.opensearch.test.InternalTestCluster; | ||
import org.opensearch.test.MockHttpTransport; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.apache.lucene.tests.util.LuceneTestCase.createTempDir; | ||
import static org.opensearch.test.NodeRoles.dataNode; | ||
import static org.opensearch.test.OpenSearchTestCase.getTestTransportPlugin; | ||
import static org.opensearch.test.OpenSearchTestCase.getTestTransportType; | ||
import static org.opensearch.test.OpenSearchTestCase.randomLong; | ||
|
||
public class ClusterSettingHelper { | ||
public Node createMockNode(Map<String, Object> configSettings) throws IOException { | ||
Path configDir = createTempDir(); | ||
File configFile = configDir.resolve("opensearch.yml").toFile(); | ||
FileWriter configFileWriter = new FileWriter(configFile); | ||
|
||
for (Map.Entry<String, Object> setting : configSettings.entrySet()) { | ||
configFileWriter.write("\"" + setting.getKey() + "\": " + setting.getValue()); | ||
} | ||
configFileWriter.close(); | ||
return new MockNode(baseSettings().build(), basePlugins(), configDir, true); | ||
} | ||
|
||
private List<Class<? extends Plugin>> basePlugins() { | ||
List<Class<? extends Plugin>> plugins = new ArrayList<>(); | ||
plugins.add(getTestTransportPlugin()); | ||
plugins.add(MockHttpTransport.TestPlugin.class); | ||
plugins.add(GeospatialPlugin.class); | ||
return plugins; | ||
} | ||
|
||
private static Settings.Builder baseSettings() { | ||
final Path tempDir = createTempDir(); | ||
return Settings.builder() | ||
.put(ClusterName.CLUSTER_NAME_SETTING.getKey(), InternalTestCluster.clusterName("single-node-cluster", randomLong())) | ||
.put(Environment.PATH_HOME_SETTING.getKey(), tempDir) | ||
.put(NetworkModule.TRANSPORT_TYPE_KEY, getTestTransportType()) | ||
.put(dataNode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.