added Export functionalities
This commit is contained in:
parent
994608da26
commit
1736417057
|
@ -27,6 +27,8 @@ services:
|
|||
ports:
|
||||
- '8081:8080'
|
||||
- '5005:5005'
|
||||
volumes:
|
||||
- /tmp:/tomcat/temp
|
||||
|
||||
minio:
|
||||
image: minio/minio
|
||||
|
|
|
@ -74,20 +74,20 @@ public class AuthorizationChecker {
|
|||
//SharedFolder parentShared = node2Item.getItem(retrieveSharedFolderParent(node, session), Excludes.EXCLUDE_ACCOUNTING);
|
||||
//if (parentShared.getUsers().getMap().keySet().contains(userToCheck)) return;
|
||||
//CHECKING ACL FOR VREFOLDER AND SHARED FOLDER
|
||||
List<ACL> acls = aclManager.get(item, session);
|
||||
List<ACL> acls = aclManager.getByItem(item, session);
|
||||
UserManager userManager = ((JackrabbitSession) session).getUserManager();
|
||||
Authorizable userAuthorizable = userManager.getAuthorizable(userToCheck);
|
||||
for (ACL entry: acls) {
|
||||
log.debug("checking access right for {} with compared with {}",userToCheck, entry.getPricipal());
|
||||
Authorizable authorizable = userManager.getAuthorizable(entry.getPricipal());
|
||||
log.debug("checking access right for {} with compared with {}",userToCheck, entry.getPrincipal());
|
||||
Authorizable authorizable = userManager.getAuthorizable(entry.getPrincipal());
|
||||
|
||||
if (authorizable==null) {
|
||||
log.warn("{} doesn't have a correspondant auhtorizable object, check it ", entry.getPricipal());
|
||||
log.warn("{} doesn't have a correspondant auhtorizable object, check it ", entry.getPrincipal());
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
if (!authorizable.isGroup() && entry.getPricipal().equals(userToCheck)) return;
|
||||
if (!authorizable.isGroup() && entry.getPrincipal().equals(userToCheck)) return;
|
||||
if (authorizable.isGroup() && ((Group) authorizable).isMember(userAuthorizable)) return;
|
||||
}catch (Throwable e) {
|
||||
log.warn("someting went wrong checking authorizations",e);
|
||||
|
@ -136,13 +136,13 @@ public class AuthorizationChecker {
|
|||
if (item.isShared()) {
|
||||
|
||||
//CHECKING ACL FOR VREFOLDER AND SHARED FOLDER
|
||||
List<ACL> acls = aclManager.get(item, session);
|
||||
List<ACL> acls = aclManager.getByItem(item, session);
|
||||
UserManager userManager = ((JackrabbitSession) session).getUserManager();
|
||||
Authorizable UserAuthorizable = userManager.getAuthorizable(userToCheck);
|
||||
//put it in a different method
|
||||
for (ACL entry: acls) {
|
||||
Authorizable authorizable = userManager.getAuthorizable(entry.getPricipal());
|
||||
if ((!authorizable.isGroup() && entry.getPricipal().equals(userToCheck)) || (authorizable.isGroup() && ((Group) authorizable).isMember(UserAuthorizable))){
|
||||
Authorizable authorizable = userManager.getAuthorizable(entry.getPrincipal());
|
||||
if ((!authorizable.isGroup() && entry.getPrincipal().equals(userToCheck)) || (authorizable.isGroup() && ((Group) authorizable).isMember(UserAuthorizable))){
|
||||
for (AccessType privilege : entry.getAccessTypes()){
|
||||
if (isNewItem && privilege!=AccessType.READ_ONLY)
|
||||
return;
|
||||
|
@ -194,10 +194,10 @@ public class AuthorizationChecker {
|
|||
if (item==null) throw new UserNotAuthorizedException("Insufficent Privileges for user "+userToCheck+": it's not a valid StorageHub node");
|
||||
|
||||
if (item.isShared()) {
|
||||
List<ACL> acls = aclManager.get(item, session);
|
||||
List<ACL> acls = aclManager.getByItem(item, session);
|
||||
|
||||
for (ACL entry: acls) {
|
||||
if (entry.getPricipal().equals(userToCheck)) {
|
||||
if (entry.getPrincipal().equals(userToCheck)) {
|
||||
for (AccessType privilege : entry.getAccessTypes()){
|
||||
if (privilege==AccessType.ADMINISTRATOR)
|
||||
return;
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package org.gcube.data.access.storagehub.handlers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.jcr.Node;
|
||||
|
@ -14,7 +17,11 @@ import org.gcube.common.storagehub.model.exceptions.StorageHubException;
|
|||
import org.gcube.common.storagehub.model.exporter.DumpData;
|
||||
import org.gcube.common.storagehub.model.exporter.GroupData;
|
||||
import org.gcube.common.storagehub.model.exporter.UserData;
|
||||
import org.gcube.common.storagehub.model.items.FolderItem;
|
||||
import org.gcube.common.storagehub.model.items.Item;
|
||||
import org.gcube.common.storagehub.model.types.SHUBUser;
|
||||
import org.gcube.data.access.storagehub.PathUtil;
|
||||
import org.gcube.data.access.storagehub.Utils;
|
||||
import org.gcube.data.access.storagehub.handlers.vres.VREManager;
|
||||
import org.gcube.data.access.storagehub.services.delegates.ACLManagerDelegate;
|
||||
import org.gcube.data.access.storagehub.services.delegates.GroupManagerDelegate;
|
||||
|
@ -39,15 +46,20 @@ public class DataHandler {
|
|||
@Inject
|
||||
ACLManagerDelegate aclHandler;
|
||||
|
||||
@Inject
|
||||
PathUtil pathUtil;
|
||||
|
||||
@Inject
|
||||
VREManager vreManager;
|
||||
|
||||
public DumpData exportData(JackrabbitSession session) throws RepositoryException,StorageHubException {
|
||||
DumpData data = new DumpData();
|
||||
List<UserData> usersData = userHandler.getAllUsers(session).stream().map(u -> new UserData(u.getUserName()))
|
||||
|
||||
List<SHUBUser> users = userHandler.getAllUsers(session);
|
||||
|
||||
List<UserData> usersData = users.stream().map(u -> new UserData(u.getUserName()))
|
||||
.collect(Collectors.toList());
|
||||
data.setUsers(usersData);
|
||||
|
||||
|
||||
List<String> groups = groupHandler.getGroups(session);
|
||||
|
||||
logger.debug("found users {} ",usersData);
|
||||
|
@ -58,12 +70,12 @@ public class DataHandler {
|
|||
logger.debug("searching for group {}",group);
|
||||
Item vreFolderItem = vreManager.getVreFolderItemByGroupName(session, group, null).getVreFolder();
|
||||
String owner = vreFolderItem.getOwner();
|
||||
List<ACL> acls = aclHandler.get(vreFolderItem, session);
|
||||
List<ACL> acls = aclHandler.getByItem(vreFolderItem, session);
|
||||
|
||||
AccessType accessType = AccessType.READ_ONLY;
|
||||
List<ACL> otherAccess = new ArrayList<ACL>(acls.size() - 1);
|
||||
for (ACL acl : acls)
|
||||
if (acl.getPricipal().equals(group))
|
||||
if (acl.getPrincipal().equals(group))
|
||||
accessType = acl.getAccessTypes().get(0);
|
||||
else
|
||||
otherAccess.add(acl);
|
||||
|
@ -72,9 +84,37 @@ public class DataHandler {
|
|||
|
||||
groupsData.add(new GroupData(group, owner, members, accessType, otherAccess));
|
||||
}
|
||||
|
||||
Map<String, List<Item>> itemsPerUser = new HashMap<String, List<Item>>();
|
||||
for (SHUBUser user : users ) {
|
||||
logger.debug("getting all the files in {} workspace folder ",user.getUserName());
|
||||
String homePath = pathUtil.getWorkspacePath(user.getUserName()).toPath();
|
||||
Node homeNode = session.getNode(homePath);
|
||||
List<Item> items = Utils.getItemList(homeNode, Collections.emptyList(), null, true, null);
|
||||
items.forEach(i -> i.setParentId(null));
|
||||
itemsPerUser.put(user.getUserName(), retrieveSubItems(items));
|
||||
|
||||
}
|
||||
|
||||
data.setUsers(usersData);
|
||||
data.setGroups(groupsData);
|
||||
data.setItemPerUser(itemsPerUser);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
List<Item> retrieveSubItems(List<Item> items) throws StorageHubException, RepositoryException {
|
||||
List<Item> toReturn = new ArrayList<Item>();
|
||||
for (Item item: items )
|
||||
if (item instanceof FolderItem f) {
|
||||
if (f.isShared()) continue;
|
||||
toReturn.addAll(retrieveSubItems(Utils.getItemList((Node) f.getRelatedNode(), Collections.emptyList(), null, true, null)));
|
||||
} else
|
||||
toReturn.add(item);
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
public void importData(JackrabbitSession session, DumpData data) {
|
||||
data.getUsers().forEach(u -> {
|
||||
|
@ -98,7 +138,7 @@ public class DataHandler {
|
|||
Item vreFolderItem = vreManager.getVreFolderItemByGroupName(session, g.getName(), null).getVreFolder();
|
||||
|
||||
for (ACL acl : g.getAcls()) {
|
||||
aclHandler.update(acl.getPricipal(), (Node)vreFolderItem.getRelatedNode(), acl.getAccessTypes().get(0), session);
|
||||
aclHandler.update(acl.getPrincipal(), (Node)vreFolderItem.getRelatedNode(), acl.getAccessTypes().get(0), session);
|
||||
}
|
||||
|
||||
} catch (Throwable e) {
|
||||
|
|
|
@ -104,7 +104,7 @@ public class ACLManager extends Impersonable {
|
|||
ses = repository.getRepository().login(Constants.JCR_CREDENTIALS);
|
||||
Item item = node2Item.getItem(ses.getNodeByIdentifier(id), Excludes.ALL);
|
||||
authChecker.checkReadAuthorizationControl(ses, currentUser, id);
|
||||
return new ACLList(aclManagerDelegate.get(item, ses));
|
||||
return new ACLList(aclManagerDelegate.getByItem(item, ses));
|
||||
}catch(RepositoryException re){
|
||||
log.error("jcr error getting acl", re);
|
||||
throw new WebApplicationException(new BackendGenericError("jcr error getting acl", re));
|
||||
|
|
|
@ -8,22 +8,18 @@ import org.gcube.common.security.providers.SecretManagerProvider;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.context.RequestScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
|
||||
@Path("")
|
||||
@ApplicationScoped
|
||||
public abstract class Impersonable {
|
||||
|
||||
Logger log = LoggerFactory.getLogger(Impersonable.class);
|
||||
|
||||
String currentUser;
|
||||
|
||||
String currentUser = null;
|
||||
|
||||
@RequestScoped
|
||||
@Inject
|
||||
public void setCurrentUser(@Context final HttpServletRequest request) {
|
||||
String impersonate = request!=null ? request.getParameter("impersonate") : null ;
|
||||
|
|
|
@ -63,7 +63,6 @@ import org.slf4j.LoggerFactory;
|
|||
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
|
||||
import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
|
||||
|
||||
import jakarta.enterprise.context.RequestScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.servlet.ServletContext;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
|
@ -95,7 +94,6 @@ public class ItemsManager extends Impersonable{
|
|||
@Inject
|
||||
AccountingHandler accountingHandler;
|
||||
|
||||
@RequestScoped
|
||||
@PathParam("id")
|
||||
String id;
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ import org.gcube.common.gxrest.response.outbound.GXOutboundErrorResponse;
|
|||
import org.gcube.common.storagehub.model.exceptions.BackendGenericError;
|
||||
import org.gcube.common.storagehub.model.exceptions.IdNotFoundException;
|
||||
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
|
||||
import org.gcube.common.storagehub.model.service.UsersList;
|
||||
import org.gcube.common.storagehub.model.types.SHUBUser;
|
||||
import org.gcube.data.access.storagehub.Constants;
|
||||
import org.gcube.data.access.storagehub.StorageHubApplicationManager;
|
||||
|
@ -55,12 +56,12 @@ public class UserManager {
|
|||
@GET
|
||||
@Path("")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public List<SHUBUser> getUsers() {
|
||||
public UsersList getUsers() {
|
||||
InnerMethodName.set("getUsers");
|
||||
JackrabbitSession session = null;
|
||||
try {
|
||||
session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS);
|
||||
return userHandler.getAllUsers(session);
|
||||
return new UsersList(userHandler.getAllUsers(session));
|
||||
} catch (Throwable e) {
|
||||
log.error("jcr error getting users", e);
|
||||
GXOutboundErrorResponse.throwException(new BackendGenericError(e));
|
||||
|
@ -73,6 +74,7 @@ public class UserManager {
|
|||
|
||||
@GET
|
||||
@Path("{user}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public SHUBUser getUser(@PathParam("user") String user) {
|
||||
|
||||
InnerMethodName.set("getUser");
|
||||
|
|
|
@ -4,9 +4,11 @@ import static org.gcube.data.access.storagehub.Roles.INFRASTRUCTURE_MANAGER_ROLE
|
|||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
|
@ -144,13 +146,9 @@ public class ScriptManager {
|
|||
session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS);
|
||||
DumpData dd = dataHandler.exportData(session);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
status.setSuccess(om.writeValueAsString(dd));
|
||||
log.info("""
|
||||
export finished with result
|
||||
---------------------------
|
||||
{}
|
||||
---------------------------
|
||||
""", status.getResult());
|
||||
File result = Files.createTempFile("export",".json").toFile();
|
||||
status.setSuccess(result.getAbsolutePath());
|
||||
om.writeValue(result, dd);
|
||||
} catch (Throwable t) {
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw, true);
|
||||
|
|
|
@ -36,7 +36,7 @@ public class ACLManagerDelegate implements ACLManagerInterface {
|
|||
private static final Logger log = LoggerFactory.getLogger(ACLManagerDelegate.class);
|
||||
|
||||
@Override
|
||||
public List<ACL> get(Item item, Session session) throws RepositoryException, BackendGenericError {
|
||||
public List<ACL> getByItem(Item item, Session session) throws RepositoryException, BackendGenericError {
|
||||
List<ACL> acls = new ArrayList<>();
|
||||
if (!item.isShared()) return acls;
|
||||
|
||||
|
@ -44,12 +44,16 @@ public class ACLManagerDelegate implements ACLManagerInterface {
|
|||
if (!(item instanceof SharedFolder))
|
||||
toRetrieve = retrieveSharedFolderParent(toRetrieve, session);
|
||||
|
||||
String path = item.getPath();
|
||||
|
||||
JackrabbitAccessControlList accessControlList = AccessControlUtils.getAccessControlList(session, path );
|
||||
return get(toRetrieve, session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ACL> get(Node node, Session session) throws RepositoryException, BackendGenericError {
|
||||
List<ACL> acls = new ArrayList<>();
|
||||
JackrabbitAccessControlList accessControlList = AccessControlUtils.getAccessControlList(session, node.getPath() );
|
||||
for (AccessControlEntry aclEntry : accessControlList.getAccessControlEntries()) {
|
||||
ACL acl = new ACL();
|
||||
acl.setPricipal(aclEntry.getPrincipal().getName());
|
||||
acl.setPrincipal(aclEntry.getPrincipal().getName());
|
||||
List<AccessType> types = new ArrayList<>();
|
||||
for (Privilege priv : aclEntry.getPrivileges())
|
||||
try {
|
||||
|
@ -62,7 +66,7 @@ public class ACLManagerDelegate implements ACLManagerInterface {
|
|||
}
|
||||
return acls;
|
||||
}
|
||||
|
||||
|
||||
private Node retrieveSharedFolderParent(Node node, Session session) throws BackendGenericError, RepositoryException{
|
||||
if (node2Item.checkNodeType(node, SharedFolder.class)) return node;
|
||||
else
|
||||
|
|
|
@ -28,6 +28,7 @@ import org.gcube.common.security.ContextBean.Type;
|
|||
import org.gcube.common.security.providers.SecretManagerProvider;
|
||||
import org.gcube.common.storagehub.model.Excludes;
|
||||
import org.gcube.common.storagehub.model.Paths;
|
||||
import org.gcube.common.storagehub.model.acls.ACL;
|
||||
import org.gcube.common.storagehub.model.acls.AccessType;
|
||||
import org.gcube.common.storagehub.model.exceptions.InvalidCallParameters;
|
||||
import org.gcube.common.storagehub.model.exceptions.NotFoundException;
|
||||
|
@ -164,16 +165,9 @@ public class GroupManagerDelegate {
|
|||
|
||||
Node vreFolder = this.getFolderNodeRelatedToGroup(session, groupId);
|
||||
|
||||
org.apache.jackrabbit.api.security.user.UserManager usrManager = ((JackrabbitSession) session).getUserManager();
|
||||
|
||||
Group group = (Group) usrManager.getAuthorizable(groupId);
|
||||
User authUser = (User) usrManager.getAuthorizable(userId);
|
||||
|
||||
if (group == null)
|
||||
throw new NotFoundException("group", groupId);
|
||||
if (authUser == null)
|
||||
throw new NotFoundException("user", userId);
|
||||
|
||||
aclManagerDelegate.delete(userId, vreFolder, session);
|
||||
|
||||
/*
|
||||
AccessControlManager acm = session.getAccessControlManager();
|
||||
JackrabbitAccessControlList acls = AccessControlUtils.getAccessControlList(acm, vreFolder.getPath());
|
||||
|
||||
|
@ -186,24 +180,21 @@ public class GroupManagerDelegate {
|
|||
|
||||
acls.removeAccessControlEntry(toRemove);
|
||||
acm.setPolicy(vreFolder.getPath(), acls);
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
public List<String> getGroupAdministators(JackrabbitSession session, String groupId) throws Throwable {
|
||||
|
||||
List<String> users = new ArrayList<String>();
|
||||
|
||||
Node node = getFolderNodeRelatedToGroup(session, groupId);
|
||||
AccessControlManager acm = session.getAccessControlManager();
|
||||
|
||||
JackrabbitAccessControlList acls = AccessControlUtils.getAccessControlList(acm, node.getPath());
|
||||
|
||||
for (AccessControlEntry acl : acls.getAccessControlEntries())
|
||||
for (Privilege pr : acl.getPrivileges()) {
|
||||
if (pr.getName().equals(AccessType.ADMINISTRATOR.getValue())) {
|
||||
users.add(acl.getPrincipal().getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
List<ACL> acls = aclManagerDelegate.get(node, session);
|
||||
|
||||
for (ACL acl : acls)
|
||||
for (AccessType pr : acl.getAccessTypes())
|
||||
if (pr == AccessType.ADMINISTRATOR) users.add(acl.getPrincipal());
|
||||
|
||||
return users;
|
||||
}
|
||||
|
|
|
@ -23,8 +23,10 @@ public interface ACLManagerInterface {
|
|||
* @exception {@link RepositoryException} when a generic jcr error occurs
|
||||
* @exception {@link UserNotAuthorizedException} when the caller is not authorized to access to the shared folder
|
||||
*/
|
||||
List<ACL> get(Item item, Session session) throws RepositoryException, BackendGenericError;
|
||||
List<ACL> getByItem(Item item, Session session) throws RepositoryException, BackendGenericError;
|
||||
|
||||
List<ACL> get(Node node, Session session) throws RepositoryException, BackendGenericError;
|
||||
|
||||
/**
|
||||
* Set a new AccessType for a user in a shared folder or VRE folder
|
||||
*
|
||||
|
@ -42,4 +44,6 @@ public interface ACLManagerInterface {
|
|||
void delete(String targetUser, Node node, Session session)
|
||||
throws RepositoryException, StorageHubException;
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
package org.gcube.data.access.fs.external;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashSet;
|
||||
|
@ -19,240 +21,325 @@ import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
|||
import org.gcube.common.storagehub.model.acls.AccessType;
|
||||
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
|
||||
import org.gcube.common.storagehub.model.items.SharedFolder;
|
||||
import org.gcube.common.storagehub.model.types.SHUBUser;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
public class CreateUsers {
|
||||
|
||||
private static StorageHubClient client;
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(CreateUsers.class);
|
||||
|
||||
private String user ="pippo.test";
|
||||
private String user2 ="lucio.lelii";
|
||||
private String vreWA ="vre-write-all";
|
||||
private String vreRO ="vre-read-only";
|
||||
private String vreWO ="vre-write-owner";
|
||||
|
||||
private static final int TEST_USERS_NUMBER = 10;
|
||||
|
||||
private static StorageHubClient client;
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(CreateUsers.class);
|
||||
|
||||
private String user1 = "pippo.test";
|
||||
private String user2 = "lucio.lelii";
|
||||
private String vreWA = "vre-write-all";
|
||||
private String vreRO = "vre-read-only";
|
||||
private String vreWO = "vre-write-owner";
|
||||
|
||||
private static final int TEST_USERS_NUMBER = 5000;
|
||||
|
||||
@BeforeClass
|
||||
public static void before() throws Exception{
|
||||
public static void before() throws Exception {
|
||||
String address = "localhost";
|
||||
int port = 8081;
|
||||
URI storagehubUri = new URL(String.format("http://%s:%d/storagehub", address,port)).toURI();
|
||||
URI storagehubUri = new URL(String.format("http://%s:%d/storagehub", address, port)).toURI();
|
||||
client = new StorageHubClient(storagehubUri);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Before
|
||||
public void setSecret() {
|
||||
CredentialSecret secret = new CredentialSecret("sg4-test-client", "a156a7db-3b32-4cd5-b27b-2488e0e01698", "/gcube");
|
||||
SecretManagerProvider.set(secret);
|
||||
CredentialSecret secret = new CredentialSecret("sg4-test-client", "a156a7db-3b32-4cd5-b27b-2488e0e01698",
|
||||
"/gcube");
|
||||
SecretManagerProvider.set(secret);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initialiseEnvironment() throws Exception {
|
||||
this.createUser(user1);
|
||||
this.createAllVREsAndAddUser1AsAdmin();
|
||||
client.impersonate(user1);
|
||||
this.createUser(user2);
|
||||
this.addUser2ToAllVres();
|
||||
client.impersonate(user1);
|
||||
createRandomFolderAndFileForCurrentUser();
|
||||
createRandomFolderAndFileForCurrentUser();
|
||||
createRandomFolderAndFileForCurrentUser();
|
||||
|
||||
client.impersonate(user2);
|
||||
createRandomFolderAndFileForCurrentUser();
|
||||
createRandomFolderAndFileForCurrentUser();
|
||||
createRandomFolderAndFileForCurrentUser();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initialiseEnvironment() throws Exception {
|
||||
this.createUser();
|
||||
this.addUserToVREs();
|
||||
this.createUser2AndAddItToVres();
|
||||
public void getUsers() throws Exception {
|
||||
client.getUsers().forEach(u -> System.out.println(u.getUserName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getItemCountPerUsers() throws Exception {
|
||||
for (SHUBUser user : client.getUsers()) {
|
||||
client.impersonate(user.getUserName());
|
||||
int size = client.getWSRoot().list().getItems().size();
|
||||
System.out.println(String.format("%s has %d items", user.getUserName(), size));
|
||||
}
|
||||
}
|
||||
|
||||
//USED TO CREATE TEST USERS
|
||||
@Test
|
||||
public void removeUser2FromVre() throws Exception {
|
||||
client.getVreFolderManager(vreRO).removeUser(user2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createRandomFileAndFolderOnEveryUser() throws Exception {
|
||||
for (SHUBUser user : client.getUsers()) {
|
||||
client.impersonate(user.getUserName());
|
||||
String rootId = this.getUserRootId();
|
||||
for (int i = 0; i<=500; i++)
|
||||
this.createRandomFolderForCurrentUser(rootId);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createFileAndFolderUser2() throws Exception {
|
||||
client.impersonate(user2);
|
||||
for (int i = 0; i<=100; i++)
|
||||
createRandomFolderAndFileForCurrentUser();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createFileAndFolderFarAllUsers() throws Exception {
|
||||
client.impersonate(user2);
|
||||
for (int i = 0; i<=100; i++)
|
||||
createRandomFolderAndFileForCurrentUser();
|
||||
}
|
||||
|
||||
public void createRandomFolderAndFileForCurrentUser() throws Exception{
|
||||
String rootId = this.getUserRootId();
|
||||
this.uploadFileWithRandomNameForCurrentUser(rootId);
|
||||
FolderContainer fc = this.createRandomFolderForCurrentUser(rootId);
|
||||
this.uploadFileWithRandomNameForCurrentUser(fc.getId());
|
||||
}
|
||||
|
||||
// USED TO CREATE TEST USERS
|
||||
final String lexicon = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12345674890";
|
||||
|
||||
final java.util.Random rand = new java.util.Random();
|
||||
|
||||
// consider using a Map<String,Boolean> to say whether the identifier is being used or not
|
||||
// consider using a Map<String,Boolean> to say whether the identifier is being
|
||||
// used or not
|
||||
final Set<String> identifiers = new HashSet<String>();
|
||||
|
||||
public String randomIdentifier() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
while(builder.toString().length() == 0) {
|
||||
int length = rand.nextInt(5)+5;
|
||||
for(int i = 0; i < length; i++) {
|
||||
builder.append(lexicon.charAt(rand.nextInt(lexicon.length())));
|
||||
}
|
||||
if(identifiers.contains(builder.toString())) {
|
||||
builder = new StringBuilder();
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
while (builder.toString().length() == 0) {
|
||||
int length = rand.nextInt(5) + 5;
|
||||
for (int i = 0; i < length; i++) {
|
||||
builder.append(lexicon.charAt(rand.nextInt(lexicon.length())));
|
||||
}
|
||||
if (identifiers.contains(builder.toString())) {
|
||||
builder = new StringBuilder();
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
// END
|
||||
|
||||
// END
|
||||
|
||||
@Test
|
||||
public void createTestUsers() throws Exception{
|
||||
for (int i =0 ; i<TEST_USERS_NUMBER; i++)
|
||||
client.createUserAccount(randomIdentifier().toLowerCase());
|
||||
public void createTestUsers() throws Exception {
|
||||
for (int i = 0; i < TEST_USERS_NUMBER; i++)
|
||||
client.createUserAccount(String.format("%s.%s", randomIdentifier().toLowerCase(), randomIdentifier().toLowerCase() ));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void createAccountforManging() throws Exception{
|
||||
public void createAccountforManging() throws Exception {
|
||||
client.createUserAccount("service-account-sg4-test-client");
|
||||
client.getVreFolderManager(vreWA).addUser("service-account-sg4-test-client");
|
||||
client.getVreFolderManager(vreRO).addUser("service-account-sg4-test-client");
|
||||
client.getVreFolderManager(vreWO).addUser("service-account-sg4-test-client");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUserRootId() throws Exception{
|
||||
client.impersonate(user);
|
||||
System.out.println(client.getWSRoot().getId());
|
||||
|
||||
public String getUserRootId() throws Exception {
|
||||
return client.getWSRoot().getId();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getBearerToken() {
|
||||
System.out.println(SecretManagerProvider.get().getHTTPAuthorizationHeaders());
|
||||
System.out.println(SecretManagerProvider.get().getHTTPAuthorizationHeaders());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getFolderACLsOnrROVRE() throws Exception {
|
||||
client.impersonate(user);
|
||||
FolderContainer vre = (FolderContainer) client.getVREFolders().getContainers().stream().filter(f -> f.get().getTitle().equals(vreWA)).findFirst().get();
|
||||
client.impersonate(user1);
|
||||
FolderContainer vre = (FolderContainer) client.getVREFolders().getContainers().stream()
|
||||
.filter(f -> f.get().getTitle().equals(vreWA)).findFirst().get();
|
||||
FolderContainer folder = (FolderContainer) vre.list().getContainers().stream().findFirst().get();
|
||||
System.out.println(folder.get().getTitle()+" "+folder.getAcls());
|
||||
System.out.println(folder.get().getTitle() + " " + folder.getAcls());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void createUser() throws Exception{
|
||||
|
||||
public void createUser(String user) throws Exception {
|
||||
client.createUserAccount(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeUserFromGroup() throws Exception{
|
||||
|
||||
public void removeUserFromGroup(String user) throws Exception {
|
||||
client.getVreFolderManager(vreWA).removeAdmin(user);
|
||||
client.getVreFolderManager(vreWA).removeUser(user);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addUserToVREs() throws Exception{
|
||||
client.getVreFolderManager(vreWA).createVRE(AccessType.WRITE_ALL, user, false);
|
||||
client.getVreFolderManager(vreWA).setAdmin(user);
|
||||
client.getVreFolderManager(vreRO).createVRE(AccessType.READ_ONLY, user);
|
||||
client.getVreFolderManager(vreRO).setAdmin(user);
|
||||
client.getVreFolderManager(vreWO).createVRE(AccessType.WRITE_OWNER, user);
|
||||
client.getVreFolderManager(vreWO).setAdmin(user);
|
||||
createFolderByAdminInVREWO();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createFolderByAdminInVREWO() throws Exception {
|
||||
client.impersonate(user);
|
||||
retrieveVREbyName(vreWO).newFolder("test created by pippo", "");
|
||||
public void createVRE(String vre, AccessType accesstype, String owner) throws Exception {
|
||||
client.getVreFolderManager(vre).createVRE(accesstype, owner);
|
||||
}
|
||||
|
||||
public void setAdmin(String vre, String user) throws Exception {
|
||||
client.getVreFolderManager(vre).setAdmin(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createUser2AndAddItToVres() throws Exception{
|
||||
client.impersonate(user);
|
||||
client.createUserAccount(user2);
|
||||
client.getVreFolderManager(vreWA).addUser(user2);
|
||||
client.getVreFolderManager(vreRO).addUser(user2);
|
||||
client.getVreFolderManager(vreWO).addUser(user2);
|
||||
public void createAllVREsAndAddUser1AsAdmin() throws Exception {
|
||||
createVRE(vreWA, AccessType.WRITE_ALL, user1 );
|
||||
setAdmin(vreWA, user1);
|
||||
createVRE(vreRO, AccessType.READ_ONLY, user1 );
|
||||
setAdmin(vreWA, user1);
|
||||
createVRE(vreWO, AccessType.WRITE_OWNER, user1 );
|
||||
setAdmin(vreWA, user1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addUser2ToAllVres() throws Exception {
|
||||
addUserToVre(vreRO, user2);
|
||||
addUserToVre(vreWA, user2);
|
||||
addUserToVre(vreWO, user2);
|
||||
}
|
||||
|
||||
public void createFolderByAdmin(String vre) throws Exception {
|
||||
retrieveVREbyName(vre).newFolder("test created by admin", "");
|
||||
}
|
||||
|
||||
|
||||
public void addUserToVre(String vre, String user) throws Exception {
|
||||
client.getVreFolderManager(vre).addUser(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printVREFoldersAdmin() throws StorageHubException{
|
||||
System.out.println("launching ....." );
|
||||
List<FolderContainer> vreFolders = client.getVREFolders().getContainers().stream().map(c -> ((FolderContainer) c)).collect(Collectors.toList());
|
||||
public void printVREFoldersAdmin() throws StorageHubException {
|
||||
System.out.println("launching .....");
|
||||
List<FolderContainer> vreFolders = client.getVREFolders().getContainers().stream()
|
||||
.map(c -> ((FolderContainer) c)).collect(Collectors.toList());
|
||||
for (FolderContainer fc : vreFolders) {
|
||||
System.out.println(fc.get().getTitle());
|
||||
System.out.println(fc.getAcls());
|
||||
System.out.println(client.getVreFolderManager(fc.get().getTitle()).getAdmins());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getVREs() throws Exception{
|
||||
client.impersonate(user);
|
||||
client.getVREFolders().getItems().stream().map(i -> ((SharedFolder) i)).forEach(s -> System.out.println(s.getDisplayName()));
|
||||
public void getVREs() throws Exception {
|
||||
client.impersonate(user1);
|
||||
client.getVREFolders().getItems().stream().map(i -> ((SharedFolder) i))
|
||||
.forEach(s -> System.out.println(s.getDisplayName()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getWsRoot() throws Exception{
|
||||
client.impersonate(user);
|
||||
client.getWSRoot().list().getItems().forEach(i -> System.out.println(i.getId()+" "+i.getTitle()));
|
||||
public void getWsRoot() throws Exception {
|
||||
client.impersonate(user1);
|
||||
client.getWSRoot().list().getItems().forEach(i -> System.out.println(i.getId() + " " + i.getTitle()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void download() throws Exception{
|
||||
client.impersonate(user);
|
||||
try ( InputStream is = client.open("5b5236d2-ec88-49f2-a2c5-04ecf8284191").asFile().download().getStream()){
|
||||
String text = new BufferedReader(
|
||||
new InputStreamReader(is, StandardCharsets.UTF_8))
|
||||
.lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
public void download() throws Exception {
|
||||
client.impersonate(user1);
|
||||
try (InputStream is = client.open("5b5236d2-ec88-49f2-a2c5-04ecf8284191").asFile().download().getStream()) {
|
||||
String text = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void uploadFile() {
|
||||
client.impersonate(user);
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")){
|
||||
public void uploadImageTest1() {
|
||||
client.impersonate(user1);
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")) {
|
||||
client.getWSRoot().uploadFile(is, "test1.jpg", "a test");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private FolderContainer retrieveVREbyName(String name) throws StorageHubException {
|
||||
List<FolderContainer> vreFolders = client.getVREFolders().getContainers().stream().map(c -> ((FolderContainer) c)).collect(Collectors.toList());
|
||||
return vreFolders.stream().filter(c -> ((SharedFolder)c.get()).getDisplayName().equals(name)).findFirst().get();
|
||||
public void uploadFileWithRandomNameForCurrentUser(String folderId) throws URISyntaxException {
|
||||
long size = new File(CreateUsers.class.getResource("/pdf-test.pdf").toURI()).length();
|
||||
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/pdf-test.pdf")) {
|
||||
client.open(folderId).asFolder().uploadFile(is, randomIdentifier() + ".pdf", "a test", size);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public FolderContainer createRandomFolderForCurrentUser(String folderId) {
|
||||
try {
|
||||
return client.open(folderId).asFolder().newFolder(randomIdentifier(), "a test");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private FolderContainer retrieveVREbyName(String name) throws StorageHubException {
|
||||
List<FolderContainer> vreFolders = client.getVREFolders().getContainers().stream()
|
||||
.map(c -> ((FolderContainer) c)).collect(Collectors.toList());
|
||||
return vreFolders.stream().filter(c -> ((SharedFolder) c.get()).getDisplayName().equals(name)).findFirst()
|
||||
.get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uploadFileToVREWriteAll() throws StorageHubException{
|
||||
public void uploadFileToVREWriteAll() throws StorageHubException {
|
||||
client.impersonate(user2);
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")){
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")) {
|
||||
retrieveVREbyName(vreWA).uploadFile(is, "test1.jpg", "a test");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = StorageHubException.class)
|
||||
public void uploadFileToVREReadOnly() throws StorageHubException{
|
||||
public void uploadFileToVREReadOnly() throws StorageHubException {
|
||||
client.impersonate(user2);
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")){
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")) {
|
||||
retrieveVREbyName(vreRO).uploadFile(is, "test1.jpg", "a test");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void uploadFileToVREReadOnlyAdmin() throws StorageHubException{
|
||||
client.impersonate(user);
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")){
|
||||
public void uploadFileToVREReadOnlyAdmin() throws StorageHubException {
|
||||
client.impersonate(user1);
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")) {
|
||||
retrieveVREbyName(vreRO).uploadFile(is, "test1.jpg", "a test");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void uploadFileToVREWriteOwner() throws StorageHubException{
|
||||
public void uploadFileToVREWriteOwner() throws StorageHubException {
|
||||
client.impersonate(user2);
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")){
|
||||
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")) {
|
||||
retrieveVREbyName(vreWO).uploadFile(is, "test1.jpg", "a test");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void removeUser() throws Exception{
|
||||
client.deleteUserAccount(user);
|
||||
public void removeUser() throws Exception {
|
||||
client.deleteUserAccount(user1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue