airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adham...@apache.org
Subject [1/6] airavata git commit: Adding common factory methods
Date Thu, 13 Apr 2017 18:47:49 GMT
Repository: airavata
Updated Branches:
  refs/heads/feature-workload-mgmt a974f3fb2 -> ea865f237


Adding common factory methods


Project: http://git-wip-us.apache.org/repos/asf/airavata/repo
Commit: http://git-wip-us.apache.org/repos/asf/airavata/commit/4248419d
Tree: http://git-wip-us.apache.org/repos/asf/airavata/tree/4248419d
Diff: http://git-wip-us.apache.org/repos/asf/airavata/diff/4248419d

Branch: refs/heads/feature-workload-mgmt
Commit: 4248419db5f02aa3ed9e8d8fc0fc1d0c85ce953b
Parents: a974f3f
Author: Ajinkya Dhamnaskar <adhamnas@apache.org>
Authored: Thu Apr 13 14:34:51 2017 -0400
Committer: Ajinkya Dhamnaskar <adhamnas@apache.org>
Committed: Thu Apr 13 14:34:51 2017 -0400

----------------------------------------------------------------------
 .../worker/core/utils/WorkerFactory.java        | 181 ++++++++++++++++++-
 1 file changed, 180 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/airavata/blob/4248419d/modules/worker/worker-core/src/main/java/org/apache/airavata/worker/core/utils/WorkerFactory.java
----------------------------------------------------------------------
diff --git a/modules/worker/worker-core/src/main/java/org/apache/airavata/worker/core/utils/WorkerFactory.java
b/modules/worker/worker-core/src/main/java/org/apache/airavata/worker/core/utils/WorkerFactory.java
index 6dcd275..75a8062 100644
--- a/modules/worker/worker-core/src/main/java/org/apache/airavata/worker/core/utils/WorkerFactory.java
+++ b/modules/worker/worker-core/src/main/java/org/apache/airavata/worker/core/utils/WorkerFactory.java
@@ -23,9 +23,17 @@ package org.apache.airavata.worker.core.utils;
 import com.google.common.cache.Cache;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.RemovalListener;
-import com.jcraft.jsch.Session;
+import com.jcraft.jsch.*;
+import org.apache.airavata.common.exception.ApplicationSettingsException;
 import org.apache.airavata.common.utils.ServerSettings;
+import org.apache.airavata.credential.store.credential.Credential;
+import org.apache.airavata.credential.store.store.CredentialReader;
+import org.apache.airavata.credential.store.store.CredentialStoreException;
 import org.apache.airavata.model.appcatalog.computeresource.ResourceJobManagerType;
+import org.apache.airavata.credential.store.credential.impl.ssh.SSHCredential;
+import org.apache.airavata.worker.core.authentication.AuthenticationInfo;
+import org.apache.airavata.worker.core.authentication.SSHKeyAuthentication;
+import org.apache.airavata.worker.core.cluster.ServerInfo;
 import org.apache.airavata.worker.core.config.ResourceConfig;
 import org.apache.airavata.worker.core.config.WorkerYamlConfigruation;
 import org.apache.airavata.worker.core.exceptions.WorkerException;
@@ -34,6 +42,7 @@ import org.slf4j.LoggerFactory;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.UUID;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -79,4 +88,174 @@ public class WorkerFactory {
     public static ResourceConfig getResourceConfig(ResourceJobManagerType resourceJobManagerType)
{
         return resources.get(resourceJobManagerType);
     }
+
+    public static SSHKeyAuthentication getSshKeyAuthentication(String gatewayId,
+                                                                String loginUserName,
+                                                                String credentialStoreToken)
+            throws ApplicationSettingsException, IllegalAccessException, InstantiationException,
+            CredentialStoreException, WorkerException {
+
+        SSHKeyAuthentication sshKA;CredentialReader credentialReader = WorkerUtils.getCredentialReader();
+        Credential credential = credentialReader.getCredential(gatewayId, credentialStoreToken);
+        if (credential instanceof SSHCredential) {
+            sshKA = new SSHKeyAuthentication();
+            sshKA.setUserName(loginUserName);
+            SSHCredential sshCredential = (SSHCredential) credential;
+            sshKA.setPublicKey(sshCredential.getPublicKey());
+            sshKA.setPrivateKey(sshCredential.getPrivateKey());
+            sshKA.setPassphrase(sshCredential.getPassphrase());
+            sshKA.setStrictHostKeyChecking("no");
+/*            sshKA.setStrictHostKeyChecking(ServerSettings.getSetting("ssh.strict.hostKey.checking",
"no"));
+            sshKA.setKnownHostsFilePath(ServerSettings.getSetting("ssh.known.hosts.file",
null));
+            if (sshKA.getStrictHostKeyChecking().equals("yes") && sshKA.getKnownHostsFilePath()
== null) {
+                throw new ApplicationSettingsException("If ssh strict hostkey checking property
is set to yes, you must " +
+                        "provide known host file path");
+            }*/
+            return sshKA;
+        } else {
+            String msg = "Provided credential store token is not valid. Please provide the
correct credential store token";
+            log.error(msg);
+            throw new CredentialStoreException("Invalid credential store token:" + credentialStoreToken);
+        }
+    }
+
+    public static synchronized Session getSSHSession(AuthenticationInfo authenticationInfo,
+                                                     ServerInfo serverInfo) throws WorkerException
{
+        if (authenticationInfo == null
+                || serverInfo == null) {
+
+            throw new IllegalArgumentException("Can't create ssh session, argument should
be valid (not null)");
+        }
+        SSHKeyAuthentication authentication;
+        if (authenticationInfo instanceof SSHKeyAuthentication) {
+            authentication = (SSHKeyAuthentication) authenticationInfo;
+        } else {
+            throw new WorkerException("Support ssh key authentication only");
+        }
+        String key = buildKey(serverInfo);
+        Session session = sessionCache.getIfPresent(key);
+        boolean valid = isValidSession(session);
+        // FIXME - move following info logs to debug
+        if (valid) {
+            log.info("SSH Session validation succeeded, key :" + key);
+            valid = testChannelCreation(session);
+            if (valid) {
+                log.info("Channel creation test succeeded, key :" + key);
+            } else {
+                log.info("Channel creation test failed, key :" + key);
+            }
+        } else {
+            log.info("Session validation failed, key :" + key);
+        }
+
+        if (!valid) {
+            if (session != null) {
+                log.info("Reinitialize a new SSH session for :" + key);
+            } else {
+                log.info("Initialize a new SSH session for :" + key);
+            }
+            try {
+
+                JSch jSch = new JSch();
+                jSch.addIdentity(UUID.randomUUID().toString(), authentication.getPrivateKey(),
authentication.getPublicKey(),
+                        authentication.getPassphrase().getBytes());
+                session = jSch.getSession(serverInfo.getUserName(), serverInfo.getHost(),
+                        serverInfo.getPort());
+                session.setUserInfo(new DefaultUserInfo(serverInfo.getUserName(), null, authentication.getPassphrase()));
+                if (authentication.getStrictHostKeyChecking().equals("yes")) {
+                    jSch.setKnownHosts(authentication.getKnownHostsFilePath());
+                } else {
+                    session.setConfig("StrictHostKeyChecking", "no");
+                }
+                session.connect(); // 0 connection timeout
+                sessionCache.put(key, session);
+            } catch (JSchException e) {
+                throw new WorkerException("JSch initialization error ", e);
+            }
+        } else {
+            // FIXME - move following info log to debug
+            log.info("Reuse SSH session for :" + key);
+        }
+        return session;
+
+    }
+
+    private static boolean testChannelCreation(Session session) {
+
+        String command = "pwd ";
+        Channel channel = null;
+        try {
+            channel = session.openChannel("exec");
+            StandardOutReader stdOutReader = new StandardOutReader();
+            ((ChannelExec) channel).setCommand(command);
+            ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError());
+            channel.connect();
+            stdOutReader.onOutput(channel);
+        } catch (JSchException e) {
+            log.error("Test Channel creation failed.", e);
+            return false;
+        } finally {
+            if (channel != null) {
+                channel.disconnect();
+            }
+        }
+        return true;
+    }
+
+    private static boolean isValidSession(Session session) {
+        return session != null && session.isConnected();
+    }
+
+    private static String buildKey(ServerInfo serverInfo) {
+        return serverInfo.getUserName() +
+                "_" +
+                serverInfo.getHost() +
+                "_" +
+                serverInfo.getPort() +
+                "_" +
+                serverInfo.getCredentialToken();
+    }
+
+    private static class DefaultUserInfo implements UserInfo {
+
+        private String userName;
+        private String password;
+        private String passphrase;
+
+        public DefaultUserInfo(String userName, String password, String passphrase) {
+            this.userName = userName;
+            this.password = password;
+            this.passphrase = passphrase;
+        }
+
+        @Override
+        public String getPassphrase() {
+            return null;
+        }
+
+        @Override
+        public String getPassword() {
+            return null;
+        }
+
+        @Override
+        public boolean promptPassword(String s) {
+            return false;
+        }
+
+        @Override
+        public boolean promptPassphrase(String s) {
+            return false;
+        }
+
+        @Override
+        public boolean promptYesNo(String s) {
+            return false;
+        }
+
+        @Override
+        public void showMessage(String s) {
+
+        }
+    }
 }


Mime
View raw message