Search in sources :

Example 1 with KeyProvider

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);
    }
}
Also used : CustomSshJConfig(com.amaze.filemanager.filesystem.ssh.CustomSshJConfig) KeyProvider(net.schmizz.sshj.userauth.keyprovider.KeyProvider) PrivateKey(java.security.PrivateKey) KeyType(net.schmizz.sshj.common.KeyType) PublicKey(java.security.PublicKey) SSHClient(net.schmizz.sshj.SSHClient) AsyncTaskResult(com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult) IOException(java.io.IOException) UserAuthException(net.schmizz.sshj.userauth.UserAuthException) TransportException(net.schmizz.sshj.transport.TransportException)

Example 2 with KeyProvider

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);
    }
}
Also used : KeyProvider(net.schmizz.sshj.userauth.keyprovider.KeyProvider) SSHClient(net.schmizz.sshj.SSHClient)

Example 3 with KeyProvider

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);
    }
}
Also used : KeyProvider(net.schmizz.sshj.userauth.keyprovider.KeyProvider) PromiscuousVerifier(net.schmizz.sshj.transport.verification.PromiscuousVerifier) LocalPortForwarder(net.schmizz.sshj.connection.channel.direct.LocalPortForwarder) DBException(org.jkiss.dbeaver.DBException) DefaultConfig(net.schmizz.sshj.DefaultConfig) Config(net.schmizz.sshj.Config) IOException(java.io.IOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) DefaultConfig(net.schmizz.sshj.DefaultConfig) SSHClient(net.schmizz.sshj.SSHClient)

Aggregations

SSHClient (net.schmizz.sshj.SSHClient)3 KeyProvider (net.schmizz.sshj.userauth.keyprovider.KeyProvider)3 IOException (java.io.IOException)2 AsyncTaskResult (com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult)1 CustomSshJConfig (com.amaze.filemanager.filesystem.ssh.CustomSshJConfig)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 Config (net.schmizz.sshj.Config)1 DefaultConfig (net.schmizz.sshj.DefaultConfig)1 KeyType (net.schmizz.sshj.common.KeyType)1 LocalPortForwarder (net.schmizz.sshj.connection.channel.direct.LocalPortForwarder)1 TransportException (net.schmizz.sshj.transport.TransportException)1 PromiscuousVerifier (net.schmizz.sshj.transport.verification.PromiscuousVerifier)1 UserAuthException (net.schmizz.sshj.userauth.UserAuthException)1 DBException (org.jkiss.dbeaver.DBException)1