From ef5270f5ca1d585c297667570ed6152335f4f5ce Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Thu, 30 Apr 2020 18:48:27 +0200 Subject: [PATCH] ref 19186: DMPoolManager - Use the dataminer user to send emails. https://support.d4science.org/issues/19081 Added application token --- pom.xml | 6 +- .../dataminer/poolmanager/util/SendMail.java | 71 ++++++++++++++++--- .../ApplicationTokenTest.java | 69 ++++++++++++++++++ 3 files changed, 137 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/gcube/dataanalysis/dataminerpoolmanager/ApplicationTokenTest.java diff --git a/pom.xml b/pom.xml index 91df139..cff6d30 100644 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,10 @@ provided + + org.gcube.core + common-encryption + org.apache.httpcomponents @@ -189,7 +193,7 @@ - + org.apache.maven.plugins maven-assembly-plugin diff --git a/src/main/java/org/gcube/dataanalysis/dataminer/poolmanager/util/SendMail.java b/src/main/java/org/gcube/dataanalysis/dataminer/poolmanager/util/SendMail.java index b304982..5e7d799 100644 --- a/src/main/java/org/gcube/dataanalysis/dataminer/poolmanager/util/SendMail.java +++ b/src/main/java/org/gcube/dataanalysis/dataminer/poolmanager/util/SendMail.java @@ -12,6 +12,7 @@ import java.net.MalformedURLException; ***REMOVED*** import java.net.URLConnection; import java.util.ArrayList; +import java.util.Iterator; ***REMOVED*** import org.apache.http.client.methods.HttpGet; @@ -21,7 +22,12 @@ import org.apache.http.util.EntityUtils; ***REMOVED*** import org.gcube.common.authorization.library.AuthorizationEntry; ***REMOVED*** +import org.gcube.common.encryption.encrypter.StringEncrypter; import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.Property; +import org.gcube.common.resources.gcore.utils.Group; import org.gcube.dataanalysis.dataminer.poolmanager.clients.configuration.DMPMClientConfiguratorManager; import org.gcube.dataanalysis.dataminer.poolmanager.util.exception.EMailException; import org.gcube.resources.discovery.client.api.DiscoveryClient; @@ -34,13 +40,17 @@ import org.json.JSONObject; public class SendMail { - private Logger logger = LoggerFactory.getLogger(SendMail.class); - private final String WRITE_MESSAGE_ADDRESS_PATH = "2/messages/write-message?gcube-token=", - USER_ROLES_ADDRESS_PATH = "2/users/get-usernames-by-role?role-name=DataMiner-Manager&gcube-token=", - SOCIAL_SERVICE_QUERY_CONDITION = "$resource/Profile/ServiceName/text() eq 'SocialNetworking'", - SOCIAL_SERVICE_URI = "jersey-servlet", JSON_MIME_TYPE = "application/json"; + private static final Logger logger = LoggerFactory.getLogger(SendMail.class); + + private static final String WRITE_MESSAGE_ADDRESS_PATH = "2/messages/write-message?gcube-token="; + private static final String USER_ROLES_ADDRESS_PATH = "2/users/get-usernames-by-role?role-name=DataMiner-Manager&gcube-token="; + private static final String SOCIAL_SERVICE_QUERY_CONDITION = "$resource/Profile/ServiceName/text() eq 'SocialNetworking'"; + private static final String SOCIAL_SERVICE_URI = "jersey-servlet", JSON_MIME_TYPE = "application/json"; + private static final String DMPOOLMANAGER_SERVICE_QUERY_CONDITION = "$resource/Profile/Name/text() eq 'DMPoolManager'"; + private String socialServiceAddress; + private String applicationToken; public SendMail() { @@ -51,6 +61,8 @@ public class SendMail { logger.debug("Notification Subject: " + subject); logger.debug("Notification Body: " + body); + applicationToken = retrieveApplicationToken(); + retrieveSocialService(); String postBody = createPostBody(subject, body); @@ -93,6 +105,49 @@ public class SendMail { ***REMOVED*** +***REMOVED*** + + private String retrieveApplicationToken() throws EMailException { + try { + + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition(DMPOOLMANAGER_SERVICE_QUERY_CONDITION); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List resources = client.submit(query); + if (resources.isEmpty()) { + logger.error("No services resource found on IS!"); + ***REMOVED*** else { + logger.debug("Retrieved: " + resources.get(0)); + ***REMOVED*** + + Group accessPoints = resources.get(0).profile().accessPoints(); + if (!accessPoints.isEmpty()) { + Iterator iterator = accessPoints.iterator(); + AccessPoint ap = iterator.next(); + Group props = ap.properties(); + if (!props.isEmpty()) { + Iterator iteratorProps = props.iterator(); + Property p = iteratorProps.next(); + String applicationToken = StringEncrypter.getEncrypter().decrypt(p.value()); + logger.debug("Application token found: " + applicationToken); + return applicationToken; + + ***REMOVED*** else { + String error = "DMPoolManager application token not found in service resource on IS!"; + logger.error(error); + throw new EMailException(error); + ***REMOVED*** + ***REMOVED*** else { + String error = "DMPoolManager invalid service resource on IS!"; + logger.error(error); + throw new EMailException(error); + ***REMOVED*** + + ***REMOVED*** catch (Throwable e) { + logger.error("DMPoolManager application token not found: " + e.getLocalizedMessage(), e); + throw new EMailException("DMPoolManager application token not found: " + e.getLocalizedMessage(), e); + ***REMOVED*** + ***REMOVED*** private void retrieveSocialService() throws EMailException { @@ -117,7 +172,6 @@ public class SendMail { ***REMOVED*** - private String username(String token) throws ObjectNotFound, Exception { AuthorizationEntry entry = authorizationService().get(token); logger.debug(entry.getClientInfo().getId()); @@ -136,7 +190,7 @@ public class SendMail { requestMessageBuilder.append(WRITE_MESSAGE_ADDRESS_PATH); logger.info("Execute Post Request: " + requestMessageBuilder.toString()); - requestMessageBuilder.append(SecurityTokenProvider.instance.get()); + requestMessageBuilder.append(applicationToken); String endpoint = requestMessageBuilder.toString(); ***REMOVED*** Send the request @@ -243,7 +297,8 @@ public class SendMail { requestMessageBuilder.append(USER_ROLES_ADDRESS_PATH); logger.info("Request Admins Url: " + requestMessageBuilder.toString()); - requestMessageBuilder.append(SecurityTokenProvider.instance.get()); + ***REMOVED*** SecurityTokenProvider.instance.get() + requestMessageBuilder.append(applicationToken); String requestAdminsUrl = requestMessageBuilder.toString(); diff --git a/src/test/java/org/gcube/dataanalysis/dataminerpoolmanager/ApplicationTokenTest.java b/src/test/java/org/gcube/dataanalysis/dataminerpoolmanager/ApplicationTokenTest.java new file mode 100644 index 0000000..460ec94 --- /dev/null +++ b/src/test/java/org/gcube/dataanalysis/dataminerpoolmanager/ApplicationTokenTest.java @@ -0,0 +1,69 @@ +package org.gcube.dataanalysis.dataminerpoolmanager; + +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.Iterator; +***REMOVED*** + +***REMOVED*** +import org.gcube.common.encryption.encrypter.StringEncrypter; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.Property; +import org.gcube.common.resources.gcore.utils.Group; +***REMOVED*** +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; + +public class ApplicationTokenTest { + + private static final String DMPOOLMANAGER_SERVICE_QUERY_CONDITION = "$resource/Profile/Name/text() eq 'DMPoolManager'"; + + public static void main(String[] args) { + ScopeProvider.instance.set("/gcube/devsec/devVRE"); + SecurityTokenProvider.instance.set("a4692fb2-b9bc-40b9-bfda-e8349b14c381-98187548"); + + decryptToken(); +***REMOVED*** + + private static void decryptToken() { + try { + + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition(DMPOOLMANAGER_SERVICE_QUERY_CONDITION); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List resources = client.submit(query); + if (resources.isEmpty()) { + System.out.println("No services resource found on IS!"); + + ***REMOVED*** else { + System.out.println("Retrieved: " + resources.get(0)); + ***REMOVED*** + + + Group accessPoints = resources.get(0).profile().accessPoints(); + if (!accessPoints.isEmpty()) { + Iterator iterator = accessPoints.iterator(); + AccessPoint ap = iterator.next(); + Group props = ap.properties(); + if (!props.isEmpty()) { + Iterator iteratorProps = props.iterator(); + Property p = iteratorProps.next(); + String applicationToken = StringEncrypter.getEncrypter().decrypt(p.value()); + System.out.println("Application token found: " + applicationToken); + + ***REMOVED*** else { + System.out.println("No application token found in service resource on IS!"); + ***REMOVED*** + ***REMOVED*** else { + System.out.println("Invalid service resource on IS!"); + + ***REMOVED*** + + ***REMOVED*** catch (Exception e) { + e.printStackTrace(); + ***REMOVED*** +***REMOVED*** + +***REMOVED***