ref 19186: DMPoolManager - Use the dataminer user to send emails.

https://support.d4science.org/issues/19081

Added application token
This commit is contained in:
Giancarlo Panichi 2020-04-30 18:48:27 +02:00
parent 67198f95bf
commit ceb23ca8a5
3 changed files with 137 additions and 9 deletions

View File

@ -56,6 +56,10 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
@ -189,7 +193,7 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>

View File

@ -12,6 +12,7 @@ import java.io.OutputStreamWriter;
***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 ***REMOVED***
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() ***REMOVED***
@ -51,6 +61,8 @@ public class SendMail ***REMOVED***
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***
***REMOVED***
private String retrieveApplicationToken() throws EMailException ***REMOVED***
try ***REMOVED***
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition(DMPOOLMANAGER_SERVICE_QUERY_CONDITION);
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> resources = client.submit(query);
if (resources.isEmpty()) ***REMOVED***
logger.error("No services resource found on IS!");
***REMOVED*** else ***REMOVED***
logger.debug("Retrieved: " + resources.get(0));
***REMOVED***
Group<AccessPoint> accessPoints = resources.get(0).profile().accessPoints();
if (!accessPoints.isEmpty()) ***REMOVED***
Iterator<AccessPoint> iterator = accessPoints.iterator();
AccessPoint ap = iterator.next();
Group<Property> props = ap.properties();
if (!props.isEmpty()) ***REMOVED***
Iterator<Property> iteratorProps = props.iterator();
Property p = iteratorProps.next();
String applicationToken = StringEncrypter.getEncrypter().decrypt(p.value());
logger.debug("Application token found: " + applicationToken);
return applicationToken;
***REMOVED*** else ***REMOVED***
String error = "DMPoolManager application token not found in service resource on IS!";
logger.error(error);
throw new EMailException(error);
***REMOVED***
***REMOVED*** else ***REMOVED***
String error = "DMPoolManager invalid service resource on IS!";
logger.error(error);
throw new EMailException(error);
***REMOVED***
***REMOVED*** catch (Throwable e) ***REMOVED***
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 ***REMOVED***
@ -117,7 +172,6 @@ public class SendMail ***REMOVED***
***REMOVED***
private String username(String token) throws ObjectNotFound, Exception ***REMOVED***
AuthorizationEntry entry = authorizationService().get(token);
logger.debug(entry.getClientInfo().getId());
@ -136,7 +190,7 @@ public class SendMail ***REMOVED***
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 ***REMOVED***
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();

View File

@ -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 ***REMOVED***
private static final String DMPOOLMANAGER_SERVICE_QUERY_CONDITION = "$resource/Profile/Name/text() eq 'DMPoolManager'";
public static void main(String[] args) ***REMOVED***
ScopeProvider.instance.set("/gcube/devsec/devVRE");
SecurityTokenProvider.instance.set("a4692fb2-b9bc-40b9-bfda-e8349b14c381-98187548");
decryptToken();
***REMOVED***
private static void decryptToken() ***REMOVED***
try ***REMOVED***
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition(DMPOOLMANAGER_SERVICE_QUERY_CONDITION);
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> resources = client.submit(query);
if (resources.isEmpty()) ***REMOVED***
System.out.println("No services resource found on IS!");
***REMOVED*** else ***REMOVED***
System.out.println("Retrieved: " + resources.get(0));
***REMOVED***
Group<AccessPoint> accessPoints = resources.get(0).profile().accessPoints();
if (!accessPoints.isEmpty()) ***REMOVED***
Iterator<AccessPoint> iterator = accessPoints.iterator();
AccessPoint ap = iterator.next();
Group<Property> props = ap.properties();
if (!props.isEmpty()) ***REMOVED***
Iterator<Property> iteratorProps = props.iterator();
Property p = iteratorProps.next();
String applicationToken = StringEncrypter.getEncrypter().decrypt(p.value());
System.out.println("Application token found: " + applicationToken);
***REMOVED*** else ***REMOVED***
System.out.println("No application token found in service resource on IS!");
***REMOVED***
***REMOVED*** else ***REMOVED***
System.out.println("Invalid service resource on IS!");
***REMOVED***
***REMOVED*** catch (Exception e) ***REMOVED***
e.printStackTrace();
***REMOVED***
***REMOVED***
***REMOVED***