Search in sources :

Example 21 with Connection

use of org.ovirt.engine.sdk4.Connection in project wildfly by wildfly.

the class RemotingLoginModule method login.

@SuppressWarnings("unchecked")
@Override
public boolean login() throws LoginException {
    if (super.login() == true) {
        log.debug("super.login()==true");
        return true;
    }
    Object credential = getCredential();
    if (credential instanceof RemotingConnectionCredential) {
        Connection con = ((RemotingConnectionCredential) credential).getConnection();
        Principal up = null;
        SecurityIdentity localIdentity = con.getLocalIdentity();
        if (localIdentity != null) {
            up = new RealmUser(localIdentity.getPrincipal().getName());
        }
        // If we found a principal from the connection then authentication succeeded.
        if (up != null) {
            identity = up;
            if (getUseFirstPass()) {
                String userName = identity.getName();
                log.debugf("Storing username '%s'", userName);
                // Add the username to the shared state map
                sharedState.put("javax.security.auth.login.name", identity);
                if (useNewClientCert) {
                    SSLSession session = con.getSslSession();
                    if (session != null) {
                        try {
                            credential = session.getPeerCertificates()[0];
                            log.debug("Using new certificate as credential.");
                        } catch (SSLPeerUnverifiedException e) {
                            log.debugf("No peer certificate available for '%s'", userName);
                        }
                    }
                } else if (useClientCert) {
                    SSLSession session = con.getSslSession();
                    if (session != null) {
                        try {
                            credential = session.getPeerCertificateChain()[0];
                            log.debug("Using certificate as credential.");
                        } catch (SSLPeerUnverifiedException e) {
                            log.debugf("No peer certificate available for '%s'", userName);
                        }
                    }
                }
                sharedState.put("javax.security.auth.login.password", credential);
            }
            loginOk = true;
            return true;
        }
    }
    // username and password has been supplied to a web auth.
    return false;
}
Also used : SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) Connection(org.jboss.remoting3.Connection) RealmUser(org.jboss.as.core.security.RealmUser) SSLSession(javax.net.ssl.SSLSession) Principal(java.security.Principal)

Example 22 with Connection

use of org.ovirt.engine.sdk4.Connection in project cloudstack by apache.

the class SshHelper method scpTo.

public static void scpTo(String host, int port, String user, File pemKeyFile, String password, String remoteTargetDirectory, String localFile, String fileMode, int connectTimeoutInMs, int kexTimeoutInMs) throws Exception {
    com.trilead.ssh2.Connection conn = null;
    com.trilead.ssh2.SCPClient scpClient = null;
    try {
        conn = new com.trilead.ssh2.Connection(host, port);
        conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
        if (pemKeyFile == null) {
            if (!conn.authenticateWithPassword(user, password)) {
                String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        } else {
            if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
                String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        scpClient = conn.createSCPClient();
        if (fileMode != null)
            scpClient.put(localFile, remoteTargetDirectory, fileMode);
        else
            scpClient.put(localFile, remoteTargetDirectory);
    } finally {
        if (conn != null)
            conn.close();
    }
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException)

Example 23 with Connection

use of org.ovirt.engine.sdk4.Connection in project cloudstack by apache.

the class SshHelper method scpTo.

public static void scpTo(String host, int port, String user, File pemKeyFile, String password, String remoteTargetDirectory, byte[] data, String remoteFileName, String fileMode, int connectTimeoutInMs, int kexTimeoutInMs) throws Exception {
    com.trilead.ssh2.Connection conn = null;
    com.trilead.ssh2.SCPClient scpClient = null;
    try {
        conn = new com.trilead.ssh2.Connection(host, port);
        conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
        if (pemKeyFile == null) {
            if (!conn.authenticateWithPassword(user, password)) {
                String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        } else {
            if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
                String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        scpClient = conn.createSCPClient();
        if (fileMode != null)
            scpClient.put(data, remoteFileName, remoteTargetDirectory, fileMode);
        else
            scpClient.put(data, remoteFileName, remoteTargetDirectory);
    } finally {
        if (conn != null)
            conn.close();
    }
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException)

Example 24 with Connection

use of org.ovirt.engine.sdk4.Connection in project cloudstack by apache.

the class SshHelper method sshExecute.

public static Pair<Boolean, String> sshExecute(String host, int port, String user, File pemKeyFile, String password, String command, int connectTimeoutInMs, int kexTimeoutInMs, int waitResultTimeoutInMs) throws Exception {
    com.trilead.ssh2.Connection conn = null;
    com.trilead.ssh2.Session sess = null;
    try {
        conn = new com.trilead.ssh2.Connection(host, port);
        conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
        if (pemKeyFile == null) {
            if (!conn.authenticateWithPassword(user, password)) {
                String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        } else {
            if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
                String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        sess = openConnectionSession(conn);
        sess.execCommand(command);
        InputStream stdout = sess.getStdout();
        InputStream stderr = sess.getStderr();
        byte[] buffer = new byte[8192];
        StringBuffer sbResult = new StringBuffer();
        int currentReadBytes = 0;
        while (true) {
            throwSshExceptionIfStdoutOrStdeerIsNull(stdout, stderr);
            if ((stdout.available() == 0) && (stderr.available() == 0)) {
                int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, waitResultTimeoutInMs);
                throwSshExceptionIfConditionsTimeout(conditions);
                if ((conditions & ChannelCondition.EXIT_STATUS) != 0) {
                    break;
                }
                if (canEndTheSshConnection(waitResultTimeoutInMs, sess, conditions)) {
                    break;
                }
            }
            while (stdout.available() > 0) {
                currentReadBytes = stdout.read(buffer);
                sbResult.append(new String(buffer, 0, currentReadBytes));
            }
            while (stderr.available() > 0) {
                currentReadBytes = stderr.read(buffer);
                sbResult.append(new String(buffer, 0, currentReadBytes));
            }
        }
        String result = sbResult.toString();
        if (StringUtils.isBlank(result)) {
            try {
                result = IOUtils.toString(stdout, StandardCharsets.UTF_8);
            } catch (IOException e) {
                s_logger.error("Couldn't get content of input stream due to: " + e.getMessage());
                return new Pair<Boolean, String>(false, result);
            }
        }
        if (sess.getExitStatus() == null) {
            //Exit status is NOT available. Returning failure result.
            s_logger.error(String.format("SSH execution of command %s has no exit status set. Result output: %s", command, result));
            return new Pair<Boolean, String>(false, result);
        }
        if (sess.getExitStatus() != null && sess.getExitStatus().intValue() != 0) {
            s_logger.error(String.format("SSH execution of command %s has an error status code in return. Result output: %s", command, result));
            return new Pair<Boolean, String>(false, result);
        }
        return new Pair<Boolean, String>(true, result);
    } finally {
        if (sess != null)
            sess.close();
        if (conn != null)
            conn.close();
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException) Session(com.trilead.ssh2.Session) Connection(com.trilead.ssh2.Connection) Pair(com.cloud.utils.Pair)

Example 25 with Connection

use of org.ovirt.engine.sdk4.Connection in project cloudstack by apache.

the class SshHelperTest method openConnectionSessionTest.

@Test
public void openConnectionSessionTest() throws IOException, InterruptedException {
    Connection conn = Mockito.mock(Connection.class);
    PowerMockito.mockStatic(Thread.class);
    SshHelper.openConnectionSession(conn);
    Mockito.verify(conn).openSession();
    PowerMockito.verifyStatic();
    Thread.sleep(Mockito.anyLong());
}
Also used : Connection(com.trilead.ssh2.Connection) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Connection (org.ovirt.engine.sdk4.Connection)63 Connection (com.trilead.ssh2.Connection)52 IOException (java.io.IOException)41 VmsService (org.ovirt.engine.sdk4.services.VmsService)33 Session (com.trilead.ssh2.Session)32 Vm (org.ovirt.engine.sdk4.types.Vm)30 InputStream (java.io.InputStream)25 VmService (org.ovirt.engine.sdk4.services.VmService)18 SystemService (org.ovirt.engine.sdk4.services.SystemService)14 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)12 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)12 Connection (okhttp3.Connection)11 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 Request (okhttp3.Request)10 File (java.io.File)9 Response (okhttp3.Response)9 Connection (ch.ethz.ssh2.Connection)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 MediaType (okhttp3.MediaType)8 RequestBody (okhttp3.RequestBody)8