Search in sources :

Example 96 with JSch

use of com.jcraft.jsch.JSch in project opentest by mcdcorp.

the class GetFromSftp method run.

@Override
public void run() {
    super.run();
    String sftpHost = this.readStringArgument("sftpHost");
    Integer sftpPort = this.readIntArgument("sftpPort", 22);
    String userName = this.readStringArgument("userName");
    String password = this.readStringArgument("password");
    String sourceDir = this.readStringArgument("sourceDir");
    String sourceFile = this.readStringArgument("sourceFile");
    String destinationDir = this.readStringArgument("destinationDir");
    String destinationFileName = this.readStringArgument("destinationFile", sourceFile);
    Session session = null;
    Channel channel = null;
    ChannelSftp channelSftp = null;
    try {
        JSch jsch = new JSch();
        session = jsch.getSession(userName, sftpHost, sftpPort);
        session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        this.log.trace("Connected to SFTP host");
        channel = session.openChannel("sftp");
        channel.connect();
        this.log.trace("The SFTP channel was opened and connected");
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd(sourceDir);
        File destinationFile = new File(destinationDir, destinationFileName);
        FileOutputStream fileOutputStream = new FileOutputStream(destinationFile);
        channelSftp.get(sourceFile, fileOutputStream);
        fileOutputStream.close();
    } catch (Exception ex) {
        throw new RuntimeException("SFTP transfer failed", ex);
    } finally {
        if (channelSftp != null) {
            channelSftp.exit();
        }
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) FileOutputStream(java.io.FileOutputStream) JSch(com.jcraft.jsch.JSch) Properties(java.util.Properties) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 97 with JSch

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

the class KeyPair method generate.

/**
 * return a "public" -> rsa public key, "private" -> its corresponding
 *   private key
 */
public static Map<String, String> generate(String passPhrase) throws JSchException {
    com.jcraft.jsch.KeyPair pair = com.jcraft.jsch.KeyPair.genKeyPair(new JSch(), com.jcraft.jsch.KeyPair.RSA);
    if (passPhrase != null) {
        pair.setPassphrase(passPhrase);
    }
    ByteArrayOutputStream publicKeyOut = new ByteArrayOutputStream();
    ByteArrayOutputStream privateKeyOut = new ByteArrayOutputStream();
    pair.writePublicKey(publicKeyOut, "whirr");
    pair.writePrivateKey(privateKeyOut);
    String publicKey = new String(publicKeyOut.toByteArray());
    String privateKey = new String(privateKeyOut.toByteArray());
    return ImmutableMap.<String, String>of("public", publicKey, "private", privateKey);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch)

Example 98 with JSch

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

the class GitUtils method getSSHKeyTransport.

private static TransportConfigCallback getSSHKeyTransport(final BasicSSHUserPrivateKey privateKey) {
    final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {

        @Override
        protected void configure(OpenSshConfig.Host hc, com.jcraft.jsch.Session session) {
            // jenkins user doesn't likely have the host key
            session.setConfig("StrictHostKeyChecking", "no");
        }

        @Override
        protected JSch getJSch(OpenSshConfig.Host hc, FS fs) throws JSchException {
            JSch jsch = new JSch();
            configureJSch(jsch);
            // TODO: might need this: jsch.setHostKeyRepository(new KnownHosts(this));
            KeyPair pair = KeyPair.load(jsch, privateKey.getPrivateKey().getBytes(StandardCharsets.UTF_8), null);
            byte[] passphrase = new byte[0];
            jsch.addIdentity(privateKey.getUsername(), pair.forSSHAgent(), null, passphrase);
            return jsch;
        }
    };
    return transport -> {
        if (transport instanceof SshTransport) {
            SshTransport sshTransport = (SshTransport) transport;
            sshTransport.setSshSessionFactory(sshSessionFactory);
        }
    };
}
Also used : BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) StringUtils(org.apache.commons.lang.StringUtils) KeyPair(com.jcraft.jsch.KeyPair) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) Date(java.util.Date) JGitText(org.eclipse.jgit.internal.JGitText) LoggerFactory(org.slf4j.LoggerFactory) SshTransport(org.eclipse.jgit.transport.SshTransport) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BasicSSHUserPrivateKey(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey) CredentialsMatchers(com.cloudbees.plugins.credentials.CredentialsMatchers) ByteArrayInputStream(java.io.ByteArrayInputStream) GitClient(org.jenkinsci.plugins.gitclient.GitClient) CredentialsUtils(io.jenkins.blueocean.credential.CredentialsUtils) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) FileMode(org.eclipse.jgit.lib.FileMode) RefSpec(org.eclipse.jgit.transport.RefSpec) TimeZone(java.util.TimeZone) RefUpdate(org.eclipse.jgit.lib.RefUpdate) OpenSshConfig(org.eclipse.jgit.transport.OpenSshConfig) Constants(org.eclipse.jgit.lib.Constants) TransportCommand(org.eclipse.jgit.api.TransportCommand) ItemGroup(hudson.model.ItemGroup) RevTree(org.eclipse.jgit.revwalk.RevTree) StandardCharsets(java.nio.charset.StandardCharsets) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GitException(hudson.plugins.git.GitException) List(java.util.List) Ref(org.eclipse.jgit.lib.Ref) PushResult(org.eclipse.jgit.transport.PushResult) DirCache(org.eclipse.jgit.dircache.DirCache) FS(org.eclipse.jgit.util.FS) JSchException(com.jcraft.jsch.JSchException) Pattern(java.util.regex.Pattern) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) RevCommit(org.eclipse.jgit.revwalk.RevCommit) JSch(com.jcraft.jsch.JSch) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) Git(org.jenkinsci.plugins.gitclient.Git) FetchCommand(org.eclipse.jgit.api.FetchCommand) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) MergeCommand(org.eclipse.jgit.api.MergeCommand) ACL(hudson.security.ACL) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) SshSessionFactory(org.eclipse.jgit.transport.SshSessionFactory) EnvVars(hudson.EnvVars) Nonnull(javax.annotation.Nonnull) PushCommand(org.eclipse.jgit.api.PushCommand) Nullable(javax.annotation.Nullable) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) TaskListener(hudson.model.TaskListener) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) Logger(org.slf4j.Logger) SmartCredentialsProvider(org.jenkinsci.plugins.gitclient.trilead.SmartCredentialsProvider) IOException(java.io.IOException) URIRequirementBuilder(com.cloudbees.plugins.credentials.domains.URIRequirementBuilder) ServiceException(io.jenkins.blueocean.commons.ServiceException) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) ObjectId(org.eclipse.jgit.lib.ObjectId) TransportException(org.eclipse.jgit.api.errors.TransportException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) CredentialsProvider(com.cloudbees.plugins.credentials.CredentialsProvider) MergeResult(org.eclipse.jgit.api.MergeResult) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) ObjectReader(org.eclipse.jgit.lib.ObjectReader) Repository(org.eclipse.jgit.lib.Repository) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) InputStream(java.io.InputStream) KeyPair(com.jcraft.jsch.KeyPair) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) SshSessionFactory(org.eclipse.jgit.transport.SshSessionFactory) JSch(com.jcraft.jsch.JSch) FS(org.eclipse.jgit.util.FS) SshTransport(org.eclipse.jgit.transport.SshTransport)

Example 99 with JSch

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

the class SSHKeyUtilTest method sshKeyUtils.

@Test
public void sshKeyUtils() throws JSchException {
    String privateKey = SSHKeyUtils.generateKey(1024);
    Assert.assertNotNull(privateKey);
    JSch jsch = new JSch();
    KeyPair.load(jsch, privateKey.getBytes(), null);
// can only really verify the key can be loaded
}
Also used : JSch(com.jcraft.jsch.JSch) Test(org.junit.Test)

Example 100 with JSch

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

the class SSHKeyUtils method getPublicKey.

/**
 * Gets the public key, with a comment for the given private key
 * @param privateKey SSH private key to use
 * @param comment comment with the key
 * @return SSH public key
 */
public static String getPublicKey(String privateKey, String comment) {
    try {
        JSch jsch = new JSch();
        KeyPair pair = KeyPair.load(jsch, privateKey.getBytes(StandardCharsets.UTF_8), null);
        ByteArrayOutputStream keyOut = new ByteArrayOutputStream();
        pair.writePublicKey(keyOut, comment);
        return new String(keyOut.toByteArray(), StandardCharsets.UTF_8);
    } catch (Exception ex) {
        throw ex instanceof RuntimeException ? (RuntimeException) ex : new RuntimeException(ex);
    }
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) IOException(java.io.IOException)

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