-
Notifications
You must be signed in to change notification settings - Fork 38
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 (#573)
Signed-off-by: Heemin Kim <[email protected]>
- Loading branch information
Showing
17 changed files
with
309 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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
/* | ||
* 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 lombok.extern.log4j.Log4j2; | ||
|
||
import org.opensearch.common.SuppressForbidden; | ||
import org.opensearch.common.settings.ClusterSettings; | ||
|
||
import inet.ipaddr.IPAddressString; | ||
|
||
/** | ||
* A class to check url against a deny-list | ||
*/ | ||
@Log4j2 | ||
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 | ||
*/ | ||
public URL toUrlIfNotInDenyList(final String url) { | ||
try { | ||
return toUrlIfNotInDenyList(url, clusterSettings.get(Ip2GeoSettings.DATASOURCE_ENDPOINT_DENYLIST)); | ||
} catch (UnknownHostException e) { | ||
log.error("Unknown host", e); | ||
throw new IllegalArgumentException("host provided in the datasource endpoint is unknown"); | ||
} catch (MalformedURLException e) { | ||
log.error("Malformed URL", e); | ||
throw new IllegalArgumentException("URL provided in the datasource endpoint 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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial; | ||
|
||
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; | ||
|
||
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 org.opensearch.cluster.ClusterName; | ||
import org.opensearch.common.SuppressForbidden; | ||
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; | ||
|
||
@SuppressForbidden(reason = "used only for testing") | ||
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.