From f44461d05d0f790a4371549459eb411b76fa6cb4 Mon Sep 17 00:00:00 2001 From: Sven Nissel Date: Sun, 24 Apr 2022 11:30:56 +0200 Subject: [PATCH] #622 fix using pageant only if no keyfile used --- .../src/main/java/olv.java-conventions.gradle | 4 +- .../browser/auth/SftpUserAuthenticator.java | 4 +- .../pl/otros/vfs/browser/util/VFSUtils.java | 117 ++++++------------ 3 files changed, 43 insertions(+), 82 deletions(-) diff --git a/buildSrc/src/main/java/olv.java-conventions.gradle b/buildSrc/src/main/java/olv.java-conventions.gradle index 91a5841a..ffea1b97 100644 --- a/buildSrc/src/main/java/olv.java-conventions.gradle +++ b/buildSrc/src/main/java/olv.java-conventions.gradle @@ -34,8 +34,8 @@ dependencies { implementation 'com.google.code.gson:gson:2.8.2' implementation 'com.github.mwiede:jsch:0.2.0' implementation 'org.bouncycastle:bcprov-jdk15on:1.70' - implementation 'net.java.dev.jna:jna-jpms:5.10.0' - implementation 'net.java.dev.jna:jna-platform-jpms:5.10.0' + implementation 'net.java.dev.jna:jna-jpms:5.10.0'//newer version 5.11.0 exists + implementation 'net.java.dev.jna:jna-platform-jpms:5.10.0'//newer version 5.11.0 exists implementation 'com.jcraft:jzlib:1.+' implementation 'com.miglayout:miglayout-swing:4.2' implementation 'com.fifesoft:rsyntaxtextarea:2.6.1' diff --git a/olv-vfs/src/main/java/pl/otros/vfs/browser/auth/SftpUserAuthenticator.java b/olv-vfs/src/main/java/pl/otros/vfs/browser/auth/SftpUserAuthenticator.java index 36ed73a6..574c5fc9 100644 --- a/olv-vfs/src/main/java/pl/otros/vfs/browser/auth/SftpUserAuthenticator.java +++ b/olv-vfs/src/main/java/pl/otros/vfs/browser/auth/SftpUserAuthenticator.java @@ -47,7 +47,7 @@ protected void getAuthenticationData(UserAuthenticationData authenticationData) if (StringUtils.isNotBlank(sshKeyFileField.getText())) { //use SSH KEY - authenticationData.setData(UserAuthenticationDataWrapper.SSH_KEY, sshKeyFileField.getText().trim().toCharArray()); +// authenticationData.setData(UserAuthenticationDataWrapper.SSH_KEY, sshKeyFileField.getText().trim().toCharArray()); IdentityProvider sshKeyAuth; if (passTx.getPassword() != null && passTx.getPassword().length > 0) { //SSH KEY secured with password @@ -57,6 +57,8 @@ protected void getAuthenticationData(UserAuthenticationData authenticationData) sshKeyAuth = new IdentityInfo(new File(sshKeyFileField.getText())); } SftpFileSystemConfigBuilder.getInstance().setIdentityProvider(getFileSystemOptions(), sshKeyAuth); + } else if (PageantIdentityRepositoryFactory.getIdentitiesCount().orElse(0) > 0) { + SftpFileSystemConfigBuilder.getInstance().setIdentityRepositoryFactory(getFileSystemOptions(), new PageantIdentityRepositoryFactory()); } else { authenticationData.setData(UserAuthenticationData.PASSWORD, passTx.getPassword()); } diff --git a/olv-vfs/src/main/java/pl/otros/vfs/browser/util/VFSUtils.java b/olv-vfs/src/main/java/pl/otros/vfs/browser/util/VFSUtils.java index 83eb4834..b4bba0fc 100644 --- a/olv-vfs/src/main/java/pl/otros/vfs/browser/util/VFSUtils.java +++ b/olv-vfs/src/main/java/pl/otros/vfs/browser/util/VFSUtils.java @@ -15,38 +15,15 @@ */ package pl.otros.vfs.browser.util; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.time.Duration; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import javax.swing.Icon; +import com.google.common.base.Joiner; +import com.google.common.base.Throwables; +import com.jcraft.jsch.JSchException; +import jcifs.smb.SmbAuthException; +import net.sf.vfsjfilechooser.utils.VFSURIParser; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; -import org.apache.commons.vfs2.CacheStrategy; -import org.apache.commons.vfs2.FileContent; -import org.apache.commons.vfs2.FileName; -import org.apache.commons.vfs2.FileObject; -import org.apache.commons.vfs2.FileSystemException; -import org.apache.commons.vfs2.FileSystemManager; -import org.apache.commons.vfs2.FileSystemOptions; -import org.apache.commons.vfs2.FileType; -import org.apache.commons.vfs2.UserAuthenticationData; +import org.apache.commons.vfs2.*; import org.apache.commons.vfs2.UserAuthenticationData.Type; import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder; import org.apache.commons.vfs2.impl.StandardFileSystemManager; @@ -56,22 +33,25 @@ import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Joiner; -import com.google.common.base.Throwables; -import com.jcraft.jsch.JSchException; -import jcifs.smb.SmbAuthException; -import net.sf.vfsjfilechooser.utils.VFSURIParser; import pl.otros.vfs.browser.Icons; import pl.otros.vfs.browser.LinkFileObject; import pl.otros.vfs.browser.TaskContext; -import pl.otros.vfs.browser.auth.AuthStore; -import pl.otros.vfs.browser.auth.AuthStoreUtils; -import pl.otros.vfs.browser.auth.MemoryAuthStore; -import pl.otros.vfs.browser.auth.OtrosUserAuthenticator; -import pl.otros.vfs.browser.auth.StaticPasswordProvider; -import pl.otros.vfs.browser.auth.UserAuthenticationDataWrapper; -import pl.otros.vfs.browser.auth.UserAuthenticationInfo; -import pl.otros.vfs.browser.auth.UserAuthenticatorFactory; +import pl.otros.vfs.browser.auth.*; + +import javax.swing.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.*; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * A helper class to deal with commons-vfs file abstractions @@ -95,7 +75,6 @@ public final class VFSUtils { private static final Logger LOGGER = LoggerFactory.getLogger(VFSUtils.class); private static final Map schemeIconMap = new HashMap(); private static final Set archivesSuffixes = new HashSet(); - private static final FileSystemOptions opts = new FileSystemOptions(); private static final ReadWriteLock aLock = new ReentrantReadWriteLock(true); private static final AuthStore sessionAuthStore = new MemoryAuthStore(); //TODO change to persistent auth store @@ -166,21 +145,7 @@ public static FileSystemManager getFileSystemManager() { } // ----------------------------------------------------------------------- - - - /** - * Returns a file representation - * - * @param filePath The file path - * @return a file representation - */ - public static FileObject createFileObject(String filePath) { - try { - return getFileSystemManager().resolveFile(filePath, opts); - } catch (FileSystemException ex) { - return null; - } - } + /** * Remove user credentials information @@ -304,27 +269,30 @@ public static boolean isRoot(FileObject fileObject) { */ public static FileObject resolveFileObject(String filePath) throws FileSystemException { LOGGER.info("Resolving file: {}", URI_UTILS.getFriendlyURI(filePath)); + + FileSystemOptions options = new FileSystemOptions(); if (filePath.startsWith("sftp://")) { SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance(); - builder.setStrictHostKeyChecking(opts, "no"); - builder.setUserDirIsRoot(opts, false); - builder.setCompression(opts, "zlib,none"); - builder.setIdentityRepositoryFactory(opts, new PageantIdentityRepositoryFactory()); - builder.setDisableDetectExecChannel(opts, true); // see https://issues.apache.org/jira/browse/VFS-818 + builder.setStrictHostKeyChecking(options, "no"); + builder.setUserDirIsRoot(options, false); + builder.setCompression(options, "zlib,none"); + builder.setDisableDetectExecChannel(options, true); // see https://issues.apache.org/jira/browse/VFS-818 } else if (filePath.startsWith("smb://")) { } else if (filePath.startsWith("ftp://")) { - FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true); + FtpFileSystemConfigBuilder.getInstance().setPassiveMode(options, true); } - UserAuthenticatorFactory factory = new UserAuthenticatorFactory(); - - OtrosUserAuthenticator authenticator = factory.getUiUserAuthenticator(persistentAuthStore, sessionAuthStore, filePath, opts); + //Getting user, password, keyfile or pageant auth information and set it. + UserAuthenticatorFactory factory = new UserAuthenticatorFactory(); + OtrosUserAuthenticator authenticator = factory.getUiUserAuthenticator(persistentAuthStore, sessionAuthStore, filePath, options); if (pathContainsCredentials(filePath)) { authenticator = null; } - return resolveFileObject(filePath, opts, authenticator, persistentAuthStore, sessionAuthStore); + DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, authenticator); + + return resolveFileObject(filePath, options, null, persistentAuthStore, sessionAuthStore); } private static boolean pathContainsCredentials(String filePath) { @@ -344,21 +312,12 @@ public static FileObject resolveFileObject(URI uri) throws FileSystemException, * @return a file representation * @throws FileSystemException */ - public static FileObject resolveFileObject(String filePath, FileSystemOptions options, OtrosUserAuthenticator authenticator, AuthStore persistentAuthStore, AuthStore sessionAuthStore) throws FileSystemException { - if (filePath.startsWith("sftp://")) { - SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance(); - builder.setStrictHostKeyChecking(opts, "no"); - builder.setUserDirIsRoot(opts, false); - builder.setCompression(opts, "zlib,none"); - builder.setSessionTimeout(opts, Duration.ofSeconds(5)); - builder.setDisableDetectExecChannel(opts, true); // see https://issues.apache.org/jira/browse/VFS-818 - } + private static FileObject resolveFileObject(String filePath, FileSystemOptions options, OtrosUserAuthenticator authenticator, AuthStore persistentAuthStore, AuthStore sessionAuthStore) throws FileSystemException { - DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, authenticator); FileObject resolveFile; - VFSURIParser parser = new VFSURIParser(filePath); + //Get file type to force authentication try { resolveFile = getFileSystemManager().resolveFile(filePath, options);