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();
}
}
}
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);
}
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);
}
};
}
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
}
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);
}
}
Aggregations