Search in sources :

Example 86 with Connection

use of org.ovirt.engine.sdk4.Connection in project cachecloud by sohutv.

the class SSHTemplate method getConnection.

/**
     * 获取连接并校验
     * @param ip
     * @param port
     * @param username
     * @param password
     * @return Connection
     * @throws Exception
     */
private Connection getConnection(String ip, int port, String username, String password) throws Exception {
    Connection conn = new Connection(ip, port);
    conn.connect(null, CONNCET_TIMEOUT, CONNCET_TIMEOUT);
    boolean isAuthenticated = conn.authenticateWithPassword(username, password);
    if (isAuthenticated == false) {
        throw new Exception("SSH authentication failed with [ userName: " + username + ", password: " + password + "]");
    }
    return conn;
}
Also used : Connection(ch.ethz.ssh2.Connection) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) SSHException(com.sohu.cache.exception.SSHException)

Example 87 with Connection

use of org.ovirt.engine.sdk4.Connection in project intellij-community by JetBrains.

the class Basic method main.

public static void main(String[] args) {
    String hostname = "127.0.0.1";
    String username = "joe";
    String password = "joespass";
    try {
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* Now connect */
        conn.connect();
        /* Authenticate.
			 * If you get an IOException saying something like
			 * "Authentication method password not supported by the server at this stage."
			 * then please check the FAQ.
			 */
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("uname -a && date && uptime && who");
        System.out.println("Here is some information about the remote host:");
        /* 
			 * This basic example does not handle stderr, which is sometimes dangerous
			 * (please read the FAQ).
			 */
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        while (true) {
            String line = br.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        /* Show exit status, if available (otherwise "null") */
        System.out.println("ExitCode: " + sess.getExitStatus());
        /* Close this session */
        sess.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Session(com.trilead.ssh2.Session)

Example 88 with Connection

use of org.ovirt.engine.sdk4.Connection in project intellij-community by JetBrains.

the class PublicKeyAuthentication method main.

public static void main(String[] args) {
    String hostname = "127.0.0.1";
    String username = "joe";
    // or "~/.ssh/id_dsa"
    File keyfile = new File("~/.ssh/id_rsa");
    // will be ignored if not needed
    String keyfilePass = "joespass";
    try {
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* Now connect */
        conn.connect();
        /* Authenticate */
        boolean isAuthenticated = conn.authenticateWithPublicKey(username, keyfile, keyfilePass);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("uname -a && date && uptime && who");
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        System.out.println("Here is some information about the remote host:");
        while (true) {
            String line = br.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        /* Close this session */
        sess.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 89 with Connection

use of org.ovirt.engine.sdk4.Connection in project intellij-community by JetBrains.

the class UsingKnownHosts method main.

public static void main(String[] args) throws IOException {
    String hostname = "somehost";
    String username = "joe";
    String password = "joespass";
    File knownHosts = new File("~/.ssh/known_hosts");
    try {
        if (knownHosts.exists())
            database.addHostkeys(knownHosts);
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* Now connect and use the SimpleVerifier */
        conn.connect(new SimpleVerifier(database));
        /* Authenticate */
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("uname -a && date && uptime && who");
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        System.out.println("Here is some information about the remote host:");
        while (true) {
            String line = br.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        /* Close this session */
        sess.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 90 with Connection

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

the class ServerSecurityInterceptor method aroundInvoke.

@AroundInvoke
public Object aroundInvoke(final InvocationContext invocationContext) throws Exception {
    Principal desiredUser = null;
    RealmUser connectionUser = null;
    Map<String, Object> contextData = invocationContext.getContextData();
    if (contextData.containsKey(DELEGATED_USER_KEY)) {
        desiredUser = new SimplePrincipal((String) contextData.get(DELEGATED_USER_KEY));
        Connection con = RemotingContext.getConnection();
        if (con != null) {
            SecurityIdentity localIdentity = con.getLocalIdentity();
            if (localIdentity != null) {
                connectionUser = new RealmUser(localIdentity.getPrincipal().getName());
            }
        } else {
            throw new IllegalStateException("Delegation user requested but no user on connection found.");
        }
    }
    SecurityContext cachedSecurityContext = null;
    boolean contextSet = false;
    try {
        if (desiredUser != null && connectionUser != null && (desiredUser.getName().equals(connectionUser.getName()) == false)) {
            try {
                // The final part of this check is to verify that the change does actually indicate a change in user.
                // We have been requested to switch user and have successfully identified the user from the connection
                // so now we attempt the switch.
                cachedSecurityContext = SecurityContextAssociation.getSecurityContext();
                final SecurityContext nextContext = SecurityContextFactory.createSecurityContext(desiredUser, new CurrentUserCredential(connectionUser.getName()), new Subject(), "fooSecurityDomain");
                SecurityContextAssociation.setSecurityContext(nextContext);
                // keep track that we switched the security context
                contextSet = true;
                RemotingContext.clear();
            } catch (Exception e) {
                LOGGER.error("Failed to switch security context for user", e);
                // Don't propagate the exception stacktrace back to the client for security reasons
                throw new EJBAccessException("Unable to attempt switching of user.");
            }
        }
        return invocationContext.proceed();
    } finally {
        // switch back to original security context
        if (contextSet) {
            SecurityContextAssociation.setSecurityContext(cachedSecurityContext);
        }
    }
}
Also used : IllegalStateException(javax.resource.spi.IllegalStateException) RealmUser(org.jboss.as.core.security.RealmUser) Connection(org.jboss.remoting3.Connection) Subject(javax.security.auth.Subject) EJBAccessException(javax.ejb.EJBAccessException) IllegalStateException(javax.resource.spi.IllegalStateException) EJBAccessException(javax.ejb.EJBAccessException) SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) SecurityContext(org.jboss.security.SecurityContext) Principal(java.security.Principal) SimplePrincipal(org.jboss.security.SimplePrincipal) SimplePrincipal(org.jboss.security.SimplePrincipal) AroundInvoke(javax.interceptor.AroundInvoke)

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