read a private key directly from environment variable 'privatekey'

This commit is contained in:
Jörg Prante 2024-05-22 10:52:58 +02:00
parent f39c974e74
commit 1d256a70d1
4 changed files with 40 additions and 5 deletions

View file

@ -85,6 +85,7 @@ import org.apache.sshd.common.auth.MutableBasicCredentials;
import org.apache.sshd.common.io.IoSession;
import org.apache.sshd.common.keyprovider.ClassLoadableResourceKeyPairProvider;
import org.apache.sshd.common.keyprovider.KeyIdentityProvider;
import org.apache.sshd.common.keyprovider.SimpleKeyPairProvider;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.NumberUtils;
import org.apache.sshd.common.util.ValidateUtils;
@ -204,6 +205,9 @@ public class SftpFileSystemProvider extends FileSystemProvider {
String username = o instanceof String ? (String) o : o != null ? o.toString() : null;
o = env.get("password");
char[] password = o instanceof char[] ? (char[]) o : o instanceof String ? ((String)o).toCharArray() : null;
if (env.containsKey("privatekey")) {
clientInstance.setKeyIdentityProvider(new SimpleKeyPairProvider(env.get("privatekey").toString()));
}
if (env.containsKey("key")) {
clientInstance.setKeyIdentityProvider(new ClassLoadableResourceKeyPairProvider(env.get("key").toString()));
}

View file

@ -3,8 +3,6 @@ package org.apache.sshd.fs.test;
import java.nio.file.Files;
import org.apache.sshd.client.ClientBuilder;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.config.hosts.HostConfigEntry;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.fs.SftpFileSystem;
import org.apache.sshd.fs.SftpFileSystemProvider;
import org.junit.jupiter.api.Test;
@ -27,9 +25,9 @@ public class SFTPFileSystemTest {
env.put("username", "joerg");
URI uri = URI.create("sftp://xbib.org");
SshClient sshClient = ClientBuilder.builder().build();
Path privateKey = Paths.get(System.getProperty("user.home") + "/.ssh/id_ed25519");
Path privateKeyPath = Paths.get(System.getProperty("user.home") + "/.ssh/id_ed25519");
PrivateKeyReader privateKeyReader = new PrivateKeyReader();
KeyPair keyPair = privateKeyReader.generateFrom(Files.newInputStream(privateKey), null);
KeyPair keyPair = privateKeyReader.generateFrom(Files.newInputStream(privateKeyPath), null);
sshClient.addPublicKeyIdentity(keyPair);
sshClient.setNioWorkers(1);
sshClient.start();

View file

@ -0,0 +1,33 @@
package org.apache.sshd.common.keyprovider;
import org.apache.sshd.common.config.keys.loader.KeyPairResourceParser;
import org.apache.sshd.common.session.SessionContext;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.NoSuchProviderException;
import static org.apache.sshd.common.util.security.SecurityUtils.getKeyPairResourceParser;
/**
* Read a key directly from a given string which represents the private key.
*/
public class SimpleKeyPairProvider extends AbstractKeyPairProvider {
// the private key
private final String data;
public SimpleKeyPairProvider(String data) {
this.data = data;
}
@Override
public Iterable<KeyPair> loadKeys(SessionContext session) throws IOException, GeneralSecurityException {
KeyPairResourceParser parser = getKeyPairResourceParser();
if (parser == null) {
throw new NoSuchProviderException("No registered key-pair resource parser");
}
return parser.loadKeyPairs(session, null, null, data);
}
}

View file

@ -1,3 +1,3 @@
group = org.xbib
name = files
version = 4.5.0
version = 4.6.0