Compare commits

...

4 Commits

Author SHA1 Message Date
luca.frosini 0b6dfd8123 Ignored MacOs File 2023-06-21 11:28:45 +02:00
Luca Frosini c1f283486a Removed -SNAPSHOT for release 2022-05-19 10:52:56 +02:00
Luca Frosini 1e6fa8b9f6 Removed old uneeded code 2022-05-16 11:07:01 +02:00
Luca Frosini d93000d280 Fixes issues #23310 #23309 #23306 2022-05-11 15:24:43 +02:00
6 changed files with 141 additions and 155 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
target target
/.DS_Store

View File

@ -3,6 +3,13 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v1.4.0]
- Removed old code which set the user role in ckan [#23310]
- Fixed gcat discovery [#23309]
- Supporting new authorization [#23306]
## [v1.3.0] ## [v1.3.0]
- updated gcat-client version [#21530] - updated gcat-client version [#21530]

14
pom.xml
View File

@ -11,7 +11,7 @@
<groupId>org.gcube.data.access</groupId> <groupId>org.gcube.data.access</groupId>
<artifactId>ckan-connector</artifactId> <artifactId>ckan-connector</artifactId>
<version>1.3.0</version> <version>1.4.0</version>
<packaging>war</packaging> <packaging>war</packaging>
<name>ckan connector</name> <name>ckan connector</name>
<description>a ckan connector for automatic login</description> <description>a ckan connector for automatic login</description>
@ -32,7 +32,7 @@
<dependency> <dependency>
<groupId>org.gcube.distribution</groupId> <groupId>org.gcube.distribution</groupId>
<artifactId>gcube-smartgears-bom</artifactId> <artifactId>gcube-smartgears-bom</artifactId>
<version>2.1.0</version> <version>2.1.1</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
@ -47,11 +47,11 @@
<version>2.5.2</version> <version>2.5.2</version>
</dependency> </dependency>
<dependency> <!-- <dependency> -->
<groupId>eu.trentorise.opendata</groupId> <!-- <groupId>eu.trentorise.opendata</groupId> -->
<artifactId>jackan</artifactId> <!-- <artifactId>jackan</artifactId> -->
<version>0.4.2</version> <!-- <version>0.4.2</version> -->
</dependency> <!-- </dependency> -->
<!-- smartgears --> <!-- smartgears -->
<dependency> <dependency>

View File

@ -10,6 +10,5 @@ public class CKanConnector extends ResourceConfig {
public CKanConnector(){ public CKanConnector(){
packages("org.gcube.data.access.ckanconnector"); packages("org.gcube.data.access.ckanconnector");
} }
} }

View File

@ -14,9 +14,6 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import lombok.extern.slf4j.Slf4j;
import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
@ -26,14 +23,7 @@ import org.python.core.PyObject;
import org.python.core.PyString; import org.python.core.PyString;
import org.python.util.PythonInterpreter; import org.python.util.PythonInterpreter;
import eu.trentorise.opendata.jackan.CkanClient; import lombok.extern.slf4j.Slf4j;
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse;
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpStatus;
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpPost;
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder;
import eu.trentorise.opendata.jackan.model.CkanUser;
@Path("/") @Path("/")
@Slf4j @Slf4j
@ -78,41 +68,44 @@ public class ConnectorManager {
@GET @GET
public Response connect(@PathParam(value = "pathInfo") String path, @Context HttpServletRequest req, @QueryParam(value="listOfVres") String vres ) { public Response connect(@PathParam(value = "pathInfo") String path, @Context HttpServletRequest req, @QueryParam(value="listOfVres") String vres ) {
try{ try{
if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build();
// This is done by smartgears
// if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build();
log.info("passed path is {}",path); log.info("passed path is {}",path);
String ckanKey = context.getInitParameter("ckanKey"); String ckanKey = context.getInitParameter("ckanKey");
String originalUserName = AuthorizationProvider.instance.get().getClient().getId(); String originalUserName = AuthorizationProvider.instance.get().getClient().getId();
String changedUserName = originalUserName.replace(".", "_"); String changedUserName = originalUserName.replace(".", "_");
User user = new User();
user.read(changedUserName);
int internalPort = Integer.parseInt(context.getInitParameter("internalPort")); int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));
String localhostName = "http://127.0.0.1:"+internalPort; String localhostName = "http://127.0.0.1:"+internalPort;
long startCheckUser = System.currentTimeMillis(); long startCheckUser = System.currentTimeMillis();
CkanClient ckanClient = new CkanClient(localhostName, ckanKey); // CkanClient ckanClient = new CkanClient(localhostName, ckanKey);
// try {
try { // User user = new User();
User user = new User(); // user.read(changedUserName);
user.read(changedUserName); // }catch (Throwable tr) {
}catch (Throwable tr) { // log.error("Error while contacting gCat. The old code will made the work", tr);
log.error("Error while contacting gCat. The old code will made the work", tr); // CkanUser user = null;
CkanUser user = null; // try{
try{ // user = ckanClient.getUser(changedUserName);
user = ckanClient.getUser(changedUserName); // }catch(Exception e){
}catch(Exception e){ // log.warn("user {} doesn't exist, the system will create it",originalUserName, e);
log.warn("user {} doesn't exist, the system will create it",originalUserName, e); // }
} // log.info("checking user took {}",(System.currentTimeMillis()-startCheckUser));
log.info("checking user took {}",(System.currentTimeMillis()-startCheckUser)); // if (user==null){
if (user==null){ // long startCreateUser = System.currentTimeMillis();
long startCreateUser = System.currentTimeMillis(); // user = ckanClient.createUser(new CkanUser(changedUserName, originalUserName+"@gcube.ckan.org" , randomString.nextString() ));
user = ckanClient.createUser(new CkanUser(changedUserName, originalUserName+"@gcube.ckan.org" , randomString.nextString() )); // log.info("create user took {}",(System.currentTimeMillis()-startCreateUser));
log.info("create user took {}",(System.currentTimeMillis()-startCreateUser)); // }
} // }
} //
// addUserToVres(vres, changedUserName, ckanClient, ckanKey, localhostName);
addUserToVres(vres, changedUserName, ckanClient, ckanKey, localhostName); //
log.info("logging {} in scope {}",originalUserName, ScopeProvider.instance.get()); log.info("logging {} in scope {}",originalUserName, ScopeProvider.instance.get());
return createResponse(changedUserName, path, req.getQueryString()); return createResponse(changedUserName, path, req.getQueryString());
}catch(Exception e){ }catch(Exception e){
@ -122,17 +115,17 @@ public class ConnectorManager {
} }
private void addUserToVres(String vres, String changedUserName, // private void addUserToVres(String vres, String changedUserName,
CkanClient ckanClient, String ckanKey, String localhostName) { // CkanClient ckanClient, String ckanKey, String localhostName) {
if (vres!=null && !vres.isEmpty()) // if (vres!=null && !vres.isEmpty())
for (String vreAndRole: vres.split(",")){ // for (String vreAndRole: vres.split(",")){
String[] splitVRE = vreAndRole.split("\\|"); // String[] splitVRE = vreAndRole.split("\\|");
String vre = splitVRE[0]; // String vre = splitVRE[0];
String role = splitVRE[1]; // String role = splitVRE[1];
boolean added = addUserToOrganization(changedUserName, vre, role, ckanKey, localhostName); // boolean added = addUserToOrganization(changedUserName, vre, role, ckanKey, localhostName);
log.info("{} {} added to vre {}",changedUserName, added?"":"not", vre); // log.info("{} {} added to vre {}",changedUserName, added?"":"not", vre);
} // }
} // }
private Response createResponse(String userName, String path, String query){ private Response createResponse(String userName, String path, String query){
try{ try{
@ -182,38 +175,38 @@ public class ConnectorManager {
} }
private boolean addUserToOrganization(String ckanUsername, String organizationName, String role, String ckanKey, String hostAddress ){ // private boolean addUserToOrganization(String ckanUsername, String organizationName, String role, String ckanKey, String hostAddress ){
// we need to use the apis to make it // // we need to use the apis to make it
String path = "/api/3/action/organization_member_create"; // String path = "/api/3/action/organization_member_create";
//
// Request parameters to be replaced // // Request parameters to be replaced
String parameter = "{" // String parameter = "{"
+ "\"id\":\"ORGANIZATION_ID_NAME\"," // + "\"id\":\"ORGANIZATION_ID_NAME\","
+ "\"username\":\"USERNAME_ID_NAME\"," // + "\"username\":\"USERNAME_ID_NAME\","
+ "\"role\":\"ROLE\"" // + "\"role\":\"ROLE\""
+ "}"; // + "}";
//
// replace those values // // replace those values
parameter = parameter.replace("ORGANIZATION_ID_NAME", organizationName.toLowerCase()); // parameter = parameter.replace("ORGANIZATION_ID_NAME", organizationName.toLowerCase());
parameter = parameter.replace("USERNAME_ID_NAME", ckanUsername); // parameter = parameter.replace("USERNAME_ID_NAME", ckanUsername);
parameter = parameter.replace("ROLE", role); // parameter = parameter.replace("ROLE", role);
//
log.debug("API request for organization membership is going to be " + parameter); // log.debug("API request for organization membership is going to be " + parameter);
//
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { // try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpPost request = new HttpPost(hostAddress + path); // HttpPost request = new HttpPost(hostAddress + path);
request.addHeader("Authorization", ckanKey); // sys token // request.addHeader("Authorization", ckanKey); // sys token
StringEntity params = new StringEntity(parameter); // StringEntity params = new StringEntity(parameter);
request.setEntity(params); // request.setEntity(params);
HttpResponse response = httpClient.execute(request); // HttpResponse response = httpClient.execute(request);
log.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); // log.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
//
return (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK); // return (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK);
//
}catch (Exception ex) { // }catch (Exception ex) {
log.error("Error while trying to change the role for this user ", ex); // log.error("Error while trying to change the role for this user ", ex);
return false; // return false;
} // }
} // }
} }

View File

@ -1,28 +1,11 @@
package org.gcube.data.access.ckanconnector; package org.gcube.data.access.ckanconnector;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.scope.api.ScopeProvider;
import com.google.gson.Gson;
import eu.trentorise.opendata.jackan.CkanClient;
import eu.trentorise.opendata.jackan.exceptions.CkanNotFoundException;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
@Path("organization") @Path("organization")
@Slf4j @Slf4j
@ -30,55 +13,58 @@ public class OrganizationManager {
@Context ServletContext context; @Context ServletContext context;
@PUT // @PUT
@Path("/{name}") // @Path("/{name}")
public Response create(@PathParam("name") String organizationName) { // public Response create(@PathParam("name") String organizationName) {
log.info("create called"); // log.info("create called");
try{ // try{
if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build(); //
//// This is done by smartgears
String ckanKey = context.getInitParameter("ckanKey"); //// if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build();
int internalPort = Integer.parseInt(context.getInitParameter("internalPort")); //
CkanClient ckanClient = new CkanClient("http://127.0.0.1:"+internalPort, ckanKey); // String ckanKey = context.getInitParameter("ckanKey");
// int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));
CkanOrganization org = new CkanOrganization(organizationName.replaceAll(" ", "_").replace(".", "_").toLowerCase()); // CkanClient ckanClient = new CkanClient("http://127.0.0.1:"+internalPort, ckanKey);
org.setDisplayName(organizationName); //
org.setTitle(organizationName); // CkanOrganization org = new CkanOrganization(organizationName.replaceAll(" ", "_").replace(".", "_").toLowerCase());
ckanClient.createOrganization(org); // org.setDisplayName(organizationName);
// org.setTitle(organizationName);
log.info("create organizzation {} called from user {} in scope {}",organizationName, AuthorizationProvider.instance.get().getClient().getId(), ScopeProvider.instance.get()); // ckanClient.createOrganization(org);
return Response.ok().build(); //
}catch(Exception e){ // log.info("create organizzation {} called from user {} in scope {}",organizationName, AuthorizationProvider.instance.get().getClient().getId(), ScopeProvider.instance.get());
log.error("error trying to create organization "+organizationName,e); // return Response.ok().build();
return Response.serverError().entity(e.getMessage()).build(); // }catch(Exception e){
} // log.error("error trying to create organization "+organizationName,e);
} // return Response.serverError().entity(e.getMessage()).build();
// }
@GET // }
@Path("/{name}") //
@Produces(MediaType.APPLICATION_JSON) // @GET
public String get(@PathParam("name") String organizationName) { // @Path("/{name}")
log.info("get called with name {}", organizationName); // @Produces(MediaType.APPLICATION_JSON)
// public String get(@PathParam("name") String organizationName) {
if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) throw new WebApplicationException("user authentication needed",Response.Status.UNAUTHORIZED); // log.info("get called with name {}", organizationName);
//
String ckanKey = context.getInitParameter("ckanKey"); //// This is done by smartgears
int internalPort = Integer.parseInt(context.getInitParameter("internalPort")); //// if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) throw new WebApplicationException("user authentication needed",Response.Status.UNAUTHORIZED);
//
try{ // String ckanKey = context.getInitParameter("ckanKey");
CkanClient ckanClient = new CkanClient("http://127.0.0.1:"+internalPort, ckanKey); // int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));
CkanOrganization org = ckanClient.getOrganization(organizationName.replaceAll(" ", "_").replace(".", "_").toLowerCase()); //
log.trace("organization {} found",organizationName); // try{
return new Gson().toJson(org); // CkanClient ckanClient = new CkanClient("http://127.0.0.1:"+internalPort, ckanKey);
}catch(CkanNotFoundException e){ // CkanOrganization org = ckanClient.getOrganization(organizationName.replaceAll(" ", "_").replace(".", "_").toLowerCase());
log.error("organization {} doesn't exist",organizationName); // log.trace("organization {} found",organizationName);
throw new WebApplicationException("organization "+organizationName+" doesn't exist",Response.Status.NOT_FOUND); // return new Gson().toJson(org);
} catch (Exception e) { // }catch(CkanNotFoundException e){
log.error("error trying to contect ckan",e); // log.error("organization {} doesn't exist",organizationName);
throw new WebApplicationException("error trying to contect ckan",Response.Status.INTERNAL_SERVER_ERROR); // throw new WebApplicationException("organization "+organizationName+" doesn't exist",Response.Status.NOT_FOUND);
} // } catch (Exception e) {
// log.error("error trying to contect ckan",e);
// throw new WebApplicationException("error trying to contect ckan",Response.Status.INTERNAL_SERVER_ERROR);
} // }
//
//
// }
} }