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.
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]
- updated gcat-client version [#21530]

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

View File

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

@ -35,7 +35,9 @@ public class OrganizationManager {
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();
// 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"));
@ -60,7 +62,8 @@ public class OrganizationManager {
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);
// 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"));