You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/01/25 00:02:31 UTC

svn commit: r1852086 - in /commons/proper/vfs/trunk: commons-vfs2/src/main/java/org/apache/commons/vfs2/ commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/ src/changes/

Author: ggregory
Date: Fri Jan 25 00:02:30 2019
New Revision: 1852086

URL: http://svn.apache.org/viewvc?rev=1852086&view=rev
Log:
[VFS-673][SFTP] Support com.jcraft.jsch.ConfigRepository (~/.ssh/config) with SftpFileSystemConfigBuilder and flag to load OpenSSHConfig.

Modified:
    commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/Resources.properties
    commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java
    commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java
    commons/proper/vfs/trunk/src/changes/changes.xml

Modified: commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/Resources.properties
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/Resources.properties?rev=1852086&r1=1852085&r2=1852086&view=diff
==============================================================================
--- commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/Resources.properties (original)
+++ commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/Resources.properties Fri Jan 25 00:02:30 2019
@@ -255,6 +255,7 @@ vfs.provider.sftp/StrictHostKeyChecking-
 vfs.provider.sftp/unknown-modtime.error=Last modification time not fetched.
 vfs.provider.sftp/unknown-permissions.error=File permissions not fetched.
 vfs.provider.sftp/unknown-size.error=File size not fetched.
+vfs.provider.sftp/load-openssh-config.error=Could not load openssh config.
 
 # URL Provider
 vfs.provider.url/badly-formed-uri.error=Badly formed URI "{0}".

Modified: commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java?rev=1852086&r1=1852085&r2=1852086&view=diff
==============================================================================
--- commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java (original)
+++ commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java Fri Jan 25 00:02:30 2019
@@ -17,6 +17,7 @@
 package org.apache.commons.vfs2.provider.sftp;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Properties;
 
 import org.apache.commons.logging.Log;
@@ -25,9 +26,11 @@ import org.apache.commons.vfs2.FileSyste
 import org.apache.commons.vfs2.FileSystemOptions;
 import org.apache.commons.vfs2.util.Os;
 
+import com.jcraft.jsch.ConfigRepository;
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.JSchException;
 import com.jcraft.jsch.Logger;
+import com.jcraft.jsch.OpenSSHConfig;
 import com.jcraft.jsch.Proxy;
 import com.jcraft.jsch.ProxyHTTP;
 import com.jcraft.jsch.ProxySOCKS5;
@@ -39,6 +42,7 @@ import com.jcraft.jsch.UserInfo;
  */
 public final class SftpClientFactory {
     private static final String SSH_DIR_NAME = ".ssh";
+    private static final String OPENSSH_CONFIG_NAME = "config";
 
     private static final Log LOG = LogFactory.getLog(SftpClientFactory.class);
 
@@ -71,6 +75,8 @@ public final class SftpClientFactory {
         final File knownHostsFile = builder.getKnownHosts(fileSystemOptions);
         final IdentityInfo[] identities = builder.getIdentityInfo(fileSystemOptions);
         final IdentityRepositoryFactory repositoryFactory = builder.getIdentityRepositoryFactory(fileSystemOptions);
+        final ConfigRepository configRepository = builder.getConfigRepository(fileSystemOptions);
+        final boolean loadOpenSSHConfig = builder.isLoadOpenSSHConfig(fileSystemOptions);
 
         sshDir = findSshDir();
 
@@ -81,6 +87,7 @@ public final class SftpClientFactory {
         }
 
         addIdentities(jsch, sshDir, identities);
+        setConfigRepository(jsch, sshDir, configRepository, loadOpenSSHConfig);
 
         Session session;
         try {
@@ -164,6 +171,20 @@ public final class SftpClientFactory {
             }
         }
     }
+
+    private static void setConfigRepository(final JSch jsch, final File sshDir, final ConfigRepository configRepository, boolean loadOpenSSHConfig) throws FileSystemException {
+        if (configRepository != null) {
+            jsch.setConfigRepository(configRepository);
+        } else if (loadOpenSSHConfig) {
+            try {
+                // loading openssh config (~/.ssh/config)
+                final ConfigRepository openSSHConfig = OpenSSHConfig.parseFile(new File(sshDir, OPENSSH_CONFIG_NAME).getAbsolutePath());
+                jsch.setConfigRepository(openSSHConfig);
+            } catch (IOException e) {
+                throw new FileSystemException("vfs.provider.sftp/load-openssh-config.error", e);
+            }
+        }
+    }
 
     private static void addIndentity(final JSch jsch, final IdentityInfo info) throws FileSystemException {
         try {

Modified: commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java?rev=1852086&r1=1852085&r2=1852086&view=diff
==============================================================================
--- commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java (original)
+++ commons/proper/vfs/trunk/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java Fri Jan 25 00:02:30 2019
@@ -24,6 +24,7 @@ import org.apache.commons.vfs2.FileSyste
 import org.apache.commons.vfs2.FileSystemException;
 import org.apache.commons.vfs2.FileSystemOptions;
 
+import com.jcraft.jsch.ConfigRepository;
 import com.jcraft.jsch.UserInfo;
 
 /**
@@ -95,6 +96,8 @@ public final class SftpFileSystemConfigB
     private static final String HOST_KEY_CHECK_YES = "yes";
     private static final String IDENTITIES = _PREFIX + ".IDENTITIES";
     private static final String IDENTITY_REPOSITORY_FACTORY = _PREFIX + "IDENTITY_REPOSITORY_FACTORY";
+    private static final String CONFIG_REPOSITORY = _PREFIX + "CONFIG_REPOSITORY";
+    private static final String LOAD_OPENSSH_CONFIG = _PREFIX + "LOAD_OPENSSH_CONFIG";
     private static final String KNOWN_HOSTS = _PREFIX + ".KNOWN_HOSTS";
     private static final String PREFERRED_AUTHENTICATIONS = _PREFIX + ".PREFERRED_AUTHENTICATIONS";
     private static final String PROXY_COMMAND = _PREFIX + ".PROXY_COMMAND";
@@ -218,6 +221,29 @@ public final class SftpFileSystemConfigB
     }
 
     /**
+     * Get the config repository.
+     *
+     * @param opts The FileSystem options.
+     * @return the ConfigRepository
+     */
+    public ConfigRepository getConfigRepository(final FileSystemOptions opts) {
+        return (ConfigRepository) this.getParam(opts, CONFIG_REPOSITORY);
+    }
+
+    /**
+     * Returns {@link Boolean#TRUE} if VFS should load the OpenSSH config. Defaults to
+     * <code>Boolean.FALSE</code> if the method {@link #setLoadOpenSSHConfig(FileSystemOptions, boolean)} has not been
+     * invoked.
+     *
+     * @param opts The FileSystemOptions.
+     * @return <code>Boolean.TRUE</code> if VFS should load the OpenSSH config.
+     * @see #setLoadOpenSSHConfig
+     */
+    public boolean isLoadOpenSSHConfig(final FileSystemOptions opts) {
+        return this.getBoolean(opts, LOAD_OPENSSH_CONFIG, Boolean.FALSE);
+    }
+
+    /**
      * @param opts The FileSystem options.
      * @return the known hosts File.
      * @see #setKnownHosts
@@ -462,6 +488,21 @@ public final class SftpFileSystemConfigB
     }
 
     /**
+     * Set the config repository. e.g. {@code /home/user/.ssh/config}.
+     * <p>
+     * This is useful when you want to use OpenSSHConfig.
+     *
+     * @param opts The FileSystem options.
+     * @param configRepository An config repository.
+     * @throws FileSystemException if an error occurs.
+     * @see <a href="http://www.jcraft.com/jsch/examples/OpenSSHConfig.java.html">OpenSSHConfig</a>
+     */
+    public void setConfigRepository(final FileSystemOptions opts, final ConfigRepository configRepository)
+            throws FileSystemException {
+        this.setParam(opts, CONFIG_REPOSITORY, configRepository);
+    }
+
+    /**
      * Sets the known_hosts file. e.g. {@code /home/user/.ssh/known_hosts2}.
      * <p>
      * We use {@link java.io.File} because JSch cannot deal with VFS FileObjects.
@@ -641,4 +682,14 @@ public final class SftpFileSystemConfigB
     public void setUserInfo(final FileSystemOptions opts, final UserInfo info) {
         this.setParam(opts, UserInfo.class.getName(), info);
     }
+
+    /**
+     * Sets the whether to load OpenSSH config.
+     *
+     * @param opts The FileSystem options.
+     * @param loadOpenSSHConfig true if the OpenSSH config should be loaded.
+     */
+    public void setLoadOpenSSHConfig(final FileSystemOptions opts, final boolean loadOpenSSHConfig) {
+        this.setParam(opts, LOAD_OPENSSH_CONFIG, loadOpenSSHConfig ? Boolean.TRUE : Boolean.FALSE);
+    }
 }

Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1852086&r1=1852085&r2=1852086&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Fri Jan 25 00:02:30 2019
@@ -125,6 +125,9 @@ The <action> type attribute can be add,u
       <action issue="VFS-677" dev="ggregory" type="add" due-to="dingxbcn">
         [SFTP] Add support for append mode.
       </action>
+      <action issue="VFS-673" dev="ggregory" type="add" due-to="Oleksandr Lykhonosov">
+        [SFTP] Support com.jcraft.jsch.ConfigRepository (~/.ssh/config) with SftpFileSystemConfigBuilder and flag to load OpenSSHConfig.
+      </action>
     </release>
     <release version="2.2" date="2017-10-06" description="New features and bug fix release.">
       <action issue="VFS-642" dev="pschumacher" type="update" due-to="ilangoldfeld">