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
81da29e597
commit
ef117e220c
6
pom.xml
6
pom.xml
|
@ -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>
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.net.MalformedURLException;
|
|||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
|
@ -21,7 +22,12 @@ import org.apache.http.util.EntityUtils;
|
|||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
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.slf4j.LoggerFactory;
|
|||
|
||||
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);
|
||||
|
@ -95,6 +107,49 @@ public class SendMail {
|
|||
|
||||
}
|
||||
|
||||
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!");
|
||||
} else {
|
||||
logger.debug("Retrieved: " + resources.get(0));
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
} else {
|
||||
String error = "DMPoolManager application token not found in service resource on IS!";
|
||||
logger.error(error);
|
||||
throw new EMailException(error);
|
||||
}
|
||||
} else {
|
||||
String error = "DMPoolManager invalid service resource on IS!";
|
||||
logger.error(error);
|
||||
throw new EMailException(error);
|
||||
}
|
||||
|
||||
} catch (Throwable e) {
|
||||
logger.error("DMPoolManager application token not found: " + e.getLocalizedMessage(), e);
|
||||
throw new EMailException("DMPoolManager application token not found: " + e.getLocalizedMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void retrieveSocialService() throws EMailException {
|
||||
try {
|
||||
SimpleQuery query = queryFor(GCoreEndpoint.class);
|
||||
|
@ -117,7 +172,6 @@ public class SendMail {
|
|||
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
|
||||
// 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());
|
||||
// SecurityTokenProvider.instance.get()
|
||||
requestMessageBuilder.append(applicationToken);
|
||||
|
||||
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;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
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;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
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();
|
||||
}
|
||||
|
||||
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!");
|
||||
|
||||
} else {
|
||||
System.out.println("Retrieved: " + resources.get(0));
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
} else {
|
||||
System.out.println("No application token found in service resource on IS!");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Invalid service resource on IS!");
|
||||
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue