Skip to content

Commit

Permalink
ESRelay release v1)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjunfeng committed May 28, 2024
1 parent dce2c92 commit 3330a0b
Show file tree
Hide file tree
Showing 28 changed files with 1,286 additions and 509 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ private String[] getFixedPath(HttpServletRequest request) {
fixedPath[0] = path[0];
fixedPath[1] = ESConstants.ALL_FRAGMENT;
}
else if(path[0].charAt(0)=='_') {
fixedPath[0] = "";
fixedPath[1] = path[0];
}
else {
fixedPath[0] = path[0];
fixedPath[1] = ESConstants.INDICES_FRAGMENT;
Expand Down Expand Up @@ -223,13 +227,11 @@ public void service(HttpServletRequest request, HttpServletResponse response) th
if("_cmd".equalsIgnoreCase(path[0])){
Map<String, String> parameters = getParams(request);
parameters.put("cmd", path[1]);
path[2] = path[1];
path[1] = path[0];
path[0] = "ignite";

// read request body
ObjectNode jsonRequest = getJSONBody(path[0],request);

ESUpdate query = new ESUpdate(path, parameters, jsonRequest);
ESUpdate query = new ESUpdate(new String[] {"","_cmd"}, parameters, jsonRequest);

// process request, forward to ES instances
result = fHandler.handleRequest(query, user);
Expand Down Expand Up @@ -263,7 +265,8 @@ else if("_views".equalsIgnoreCase(path[0])) { // views query /_views/{schema}/{n
}
else if(request.getMethod().equals("GET")
|| action.equalsIgnoreCase(ESConstants.SEARCH_FRAGMENT)
|| action.equalsIgnoreCase(ESConstants.ALL_FRAGMENT)){
|| action.equalsIgnoreCase(ESConstants.ALL_FRAGMENT)
|| action.equalsIgnoreCase(ESConstants.GET_FRAGMENT)){


if(!request.getMethod().equals("GET")){
Expand Down Expand Up @@ -339,12 +342,23 @@ else if(request.getMethod().equals("HEAD")){
fLogger.log(Level.SEVERE, "Error during error JSON generation", e);

} catch (Exception e) {
response.setStatus(500);

response.resetBuffer();

ObjectNode jsonError = new ObjectNode(jsonNodeFactory);
jsonError.put(ESConstants.R_ERROR, e.getMessage());
jsonError.put(ESConstants.R_STATUS, 500);
if(e.getClass().getSimpleName().indexOf("NotFound")>=0) {
jsonError.put(ESConstants.R_STATUS, 404);
response.setStatus(404);
}
if(e.getClass().getSimpleName().indexOf("Access")>=0) {
jsonError.put(ESConstants.R_STATUS, 402);
response.setStatus(402);
}
else {
response.setStatus(500);
}

out.println(jsonError.toPrettyString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.Set;

import org.elasticsearch.relay.ESRelay;
import org.elasticsearch.relay.model.ESDelete;
import org.elasticsearch.relay.model.ESQuery;
import org.elasticsearch.relay.model.ESUpdate;
import org.elasticsearch.relay.permissions.UserPermSet;
import org.elasticsearch.relay.util.ESConstants;
import org.elasticsearch.relay.util.ESUtil;
Expand All @@ -17,7 +19,7 @@
* Filter excluding indices and types from the query result. Adjusts query body
* and query path.
*/
public class BlacklistFilter implements IFilter {
public class BlacklistFilter {
private static final String TYPE_VALUE = "value";

private final Set<String> fIndices; // black indices
Expand All @@ -36,84 +38,92 @@ public BlacklistFilter(Set<String> indices, Set<String> types) {
fTypes = types;
}

@Override
public ESQuery addFilter(UserPermSet perms, ESQuery query, List<String> indices, List<String> types) {
boolean allIndices = false;
boolean allTypes = false;

// check if all indices are to be searched

if (indices.isEmpty() || indices.size() == 1 && indices.get(0).equals(ESConstants.WILDCARD)) {
allIndices = true;
}
public ESQuery addFilter(ESQuery query) {
boolean removedLast = false;

// check if all types are to be searched
if (types.isEmpty() || types.size() == 1 && types.get(0).equals(ESConstants.WILDCARD)) {
allTypes = true;
if (fIndices.size()>0) {

if (fIndices.contains(query.getIndices())) {
removedLast = true;
query.cancel();
}
}

if (!removedLast && fTypes.size()>0) {
// add blacklisted types

if (fTypes.contains(query.getTypeName())) {
// repackage into request path
//-filterTypes(query,query.getTypeName());
query.cancel();
}
}

return query;
}



public ESDelete addFilter(ESDelete query) {
boolean removedLast = false;

if (fIndices.size()>0) {
// remove blacklisted indices
indices.removeAll(fIndices);

if (indices.isEmpty()) {

if (fIndices.contains(query.getIndices())) {
removedLast = true;
} else {
query.cancel();
}
}

if (!removedLast && fTypes.size()>0) {
// add blacklisted types

if (fTypes.contains(query.getTypeName())) {
// repackage into request path
replaceInPath(query, indices);
query.cancel();
}
}

return query;
}

if (!allTypes && fTypes.size()>0) {
// remove blacklisted types
types.removeAll(fTypes);
public ESUpdate addFilter(ESUpdate query) {
boolean removedLast = false;

if (types.isEmpty()) {
if (fIndices.size()>0) {

if (fIndices.contains(query.getIndices())) {
removedLast = true;
} else {
query.cancel();
}
}

if (!removedLast && fTypes.size()>0) {
// add blacklisted types

if (fTypes.contains(query.getTypeName())) {
// repackage into request path
try {
filterTypes(query);
} catch (Exception e) {
e.printStackTrace();
}
query.cancel();
}
}

// check that if all indices or types were removed the query won't pass as "get all"
if (removedLast) {
query.cancel();
}

return query;
}

private void filterTypes(ESQuery query) throws Exception {
private void filterTypes(ESQuery query,String type) {
// block problematic types through exclusion
ArrayNode filters = ESUtil.getOrCreateFilterArray(query);

for (String type : fTypes) {
ObjectNode notObject = new ObjectNode(ESRelay.jsonNodeFactory);
ObjectNode typeFilter = new ObjectNode(ESRelay.jsonNodeFactory);
ObjectNode valueObject = new ObjectNode(ESRelay.jsonNodeFactory);
ObjectNode notObject = new ObjectNode(ESRelay.jsonNodeFactory);
ObjectNode typeFilter = new ObjectNode(ESRelay.jsonNodeFactory);
ObjectNode valueObject = new ObjectNode(ESRelay.jsonNodeFactory);

valueObject.put(TYPE_VALUE, type);
valueObject.put(TYPE_VALUE, type);

typeFilter.set(ESConstants.Q_TYPE, valueObject);
typeFilter.set(ESConstants.Q_TYPE, valueObject);

notObject.set(ESConstants.Q_NOT, typeFilter);
filters.add(notObject);
}
}

private void replaceInPath(ESQuery query, List<String> entries) {
String fragment = "";
for (String entry : entries) {
fragment += entry + ",";
}
fragment = fragment.substring(0, fragment.length() - 1);
query.setIndices(fragment);
notObject.set(ESConstants.Q_NOT, typeFilter);
filters.add(notObject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public interface IFilter {
* permissions and other info for the current user
* @param query
* query to an Elasticsearch instance
* @param indices
* indices that are being queried
* @param types
* types that are being queried
* @param indice
* indice that are being queried
* @param type
* type that are being queried
* @return filtered/modified query
*/
public ESQuery addFilter(UserPermSet perms, ESQuery query, List<String> indices, List<String> types);
public ESQuery addFilter(UserPermSet perms, ESQuery query, String indice, String type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public LiferayFilter(Set<String> types, Set<String> passRoles) {
}

@Override
public ESQuery addFilter(UserPermSet perms, ESQuery query, List<String> indices, List<String> types) {
public ESQuery addFilter(UserPermSet perms, ESQuery query, String indices, String types) {
ArrayNode filters = query.getAuthFilterOrArr();
if(fTypes!=null && !fTypes.isEmpty() && !fTypes.contains(types)) {
return query;
}

String userId = perms.getLiferayId();
List<String> roleIds = perms.getLiferayRoles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ public NuxeoFilter(Set<String> types) {
}

@Override
public ESQuery addFilter(UserPermSet perms, ESQuery query, List<String> indices, List<String> types) {
public ESQuery addFilter(UserPermSet perms, ESQuery query, String indices, String types) {
String user = perms.getUserName();
if(fTypes!=null && !fTypes.isEmpty() && !fTypes.contains(types)) {
return query;
}

try {
ArrayNode filters = query.getAuthFilterOrArr();
Expand All @@ -45,7 +48,7 @@ public ESQuery addFilter(UserPermSet perms, ESQuery query, List<String> indices,

// TODO: what if permissions are taken away from a user

creatorFilter.put(ESConstants.Q_TERM, termObj);
creatorFilter.set(ESConstants.Q_TERM, termObj);

filters.add(creatorFilter);

Expand All @@ -55,7 +58,7 @@ public ESQuery addFilter(UserPermSet perms, ESQuery query, List<String> indices,
termObj = new ObjectNode(ESRelay.jsonNodeFactory);
termObj.put(NX_ACL, user);

aclFilter.put(ESConstants.Q_TERM, termObj);
aclFilter.set(ESConstants.Q_TERM, termObj);

filters.add(aclFilter);

Expand All @@ -66,7 +69,7 @@ public ESQuery addFilter(UserPermSet perms, ESQuery query, List<String> indices,
termObj = new ObjectNode(ESRelay.jsonNodeFactory);
termObj.put(NX_ACL, group);

aclFilter.put(ESConstants.Q_TERM, termObj);
aclFilter.set(ESConstants.Q_TERM, termObj);

filters.add(aclFilter);
}
Expand Down
Loading

0 comments on commit 3330a0b

Please sign in to comment.