Skip to content

Commit

Permalink
bulk api: handle relative paths
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Litvintsev <[email protected]>
  • Loading branch information
DmitryLitvintsev committed Nov 26, 2024
1 parent 233aeb2 commit 3a47f42
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
*/
package org.dcache.restful.resources.bulk;

import static org.dcache.http.AuthenticationHandler.getLoginAttributes;
import static org.dcache.restful.util.HttpServletRequests.getUserRootAwareTargetPrefix;
import static org.dcache.restful.util.JSONUtils.newBadRequestException;

Expand All @@ -67,6 +68,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import diskCacheV111.util.FsPath;
import diskCacheV111.util.PnfsHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
Expand All @@ -92,6 +94,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.GET;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.PATCH;
Expand All @@ -103,9 +106,11 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.dcache.auth.attributes.LoginAttributes;
import org.dcache.auth.attributes.Restriction;
import org.dcache.auth.attributes.Restrictions;
import org.dcache.cells.CellStub;
import org.dcache.http.PathMapper;
import org.dcache.restful.util.HandlerBuilders;
import org.dcache.restful.util.RequestUser;
import org.dcache.restful.util.bulk.BulkServiceCommunicator;
Expand All @@ -124,10 +129,14 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import org.dcache.services.bulk.BulkRequestStatus;
import org.dcache.services.bulk.BulkRequestStatusMessage;
import org.dcache.services.bulk.BulkRequestSummary;
import org.dcache.services.bulk.BulkRequestTargetInfo;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


/**
* <p>RESTful API to the BulkService.</p>
*
Expand All @@ -138,12 +147,18 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
@Path("/bulk-requests")
public final class BulkResources {

private static final Logger LOGGER =
LoggerFactory.getLogger(BulkResources.class);

@Context
private HttpServletRequest request;

@Inject
private BulkServiceCommunicator service;

@Inject
private PathMapper pathMapper;

@Inject
@Named("pnfs-stub")
private CellStub pnfsmanager;
Expand Down Expand Up @@ -235,8 +250,9 @@ public Response submit(
Subject subject = getSubject();
Restriction restriction = getRestriction();
PnfsHandler handler = HandlerBuilders.unrestrictedPnfsHandler(pnfsmanager);
BulkRequest request = toBulkRequest(requestPayload, this.request, handler);

FsPath userRoot = LoginAttributes.getUserRoot(getLoginAttributes(request));
FsPath rootPath = pathMapper.effectiveRoot(userRoot, ForbiddenException::new);
BulkRequest request = toBulkRequest(requestPayload, this.request, handler, rootPath);
/*
* Frontend sets the URL. The backend service provides the UUID.
*/
Expand Down Expand Up @@ -284,7 +300,12 @@ public BulkRequestInfo getBulkRequestStatus(@ApiParam("The unique id of the requ
message.setSubject(subject);
message.setOffset(offset);
message = service.send(message);
return message.getInfo();
BulkRequestInfo info = message.getInfo();
List<BulkRequestTargetInfo> targetInfos = info.getTargets();
targetInfos.forEach(ti -> ti.setTarget(FsPath.create(ti.getTarget())
.stripPrefix(FsPath.create(info.getTargetPrefix()))));
info.setTargets(targetInfos);
return info;
}

@GET
Expand Down Expand Up @@ -498,7 +519,11 @@ public static Restriction getRestriction() {
* they are defined in the Bulk service as well.
*/
@VisibleForTesting
static BulkRequest toBulkRequest(String requestPayload, HttpServletRequest httpServletRequest, PnfsHandler handler) {
static BulkRequest toBulkRequest(String requestPayload,
HttpServletRequest httpServletRequest,
PnfsHandler handler,
FsPath rootPath) {

if (Strings.emptyToNull(requestPayload) == null) {
throw new BadRequestException("empty request payload.");
}
Expand All @@ -511,6 +536,7 @@ static BulkRequest toBulkRequest(String requestPayload, HttpServletRequest httpS
}

BulkRequest request = new BulkRequest();
request.setTargetPrefix(getUserRootAwareTargetPrefix(httpServletRequest, rootPath.toString(), handler));

Map<String, Object> arguments = (Map<String, Object>) map.remove("arguments");
if (arguments != null) {
Expand All @@ -524,13 +550,23 @@ static BulkRequest toBulkRequest(String requestPayload, HttpServletRequest httpS
if (targets.isEmpty()) {
throw new BadRequestException("request contains no targets.");
}
request.setTarget(targets);

List<String> paths = targets.stream()
.map(t -> rootPath.chroot(t).toString())
.collect(Collectors.toList());

LOGGER.error("targets {}", paths);

request.setTarget(paths);

String string = removeEntry(map, String.class, "activity");
request.setActivity(string);

string = removeEntry(map, String.class, "target_prefix", "target-prefix",
"targetPrefix");

LOGGER.error("target_prefix {}", string);

if (httpServletRequest != null) {
request.setTargetPrefix(getUserRootAwareTargetPrefix(httpServletRequest, string, handler));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING

import static org.dcache.restful.resources.bulk.BulkResources.toBulkRequest;
import static org.junit.Assert.assertEquals;
import static diskCacheV111.util.FsPath.create;

import diskCacheV111.util.CacheException;
import diskCacheV111.util.PnfsHandler;
Expand Down Expand Up @@ -182,6 +183,6 @@ private void givenJsonWithArrayTargetAttribute() {
}

private void whenParsed() {
bulkRequest = toBulkRequest(requestJson, null, handler);
bulkRequest = toBulkRequest(requestJson, null, handler, create("/"));
}
}

0 comments on commit 3a47f42

Please sign in to comment.