This commit is contained in:
Luca Frosini 2022-05-11 15:24:43 +02:00
parent ba55624d4f
commit d93000d280
4 changed files with 85 additions and 72 deletions

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-SNAPSHOT]
- 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]

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-SNAPSHOT</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>

View File

@ -78,41 +78,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 +125,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 +185,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

@ -35,7 +35,9 @@ public class OrganizationManager {
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
// if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build();
String ckanKey = context.getInitParameter("ckanKey"); String ckanKey = context.getInitParameter("ckanKey");
int internalPort = Integer.parseInt(context.getInitParameter("internalPort")); int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));
@ -60,7 +62,8 @@ public class OrganizationManager {
public String get(@PathParam("name") String organizationName) { public String get(@PathParam("name") String organizationName) {
log.info("get called with name {}", 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); // 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"); String ckanKey = context.getInitParameter("ckanKey");
int internalPort = Integer.parseInt(context.getInitParameter("internalPort")); int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));