Skip to content

Commit

Permalink
added simple javabean to hold values instead of properties
Browse files Browse the repository at this point in the history
use SimpleTable for output
added simple test
added help text
  • Loading branch information
claudio4j committed Aug 15, 2014
1 parent fa019cc commit 18e143f
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,107 +24,75 @@

import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.jboss.as.cli.CommandArgument;
import org.jboss.as.cli.CommandContext;
import org.jboss.as.cli.CommandFormatException;
import org.jboss.as.cli.CommandHandler;
import org.jboss.as.cli.CommandLineException;
import org.jboss.as.cli.impl.ConnectionInfoBean;
import org.jboss.as.cli.util.FingerprintGenerator;
import org.jboss.as.cli.util.SimpleTable;
import org.jboss.as.controller.client.ModelControllerClient;

/**
*
* @author Claudio Miranda
*/
public class ConnectionInfoHandler implements CommandHandler {
public class ConnectionInfoHandler extends CommandHandlerWithHelp {

public static final ConnectionInfoHandler INSTANCE = new ConnectionInfoHandler();

/* (non-Javadoc)
* @see org.jboss.as.cli.CommandHandler#isAvailable(org.jboss.as.cli.CommandContext)
*/
@Override
public boolean isAvailable(CommandContext ctx) {
return true;
public ConnectionInfoHandler() {
this("connection-info");
}

/* (non-Javadoc)
* @see org.jboss.as.cli.CommandHandler#isBatchMode()
*/
@Override
public boolean isBatchMode(CommandContext ctx) {
return true;
public ConnectionInfoHandler(String command) {
super(command);
}


/* (non-Javadoc)
* @see org.jboss.as.cli.CommandHandler#handle(org.jboss.as.cli.CommandContext)
*/
@Override
public void handle(CommandContext ctx) throws CommandFormatException {
final StringBuilder buf = new StringBuilder();
protected void doHandle(CommandContext ctx) throws CommandLineException {
final ModelControllerClient client = ctx.getModelControllerClient();
if(client == null) {
buf.append("<connect to the controller and re-run the connection-info command to see the connection information>\n");
ctx.printLine("<connect to the controller and re-run the connection-info command to see the connection information>\n");
} else {

boolean disableLocalAuth = (boolean) ctx.get("disableLocalAuth");
ConnectionInfoBean connInfo = (ConnectionInfoBean) ctx.get("connection_info");
boolean disableLocalAuth = connInfo.isDisableLocalAuth();
String username = "Local connection authenticated as SuperUser";
if (disableLocalAuth)
username = (String) ctx.get("username");
Date _loggedSince = (Date) ctx.get("logged_since");
buf.append("Username - ").append(username).append('\n');
buf.append("Logged since - ").append(_loggedSince).append('\n');
X509Certificate[] lastChain = (X509Certificate[]) ctx.get("server_certificate");
username = connInfo.getUsername();
SimpleTable st = new SimpleTable(2);
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 (Certificate current : lastChain) {
if (current instanceof X509Certificate) {
X509Certificate x509Current = (X509Certificate) current;
Map<String, String> fingerprints = FingerprintGenerator.generateFingerprints(x509Current);
buf.append("Subject - " + x509Current.getSubjectX500Principal().getName()).append("\n");
buf.append("Issuer - " + x509Current.getIssuerDN().getName()).append("\n");
buf.append("Valid From - " + x509Current.getNotBefore()).append("\n");
buf.append("Valid To - " + x509Current.getNotAfter()).append("\n");
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()) {
String algName = String.format("%-13s", alg);
buf.append(algName + "- " + fingerprints.get(alg)).append("\n");
st.addLine(new String[] {alg, fingerprints.get(alg)});
}
buf.append("");
}
}
} catch (CommandLineException cle) {
throw new CommandFormatException("Error trying to generate server certificate fingerprint.", cle);
}
} else {
buf.append("Not an SSL connection.");
st.addLine(new String[] {"Not an SSL connection.", ""});
}
ctx.printLine(st.toString());
}
ctx.printLine(buf.toString());
}

@Override
public CommandArgument getArgument(CommandContext ctx, String name) {
return null;
}

@Override
public boolean hasArgument(CommandContext ctx, String name) {
return false;
}

@Override
public boolean hasArgument(CommandContext ctx, int index) {
return false;
}

@Override
public List<CommandArgument> getArguments(CommandContext ctx) {
return Collections.emptyList();
}
}
14 changes: 10 additions & 4 deletions cli/src/main/java/org/jboss/as/cli/impl/CommandContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,13 @@ class CommandContextImpl implements CommandContext, ModelControllerClientFactory

private CliShutdownHook.Handler shutdownHook;

<<<<<<< HEAD
/** command line handling redirection */
private CommandLineRedirectionRegistration redirection;
=======
/** this object saves information to be used in ConnectionInfoHandler */
private final ConnectionInfoBean connInfoBean = new ConnectionInfoBean();
>>>>>>> added simple javabean to hold values instead of properties

/**
* Version mode - only used when --version is called from the command line.
Expand Down Expand Up @@ -839,7 +844,7 @@ public void connectController(String controller) throws CommandLineException {
do {
try {
CallbackHandler cbh = new AuthenticationCallbackHandler(username, password);
set("disableLocalAuth", disableLocalAuth);
connInfoBean.setDisableLocalAuth(disableLocalAuth);
if (log.isDebugEnabled()) {
log.debug("connecting to " + address.getHost() + ':' + address.getPort() + " as " + username);
}
Expand All @@ -848,7 +853,8 @@ public void connectController(String controller) throws CommandLineException {
retry = false;
tryConnection(tempClient, address);
initNewClient(tempClient, address);
set("logged_since", new Date());
connInfoBean.setLoggedSince(new Date());
set("connection_info", connInfoBean);
} catch (RedirectException re) {
try {
URI location = new URI(re.getLocation());
Expand Down Expand Up @@ -1441,7 +1447,7 @@ private void dohandle(Callback[] callbacks) throws IOException, UnsupportedCallb
throw new SaslException("No username supplied.");
}
}
set("username", username);
connInfoBean.setUsername(username);
ncb.setName(username);
} else if (current instanceof PasswordCallback && digest == null) {
// If a digest had been set support for PasswordCallback is disabled.
Expand Down Expand Up @@ -1617,7 +1623,7 @@ public void checkServerTrusted(final X509Certificate[] chain, String authType) t
retry = false;
try {
getDelegate().checkServerTrusted(chain, authType);
set("server_certificate", chain);
connInfoBean.setServerCertificates(chain);
} catch (CertificateException ce) {
if (retry == false) {
timeoutHandler.suspendAndExecute(new Runnable() {
Expand Down
66 changes: 66 additions & 0 deletions cli/src/main/java/org/jboss/as/cli/impl/ConnectionInfoBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.cli.impl;

import java.security.cert.X509Certificate;
import java.util.Date;

public class ConnectionInfoBean {

private boolean disableLocalAuth;
private String username;
private Date loggedSince;
private X509Certificate[] serverCertificates;

public boolean isDisableLocalAuth() {
return disableLocalAuth;
}

public void setDisableLocalAuth(boolean disableLocalAuth) {
this.disableLocalAuth = disableLocalAuth;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public Date getLoggedSince() {
return loggedSince;
}

public void setLoggedSince(Date loggedSince) {
this.loggedSince = loggedSince;
}

public X509Certificate[] getServerCertificates() {
return serverCertificates;
}

public void setServerCertificates(X509Certificate[] serverCertificates) {
this.serverCertificates = serverCertificates;
}

}
15 changes: 15 additions & 0 deletions cli/src/main/resources/help/connection-info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SYNOPSIS

connection-info

DESCRIPTION

Prints information about the current connection to the server. The
information is username and date and hour since the user is logged in.
If it is an SSL connection it also prints the server certificate as:
subject, issuer, validity, SHA1 and MD5 fingerprint.


ARGUMENTS

n/a
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.jboss.as.cli.CliConfig;
import org.jboss.as.cli.CliEventListener;
Expand All @@ -35,6 +37,10 @@
import org.jboss.as.cli.ControllerAddress;
import org.jboss.as.cli.batch.BatchManager;
import org.jboss.as.cli.batch.BatchedCommand;
<<<<<<< HEAD
=======
import org.jboss.as.cli.impl.ConnectionInfoBean;
>>>>>>> added simple javabean to hold values instead of properties
import org.jboss.as.cli.operation.CommandLineParser;
import org.jboss.as.cli.operation.NodePathFormatter;
import org.jboss.as.cli.operation.OperationCandidatesProvider;
Expand Down Expand Up @@ -67,9 +73,16 @@ public class MockCommandContext implements CommandContext {

private File curDir = new File("");
private boolean resolveParameterValues;
private Map<String, Object> map = new HashMap<String, Object>();

private boolean silent;

public MockCommandContext() {
ConnectionInfoBean connInfo = new ConnectionInfoBean();
connInfo.setUsername("test");
set("connection_info", connInfo);
}

public void parseCommandLine(String buffer) throws CommandFormatException {
try {
parsedCmd.parse(prefix, buffer);
Expand Down Expand Up @@ -121,17 +134,15 @@ public void terminateSession() {
*/
@Override
public void set(String key, Object value) {
// TODO Auto-generated method stub

map.put(key, value);
}

/* (non-Javadoc)
* @see org.jboss.as.cli.CommandContext#get(java.lang.String)
*/
@Override
public Object get(String key) {
// TODO Auto-generated method stub
return null;
return map.get(key);
}

/* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.cli.completion.operation.test;

import static org.junit.Assert.assertNotNull;

import org.jboss.as.cli.completion.mock.MockCommandContext;
import org.jboss.as.cli.impl.ConnectionInfoBean;
import org.junit.Test;

/**
*
* @author Claudio Miranda
*/
public class ConnectionInfoHandlerTestCase {

private MockCommandContext ctx;

public ConnectionInfoHandlerTestCase() {
ctx = new MockCommandContext();
}

@Test
public void testConnected() {
// if the username is populated, it is connected to the server.
ConnectionInfoBean connInfo = (ConnectionInfoBean) ctx.get("connection_info");
assertNotNull(connInfo.getUsername());
}

}

0 comments on commit 18e143f

Please sign in to comment.