Porting service to new authorization-utils

This commit is contained in:
Luca Frosini 2022-03-08 11:21:19 +01:00
parent 7ca5c8ea33
commit 9f26bdb42c
13 changed files with 102 additions and 161 deletions

View File

@ -11,7 +11,7 @@
<groupId>org.gcube.vremanagement</groupId>
<artifactId>smart-executor</artifactId>
<version>3.0.0</version>
<version>3.0.1-SNAPSHOT</version>
<name>SmartExecutor</name>
<description>Smart Executor Service allows to launch recurrent tasks such as task for infrastructure management, healthy monitoring etc.</description>
<packaging>war</packaging>
@ -33,7 +33,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-smartgears-bom</artifactId>
<version>2.1.0</version>
<version>2.2.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>

View File

@ -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<String> getRoles() {
return new ArrayList<>();
}
@Override
public String getId() {
return "UNKNOWN";
}
};
}
return authorizationEntry.getClientInfo();
return SecretManagerProvider.instance.get().getContext();
}
}

View File

@ -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();
}
}
};

View File

@ -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;
}

View File

@ -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<T extends Plugin> 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<T extends Plugin> 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<T extends Plugin> 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<T extends Plugin> implements Runnable {
} catch (InvalidValueException e) {
logger.error("Unable to account {}", jobUsageRecord, e);
}
secretManager.endSession();
}
}

View File

@ -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;
}

View File

@ -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

View File

@ -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")

View File

@ -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();
}
}

View File

@ -63,7 +63,7 @@ public class SerializationTest extends ContextTest {
}
@Test
public void testScheduledTask() throws JsonGenerationException, JsonMappingException, IOException {
public void testScheduledTask() throws Exception {
Map<String,Object> inputs = new HashMap<String,Object>();
inputs.put("Hello", "World");
long sleepTime = 10000;

View File

@ -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);

View File

@ -48,7 +48,8 @@ public class SmartExecutorPersistenceConnectorTest extends ContextTest {
for(int i=0; i<states.length; i++){
long timestamp = new Date().getTime();
PluginStateEvolution pluginStateEvolution = new PluginStateEvolution(uuid, 1, timestamp, HelloWorldPlugin.class.newInstance(), states[i], 0);
HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin();
PluginStateEvolution pluginStateEvolution = new PluginStateEvolution(uuid, 1, timestamp, helloWorldPlugin, states[i], 0);
persistenceConnector.pluginStateEvolution(pluginStateEvolution, null);
long startTime = Calendar.getInstance().getTimeInMillis();

View File

@ -7,7 +7,9 @@ import java.util.List;
import java.util.Map;
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.ContextTest;
import org.gcube.vremanagement.executor.exception.InputsNullException;
import org.gcube.vremanagement.executor.exception.InvalidInputsException;
@ -29,6 +31,13 @@ public class RunnablePluginTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(SmartExecutorSchedulerTest.class);
private String getToken() {
SecretManager secretManager = SecretManagerProvider.instance.get();
Secret secret = secretManager.getCurrentSecretHolder().getSecrets().first();
String token = secret.getToken();
return token;
}
@Test
public void launchNullInputsTest() throws Exception {
logger.debug("Testing Null inputs");
@ -39,7 +48,7 @@ public class RunnablePluginTest extends ContextTest {
pluginStateNotifications.add(persistenceConnector);
HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin();
try {
RunnablePlugin<HelloWorldPlugin> runnablePlugin = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, null, uuid, 1, pluginStateNotifications, SecurityTokenProvider.instance.get());
RunnablePlugin<HelloWorldPlugin> runnablePlugin = new RunnablePlugin<HelloWorldPlugin>(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<HelloWorldPlugin> pt = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,SecurityTokenProvider.instance.get());
RunnablePlugin<HelloWorldPlugin> pt = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,getToken());
try {
pt.run();
} catch(RuntimeException e) {
@ -78,7 +87,7 @@ public class RunnablePluginTest extends ContextTest {
List<PluginStateNotification> pluginStateNotifications = new ArrayList<PluginStateNotification>();
pluginStateNotifications.add(persistenceConnector);
HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin();
RunnablePlugin<HelloWorldPlugin> rp = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,SecurityTokenProvider.instance.get());
RunnablePlugin<HelloWorldPlugin> rp = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,getToken());
long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime;
while(endTime <= (startTime + 1000)){