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/07/28 08:30:10 UTC

[1/3] mina-sshd git commit: Fixed some calls to Objects.toString() to invoke Objects.toString(o, null)

Repository: mina-sshd
Updated Branches:
  refs/heads/master 9521dfa27 -> 9dbd66ea9


Fixed some calls to Objects.toString() to invoke Objects.toString(o, null)


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

Branch: refs/heads/master
Commit: a5bf6cabf9a1aaa21e6ee9506f0ba64eeba18953
Parents: 9521dfa
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Tue Jul 28 08:18:18 2015 +0300
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Tue Jul 28 08:18:18 2015 +0300

----------------------------------------------------------------------
 .../AbstractResourceKeyPairProvider.java        |  9 ++--
 .../apache/sshd/common/util/Transformer.java    |  5 +-
 .../apache/sshd/common/util/buffer/Buffer.java  |  9 ++--
 .../sshd/server/subsystem/sftp/SftpHelper.java  |  6 +--
 .../server/subsystem/sftp/SftpSubsystem.java    | 48 ++++++++++----------
 .../sshd/common/cipher/BuiltinCiphersTest.java  |  2 +-
 6 files changed, 41 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5bf6cab/sshd-core/src/main/java/org/apache/sshd/common/keyprovider/AbstractResourceKeyPairProvider.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/keyprovider/AbstractResourceKeyPairProvider.java b/sshd-core/src/main/java/org/apache/sshd/common/keyprovider/AbstractResourceKeyPairProvider.java
index ee65334..259b6bc 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/keyprovider/AbstractResourceKeyPairProvider.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/keyprovider/AbstractResourceKeyPairProvider.java
@@ -34,6 +34,7 @@ import java.util.TreeSet;
 
 import org.apache.sshd.common.config.keys.FilePasswordProvider;
 import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.ValidateUtils;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
@@ -41,10 +42,10 @@ import org.apache.sshd.common.util.GenericUtils;
 public abstract class AbstractResourceKeyPairProvider<R> extends AbstractKeyPairProvider {
 
     private FilePasswordProvider passwordFinder;
-    /* 
+    /*
      * NOTE: the map is case insensitive even for Linux, as it is (very) bad
      * practice to have 2 key files that differ from one another only in their
-     * case... 
+     * case...
      */
     private final Map<String, KeyPair> cacheMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
 
@@ -74,7 +75,7 @@ public abstract class AbstractResourceKeyPairProvider<R> extends AbstractKeyPair
             }
 
             for (Object r : resources) {
-                String resourceKey = Objects.toString(r);
+                String resourceKey = ValidateUtils.checkNotNullAndNotEmpty(Objects.toString(r, null), "No resource key value");
                 if (cacheMap.containsKey(resourceKey)) {
                     continue;
                 }
@@ -114,7 +115,7 @@ public abstract class AbstractResourceKeyPairProvider<R> extends AbstractKeyPair
     }
 
     protected KeyPair doLoadKey(R resource) throws IOException, GeneralSecurityException {
-        String resourceKey = Objects.toString(resource);
+        String resourceKey = ValidateUtils.checkNotNullAndNotEmpty(Objects.toString(resource, null), "No resource string value");
         KeyPair kp;
         synchronized (cacheMap) {
             // check if lucky enough to have already loaded this file

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5bf6cab/sshd-core/src/main/java/org/apache/sshd/common/util/Transformer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/Transformer.java b/sshd-core/src/main/java/org/apache/sshd/common/util/Transformer.java
index 73e2233..96adbc1 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/Transformer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/Transformer.java
@@ -28,12 +28,13 @@ public interface Transformer<I, O> {
     // TODO in JDK-8 replace this with Function
 
     /**
-     * Invokes {@link Objects#toString(Object)} on the argument
+     * Invokes {@link Objects#toString(Object, String)} on the argument
+     * with {@code null} as the value to return if argument is {@code null}
      */
     Transformer<Object, String> TOSTRING = new Transformer<Object, String>() {
         @Override
         public String transform(Object input) {
-            return Objects.toString(input);
+            return Objects.toString(input, null);
         }
     };
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5bf6cab/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java
index 9cf4d92..106d9cd 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java
@@ -58,6 +58,7 @@ import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.Int2IntFunction;
 import org.apache.sshd.common.util.Readable;
 import org.apache.sshd.common.util.SecurityUtils;
+import org.apache.sshd.common.util.Transformer;
 
 /**
  * Provides an abstract message buffer for encoding SSH messages
@@ -493,7 +494,7 @@ public abstract class Buffer implements Readable {
     }
 
     /**
-     * Encodes the {@link Objects#toString(Object)} value of each member.
+     * Encodes the {@link Transformer#TOSTRING} value of each member.
      *
      * @param objects       The objects to be encoded in the buffer - OK if
      *                      {@code null}/empty
@@ -506,7 +507,7 @@ public abstract class Buffer implements Readable {
     }
 
     /**
-     * Encodes the {@link Objects#toString(Object)} value of each member
+     * Encodes the {@link Transformer#TOSTRING} value of each member
      *
      * @param objects       The objects to be encoded in the buffer - OK if
      *                      {@code null}/empty
@@ -526,7 +527,7 @@ public abstract class Buffer implements Readable {
         }
 
         for (Object o : objects) {
-            putString(Objects.toString(o), charset);
+            putString(Transformer.TOSTRING.transform(o), charset);
         }
     }
 
@@ -535,7 +536,7 @@ public abstract class Buffer implements Readable {
     }
 
     public void putString(String string, Charset charset) {
-        putBytes(string.getBytes(charset));
+        putBytes(GenericUtils.isEmpty(string) ? GenericUtils.EMPTY_BYTE_ARRAY : string.getBytes(charset));
     }
 
     public void putMPInt(BigInteger bi) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5bf6cab/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpHelper.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpHelper.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpHelper.java
index 060bb88..502b9d5 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpHelper.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpHelper.java
@@ -126,7 +126,7 @@ import static org.apache.sshd.common.subsystem.sftp.SftpConstants.S_IXUSR;
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
 public final class SftpHelper {
-    
+
     private SftpHelper() {
         throw new UnsupportedOperationException("No instance allowed");
     }
@@ -199,8 +199,8 @@ public final class SftpHelper {
             buffer.putLong(size.longValue());
         }
         if ((flags & SSH_FILEXFER_ATTR_OWNERGROUP) != 0) {
-            buffer.putString(Objects.toString(attributes.get("owner")));
-            buffer.putString(Objects.toString(attributes.get("group")));
+            buffer.putString(Objects.toString(attributes.get("owner"), null));
+            buffer.putString(Objects.toString(attributes.get("group"), null));
         }
         if ((flags & SSH_FILEXFER_ATTR_PERMISSIONS) != 0) {
             buffer.putInt(attributesToPermissions(isReg, isDir, isLnk, perms));

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5bf6cab/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 5d5092f..6d304a4 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
@@ -218,7 +218,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
     /**
      * Force the use of a max. packet length - especially for {@link #doReadDir(Buffer, int)}
      * and {@link #doRead(Buffer, int)} methods
-     * 
+     *
      * @see #DEFAULT_MAX_PACKET_LENGTH
      */
     public static final String MAX_PACKET_LENGTH_PROP = "sftp-max-packet-length";
@@ -393,7 +393,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
     public void setFileSystem(FileSystem fileSystem) {
         if (fileSystem != this.fileSystem) {
             this.fileSystem = fileSystem;
-            
+
             Iterable<Path> roots = ValidateUtils.checkNotNull(fileSystem.getRootDirectories(), "No root directories");
             Iterator<Path> available = ValidateUtils.checkNotNull(roots.iterator(), "No roots iterator");
             ValidateUtils.checkTrue(available.hasNext(), "No available root");
@@ -720,7 +720,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
 
             /*
              * To quote http://tools.ietf.org/wg/secsh/draft-ietf-secsh-filexfer/draft-ietf-secsh-filexfer-09.txt section 9.1.2:
-             * 
+             *
              *       If ACE4_READ_DATA was not included when the file was opened,
              *       the server MUST return STATUS_PERMISSION_DENIED.
              */
@@ -730,10 +730,10 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
             }
         } else {
             path = resolveFile(target);
-            
+
             /*
              * To quote http://tools.ietf.org/wg/secsh/draft-ietf-secsh-filexfer/draft-ietf-secsh-filexfer-09.txt section 9.1.2:
-             * 
+             *
              *      If 'check-file-name' refers to a SSH_FILEXFER_TYPE_SYMLINK, the
              *      target should be opened.
              */
@@ -891,7 +891,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
 
             /*
              * To quote http://tools.ietf.org/wg/secsh/draft-ietf-secsh-filexfer/draft-ietf-secsh-filexfer-09.txt section 9.1.1:
-             * 
+             *
              *      The handle MUST be a file handle, and ACE4_READ_DATA MUST
              *      have been included in the desired-access when the file
              *      was opened
@@ -944,7 +944,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
 
             /*
              * To quote http://tools.ietf.org/wg/secsh/draft-ietf-secsh-filexfer/draft-ietf-secsh-filexfer-09.txt section 9.1.1:
-             * 
+             *
              *      If this is a zero length string, the client does not have the
              *      data, and is requesting the hash for reasons other than comparing
              *      with a local file.  The server MAY return SSH_FX_OP_UNSUPPORTED in
@@ -966,10 +966,10 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
                 if (hashMatches) {
                     /*
                      * Need to re-initialize the digester due to the Javadoc:
-                     * 
+                     *
                      *      "The digest method can be called once for a given number
                      *       of updates. After digest has been called, the MessageDigest
-                     *       object is reset to its initialized state." 
+                     *       object is reset to its initialized state."
                      */
                     if (effectiveLength > 0L) {
                         digest = BuiltinDigests.md5.create();
@@ -1471,10 +1471,10 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
             if (version < SFTP_V6) {
                 /*
                  * See http://www.openssh.com/txt/draft-ietf-secsh-filexfer-02.txt:
-                 * 
+                 *
                  *      The SSH_FXP_REALPATH request can be used to have the server
                  *      canonicalize any given path name to an absolute path.
-                 * 
+                 *
                  * See also SSHD-294
                  */
                 result = doRealPathV345(id, path, options);
@@ -2345,9 +2345,9 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
 
         /*
          * As per the spec:
-         * 
+         *
          *      The server will respond with a SSH_FXP_NAME packet containing only
-         *      one name and a dummy attributes value. 
+         *      one name and a dummy attributes value.
          */
         SftpHelper.writeAttrs(version, buffer, Collections.<String, Object>emptyMap());
         send(buffer);
@@ -2428,7 +2428,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
     private String getLongName(Path f, Map<String, ?> attributes) throws IOException {
         String username;
         if (attributes.containsKey("owner")) {
-            username = Objects.toString(attributes.get("owner"));
+            username = Objects.toString(attributes.get("owner"), null);
         } else {
             username = "owner";
         }
@@ -2441,7 +2441,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
         }
         String group;
         if (attributes.containsKey("group")) {
-            group = Objects.toString(attributes.get("group"));
+            group = Objects.toString(attributes.get("group"), null);
         } else {
             group = "group";
         }
@@ -2480,7 +2480,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
         int  count = nrm.getNameCount();
         /*
          * According to the javadoc:
-         * 
+         *
          *      The number of elements in the path, or 0 if this path only
          *      represents a root component
          */
@@ -2620,11 +2620,11 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
                 if (Objects.equals(resolved, value)) {
                     continue;
                 }
-                
+
                 if (attrs == null) {
                     attrs = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
                 }
-                
+
                 attrs.put(name, resolved);
 
                 if (log.isDebugEnabled()) {
@@ -2635,18 +2635,18 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
                 if (log.isDebugEnabled()) {
                     log.debug("resolveMissingFileAttributes(" + file + ")[" + name + "]"
                             + " failed (" + e.getClass().getSimpleName() + ")"
-                            + " to resolve missing value: " + e.getMessage()); 
+                            + " to resolve missing value: " + e.getMessage());
                 }
             }
         }
-        
+
         if (attrs == null) {
             return Collections.emptyMap();
         } else {
             return attrs;
         }
     }
-    
+
     protected Object resolveMissingFileAttributeValue(Path file, String name, Object value, FileInfoExtractor<?> x, LinkOption ... options) throws IOException {
         if (value != null) {
             return value;
@@ -2660,7 +2660,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
         if (value != null) {    // already have the value
             return current;
         }
-        
+
         // skip if still no value
         value = x.infoOf(file, options);
         if (value == null) {
@@ -2670,7 +2670,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
         if (current == null) {
             current = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
         }
-        
+
         current.put(name, value);
         return current;
     }
@@ -2809,7 +2809,7 @@ public class SftpSubsystem extends AbstractLoggingBean implements Command, Runna
 
     protected void handleUserPrincipalLookupServiceException(Class<? extends Principal> principalType, String name, IOException e) throws IOException {
         /* According to Javadoc:
-         * 
+         *
          *      "Where an implementation does not support any notion of group
          *      or user then this method always throws UserPrincipalNotFoundException."
          */

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5bf6cab/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java b/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
index c9514f2..1e1bee9 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
@@ -110,7 +110,7 @@ public class BuiltinCiphersTest extends BaseTestSupport {
                 continue;
             }
 
-            String name = Objects.toString(f.get(null));
+            String name = Objects.toString(f.get(null), null);
             BuiltinCiphers value = BuiltinCiphers.fromFactoryName(name);
             assertNotNull("No match found for " + name, value);
             assertTrue(name + " re-specified", avail.add(value));


[3/3] mina-sshd git commit: [SSHD-543] Consider logging client session setup with level 'debug'

Posted by lg...@apache.org.
[SSHD-543] Consider logging client session setup with level 'debug'


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

Branch: refs/heads/master
Commit: 9dbd66ea9c8ddfea0d64be16360e99aa281b52ad
Parents: 84f7b62
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Tue Jul 28 09:29:58 2015 +0300
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Tue Jul 28 09:29:58 2015 +0300

----------------------------------------------------------------------
 .../keyverifier/StaticServerKeyVerifier.java    | 28 +++++++++++++-------
 .../sshd/common/session/AbstractSession.java    |  4 +--
 .../password/StaticPasswordAuthenticator.java   | 17 ++++++++++--
 .../pubkey/StaticPublickeyAuthenticator.java    | 18 ++++++++++---
 4 files changed, 51 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9dbd66ea/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifier.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifier.java b/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifier.java
index 09111bd..14685f9 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifier.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifier.java
@@ -45,17 +45,27 @@ public abstract class StaticServerKeyVerifier extends AbstractLoggingBean implem
 
     @Override
     public final boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) {
-        if (isAccepted()) {
-            log.warn("Server at {} presented unverified {} key: {}",
-                    new Object[]{remoteAddress, (serverKey == null) ? null : serverKey.getAlgorithm(), KeyUtils.getFingerPrint(serverKey)});
-            return true;
+        boolean accepted = isAccepted();
+        if (accepted) {
+            handleAcceptance(sshClientSession, remoteAddress, serverKey);
         } else {
-            if (log.isDebugEnabled()) {
-                log.debug("Reject server {} unverified {} key: {}",
-                        new Object[]{remoteAddress, (serverKey == null) ? null : serverKey.getAlgorithm(), KeyUtils.getFingerPrint(serverKey)});
-            }
+            handleRejection(sshClientSession, remoteAddress, serverKey);
+        }
+
+        return accepted;
+    }
 
-            return false;
+    protected void handleAcceptance(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) {
+        // accepting without really checking is dangerous, thus the warning
+        log.warn("Server at {} presented unverified {} key: {}",
+                 remoteAddress, (serverKey == null) ? null : serverKey.getAlgorithm(), KeyUtils.getFingerPrint(serverKey));
+    }
+
+    protected void handleRejection(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) {
+        if (log.isDebugEnabled()) {
+            log.debug("Reject server {} unverified {} key: {}",
+                      remoteAddress, (serverKey == null) ? null : serverKey.getAlgorithm(), KeyUtils.getFingerPrint(serverKey));
         }
     }
+
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9dbd66ea/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
index acf0c2f..ba4ac67 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
@@ -589,7 +589,7 @@ public abstract class AbstractSession extends CloseableUtils.AbstractInnerClosea
     public IoWriteFuture writePacket(Buffer buffer, final long timeout, final TimeUnit unit) throws IOException {
         final IoWriteFuture writeFuture = writePacket(buffer);
         final DefaultSshFuture<IoWriteFuture> future = (DefaultSshFuture<IoWriteFuture>) writeFuture;
-        ScheduledExecutorService executor = factoryManager.getScheduledExecutorService(); 
+        ScheduledExecutorService executor = factoryManager.getScheduledExecutorService();
         final ScheduledFuture<?> sched = executor.schedule(new Runnable() {
                 @SuppressWarnings("synthetic-access")
                 @Override
@@ -1202,7 +1202,7 @@ public abstract class AbstractSession extends CloseableUtils.AbstractInnerClosea
 
     @Override
     public void disconnect(int reason, String msg) throws IOException {
-        log.info("Disconnecting: {} - {}", Integer.valueOf(reason), msg);
+        log.info("Disconnecting: {} - {}", reason, msg);
         Buffer buffer = createBuffer(SshConstants.SSH_MSG_DISCONNECT);
         buffer.putInt(reason);
         buffer.putString(msg);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9dbd66ea/sshd-core/src/main/java/org/apache/sshd/server/auth/password/StaticPasswordAuthenticator.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/auth/password/StaticPasswordAuthenticator.java b/sshd-core/src/main/java/org/apache/sshd/server/auth/password/StaticPasswordAuthenticator.java
index 30ad31e..fad2ea8 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/auth/password/StaticPasswordAuthenticator.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/auth/password/StaticPasswordAuthenticator.java
@@ -38,10 +38,23 @@ public class StaticPasswordAuthenticator extends AbstractLoggingBean implements
     @Override
     public final boolean authenticate(String username, String password, ServerSession session) {
         boolean accepted = isAccepted();
-        if (log.isDebugEnabled()) {
-            log.debug("authenticate({}[{}]: {}", username, session, accepted);
+        if (accepted) {
+            handleAcceptance(username, password, session);
+        } else {
+            handleRejection(username, password, session);
         }
 
         return accepted;
     }
+
+    protected void handleAcceptance(String username, String password, ServerSession session) {
+        // accepting without really checking is dangerous, thus the warning
+        log.warn("authenticate({}[{}]: accepted without checking", username, session);
+    }
+
+    protected void handleRejection(String username, String password, ServerSession session) {
+        if (log.isDebugEnabled()) {
+            log.debug("authenticate({}[{}]: rejected", username, session);
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9dbd66ea/sshd-core/src/main/java/org/apache/sshd/server/auth/pubkey/StaticPublickeyAuthenticator.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/auth/pubkey/StaticPublickeyAuthenticator.java b/sshd-core/src/main/java/org/apache/sshd/server/auth/pubkey/StaticPublickeyAuthenticator.java
index d760f1d..29f7a57 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/auth/pubkey/StaticPublickeyAuthenticator.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/auth/pubkey/StaticPublickeyAuthenticator.java
@@ -41,11 +41,23 @@ public abstract class StaticPublickeyAuthenticator extends AbstractLoggingBean i
     @Override
     public final boolean authenticate(String username, PublicKey key, ServerSession session) {
         boolean accepted = isAccepted();
-        if (log.isDebugEnabled()) {
-            log.debug("authenticate({}[{}][{}][{}]: {}",
-                    username, session, key.getAlgorithm(), KeyUtils.getFingerPrint(key), accepted);
+        if (accepted) {
+            handleAcceptance(username, key, session);
         }
 
         return accepted;
     }
+
+    protected void handleAcceptance(String username, PublicKey key, ServerSession session) {
+        // accepting without really checking is dangerous, thus the warning
+        log.warn("authenticate({}[{}][{}][{}]: accepted without checking",
+                 username, session, key.getAlgorithm(), KeyUtils.getFingerPrint(key));
+    }
+
+    protected void handleRejection(String username, PublicKey key, ServerSession session) {
+        if (log.isDebugEnabled()) {
+            log.debug("authenticate({}[{}][{}][{}]: rejected",
+                      username, session, key.getAlgorithm(), KeyUtils.getFingerPrint(key));
+        }
+    }
 }
\ No newline at end of file


[2/3] mina-sshd git commit: [SSHD-546] Fix the visibility of some classes that were once inner ones and now are regular ones

Posted by lg...@apache.org.
[SSHD-546] Fix the visibility of some classes that were once inner ones and now are regular ones


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

Branch: refs/heads/master
Commit: 84f7b6232cbf2e835139acf30736cfa8a653129c
Parents: a5bf6ca
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Tue Jul 28 09:07:28 2015 +0300
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Tue Jul 28 09:07:28 2015 +0300

----------------------------------------------------------------------
 .../apache/sshd/common/util/Transformer.java    | 15 ++--
 .../apache/sshd/common/util/buffer/Buffer.java  |  1 -
 .../subsystem/sftp/DefaultGroupPrincipal.java   |  2 +-
 .../subsystem/sftp/DefaultUserPrincipal.java    |  2 +-
 .../server/subsystem/sftp/DirectoryHandle.java  |  2 +-
 .../sshd/server/subsystem/sftp/FileHandle.java  |  2 +-
 .../sshd/server/subsystem/sftp/Handle.java      |  4 +-
 .../subsystem/sftp/InvalidHandleException.java  |  2 +-
 .../server/subsystem/sftp/PrincipalBase.java    |  2 +-
 .../server/subsystem/sftp/UnixDateFormat.java   | 20 ++++--
 .../sshd/common/util/TransformerTest.java       | 75 ++++++++++++++++++++
 11 files changed, 106 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/main/java/org/apache/sshd/common/util/Transformer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/Transformer.java b/sshd-core/src/main/java/org/apache/sshd/common/util/Transformer.java
index 96adbc1..4a81afd 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/Transformer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/Transformer.java
@@ -59,18 +59,21 @@ public interface Transformer<I, O> {
     O transform(I input);
 
     final class Utils {
+        @SuppressWarnings("rawtypes")
+        private static final Transformer IDENTITY = new Transformer() {
+            @Override
+            public Object transform(Object input) {
+                return input;
+            }
+        };
 
         private Utils() {
             throw new UnsupportedOperationException("No instance allowed");
         }
 
+        @SuppressWarnings({ "cast", "unchecked" })
         public static <U extends V, V> Transformer<U, V> identity() {
-            return new Transformer<U, V>() {
-                @Override
-                public V transform(U input) {
-                    return input;
-                }
-            };
+            return (Transformer<U, V>) IDENTITY;
         }
 
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java
index 106d9cd..1595a79 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java
@@ -48,7 +48,6 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Objects;
 
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.cipher.ECCurves;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DefaultGroupPrincipal.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DefaultGroupPrincipal.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DefaultGroupPrincipal.java
index d52b385..b304f0f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DefaultGroupPrincipal.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DefaultGroupPrincipal.java
@@ -23,7 +23,7 @@ import java.nio.file.attribute.GroupPrincipal;
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-class DefaultGroupPrincipal extends PrincipalBase implements GroupPrincipal {
+public class DefaultGroupPrincipal extends PrincipalBase implements GroupPrincipal {
 
     public DefaultGroupPrincipal(String name) {
         super(name);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DefaultUserPrincipal.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DefaultUserPrincipal.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DefaultUserPrincipal.java
index 1ca17d8..4f50d7b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DefaultUserPrincipal.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DefaultUserPrincipal.java
@@ -23,7 +23,7 @@ import java.nio.file.attribute.UserPrincipal;
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-class DefaultUserPrincipal extends PrincipalBase implements UserPrincipal {
+public class DefaultUserPrincipal extends PrincipalBase implements UserPrincipal {
 
     public DefaultUserPrincipal(String name) {
         super(name);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DirectoryHandle.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DirectoryHandle.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DirectoryHandle.java
index c34d3ef..090b2cc 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DirectoryHandle.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/DirectoryHandle.java
@@ -27,7 +27,7 @@ import java.util.Iterator;
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-class DirectoryHandle extends Handle implements Iterator<Path> {
+public class DirectoryHandle extends Handle implements Iterator<Path> {
 
     private boolean done;
     private boolean sendDotDot;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/FileHandle.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/FileHandle.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/FileHandle.java
index fed8805..92f9346 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/FileHandle.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/FileHandle.java
@@ -48,7 +48,7 @@ import static org.apache.sshd.common.subsystem.sftp.SftpConstants.SSH_FXF_TRUNCA
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-class FileHandle extends Handle {
+public class FileHandle extends Handle {
 
     private final int access;
     private final FileChannel fileChannel;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/Handle.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/Handle.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/Handle.java
index ea0af1a..6faf7a4 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/Handle.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/Handle.java
@@ -25,10 +25,10 @@ import java.util.Objects;
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-abstract class Handle implements java.io.Closeable {
+public abstract class Handle implements java.io.Closeable {
     private Path file;
 
-    public Handle(Path file) {
+    protected Handle(Path file) {
         this.file = file;
     }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/InvalidHandleException.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/InvalidHandleException.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/InvalidHandleException.java
index cd986d3..e952adc 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/InvalidHandleException.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/InvalidHandleException.java
@@ -23,7 +23,7 @@ import java.io.IOException;
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-class InvalidHandleException extends IOException {
+public class InvalidHandleException extends IOException {
     private static final long serialVersionUID = -1686077114375131889L;
 
     public InvalidHandleException(String handle, Handle h, Class<? extends Handle> expected) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/PrincipalBase.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/PrincipalBase.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/PrincipalBase.java
index e61238e..39d8fc0 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/PrincipalBase.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/PrincipalBase.java
@@ -24,7 +24,7 @@ import java.util.Objects;
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-class PrincipalBase implements Principal {
+public class PrincipalBase implements Principal {
 
     private final String name;
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/UnixDateFormat.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/UnixDateFormat.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/UnixDateFormat.java
index 902556b..e4fefda 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/UnixDateFormat.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/UnixDateFormat.java
@@ -28,13 +28,21 @@ import java.util.List;
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-final class UnixDateFormat {
+public final class UnixDateFormat {
 
-    private static final List<String> MONTHS =
+    /**
+     * A {@link List} of <U>short</U> months names where Jan=0, Feb=1, etc.
+     */
+    public static final List<String> MONTHS =
         Collections.unmodifiableList(Arrays.asList(
             "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
         ));
 
+    /**
+     * Six months duration in msec.
+     */
+    public static final long SIX_MONTHS = 183L * 24L * 60L * 60L * 1000L;
+
     private UnixDateFormat() {
         throw new UnsupportedOperationException("No instance allowed");
     }
@@ -43,11 +51,11 @@ final class UnixDateFormat {
      * Get unix style date string.
      */
     public static String getUnixDate(FileTime time) {
-        return getUnixDate(time != null ? time.toMillis() : -1);
+        return getUnixDate((time != null) ? time.toMillis() : -1L);
     }
 
     public static String getUnixDate(long millis) {
-        if (millis < 0) {
+        if (millis < 0L) {
             return "------------";
         }
 
@@ -67,9 +75,8 @@ final class UnixDateFormat {
         sb.append(day);
         sb.append(' ');
 
-        long sixMonth = 15811200000L; // 183L * 24L * 60L * 60L * 1000L;
         long nowTime = System.currentTimeMillis();
-        if (Math.abs(nowTime - millis) > sixMonth) {
+        if (Math.abs(nowTime - millis) > SIX_MONTHS) {
 
             // year
             int year = cal.get(Calendar.YEAR);
@@ -92,6 +99,7 @@ final class UnixDateFormat {
             }
             sb.append(mm);
         }
+
         return sb.toString();
     }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/84f7b623/sshd-core/src/test/java/org/apache/sshd/common/util/TransformerTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/TransformerTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/TransformerTest.java
new file mode 100644
index 0000000..8dcbc6b
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/common/util/TransformerTest.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sshd.common.util;
+
+import java.util.Date;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.sshd.util.BaseTestSupport;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class TransformerTest extends BaseTestSupport {
+    public TransformerTest() {
+        super();
+    }
+
+    @Test
+    public void testToString() {
+        assertNull("Invalid null result", Transformer.TOSTRING.transform(null));
+        for (Object o : new Object[] { "", getClass(), new Date() }) {
+            String expected = o.toString();
+            String actual = Transformer.TOSTRING.transform(o);
+            assertEquals("Mismatched result for type=" + o.getClass().getSimpleName(), expected, actual);
+        }
+    }
+
+    @Test
+    public void testExtractEnumName() {
+        assertNull("Invalid null result", Transformer.ENUM_NAME_EXTRACTOR.transform(null));
+
+        for (TimeUnit u : TimeUnit.values()) {
+            String expected = u.name();
+            String actual = Transformer.ENUM_NAME_EXTRACTOR.transform(u);
+            assertEquals("Mismatched name", expected, actual);
+        }
+    }
+
+    @Test
+    public void testSingletonIdentityInstance() {
+        Transformer<Date,Date> dateTransformer = Transformer.Utils.identity();
+        Transformer<String,String> stringTransformer = Transformer.Utils.identity();
+        assertSame("Mismatched identity instance", dateTransformer, stringTransformer);
+    }
+
+    @Test
+    public void testIdentity() {
+        Transformer<Object,Object> identity = Transformer.Utils.identity();
+        for (Object expected : new Object[] { null, getClass(), getCurrentTestName() }) {
+            Object actual = identity.transform(expected);
+            assertSame("Mismatched identity result", expected, actual);
+        }
+    }
+}