generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 49
Add denylist ip config for datasource endpoint #573
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.