Search in sources :

Example 1 with Decrypter

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();
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SchedulerNodeClient(org.ow2.proactive.scheduler.task.client.SchedulerNodeClient) Decrypter(org.ow2.proactive.scheduler.task.utils.Decrypter) LoginException(javax.security.auth.login.LoginException) TaskLogger(org.ow2.proactive.scheduler.util.TaskLogger) RemoteSpace(org.ow2.proactive.scheduler.common.task.dataspaces.RemoteSpace) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) DataSpaceNodeClient(org.ow2.proactive.scheduler.task.client.DataSpaceNodeClient) ScriptHandler(org.ow2.proactive.scripting.ScriptHandler)

Example 2 with Decrypter

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");
}
Also used : InputOutputSpaceConfiguration(org.objectweb.proactive.extensions.dataspaces.core.InputOutputSpaceConfiguration) SpaceInstanceInfo(org.objectweb.proactive.extensions.dataspaces.core.SpaceInstanceInfo) SpaceAlreadyRegisteredException(org.objectweb.proactive.extensions.dataspaces.exceptions.SpaceAlreadyRegisteredException) Node(org.objectweb.proactive.core.node.Node) CredData(org.ow2.proactive.authentication.crypto.CredData) UserCredentials(org.objectweb.proactive.extensions.dataspaces.api.UserCredentials) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) Callable(java.util.concurrent.Callable)

Example 3 with Decrypter

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());
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) NodeInfo(org.ow2.proactive.scheduler.task.context.NodeInfo) ForkedTaskExecutor(org.ow2.proactive.scheduler.task.executors.ForkedTaskExecutor) SimpleScript(org.ow2.proactive.scripting.SimpleScript) Decrypter(org.ow2.proactive.scheduler.task.utils.Decrypter) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) TestTaskOutput(org.ow2.proactive.scheduler.task.TestTaskOutput) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) Test(org.junit.Test)

Example 4 with Decrypter

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());
}
Also used : Serializable(java.io.Serializable) TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskScript(org.ow2.proactive.scripting.TaskScript) InProcessTaskExecutor(org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor) SimpleScript(org.ow2.proactive.scripting.SimpleScript) Decrypter(org.ow2.proactive.scheduler.task.utils.Decrypter) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) NodeInfo(org.ow2.proactive.scheduler.task.context.NodeInfo) JobIdImpl(org.ow2.proactive.scheduler.job.JobIdImpl) Test(org.junit.Test)

Example 5 with Decrypter

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;
}
Also used : KeyPair(java.security.KeyPair) CredData(org.ow2.proactive.authentication.crypto.CredData) SecureRandom(java.security.SecureRandom) Decrypter(org.ow2.proactive.scheduler.task.utils.Decrypter) KeyPairGenerator(java.security.KeyPairGenerator) Credentials(org.ow2.proactive.authentication.crypto.Credentials)

Aggregations

Decrypter (org.ow2.proactive.scheduler.task.utils.Decrypter)16 Test (org.junit.Test)9 ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)9 CredData (org.ow2.proactive.authentication.crypto.CredData)7 NodeDataSpacesURIs (org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs)7 NodeInfo (org.ow2.proactive.scheduler.task.context.NodeInfo)7 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)7 SimpleScript (org.ow2.proactive.scripting.SimpleScript)6 TaskScript (org.ow2.proactive.scripting.TaskScript)6 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)5 KeyPair (java.security.KeyPair)4 Credentials (org.ow2.proactive.authentication.crypto.Credentials)4 TaskDataspaces (org.ow2.proactive.scheduler.task.data.TaskDataspaces)4 VariablesMap (org.ow2.proactive.scheduler.task.utils.VariablesMap)4 ScriptHandler (org.ow2.proactive.scripting.ScriptHandler)4 Serializable (java.io.Serializable)3 KeyPairGenerator (java.security.KeyPairGenerator)3 SecureRandom (java.security.SecureRandom)3 HashMap (java.util.HashMap)3 NamingService (org.objectweb.proactive.extensions.dataspaces.core.naming.NamingService)3