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
/.DS_Store

View File

@ -3,6 +3,13 @@
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).
## [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]
- updated gcat-client version [#21530]

14
pom.xml
View File

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

View File

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

View File

@ -1,28 +1,11 @@
package org.gcube.data.access.ckanconnector;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
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.MediaType;
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.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")
@Slf4j
@ -30,55 +13,58 @@ public class OrganizationManager {
@Context ServletContext context;
@PUT
@Path("/{name}")
public Response create(@PathParam("name") String organizationName) {
log.info("create called");
try{
if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build();
String ckanKey = context.getInitParameter("ckanKey");
int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));
CkanClient ckanClient = new CkanClient("http://127.0.0.1:"+internalPort, ckanKey);
CkanOrganization org = new CkanOrganization(organizationName.replaceAll(" ", "_").replace(".", "_").toLowerCase());
org.setDisplayName(organizationName);
org.setTitle(organizationName);
ckanClient.createOrganization(org);
log.info("create organizzation {} called from user {} in scope {}",organizationName, AuthorizationProvider.instance.get().getClient().getId(), ScopeProvider.instance.get());
return Response.ok().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)
public String get(@PathParam("name") String organizationName) {
log.info("get called with name {}", organizationName);
if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) throw new WebApplicationException("user authentication needed",Response.Status.UNAUTHORIZED);
String ckanKey = context.getInitParameter("ckanKey");
int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));
try{
CkanClient ckanClient = new CkanClient("http://127.0.0.1:"+internalPort, ckanKey);
CkanOrganization org = ckanClient.getOrganization(organizationName.replaceAll(" ", "_").replace(".", "_").toLowerCase());
log.trace("organization {} found",organizationName);
return new Gson().toJson(org);
}catch(CkanNotFoundException e){
log.error("organization {} doesn't exist",organizationName);
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);
}
}
// @PUT
// @Path("/{name}")
// public Response create(@PathParam("name") String organizationName) {
// log.info("create called");
// try{
//
//// This is done by smartgears
//// if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build();
//
// String ckanKey = context.getInitParameter("ckanKey");
// int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));
// CkanClient ckanClient = new CkanClient("http://127.0.0.1:"+internalPort, ckanKey);
//
// CkanOrganization org = new CkanOrganization(organizationName.replaceAll(" ", "_").replace(".", "_").toLowerCase());
// org.setDisplayName(organizationName);
// org.setTitle(organizationName);
// ckanClient.createOrganization(org);
//
// log.info("create organizzation {} called from user {} in scope {}",organizationName, AuthorizationProvider.instance.get().getClient().getId(), ScopeProvider.instance.get());
// return Response.ok().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)
// public String get(@PathParam("name") String organizationName) {
// log.info("get called with name {}", organizationName);
//
//// This is done by smartgears
//// if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) throw new WebApplicationException("user authentication needed",Response.Status.UNAUTHORIZED);
//
// String ckanKey = context.getInitParameter("ckanKey");
// int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));
//
// try{
// CkanClient ckanClient = new CkanClient("http://127.0.0.1:"+internalPort, ckanKey);
// CkanOrganization org = ckanClient.getOrganization(organizationName.replaceAll(" ", "_").replace(".", "_").toLowerCase());
// log.trace("organization {} found",organizationName);
// return new Gson().toJson(org);
// }catch(CkanNotFoundException e){
// log.error("organization {} doesn't exist",organizationName);
// 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);
// }
//
//
// }
}