From 1f6329c38ec23cc2b0ed2577d96305a1160c351a Mon Sep 17 00:00:00 2001 From: lucio Date: Fri, 2 Dec 2022 14:39:37 +0100 Subject: [PATCH] added possibility to set owner on backends --- pom.xml | 2 ++ .../gcube/data/access/storagehub/Utils.java | 11 +++--- .../storagehub/handlers/DownloadHandler.java | 20 ++++++++--- .../handlers/items/ItemHandler.java | 4 +-- .../plugins/StorageOperationMediator.java | 4 +-- .../storagehub/services/ItemsManager.java | 35 ++++++++----------- .../storagehub/services/WorkspaceManager.java | 2 +- .../impl/GCubeMongoStorageBackend.java | 10 +++--- .../storage/backend/impl/S3Backend.java | 12 +++---- 9 files changed, 54 insertions(+), 46 deletions(-) diff --git a/pom.xml b/pom.xml index 6d3f3ba..234bfdf 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,8 @@ UTF-8 UTF-8 2.14.0 + 1.8 + 1.8 diff --git a/src/main/java/org/gcube/data/access/storagehub/Utils.java b/src/main/java/org/gcube/data/access/storagehub/Utils.java index a9552b0..f44b282 100644 --- a/src/main/java/org/gcube/data/access/storagehub/Utils.java +++ b/src/main/java/org/gcube/data/access/storagehub/Utils.java @@ -217,17 +217,18 @@ public class Utils { public static String checkExistanceAndGetUniqueName(Session ses, Node destination, String name) throws BackendGenericError{ + String escapedName = Text.escapeIllegalJcrChars(name); try { - destination.getNode(name); + destination.getNode(escapedName); }catch(PathNotFoundException pnf) { - return Text.escapeIllegalJcrChars(name); + return escapedName; } catch (Exception e) { throw new BackendGenericError(e); } try { - String filename = FilenameUtils.getBaseName(name); - String ext = FilenameUtils.getExtension(name); + String filename = FilenameUtils.getBaseName(escapedName); + String ext = FilenameUtils.getExtension(escapedName); String nameTocheck = ext.isEmpty()? String.format("%s(*)",filename): String.format("%s(*).%s",filename, ext); @@ -244,7 +245,7 @@ public class Utils { String newName = ext.isEmpty()? String.format("%s(%d)", filename,maxval+1) : String.format("%s(%d).%s", filename,maxval+1, ext) ; - return Text.escapeIllegalJcrChars(newName); + return newName; } catch (Exception e) { throw new BackendGenericError(e); } diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/DownloadHandler.java b/src/main/java/org/gcube/data/access/storagehub/handlers/DownloadHandler.java index 0a8a721..d4d9541 100644 --- a/src/main/java/org/gcube/data/access/storagehub/handlers/DownloadHandler.java +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/DownloadHandler.java @@ -18,12 +18,14 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; import org.apache.commons.io.FilenameUtils; +import org.gcube.common.storagehub.model.Constants; import org.gcube.common.storagehub.model.Excludes; import org.gcube.common.storagehub.model.Paths; import org.gcube.common.storagehub.model.exceptions.InvalidItemException; import org.gcube.common.storagehub.model.exceptions.PluginInitializationException; import org.gcube.common.storagehub.model.exceptions.PluginNotFoundException; import org.gcube.common.storagehub.model.exceptions.StorageHubException; +import org.gcube.common.storagehub.model.exceptions.StorageIdNotFoundException; import org.gcube.common.storagehub.model.items.AbstractFileItem; import org.gcube.common.storagehub.model.items.FolderItem; import org.gcube.common.storagehub.model.items.Item; @@ -104,7 +106,7 @@ public class DownloadHandler { StorageBackend sb = sbf.create(content.getPayloadBackend()); - final InputStream streamToWrite = sb.download(content); + InputStream streamToWrite = sb.download(content); if (withAccounting) accountingHandler.createReadObj(fileItem.getTitle(), ses, (Node) fileItem.getRelatedNode(), login, true); @@ -133,8 +135,18 @@ public class DownloadHandler { StorageBackendFactory sbf = storageBackendHandler.get(content.getPayloadBackend()); StorageBackend sb = sbf.create(content.getPayloadBackend()); - final InputStream streamToWrite = sb.download(content); - + InputStream streamToWrite = null; + try { + streamToWrite = sb.download(content); + }catch (StorageIdNotFoundException e) { + //TODO: temporary code, it will last until the MINIO porting will not finish + if (sbf.getName().equals(Constants.MONGO_STORAGE)) { + sbf = storageBackendHandler.get(Constants.DEFAULT_MINIO_STORAGE); + sbf.create(new PayloadBackend(Constants.DEFAULT_MINIO_STORAGE, null)); + } else + throw e; + } + log.debug("retrieved storage id is {} with storageBackend {} (stream is null? {})",content.getStorageId(), sbf.getName(), streamToWrite==null ); String oldfilename = FilenameUtils.getBaseName(currentItem.getTitle()); @@ -165,7 +177,7 @@ public class DownloadHandler { StorageBackend sb = sbf.create(new PayloadBackend(storageName, null)); - final InputStream streamToWrite = sb.download(storageId); + InputStream streamToWrite = sb.download(storageId); Map userMetadata = sb.getFileMetadata(storageId); diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java b/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java index 96edba3..2f5a2e7 100644 --- a/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java @@ -340,9 +340,9 @@ public class ItemHandler { log.debug("UPLOAD: upload on {} - start",storageBackend.getClass()); if (fileDetails !=null && fileDetails.getSize()>0) { log.debug("UPLOAD: file size set is {} Byte",fileDetails.getSize()); - info = storageBackend.upload(is1, relPath, name, fileDetails.getSize()); + info = storageBackend.upload(is1, relPath, name, fileDetails.getSize(), login); } else - info = storageBackend.upload(is1, relPath, name); + info = storageBackend.upload(is1, relPath, name, login); log.debug("UPLOAD: upload on storage - stop"); }catch (Throwable e) { log.error("error writing content",e ); diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/plugins/StorageOperationMediator.java b/src/main/java/org/gcube/data/access/storagehub/handlers/plugins/StorageOperationMediator.java index 295c5a6..9820721 100644 --- a/src/main/java/org/gcube/data/access/storagehub/handlers/plugins/StorageOperationMediator.java +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/plugins/StorageOperationMediator.java @@ -22,7 +22,7 @@ public class StorageOperationMediator { @Inject StorageBackendHandler storageBackendHandler; - public MetaInfo copy(Content source, PayloadBackend destination, String newName, String newParentPath) throws StorageHubException{ + public MetaInfo copy(Content source, PayloadBackend destination, String newName, String newParentPath, String login) throws StorageHubException{ log.info("creating Storages for source {} and destination {}", source.getPayloadBackend(), destination.getStorageName()); @@ -39,7 +39,7 @@ public class StorageOperationMediator { }else { log.info("source and destintiona are different storage"); InputStream stream = sourceSB.download(source); - MetaInfo info = destSB.upload(stream, newParentPath, newName, source.getSize()); + MetaInfo info = destSB.upload(stream, newParentPath, newName, source.getSize(), login); return info; } } diff --git a/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java b/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java index 5b8b61b..17597b9 100644 --- a/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java +++ b/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java @@ -54,6 +54,7 @@ import org.gcube.common.storagehub.model.service.VersionList; import org.gcube.common.storagehub.model.storages.MetaInfo; import org.gcube.common.storagehub.model.types.ItemAction; import org.gcube.common.storagehub.model.types.NodeProperty; +import org.gcube.contentmanagement.blobstorage.transport.backend.util.GetPayloadMap; import org.gcube.data.access.storagehub.AuthorizationChecker; import org.gcube.data.access.storagehub.Constants; import org.gcube.data.access.storagehub.PathUtil; @@ -506,7 +507,7 @@ public class ItemsManager extends Impersonable{ ses.save(); }catch(RepositoryException re ){ - log.error("jcr error getting rootSharedFolder", re); + log.error("jcr error publishing folder", re); GXOutboundErrorResponse.throwException(new BackendGenericError(re)); }catch(StorageHubException she ){ log.error(she.getErrorMessage(), she); @@ -830,7 +831,7 @@ public class ItemsManager extends Impersonable{ Content contentToCopy = ((AbstractFileItem) item).getContent(); - MetaInfo contentInfo = opMediator.copy(contentToCopy, destinationItem.getBackend(), destination.getPath(), uniqueName); + MetaInfo contentInfo = opMediator.copy(contentToCopy, destinationItem.getBackend(), destination.getPath(), uniqueName, currentUser); Utils.setContentFromMetaInfo((AbstractFileItem) item, contentInfo); item2Node.replaceContent(newNode, (AbstractFileItem) item, ItemAction.CLONED); @@ -876,14 +877,16 @@ public class ItemsManager extends Impersonable{ ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context)); authChecker.checkMoveOpsForProtectedFolders(ses, id); - + authChecker.checkWriteAuthorizationControl(ses, currentUser, id, false); + final Node nodeToMove = ses.getNodeByIdentifier(id); final Item item = node2Item.getItem(nodeToMove, null); - if (item instanceof SharedFolder && ((SharedFolder) item).isVreFolder() ) - throw new InvalidItemException("vre folder"); - + if (item instanceof SharedFolder) + if (getSharedParentNode(nodeToMove).getIdentifier() == item.getId()) + throw new InvalidItemException("root shared folder name cannot be modfied"); + String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, nodeToMove.getParent(), newName); try { @@ -893,22 +896,12 @@ public class ItemsManager extends Impersonable{ throw new ItemLockedException(e); } try { - if (item instanceof SharedFolder) { - authChecker.checkAdministratorControl(ses, currentUser, (SharedFolder)item); - nodeToMove.setProperty(NodeProperty.TITLE.toString(), uniqueName); - Utils.setPropertyOnChangeNode(nodeToMove, currentUser, ItemAction.RENAMED); - accountingHandler.createRename(item.getTitle(), uniqueName, nodeToMove, currentUser, ses, false); - } else { - authChecker.checkWriteAuthorizationControl(ses, currentUser, id, false); - String newPath = String.format("%s/%s", nodeToMove.getParent().getPath(), uniqueName); - nodeToMove.setProperty(NodeProperty.TITLE.toString(), uniqueName); - Utils.setPropertyOnChangeNode(nodeToMove, currentUser, ItemAction.RENAMED); - accountingHandler.createRename(item.getTitle(), uniqueName, ses.getNode(newPath), currentUser, ses, false); - ses.move(nodeToMove.getPath(), newPath); - } - + String newPath = String.format("%s/%s", nodeToMove.getParent().getPath(), uniqueName); + nodeToMove.setProperty(NodeProperty.TITLE.toString(), uniqueName); + Utils.setPropertyOnChangeNode(nodeToMove, currentUser, ItemAction.RENAMED); + ses.move(nodeToMove.getPath(), newPath); + accountingHandler.createRename(item.getTitle(), uniqueName, ses.getNode(newPath), currentUser, ses, false); ses.save(); - }finally { ses.getWorkspace().getLockManager().unlock(nodeToMove.getPath()); ses.getWorkspace().getLockManager().unlock(nodeToMove.getParent().getPath()); diff --git a/src/main/java/org/gcube/data/access/storagehub/services/WorkspaceManager.java b/src/main/java/org/gcube/data/access/storagehub/services/WorkspaceManager.java index 585adf1..8a5aadb 100644 --- a/src/main/java/org/gcube/data/access/storagehub/services/WorkspaceManager.java +++ b/src/main/java/org/gcube/data/access/storagehub/services/WorkspaceManager.java @@ -180,7 +180,7 @@ public class WorkspaceManager extends Impersonable{ StorageBackend sb = sbf.create(payloadBackend); log.info("UPLOAD: call started with file size {}",size); - MetaInfo info = sb.upload(stream, null, fileDetail.getFileName()); + MetaInfo info = sb.upload(stream, null, fileDetail.getFileName(), currentUser); log.debug("UPLOAD: call finished"); toReturn = publicLinkHandler.getForVolatile(info.getStorageId(), GCubeVolatileStorageBackendFactory.NAME, context); diff --git a/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/GCubeMongoStorageBackend.java b/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/GCubeMongoStorageBackend.java index a894239..f1c9480 100644 --- a/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/GCubeMongoStorageBackend.java +++ b/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/GCubeMongoStorageBackend.java @@ -67,9 +67,9 @@ public class GCubeMongoStorageBackend extends StorageBackend { } @Override - public MetaInfo upload(InputStream stream, String relPath, String name) { + public MetaInfo upload(InputStream stream, String relPath, String name, String user) { log.debug("uploading file"); - IClient storageClient = getStorageClient(SecretManagerProvider.instance.get().getOwner().getId()).getClient(); + IClient storageClient = getStorageClient(user).getClient(); String uid = UUID.randomUUID().toString(); String remotePath= String.format("%s/%s-%s",relPath,uid,name); String storageId =storageClient.put(true).LFile(stream).RFile(remotePath); @@ -80,13 +80,13 @@ public class GCubeMongoStorageBackend extends StorageBackend { @Override - public MetaInfo upload(InputStream stream, String relPath, String name, Long size) { - return this.upload(stream, relPath, name); + public MetaInfo upload(InputStream stream, String relPath, String name, Long size, String user) { + return this.upload(stream, relPath, name, user); } @Override - public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size) { + public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size, String user) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/S3Backend.java b/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/S3Backend.java index 66bb7e9..1547e7e 100644 --- a/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/S3Backend.java +++ b/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/S3Backend.java @@ -101,24 +101,24 @@ public class S3Backend extends StorageBackend{ @Override - public MetaInfo upload(InputStream stream, String relativePath, String name) { - return this.upload(stream, relativePath, name, null); + public MetaInfo upload(InputStream stream, String relativePath, String name, String user) { + return this.upload(stream, relativePath, name, null, user); } @Override - public MetaInfo upload(InputStream stream, String relativePath, String name, Long size) { + public MetaInfo upload(InputStream stream, String relativePath, String name, Long size, String user) { String storageId = keyGenerator.apply(null); - return upload(stream, relativePath, name, storageId, size); + return upload(stream, relativePath, name, storageId, size, user); } @Override - public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size) { + public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size, String user) { try { Map headers = new HashMap<>(); headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY"); Map userMetadata = new HashMap<>(); - userMetadata.put("user", SecretManagerProvider.instance.get().getOwner().getId()); + userMetadata.put("user", user); userMetadata.put("title", name); client.putObject(