From: Mathieu Baudier Date: Sun, 7 Aug 2022 05:51:57 +0000 (+0200) Subject: Simplify SSH in order to better support native image X-Git-Tag: v2.3.10~68 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=8f7b4b1532821bd46f7b838f775cf7282b1a67ec Simplify SSH in order to better support native image --- diff --git a/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/AbstractSsh.java b/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/AbstractSsh.java index 47b46cdd2..c91ab4805 100644 --- a/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/AbstractSsh.java +++ b/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/AbstractSsh.java @@ -22,8 +22,8 @@ import org.argeo.api.cms.CmsLog; public abstract class AbstractSsh { private final static CmsLog log = CmsLog.getLog(AbstractSsh.class); - private static SshClient sshClient; - private static SftpFileSystemProvider sftpFileSystemProvider; + private SshClient sshClient; + private SftpFileSystemProvider sftpFileSystemProvider; private boolean passwordSet = false; private ClientSession session; diff --git a/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/cli/DefaultClientIdentityLoader.java b/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/cli/DefaultClientIdentityLoader.java new file mode 100644 index 000000000..9199198d7 --- /dev/null +++ b/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/cli/DefaultClientIdentityLoader.java @@ -0,0 +1,54 @@ +package org.argeo.cms.ssh.cli; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.security.KeyPair; +import java.util.Objects; + +import org.apache.sshd.client.config.keys.ClientIdentityLoader; +import org.apache.sshd.common.NamedResource; +import org.apache.sshd.common.config.keys.FilePasswordProvider; +import org.apache.sshd.common.session.SessionContext; +import org.apache.sshd.common.util.ValidateUtils; +import org.apache.sshd.common.util.io.IoUtils; +import org.apache.sshd.common.util.io.resource.PathResource; +import org.apache.sshd.common.util.security.SecurityUtils; + +/** Separate class in order to avoit static field from Apache SSHD. */ +class DefaultClientIdentityLoader implements ClientIdentityLoader { + @Override + public boolean isValidLocation(NamedResource location) throws IOException { + Path path = toPath(location); + return Files.exists(path, IoUtils.EMPTY_LINK_OPTIONS); + } + + @Override + public Iterable loadClientIdentities(SessionContext session, NamedResource location, + FilePasswordProvider provider) throws IOException, GeneralSecurityException { + Path path = toPath(location); + PathResource resource = new PathResource(path); + try (InputStream inputStream = resource.openInputStream()) { + return SecurityUtils.loadKeyPairIdentities(session, resource, inputStream, provider); + } + } + + @Override + public String toString() { + return "DEFAULT"; + } + + private Path toPath(NamedResource location) { + Objects.requireNonNull(location, "No location provided"); + + Path path = Paths + .get(ValidateUtils.checkNotNullAndNotEmpty(location.getName(), "No location value for %s", location)); + path = path.toAbsolutePath(); + path = path.normalize(); + return path; + } + +} diff --git a/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/cli/SshShell.java b/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/cli/SshShell.java index f8870b97b..dffb440f1 100644 --- a/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/cli/SshShell.java +++ b/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/cli/SshShell.java @@ -14,7 +14,6 @@ import org.apache.sshd.agent.SshAgent; import org.apache.sshd.agent.SshAgentFactory; import org.apache.sshd.agent.local.LocalAgentFactory; import org.apache.sshd.agent.unix.UnixAgentFactory; -import org.apache.sshd.client.config.keys.ClientIdentityLoader; import org.apache.sshd.common.NamedResource; import org.apache.sshd.common.config.keys.FilePasswordProvider; import org.argeo.api.cli.CommandArgsException; @@ -83,7 +82,7 @@ public class SshShell implements DescribedCommand { return keyPath; } }; - KeyPair keyPair = ClientIdentityLoader.DEFAULT + KeyPair keyPair = new DefaultClientIdentityLoader() .loadClientIdentities(null, namedResource, FilePasswordProvider.of(new String(keyPassword))) .iterator().next(); sshAgent.addIdentity(keyPair, "NO COMMENT");