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