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
16 changed files
with
216 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
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
/* | ||
* 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 org.opensearch.geospatial.annotation.VisibleForTesting; | ||
|
||
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) throws UnknownHostException, MalformedURLException { | ||
return toUrlIfNotInDenyList(url, clusterSettings.get(Ip2GeoSettings.DATASOURCE_ENDPOINT_DENYLIST)); | ||
} | ||
|
||
@VisibleForTesting | ||
@SuppressForbidden(reason = "Need to connect to http endpoint to read GeoIP database file") | ||
protected 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
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.