use of org.ow2.proactive.scheduler.task.utils.Decrypter in project scheduling by ow2-proactive.
the class RMProxyActiveObject method handleCleaningScript.
/**
* Execute the given script on the given node.
* Also register a callback on {@link #cleanCallBack(Future, NodeSet)} method when script has returned.
* @param nodes the nodeset on which to start the script
* @param cleaningScript the script to be executed
* @param variables
* @param genericInformation
* @param taskId
* @param creds credentials with CredData containing third party credentials
*/
private void handleCleaningScript(NodeSet nodes, Script<?> cleaningScript, VariablesMap variables, Map<String, String> genericInformation, TaskId taskId, Credentials creds) {
TaskLogger instance = TaskLogger.getInstance();
try {
this.nodesTaskId.put(nodes, taskId);
// create a decrypter to access scheduler and retrieve Third Party User Credentials
String privateKeyPath = PASchedulerProperties.getAbsolutePath(PASchedulerProperties.SCHEDULER_AUTH_PRIVKEY_PATH.getValueAsString());
Decrypter decrypter = new Decrypter(Credentials.getPrivateKey(privateKeyPath));
decrypter.setCredentials(creds);
HashMap<String, Serializable> dictionary = new HashMap<>();
dictionary.putAll(variables.getScriptMap());
dictionary.putAll(variables.getInheritedMap());
dictionary.putAll(variables.getPropagatedVariables());
dictionary.putAll(variables.getScopeMap());
// start handler for binding
ScriptHandler handler = ScriptLoader.createHandler(nodes.get(0));
VariablesMap resolvedMap = new VariablesMap();
resolvedMap.setInheritedMap(VariableSubstitutor.resolveVariables(variables.getInheritedMap(), dictionary));
resolvedMap.setScopeMap(VariableSubstitutor.resolveVariables(variables.getScopeMap(), dictionary));
handler.addBinding(SchedulerConstants.VARIABLES_BINDING_NAME, (Serializable) resolvedMap);
handler.addBinding(SchedulerConstants.GENERIC_INFO_BINDING_NAME, (Serializable) genericInformation);
// retrieve scheduler URL to bind with schedulerapi, globalspaceapi, and userspaceapi
String schedulerUrl = PASchedulerProperties.SCHEDULER_REST_URL.getValueAsString();
logger.debug("Binding schedulerapi...");
SchedulerNodeClient client = new SchedulerNodeClient(decrypter, schedulerUrl);
handler.addBinding(SchedulerConstants.SCHEDULER_CLIENT_BINDING_NAME, (Serializable) client);
logger.debug("Binding globalspaceapi...");
RemoteSpace globalSpaceClient = new DataSpaceNodeClient(client, IDataSpaceClient.Dataspace.GLOBAL, schedulerUrl);
handler.addBinding(SchedulerConstants.DS_GLOBAL_API_BINDING_NAME, (Serializable) globalSpaceClient);
logger.debug("Binding userspaceapi...");
RemoteSpace userSpaceClient = new DataSpaceNodeClient(client, IDataSpaceClient.Dataspace.USER, schedulerUrl);
handler.addBinding(SchedulerConstants.DS_USER_API_BINDING_NAME, (Serializable) userSpaceClient);
logger.debug("Binding credentials...");
Map<String, String> resolvedThirdPartyCredentials = VariableSubstitutor.filterAndUpdate(decrypter.decrypt().getThirdPartyCredentials(), dictionary);
handler.addBinding(SchedulerConstants.CREDENTIALS_VARIABLE, (Serializable) resolvedThirdPartyCredentials);
ScriptResult<?> future = handler.handle(cleaningScript);
try {
PAEventProgramming.addActionOnFuture(future, "cleanCallBack", nodes);
} catch (IllegalArgumentException e) {
// TODO - linked to PROACTIVE-936 -> IllegalArgumentException is raised if method name is unknown
// should be replaced by checked exception
instance.error(taskId, "ERROR : Callback method won't be executed, node won't be released. This is a critical state, check the callback method name", e);
}
instance.info(taskId, "Cleaning Script started on node " + nodes.get(0).getNodeInformation().getURL());
} catch (Exception e) {
// if active object cannot be created or script has failed
instance.error(taskId, "Error while starting cleaning script for task " + taskId + " on " + nodes.get(0), e);
releaseNodes(nodes).booleanValue();
}
}
use of org.ow2.proactive.scheduler.task.utils.Decrypter in project scheduling by ow2-proactive.
the class TaskProActiveDataspaces method initDataSpaces.
private void initDataSpaces() throws Exception {
long startTime = System.currentTimeMillis();
// configure node for application
String appId = taskId.toString();
// prepare scratch, input, output
Node node = PAActiveObject.getNode();
logger.info("Configuring dataspaces for app " + appId + " on " + node.getNodeInformation().getName());
DataSpacesNodes.configureApplication(node, appId, namingService);
SCRATCH = PADataSpaces.resolveScratchForAO();
logger.info("SCRATCH space is " + SCRATCH.getRealURI());
// Set the scratch folder writable for everyone
if (!SCRATCH.setWritable(true, false)) {
logger.warn("Missing permission to change write permissions to " + getScratchFolder());
}
InputOutputSpaceConfiguration cacheConfiguration = DataSpaceNodeConfigurationAgent.getCacheSpaceConfiguration();
if (cacheConfiguration != null) {
final String cacheName = cacheConfiguration.getName();
cacheSpaceInstanceInfo = new SpaceInstanceInfo(appId, cacheConfiguration);
try {
namingService.register(cacheSpaceInstanceInfo);
} catch (SpaceAlreadyRegisteredException e) {
// this is a rare case where the cache space has already been registered for the same task and there was a node failure.
namingService.unregister(cacheSpaceInstanceInfo.getMountingPoint());
namingService.register(cacheSpaceInstanceInfo);
}
CACHE = initDataSpace(new Callable<DataSpacesFileObject>() {
@Override
public DataSpacesFileObject call() throws Exception {
return PADataSpaces.resolveOutput(cacheName);
}
}, "CACHE", false);
} else {
logger.error("No Cache space configuration found, cache space is disabled.");
}
UserCredentials userCredentials;
if (decrypter != null) {
CredData credData = decrypter.decrypt();
userCredentials = new UserCredentials(credData.getLogin(), credData.getPassword(), credData.getDomain(), credData.getKey());
} else {
logger.warn("No decryter found");
userCredentials = new UserCredentials();
}
INPUT = initDataSpace(new Callable<DataSpacesFileObject>() {
@Override
public DataSpacesFileObject call() throws Exception {
return PADataSpaces.resolveDefaultInput();
}
}, "INPUT", true);
OUTPUT = initDataSpace(new Callable<DataSpacesFileObject>() {
@Override
public DataSpacesFileObject call() throws Exception {
return PADataSpaces.resolveDefaultOutput();
}
}, "OUTPUT", false);
GLOBAL = initDataSpace(new Callable<DataSpacesFileObject>() {
@Override
public DataSpacesFileObject call() throws Exception {
return PADataSpaces.resolveOutput(SchedulerConstants.GLOBALSPACE_NAME);
}
}, "GLOBAL", false);
USER = initDataSpace(new Callable<DataSpacesFileObject>() {
@Override
public DataSpacesFileObject call() throws Exception {
return PADataSpaces.resolveOutput(SchedulerConstants.USERSPACE_NAME, userCredentials);
}
}, "USER", false);
logger.info("Time needed to mount data spaces: " + (System.currentTimeMillis() - startTime) + " ms");
}
use of org.ow2.proactive.scheduler.task.utils.Decrypter in project scheduling by ow2-proactive.
the class ForkedTaskExecutorRunAsMeTest method runAsMe_PasswordMethod.
@Test
public void runAsMe_PasswordMethod() throws Throwable {
TestTaskOutput taskOutput = new TestTaskOutput();
Decrypter decrypter = createCredentials(USERNAME, PASSWORD);
ForkedTaskExecutor taskExecutor = new ForkedTaskExecutor(tmpFolder.newFolder());
TaskLauncherInitializer initializer = new TaskLauncherInitializer();
initializer.setTaskId((TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L)));
ScriptExecutableContainer container = new ScriptExecutableContainer(new TaskScript(new SimpleScript("whoami", "native")));
container.setRunAsUser(true);
TaskContext taskContext = new TaskContext(container, initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", new NodeInfo("", "", "", ""), decrypter);
TaskResultImpl result = taskExecutor.execute(taskContext, taskOutput.outputStream, taskOutput.error);
assertTaskResultOk(result);
assertEquals("admin\n", taskOutput.output());
}
use of org.ow2.proactive.scheduler.task.utils.Decrypter in project scheduling by ow2-proactive.
the class InProcessTaskExecutorTest method scriptArgumentsReplacements.
@Test
public void scriptArgumentsReplacements() throws Throwable {
TestTaskOutput taskOutput = new TestTaskOutput();
TaskLauncherInitializer initializer = new TaskLauncherInitializer();
String printArgs = "println(args[0] + args[1]);";
initializer.setPreScript(new SimpleScript(printArgs, "groovy", new Serializable[] { "$credentials_PASSWORD", "$PA_JOB_ID" }));
initializer.setPostScript(new SimpleScript(printArgs, "groovy", new Serializable[] { "$credentials_PASSWORD", "$PA_JOB_ID" }));
initializer.setTaskId(TaskIdImpl.createTaskId(new JobIdImpl(1000, "job"), "task", 42L));
Decrypter decrypter = createCredentials("somebody_that_does_not_exists");
TaskContext taskContext = new TaskContext(new ScriptExecutableContainer(new TaskScript(new SimpleScript(printArgs, "groovy", new Serializable[] { "$credentials_PASSWORD", "${PA_JOB_ID}" }))), initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", new NodeInfo("", "", "", ""), decrypter);
new InProcessTaskExecutor().execute(taskContext, taskOutput.outputStream, taskOutput.error);
// pre, task and post
assertEquals(String.format("p4ssw0rd1000%np4ssw0rd1000%np4ssw0rd1000%n"), taskOutput.output());
}
use of org.ow2.proactive.scheduler.task.utils.Decrypter in project scheduling by ow2-proactive.
the class InProcessTaskExecutorTest method createCredentials.
private Decrypter createCredentials(String username) throws NoSuchAlgorithmException, KeyException {
CredData credData = new CredData(username, "pwd");
credData.addThirdPartyCredential("PASSWORD", "p4ssw0rd");
KeyPairGenerator keyGen;
keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(512, new SecureRandom());
KeyPair keyPair = keyGen.generateKeyPair();
Decrypter decrypter = new Decrypter(keyPair.getPrivate());
Credentials credentials = Credentials.createCredentials(credData, keyPair.getPublic());
decrypter.setCredentials(credentials);
return decrypter;
}
Aggregations