Search in sources :

Example 11 with Connection

use of org.osate.aadl2.Connection in project intellij-community by JetBrains.

the class PortForwarding 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.");
        /* ===== OK, now let's establish some local port forwardings ===== */
        /* Example Port Forwarding: -L 8080:www.icann.org:80 (OpenSSH notation)
			 * 
			 * This works by allocating a socket to listen on 8080 on the local interface (127.0.0.1).
			 * Whenever a connection is made to this port (127.0.0.1:8080), the connection is forwarded
			 * over the secure channel, and a connection is made to www.icann.org:80 from the remote
			 * machine (i.e., the ssh server).
			 * 
			 * (the above text is based partially on the OpenSSH man page)
			 */
        /* You can create as many of them as you want */
        LocalPortForwarder lpf1 = conn.createLocalPortForwarder(8080, "www.icann.org", 80);
        /* Now simply point your webbrowser to 127.0.0.1:8080 */
        /* (on the host where you execute this program)                         */
        /* ===== OK, now let's establish some remote port forwardings ===== */
        /* Example Port Forwarding: -R 127.0.0.1:8080:www.ripe.net:80 (OpenSSH notation)
			 * 
			 * Specifies that the port 127.0.0.1:8080 on the remote server is to be forwarded to the
			 * given host and port on the local side.  This works by allocating a socket to listen to port
			 * 8080 on the remote side (the ssh server), and whenever a connection is made to this port, the
			 * connection is forwarded over the secure channel, and a connection is made to
			 * www.ripe.net:80 by the Trilead SSH-2 library.
			 * 
			 * (the above text is based partially on the OpenSSH man page)
			 */
        /* You can create as many of them as you want */
        conn.requestRemotePortForwarding("127.0.0.1", 8080, "www.ripe.net", 80);
        /* Now, on the ssh server, if you connect to 127.0.0.1:8080, then the connection is forwarded
			 * through the secure tunnel to the library, which in turn will forward the connection
			 * to www.ripe.net:80. */
        /* Sleep a bit... (30 seconds) */
        sleepSomeTime(30000);
        /* Stop accepting remote connections that are being forwarded to www.ripe.net:80 */
        conn.cancelRemotePortForwarding(8080);
        /* Sleep a bit... (20 seconds) */
        sleepSomeTime(20000);
        /* Stop accepting connections on 127.0.0.1:8080 that are being forwarded to www.icann.org:80 */
        lpf1.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : LocalPortForwarder(com.trilead.ssh2.LocalPortForwarder) Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException) File(java.io.File)

Example 12 with Connection

use of org.osate.aadl2.Connection in project intellij-community by JetBrains.

the class SingleThreadStdoutStderr 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 */
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("echo \"Huge amounts of text on STDOUT\"; echo \"Huge amounts of text on STDERR\" >&2");
        /*
			 * Advanced:
			 * The following is a demo on how one can read from stdout and
			 * stderr without having to use two parallel worker threads (i.e.,
			 * we don't use the Streamgobblers here) and at the same time not
			 * risking a deadlock (due to a filled SSH2 channel window, caused
			 * by the stream which you are currently NOT reading from =).
			 */
        /* Don't wrap these streams and don't let other threads work on
			 * these streams while you work with Session.waitForCondition()!!!
			 */
        InputStream stdout = sess.getStdout();
        InputStream stderr = sess.getStderr();
        byte[] buffer = new byte[8192];
        while (true) {
            if ((stdout.available() == 0) && (stderr.available() == 0)) {
                /* Even though currently there is no data available, it may be that new data arrives
					 * and the session's underlying channel is closed before we call waitForCondition().
					 * This means that EOF and STDOUT_DATA (or STDERR_DATA, or both) may
					 * be set together.
					 */
                int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 2000);
                if ((conditions & ChannelCondition.TIMEOUT) != 0) {
                    /* A timeout occured. */
                    throw new IOException("Timeout while waiting for data from peer.");
                }
                if ((conditions & ChannelCondition.EOF) != 0) {
                    if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
                        /* ... and we have consumed all data in the local arrival window. */
                        break;
                    }
                }
            /* OK, either STDOUT_DATA or STDERR_DATA (or both) is set. */
            // You can be paranoid and check that the library is not going nuts:
            // if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0)
            //	throw new IllegalStateException("Unexpected condition result (" + conditions + ")");
            }
            while (stdout.available() > 0) {
                int len = stdout.read(buffer);
                if (// this check is somewhat paranoid
                len > 0)
                    System.out.write(buffer, 0, len);
            }
            while (stderr.available() > 0) {
                int len = stderr.read(buffer);
                if (// this check is somewhat paranoid
                len > 0)
                    System.err.write(buffer, 0, len);
            }
        }
        /* Close this session */
        sess.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException) Session(com.trilead.ssh2.Session)

Example 13 with Connection

use of org.osate.aadl2.Connection in project intellij-community by JetBrains.

the class StdoutAndStderr 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 */
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("echo \"Text on STDOUT\"; echo \"Text on STDERR\" >&2");
        InputStream stdout = new StreamGobbler(sess.getStdout());
        InputStream stderr = new StreamGobbler(sess.getStderr());
        BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
        BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
        System.out.println("Here is the output from stdout:");
        while (true) {
            String line = stdoutReader.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        System.out.println("Here is the output from stderr:");
        while (true) {
            String line = stderrReader.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) 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 14 with Connection

use of org.osate.aadl2.Connection in project wildfly by wildfly.

the class ConnectionSecurityContext method getConnectionPrincipals.

/**
     * Obtain a {@link Collection} containing the {@link Principal} instances for the user associated with the connection.
     *
     * Note: This method should be called from within a {@link PrivilegedAction}.
     *
     * @return The Collection of Principals for the user authenticated with the connection. An empty Collection will be returned
     *         of no user is associated with the connection, {@code null} will be returned if no connection is associated with
     *         the {@link Thread}
     */
public static Collection<Principal> getConnectionPrincipals() {
    Connection con = RemotingContext.getConnection();
    if (con != null) {
        Collection<Principal> principals = new HashSet<>();
        SecurityIdentity localIdentity = con.getLocalIdentity();
        if (localIdentity != null) {
            principals.add(new RealmUser(localIdentity.getPrincipal().getName()));
            StreamSupport.stream(localIdentity.getRoles().spliterator(), true).forEach((String role) -> {
                principals.add(new RealmGroup(role));
                principals.add(new RealmRole(role));
            });
            return principals;
        } else {
            return Collections.emptySet();
        }
    }
    return null;
}
Also used : SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) RealmRole(org.jboss.as.core.security.RealmRole) RealmGroup(org.jboss.as.core.security.RealmGroup) Connection(org.jboss.remoting3.Connection) RealmUser(org.jboss.as.core.security.RealmUser) Principal(java.security.Principal) HashSet(java.util.HashSet)

Example 15 with Connection

use of org.osate.aadl2.Connection in project wildfly by wildfly.

the class ConnectionSecurityContext method pushIdentity.

/**
     * Push a new {@link Principal} and Credential pair.
     *
     * This method is to be called before an EJB invocation is passed through it's security interceptor, at that point the
     * Principal and Credential pair can be verified.
     *
     * Note: This method should be called from within a {@link PrivilegedAction}.
     *
     * @param principal - The alternative {@link Principal} to use in verification before the next EJB is called.
     * @param credential - The credential to verify with the {@linl Principal}
     * @return A {@link ContextStateCache} that can later be used to pop the identity pushed here and restore internal state to it's previous values.
     * @throws Exception If there is a problem associating the new {@link Principal} and Credential pair.
     */
public static ContextStateCache pushIdentity(final Principal principal, final Object credential) throws Exception {
    SecurityContext current = SecurityContextAssociation.getSecurityContext();
    SecurityContext nextContext = SecurityContextFactory.createSecurityContext(principal, credential, new Subject(), "USER_DELEGATION");
    SecurityContextAssociation.setSecurityContext(nextContext);
    Connection con = RemotingContext.getConnection();
    RemotingContext.clear();
    return new ContextStateCache(con, current);
}
Also used : SecurityContext(org.jboss.security.SecurityContext) Connection(org.jboss.remoting3.Connection) Subject(javax.security.auth.Subject)

Aggregations

IOException (java.io.IOException)69 ComponentInstance (org.osate.aadl2.instance.ComponentInstance)65 Connection (com.trilead.ssh2.Connection)64 Connection (org.ovirt.engine.sdk4.Connection)64 Connection (org.osate.aadl2.Connection)58 ConnectionInstance (org.osate.aadl2.instance.ConnectionInstance)52 ArrayList (java.util.ArrayList)37 FeatureInstance (org.osate.aadl2.instance.FeatureInstance)36 Session (com.trilead.ssh2.Session)35 Connection (org.jboss.remoting3.Connection)33 ConnectionInstanceEnd (org.osate.aadl2.instance.ConnectionInstanceEnd)33 Connection (com.google.cloud.bigquery.connection.v1.Connection)31 Test (org.junit.Test)30 ConnectionReference (org.osate.aadl2.instance.ConnectionReference)30 VmsService (org.ovirt.engine.sdk4.services.VmsService)30 Vm (org.ovirt.engine.sdk4.types.Vm)30 InputStream (java.io.InputStream)29 Connection (okhttp3.Connection)29 Subcomponent (org.osate.aadl2.Subcomponent)29 NamedElement (org.osate.aadl2.NamedElement)28