From 3e6207d7b9f1db1be20890525875945f9a1b1c10 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 3 Apr 2023 15:55:43 +0200 Subject: [PATCH] Fixing incident [#24597], revisited the getSharedFolderMembers --- .classpath | 2 +- CHANGELOG.md | 4 + pom.xml | 12 +- .../server/StorageHubClientService.java | 109 ++++++++++++++---- .../server/WrapperUtility.java | 35 ++++++ src/test/java/WorkspaceInstance.java | 37 ++++-- 6 files changed, 169 insertions(+), 30 deletions(-) diff --git a/.classpath b/.classpath index 8d06719..5bda7dd 100644 --- a/.classpath +++ b/.classpath @@ -38,7 +38,7 @@ - + diff --git a/CHANGELOG.md b/CHANGELOG.md index ebead29..f4a186e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [v1.2.1-SNAPSHOT] - 2023-04-03 + +- [#24597] Fixed no notification sent for items 'updated" in the VRE Folder with '-' in the name + ## [v1.2.0] - 2022-05-02 #### Enhancements diff --git a/pom.xml b/pom.xml index 1e03923..1388d64 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.gcube.common storagehub-client-wrapper - 1.2.0 + 1.2.1-SNAPSHOT storagehub-client-wrapper This is a wrapper of storagehub-client library. It allows to interact with storagehub in a simplified way by exposing several utilities. Moreover, It exposes by another inteface java beans as defined in (the old) HL interfaces @@ -57,10 +57,18 @@ lombok 1.14.8 + + + org.gcube.common + storagehub-model + + compile + org.gcube.common storagehub-client-library + compile @@ -168,4 +176,4 @@ - \ No newline at end of file + diff --git a/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java b/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java index c9f9468..6c4a8b7 100644 --- a/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java +++ b/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java @@ -580,17 +580,33 @@ public class StorageHubClientService { List members = new ArrayList(); if (item instanceof SharedFolder) { + SharedFolder sharedfolder = (SharedFolder) item; + + String vreName = null; + if (sharedfolder.isVreFolder()) { + vreName = sharedfolder.getDisplayName(); // the displayName should matching the VRE Name + // this class is used? + if (sharedfolder instanceof VreFolder) { + VreFolder vreFolder = (VreFolder) sharedfolder; + vreName = vreFolder.getDisplayName(); // the displayName should matching the VRE Name + } + } + + String infra = WrapperUtility.getInfrastructureNameFromScope(scope); + logger.debug("Infrastructure is: " + infra); + logger.debug("vreName is: " + vreName); List listACL = shClient.open(folderId).asFolder().getAcls(); logger.info("Reading users from ACLs"); for (ACL acl : listACL) { - logger.trace("acl princial is: " + acl.getPricipal()); + logger.info("acl principal is: " + acl.getPricipal()); Member member = new Member(acl.getPricipal(), acl.getPricipal(), Member.TYPE.USER); - if (isGroupName(acl.getPricipal())) { - logger.info("pricipal: " + acl.getPricipal() + " is a group"); + boolean isAGroup = isGroupName(acl.getPricipal(), infra, vreName); + if (isAGroup) { member.setMemberType(TYPE.GROUP); // gcube-devsec-devVRE -> devVRE that is the groupName - member.setName(acl.getPricipal().substring(acl.getPricipal().lastIndexOf("-") + 1, - acl.getPricipal().length())); + // the VRE Name should be the group name + member.setName(vreName); + logger.info("pricipal: " + acl.getPricipal() + " is "+member.getMemberType()+": "+member.getName()); } members.add(member); logger.debug("added member: " + member); @@ -609,22 +625,35 @@ public class StorageHubClientService { * Checks if is group name. * * @param pricipal the pricipal + * @param infrastructureName the infrastructur name + * @param vreName the vre name * @return true, if is group name E.g. with the input 'gcube-devNext-NextNext' * returns true */ - private boolean isGroupName(String pricipal) { + private boolean isGroupName(String pricipal, String infrastructureName, String vreName) { if (pricipal == null || pricipal.isEmpty()) return false; - String theScope = getScopeFromVREGroupName(pricipal); - ScopeBean scope = null; - try { - scope = new ScopeBean(theScope); - logger.info("pricipal '" + pricipal + "' is a valid scope"); - } catch (IllegalArgumentException e) { - logger.trace("principal '" + pricipal + "' is not a scope"); + if (vreName == null || vreName.isEmpty()) return false; + + ScopeBean scope = null; + + //Removing the first / from the infrastructure name. E.g /gcube -> gcube + infrastructureName = infrastructureName.replace(WrapperUtility.SCOPE_SEPARATOR, ""); + if (principalContainsScopes(pricipal, infrastructureName, vreName)) { + String theScope = getScopeFromVREGroupName(pricipal, infrastructureName, vreName); + + try { + //Just to be sure of the format /RootVO/VO/VRE + scope = new ScopeBean(theScope); + logger.info("pricipal '" + pricipal + "' remapping as "+theScope+" should be a valid scope"); + } catch (IllegalArgumentException e) { + logger.trace("principal '" + pricipal + "' is not a scope"); + return false; + } + } return scope != null; @@ -632,15 +661,55 @@ public class StorageHubClientService { } /** - * Gets the scope from VRE group name. + * Principal contains scopes. * - * @param context the context - * @return the scope from VRE group name. Eg. with the input - * 'gcube-devNext-NextNext' returns '/gcube/devNext/NextNext' + * @param principal the principal + * @param infrastructurName the infrastructur name + * @param vreName the vre name + * @return true, if principal (as string) contains the RootVO and the VRE name. */ - public static String getScopeFromVREGroupName(String context) { - String entireScopeName = context.replaceAll("^/(.*)/?$", "$1").replaceAll("-", "/"); - return entireScopeName; + private static boolean principalContainsScopes(String principal, String infrastructurName, String vreName) { + + if (principal.contains(infrastructurName)) { + if (principal.contains(vreName)) { + return true; + } + } + + return false; + + } + + public static String getScopeFromVREGroupName(String context, String infrastructurName, String vreName) { + + if (vreName == null) + vreName = ""; + + logger.debug("vreName: " + vreName); + + if (infrastructurName.startsWith("/")) + infrastructurName = infrastructurName.substring(1, infrastructurName.length()); + + logger.debug("infrastructurName: " + infrastructurName); + + String voGroup = context.replaceAll(infrastructurName, ""); + if (vreName != null) { + voGroup = voGroup.replaceAll(vreName, "").replaceAll("-", ""); + } + String voName = voGroup.replaceAll("-", ""); + logger.debug("voName: " + voName); + + StringBuilder theScopeBuilder = new StringBuilder(); + theScopeBuilder.append("/" + infrastructurName); + if (voName != null && !voName.isEmpty()) { + theScopeBuilder.append(WrapperUtility.SCOPE_SEPARATOR + voName); + } + if (vreName != null && !vreName.isEmpty()) { + theScopeBuilder.append(WrapperUtility.SCOPE_SEPARATOR + vreName); + } + String theScope = theScopeBuilder.toString(); + logger.debug("Built scope: " + theScope); + return theScope; } /** diff --git a/src/main/java/org/gcube/common/storagehubwrapper/server/WrapperUtility.java b/src/main/java/org/gcube/common/storagehubwrapper/server/WrapperUtility.java index 7312a6b..7bdfda3 100644 --- a/src/main/java/org/gcube/common/storagehubwrapper/server/WrapperUtility.java +++ b/src/main/java/org/gcube/common/storagehubwrapper/server/WrapperUtility.java @@ -12,9 +12,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** + * The Class WrapperUtility. + * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Oct 17, 2018 */ public class WrapperUtility { + + public static final String SCOPE_SEPARATOR = "/"; private static Logger logger = LoggerFactory.getLogger(WrapperUtility.class); @@ -47,4 +51,35 @@ public class WrapperUtility { } } } + + /** + * Gets the infrastructure name from scope. + * + * @param scope the scope + * @return the infrastructure name from scope + * @throws Exception the exception + */ + public static String getInfrastructureNameFromScope(String scope) throws Exception{ + + if(scope==null || scope.isEmpty()){ + throw new Exception("Scope is null or empty"); + } + + if(!scope.startsWith(SCOPE_SEPARATOR)){ + logger.warn("Input scope: "+scope+" not have / is a really scope?"); + scope = SCOPE_SEPARATOR+scope; + logger.warn("Tentative as scope: "+scope); + } + + String[] splitScope = scope.split(SCOPE_SEPARATOR); + + String rootScope = SCOPE_SEPARATOR + splitScope[1]; + + if(rootScope.length()<2){ + throw new Exception("Infrastructure name not found in "+scope); + } + + return rootScope; + + } } diff --git a/src/test/java/WorkspaceInstance.java b/src/test/java/WorkspaceInstance.java index f0eb4f8..5852021 100644 --- a/src/test/java/WorkspaceInstance.java +++ b/src/test/java/WorkspaceInstance.java @@ -3,6 +3,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.storagehub.client.dsl.FolderContainer; +import org.gcube.common.storagehub.client.dsl.StorageHubClient; import org.gcube.common.storagehubwrapper.server.StorageHubWrapper; import org.gcube.common.storagehubwrapper.server.WorkspaceStorageHubClientService; import org.gcube.common.storagehubwrapper.server.tohl.Workspace; @@ -26,13 +30,15 @@ import org.junit.Test; */ public class WorkspaceInstance { - // public static String SCOPE = "/gcube"; - public static String SCOPE = "/d4science.research-infrastructures.eu"; + public static String SCOPE = "/gcube/devsec/devVRE"; + //public static String SCOPE = "/d4science.research-infrastructures.eu"; // public static String USERNAME = "massimiliano.assante"; public static String USERNAME = "francesco.mangiacrapa"; - public static String TOKEN = System.getenv("TOKEN_gcube"); // YOU MUST SET THIS AS Environment variable + //public static String TOKEN = System.getenv("TOKEN_gcube"); // YOU MUST SET THIS AS Environment variable + + public static String TOKEN = ""; public static String FIND_FILE_NAME = "francesco"; public static String rootId = null; @@ -40,7 +46,7 @@ public class WorkspaceInstance { private WorkspaceStorageHubClientService workspace; - // @Before + //@Before public void init() { if (TOKEN == null) { @@ -48,6 +54,9 @@ public class WorkspaceInstance { } System.out.println("Read TOKEN_gcube: " + TOKEN); + + ScopeProvider.instance.set(SCOPE); + SecurityTokenProvider.instance.set(TOKEN); // METHOD 1 storageHubWrapper = new StorageHubWrapper(SCOPE, TOKEN, false, false, true); @@ -110,6 +119,16 @@ public class WorkspaceInstance { } } + + //@Test + public void getVREFolder() throws Exception { + System.out.println("Get VRE Folder"); + + StorageHubClient shClient = new StorageHubClient(); + FolderContainer vre= shClient.openVREFolder(); + System.out.println("VRE folder: "+vre.getId() + " ACLs: "+vre.getAcls()); + } + // @Test public void getFilteredChildren() throws Exception { @@ -166,14 +185,18 @@ public class WorkspaceInstance { } } - //@Test + @Test public void getSharedFolderMembers() { System.out.println("Getting getSharedFolderMembers"); try { - String sharedFolderId = "7e3c6636-927c-4139-9fcc-64986ea70cbf"; // devNext - sharedFolderId = "c141d53d-4cae-4bfc-a6f7-9b8bbd8c0e2b"; //NextNext + //String sharedFolderId = "7e3c6636-927c-4139-9fcc-64986ea70cbf"; // devVRE (VRE Folder) + String sharedFolderId = "f8275521-5da1-4501-8385-bb9c79055aa6"; // PROD - d4science.research-infrastructures.eu-D4OS-Blue-Cloud2026Project (VRE Folder) + //String sharedFolderId = "4d6f1fe9-8f04-45c9-a87e-ee6093553d77"; // PROD - AriadnePlus-LiDAR Dataset (simple Shared Folder) + //String sharedFolderId = "a4837a77-c1d0-4ea4-9f9d-c4bd5cf07e9a"; // PROD - Sample Reports and Templates (private Folder) + //sharedFolderId = "c141d53d-4cae-4bfc-a6f7-9b8bbd8c0e2b"; //NextNext SCOPE = "/gcube"; + SCOPE = "/d4science.research-infrastructures.eu"; TOKEN = ""; storageHubWrapper = new StorageHubWrapper(SCOPE, TOKEN); List listMembers = storageHubWrapper.getWorkspace().getSharedFolderMembers(sharedFolderId);