Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/save-and-restore/app/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ the selected PV *around the point in time defined by the PV timestamp*.
Authentication and Authorization
--------------------------------

Authorization uses a role-based approach like so:
Authorization is optional (configured on the service) and uses a role-based approach like so:

* Unauthenticated users may read data, i.e. browse the tree and view configurations, snapshots, search and view filters.
* Role "user":
Expand All @@ -541,7 +541,8 @@ Authorization uses a role-based approach like so:
* Object is a composite snapshot node
* Object is configuration or folder node with no child nodes
* Object is a filter
* Role "superuser": +perform restore operation
* Perform restore operations
* Copy or move objects if the target node is "owned" by current user
* Role "admin": no restrictions

Roles are defined and managed on the service. Role (group) membership is managed in Active Directory or LDAP.
Expand Down
2 changes: 2 additions & 0 deletions services/save-and-restore/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,8 @@ Authorization uses a role-based approach like so:
* Object is configuration or folder node with no child nodes
* Object is a filter
* Object is a tag
* Perform restore operations
* Copy or move objects if the target node is "owned" by current user
* Save-and-restore role "sar-admin": no restrictions

Authentication endpoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.security.Principal;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -279,13 +282,22 @@ public boolean mayCreate(MethodSecurityExpressionOperations methodSecurityExpres

/**
* Checks if user is allowed to move or copy objects. This is the case if authorization
* is disabled or if user has admin role.
* is disabled, if the target {@link Node} was created by the user performing the copy/move operation,
* or if user has admin role.
*
* @param targetNodeId The id of the target {@link Node}
* @param methodSecurityExpressionOperations {@link MethodSecurityExpressionOperations} Spring managed object
* queried for authorization.
* @return <code>false</code> if user may not create the object, otherwise <code>true</code>.
*/
public boolean mayMoveOrCopy(MethodSecurityExpressionOperations methodSecurityExpressionOperations) {
return permitAll || methodSecurityExpressionOperations.hasAuthority(ROLE_PREFIX + roleAdmin);
public boolean mayMoveOrCopy(String targetNodeId, MethodSecurityExpressionOperations methodSecurityExpressionOperations) {
if(permitAll || methodSecurityExpressionOperations.hasAuthority(ROLE_PREFIX + roleAdmin)){
return true;
}

Node targetNode = nodeDAO.getNode(targetNodeId);

return targetNode.getUserName()
.equals(((UserDetails) methodSecurityExpressionOperations.getAuthentication().getPrincipal()).getUsername());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is username always the same as the owner?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public class StructureController extends BaseController {
*/
@SuppressWarnings("unused")
@PostMapping(value = "/move", produces = JSON)
@PreAuthorize("@authorizationHelper.mayMoveOrCopy(#root)")
@PreAuthorize("@authorizationHelper.mayMoveOrCopy(#to, #root)")
public Node moveNodes(@RequestParam(value = "to") String to,
@RequestBody List<String> nodes,
Principal principal) {
if (to.isEmpty() || nodes.isEmpty()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Target node and list of source nodes must all be non-empty.");
}
// Get parent node before move
Node sourceParentNode = nodeDAO.getParentNode(nodes.get(0));
Node sourceParentNode = nodeDAO.getParentNode(nodes.getFirst());
Logger.getLogger(StructureController.class.getName()).info(Thread.currentThread().getName() + " " + (new Date()) + " move");
Node targetNode = nodeDAO.moveNodes(nodes, to, principal.getName());
// Update clients
Expand All @@ -84,7 +84,7 @@ public Node moveNodes(@RequestParam(value = "to") String to,
}

/**
* Copies a list of source nodes to a target (parent) node. Since the source nodes may contain sub-trees at
* Copies a list of source nodes to a target (parent) node. Since the source nodes may contain subtrees at
* any depth, the copy operation needs to do a deep copy, which may take some time to complete.
*
* @param to The unique id of the target parent node, which must be a folder. If empty or if
Expand All @@ -96,7 +96,7 @@ public Node moveNodes(@RequestParam(value = "to") String to,
*/
@SuppressWarnings("unused")
@PostMapping(value = "/copy", produces = JSON)
@PreAuthorize("@authorizationHelper.mayMoveOrCopy(#root)")
@PreAuthorize("@authorizationHelper.mayMoveOrCopy(#to, #root)")
public Node copyNodes(@RequestParam(value = "to") String to,
@RequestBody List<String> nodes,
Principal principal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ void setUp() {
@Autowired
private String adminAuthorization;

@Autowired
private String readOnlyAuthorization;

@Autowired
private WebSocketService webSocketService;

Expand All @@ -85,7 +82,7 @@ public void resetMocks() {
}

@Test
public void testMoveNode1() throws Exception {
public void testMoveNodeAdmin() throws Exception {
when(nodeDAO.moveNodes(List.of("a"), "b", demoAdmin))
.thenReturn(Node.builder().uniqueId("2").uniqueId("a").userName(demoAdmin).build());

Expand All @@ -106,42 +103,40 @@ public void testMoveNode1() throws Exception {
}

@Test
public void testMoveNode2() throws Exception {
public void testMoveNodeUser() throws Exception {

when(nodeDAO.moveNodes(List.of("a"), "b", demoUser))
when(nodeDAO.moveNodes(List.of("a"), "target", demoUser))
.thenReturn(Node.builder().uniqueId("2").uniqueId("a").userName(demoUser).build());
when(nodeDAO.getNode("target"))
.thenReturn(Node.builder().uniqueId("target").userName("user").build());

MockHttpServletRequestBuilder request = post("/move")
.header(HttpHeaders.AUTHORIZATION, userAuthorization)
.contentType(JSON)
.content(objectMapper.writeValueAsString(List.of("a")))
.param("to", "b")
.param("to", "target")
.param("username", "username");

mockMvc.perform(request).andExpect(status().isForbidden());
mockMvc.perform(request).andExpect(status().isOk());

verify(webSocketService, times(0)).sendMessageToClients(Mockito.any(WebSocketMessage.class));
verify(webSocketService, times(2)).sendMessageToClients(Mockito.any(WebSocketMessage.class));
}

@Test
public void testMoveNode3() throws Exception {
public void testMoveNodeOtherUser() throws Exception {

when(nodeDAO.getNode("target"))
.thenReturn(Node.builder().uniqueId("target").userName("otheruser").build());

MockHttpServletRequestBuilder request = post("/move")
.header(HttpHeaders.AUTHORIZATION, readOnlyAuthorization)
.header(HttpHeaders.AUTHORIZATION, userAuthorization)
.contentType(JSON)
.content(objectMapper.writeValueAsString(List.of("a")))
.param("to", "b")
.param("to", "target")
.param("username", "username");

mockMvc.perform(request).andExpect(status().isForbidden());

request = post("/move")
.contentType(JSON)
.content(objectMapper.writeValueAsString(List.of("a")))
.param("to", "b")
.param("username", "username");

mockMvc.perform(request).andExpect(status().isUnauthorized());

verify(webSocketService, times(0)).sendMessageToClients(Mockito.any(WebSocketMessage.class));
}
Expand Down Expand Up @@ -175,15 +170,36 @@ public void testMoveNodeTargetIdEmpty() throws Exception {
}

@Test
public void testCopyNodes1() throws Exception {
public void testCopyNodesAdmin() throws Exception {
MockHttpServletRequestBuilder request = post("/copy")
.header(HttpHeaders.AUTHORIZATION, adminAuthorization)
.contentType(JSON)
.content(objectMapper.writeValueAsString(List.of("a")))
.param("to", "target");
mockMvc.perform(request).andExpect(status().isOk());
}

@Test
public void testCopyNodesUser() throws Exception{

when(nodeDAO.getNode("target"))
.thenReturn(Node.builder().uniqueId("target").userName("user").build());
MockHttpServletRequestBuilder request = post("/copy")
.header(HttpHeaders.AUTHORIZATION, userAuthorization)
.contentType(JSON)
.content(objectMapper.writeValueAsString(List.of("a")))
.param("to", "target");
mockMvc.perform(request).andExpect(status().isOk());

verify(webSocketService, times(0)).sendMessageToClients(Mockito.any(WebSocketMessage.class));
}

@Test
public void testCopyNodesOtherUser() throws Exception{

request = post("/copy")
when(nodeDAO.getNode("target"))
.thenReturn(Node.builder().uniqueId("target").userName("otheruser").build());
MockHttpServletRequestBuilder request = post("/copy")
.header(HttpHeaders.AUTHORIZATION, userAuthorization)
.contentType(JSON)
.content(objectMapper.writeValueAsString(List.of("a")))
Expand All @@ -194,7 +210,7 @@ public void testCopyNodes1() throws Exception {
}

@Test
public void testCopyNodes2() throws Exception {
public void testCopyNodesNotLoggedIn() throws Exception {

MockHttpServletRequestBuilder request = post("/copy")
.contentType(JSON)
Expand Down
Loading