Search in sources :

Example 1 with IServerKeyVerifier

use of org.platformlayer.ops.ssh.IServerKeyVerifier in project platformlayer by platformlayer.

the class MachineBase method getTarget.

@Override
public OpsTarget getTarget(String user, KeyPair sshKeyPair) throws OpsException {
    OpsSystem opsSystem = OpsContext.get().getOpsSystem();
    ISshContext sshContext = opsSystem.getSshContext();
    SshConnection sshConnection = sshContext.getSshConnection(user);
    String address = getNetworkPoint().getBestAddress(NetworkPoint.forMe());
    try {
        sshConnection.setHost(InetAddress.getByName(address));
    } catch (UnknownHostException e) {
        throw new OpsException("Error resolving address: " + address, e);
    }
    sshConnection.setKeyPair(sshKeyPair);
    File tempDirBase = new File("/tmp/");
    // TODO: Verify the server key once we've learned it
    IServerKeyVerifier serverKeyVerifier = new AcceptAllLearningServerKeyVerifier();
    sshConnection.setServerKeyVerifier(serverKeyVerifier);
    return new SshOpsTarget(tempDirBase, this, sshConnection);
}
Also used : SshConnection(org.platformlayer.ops.ssh.SshConnection) AcceptAllLearningServerKeyVerifier(org.platformlayer.ops.ssh.AcceptAllLearningServerKeyVerifier) UnknownHostException(java.net.UnknownHostException) IServerKeyVerifier(org.platformlayer.ops.ssh.IServerKeyVerifier) File(java.io.File) ISshContext(org.platformlayer.ops.ssh.ISshContext)

Example 2 with IServerKeyVerifier

use of org.platformlayer.ops.ssh.IServerKeyVerifier in project platformlayer by platformlayer.

the class CloudContextRegistry method getSshConnection.

private SshConnection getSshConnection(String host, String user, KeyPair sshKeyPair) throws OpsException {
    OpsSystem opsSystem = OpsContext.get().getOpsSystem();
    ISshContext sshContext = opsSystem.getSshContext();
    SshConnection sshConnection = sshContext.getSshConnection(user);
    try {
        sshConnection.setHost(InetAddress.getByName(host));
    } catch (UnknownHostException e) {
        throw new OpsException("Error resolving address: " + host, e);
    }
    sshConnection.setKeyPair(sshKeyPair);
    // TODO: Verify the server key once we've learned it
    IServerKeyVerifier serverKeyVerifier = new AcceptAllLearningServerKeyVerifier();
    sshConnection.setServerKeyVerifier(serverKeyVerifier);
    return sshConnection;
}
Also used : SshConnection(org.platformlayer.ops.ssh.SshConnection) AcceptAllLearningServerKeyVerifier(org.platformlayer.ops.ssh.AcceptAllLearningServerKeyVerifier) UnknownHostException(java.net.UnknownHostException) IServerKeyVerifier(org.platformlayer.ops.ssh.IServerKeyVerifier) ISshContext(org.platformlayer.ops.ssh.ISshContext)

Example 3 with IServerKeyVerifier

use of org.platformlayer.ops.ssh.IServerKeyVerifier in project platformlayer by platformlayer.

the class MinaSshConnection method activateConnection.

private void activateConnection(MinaSshConnectionWrapper sshConnection, TimeSpan connectTimeout, TimeSpan keyExchangeTimeout) throws IOException, SshException {
    boolean okay = false;
    try {
        if (!sshConnection.isConnected()) {
            log.info("Making new SSH connection to " + getHost());
            sshConnection.setServerKeyVerifier(this.getServerKeyVerifier());
            if (connectTimeout == null) {
                connectTimeout = TimeSpan.ZERO;
            }
            if (keyExchangeTimeout == null) {
                keyExchangeTimeout = TimeSpan.ZERO;
            }
            sshConnection.connect(connectTimeout);
            if (!sshConnection.isConnected()) {
                throw new IllegalStateException("Connection completed, but could not get connection details");
            }
        } else {
            IServerKeyVerifier myServerKeyVerifier = getServerKeyVerifier();
            myServerKeyVerifier.verifyPooled(sshConnection.getServerKeyVerifier());
        }
        if (!sshConnection.isAuthenticationComplete()) {
            // Authenticate
            boolean isAuthenticated = sshConnection.authenticateWithPublicKey(getUser(), getKeyPair(), keyExchangeTimeout);
            if (isAuthenticated == false) {
                // valid
                throw new SshException("Authentication failed.  Tried to connect to " + getUser() + "@" + sshConnection.getConnectionInfo().getHost());
            } else {
                log.debug("SSH authentication succeeded");
            }
        }
        okay = true;
    } finally {
        if (!okay) {
            // If we fail to activate for any reason, we reset the
            // connection so that we start clean
            log.info("Resetting connection after failure to connect");
            close();
        }
    }
}
Also used : IServerKeyVerifier(org.platformlayer.ops.ssh.IServerKeyVerifier) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Aggregations

IServerKeyVerifier (org.platformlayer.ops.ssh.IServerKeyVerifier)3 UnknownHostException (java.net.UnknownHostException)2 AcceptAllLearningServerKeyVerifier (org.platformlayer.ops.ssh.AcceptAllLearningServerKeyVerifier)2 ISshContext (org.platformlayer.ops.ssh.ISshContext)2 SshConnection (org.platformlayer.ops.ssh.SshConnection)2 File (java.io.File)1 RuntimeSshException (org.apache.sshd.common.RuntimeSshException)1 SshException (org.platformlayer.ops.ssh.SshException)1