diff --git a/distro/changelog.xml b/distro/changelog.xml index 136cbd0..0db5d9e 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -4,6 +4,8 @@ date="${buildDate}"> [Task #17349] Migrate ws-task-executor components to SHUB + [Incident #17506] Bug fixing + org.gcube.common authorization-client - [2.0.0-SNAPSHOT, 3-0-0-SNAPSHOT) provided + + org.gcube.core + common-encryption + compile + + + org.gcube.common.portal diff --git a/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/WsTaskExecutorWidgetServiceImpl.java b/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/WsTaskExecutorWidgetServiceImpl.java index d7261dc..37e688a 100644 --- a/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/WsTaskExecutorWidgetServiceImpl.java +++ b/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/WsTaskExecutorWidgetServiceImpl.java @@ -3,6 +3,7 @@ package org.gcube.portlets.widgets.wstaskexecutor.server; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Set; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.portal.PortalContext; @@ -24,6 +25,7 @@ import org.gcube.common.workspacetaskexecutor.shared.exception.WorkspaceFolderLo import org.gcube.common.workspacetaskexecutor.util.EncrypterUtil; import org.gcube.portlets.widgets.wstaskexecutor.client.rpc.WsTaskExecutorWidgetService; import org.gcube.portlets.widgets.wstaskexecutor.server.util.PortalContextUtil; +import org.gcube.portlets.widgets.wstaskexecutor.server.util.RuntimeResourceReader; import org.gcube.portlets.widgets.wstaskexecutor.shared.GcubeScope; import org.gcube.portlets.widgets.wstaskexecutor.shared.GcubeScopeType; import org.gcube.portlets.widgets.wstaskexecutor.shared.SelectableOperator; @@ -100,8 +102,6 @@ public class WsTaskExecutorWidgetServiceImpl extends RemoteServiceServlet implem GCubeUser user = PortalContextUtil.getUserLogged(this.getThreadLocalRequest()); String scope = PortalContextUtil.getCurrentScope(this.getThreadLocalRequest()); logger.debug("Found user "+user+ ", scope: "+scope+" into PortalContext, using them..."); - //String userToken = PortalContextUtil.getTokenFor(scope, user.getUsername()); - //SecurityTokenProvider.instance.set(userToken); ScopeProvider.instance.set(scope); logger.info("Using user "+user.getUsername()+ "and scope: "+scope+" to intancing the "+WorkspaceDataMinerTaskExecutor.class.getSimpleName()); WorkspaceDataMinerTaskExecutor exec = WorkspaceDataMinerTaskExecutor.getInstance(); @@ -116,27 +116,29 @@ public class WsTaskExecutorWidgetServiceImpl extends RemoteServiceServlet implem public List getListOfScopesForLoggedUser() throws Exception { - logger.debug("getListOfVREsForLoggedUser...: "); - GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest()); - long userId = user.getUserId(); - - // Instanciate the manager - GroupManager groupManager = new LiferayGroupManager(); + logger.debug("getListOfScopesForLoggedUser called"); List listOfScopes = new ArrayList(); if (!isWithinPortal()){ listOfScopes.add(new GcubeScope("devVRE", "/gcube/devsec/devVRE", GcubeScopeType.VRE)); listOfScopes.add(new GcubeScope("NextNext", "/gcube/devNext/NextNext", GcubeScopeType.VRE)); -// listOfScopes.add(new GcubeScope("devNext", "/gcube/devNext", GcubeScopeType.VO)); -// listOfScopes.add(new GcubeScope("devsec", "/gcube/devsec", GcubeScopeType.VO)); -// listOfScopes.add(new GcubeScope("gcube", "/gcube", GcubeScopeType.ROOT)); + listOfScopes.add(new GcubeScope("devNext", "/gcube/devNext/devNext", GcubeScopeType.VRE)); Collections.sort(listOfScopes); return listOfScopes; } try { - - List listOfGroups = groupManager.listGroupsByUser(userId); + GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest()); + long userId = user.getUserId(); + // Instanciate the manager + GroupManager groupManager = new LiferayGroupManager(); + + //FILTERING THE VREs FOR GATEWAY + long theGroupId = PortalContext.getConfiguration().getCurrentGroupId(this.getThreadLocalRequest()); + logger.info("Searching for userId {} and grouId {}", userId, theGroupId); + Set listOfGroups = groupManager.listGroupsByUserAndSiteGroupId(userId, theGroupId); + logger.info("Found {} GcubeGroups", listOfGroups.size()); + for (GCubeGroup gCubeGroup : listOfGroups) { GcubeScopeType scopeType=null; if(groupManager.isVRE(gCubeGroup.getGroupId())){ @@ -167,11 +169,21 @@ public class WsTaskExecutorWidgetServiceImpl extends RemoteServiceServlet implem logger.error("Error occurred server-side getting VRE folders: ", e); throw new Exception("Sorry, an error occurred server-side getting VRE folders, try again later"); } + + String scope = PortalContextUtil.getCurrentScope(this.getThreadLocalRequest()); + List dmScopes = new ArrayList(listOfScopes.size()); + //GETTING THE ENDPOINTS WHERE THE DM IS DEPLOYED + for (GcubeScope gcubeScope : listOfScopes) { + boolean exists = RuntimeResourceReader.serviceEndpointExists(scope, "DataMiner", "DataAnalysis"); + if(exists) + dmScopes.add(gcubeScope); + } - Collections.sort(listOfScopes); - logger.info("Returning list of VREs: "+listOfScopes); - return listOfScopes; + Collections.sort(dmScopes); + logger.info("Returning list of DM VREs where the User is registered: "+dmScopes); + return dmScopes; } + /** diff --git a/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/util/RuntimeResourceReader.java b/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/util/RuntimeResourceReader.java new file mode 100644 index 0000000..7812bbc --- /dev/null +++ b/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/util/RuntimeResourceReader.java @@ -0,0 +1,148 @@ +/** + * + */ +package org.gcube.portlets.widgets.wstaskexecutor.server.util; + +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.List; + +import org.gcube.common.encryption.StringEncrypter; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.utils.Group; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * The Class RuntimeResourceReader. + * + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * + * Sep 13, 2019 + */ +public class RuntimeResourceReader { + + public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class); + + /** + * Gets the parameters. + * + * @param scope the scope + * @param resourceName the resource name + * @param resourceCategory the resource category + * @return the parameters + * @throws Exception the exception + */ + public static ServerParameters getParameters(String scope, String resourceName, String resourceCategory) throws Exception + { + + ServerParameters parameters = new ServerParameters(); + String originalScope = null; + try{ + + ServiceEndpoint se = getServiceEndpoint(scope, resourceName, resourceCategory); + if(se.profile()==null) + throw new Exception("IS profile is null for resource: "+resourceName); + + Group accessPoints = se.profile().accessPoints(); + if(accessPoints.size()==0) throw new Exception("Accesspoint in resource "+resourceName+" not found"); + + AccessPoint ap = accessPoints.iterator().next(); + parameters.setUrl(ap.address()); + parameters.setUser(ap.username()); //username + + String decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password()); + parameters.setPassword(decryptedPassword); //password + + + }catch (Exception e) { + logger.error("Sorry, an error occurred on reading parameters in Runtime Resources",e); + }finally{ + if(originalScope!=null && !originalScope.isEmpty()){ + ScopeProvider.instance.set(originalScope); + logger.info("scope provider setted to orginal scope: "+originalScope); + }else{ + ScopeProvider.instance.reset(); + logger.info("scope provider reset"); + } + } + + return parameters; + } + + + /** + * Service endpoint exists. + * + * @param scope the scope + * @param resourceName the resource name + * @param resourceCategory the resource category + * @return true, if successful + */ + public static boolean serviceEndpointExists(String scope, String resourceName, String resourceCategory) { + + ServiceEndpoint sEp = getServiceEndpoint(scope, resourceName, resourceCategory); + return sEp!=null; + + } + + + /** + * Gets the service endpoint. + * + * @param scope the scope + * @param resourceName the resource name + * @param resourceCategory the resource category + * @return the service endpoint found. Otherwise null. + */ + public static ServiceEndpoint getServiceEndpoint(String scope, String resourceName, String resourceCategory) { + + String originalScope = null; + List listEndPoints = null; + try{ + + originalScope = ScopeProvider.instance.get(); + logger.info("Setting scope: {}", scope); + ScopeProvider.instance.set(scope); + + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Name/string() eq '"+resourceName+"'"); + + if(resourceCategory!=null && !resourceCategory.isEmpty()) { + query.addCondition("$resource/Profile/Category/string() eq '"+resourceCategory+"'"); + } + + logger.info("Searching the RR with Profile/Name '{}' and Profile/Category '{}'", resourceName, resourceCategory); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + listEndPoints = client.submit(query); + + }catch (Exception e) { + logger.error("Unexpeted error occurred on searching the EndPoint: ",e); + + }finally{ + if(originalScope!=null && !originalScope.isEmpty()){ + ScopeProvider.instance.set(originalScope); + logger.info("scope provider setted to orginal scope: "+originalScope); + }else{ + ScopeProvider.instance.reset(); + logger.info("scope provider reset"); + } + } + + if (listEndPoints == null || listEndPoints.isEmpty()) { + logger.info("No RR found with Profile/Name '{}' and Profile/Category '{}'"+ " in the scope "+scope , resourceName, resourceCategory); + return null; + } + + ServiceEndpoint sEp = listEndPoints.get(0); //Returning the first EndPoint. I'm assuming that only one instance exists for input RR + logger.info("{} found in the scope {}",ServiceEndpoint.class.getSimpleName(),scope); + return sEp; + } + +} diff --git a/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/util/ServerParameters.java b/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/util/ServerParameters.java new file mode 100644 index 0000000..51dd19d --- /dev/null +++ b/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/server/util/ServerParameters.java @@ -0,0 +1,110 @@ +package org.gcube.portlets.widgets.wstaskexecutor.server.util; + +import java.io.Serializable; + + +/** + * The Class ServerParameters. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 16, 2017 + */ +public class ServerParameters implements Serializable{ + + + /** + * + */ + private static final long serialVersionUID = -507665481374175091L; + protected String url; + protected String user; + protected String password; + + /** + * Instantiates a new server parameters. + */ + public ServerParameters(){} + + /** + * Instantiates a new server parameters. + * + * @param url the url + * @param user the user + * @param password the password + */ + public ServerParameters(String url, String user, String password) { + this.url = url; + this.user = user; + this.password = password; + } + + /** + * Gets the url. + * + * @return the url + */ + public String getUrl() { + return url; + } + + /** + * Sets the url. + * + * @param url the url to set + */ + public void setUrl(String url) { + this.url = url; + } + + /** + * Gets the user. + * + * @return the user + */ + public String getUser() { + return user; + } + + /** + * Sets the user. + * + * @param user the user to set + */ + public void setUser(String user) { + this.user = user; + } + + /** + * Gets the password. + * + * @return the password + */ + public String getPassword() { + return password; + } + + /** + * Sets the password. + * + * @param password the password to set + */ + public void setPassword(String password) { + this.password = password; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("ServerParameters [url="); + builder.append(url); + builder.append(", user="); + builder.append(user); + builder.append(", password="); + builder.append(password); + builder.append("]"); + return builder.toString(); + } +} diff --git a/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/shared/GcubeScope.java b/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/shared/GcubeScope.java index bc2cc7c..8e42c97 100644 --- a/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/shared/GcubeScope.java +++ b/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/shared/GcubeScope.java @@ -115,8 +115,8 @@ public class GcubeScope implements Serializable, IsSerializable, Comparable listOfScopes = new ArrayList(); + //WsTaskExecutorWidgetServiceImpl sImpl = new WsTaskExecutorWidgetServiceImpl(); + listOfScopes.add(new GcubeScope("devVRE", "/gcube/devsec/devVRE", GcubeScopeType.VRE)); + listOfScopes.add(new GcubeScope("NextNext", "/gcube/devNext/NextNext", GcubeScopeType.VRE)); + + for (GcubeScope gcubeScope : listOfScopes) { + boolean exists = RuntimeResourceReader.serviceEndpointExists(gcubeScope.getScopeName(), "DataMiner", "DataAnalysis"); + System.out.println("Is the DataMiner deployed in the scope: "+gcubeScope.getScopeName()+"? "+exists); + } + + + WsTaskExecutorWidgetServiceImpl wsImpl = new WsTaskExecutorWidgetServiceImpl(); + List listScopes = wsImpl.getListOfScopesForLoggedUser(); + + for (GcubeScope gcubeScope : listScopes) { + System.out.println(gcubeScope.getScopeTitle()); + } + System.out.println("\n\n"); + for(int i=0; i