You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2015/11/26 06:33:05 UTC

[04/10] mina-sshd git commit: Added more informative log messages to some classes

Added more informative log messages to some classes


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/1ceea031
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/1ceea031
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/1ceea031

Branch: refs/heads/master
Commit: 1ceea031f4dce7ed0da94470da3add619168855b
Parents: e9dd7f4
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Thu Nov 26 07:20:26 2015 +0200
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Thu Nov 26 07:20:26 2015 +0200

----------------------------------------------------------------------
 .../apache/sshd/agent/SshAgentConstants.java    | 26 +++++++++
 .../sshd/agent/common/AbstractAgentClient.java  | 60 ++++++++++----------
 .../org/apache/sshd/agent/local/AgentImpl.java  | 11 +++-
 .../sshd/agent/unix/AgentServerProxy.java       |  4 +-
 .../apache/sshd/common/io/nio2/Nio2Session.java |  9 ++-
 .../session/AbstractConnectionService.java      |  4 +-
 .../apache/sshd/server/command/ScpCommand.java  |  5 +-
 .../server/subsystem/sftp/SftpSubsystem.java    |  4 +-
 8 files changed, 85 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/1ceea031/sshd-core/src/main/java/org/apache/sshd/agent/SshAgentConstants.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/SshAgentConstants.java b/sshd-core/src/main/java/org/apache/sshd/agent/SshAgentConstants.java
index e91554d..3587f51 100644
--- a/sshd-core/src/main/java/org/apache/sshd/agent/SshAgentConstants.java
+++ b/sshd-core/src/main/java/org/apache/sshd/agent/SshAgentConstants.java
@@ -18,6 +18,11 @@
  */
 package org.apache.sshd.agent;
 
+import java.util.Map;
+
+import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.logging.LoggingUtils;
+
 public final class SshAgentConstants {
 
     public static final byte SSH_AGENT_SUCCESS = 6;
@@ -33,4 +38,25 @@ public final class SshAgentConstants {
     private SshAgentConstants() {
     }
 
+    private static class LazyMessagesMapHolder {
+        private static final Map<Integer, String> MESSAGES_MAP =
+                LoggingUtils.generateMnemonicMap(SshAgentConstants.class, "SSH2_AGENT");
+    }
+
+    /**
+     * Converts a command value to a user-friendly name
+     *
+     * @param cmd The command value
+     * @return The user-friendly name - if not one of the defined {@code SSH2_AGENT}
+     * values then returns the string representation of the command's value
+     */
+    public static String getCommandMessageName(int cmd) {
+        @SuppressWarnings("synthetic-access")
+        String name = LazyMessagesMapHolder.MESSAGES_MAP.get(cmd);
+        if (GenericUtils.isEmpty(name)) {
+            return Integer.toString(cmd);
+        } else {
+            return name;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/1ceea031/sshd-core/src/main/java/org/apache/sshd/agent/common/AbstractAgentClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/common/AbstractAgentClient.java b/sshd-core/src/main/java/org/apache/sshd/agent/common/AbstractAgentClient.java
index 4c10fe1..d000510 100644
--- a/sshd-core/src/main/java/org/apache/sshd/agent/common/AbstractAgentClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/agent/common/AbstractAgentClient.java
@@ -24,6 +24,7 @@ import java.security.PublicKey;
 import java.util.List;
 
 import org.apache.sshd.agent.SshAgent;
+import org.apache.sshd.agent.SshAgentConstants;
 import org.apache.sshd.common.config.keys.KeyUtils;
 import org.apache.sshd.common.util.Pair;
 import org.apache.sshd.common.util.ValidateUtils;
@@ -32,16 +33,6 @@ import org.apache.sshd.common.util.buffer.BufferUtils;
 import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
 import org.apache.sshd.common.util.logging.AbstractLoggingBean;
 
-import static org.apache.sshd.agent.SshAgentConstants.SSH2_AGENTC_ADD_IDENTITY;
-import static org.apache.sshd.agent.SshAgentConstants.SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
-import static org.apache.sshd.agent.SshAgentConstants.SSH2_AGENTC_REMOVE_IDENTITY;
-import static org.apache.sshd.agent.SshAgentConstants.SSH2_AGENTC_REQUEST_IDENTITIES;
-import static org.apache.sshd.agent.SshAgentConstants.SSH2_AGENTC_SIGN_REQUEST;
-import static org.apache.sshd.agent.SshAgentConstants.SSH2_AGENT_FAILURE;
-import static org.apache.sshd.agent.SshAgentConstants.SSH2_AGENT_IDENTITIES_ANSWER;
-import static org.apache.sshd.agent.SshAgentConstants.SSH2_AGENT_SIGN_RESPONSE;
-import static org.apache.sshd.agent.SshAgentConstants.SSH_AGENT_SUCCESS;
-
 public abstract class AbstractAgentClient extends AbstractLoggingBean {
 
     private final Buffer buffer = new ByteArrayBuffer();
@@ -57,7 +48,7 @@ public abstract class AbstractAgentClient extends AbstractLoggingBean {
         if (avail < 4) {
             if (log.isTraceEnabled()) {
                 log.trace("Received message total length ({}) below minuimum ({})",
-                          Integer.valueOf(avail), Integer.valueOf(4)); 
+                          Integer.valueOf(avail), Integer.valueOf(4));
             }
             return;
         }
@@ -65,12 +56,12 @@ public abstract class AbstractAgentClient extends AbstractLoggingBean {
         int rpos = buffer.rpos();
         int len = buffer.getInt();
         buffer.rpos(rpos);
-        
+
         avail = buffer.available();
         if (avail < (len + 4)) {
             if (log.isTraceEnabled()) {
                 log.trace("Received request length ({}) below minuimum ({})",
-                          Integer.valueOf(avail), Integer.valueOf(len + 4)); 
+                          Integer.valueOf(avail), Integer.valueOf(len + 4));
             }
             return;
         }
@@ -79,7 +70,7 @@ public abstract class AbstractAgentClient extends AbstractLoggingBean {
         Buffer rep = BufferUtils.clear(message);
         rep.putInt(0);
         rep.rpos(rep.wpos());
-        
+
         Buffer req = new ByteArrayBuffer(buffer.getBytes());
         int cmd = -1;
         try {
@@ -94,26 +85,29 @@ public abstract class AbstractAgentClient extends AbstractLoggingBean {
             rep.putInt(0);
             rep.rpos(rep.wpos());
             rep.putInt(1);
-            rep.putByte(SSH2_AGENT_FAILURE);
+            rep.putByte(SshAgentConstants.SSH2_AGENT_FAILURE);
         }
         reply(prepare(rep));
     }
 
     protected void process(int cmd, Buffer req, Buffer rep) throws Exception {
         if (log.isDebugEnabled()) {
-            log.debug("process(cmd={})", Integer.valueOf(cmd));
+            log.debug("process(cmd={})", SshAgentConstants.getCommandMessageName(cmd));
         }
         switch (cmd) {
-            case SSH2_AGENTC_REQUEST_IDENTITIES:
+            case SshAgentConstants.SSH2_AGENTC_REQUEST_IDENTITIES:
+            {
                 List<Pair<PublicKey, String>> keys = agent.getIdentities();
-                rep.putByte(SSH2_AGENT_IDENTITIES_ANSWER);
+                rep.putByte(SshAgentConstants.SSH2_AGENT_IDENTITIES_ANSWER);
                 rep.putInt(keys.size());
                 for (Pair<PublicKey, String> key : keys) {
                     rep.putPublicKey(key.getFirst());
                     rep.putString(key.getSecond());
                 }
                 break;
-            case SSH2_AGENTC_SIGN_REQUEST:
+            }
+            case SshAgentConstants.SSH2_AGENTC_SIGN_REQUEST:
+            {
                 PublicKey signingKey = req.getPublicKey();
                 byte[] data = req.getBytes();
                 int flags = req.getInt();
@@ -125,34 +119,40 @@ public abstract class AbstractAgentClient extends AbstractLoggingBean {
                         KeyUtils.getKeyType(signingKey),
                         "Cannot resolve key type of %s",
                         signingKey.getClass().getSimpleName());
-                Buffer sig = new ByteArrayBuffer();
+                byte[] signature = agent.sign(signingKey, data);
+                Buffer sig = new ByteArrayBuffer(keyType.length() + signature.length + Long.SIZE);
                 sig.putString(keyType);
-                sig.putBytes(agent.sign(signingKey, data));
-                rep.putByte(SSH2_AGENT_SIGN_RESPONSE);
+                sig.putBytes(signature);
+                rep.putByte(SshAgentConstants.SSH2_AGENT_SIGN_RESPONSE);
                 rep.putBytes(sig.array(), sig.rpos(), sig.available());
                 break;
-            case SSH2_AGENTC_ADD_IDENTITY:
+            }
+            case SshAgentConstants.SSH2_AGENTC_ADD_IDENTITY:
+            {
                 KeyPair keyToAdd = req.getKeyPair();
                 String comment = req.getString();
                 log.debug("SSH2_AGENTC_ADD_IDENTITY comment={}", comment);
                 agent.addIdentity(keyToAdd, comment);
-                rep.putByte(SSH_AGENT_SUCCESS);
+                rep.putByte(SshAgentConstants.SSH_AGENT_SUCCESS);
                 break;
-            case SSH2_AGENTC_REMOVE_IDENTITY:
+            }
+            case SshAgentConstants.SSH2_AGENTC_REMOVE_IDENTITY:
+            {
                 PublicKey keyToRemove = req.getPublicKey();
                 log.debug("SSH2_AGENTC_REMOVE_IDENTITY {}", keyToRemove.getClass().getSimpleName());
                 agent.removeIdentity(keyToRemove);
-                rep.putByte(SSH_AGENT_SUCCESS);
+                rep.putByte(SshAgentConstants.SSH_AGENT_SUCCESS);
                 break;
-            case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
+            }
+            case SshAgentConstants.SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
                 agent.removeAllIdentities();
-                rep.putByte(SSH_AGENT_SUCCESS);
+                rep.putByte(SshAgentConstants.SSH_AGENT_SUCCESS);
                 break;
             default:
                 if (log.isDebugEnabled()) {
-                    log.debug("Unknown command: {}", Integer.valueOf(cmd));
+                    log.debug("Unknown command: {}", SshAgentConstants.getCommandMessageName(cmd));
                 }
-                rep.putByte(SSH2_AGENT_FAILURE);
+                rep.putByte(SshAgentConstants.SSH2_AGENT_FAILURE);
                 break;
         }
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/1ceea031/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentImpl.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentImpl.java b/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentImpl.java
index b99bd3a..cb562a6 100644
--- a/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentImpl.java
+++ b/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentImpl.java
@@ -25,6 +25,7 @@ import java.security.interfaces.DSAPublicKey;
 import java.security.interfaces.ECPublicKey;
 import java.security.interfaces.RSAPublicKey;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -33,6 +34,7 @@ import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.config.keys.KeyUtils;
 import org.apache.sshd.common.signature.BuiltinSignatures;
 import org.apache.sshd.common.signature.Signature;
+import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.Pair;
 import org.apache.sshd.common.util.ValidateUtils;
 
@@ -103,7 +105,7 @@ public class AgentImpl implements SshAgent {
         if (!isOpen()) {
             throw new SshException("Agent closed");
         }
-        keys.add(new Pair<>(key, comment));
+        keys.add(new Pair<>(ValidateUtils.checkNotNull(key, "No key"), comment));
     }
 
     @Override
@@ -111,6 +113,7 @@ public class AgentImpl implements SshAgent {
         if (!isOpen()) {
             throw new SshException("Agent closed");
         }
+
         Pair<KeyPair, String> kp = getKeyPair(keys, key);
         if (kp == null) {
             throw new SshException("Key not found");
@@ -133,7 +136,11 @@ public class AgentImpl implements SshAgent {
         }
     }
 
-    protected static Pair<KeyPair, String> getKeyPair(List<Pair<KeyPair, String>> keys, PublicKey key) {
+    protected static Pair<KeyPair, String> getKeyPair(Collection<Pair<KeyPair, String>> keys, PublicKey key) {
+        if (GenericUtils.isEmpty(keys) || (key == null)) {
+            return null;
+        }
+
         for (Pair<KeyPair, String> k : keys) {
             KeyPair kp = k.getFirst();
             if (KeyUtils.compareKeys(key, kp.getPublic())) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/1ceea031/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServerProxy.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServerProxy.java b/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServerProxy.java
index 05e9ccf..5c1e9af 100644
--- a/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServerProxy.java
+++ b/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServerProxy.java
@@ -111,7 +111,9 @@ public class AgentServerProxy extends AbstractLoggingBean implements SshAgentSer
                                 channel.open().verify(PropertyResolverUtils.getLongProperty(session, CHANNEL_OPEN_TIMEOUT_PROP, DEFAULT_CHANNEL_OPEN_TIMEOUT));
                             } catch (Exception e) {
                                 if (isOpen()) {
-                                    log.info(e.getClass().getSimpleName() + " while authentication forwarding: " + e.getMessage(), e);
+                                    if (log.isDebugEnabled()) {
+                                        log.debug(e.getClass().getSimpleName() + " while authentication forwarding: " + e.getMessage(), e);
+                                    }
                                 }
                             }
                         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/1ceea031/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
index d37403d..5629d63 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
@@ -133,10 +133,15 @@ public class Nio2Session extends AbstractCloseable implements IoSession {
                 close(true);
             } else {
                 try {
-                    log.debug("Caught exception, now calling handler");
+                    if (log.isDebugEnabled()) {
+                        log.debug("Caught {}[{}] - calling handler", exc.getClass().getSimpleName(), exc.getMessage());
+                    }
                     handler.exceptionCaught(this, exc);
                 } catch (Throwable t) {
-                    log.info("Exception handler threw exception, closing the session", t);
+                    if (log.isDebugEnabled()) {
+                        log.debug("Exception handler threw {}, closing the session: {}",
+                                  t.getClass().getSimpleName(), t.getMessage());
+                    }
                     close(true);
                 }
             }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/1ceea031/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
index 1e51b6f..8e4943a 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
@@ -271,7 +271,9 @@ public abstract class AbstractConnectionService extends AbstractInnerCloseable i
             Channel channel = getChannel(buffer);
             channel.handleWindowAdjust(buffer);
         } catch (SshException e) {
-            log.info("channelWindowAdjust error: {}", e.getMessage());
+            if (log.isDebugEnabled()) {
+                log.debug("channelWindowAdjust {} error: {}", e.getClass().getSimpleName(), e.getMessage());
+            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/1ceea031/sshd-core/src/main/java/org/apache/sshd/server/command/ScpCommand.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/command/ScpCommand.java b/sshd-core/src/main/java/org/apache/sshd/server/command/ScpCommand.java
index 424de53..549a767 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/command/ScpCommand.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/command/ScpCommand.java
@@ -245,7 +245,10 @@ public class ScpCommand extends AbstractLoggingBean implements Command, Runnable
             } catch (IOException e2) {
                 // Ignore
             }
-            log.info("Error in scp command=" + name, e);
+
+            if (log.isDebugEnabled()) {
+                log.debug("Error ({}) in scp command={}: {}", e.getClass().getSimpleName(), name, e.getMessage());
+            }
         } finally {
             if (callback != null) {
                 callback.onExit(exitValue, GenericUtils.trimToEmpty(exitMessage));

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/1ceea031/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
index 6c739d2..5aa6d5f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
@@ -562,7 +562,9 @@ public class SftpSubsystem
                 doSpaceAvailable(buffer, id);
                 break;
             default:
-                log.info("executeExtendedCommand({}) received unsupported SSH_FXP_EXTENDED({})", getServerSession(), extension);
+                if (log.isDebugEnabled()) {
+                    log.debug("executeExtendedCommand({}) received unsupported SSH_FXP_EXTENDED({})", getServerSession(), extension);
+                }
                 sendStatus(BufferUtils.clear(buffer), id, SftpConstants.SSH_FX_OP_UNSUPPORTED, "Command SSH_FXP_EXTENDED(" + extension + ") is unsupported or not implemented");
                 break;
         }