Search in sources :

Example 61 with JSch

use of com.jcraft.jsch.JSch in project whirr by apache.

the class ClusterSpec method checkAndSetKeyPair.

protected void checkAndSetKeyPair() throws ConfigurationException {
    String pairRepresentation = "";
    try {
        String privateKeyPath = getString(Property.PRIVATE_KEY_FILE);
        String publicKeyPath = getString(Property.PUBLIC_KEY_FILE);
        publicKeyPath = (publicKeyPath == null && privateKeyPath != null) ? privateKeyPath + ".pub" : publicKeyPath;
        if (privateKeyPath != null && publicKeyPath != null) {
            pairRepresentation = "(" + privateKeyPath + ", " + publicKeyPath + ")";
            KeyPair pair = KeyPair.load(new JSch(), privateKeyPath, publicKeyPath);
            if (pair.isEncrypted()) {
                throw new ConfigurationException("Key pair " + pairRepresentation + " is encrypted. Try generating a new passwordless SSH keypair " + "(e.g. with ssh-keygen).");
            }
            if (!sameKeyPair(new File(privateKeyPath), new File(publicKeyPath))) {
                throw new ConfigurationException("Both keys should belong " + "to the same key pair: " + pairRepresentation);
            }
            setPrivateKey(new File(privateKeyPath));
            setPublicKey(new File(publicKeyPath));
        }
    } catch (JSchException e) {
        throw new ConfigurationException("Invalid key pair: " + pairRepresentation, e);
    } catch (IllegalArgumentException e) {
        throw new ConfigurationException("Invalid key: " + pairRepresentation, e);
    } catch (IOException e) {
        throw new ConfigurationException("Error reading one of key file: " + pairRepresentation, e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) KeyPair(com.jcraft.jsch.KeyPair) KeyPair.sameKeyPair(org.apache.whirr.util.KeyPair.sameKeyPair) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) File(java.io.File)

Example 62 with JSch

use of com.jcraft.jsch.JSch in project blueocean-plugin by jenkinsci.

the class GitReadSaveTest method startSSH.

private void startSSH(@Nullable User u) throws Exception {
    if (sshd == null) {
        // Set up an SSH server with access to a git repo
        User user;
        if (u == null) {
            user = login();
        } else {
            user = u;
        }
        final BasicSSHUserPrivateKey key = UserSSHKeyManager.getOrCreate(user);
        final JSch jsch = new JSch();
        final KeyPair pair = KeyPair.load(jsch, key.getPrivateKey().getBytes(), null);
        File keyFile = new File(System.getProperty("TEST_SSH_SERVER_KEY_FILE", File.createTempFile("hostkey", "ser").getCanonicalPath()));
        int port = Integer.parseInt(System.getProperty("TEST_SSH_SERVER_PORT", "0"));
        boolean allowLocalUser = Boolean.getBoolean("TEST_SSH_SERVER_ALLOW_LOCAL");
        String userPublicKey = Base64.getEncoder().encodeToString(pair.getPublicKeyBlob());
        sshd = new SSHServer(repoForSSH.getRoot(), keyFile, port, allowLocalUser, MapsHelper.of("bob", userPublicKey), true);
        // Go, go, go
        sshd.start();
    }
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) SSHServer(io.jenkins.blueocean.test.ssh.SSHServer) User(hudson.model.User) JSch(com.jcraft.jsch.JSch) File(java.io.File) BasicSSHUserPrivateKey(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)

Example 63 with JSch

use of com.jcraft.jsch.JSch in project ats-framework by Axway.

the class JschSftpClient method connect.

/**
 * Create SFTP session connection
 *
 * @param user the user name
 * @param password the user password
 * @param host the target host
 * @param port the specific port to use
 * @param privateKey private key location. For example: ~/.ssh/id_rsa
 * @param privateKeyPassword private key passphrase (or null if it hasn't)
 */
public void connect(String user, String password, String host, int port, String privateKey, String privateKeyPassword) {
    try {
        // disconnect if needed or stay connected if the host is the same
        if (this.session != null && this.session.isConnected()) {
            if (this.host.equals(host) && this.user.equals(user) && this.port == port) {
                // already connected
                return;
            } else {
                disconnect();
            }
        }
        this.user = user;
        this.host = host;
        this.port = port;
        JSch jsch = new JSch();
        jsch.setConfigRepository(new JschConfigRepository(this.host, this.user, this.port, this.configurationProperties));
        for (Entry<String, String> entry : configurationProperties.entrySet()) {
            if (entry.getKey().startsWith("global.")) {
                JSch.setConfig(entry.getKey().split("\\.")[1], entry.getValue());
            }
        }
        if (privateKey != null) {
            jsch.addIdentity(privateKey, privateKeyPassword);
        }
        if (port > 0) {
            this.session = jsch.getSession(user, host, port);
        } else {
            this.session = jsch.getSession(user, host);
        }
        this.session.setPassword(password);
        // apply any configuration properties
        for (Entry<String, String> entry : configurationProperties.entrySet()) {
            if (entry.getKey().startsWith("session.")) {
                session.setConfig(entry.getKey().split("\\.")[1], entry.getValue());
            } else if (!entry.getKey().startsWith("global.")) {
                // by default if global or session prefix is
                // missing, we assume it is a session property
                session.setConfig(entry.getKey(), entry.getValue());
            }
        }
        this.session.connect(CONNECTION_TIMEOUT);
        this.channel = (ChannelSftp) this.session.openChannel("sftp");
        // there is a bug in the other method channel.connect( TIMEOUT );
        this.channel.connect();
    } catch (Exception e) {
        throw new JschSftpClientException(e.getMessage(), e);
    }
}
Also used : JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException) JSch(com.jcraft.jsch.JSch) JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException)

Example 64 with JSch

use of com.jcraft.jsch.JSch in project ats-framework by Axway.

the class JschSshClient method connect.

/**
 * @param user the user name
 * @param password the user password
 * @param host the target host
 * @param port the specific port to use
 * @param privateKey private key location. For example: ~/.ssh/id_rsa
 * @param privateKeyPassword private key passphrase (or null if it hasn't)
 */
public void connect(String user, String password, String host, int port, String privateKey, String privateKeyPassword) {
    try {
        // disconnect if needed or stay connected if the host is the same
        if (session != null && session.isConnected()) {
            if (this.host.equals(host) && this.user.equals(user) && this.port == port) {
                // already connected
                return;
            } else {
                disconnect();
            }
        }
        this.user = user;
        this.host = host;
        this.port = port;
        JSch jsch = new JSch();
        jsch.setConfigRepository(new JschConfigRepository(this.host, this.user, this.port, this.configurationProperties));
        // apply global configuration properties
        for (Entry<String, String> entry : configurationProperties.entrySet()) {
            if (entry.getKey().startsWith("global.")) {
                JSch.setConfig(entry.getKey().split("\\.")[1], entry.getValue());
            }
        }
        if (privateKey != null) {
            jsch.addIdentity(privateKey, privateKeyPassword);
        }
        if (port > 0) {
            session = jsch.getSession(user, host, port);
        } else {
            session = jsch.getSession(user, host);
        }
        session.setPassword(password);
        // apply session configuration properties
        for (Entry<String, String> entry : configurationProperties.entrySet()) {
            if (entry.getKey().startsWith("session.")) {
                session.setConfig(entry.getKey().split("\\.")[1], entry.getValue());
            } else if (!entry.getKey().startsWith("global.")) {
                // by default if global or session prefix is
                // missing, we assume it is a session property
                session.setConfig(entry.getKey(), entry.getValue());
            }
        }
        session.connect(CONNECTION_TIMEOUT);
    } catch (Exception e) {
        throw new JschSshClientException(e.getMessage() + "; Connection parameters are: user '" + user + "' at " + host + " on port " + port, e);
    }
}
Also used : JschSshClientException(com.axway.ats.core.ssh.exceptions.JschSshClientException) JSch(com.jcraft.jsch.JSch) JschSshClientException(com.axway.ats.core.ssh.exceptions.JschSshClientException)

Example 65 with JSch

use of com.jcraft.jsch.JSch in project aws-cf-templates by widdix.

the class ATest method probeSSH.

protected final void probeSSH(final Context context, final String host, final KeyPair key) {
    final Callable<Boolean> callable = () -> {
        final JSch jsch = new JSch();
        final Session session = jsch.getSession("ec2-user", host);
        jsch.addIdentity(key.getKeyName(), key.getKeyMaterial().getBytes(), null, null);
        // for testing this should be fine. adding the host key seems to be only possible via a file which is not very useful here
        jsch.setConfig("StrictHostKeyChecking", "no");
        session.connect(10000);
        session.disconnect();
        return true;
    };
    Assert.assertTrue("successful SSH connection", this.retry(context, callable));
}
Also used : JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Aggregations

JSch (com.jcraft.jsch.JSch)130 Session (com.jcraft.jsch.Session)72 JSchException (com.jcraft.jsch.JSchException)51 IOException (java.io.IOException)50 Channel (com.jcraft.jsch.Channel)35 File (java.io.File)29 InputStream (java.io.InputStream)29 Properties (java.util.Properties)27 ChannelExec (com.jcraft.jsch.ChannelExec)26 ChannelSftp (com.jcraft.jsch.ChannelSftp)22 KeyPair (com.jcraft.jsch.KeyPair)19 BufferedReader (java.io.BufferedReader)16 UserInfo (com.jcraft.jsch.UserInfo)15 InputStreamReader (java.io.InputStreamReader)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 FileInputStream (java.io.FileInputStream)11 OutputStream (java.io.OutputStream)11 SftpException (com.jcraft.jsch.SftpException)10 FS (org.eclipse.jgit.util.FS)8 FileOutputStream (java.io.FileOutputStream)7