Search in sources :

Example 1 with ConnectionInfo

use of org.jboss.as.cli.ConnectionInfo in project wildfly-core by wildfly.

the class ConnectionInfoHandlerTestCase method testConnected.

@Test
public void testConnected() {
    // if the username is populated, it is connected to the server.
    ConnectionInfo connInfo = ctx.getConnectionInfo();
    assertNotNull(connInfo.getUsername());
}
Also used : ConnectionInfo(org.jboss.as.cli.ConnectionInfo) Test(org.junit.Test)

Example 2 with ConnectionInfo

use of org.jboss.as.cli.ConnectionInfo in project wildfly-core by wildfly.

the class ConnectionInfoHandlerTestCase method testDisconnected.

@Test
public void testDisconnected() {
    ctx.disconnectController();
    ConnectionInfo connInfo = ctx.getConnectionInfo();
    assertNull(connInfo);
}
Also used : ConnectionInfo(org.jboss.as.cli.ConnectionInfo) Test(org.junit.Test)

Example 3 with ConnectionInfo

use of org.jboss.as.cli.ConnectionInfo in project wildfly-core by wildfly.

the class ConnectionInfoHandler method doHandle.

/* (non-Javadoc)
     * @see org.jboss.as.cli.CommandHandler#doHandle(org.jboss.as.cli.CommandContext)
     */
@Override
protected void doHandle(CommandContext ctx) throws CommandLineException {
    final ModelControllerClient client = ctx.getModelControllerClient();
    if (client == null) {
        ctx.printLine("<connect to the controller and re-run the connection-info command to see the connection information>\n");
    } else {
        ConnectionInfo connInfo = ctx.getConnectionInfo();
        String username = null;
        final ModelNode req = new ModelNode();
        req.get(Util.OPERATION).set("whoami");
        req.get(Util.ADDRESS).setEmptyList();
        req.get("verbose").set(true);
        try {
            final ModelNode response = client.execute(req);
            if (Util.isSuccess(response)) {
                if (response.hasDefined(Util.RESULT)) {
                    final ModelNode result = response.get(Util.RESULT);
                    if (result.hasDefined("identity")) {
                        username = result.get("identity").get("username").asString();
                    }
                    if (result.hasDefined("mapped-roles")) {
                        String strRoles = result.get("mapped-roles").asString();
                        String grantedStr = "granted role";
                        // a comma is contained in the string if there is more than one role
                        if (strRoles.indexOf(',') > 0)
                            grantedStr = "granted roles";
                        username = username + ", " + grantedStr + " " + strRoles;
                    } else {
                        username = username + " has no role associated.";
                    }
                } else {
                    username = "result was not available.";
                }
            } else {
                ctx.printLine(Util.getFailureDescription(response));
            }
        } catch (IOException e) {
            throw new CommandFormatException("Failed to get the AS release info: " + e.getLocalizedMessage());
        }
        SimpleTable st = new SimpleTable(2, ctx.getTerminalWidth());
        st.addLine(new String[] { "Username", username });
        st.addLine(new String[] { "Logged since", connInfo.getLoggedSince().toString() });
        X509Certificate[] lastChain = connInfo.getServerCertificates();
        boolean sslConn = lastChain != null;
        if (sslConn) {
            try {
                for (X509Certificate x509Current : lastChain) {
                    Map<String, String> fingerprints = FingerprintGenerator.generateFingerprints(x509Current);
                    st.addLine(new String[] { "Subject", x509Current.getSubjectX500Principal().getName() });
                    st.addLine(new String[] { "Issuer", x509Current.getIssuerDN().getName() });
                    st.addLine(new String[] { "Valid from", x509Current.getNotBefore().toString() });
                    st.addLine(new String[] { "Valid to", x509Current.getNotAfter().toString() });
                    for (String alg : fingerprints.keySet()) {
                        st.addLine(new String[] { alg, fingerprints.get(alg) });
                    }
                }
            } catch (CommandLineException cle) {
                throw new CommandFormatException("Error trying to generate server certificate fingerprint.", cle);
            }
        } else {
            st.addLine(new String[] { "Not an SSL connection.", "" });
        }
        ctx.printLine(st.toString());
    }
}
Also used : ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) CommandFormatException(org.jboss.as.cli.CommandFormatException) SimpleTable(org.jboss.as.cli.util.SimpleTable) ConnectionInfo(org.jboss.as.cli.ConnectionInfo) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) X509Certificate(java.security.cert.X509Certificate) CommandLineException(org.jboss.as.cli.CommandLineException)

Aggregations

ConnectionInfo (org.jboss.as.cli.ConnectionInfo)3 Test (org.junit.Test)2 IOException (java.io.IOException)1 X509Certificate (java.security.cert.X509Certificate)1 CommandFormatException (org.jboss.as.cli.CommandFormatException)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 SimpleTable (org.jboss.as.cli.util.SimpleTable)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1 ModelNode (org.jboss.dmr.ModelNode)1