use of net.schmizz.sshj.userauth.keyprovider.KeyProvider in project AmazeFileManager by TeamAmaze.
the class SshAuthenticationTask method doInBackground.
@Override
protected AsyncTaskResult<SSHClient> doInBackground(Void... voids) {
final SSHClient sshClient = new SSHClient(new CustomSshJConfig());
sshClient.addHostKeyVerifier(hostKey);
sshClient.setConnectTimeout(SSH_CONNECT_TIMEOUT);
try {
sshClient.connect(hostname, port);
if (password != null && !"".equals(password)) {
sshClient.authPassword(username, password);
return new AsyncTaskResult<SSHClient>(sshClient);
} else {
sshClient.authPublickey(username, new KeyProvider() {
@Override
public PrivateKey getPrivate() throws IOException {
return privateKey.getPrivate();
}
@Override
public PublicKey getPublic() throws IOException {
return privateKey.getPublic();
}
@Override
public KeyType getType() throws IOException {
return KeyType.fromKey(getPublic());
}
});
return new AsyncTaskResult<SSHClient>(sshClient);
}
} catch (UserAuthException e) {
e.printStackTrace();
return new AsyncTaskResult<SSHClient>(e);
} catch (TransportException e) {
e.printStackTrace();
return new AsyncTaskResult<SSHClient>(e);
} catch (IOException e) {
e.printStackTrace();
return new AsyncTaskResult<SSHClient>(e);
}
}
use of net.schmizz.sshj.userauth.keyprovider.KeyProvider in project airavata by apache.
the class SSHSecurityContext method getSession.
public Session getSession(String hostAddress) throws IOException {
try {
if (sshClient == null) {
sshClient = new SSHClient();
}
if (getSSHClient().isConnected())
return getSSHClient().startSession();
KeyProvider pkey = getSSHClient().loadKeys(getPrivateKeyLoc(), getKeyPass());
getSSHClient().loadKnownHosts();
getSSHClient().connect(hostAddress);
getSSHClient().authPublickey(getUsername(), pkey);
session = getSSHClient().startSession();
return session;
} catch (NullPointerException ne) {
throw new SecurityException("Cannot load security context for SSH", ne);
}
}
use of net.schmizz.sshj.userauth.keyprovider.KeyProvider in project dbeaver by dbeaver.
the class SSHImplementationSshj method setupTunnel.
@Override
protected void setupTunnel(DBRProgressMonitor monitor, DBWHandlerConfiguration configuration, String dbHost, String sshHost, String aliveInterval, int sshPortNum, File privKeyFile, int connectTimeout, int dbPort, int localPort) throws DBException, IOException {
try {
Config clientConfig = new DefaultConfig();
clientConfig.setLoggerFactory(LoggerFactory.DEFAULT);
sshClient = new SSHClient(clientConfig);
// TODO: make real host verifier
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
String sshUser = configuration.getUserName();
String sshPassword = configuration.getPassword();
try {
sshClient.loadKnownHosts();
} catch (IOException e) {
log.warn("Error loading known hosts", e);
}
sshClient.connect(sshHost);
if (privKeyFile != null) {
if (!CommonUtils.isEmpty(sshPassword)) {
KeyProvider keyProvider = sshClient.loadKeys(privKeyFile.getAbsolutePath(), sshPassword.toCharArray());
sshClient.authPublickey(sshUser, keyProvider);
} else {
sshClient.authPublickey(sshUser, privKeyFile.getAbsolutePath());
}
} else {
sshClient.authPassword(sshUser, sshPassword);
}
log.debug("Instantiate SSH tunnel");
final LocalPortForwarder.Parameters params = new LocalPortForwarder.Parameters(SSHConstants.LOCALHOST_NAME, localPort, dbHost, dbPort);
portListener = new LocalPortListener(params);
portListener.start();
RuntimeUtils.pause(100);
} catch (Exception e) {
throw new DBException("Cannot establish tunnel", e);
}
}
Aggregations