Search in sources :

Example 36 with Connection

use of org.ovirt.engine.sdk4.Connection in project pentaho-kettle by pentaho.

the class SSHData method OpenConnection.

public static Connection OpenConnection(String serveur, int port, String username, String password, boolean useKey, String keyFilename, String passPhrase, int timeOut, VariableSpace space, String proxyhost, int proxyport, String proxyusername, String proxypassword) throws KettleException {
    Connection conn = null;
    char[] content = null;
    boolean isAuthenticated = false;
    try {
        // perform some checks
        if (useKey) {
            if (Utils.isEmpty(keyFilename)) {
                throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyFileMissing"));
            }
            FileObject keyFileObject = KettleVFS.getFileObject(keyFilename);
            if (!keyFileObject.exists()) {
                throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyNotExist", keyFilename));
            }
            FileContent keyFileContent = keyFileObject.getContent();
            CharArrayWriter charArrayWriter = new CharArrayWriter((int) keyFileContent.getSize());
            try (InputStream in = keyFileContent.getInputStream()) {
                IOUtils.copy(in, charArrayWriter);
            }
            content = charArrayWriter.toCharArray();
        }
        // Create a new connection
        conn = createConnection(serveur, port);
        /* We want to connect through a HTTP proxy */
        if (!Utils.isEmpty(proxyhost)) {
            // if the proxy requires basic authentication:
            if (!Utils.isEmpty(proxyusername)) {
                conn.setProxyData(new HTTPProxyData(proxyhost, proxyport, proxyusername, proxypassword));
            } else {
                conn.setProxyData(new HTTPProxyData(proxyhost, proxyport));
            }
        }
        // and connect
        if (timeOut == 0) {
            conn.connect();
        } else {
            conn.connect(null, 0, timeOut * 1000);
        }
        // authenticate
        if (useKey) {
            isAuthenticated = conn.authenticateWithPublicKey(username, content, space.environmentSubstitute(passPhrase));
        } else {
            isAuthenticated = conn.authenticateWithPassword(username, password);
        }
        if (isAuthenticated == false) {
            throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.AuthenticationFailed", username));
        }
    } catch (Exception e) {
        // do not forget to disconnect if connected
        if (conn != null) {
            conn.close();
        }
        throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.ErrorConnecting", serveur, username), e);
    }
    return conn;
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) KettleException(org.pentaho.di.core.exception.KettleException) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) FileObject(org.apache.commons.vfs2.FileObject) HTTPProxyData(com.trilead.ssh2.HTTPProxyData) CharArrayWriter(java.io.CharArrayWriter) KettleException(org.pentaho.di.core.exception.KettleException)

Example 37 with Connection

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

the class ConfigTest method executeTest.

@Override
public boolean executeTest() {
    int error = 0;
    Element rootElement = this.getInputFile().get(0).getDocumentElement();
    NodeList commandLst = rootElement.getElementsByTagName("command");
    //Analyze each command, send request and build the array list of api commands
    for (int i = 0; i < commandLst.getLength(); i++) {
        Node fstNode = commandLst.item(i);
        Element fstElmnt = (Element) fstNode;
        //new command
        ApiCommand api = new ApiCommand(fstElmnt, this.getParam(), this.getCommands());
        if (api.getName().equals("rebootManagementServer")) {
            s_logger.info("Attempting to SSH into management server " + this.getParam().get("hostip"));
            try {
                Connection conn = new Connection(this.getParam().get("hostip"));
                conn.connect(null, 60000, 60000);
                s_logger.info("SSHed successfully into management server " + this.getParam().get("hostip"));
                boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
                if (isAuthenticated == false) {
                    s_logger.info("Authentication failed for root with password");
                    return false;
                }
                String restartCommand = "service cloud-management restart; service cloud-usage restart";
                Session sess = conn.openSession();
                s_logger.info("Executing : " + restartCommand);
                sess.execCommand(restartCommand);
                Thread.sleep(120000);
                sess.close();
                conn.close();
            } catch (Exception ex) {
                s_logger.error(ex);
                return false;
            }
        } else {
            //send a command
            api.sendCommand(this.getClient(), null);
            //verify the response of the command
            if ((api.getResponseType() == ResponseType.ERROR) && (api.getResponseCode() == 200) && (api.getTestCaseInfo() != null)) {
                s_logger.error("Test case " + api.getTestCaseInfo() + "failed. Command that was supposed to fail, passed. The command was sent with the following url " + api.getUrl());
                error++;
            } else if ((api.getResponseType() != ResponseType.ERROR) && (api.getResponseCode() == 200)) {
                //set parameters for the future use
                if (api.setParam(this.getParam()) == false) {
                    s_logger.error("Exiting the test...Command " + api.getName() + " didn't return parameters needed for the future use. The command was sent with url " + api.getUrl());
                    return false;
                } else {
                    //verify parameters
                    if (api.verifyParam() == false) {
                        s_logger.error("Command " + api.getName() + " failed. Verification for returned parameters failed. Command was sent with url " + api.getUrl());
                        error++;
                    } else if (api.getTestCaseInfo() != null) {
                        s_logger.info("Test case " + api.getTestCaseInfo() + " passed. Command was sent with the url " + api.getUrl());
                    }
                }
            } else if ((api.getResponseType() != ResponseType.ERROR) && (api.getResponseCode() != 200)) {
                s_logger.error("Command " + api.getName() + " failed with an error code " + api.getResponseCode() + " . Command was sent with url  " + api.getUrl() + " Required: " + api.getRequired());
                if (api.getRequired() == true) {
                    s_logger.info("The command is required for the future use, so exiging");
                    return false;
                }
                error++;
            } else if (api.getTestCaseInfo() != null) {
                s_logger.info("Test case " + api.getTestCaseInfo() + " passed. Command that was supposed to fail, failed - test passed. Command was sent with url " + api.getUrl());
            }
        }
    }
    if (error != 0)
        return false;
    else
        return true;
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 38 with Connection

use of org.ovirt.engine.sdk4.Connection in project warn-report by saaavsaaa.

the class LinuxExecUtil method connect.

public Connection connect(String host, String user, String password, String keyPath) throws IOException {
    conn = new Connection(host);
    conn.connect();
    File KeyFile = new File(keyPath);
    boolean isAuthenticated = conn.authenticateWithPublicKey(user, KeyFile, password);
    if (isAuthenticated == false) {
        throw new IOException("Authentication failed.");
    }
    return conn;
}
Also used : Connection(ch.ethz.ssh2.Connection)

Example 39 with Connection

use of org.ovirt.engine.sdk4.Connection in project Payara by payara.

the class SSHLauncher method checkPasswordAuth.

/**
 * Check if we can connect using password auth
 * @return true|false
 */
public boolean checkPasswordAuth() {
    boolean status = false;
    Connection c = null;
    try {
        c = new Connection(host, port);
        c.connect();
        if (logger.isLoggable(Level.FINER)) {
            logger.finer("Checking connection...");
        }
        status = c.authenticateWithPassword(userName, password);
        if (status) {
            if (logger.isLoggable(Level.FINER)) {
                logger.finer("Successfully connected to " + userName + "@" + host + " using password authentication");
            }
        }
    } catch (IOException ioe) {
        // logger.printExceptionStackTrace(ioe);
        if (logger.isLoggable(Level.FINER)) {
            ioe.printStackTrace();
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return status;
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException)

Example 40 with Connection

use of org.ovirt.engine.sdk4.Connection in project Payara by payara.

the class SSHLauncher method checkConnection.

/**
 * Check if we can authenticate using public key auth
 * @return true|false
 */
public boolean checkConnection() {
    boolean status = false;
    Connection c = null;
    c = new Connection(host, port);
    try {
        c.connect();
        File f = new File(keyFile);
        if (logger.isLoggable(Level.FINER)) {
            logger.finer("Checking connection...");
        }
        status = c.authenticateWithPublicKey(userName, f, rawKeyPassPhrase);
        if (status) {
            logger.info("Successfully connected to " + userName + "@" + host + " using keyfile " + keyFile);
        }
    } catch (IOException ioe) {
        Throwable t = ioe.getCause();
        if (t != null) {
            String msg = t.getMessage();
            logger.warning("Failed to connect or authenticate: " + msg);
        }
        if (logger.isLoggable(Level.FINER)) {
            logger.log(Level.FINER, "Failed to connect or autheticate: ", ioe);
        }
    } finally {
        c.close();
    }
    return status;
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException) File(java.io.File)

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