Skip to content
Snippets Groups Projects
Commit 9a87b926 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Handled special chars and added some logging

parent 32994748
Branches
Tags
No related merge requests found
Pipeline #1013 passed
package it.inaf.oats.vospace; package it.inaf.oats.vospace;
import it.inaf.oats.vospace.datamodel.NodeUtils; import it.inaf.oats.vospace.datamodel.NodeUtils;
import it.inaf.oats.vospace.exception.InvalidURIException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -9,20 +10,16 @@ public abstract class BaseNodeController { ...@@ -9,20 +10,16 @@ public abstract class BaseNodeController {
@Autowired @Autowired
private HttpServletRequest servletRequest; private HttpServletRequest servletRequest;
protected String getPath() { protected String getPath() {
String requestURL = servletRequest.getRequestURL().toString(); String requestURL = servletRequest.getRequestURL().toString();
try {
return NodeUtils.getPathFromRequestURLString(requestURL); return NodeUtils.getPathFromRequestURLString(requestURL);
} catch (IllegalArgumentException ex) {
throw new InvalidURIException(ex);
}
} }
protected String getParentPath(String path) { protected String getParentPath(String path) {
return NodeUtils.getParentPath(path); return NodeUtils.getParentPath(path);
} }
} }
package it.inaf.oats.vospace; package it.inaf.oats.vospace;
import it.inaf.ia2.aa.data.User; import it.inaf.ia2.aa.data.User;
import it.inaf.oats.vospace.datamodel.NodeProperties;
import it.inaf.oats.vospace.datamodel.NodeUtils; import it.inaf.oats.vospace.datamodel.NodeUtils;
import net.ivoa.xml.vospace.v2.Node; import net.ivoa.xml.vospace.v2.Node;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -9,15 +8,17 @@ import org.springframework.web.bind.annotation.RequestBody; ...@@ -9,15 +8,17 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import it.inaf.oats.vospace.persistence.NodeDAO; import it.inaf.oats.vospace.persistence.NodeDAO;
import java.util.List;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.PutMapping;
import it.inaf.oats.vospace.exception.*; import it.inaf.oats.vospace.exception.*;
import java.util.stream.Collectors; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RestController @RestController
public class CreateNodeController extends BaseNodeController { public class CreateNodeController extends BaseNodeController {
private static final Logger LOG = LoggerFactory.getLogger(CreateNodeController.class);
@Autowired @Autowired
private NodeDAO nodeDao; private NodeDAO nodeDao;
...@@ -31,6 +32,8 @@ public class CreateNodeController extends BaseNodeController { ...@@ -31,6 +32,8 @@ public class CreateNodeController extends BaseNodeController {
String path = getPath(); String path = getPath();
LOG.debug("createNode called for path {}", path);
// Validate payload node URI // Validate payload node URI
if (!isValidURI(node.getUri())) { if (!isValidURI(node.getUri())) {
throw new InvalidURIException(node.getUri()); throw new InvalidURIException(node.getUri());
...@@ -93,9 +96,5 @@ public class CreateNodeController extends BaseNodeController { ...@@ -93,9 +96,5 @@ public class CreateNodeController extends BaseNodeController {
// form vos://authority[!~]somepath/mynode..." // form vos://authority[!~]somepath/mynode..."
return nodeURI.replaceAll("vos://[^/]+", "").equals(path); return nodeURI.replaceAll("vos://[^/]+", "").equals(path);
} }
} }
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.inaf.oats.vospace; package it.inaf.oats.vospace;
import it.inaf.ia2.aa.data.User; import it.inaf.ia2.aa.data.User;
import it.inaf.oats.vospace.datamodel.NodeProperties;
import it.inaf.oats.vospace.datamodel.NodeUtils; import it.inaf.oats.vospace.datamodel.NodeUtils;
import it.inaf.oats.vospace.exception.ContainerNotFoundException;
import it.inaf.oats.vospace.exception.InternalFaultException;
import it.inaf.oats.vospace.exception.LinkFoundException; import it.inaf.oats.vospace.exception.LinkFoundException;
import it.inaf.oats.vospace.exception.NodeNotFoundException; import it.inaf.oats.vospace.exception.NodeNotFoundException;
import it.inaf.oats.vospace.exception.PermissionDeniedException; import it.inaf.oats.vospace.exception.PermissionDeniedException;
import it.inaf.oats.vospace.persistence.NodeDAO; import it.inaf.oats.vospace.persistence.NodeDAO;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import net.ivoa.xml.vospace.v2.Node; import net.ivoa.xml.vospace.v2.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class DeleteNodeController extends BaseNodeController { public class DeleteNodeController extends BaseNodeController {
private static final Logger LOG = LoggerFactory.getLogger(DeleteNodeController.class);
@Autowired @Autowired
private NodeDAO nodeDAO; private NodeDAO nodeDAO;
...@@ -39,6 +32,7 @@ public class DeleteNodeController extends BaseNodeController { ...@@ -39,6 +32,7 @@ public class DeleteNodeController extends BaseNodeController {
public ResponseEntity<String> deleteNode(HttpServletRequest request, User principal) { public ResponseEntity<String> deleteNode(HttpServletRequest request, User principal) {
String path = getPath(); String path = getPath();
LOG.debug("deleteNode called for path {}", path);
// Check if the node is present, // Check if the node is present,
// if the node does not exist the service SHALL throw a HTTP 404 status code // if the node does not exist the service SHALL throw a HTTP 404 status code
...@@ -53,7 +47,7 @@ public class DeleteNodeController extends BaseNodeController { ...@@ -53,7 +47,7 @@ public class DeleteNodeController extends BaseNodeController {
// status code including a LinkFound fault in the entity-body if either /a // status code including a LinkFound fault in the entity-body if either /a
// or /a/b are LinkNodes. // or /a/b are LinkNodes.
List<String> pathComponents = NodeUtils.subPathComponents(path); List<String> pathComponents = NodeUtils.subPathComponents(path);
if (pathComponents.size() == 0) { if (pathComponents.isEmpty()) {
// Manage root node // Manage root node
throw new PermissionDeniedException("root"); throw new PermissionDeniedException("root");
......
...@@ -10,11 +10,15 @@ import net.ivoa.xml.vospace.v2.Node; ...@@ -10,11 +10,15 @@ import net.ivoa.xml.vospace.v2.Node;
import it.inaf.oats.vospace.persistence.NodeDAO; import it.inaf.oats.vospace.persistence.NodeDAO;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@RestController @RestController
public class ListNodeController extends BaseNodeController { public class ListNodeController extends BaseNodeController {
private static final Logger LOG = LoggerFactory.getLogger(ListNodeController.class);
@Autowired @Autowired
private NodeDAO nodeDAO; private NodeDAO nodeDAO;
...@@ -22,6 +26,7 @@ public class ListNodeController extends BaseNodeController { ...@@ -22,6 +26,7 @@ public class ListNodeController extends BaseNodeController {
produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_XML_VALUE}) produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_XML_VALUE})
public ResponseEntity<Node> listNode(HttpServletRequest request) { public ResponseEntity<Node> listNode(HttpServletRequest request) {
String path = getPath(); String path = getPath();
LOG.debug("listNode called for path {}", path);
return ResponseEntity.ok(nodeDAO.listNode(path) return ResponseEntity.ok(nodeDAO.listNode(path)
.orElseThrow(() -> new NodeNotFoundException(path))); .orElseThrow(() -> new NodeNotFoundException(path)));
} }
......
...@@ -4,6 +4,8 @@ import it.inaf.ia2.aa.ServletRapClient; ...@@ -4,6 +4,8 @@ import it.inaf.ia2.aa.ServletRapClient;
import it.inaf.ia2.aa.data.User; import it.inaf.ia2.aa.data.User;
import it.inaf.ia2.rap.client.call.TokenExchangeRequest; import it.inaf.ia2.rap.client.call.TokenExchangeRequest;
import it.inaf.oats.vospace.datamodel.NodeProperties; import it.inaf.oats.vospace.datamodel.NodeProperties;
import static it.inaf.oats.vospace.datamodel.NodeUtils.urlEncodePath;
import it.inaf.oats.vospace.exception.NodeNotFoundException;
import it.inaf.oats.vospace.persistence.NodeDAO; import it.inaf.oats.vospace.persistence.NodeDAO;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -65,13 +67,11 @@ public class UriService { ...@@ -65,13 +67,11 @@ public class UriService {
String relativePath = transfer.getTarget().substring("vos://".length() + authority.length()); String relativePath = transfer.getTarget().substring("vos://".length() + authority.length());
// TODO handle node not found Node node = nodeDao.listNode(relativePath).orElseThrow(() -> new NodeNotFoundException(relativePath));
Node node = nodeDao.listNode(relativePath).get();
// TODO build the path according to node type // TODO build the path according to node type
// //
// TODO add token for authenticated access String endpoint = fileServiceUrl + urlEncodePath(relativePath) + "?jobId=" + job.getJobId();
String endpoint = fileServiceUrl + relativePath + "?jobId=" + job.getJobId();
if (!"true".equals(NodeProperties.getNodePropertyByURI(node, NodeProperties.PUBLIC_READ_URI))) { if (!"true".equals(NodeProperties.getNodePropertyByURI(node, NodeProperties.PUBLIC_READ_URI))) {
endpoint += "&token=" + getEndpointToken(fileServiceUrl + relativePath); endpoint += "&token=" + getEndpointToken(fileServiceUrl + relativePath);
......
...@@ -7,12 +7,15 @@ import org.springframework.web.bind.annotation.ResponseStatus; ...@@ -7,12 +7,15 @@ import org.springframework.web.bind.annotation.ResponseStatus;
public class InvalidURIException extends VoSpaceException { public class InvalidURIException extends VoSpaceException {
public InvalidURIException(String URI, String path) { public InvalidURIException(String URI, String path) {
super("InvalidURI. Payload node URI: " + URI + super("InvalidURI. Payload node URI: " + URI
" is not consistent with request path: " + path); + " is not consistent with request path: " + path);
} }
public InvalidURIException(String URI) public InvalidURIException(String URI) {
{
super("InvalidURI. URI: " + URI + " is not in a valid format"); super("InvalidURI. URI: " + URI + " is not in a valid format");
} }
public InvalidURIException(IllegalArgumentException ex) {
super("InvalidURI. " + ex.getMessage());
}
} }
...@@ -2,6 +2,7 @@ package it.inaf.oats.vospace; ...@@ -2,6 +2,7 @@ package it.inaf.oats.vospace;
import it.inaf.oats.vospace.persistence.NodeDAO; import it.inaf.oats.vospace.persistence.NodeDAO;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import net.ivoa.xml.vospace.v2.Property; import net.ivoa.xml.vospace.v2.Property;
import net.ivoa.xml.vospace.v2.UnstructuredDataNode; import net.ivoa.xml.vospace.v2.UnstructuredDataNode;
...@@ -23,9 +24,9 @@ import org.springframework.boot.test.mock.mockito.SpyBean; ...@@ -23,9 +24,9 @@ import org.springframework.boot.test.mock.mockito.SpyBean;
import net.ivoa.xml.vospace.v2.ContainerNode; import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.LinkNode; import net.ivoa.xml.vospace.v2.LinkNode;
import java.util.List; import java.util.List;
import it.inaf.ia2.aa.data.User;
import java.util.Optional; import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.TestPropertySource;
...@@ -83,12 +84,6 @@ public class CreateNodeControllerTest { ...@@ -83,12 +84,6 @@ public class CreateNodeControllerTest {
return parentNode; return parentNode;
} }
private User getUser() {
User user = new User();
user.setGroups(List.of("test1"));
return user;
}
@Test @Test
public void testFromJsonToXml() throws Exception { public void testFromJsonToXml() throws Exception {
String requestBody String requestBody
...@@ -300,6 +295,24 @@ public class CreateNodeControllerTest { ...@@ -300,6 +295,24 @@ public class CreateNodeControllerTest {
assertEquals("/mydata1", argCaptor.getAllValues().get(1)); assertEquals("/mydata1", argCaptor.getAllValues().get(1));
} }
@Test
public void testIllegalChars() throws Exception {
String requestBody = getResourceFileContent("create-unstructured-data-node.xml")
.replace("/mydata1", "/mydata1/?anothernode");
String message = mockMvc.perform(put(new URI("/nodes/mydata1/%3Fanothernode"))
.header("Authorization", "Bearer user2_token")
.content(requestBody)
.contentType(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(status().isBadRequest())
.andReturn().getResolvedException().getMessage();
assertTrue(message.contains("contains an illegal character"));
}
private void verifyArguments() { private void verifyArguments() {
verify(controller).createNode( verify(controller).createNode(
argThat(node -> { argThat(node -> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment