Class SSHUtil
- java.lang.Object
-
- nl.esciencecenter.xenon.adaptors.shared.ssh.SSHUtil
-
public class SSHUtil extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
SSHUtil.PasswordProvider
(package private) static class
SSHUtil.Tunnel
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_SSH_PORT
private static org.slf4j.Logger
LOGGER
private static java.lang.String
VIA_TAG
-
Constructor Summary
Constructors Constructor Description SSHUtil()
This constructor is only needed for testing.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static SSHConnection
connect(java.lang.String adaptorName, org.apache.sshd.client.SshClient client, java.lang.String location, Credential credential, int bufferSize, long timeout)
Connect an existingSshClient
to the server atlocation
and authenticate using the givencredential
.private static org.apache.sshd.client.session.ClientSession
connectAndAuthenticate(java.lang.String adaptorName, org.apache.sshd.client.SshClient client, java.lang.String host, int port, UserCredential credential, long timeout)
static org.apache.sshd.client.SshClient
createSSHClient()
Create a newSshClient
with a default configuration similar to a stand-alone SSH client.static org.apache.sshd.client.SshClient
createSSHClient(boolean useKnownHosts, boolean loadSSHConfig, boolean stricHostCheck, boolean useSSHAgent, boolean useAgentForwarding)
Create a newSshClient
with the desired configuration.static UserCredential
extractCredential(org.apache.sshd.common.util.net.SshdSocketAddress location, Credential credential)
static UserCredential[]
extractCredentials(java.lang.String adaptorName, org.apache.sshd.common.util.net.SshdSocketAddress[] locations, Credential credentials)
static org.apache.sshd.common.util.net.SshdSocketAddress[]
extractLocations(java.lang.String adaptorName, java.lang.String location)
Extract a series of locations from a location string.private static org.apache.sshd.common.util.net.SshdSocketAddress
extractSocketAddress(java.lang.String adaptorName, java.lang.String location)
static java.lang.String
getHost(java.lang.String adaptorName, java.lang.String location)
static int
getPort(java.lang.String adaptorName, java.lang.String location)
static java.util.Map<java.lang.String,java.lang.String>
translateProperties(java.util.Map<java.lang.String,java.lang.String> providedProperties, java.lang.String orginalPrefix, XenonPropertyDescription[] supportedProperties, java.lang.String newPrefix)
static java.lang.String
validateHost(java.lang.String adaptorName, java.lang.String host)
Weak validation of a host string containing either a hostame of IP adres.static java.util.Set<java.lang.String>
validProperties(XenonPropertyDescription[] properties)
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
VIA_TAG
private static final java.lang.String VIA_TAG
- See Also:
- Constant Field Values
-
DEFAULT_SSH_PORT
public static final int DEFAULT_SSH_PORT
- See Also:
- Constant Field Values
-
-
Method Detail
-
createSSHClient
public static org.apache.sshd.client.SshClient createSSHClient()
Create a newSshClient
with a default configuration similar to a stand-alone SSH client.The default configuration loads the SSH known_hosts and config file and uses strict host key checking.
- Returns:
- the configured
SshClient
-
createSSHClient
public static org.apache.sshd.client.SshClient createSSHClient(boolean useKnownHosts, boolean loadSSHConfig, boolean stricHostCheck, boolean useSSHAgent, boolean useAgentForwarding)
Create a newSshClient
with the desired configuration.SSH clients have a significant number of options. This method will create a
SshClient
providing the most important settings.- Parameters:
useKnownHosts
- Load the SSH known_hosts file from the default location (for OpenSSH this is typically found in $HOME/.ssh/known_hosts).loadSSHConfig
- Load the SSH config file from the default location (for OpenSSH this is typically found in $HOME/.ssh/config).stricHostCheck
- Perform a strict host key check. When setting up a connection, the key presented by the server is compared to the default known_hosts file (for OpenSSH this is typically found in $HOME/.ssh/known_hosts).useSSHAgent
- When setting up a connection, handoff authentication to a separate SSH agent process.useAgentForwarding
- Support agent forwarding, allowing remote SSH servers to use the local SSH agent process to authenticate connections to other servers.- Returns:
- the configured
SshClient
-
validateHost
public static java.lang.String validateHost(java.lang.String adaptorName, java.lang.String host) throws InvalidLocationException
Weak validation of a host string containing either a hostame of IP adres.- Parameters:
adaptorName
- the name of the adaptor using this method.host
- the hostname to validate- Returns:
- the value of
host
if the validation succeeded. - Throws:
InvalidLocationException
- if the validation failed
-
getHost
public static java.lang.String getHost(java.lang.String adaptorName, java.lang.String location) throws InvalidLocationException
- Throws:
InvalidLocationException
-
getPort
public static int getPort(java.lang.String adaptorName, java.lang.String location) throws InvalidLocationException
- Throws:
InvalidLocationException
-
extractCredential
public static UserCredential extractCredential(org.apache.sshd.common.util.net.SshdSocketAddress location, Credential credential)
-
extractCredentials
public static UserCredential[] extractCredentials(java.lang.String adaptorName, org.apache.sshd.common.util.net.SshdSocketAddress[] locations, Credential credentials) throws CredentialNotFoundException
- Throws:
CredentialNotFoundException
-
extractLocations
public static org.apache.sshd.common.util.net.SshdSocketAddress[] extractLocations(java.lang.String adaptorName, java.lang.String location) throws InvalidLocationException
Extract a series of locations from a location string. This method will split the location string into the destination and any number of intermediate hops. The accepted format is:"host[:port][/workdir] [via:otherhost[:port]]*"
The locations will be returned in connection setup order, which is the reverse order in which they are listed in the location string.- Parameters:
adaptorName
- the adaptor calling this method (used in exceptions).location
- the location to parse- Returns:
- the location string split into its individual locations.
- Throws:
InvalidLocationException
- if the provided location is could not be parsed.
-
extractSocketAddress
private static org.apache.sshd.common.util.net.SshdSocketAddress extractSocketAddress(java.lang.String adaptorName, java.lang.String location) throws InvalidLocationException
- Throws:
InvalidLocationException
-
connectAndAuthenticate
private static org.apache.sshd.client.session.ClientSession connectAndAuthenticate(java.lang.String adaptorName, org.apache.sshd.client.SshClient client, java.lang.String host, int port, UserCredential credential, long timeout) throws XenonException
- Throws:
XenonException
-
connect
public static SSHConnection connect(java.lang.String adaptorName, org.apache.sshd.client.SshClient client, java.lang.String location, Credential credential, int bufferSize, long timeout) throws XenonException
Connect an existingSshClient
to the server atlocation
and authenticate using the givencredential
.- Parameters:
adaptorName
- the adaptor where this method was called from.client
- the client to connect.location
- the server to connect tocredential
- the credential to authenticate with.bufferSize
- the buffer size used for the (optional) SSH tunnels.timeout
- the timeout to use in connection setup (in milliseconds).- Returns:
- the connected
ClientSession
- Throws:
XenonException
- if the connection setup or authentication failed.
-
translateProperties
public static java.util.Map<java.lang.String,java.lang.String> translateProperties(java.util.Map<java.lang.String,java.lang.String> providedProperties, java.lang.String orginalPrefix, XenonPropertyDescription[] supportedProperties, java.lang.String newPrefix)
-
validProperties
public static java.util.Set<java.lang.String> validProperties(XenonPropertyDescription[] properties)
-
-