diff --git a/pom.xml b/pom.xml index f499dfc..6c83ed7 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.gcube.vremanagement smart-executor - 3.0.0 + 3.0.1-SNAPSHOT SmartExecutor Smart Executor Service allows to launch recurrent tasks such as task for infrastructure management, healthy monitoring etc. war @@ -33,7 +33,7 @@ org.gcube.distribution gcube-smartgears-bom - 2.1.0 + 2.2.0-SNAPSHOT pom import diff --git a/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java b/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java index f09b547..6b5d0dc 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java +++ b/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java @@ -1,82 +1,14 @@ package org.gcube.vremanagement.executor; -import java.util.ArrayList; -import java.util.List; - -import org.gcube.common.authorization.client.Constants; -import org.gcube.common.authorization.client.exceptions.ObjectNotFound; -import org.gcube.common.authorization.library.AuthorizationEntry; -import org.gcube.common.authorization.library.ClientType; -import org.gcube.common.authorization.library.provider.ClientInfo; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.gcube.common.scope.api.ScopeProvider; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; /** * @author Luca Frosini (ISTI - CNR) */ public class ContextUtility { - private static Logger logger = LoggerFactory.getLogger(ContextUtility.class); - public static String getCurrentContext() { - String token = SecurityTokenProvider.instance.get(); - AuthorizationEntry authorizationEntry; - try { - authorizationEntry = Constants.authorizationService().get(token); - } catch(Exception e) { - logger.trace("Context was not retrieved from token. Going to get it from {}", - ScopeProvider.class.getSimpleName()); - return ScopeProvider.instance.get(); - } - String context = authorizationEntry.getContext(); - logger.trace("Context retrieved from token is {}. Context in {} is {}", context, - ScopeProvider.class.getSimpleName(), ScopeProvider.instance.get()); - return context; - } - - public static String getCurrentContext(String token) throws ObjectNotFound, Exception { - AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); - String context = authorizationEntry.getContext(); - logger.info("Context of token {} is {}", token, context); - return context; - } - - public static void setContext(String token) throws ObjectNotFound, Exception { - SecurityTokenProvider.instance.set(token); - ScopeProvider.instance.set(getCurrentContext(token)); - } - - public static ClientInfo getClientInfo() { - String token = SecurityTokenProvider.instance.get(); - AuthorizationEntry authorizationEntry; - try { - authorizationEntry = Constants.authorizationService().get(token); - } catch(Exception e) { - return new ClientInfo() { - - /** - * Generated Serial Version UID - */ - private static final long serialVersionUID = 8311873203596762883L; - - @Override - public ClientType getType() { - return ClientType.USER; - } - - @Override - public List getRoles() { - return new ArrayList<>(); - } - - @Override - public String getId() { - return "UNKNOWN"; - } - }; - } - return authorizationEntry.getClientInfo(); + return SecretManagerProvider.instance.get().getContext(); } + } diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index 396d0a0..2816137 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -4,6 +4,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.authorization.utils.secret.Secret; +import org.gcube.common.authorization.utils.secret.SecretUtility; import org.gcube.smartgears.ApplicationManager; import org.gcube.smartgears.ContextProvider; import org.gcube.smartgears.context.application.ApplicationContext; @@ -132,9 +136,11 @@ public class SmartExecutorInitializator implements ApplicationManager { } String scheduledTasktoken = scheduledTask.getToken(); + Secret secret = SecretUtility.getSecretByTokenString(scheduledTasktoken); + + SecretManager secretManager = SecretManagerProvider.instance.get(); try { - ContextUtility.setContext(scheduledTasktoken); - + secretManager.startSession(secret); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler(); // A new Scheduled Task will be persisted due to launch. Removing it smartExecutorPersistenceConnector.removeScheduledTask(scheduledTask); @@ -146,8 +152,11 @@ public class SmartExecutorInitializator implements ApplicationManager { } catch (Exception ex) { logger.error("({}) Unable to add back scheduled task {}", context, taskAsString); } + }finally { + secretManager.endSession(); } + } }; diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index 8b47fc6..73a3c75 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import org.gcube.informationsystem.base.reference.Direction; import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.reference.entities.Facet; @@ -14,7 +15,6 @@ import org.gcube.informationsystem.model.reference.properties.PropagationConstra import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; -import org.gcube.informationsystem.resourceregistry.client.Direction; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher; @@ -130,7 +130,7 @@ public class RestISPublisher extends ISPublisher { } catch (AvailableInAnotherContextException e) { runningPlugin = new RunningPluginImpl(); runningPlugin.setHeader(new HeaderImpl(pluginUUID)); - resourceRegistryPublisher.addToCurrentContext(runningPlugin); + resourceRegistryPublisher.addToCurrentContext(runningPlugin, false); } catch (ResourceRegistryException e) { throw e; } diff --git a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePlugin.java b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePlugin.java index c34b142..8417e61 100644 --- a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePlugin.java +++ b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePlugin.java @@ -14,11 +14,12 @@ import org.gcube.accounting.persistence.AccountingPersistence; import org.gcube.accounting.persistence.AccountingPersistenceFactory; import org.gcube.common.authorization.client.Constants; import org.gcube.common.authorization.library.AuthorizationEntry; -import org.gcube.common.authorization.library.provider.ClientInfo; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.authorization.utils.secret.Secret; +import org.gcube.common.authorization.utils.secret.SecretUtility; import org.gcube.documentstore.exception.InvalidValueException; import org.gcube.smartgears.ContextProvider; -import org.gcube.vremanagement.executor.ContextUtility; import org.gcube.vremanagement.executor.exception.AlreadyInFinalStateException; import org.gcube.vremanagement.executor.exception.InvalidPluginStateEvolutionException; import org.gcube.vremanagement.executor.plugin.Plugin; @@ -63,11 +64,6 @@ public class RunnablePlugin implements Runnable { this.iterationNumber = iterationNumber; this.pluginStateNotifications = pluginStateNotifications; this.token = token; - try { - SecurityTokenProvider.instance.set(token); - }catch (Exception e) { - throw new RuntimeException(e); - } try { setState(PluginState.CREATED); } catch (AlreadyInFinalStateException | InvalidPluginStateEvolutionException e) { @@ -89,9 +85,11 @@ public class RunnablePlugin implements Runnable { JobUsageRecord jobUsageRecord = new JobUsageRecord(); long startTime = actualStateEvolution.getTimestamp(); + SecretManager secretManager = SecretManagerProvider.instance.get(); try { - ContextUtility.setContext(token); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); setState(PluginState.RUNNING); @@ -101,10 +99,7 @@ public class RunnablePlugin implements Runnable { Ref hnRef = runOn.getHostingNode(); jobUsageRecord.setHost(hnRef.getAddress()); - - ClientInfo clientInfo = ContextUtility.getClientInfo(); - String consumerId = clientInfo.getId(); - jobUsageRecord.setConsumerId(consumerId); + jobUsageRecord.setConsumerId(secretManager.getUser().getUsername()); try { AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); @@ -156,7 +151,7 @@ public class RunnablePlugin implements Runnable { } catch (InvalidValueException e) { logger.error("Unable to account {}", jobUsageRecord, e); } - + secretManager.endSession(); } } diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java index ca28ea9..4a52519 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java @@ -7,14 +7,15 @@ import java.util.UUID; import org.gcube.com.fasterxml.jackson.annotation.JsonTypeInfo; import org.gcube.common.authorization.library.provider.ClientInfo; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.authorization.utils.secret.Secret; import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.GCoreEndpoint.Profile.Endpoint; import org.gcube.common.resources.gcore.HostingNode; import org.gcube.common.resources.gcore.utils.Group; import org.gcube.smartgears.Constants; import org.gcube.smartgears.ContextProvider; -import org.gcube.vremanagement.executor.ContextUtility; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.json.ExtendedSEMapper; import org.gcube.vremanagement.executor.json.SEMapper; @@ -34,16 +35,18 @@ public class ScheduledTask extends org.gcube.vremanagement.executor.plugin.Sched protected ScheduledTask() { } - public ScheduledTask(UUID uuid, LaunchParameter launchParameter) { + public ScheduledTask(UUID uuid, LaunchParameter launchParameter) throws Exception { this(uuid, launchParameter, generateRunOn()); } - public ScheduledTask(UUID uuid, LaunchParameter launchParameter, RunOn runOn) { + public ScheduledTask(UUID uuid, LaunchParameter launchParameter, RunOn runOn) throws Exception { this.uuid = uuid; this.launchParameter = launchParameter; - this.token = SecurityTokenProvider.instance.get(); - this.context = ContextUtility.getCurrentContext(); - this.clientInfo = ContextUtility.getClientInfo(); + SecretManager secretManager = SecretManagerProvider.instance.get(); + Secret secret = secretManager.getCurrentSecretHolder().getSecrets().first(); + this.token = secret.getToken(); + this.context = secretManager.getContext(); + this.clientInfo = secret.getClientInfo(); this.runOn = runOn; } diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduler/JobCompletedNotification.java b/src/main/java/org/gcube/vremanagement/executor/scheduler/JobCompletedNotification.java index ec9f3f0..7d8f1f3 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduler/JobCompletedNotification.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduler/JobCompletedNotification.java @@ -14,8 +14,11 @@ import org.gcube.vremanagement.executor.plugin.PluginStateNotification; * This class is useless is just used to simulate and indicate the code * insertion point the possibility to add multiple notification of an event * in the running plugin evolution. - * Future use of this possibility are possibility to send an email to - * the job owner, notify a registered process. Send a tweet and so on. + * Future use of this are: + * - send an email to the job owner; + * - notify a registered process; + * - send a tweet; + * - etc * @author Luca Frosini (ISTI - CNR) */ @Deprecated diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java index 9a5e213..b002ff8 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java @@ -11,7 +11,9 @@ import java.util.Map; import java.util.Set; import java.util.UUID; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.authorization.utils.secret.Secret; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.api.types.Scheduling; import org.gcube.vremanagement.executor.exception.ExecutorException; @@ -106,7 +108,9 @@ public class SmartExecutorScheduler { jobDataMap.put(SmartExecutorTask.UUID, uuid); jobDataMap.put(SmartExecutorTask.LAUNCH_PARAMETER, parameter); - String token = SecurityTokenProvider.instance.get(); + SecretManager secretManager = SecretManagerProvider.instance.get(); + Secret secret = secretManager.getCurrentSecretHolder().getSecrets().first(); + String token = secret.getToken(); jobDataMap.put(SmartExecutorTask.TOKEN, token); @SuppressWarnings("rawtypes") diff --git a/src/test/java/org/gcube/vremanagement/executor/ContextTest.java b/src/test/java/org/gcube/vremanagement/executor/ContextTest.java index 8d2198e..d50d329 100644 --- a/src/test/java/org/gcube/vremanagement/executor/ContextTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/ContextTest.java @@ -7,33 +7,24 @@ import java.io.IOException; import java.io.InputStream; import java.util.Properties; -import org.gcube.common.authorization.client.Constants; -import org.gcube.common.authorization.client.exceptions.ObjectNotFound; -import org.gcube.common.authorization.library.AuthorizationEntry; -import org.gcube.common.authorization.library.provider.AuthorizationProvider; -import org.gcube.common.authorization.library.provider.ClientInfo; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.gcube.common.authorization.library.utils.Caller; -import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.authorization.utils.secret.Secret; +import org.gcube.common.authorization.utils.secret.SecretUtility; import org.junit.AfterClass; import org.junit.BeforeClass; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) - * */ public class ContextTest { - private static final Logger logger = LoggerFactory.getLogger(ContextTest.class); - protected static Properties properties; protected static final String PROPERTIES_FILENAME = "token.properties"; - public static final String ROOT = "/d4science.research-infrastructures.eu"; - - public static final String DEFAULT_TEST_SCOPE_NAME; + public static final String ROOT; + public static final String VO; + public static final String VRE; static { properties = new Properties(); @@ -46,57 +37,50 @@ public class ContextTest { throw new RuntimeException(e); } - //DEFAULT_TEST_SCOPE_NAME = "/pred4s/preprod/preVRE"; - DEFAULT_TEST_SCOPE_NAME = "/gcube/devNext/NextNext"; + ROOT = "/gcube"; +// VO = ROOT + "/devsec"; +// VRE = VO + "/devVRE"; + VO = ROOT + "/devNext"; + VRE = VO + "/NextNext"; + } - public static String getCurrentContext(){ - String token = SecurityTokenProvider.instance.get(); - AuthorizationEntry authorizationEntry; - try { - authorizationEntry = Constants.authorizationService().get(token); - } catch (Exception e) { - logger.trace("Context was not retrieved from token. Going to get it from {}", ScopeProvider.class.getSimpleName()); - return ScopeProvider.instance.get(); - } - String context = authorizationEntry.getContext(); - logger.trace("Context retrieved from token is {}. Context in {} is {}", - context, ScopeProvider.class.getSimpleName(), ScopeProvider.instance.get()); - return context; + public static void set(Secret secret) throws Exception { + SecretManagerProvider.instance.reset(); + SecretManager secretManager = new SecretManager(); + SecretManagerProvider.instance.set(secretManager); + secretManager.addSecret(secret); + secretManager.set(); } - public static String getContext(String token) throws ObjectNotFound, Exception { - AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); - String context = authorizationEntry.getContext(); - logger.info("Context of token {} is {}", token, context); - return context; + public static void setContext(String token) throws Exception { + Secret secret = getSecret(token); + set(secret); + } + + public static void setContextByName(String fullContextName) throws Exception { + Secret secret = getSecretByContextName(fullContextName); + set(secret); } - public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception { + private static Secret getSecret(String token) throws Exception { + Secret secret = SecretUtility.getSecretByTokenString(token); + return secret; + } + + private static Secret getSecretByContextName(String fullContextName) throws Exception { String token = ContextTest.properties.getProperty(fullContextName); - setContext(token); - } - - public static void setContext(String token) throws ObjectNotFound, Exception { - SecurityTokenProvider.instance.set(token); - AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); - ClientInfo clientInfo = authorizationEntry.getClientInfo(); - logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name()); - String qualifier = authorizationEntry.getQualifier(); - Caller caller = new Caller(clientInfo, qualifier); - AuthorizationProvider.instance.set(caller); - ScopeProvider.instance.set(getContext(token)); + return getSecret(token); } @BeforeClass public static void beforeClass() throws Exception { - setContextByName(DEFAULT_TEST_SCOPE_NAME); + setContextByName(ROOT); } @AfterClass public static void afterClass() throws Exception { - SecurityTokenProvider.instance.reset(); - ScopeProvider.instance.reset(); + SecretManagerProvider.instance.reset(); } } diff --git a/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java b/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java index 3d7e9d2..208501d 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java @@ -63,7 +63,7 @@ public class SerializationTest extends ContextTest { } @Test - public void testScheduledTask() throws JsonGenerationException, JsonMappingException, IOException { + public void testScheduledTask() throws Exception { Map inputs = new HashMap(); inputs.put("Hello", "World"); long sleepTime = 10000; diff --git a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java index 89cd8c4..312c2e6 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.common.resources.gcore.Resource; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.informationsystem.publisher.RegistryPublisher; @@ -52,7 +53,7 @@ public class SmartExecutorInizializatorTest { RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); String id = resource.id(); - logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, ContextTest.getCurrentContext()); + logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, SecretManagerProvider.instance.get().getContext()); registryPublisher.remove(resource); diff --git a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java index a84db16..c963fac 100644 --- a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java @@ -48,7 +48,8 @@ public class SmartExecutorPersistenceConnectorTest extends ContextTest { for(int i=0; i runnablePlugin = new RunnablePlugin(helloWorldPlugin, null, uuid, 1, pluginStateNotifications, SecurityTokenProvider.instance.get()); + RunnablePlugin runnablePlugin = new RunnablePlugin(helloWorldPlugin, null, uuid, 1, pluginStateNotifications, getToken()); runnablePlugin.run(); } catch(Exception e){ Assert.assertEquals(InputsNullException.class, e.getCause().getClass()); @@ -56,7 +65,7 @@ public class RunnablePluginTest extends ContextTest { pluginStateNotifications.add(persistenceConnector); HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(); - RunnablePlugin pt = new RunnablePlugin(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,SecurityTokenProvider.instance.get()); + RunnablePlugin pt = new RunnablePlugin(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,getToken()); try { pt.run(); } catch(RuntimeException e) { @@ -78,7 +87,7 @@ public class RunnablePluginTest extends ContextTest { List pluginStateNotifications = new ArrayList(); pluginStateNotifications.add(persistenceConnector); HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(); - RunnablePlugin rp = new RunnablePlugin(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,SecurityTokenProvider.instance.get()); + RunnablePlugin rp = new RunnablePlugin(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,getToken()); long startTime = Calendar.getInstance().getTimeInMillis(); long endTime = startTime; while(endTime <= (startTime + 1000)){