fixed add and remove of a context using the conductor

This commit is contained in:
lucio 2024-03-06 16:47:18 +01:00
parent 8069bdae44
commit 5e9492da8e
8 changed files with 260 additions and 155 deletions

View File

@ -1,5 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
@ -13,22 +36,5 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,21 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="whn-manager">
<wb-resource deploy-path="/WEB-INF/web.xml" source-path="/src/main/webapp/WEB-INF/web.xml"/>
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<dependent-module archiveName="common-smartgears-app-3.0.0.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/common-smartgears-app/common-smartgears-app">
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
<dependent-module archiveName="common-smartgears-app-3.0.1-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/common-smartgears-app/common-smartgears-app">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="whn-manager"/>
<property name="java-output-path" value="/Whn-Manager/target/classes"/>
</wb-module>
</wb-module>
</project-modules>

View File

@ -3,5 +3,5 @@
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="11"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="jst.web" version="4.0"/>
</faceted-project>

View File

@ -1,5 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>org.gcube.resourcemanagement.whnmanager.WHNManager</servlet-name>

52
pom.xml
View File

@ -22,7 +22,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-smartgears-bom</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@ -39,30 +39,31 @@
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears-app</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-cdi2-se -->
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-cdi2-se</artifactId>
</dependency>
<!-- jersey -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- add jackson as json provider -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
@ -80,6 +81,27 @@
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gxHTTP</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>event-publisher-library</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
</dependencies>
<build>

View File

@ -1,135 +1,182 @@
package org.gcube.resourcemanagement.whnmanager;
import java.net.HttpURLConnection;
import java.util.Map.Entry;
import java.net.URL;
import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.common.gxhttp.util.ContentUtils;
import org.gcube.common.keycloak.KeycloakClient;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.ModelUtils;
import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.event.publisher.AbstractHTTPWithJWTTokenAuthEventSender;
import org.gcube.event.publisher.Event;
import org.gcube.event.publisher.EventStatus.Status;
import org.gcube.oidc.rest.JWTToken;
import org.gcube.oidc.rest.OpenIdConnectRESTHelperException;
import org.gcube.resourcemanagement.whnmanager.utils.ValidationUtils;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.managers.ContextEvents;
import org.gcube.smartgears.security.AuthorizationProvider;
import org.gcube.smartgears.security.SimpleCredentials;
import org.gcube.smartgears.security.defaults.DefaultAuthorizationProvider;
import org.gcube.smartgears.utils.InnerMethodName;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
@Path("/contexts")
public class ContextManager {
private static Logger logger=LoggerFactory.getLogger(ContextManager.class);
private static Logger logger = LoggerFactory.getLogger(ContextManager.class);
private static final String CONDUCTOR_URI = "https://conductor.cloud-dev.d4science.org/api/workflow/";
// private static final String REQUEST_PATH ="workflow";
private static final String ADD_CONTEXT_TASK = "ghn_client_add_to_context";
private static final String REMOVE_CONTEXT_TASK = "ghn_client_remove_from_context";
private enum TaskType {
ADD(ADD_CONTEXT_TASK), REMOVE(REMOVE_CONTEXT_TASK);
private String taskName;
TaskType(String contextName) {
this.taskName = contextName;
}
}
private static final int READ_TIMEOUT = 40000;
private static final int CONNECTION_TIMEOUT = 40000;
@PUT
@Path("")
public Response add(@FormParam("context") String context){
InnerMethodName.instance.set("addContext");
Secret secret = SecretManagerProvider.instance.get();
logger.trace("WHNManager: addToContext method invokation with parameters context :{} and caller: {} curentContext: {}",context, secret.getOwner().getId(), secret.getContext() );
ValidationUtils.valid("context", context);
ApplicationContext appContext = ContextProvider.get();
if(context!=null){
GXHTTPStringRequest request = GXHTTPStringRequest.newRequest("https://conductor.dev.d4science.org/api");
try {
request = request.path("workflow");
for(Entry<String, String> entry : secret.getHTTPAuthorizationHeaders().entrySet())
request = request.header(entry.getKey(), entry.getValue());
SimpleCredentials credentials = ((DefaultAuthorizationProvider) appContext.container().authorizationProvider()).getCredentials();
HttpURLConnection response = request.post(String.format(" \"name\": \"ghn_client_add_to_contexts\",\n" +
" \"input\" : {\n" +
" \"client_id\" : \"%s\",\n" +
" \"context_list\" : [\"%s\"] }", credentials.getClientID(), context)) ;
if(response.getResponseCode() == Status.CREATED.getStatusCode()) {
String body = ContentUtils.toString(ContentUtils.toByteArray(response.getInputStream()));
logger.info("Returned response for remove scope {} ",body);
}
appContext.container().events().fire(context, ContextEvents.ADD_CONTEXT_TO_CONTAINER);
} catch (Exception e) {
logger.error("error adding context {}", context, e);
return Response.serverError().build();
}
}else{
logger.error("context is null");
return Response.status(Status.BAD_REQUEST).build();
public Response add(final @FormParam("context") String context) {
InnerMethodName.set("addContext");
Secret secret = SecretManagerProvider.get();
logger.debug(
"WHNManager: addToContext method invokation with parameters context :{} and caller: {} curentContext: {}",
context, secret.getOwner().getId(), secret.getContext());
try {
executeTask(l -> l.contains(context), TaskType.ADD, context);
ApplicationContext appContext = ContextProvider.get();
appContext.container().events().fire(context, ContextEvents.ADD_CONTEXT_TO_CONTAINER);
return Response.ok().build();
} catch (InvalidParameterException ip) {
logger.warn("clientId already contains {}", context);
return Response.noContent().build();
} catch (IllegalArgumentException ia) {
logger.warn("null context passed",ia);
return Response.status(400, "context parameter is null").build();
} catch (Throwable t) {
logger.error("error adding context", t);
throw new WebApplicationException(t);
}
return Response.ok().build();
}
@DELETE
@Path("")
public Response remove(@FormParam("context") String context){
InnerMethodName.instance.set("removeContext");
Secret secret = SecretManagerProvider.instance.get();
logger.trace("WHNManager: removeFromContext method invokation with parameters context :{} and caller: {} curentContext: {}",context, secret.getOwner().getId(), secret.getContext());
ValidationUtils.valid("context", context);
ApplicationContext appContext = ContextProvider.get();
if(context!=null){
logger.trace("allowed container in context are {} ",appContext.container().authorizationProvider().getContexts());
GXHTTPStringRequest request = GXHTTPStringRequest.newRequest("https://conductor.dev.d4science.org/api");
try {
request = request.path("workflow");
for(Entry<String, String> entry : secret.getHTTPAuthorizationHeaders().entrySet())
request = request.header(entry.getKey(), entry.getValue());
SimpleCredentials credentials = ((DefaultAuthorizationProvider) appContext.container().authorizationProvider()).getCredentials();
HttpURLConnection response = request.post(String.format(" \"name\": \"ghn_client_remove_from_contexts\",\n" +
" \"input\" : {\n" +
" \"client_id\" : \"%s\",\n" +
" \"context_list\" : [\"%s\"] }", credentials.getClientID(), context)) ;
if(response.getResponseCode() == Status.CREATED.getStatusCode()) {
String body = ContentUtils.toString(ContentUtils.toByteArray(response.getInputStream()));
logger.info("Returned response for remove scope {} ",body);
}
appContext.container().events().fire(context, ContextEvents.REMOVE_CONTEXT_FROM_CONTAINER);
} catch (Exception e) {
logger.error("error removing context {}", context, e);
return Response.serverError().build();
}
}else{
logger.error("context is null");
return Response.status(Status.BAD_REQUEST).build();
public Response remove(final @FormParam("context") String context) {
InnerMethodName.set("removeContext");
Secret secret = SecretManagerProvider.get();
logger.debug(
"WHNManager: removeFromContext method invokation with parameters context :{} and caller: {} curentContext: {}",
context, secret.getOwner().getId(), secret.getContext());
try {
executeTask(l -> !l.contains(context), TaskType.REMOVE, context);
ApplicationContext appContext = ContextProvider.get();
appContext.container().events().fire(context, ContextEvents.REMOVE_CONTEXT_FROM_CONTAINER);
return Response.ok().build();
} catch (Throwable t) {
logger.error("error removing context", t);
throw new WebApplicationException(t);
}
return Response.ok().build();
}
private Status executeTask(Predicate<Set<String>> notModifiablePredicate, TaskType type, String inputContext ) throws Throwable {
ValidationUtils.valid("context", inputContext);
ApplicationContext appContext = ContextProvider.get();
AuthorizationProvider authProvider = appContext.container().authorizationProvider();
SimpleCredentials credentials = (SimpleCredentials) authProvider.getCredentials();
logger.debug("contexts already present in the container are {} executing task {}",authProvider.getContexts(), type.taskName);
if (notModifiablePredicate.test(authProvider.getContexts()))
throw new InvalidParameterException();
Map<String, String> inputs = new HashMap<String, String>();
inputs.put("client_id", credentials.getClientID());
inputs.put("context", inputContext);
Event event = new Event(type.taskName, type.taskName, appContext.name(), inputs);
Status result = checkEventResult(credentials, event);
logger.debug("{} exectured with result {}", type.taskName, result);
if (result != Status.COMPLETED) {
throw new Exception("error executing the workflow retuned with status " + result);
}
return result;
}
private Status checkEventResult(SimpleCredentials credentials, Event event) throws Throwable {
Secret secret = SecretManagerProvider.get();
AbstractHTTPWithJWTTokenAuthEventSender eventRequest = getAuthEventSender(credentials, secret);
String result = eventRequest.sendAndGetResult(event);
JSONObject eventResult;
do {
Thread.sleep(5000);
eventResult = eventRequest.retrive(result);
} while (Status.RUNNING == Status.valueOf((String) eventResult.get("status")));
return Status.valueOf((String) eventResult.get("status"));
}
@GET
@Path("")
@Produces(MediaType.APPLICATION_JSON)
public String[] get(){
InnerMethodName.instance.set("addContext");
public String[] get() {
InnerMethodName.set("getContext");
ApplicationContext appContext = ContextProvider.get();
Set<String> contexts = appContext.authorizationProvider().getContexts();
return contexts.stream().toArray(String[]::new);
}
private AbstractHTTPWithJWTTokenAuthEventSender getAuthEventSender(SimpleCredentials credentials, Secret secret)
throws Throwable {
KeycloakClient client = KeycloakClientFactory.newInstance();
TokenResponse tokenResponse = client.queryOIDCToken(secret.getContext(), credentials.getClientID(),
credentials.getSecret());
AbstractHTTPWithJWTTokenAuthEventSender eventSender = new AbstractHTTPWithJWTTokenAuthEventSender(
new URL(CONDUCTOR_URI), credentials.getClientID(), credentials.getSecret(), null) {
protected JWTToken getAuthorizationToken() throws OpenIdConnectRESTHelperException {
return JWTToken.fromString(ModelUtils.toJSONString(tokenResponse));
}
};
eventSender.setConnectionTimeout(CONNECTION_TIMEOUT);
eventSender.setReadTimeout(READ_TIMEOUT);
return eventSender;
}
}

View File

@ -3,8 +3,8 @@ package org.gcube.resourcemanagement.whnmanager;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Application;
@Path("/")
public class WHNManager extends Application{
@ -17,6 +17,4 @@ public class WHNManager extends Application{
return classes;
}
}

View File

@ -6,17 +6,16 @@ import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import jakarta.servlet.ServletContext;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import org.gcube.common.events.Hub;
import org.gcube.common.security.credentials.Credentials;
import org.gcube.common.security.secrets.Secret;
import org.gcube.resourcemanagement.whnmanager.ContextManager;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.configuration.application.ApplicationConfiguration;
import org.gcube.smartgears.context.Properties;