Author: joehni Date: Fri Mar 1 15:46:38 2013 New Revision: 1451622 URL: http://svn.apache.org/r1451622 Log: Support private key files with passphrase (VFS-283) and additional public key.
Added: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/IdentityInfo.java (with props) Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java Added: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/IdentityInfo.java URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/IdentityInfo.java?rev=1451622&view=auto ============================================================================== --- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/IdentityInfo.java (added) +++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/IdentityInfo.java Fri Mar 1 15:46:38 2013 @@ -0,0 +1,118 @@ +/* + * 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.commons.vfs2.provider.sftp; + +import java.io.File; + + +/** + * Structure for an identity. + * + * @since 2.1 + */ +public class IdentityInfo +{ + private final File privateKey; + private final File publicKey; + private final byte[] passPhrase; + + /** + * Constructs an identity info with private key. + * + * <p> + * The key is not passphrase protected. + * </p> + * <p> + * We use java.io.File because JSch cannot deal with VFS FileObjects. + * </p> + * + * @param privateKey The file with the private key + * @since 2.1 + */ + public IdentityInfo(final File privateKey) + { + this(privateKey, null, null); + } + + /** + * Constructs an identity info with private key and its passphrase. + * + * <p> + * We use java.io.File because JSch cannot deal with VFS FileObjects. + * </p> + * + * @param privateKey The file with the private key + * @param passPhrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used) + * @since 2.1 + */ + public IdentityInfo(final File privateKey, final byte[] passPhrase) + { + this(privateKey, null, passPhrase); + } + + /** + * Constructs an identity info with private and public key and passphrase for the private key. + * + * <p> + * We use java.io.File because JSch cannot deal with VFS FileObjects. + * </p> + * + * @param privateKey The file with the private key + * @param publicKey The public key part used for connections with exchange of certificates (can be {@code null}) + * @param passPhrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used) + * @since 2.1 + */ + public IdentityInfo(final File privateKey, final File publicKey, final byte[] passPhrase) + { + this.privateKey = privateKey; + this.publicKey = publicKey; + this.passPhrase = passPhrase; + } + + /** + * Get the file with the private key. + * + * @return the file + * @since 2.1 + */ + public File getPrivateKey() + { + return privateKey; + } + + /** + * Get the file with the public key. + * + * @return the file + * @since 2.1 + */ + public File getPublicKey() + { + return publicKey; + } + + /** + * Get the passphrase of the private key. + * + * @return the passphrase + * @since 2.1 + */ + public byte[] getPassPhrase() + { + return passPhrase; + } +} Propchange: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/IdentityInfo.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/IdentityInfo.java ------------------------------------------------------------------------------ svn:keywords = Author Id HeadURL Revision Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java?rev=1451622&r1=1451621&r2=1451622&view=diff ============================================================================== --- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java (original) +++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java Fri Mar 1 15:46:38 2013 @@ -73,7 +73,7 @@ public final class SftpClientFactory // new style - user passed final SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance(); final File knownHostsFile = builder.getKnownHosts(fileSystemOptions); - final File[] identities = builder.getIdentities(fileSystemOptions); + final IdentityInfo[] identities = builder.getIdentityInfo(fileSystemOptions); final IdentityRepositoryFactory repositoryFactory = builder.getIdentityRepositoryFactory(fileSystemOptions); sshDir = findSshDir(); @@ -172,13 +172,13 @@ public final class SftpClientFactory return session; } - private static void addIdentities(final JSch jsch, final File sshDir, final File[] identities) throws FileSystemException + private static void addIdentities(final JSch jsch, final File sshDir, final IdentityInfo[] identities) throws FileSystemException { if (identities != null) { - for (final File privateKeyFile : identities) + for (final IdentityInfo info : identities) { - addIndentity(jsch, privateKeyFile); + addIndentity(jsch, info); } } else @@ -187,20 +187,22 @@ public final class SftpClientFactory final File privateKeyFile = new File(sshDir, "id_rsa"); if (privateKeyFile.isFile() && privateKeyFile.canRead()) { - addIndentity(jsch, privateKeyFile); + addIndentity(jsch, new IdentityInfo(privateKeyFile)); } } } - private static void addIndentity(final JSch jsch, final File privateKeyFile) throws FileSystemException + private static void addIndentity(final JSch jsch, final IdentityInfo info) throws FileSystemException { try { - jsch.addIdentity(privateKeyFile.getAbsolutePath()); + final String privateKeyFile = info.getPrivateKey() != null ? info.getPrivateKey().getAbsolutePath() : null; + final String publicKeyFile = info.getPublicKey() != null ? info.getPublicKey().getAbsolutePath() : null; + jsch.addIdentity(privateKeyFile, publicKeyFile, info.getPassPhrase()); } catch (final JSchException e) { - throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e); + throw new FileSystemException("vfs.provider.sftp/load-private-key.error", info, e); } } Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java?rev=1451622&r1=1451621&r2=1451622&view=diff ============================================================================== --- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java (original) +++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java Fri Mar 1 15:46:38 2013 @@ -173,15 +173,39 @@ public final class SftpFileSystemConfigB * <p> * We use java.io.File because JSch cannot deal with VFS FileObjects. * </p> - * - * @param opts - * The FileSystem options. + * + * @param opts The FileSystem options. * @return the array of identity Files. * @see #setIdentities + * @deprecated As of 2.1 use {@link #getIdentityInfo(FileSystemOptions)} */ + @Deprecated public File[] getIdentities(final FileSystemOptions opts) { - return (File[]) this.getParam(opts, IDENTITIES); + final IdentityInfo[] info = getIdentityInfo(opts); + if (info != null) + { + final File[] files = new File[info.length]; + for (int i = 0; i < files.length; ++i) + { + files[i] = info[i].getPrivateKey(); + } + return files; + } + return null; + } + + /** + * Gets the identity info. + * + * @param opts + * The FileSystem options. + * @return the array of identity info instances. + * @see #setIdentityInfo + */ + public IdentityInfo[] getIdentityInfo(final FileSystemOptions opts) + { + return (IdentityInfo[]) this.getParam(opts, IDENTITIES); } /** @@ -394,17 +418,41 @@ public final class SftpFileSystemConfigB * <p> * We use java.io.File because JSch cannot deal with VFS FileObjects. * </p> + * + * @param opts The FileSystem options. + * @param identityFiles An array of identity Files. + * @throws FileSystemException if an error occurs. + * @deprecated As of 2.1 use {@link #setIdentityInfo(FileSystemOptions, IdentityInfo...)} + */ + @Deprecated + public void setIdentities(final FileSystemOptions opts, final File... identityFiles) throws FileSystemException + { + IdentityInfo[] info = null; + if (identityFiles != null) + { + info = new IdentityInfo[identityFiles.length]; + for (int i = 0; i < identityFiles.length; i++) + { + info[i] = new IdentityInfo(identityFiles[i]); + } + } + this.setParam(opts, IDENTITIES, info); + } + + /** + * Sets the identity info (your private key files). * * @param opts * The FileSystem options. - * @param identityFiles - * An array of identity Files. + * @param identites + * An array of identity info. * @throws FileSystemException * if an error occurs. + * @since 2.1 */ - public void setIdentities(final FileSystemOptions opts, final File... identityFiles) throws FileSystemException + public void setIdentityInfo(final FileSystemOptions opts, final IdentityInfo... identites) throws FileSystemException { - this.setParam(opts, IDENTITIES, identityFiles); + this.setParam(opts, IDENTITIES, identites); } /**