added Export functionalities

This commit is contained in:
lucio 2024-07-17 11:41:33 +02:00
parent 994608da26
commit 1736417057
13 changed files with 317 additions and 195 deletions

View File

@ -27,6 +27,8 @@ services:
ports: ports:
- '8081:8080' - '8081:8080'
- '5005:5005' - '5005:5005'
volumes:
- /tmp:/tomcat/temp
minio: minio:
image: minio/minio image: minio/minio

View File

@ -74,20 +74,20 @@ public class AuthorizationChecker {
//SharedFolder parentShared = node2Item.getItem(retrieveSharedFolderParent(node, session), Excludes.EXCLUDE_ACCOUNTING); //SharedFolder parentShared = node2Item.getItem(retrieveSharedFolderParent(node, session), Excludes.EXCLUDE_ACCOUNTING);
//if (parentShared.getUsers().getMap().keySet().contains(userToCheck)) return; //if (parentShared.getUsers().getMap().keySet().contains(userToCheck)) return;
//CHECKING ACL FOR VREFOLDER AND SHARED FOLDER //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(); UserManager userManager = ((JackrabbitSession) session).getUserManager();
Authorizable userAuthorizable = userManager.getAuthorizable(userToCheck); Authorizable userAuthorizable = userManager.getAuthorizable(userToCheck);
for (ACL entry: acls) { for (ACL entry: acls) {
log.debug("checking access right for {} with compared with {}",userToCheck, entry.getPricipal()); log.debug("checking access right for {} with compared with {}",userToCheck, entry.getPrincipal());
Authorizable authorizable = userManager.getAuthorizable(entry.getPricipal()); Authorizable authorizable = userManager.getAuthorizable(entry.getPrincipal());
if (authorizable==null) { 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; continue;
} }
try { 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; if (authorizable.isGroup() && ((Group) authorizable).isMember(userAuthorizable)) return;
}catch (Throwable e) { }catch (Throwable e) {
log.warn("someting went wrong checking authorizations",e); log.warn("someting went wrong checking authorizations",e);
@ -136,13 +136,13 @@ public class AuthorizationChecker {
if (item.isShared()) { if (item.isShared()) {
//CHECKING ACL FOR VREFOLDER AND SHARED FOLDER //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(); UserManager userManager = ((JackrabbitSession) session).getUserManager();
Authorizable UserAuthorizable = userManager.getAuthorizable(userToCheck); Authorizable UserAuthorizable = userManager.getAuthorizable(userToCheck);
//put it in a different method //put it in a different method
for (ACL entry: acls) { for (ACL entry: acls) {
Authorizable authorizable = userManager.getAuthorizable(entry.getPricipal()); Authorizable authorizable = userManager.getAuthorizable(entry.getPrincipal());
if ((!authorizable.isGroup() && entry.getPricipal().equals(userToCheck)) || (authorizable.isGroup() && ((Group) authorizable).isMember(UserAuthorizable))){ if ((!authorizable.isGroup() && entry.getPrincipal().equals(userToCheck)) || (authorizable.isGroup() && ((Group) authorizable).isMember(UserAuthorizable))){
for (AccessType privilege : entry.getAccessTypes()){ for (AccessType privilege : entry.getAccessTypes()){
if (isNewItem && privilege!=AccessType.READ_ONLY) if (isNewItem && privilege!=AccessType.READ_ONLY)
return; 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==null) throw new UserNotAuthorizedException("Insufficent Privileges for user "+userToCheck+": it's not a valid StorageHub node");
if (item.isShared()) { if (item.isShared()) {
List<ACL> acls = aclManager.get(item, session); List<ACL> acls = aclManager.getByItem(item, session);
for (ACL entry: acls) { for (ACL entry: acls) {
if (entry.getPricipal().equals(userToCheck)) { if (entry.getPrincipal().equals(userToCheck)) {
for (AccessType privilege : entry.getAccessTypes()){ for (AccessType privilege : entry.getAccessTypes()){
if (privilege==AccessType.ADMINISTRATOR) if (privilege==AccessType.ADMINISTRATOR)
return; return;

View File

@ -1,7 +1,10 @@
package org.gcube.data.access.storagehub.handlers; package org.gcube.data.access.storagehub.handlers;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.jcr.Node; 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.DumpData;
import org.gcube.common.storagehub.model.exporter.GroupData; import org.gcube.common.storagehub.model.exporter.GroupData;
import org.gcube.common.storagehub.model.exporter.UserData; 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.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.handlers.vres.VREManager;
import org.gcube.data.access.storagehub.services.delegates.ACLManagerDelegate; import org.gcube.data.access.storagehub.services.delegates.ACLManagerDelegate;
import org.gcube.data.access.storagehub.services.delegates.GroupManagerDelegate; import org.gcube.data.access.storagehub.services.delegates.GroupManagerDelegate;
@ -39,14 +46,19 @@ public class DataHandler {
@Inject @Inject
ACLManagerDelegate aclHandler; ACLManagerDelegate aclHandler;
@Inject
PathUtil pathUtil;
@Inject @Inject
VREManager vreManager; VREManager vreManager;
public DumpData exportData(JackrabbitSession session) throws RepositoryException,StorageHubException { public DumpData exportData(JackrabbitSession session) throws RepositoryException,StorageHubException {
DumpData data = new DumpData(); 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()); .collect(Collectors.toList());
data.setUsers(usersData);
List<String> groups = groupHandler.getGroups(session); List<String> groups = groupHandler.getGroups(session);
@ -58,12 +70,12 @@ public class DataHandler {
logger.debug("searching for group {}",group); logger.debug("searching for group {}",group);
Item vreFolderItem = vreManager.getVreFolderItemByGroupName(session, group, null).getVreFolder(); Item vreFolderItem = vreManager.getVreFolderItemByGroupName(session, group, null).getVreFolder();
String owner = vreFolderItem.getOwner(); String owner = vreFolderItem.getOwner();
List<ACL> acls = aclHandler.get(vreFolderItem, session); List<ACL> acls = aclHandler.getByItem(vreFolderItem, session);
AccessType accessType = AccessType.READ_ONLY; AccessType accessType = AccessType.READ_ONLY;
List<ACL> otherAccess = new ArrayList<ACL>(acls.size() - 1); List<ACL> otherAccess = new ArrayList<ACL>(acls.size() - 1);
for (ACL acl : acls) for (ACL acl : acls)
if (acl.getPricipal().equals(group)) if (acl.getPrincipal().equals(group))
accessType = acl.getAccessTypes().get(0); accessType = acl.getAccessTypes().get(0);
else else
otherAccess.add(acl); otherAccess.add(acl);
@ -72,10 +84,38 @@ public class DataHandler {
groupsData.add(new GroupData(group, owner, members, accessType, otherAccess)); 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.setGroups(groupsData);
data.setItemPerUser(itemsPerUser);
return data; 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) { public void importData(JackrabbitSession session, DumpData data) {
data.getUsers().forEach(u -> { data.getUsers().forEach(u -> {
try { try {
@ -98,7 +138,7 @@ public class DataHandler {
Item vreFolderItem = vreManager.getVreFolderItemByGroupName(session, g.getName(), null).getVreFolder(); Item vreFolderItem = vreManager.getVreFolderItemByGroupName(session, g.getName(), null).getVreFolder();
for (ACL acl : g.getAcls()) { 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) { } catch (Throwable e) {

View File

@ -104,7 +104,7 @@ public class ACLManager extends Impersonable {
ses = repository.getRepository().login(Constants.JCR_CREDENTIALS); ses = repository.getRepository().login(Constants.JCR_CREDENTIALS);
Item item = node2Item.getItem(ses.getNodeByIdentifier(id), Excludes.ALL); Item item = node2Item.getItem(ses.getNodeByIdentifier(id), Excludes.ALL);
authChecker.checkReadAuthorizationControl(ses, currentUser, id); authChecker.checkReadAuthorizationControl(ses, currentUser, id);
return new ACLList(aclManagerDelegate.get(item, ses)); return new ACLList(aclManagerDelegate.getByItem(item, ses));
}catch(RepositoryException re){ }catch(RepositoryException re){
log.error("jcr error getting acl", re); log.error("jcr error getting acl", re);
throw new WebApplicationException(new BackendGenericError("jcr error getting acl", re)); throw new WebApplicationException(new BackendGenericError("jcr error getting acl", re));

View File

@ -8,22 +8,18 @@ import org.gcube.common.security.providers.SecretManagerProvider;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.Path; import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.Context;
@Path("") @Path("")
@ApplicationScoped
public abstract class Impersonable { public abstract class Impersonable {
Logger log = LoggerFactory.getLogger(Impersonable.class); Logger log = LoggerFactory.getLogger(Impersonable.class);
String currentUser; String currentUser = null;
@RequestScoped
@Inject @Inject
public void setCurrentUser(@Context final HttpServletRequest request) { public void setCurrentUser(@Context final HttpServletRequest request) {
String impersonate = request!=null ? request.getParameter("impersonate") : null ; String impersonate = request!=null ? request.getParameter("impersonate") : null ;

View File

@ -63,7 +63,6 @@ import org.slf4j.LoggerFactory;
import com.webcohesion.enunciate.metadata.rs.RequestHeader; import com.webcohesion.enunciate.metadata.rs.RequestHeader;
import com.webcohesion.enunciate.metadata.rs.RequestHeaders; import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.servlet.ServletContext; import jakarta.servlet.ServletContext;
import jakarta.ws.rs.Consumes; import jakarta.ws.rs.Consumes;
@ -95,7 +94,6 @@ public class ItemsManager extends Impersonable{
@Inject @Inject
AccountingHandler accountingHandler; AccountingHandler accountingHandler;
@RequestScoped
@PathParam("id") @PathParam("id")
String id; String id;

View File

@ -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.BackendGenericError;
import org.gcube.common.storagehub.model.exceptions.IdNotFoundException; import org.gcube.common.storagehub.model.exceptions.IdNotFoundException;
import org.gcube.common.storagehub.model.exceptions.StorageHubException; 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.common.storagehub.model.types.SHUBUser;
import org.gcube.data.access.storagehub.Constants; import org.gcube.data.access.storagehub.Constants;
import org.gcube.data.access.storagehub.StorageHubApplicationManager; import org.gcube.data.access.storagehub.StorageHubApplicationManager;
@ -55,12 +56,12 @@ public class UserManager {
@GET @GET
@Path("") @Path("")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public List<SHUBUser> getUsers() { public UsersList getUsers() {
InnerMethodName.set("getUsers"); InnerMethodName.set("getUsers");
JackrabbitSession session = null; JackrabbitSession session = null;
try { try {
session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS); session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS);
return userHandler.getAllUsers(session); return new UsersList(userHandler.getAllUsers(session));
} catch (Throwable e) { } catch (Throwable e) {
log.error("jcr error getting users", e); log.error("jcr error getting users", e);
GXOutboundErrorResponse.throwException(new BackendGenericError(e)); GXOutboundErrorResponse.throwException(new BackendGenericError(e));
@ -73,6 +74,7 @@ public class UserManager {
@GET @GET
@Path("{user}") @Path("{user}")
@Produces(MediaType.APPLICATION_JSON)
public SHUBUser getUser(@PathParam("user") String user) { public SHUBUser getUser(@PathParam("user") String user) {
InnerMethodName.set("getUser"); InnerMethodName.set("getUser");

View File

@ -4,9 +4,11 @@ import static org.gcube.data.access.storagehub.Roles.INFRASTRUCTURE_MANAGER_ROLE
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
@ -144,13 +146,9 @@ public class ScriptManager {
session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS); session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS);
DumpData dd = dataHandler.exportData(session); DumpData dd = dataHandler.exportData(session);
ObjectMapper om = new ObjectMapper(); ObjectMapper om = new ObjectMapper();
status.setSuccess(om.writeValueAsString(dd)); File result = Files.createTempFile("export",".json").toFile();
log.info(""" status.setSuccess(result.getAbsolutePath());
export finished with result om.writeValue(result, dd);
---------------------------
{}
---------------------------
""", status.getResult());
} catch (Throwable t) { } catch (Throwable t) {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true); PrintWriter pw = new PrintWriter(sw, true);

View File

@ -36,7 +36,7 @@ public class ACLManagerDelegate implements ACLManagerInterface {
private static final Logger log = LoggerFactory.getLogger(ACLManagerDelegate.class); private static final Logger log = LoggerFactory.getLogger(ACLManagerDelegate.class);
@Override @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<>(); List<ACL> acls = new ArrayList<>();
if (!item.isShared()) return acls; if (!item.isShared()) return acls;
@ -44,12 +44,16 @@ public class ACLManagerDelegate implements ACLManagerInterface {
if (!(item instanceof SharedFolder)) if (!(item instanceof SharedFolder))
toRetrieve = retrieveSharedFolderParent(toRetrieve, session); toRetrieve = retrieveSharedFolderParent(toRetrieve, session);
String path = item.getPath(); return get(toRetrieve, session);
}
JackrabbitAccessControlList accessControlList = AccessControlUtils.getAccessControlList(session, path ); @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()) { for (AccessControlEntry aclEntry : accessControlList.getAccessControlEntries()) {
ACL acl = new ACL(); ACL acl = new ACL();
acl.setPricipal(aclEntry.getPrincipal().getName()); acl.setPrincipal(aclEntry.getPrincipal().getName());
List<AccessType> types = new ArrayList<>(); List<AccessType> types = new ArrayList<>();
for (Privilege priv : aclEntry.getPrivileges()) for (Privilege priv : aclEntry.getPrivileges())
try { try {

View File

@ -28,6 +28,7 @@ import org.gcube.common.security.ContextBean.Type;
import org.gcube.common.security.providers.SecretManagerProvider; import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.storagehub.model.Excludes; import org.gcube.common.storagehub.model.Excludes;
import org.gcube.common.storagehub.model.Paths; 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.acls.AccessType;
import org.gcube.common.storagehub.model.exceptions.InvalidCallParameters; import org.gcube.common.storagehub.model.exceptions.InvalidCallParameters;
import org.gcube.common.storagehub.model.exceptions.NotFoundException; import org.gcube.common.storagehub.model.exceptions.NotFoundException;
@ -164,16 +165,9 @@ public class GroupManagerDelegate {
Node vreFolder = this.getFolderNodeRelatedToGroup(session, groupId); Node vreFolder = this.getFolderNodeRelatedToGroup(session, groupId);
org.apache.jackrabbit.api.security.user.UserManager usrManager = ((JackrabbitSession) session).getUserManager(); aclManagerDelegate.delete(userId, vreFolder, session);
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);
/*
AccessControlManager acm = session.getAccessControlManager(); AccessControlManager acm = session.getAccessControlManager();
JackrabbitAccessControlList acls = AccessControlUtils.getAccessControlList(acm, vreFolder.getPath()); JackrabbitAccessControlList acls = AccessControlUtils.getAccessControlList(acm, vreFolder.getPath());
@ -186,24 +180,21 @@ public class GroupManagerDelegate {
acls.removeAccessControlEntry(toRemove); acls.removeAccessControlEntry(toRemove);
acm.setPolicy(vreFolder.getPath(), acls); acm.setPolicy(vreFolder.getPath(), acls);
*/
} }
public List<String> getGroupAdministators(JackrabbitSession session, String groupId) throws Throwable { public List<String> getGroupAdministators(JackrabbitSession session, String groupId) throws Throwable {
List<String> users = new ArrayList<String>(); List<String> users = new ArrayList<String>();
Node node = getFolderNodeRelatedToGroup(session, groupId); Node node = getFolderNodeRelatedToGroup(session, groupId);
AccessControlManager acm = session.getAccessControlManager();
JackrabbitAccessControlList acls = AccessControlUtils.getAccessControlList(acm, node.getPath()); List<ACL> acls = aclManagerDelegate.get(node, session);
for (AccessControlEntry acl : acls.getAccessControlEntries()) for (ACL acl : acls)
for (Privilege pr : acl.getPrivileges()) { for (AccessType pr : acl.getAccessTypes())
if (pr.getName().equals(AccessType.ADMINISTRATOR.getValue())) { if (pr == AccessType.ADMINISTRATOR) users.add(acl.getPrincipal());
users.add(acl.getPrincipal().getName());
}
}
return users; return users;
} }

View File

@ -23,7 +23,9 @@ public interface ACLManagerInterface {
* @exception {@link RepositoryException} when a generic jcr error occurs * @exception {@link RepositoryException} when a generic jcr error occurs
* @exception {@link UserNotAuthorizedException} when the caller is not authorized to access to the shared folder * @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 * 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) void delete(String targetUser, Node node, Session session)
throws RepositoryException, StorageHubException; throws RepositoryException, StorageHubException;
} }

View File

@ -1,10 +1,12 @@
package org.gcube.data.access.fs.external; package org.gcube.data.access.fs.external;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashSet; import java.util.HashSet;
@ -19,26 +21,26 @@ import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.common.storagehub.model.acls.AccessType; import org.gcube.common.storagehub.model.acls.AccessType;
import org.gcube.common.storagehub.model.exceptions.StorageHubException; import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.common.storagehub.model.items.SharedFolder; import org.gcube.common.storagehub.model.items.SharedFolder;
import org.gcube.common.storagehub.model.types.SHUBUser;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class CreateUsers { public class CreateUsers {
private static StorageHubClient client; private static StorageHubClient client;
private static Logger log = LoggerFactory.getLogger(CreateUsers.class); private static Logger log = LoggerFactory.getLogger(CreateUsers.class);
private String user ="pippo.test"; private String user1 = "pippo.test";
private String user2 = "lucio.lelii"; private String user2 = "lucio.lelii";
private String vreWA = "vre-write-all"; private String vreWA = "vre-write-all";
private String vreRO = "vre-read-only"; private String vreRO = "vre-read-only";
private String vreWO = "vre-write-owner"; private String vreWO = "vre-write-owner";
private static final int TEST_USERS_NUMBER = 10; private static final int TEST_USERS_NUMBER = 5000;
@BeforeClass @BeforeClass
public static void before() throws Exception { public static void before() throws Exception {
@ -48,29 +50,89 @@ public class CreateUsers {
client = new StorageHubClient(storagehubUri); client = new StorageHubClient(storagehubUri);
} }
@Before @Before
public void setSecret() { public void setSecret() {
CredentialSecret secret = new CredentialSecret("sg4-test-client", "a156a7db-3b32-4cd5-b27b-2488e0e01698", "/gcube"); CredentialSecret secret = new CredentialSecret("sg4-test-client", "a156a7db-3b32-4cd5-b27b-2488e0e01698",
"/gcube");
SecretManagerProvider.set(secret); SecretManagerProvider.set(secret);
} }
@Test @Test
public void initialiseEnvironment() throws Exception { public void initialiseEnvironment() throws Exception {
this.createUser(); this.createUser(user1);
this.addUserToVREs(); this.createAllVREsAndAddUser1AsAdmin();
this.createUser2AndAddItToVres(); client.impersonate(user1);
this.createUser(user2);
this.addUser2ToAllVres();
client.impersonate(user1);
createRandomFolderAndFileForCurrentUser();
createRandomFolderAndFileForCurrentUser();
createRandomFolderAndFileForCurrentUser();
client.impersonate(user2);
createRandomFolderAndFileForCurrentUser();
createRandomFolderAndFileForCurrentUser();
createRandomFolderAndFileForCurrentUser();
} }
@Test
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));
}
}
@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 // USED TO CREATE TEST USERS
final String lexicon = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12345674890"; final String lexicon = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12345674890";
final java.util.Random rand = new java.util.Random(); 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>(); final Set<String> identifiers = new HashSet<String>();
public String randomIdentifier() { public String randomIdentifier() {
@ -91,7 +153,7 @@ public class CreateUsers {
@Test @Test
public void createTestUsers() throws Exception { public void createTestUsers() throws Exception {
for (int i = 0; i < TEST_USERS_NUMBER; i++) for (int i = 0; i < TEST_USERS_NUMBER; i++)
client.createUserAccount(randomIdentifier().toLowerCase()); client.createUserAccount(String.format("%s.%s", randomIdentifier().toLowerCase(), randomIdentifier().toLowerCase() ));
} }
@Test @Test
@ -102,10 +164,8 @@ public class CreateUsers {
client.getVreFolderManager(vreWO).addUser("service-account-sg4-test-client"); client.getVreFolderManager(vreWO).addUser("service-account-sg4-test-client");
} }
@Test public String getUserRootId() throws Exception {
public void getUserRootId() throws Exception{ return client.getWSRoot().getId();
client.impersonate(user);
System.out.println(client.getWSRoot().getId());
} }
@Test @Test
@ -115,55 +175,62 @@ public class CreateUsers {
@Test @Test
public void getFolderACLsOnrROVRE() throws Exception { public void getFolderACLsOnrROVRE() throws Exception {
client.impersonate(user); client.impersonate(user1);
FolderContainer vre = (FolderContainer) client.getVREFolders().getContainers().stream().filter(f -> f.get().getTitle().equals(vreWA)).findFirst().get(); 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(); 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());
} }
public void createUser(String user) throws Exception {
@Test
public void createUser() throws Exception{
client.createUserAccount(user); client.createUserAccount(user);
} }
@Test public void removeUserFromGroup(String user) throws Exception {
public void removeUserFromGroup() throws Exception{
client.getVreFolderManager(vreWA).removeAdmin(user); client.getVreFolderManager(vreWA).removeAdmin(user);
client.getVreFolderManager(vreWA).removeUser(user); client.getVreFolderManager(vreWA).removeUser(user);
} }
@Test public void createVRE(String vre, AccessType accesstype, String owner) throws Exception {
public void addUserToVREs() throws Exception{ client.getVreFolderManager(vre).createVRE(accesstype, owner);
client.getVreFolderManager(vreWA).createVRE(AccessType.WRITE_ALL, user, false); }
client.getVreFolderManager(vreWA).setAdmin(user);
client.getVreFolderManager(vreRO).createVRE(AccessType.READ_ONLY, user); public void setAdmin(String vre, String user) throws Exception {
client.getVreFolderManager(vreRO).setAdmin(user); client.getVreFolderManager(vre).setAdmin(user);
client.getVreFolderManager(vreWO).createVRE(AccessType.WRITE_OWNER, user);
client.getVreFolderManager(vreWO).setAdmin(user);
createFolderByAdminInVREWO();
} }
@Test @Test
public void createFolderByAdminInVREWO() throws Exception { public void createAllVREsAndAddUser1AsAdmin() throws Exception {
client.impersonate(user); createVRE(vreWA, AccessType.WRITE_ALL, user1 );
retrieveVREbyName(vreWO).newFolder("test created by pippo", ""); setAdmin(vreWA, user1);
createVRE(vreRO, AccessType.READ_ONLY, user1 );
setAdmin(vreWA, user1);
createVRE(vreWO, AccessType.WRITE_OWNER, user1 );
setAdmin(vreWA, user1);
} }
@Test @Test
public void createUser2AndAddItToVres() throws Exception{ public void addUser2ToAllVres() throws Exception {
client.impersonate(user); addUserToVre(vreRO, user2);
client.createUserAccount(user2); addUserToVre(vreWA, user2);
client.getVreFolderManager(vreWA).addUser(user2); addUserToVre(vreWO, user2);
client.getVreFolderManager(vreRO).addUser(user2); }
client.getVreFolderManager(vreWO).addUser(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 @Test
public void printVREFoldersAdmin() throws StorageHubException { public void printVREFoldersAdmin() throws StorageHubException {
System.out.println("launching ....."); System.out.println("launching .....");
List<FolderContainer> vreFolders = client.getVREFolders().getContainers().stream().map(c -> ((FolderContainer) c)).collect(Collectors.toList()); List<FolderContainer> vreFolders = client.getVREFolders().getContainers().stream()
.map(c -> ((FolderContainer) c)).collect(Collectors.toList());
for (FolderContainer fc : vreFolders) { for (FolderContainer fc : vreFolders) {
System.out.println(fc.get().getTitle()); System.out.println(fc.get().getTitle());
System.out.println(fc.getAcls()); System.out.println(fc.getAcls());
@ -173,31 +240,30 @@ public class CreateUsers {
@Test @Test
public void getVREs() throws Exception { public void getVREs() throws Exception {
client.impersonate(user); client.impersonate(user1);
client.getVREFolders().getItems().stream().map(i -> ((SharedFolder) i)).forEach(s -> System.out.println(s.getDisplayName())); client.getVREFolders().getItems().stream().map(i -> ((SharedFolder) i))
.forEach(s -> System.out.println(s.getDisplayName()));
} }
@Test @Test
public void getWsRoot() throws Exception { public void getWsRoot() throws Exception {
client.impersonate(user); client.impersonate(user1);
client.getWSRoot().list().getItems().forEach(i -> System.out.println(i.getId() + " " + i.getTitle())); client.getWSRoot().list().getItems().forEach(i -> System.out.println(i.getId() + " " + i.getTitle()));
} }
@Test @Test
public void download() throws Exception { public void download() throws Exception {
client.impersonate(user); client.impersonate(user1);
try (InputStream is = client.open("5b5236d2-ec88-49f2-a2c5-04ecf8284191").asFile().download().getStream()) { try (InputStream is = client.open("5b5236d2-ec88-49f2-a2c5-04ecf8284191").asFile().download().getStream()) {
String text = new BufferedReader( String text = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).lines()
new InputStreamReader(is, StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n")); .collect(Collectors.joining("\n"));
System.out.println(text); System.out.println(text);
} }
} }
@Test @Test
public void uploadFile() { public void uploadImageTest1() {
client.impersonate(user); client.impersonate(user1);
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")) { try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")) {
client.getWSRoot().uploadFile(is, "test1.jpg", "a test"); client.getWSRoot().uploadFile(is, "test1.jpg", "a test");
} catch (Exception e) { } catch (Exception e) {
@ -205,9 +271,30 @@ public class CreateUsers {
} }
} }
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 { private FolderContainer retrieveVREbyName(String name) throws StorageHubException {
List<FolderContainer> vreFolders = client.getVREFolders().getContainers().stream().map(c -> ((FolderContainer) c)).collect(Collectors.toList()); List<FolderContainer> vreFolders = client.getVREFolders().getContainers().stream()
return vreFolders.stream().filter(c -> ((SharedFolder)c.get()).getDisplayName().equals(name)).findFirst().get(); .map(c -> ((FolderContainer) c)).collect(Collectors.toList());
return vreFolders.stream().filter(c -> ((SharedFolder) c.get()).getDisplayName().equals(name)).findFirst()
.get();
} }
@Test @Test
@ -232,7 +319,7 @@ public class CreateUsers {
@Test @Test
public void uploadFileToVREReadOnlyAdmin() throws StorageHubException { public void uploadFileToVREReadOnlyAdmin() throws StorageHubException {
client.impersonate(user); client.impersonate(user1);
try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")) { try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")) {
retrieveVREbyName(vreRO).uploadFile(is, "test1.jpg", "a test"); retrieveVREbyName(vreRO).uploadFile(is, "test1.jpg", "a test");
} catch (IOException e) { } catch (IOException e) {
@ -252,7 +339,7 @@ public class CreateUsers {
@Test @Test
public void removeUser() throws Exception { public void removeUser() throws Exception {
client.deleteUserAccount(user); client.deleteUserAccount(user1);
} }
} }

Binary file not shown.