ref 19186: DMPoolManager - Use the dataminer user to send emails.
https://support.d4science.org/issues/19081 Added application token
This commit is contained in:
parent
c7404b13f5
commit
ef5270f5ca
6
pom.xml
6
pom.xml
|
@ -56,6 +56,10 @@
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.core</groupId>
|
||||||
|
<artifactId>common-encryption</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
@ -189,7 +193,7 @@
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
|
|
@ -12,6 +12,7 @@ import java.net.MalformedURLException;
|
||||||
***REMOVED***
|
***REMOVED***
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
***REMOVED***
|
***REMOVED***
|
||||||
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
@ -21,7 +22,12 @@ import org.apache.http.util.EntityUtils;
|
||||||
***REMOVED***
|
***REMOVED***
|
||||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||||
***REMOVED***
|
***REMOVED***
|
||||||
|
import org.gcube.common.encryption.encrypter.StringEncrypter;
|
||||||
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
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.clients.configuration.DMPMClientConfiguratorManager;
|
||||||
import org.gcube.dataanalysis.dataminer.poolmanager.util.exception.EMailException;
|
import org.gcube.dataanalysis.dataminer.poolmanager.util.exception.EMailException;
|
||||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||||
|
@ -34,13 +40,17 @@ import org.json.JSONObject;
|
||||||
|
|
||||||
public class SendMail {
|
public class SendMail {
|
||||||
|
|
||||||
private Logger logger = LoggerFactory.getLogger(SendMail.class);
|
private static final 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=",
|
private static final String WRITE_MESSAGE_ADDRESS_PATH = "2/messages/write-message?gcube-token=";
|
||||||
SOCIAL_SERVICE_QUERY_CONDITION = "$resource/Profile/ServiceName/text() eq 'SocialNetworking'",
|
private static final String USER_ROLES_ADDRESS_PATH = "2/users/get-usernames-by-role?role-name=DataMiner-Manager&gcube-token=";
|
||||||
SOCIAL_SERVICE_URI = "jersey-servlet", JSON_MIME_TYPE = "application/json";
|
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 socialServiceAddress;
|
||||||
|
private String applicationToken;
|
||||||
|
|
||||||
public SendMail() {
|
public SendMail() {
|
||||||
|
|
||||||
|
@ -51,6 +61,8 @@ public class SendMail {
|
||||||
logger.debug("Notification Subject: " + subject);
|
logger.debug("Notification Subject: " + subject);
|
||||||
logger.debug("Notification Body: " + body);
|
logger.debug("Notification Body: " + body);
|
||||||
|
|
||||||
|
applicationToken = retrieveApplicationToken();
|
||||||
|
|
||||||
retrieveSocialService();
|
retrieveSocialService();
|
||||||
|
|
||||||
String postBody = createPostBody(subject, body);
|
String postBody = createPostBody(subject, body);
|
||||||
|
@ -93,6 +105,49 @@ public class SendMail {
|
||||||
|
|
||||||
***REMOVED***
|
***REMOVED***
|
||||||
|
|
||||||
|
***REMOVED***
|
||||||
|
|
||||||
|
private String retrieveApplicationToken() throws EMailException {
|
||||||
|
try {
|
||||||
|
|
||||||
|
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()) {
|
||||||
|
logger.error("No services resource found on IS!");
|
||||||
|
***REMOVED*** else {
|
||||||
|
logger.debug("Retrieved: " + resources.get(0));
|
||||||
|
***REMOVED***
|
||||||
|
|
||||||
|
Group<AccessPoint> accessPoints = resources.get(0).profile().accessPoints();
|
||||||
|
if (!accessPoints.isEmpty()) {
|
||||||
|
Iterator<AccessPoint> iterator = accessPoints.iterator();
|
||||||
|
AccessPoint ap = iterator.next();
|
||||||
|
Group<Property> props = ap.properties();
|
||||||
|
if (!props.isEmpty()) {
|
||||||
|
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 {
|
||||||
|
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***
|
***REMOVED***
|
||||||
|
|
||||||
private void retrieveSocialService() throws EMailException {
|
private void retrieveSocialService() throws EMailException {
|
||||||
|
@ -117,7 +172,6 @@ public class SendMail {
|
||||||
|
|
||||||
***REMOVED***
|
***REMOVED***
|
||||||
|
|
||||||
|
|
||||||
private String username(String token) throws ObjectNotFound, Exception {
|
private String username(String token) throws ObjectNotFound, Exception {
|
||||||
AuthorizationEntry entry = authorizationService().get(token);
|
AuthorizationEntry entry = authorizationService().get(token);
|
||||||
logger.debug(entry.getClientInfo().getId());
|
logger.debug(entry.getClientInfo().getId());
|
||||||
|
@ -136,7 +190,7 @@ public class SendMail {
|
||||||
|
|
||||||
requestMessageBuilder.append(WRITE_MESSAGE_ADDRESS_PATH);
|
requestMessageBuilder.append(WRITE_MESSAGE_ADDRESS_PATH);
|
||||||
logger.info("Execute Post Request: " + requestMessageBuilder.toString());
|
logger.info("Execute Post Request: " + requestMessageBuilder.toString());
|
||||||
requestMessageBuilder.append(SecurityTokenProvider.instance.get());
|
requestMessageBuilder.append(applicationToken);
|
||||||
String endpoint = requestMessageBuilder.toString();
|
String endpoint = requestMessageBuilder.toString();
|
||||||
|
|
||||||
***REMOVED*** Send the request
|
***REMOVED*** Send the request
|
||||||
|
@ -243,7 +297,8 @@ public class SendMail {
|
||||||
|
|
||||||
requestMessageBuilder.append(USER_ROLES_ADDRESS_PATH);
|
requestMessageBuilder.append(USER_ROLES_ADDRESS_PATH);
|
||||||
logger.info("Request Admins Url: " + requestMessageBuilder.toString());
|
logger.info("Request Admins Url: " + requestMessageBuilder.toString());
|
||||||
requestMessageBuilder.append(SecurityTokenProvider.instance.get());
|
***REMOVED*** SecurityTokenProvider.instance.get()
|
||||||
|
requestMessageBuilder.append(applicationToken);
|
||||||
|
|
||||||
String requestAdminsUrl = requestMessageBuilder.toString();
|
String requestAdminsUrl = requestMessageBuilder.toString();
|
||||||
|
|
||||||
|
|
|
@ -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<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
||||||
|
List<ServiceEndpoint> 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<AccessPoint> accessPoints = resources.get(0).profile().accessPoints();
|
||||||
|
if (!accessPoints.isEmpty()) {
|
||||||
|
Iterator<AccessPoint> iterator = accessPoints.iterator();
|
||||||
|
AccessPoint ap = iterator.next();
|
||||||
|
Group<Property> props = ap.properties();
|
||||||
|
if (!props.isEmpty()) {
|
||||||
|
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 {
|
||||||
|
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***
|
Loading…
Reference in New Issue