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
201 additions
and
27 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
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.ip2geo.common; | ||
|
||
import inet.ipaddr.IPAddressString; | ||
import org.opensearch.common.settings.ClusterSettings; | ||
import org.opensearch.geospatial.annotation.VisibleForTesting; | ||
|
||
import java.net.InetAddress; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.UnknownHostException; | ||
import java.util.List; | ||
|
||
/** | ||
* 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 | ||
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.