added call to Keycloak to add and remove contexts

This commit is contained in:
Lucio Lelii 2022-06-24 15:55:42 +02:00
parent 570b010506
commit b9ca6eab87
7 changed files with 90 additions and 18 deletions

View File

@ -15,14 +15,16 @@
<attributes> <attributes>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target/

View File

@ -1,12 +1,15 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7 org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -1,8 +1,7 @@
<application mode='online'> <application proxable='false'>
<name>WhnManager</name> <name>WhnManager</name>
<group>VREManagement</group> <group>VREManagement</group>
<version>${version}</version> <version>${version}</version>
<description>Web Hosting Node Service</description> <description>Web Hosting Node Service</description>
<local-persistence location='target' />
</application> </application>

View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
@ -46,6 +47,11 @@
<artifactId>common-smartgears-app</artifactId> <artifactId>common-smartgears-app</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gxHTTP</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.sun.xml.ws</groupId> <groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId> <artifactId>jaxws-rt</artifactId>

View File

@ -1,7 +1,13 @@
package org.gcube.vremanagement.whnmanager.jaxws.ws; package org.gcube.vremanagement.whnmanager.jaxws.ws;
import javax.jws.WebService; import java.net.HttpURLConnection;
import java.util.Map.Entry;
import javax.jws.WebService;
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.security.providers.SecretManagerProvider; import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret; import org.gcube.common.security.secrets.Secret;
import org.gcube.resourcemanagement.whnmanager.api.WhnManager; import org.gcube.resourcemanagement.whnmanager.api.WhnManager;
@ -9,6 +15,8 @@ import org.gcube.resourcemanagement.whnmanager.api.exception.GCUBEUnrecoverableE
import org.gcube.smartgears.ContextProvider; import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.managers.ContextEvents; import org.gcube.smartgears.managers.ContextEvents;
import org.gcube.smartgears.security.SimpleCredentials;
import org.gcube.smartgears.security.defaults.DefaultAuthorizationProvider;
import org.gcube.vremanagement.whnmanager.utils.ValidationUtils; import org.gcube.vremanagement.whnmanager.utils.ValidationUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -34,7 +42,34 @@ public class WhnManagerImpl implements WhnManager{
ValidationUtils.valid("context", context); ValidationUtils.valid("context", context);
ApplicationContext appContext = ContextProvider.get(); ApplicationContext appContext = ContextProvider.get();
if(context!=null){ if(context!=null){
//TODO must add client id to new context
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().configuration().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.REMOVE_CONTEXT_FROM_CONTAINER);
} catch (Exception e) {
logger.error("error removing context {}", context, e);
return false;
}
appContext.container().events().fire(context, ContextEvents.ADD_CONTEXT_TO_CONTAINER); appContext.container().events().fire(context, ContextEvents.ADD_CONTEXT_TO_CONTAINER);
}else{ }else{
logger.error("context is null"); logger.error("context is null");
@ -54,7 +89,34 @@ public class WhnManagerImpl implements WhnManager{
ApplicationContext appContext = ContextProvider.get(); ApplicationContext appContext = ContextProvider.get();
if(context!=null){ if(context!=null){
logger.trace("allowed container in context are {} ",appContext.container().configuration().authorizationProvider().getContexts()); logger.trace("allowed container in context are {} ",appContext.container().configuration().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().configuration().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); appContext.container().events().fire(context, ContextEvents.REMOVE_CONTEXT_FROM_CONTAINER);
} catch (Exception e) {
logger.error("error removing context {}", context, e);
return false;
}
}else{ }else{
logger.error("context is null"); logger.error("context is null");
return false; return false;

View File

@ -1,8 +1,7 @@
<application mode='online'> <application proxable='false'>
<name>WhnManager</name> <name>WhnManager</name>
<group>VREManagement</group> <group>VREManagement</group>
<version>3.0.0-SNAPSHOT</version> <version>3.0.0-SNAPSHOT</version>
<description>Web Hosting Node Service</description> <description>Web Hosting Node Service</description>
<local-persistence location='target' />
</application> </application>