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>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<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-1.7">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

1
.gitignore vendored Normal file
View File

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

View File

@ -1,12 +1,15 @@
eclipse.preferences.version=1
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.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.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
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.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>
<group>VREManagement</group>
<version>${version}</version>
<description>Web Hosting Node Service</description>
<local-persistence location='target' />
</application>

18
pom.xml
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">
<modelVersion>4.0.0</modelVersion>
<parent>
@ -41,11 +42,16 @@
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears-app</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears-app</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gxHTTP</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>

View File

@ -1,7 +1,13 @@
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.secrets.Secret;
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.context.application.ApplicationContext;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -23,7 +31,7 @@ public class WhnManagerImpl implements WhnManager{
private static Logger logger=LoggerFactory.getLogger(WhnManagerImpl.class);
/**
* Add a scope to the ghn profile and publish it on IS
*/
@ -34,7 +42,34 @@ public class WhnManagerImpl implements WhnManager{
ValidationUtils.valid("context", context);
ApplicationContext appContext = ContextProvider.get();
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);
}else{
logger.error("context is null");
@ -54,7 +89,34 @@ public class WhnManagerImpl implements WhnManager{
ApplicationContext appContext = ContextProvider.get();
if(context!=null){
logger.trace("allowed container in context are {} ",appContext.container().configuration().authorizationProvider().getContexts());
appContext.container().events().fire(context, ContextEvents.REMOVE_CONTEXT_FROM_CONTAINER);
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);
} catch (Exception e) {
logger.error("error removing context {}", context, e);
return false;
}
}else{
logger.error("context is null");
return false;

View File

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