-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Blocking API calls - enabled (#976). Currently - DROP only
- Loading branch information
1 parent
c391096
commit 35e748a
Showing
8 changed files
with
164 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
|
||
# Run this script post-installation, to block all the settings that | ||
# should not be available to the general public in a production Dataverse installation. | ||
|
||
curl -X PUT -d groups,s,index,datasetfield http://localhost:8080/api/s/settings/:BlockedApiEndpoints |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/bin/bash | ||
|
||
ADMIN_KEY=$1 | ||
|
||
echo Testing Groups | ||
curl http://localhost:8080/api/groups/ip/?key=$ADMIN_KEY | ||
echo | ||
|
||
echo blocking groups | ||
curl -X PUT -d groups http://localhost:8080/api/s/settings/:BlockedApiEndpoints | ||
echo | ||
|
||
echo Testing Groups again - expecting 503 Unavailable | ||
curl -v http://localhost:8080/api/groups/ip/?key=$ADMIN_KEY | ||
echo | ||
|
||
echo Unblocking groups | ||
curl -X DELETE http://localhost:8080/api/s/settings/:BlockedApiEndpoints | ||
echo | ||
|
||
echo Testing Groups | ||
curl http://localhost:8080/api/groups/ip/?key=$ADMIN_KEY | ||
echo | ||
|
||
echo blocking groups, Roles | ||
curl -X PUT -d groups,roles http://localhost:8080/api/s/settings/:BlockedApiEndpoints | ||
echo | ||
|
||
echo Testing Groups again - expecting 503 Unavailable | ||
curl -v http://localhost:8080/api/groups/ip/?key=$ADMIN_KEY | ||
echo | ||
|
||
echo Testing Roles - expecting 503 Unavailable | ||
curl -v http://localhost:8080/api/roles/?key=$ADMIN_KEY | ||
echo | ||
|
||
echo blocking Roles only | ||
curl -X PUT -d roles http://localhost:8080/api/s/settings/:BlockedApiEndpoints | ||
echo | ||
|
||
echo Testing Groups again | ||
curl -v http://localhost:8080/api/groups/ip/?key=$ADMIN_KEY | ||
echo | ||
|
||
echo Testing Roles - expecting 503 Unavailable | ||
curl -v http://localhost:8080/api/roles/?key=$ADMIN_KEY | ||
echo | ||
|
||
echo Unblocking all | ||
curl -X DELETE http://localhost:8080/api/s/settings/:BlockedApiEndpoints | ||
echo | ||
|
||
echo DONE |
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
97 changes: 97 additions & 0 deletions
97
src/main/java/edu/harvard/iq/dataverse/api/ApiBlockingFilter.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,97 @@ | ||
package edu.harvard.iq.dataverse.api; | ||
|
||
import edu.harvard.iq.dataverse.settings.SettingsServiceBean; | ||
import java.io.IOException; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import javax.ejb.EJB; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.annotation.WebFilter; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* A web filter to block API administration calls. | ||
* @author michael | ||
*/ | ||
@WebFilter( urlPatterns={"/api/*"} ) | ||
public class ApiBlockingFilter implements javax.servlet.Filter { | ||
|
||
private static final Logger logger = Logger.getLogger(ApiBlockingFilter.class.getName()); | ||
|
||
@EJB | ||
protected SettingsServiceBean settingsSvc; | ||
|
||
final Set<String> blockedApiEndpoints = new TreeSet<>(); | ||
private String lastEndpointList; | ||
|
||
@Override | ||
public void init(FilterConfig fc) throws ServletException { | ||
updateBlockedPoints(); | ||
} | ||
|
||
private void updateBlockedPoints() { | ||
blockedApiEndpoints.clear(); | ||
String endpointList = settingsSvc.getValueForKey(SettingsServiceBean.Key.BlockedApiEndpoints, ""); | ||
for ( String endpoint : endpointList.split(",") ) { | ||
String endpointPrefix = canonize(endpoint); | ||
if ( ! endpointPrefix.isEmpty() ) { | ||
logger.log(Level.INFO, "Blocking API endpoint: {0}", endpointPrefix); | ||
blockedApiEndpoints.add(endpointPrefix); | ||
} | ||
} | ||
lastEndpointList = endpointList; | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException { | ||
String endpointList = settingsSvc.getValueForKey(SettingsServiceBean.Key.BlockedApiEndpoints, ""); | ||
if ( ! endpointList.equals(lastEndpointList) ) { | ||
updateBlockedPoints(); | ||
} | ||
|
||
HttpServletRequest hsr = (HttpServletRequest) sr; | ||
String apiEndpoint = canonize(hsr.getRequestURI().substring(hsr.getServletPath().length())); | ||
|
||
for ( String prefix : blockedApiEndpoints ) { | ||
if ( apiEndpoint.startsWith(prefix) ) { | ||
// Block! | ||
HttpServletResponse httpResponse = (HttpServletResponse) sr1; | ||
httpResponse.getWriter().println("{ status:\"error\", message:\"Endpoint blocked. Please contact the dataverse administrator\"}" ); | ||
httpResponse.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); | ||
httpResponse.setContentType("application/json"); | ||
return; | ||
} | ||
} | ||
|
||
fc.doFilter(sr, sr1); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
logger.info("WebFilter destroy"); | ||
} | ||
|
||
/** | ||
* Creates a canonical representation of {@code in}: trimmed spaces and slashes | ||
* @param in the raw string | ||
* @return {@code in} with no trailing and leading spaces and slashes. | ||
*/ | ||
private String canonize( String in ) { | ||
in = in.trim(); | ||
if ( in.startsWith("/") ) { | ||
in = in.substring(1); | ||
} | ||
if ( in.endsWith("/") ) { | ||
in = in.substring(0, in.length()-1); | ||
} | ||
return in; | ||
} | ||
|
||
} |
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