diff --git a/.classpath b/.classpath index 27073a0..045159b 100644 --- a/.classpath +++ b/.classpath @@ -29,6 +29,5 @@ - diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java index bcb369c..53290b1 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java @@ -3,6 +3,8 @@ package org.gcube.datatransfer.resolver.services; import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; +import java.io.BufferedInputStream; +import java.io.InputStream; import java.util.Collection; import java.util.List; @@ -13,6 +15,15 @@ import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.HttpClientBuilder; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.encryption.StringEncrypter; import org.gcube.common.resources.gcore.ServiceEndpoint; @@ -58,7 +69,7 @@ public class WekeoResolver { public Response getToken(@Context HttpServletRequest req) throws WebApplicationException{ logger.info(this.getClass().getSimpleName()+" getToken starts..."); - String wekeoToken = ""; + String wekeoToken = null; try { @@ -73,15 +84,64 @@ public class WekeoResolver { throw ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI); } - List endPoints = getConfigurationFromIS(); + AccessPoint wekeoAccessPoint = readWekeoServiceEndpoint(req, scope); - if(endPoints==null || endPoints.size()==0) { - String error = String.format("Missing the RR with Name '%s' and Category '%s' in the scope '%s'. Please contact the support.",RUNTIME_WKEO_RESOURCE_NAME,CATEGORY_WEKEO_TYPE,scope); - throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + if(wekeoAccessPoint!=null) { + + String wekeoUsername = wekeoAccessPoint.username(); + String wekeoAddress = wekeoAccessPoint.address(); + String wekeoPwd = wekeoAccessPoint.password(); + + //printing the access point found + if(logger.isDebugEnabled()) { + String msg = String.format("Found the username %s and the address %s to perform the request", wekeoUsername, wekeoAddress); + logger.debug(msg); + } + + //decrypting the pwd + if(wekeoPwd!=null){ + wekeoPwd = StringEncrypter.getEncrypter().decrypt(wekeoAccessPoint.password()); + logger.info("Returning decrypted pwd registered into "+RUNTIME_WKEO_RESOURCE_NAME +" SE: "+wekeoPwd.substring(0,wekeoPwd.length()/2)+"...."); + } + + if(wekeoUsername!=null && wekeoPwd!=null & wekeoAddress!=null) { + + try { + //performing the HTTP request with Basic Authentication + CredentialsProvider provider = new BasicCredentialsProvider(); + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(wekeoUsername, wekeoPwd); + provider.setCredentials(AuthScope.ANY, credentials); + + HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); + logger.info("calling the URL and performing basic authentication to: "+wekeoAddress); + HttpResponse response = client.execute(new HttpGet(wekeoAddress)); + int statusCode = response.getStatusLine().getStatusCode(); + logger.info("the response stus code is: "+statusCode); + if(statusCode == 200) { + InputStream is = response.getEntity().getContent(); + BufferedInputStream bif = new BufferedInputStream(is); + wekeoToken = bif.toString(); + logger.info("got the wekeo token: "+wekeoToken.substring(0,wekeoToken.length()/2)+"...."); + }else { + String error = String.format("The request to %s returned status code %d",wekeoAddress,statusCode); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } + }catch (Exception e) { + String error = String.format("Error on performing request to %s",wekeoAddress); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } + + }else { + String error = String.format("I cannot read the configurations (adress, username,password) from %s in the scope %s",RUNTIME_WKEO_RESOURCE_NAME,scope); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } } - String wekeoEndPoint = readWekeoServiceEndpoint(req, scope); - + //to be sure + if(wekeoToken==null) { + String error = String.format("Sorry an rrror occured on getting the wekeo token. Please, retry the request"); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } return Response.ok(wekeoToken).build(); }catch (Exception e) { @@ -92,24 +152,6 @@ public class WekeoResolver { } - /** - * Retrieve the wekeo endpoint information from IS. - * - * @return list of endpoints for ckan database - * @throws Exception the exception - */ - private static List getConfigurationFromIS() throws Exception{ - - SimpleQuery query = queryFor(ServiceEndpoint.class); - query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_WKEO_RESOURCE_NAME +"'"); - query.addCondition("$resource/Profile/Cateogory/Name/text() eq '"+ CATEGORY_WEKEO_TYPE +"'"); - DiscoveryClient client = clientFor(ServiceEndpoint.class); - List toReturn = client.submit(query); - return toReturn; - - } - - /** * Reads the wekeo endpoint information from IS. {The SE name is: @link WekeoResolver#RUNTIME_WKEO_RESOURCE_NAME} * @@ -117,10 +159,9 @@ public class WekeoResolver { * @param scope the scope * @return the string */ - private static String readWekeoServiceEndpoint(HttpServletRequest req, String scope){ + public static AccessPoint readWekeoServiceEndpoint(HttpServletRequest req, String scope){ String callerScope = null; - String gCubeAppToken = null; try{ callerScope = ScopeProvider.instance.get(); ScopeProvider.instance.set(scope); @@ -141,37 +182,22 @@ public class WekeoResolver { throw ExceptionManager.internalErrorException(req, errorMessage, AnalyticsCreateResolver.class, helpURI); } - + String wekeoUsername = null; + String wekeoPwd = null; + ServiceEndpoint se = toReturn.get(0); Collection theAccessPoints = se.profile().accessPoints().asCollection(); for (AccessPoint accessPoint : theAccessPoints) { - Collection properties = accessPoint.properties().asCollection(); - for (Property property : properties) { -// if(property.name().equalsIgnoreCase(GCUBE_TOKEN)){ -// logger.info("gcube-token as property was found, returning it"); -// gCubeAppToken = property.value(); -// break; -// } + wekeoUsername = accessPoint.username(); + wekeoPwd = accessPoint.password(); + if(wekeoUsername!=null && wekeoPwd!=null) { + logger.info("returning the access point with name: "+accessPoint.name()); + return accessPoint; } - - if(gCubeAppToken!=null) - break; - } - - if(gCubeAppToken!=null){ - String decryptedPassword = StringEncrypter.getEncrypter().decrypt(gCubeAppToken); - logger.info("Returning decrypted Application Token registered into "+RUNTIME_WKEO_RESOURCE_NAME +" SE: "+decryptedPassword.substring(0,decryptedPassword.length()/2)+"...."); - return decryptedPassword; } return null; -// -// String errorMessage = "No "+GCUBE_TOKEN+" as Property saved in the "+RUNTIME_WKEO_RESOURCE_NAME+" SE registered in the scope: "+ScopeProvider.instance.get(); -// logger.error(errorMessage); -// throw ExceptionManager.internalErrorException(req, errorMessage, AnalyticsCreateResolver.class, helpURI); - - }catch(Exception e){ String errorMessage = "Error occurred on reading the "+RUNTIME_WKEO_RESOURCE_NAME+" SE registered in the scope: "+ScopeProvider.instance.get(); logger.error(errorMessage, e); diff --git a/src/main/java/org/gcube/datatransfer/resolver/util/HttpRequestUtil.java b/src/main/java/org/gcube/datatransfer/resolver/util/HttpRequestUtil.java index 936431c..a652d6a 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/util/HttpRequestUtil.java +++ b/src/main/java/org/gcube/datatransfer/resolver/util/HttpRequestUtil.java @@ -120,14 +120,4 @@ public class HttpRequestUtil { return false; } - - /** - * The main method. - * - * @param args the arguments - * @throws Exception the exception - */ - public static void main(String[] args) throws Exception { - System.out.println(HttpRequestUtil.urlExists("http://geoserver2.d4science.research-infrastructures.eu/geoserver/wms", true)); - } } diff --git a/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java b/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java new file mode 100644 index 0000000..468e5a4 --- /dev/null +++ b/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java @@ -0,0 +1,114 @@ +package org.gcube.datatransfer.test; +import java.io.BufferedInputStream; +import java.io.InputStream; + +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.HttpClientBuilder; +import org.gcube.common.encryption.StringEncrypter; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.datatransfer.resolver.services.WekeoResolver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class WekeoResolverTest. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Mar 31, 2021 + */ +public class WekeoResolverTest { + + private static Logger logger = LoggerFactory.getLogger(WekeoResolverTest.class); + + private final static String RUNTIME_WKEO_RESOURCE_NAME = "WekeoDataBroker"; + + public static final String scope = "/gcube/devsec/devVRE"; + + public static void main(String[] args) { + + try { + + logger.info("starts..."); + String wekeoToken = null; + + AccessPoint wekeoAccessPoint = WekeoResolver.readWekeoServiceEndpoint(null, scope); + + if (wekeoAccessPoint != null) { + + String wekeoUsername = wekeoAccessPoint.username(); + String wekeoAddress = wekeoAccessPoint.address(); + String wekeoPwd = wekeoAccessPoint.password(); + + // printing the access point found + if (logger.isDebugEnabled()) { + String msg = String.format("Found the username %s and the address %s to perform the request", + wekeoUsername, wekeoAddress); + logger.debug(msg); + } + + // decrypting the pwd + if (wekeoPwd != null) { + wekeoPwd = StringEncrypter.getEncrypter().decrypt(wekeoAccessPoint.password()); + logger.info("Returning decrypted pwd registered into " + RUNTIME_WKEO_RESOURCE_NAME + " SE: " + + wekeoPwd.substring(0, wekeoPwd.length() / 2) + "...."); + } + + if (wekeoUsername != null && wekeoPwd != null & wekeoAddress != null) { + + try { + // performing the HTTP request with Basic Authentication + CredentialsProvider provider = new BasicCredentialsProvider(); + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(wekeoUsername, + wekeoPwd); + provider.setCredentials(AuthScope.ANY, credentials); + + HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); + logger.info("calling the URL and performing basic authentication to: " + wekeoAddress); + HttpResponse response = client.execute(new HttpGet(wekeoAddress)); + int statusCode = response.getStatusLine().getStatusCode(); + logger.info("the response stus code is: " + statusCode); + if (statusCode == 200) { + InputStream is = response.getEntity().getContent(); + BufferedInputStream bif = new BufferedInputStream(is); + wekeoToken = bif.toString(); + logger.info("got the wekeo token: " + wekeoToken.substring(0, wekeoToken.length() / 2) + + "...."); + } else { + String error = String.format("The request to %s returned status code %d", wekeoAddress, + statusCode); + throw new Exception(error); + } + } catch (Exception e) { + String error = String.format("Error on performing request to %s", wekeoAddress); + throw new Exception(error); + } + + } else { + String error = String.format( + "I cannot read the configurations (adress, username,password) from %s in the scope %s", + RUNTIME_WKEO_RESOURCE_NAME, scope); + throw new Exception(error); + } + } + + // to be sure + if (wekeoToken == null) { + String error = String + .format("Sorry an rrror occured on getting the wekeo token. Please, retry the request"); + throw new Exception(error); + } + + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + + } + +} diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore index c04ed84..91f56a4 100644 --- a/src/test/resources/.gitignore +++ b/src/test/resources/.gitignore @@ -11,3 +11,4 @@ /gcube.gcubekey /preprod.gcubekey /pred4s.gcubekey +/log4j.properties