public class SshdSessionFactory extends SshSessionFactory implements Closeable
SshSessionFactory
that uses Apache MINA sshd. Classes from Apache
MINA sshd are kept private to avoid API evolution problems when Apache MINA
sshd interfaces change.Constructor and Description |
---|
SshdSessionFactory()
Creates a new
SshdSessionFactory without key cache and a
DefaultProxyDataFactory . |
SshdSessionFactory(KeyCache keyCache,
ProxyDataFactory proxies)
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
protected KeyPasswordProvider |
createKeyPasswordProvider(CredentialsProvider provider)
Creates a
KeyPasswordProvider for a new session. |
protected ServerKeyDatabase |
createServerKeyDatabase(File homeDir,
File sshDir)
Creates a
ServerKeyDatabase to verify server host keys. |
protected SshConfigStore |
createSshConfigStore(File homeDir,
File configFile,
String localUserName)
Obtains a
SshConfigStore , or null if not SSH config is to
be used. |
protected List<Path> |
getDefaultIdentities(File sshDir)
Gets a list of default identities, i.e., private key files that shall
always be tried for public key authentication.
|
protected Iterable<KeyPair> |
getDefaultKeys(File sshDir)
Determines the default keys.
|
protected List<Path> |
getDefaultKnownHostsFiles(File sshDir)
Gets the list of default user known hosts files.
|
protected String |
getDefaultPreferredAuthentications()
Gets the list of default preferred authentication mechanisms.
|
File |
getHomeDirectory()
Retrieves the global user home directory
|
protected KeyCache |
getKeyCache()
Obtains the
KeyCache to use to cache loaded keys. |
protected ServerKeyDatabase |
getServerKeyDatabase(File homeDir,
File sshDir)
Obtains a
ServerKeyDatabase to verify server host keys. |
SshdSession |
getSession(URIish uri,
CredentialsProvider credentialsProvider,
FS fs,
int tms)
Opens (or reuses) a session to a host.
|
protected File |
getSshConfig(File sshDir)
Determines the ssh config file.
|
File |
getSshDirectory()
Retrieves the global .ssh directory
|
String |
getType()
The name of the type of session factory.
|
void |
setHomeDirectory(File homeDir)
Set a global directory to use as the user's home directory
|
void |
setSshDirectory(File sshDir)
Set a global directory to use as the .ssh directory
|
getInstance, getLocalUserName, releaseSession, setInstance
public SshdSessionFactory()
SshdSessionFactory
without key cache and a
DefaultProxyDataFactory
.public SshdSessionFactory(KeyCache keyCache, ProxyDataFactory proxies)
SshdSessionFactory
using the given KeyCache
and ProxyDataFactory
. The keyCache
is used for all sessions
created through this session factory; cached keys are destroyed when the
session factory is closed
.
Caching ssh keys in memory for an extended period of time is generally
considered bad practice, but there may be circumstances where using a
KeyCache
is still the right choice, for instance to avoid that a
user gets prompted several times for the same password for the same key.
In general, however, it is preferable not to use a key cache but
to use a KeyPasswordProvider
that has access to some secure storage and can save
and retrieve passwords from there without user interaction. Another
approach is to use an ssh agent.
Note that the underlying ssh library (Apache MINA sshd) may or may not
keep ssh keys in memory for unspecified periods of time irrespective of
the use of a KeyCache
.
keyCache
- KeyCache
to use for caching ssh keys, or null
to not use a key cacheproxies
- ProxyDataFactory
to use, or null
to not use a
proxy database (in which case connections through proxies will
not be possible)public String getType()
SshSessionFactory
getType
in class SshSessionFactory
public SshdSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms) throws TransportException
SshSessionFactory
getSession
in class SshSessionFactory
uri
- URI of the remote host to connect tocredentialsProvider
- provider to support authentication, may be null
if no
user input for authentication is neededfs
- the file system abstraction to use for certain file
operations, such as reading configuration filestms
- connection timeout for creating the session, in millisecondsuri
TransportException
- if the session could not be createdpublic void close()
close
in interface Closeable
close
in interface AutoCloseable
public void setHomeDirectory(@NonNull File homeDir)
homeDir
- to usepublic File getHomeDirectory()
null
if not setpublic void setSshDirectory(@NonNull File sshDir)
sshDir
- to usepublic File getSshDirectory()
null
if not setprotected File getSshConfig(@NonNull File sshDir)
null
.sshDir
- representing ~/.ssh/null
if no config file
shall be usedprotected SshConfigStore createSshConfigStore(@NonNull File homeDir, File configFile, String localUserName)
SshConfigStore
, or null
if not SSH config is to
be used. The default implementation returns null
if
configFile == null
and otherwise an OpenSSH-compatible store
reading host entries from the given file.homeDir
- may be used for ~-replacements by the returned config storeconfigFile
- to use, or null
if nonelocalUserName
- user name of the current user on the local OSSshConfigStore
, or null
if none is to be used@NonNull protected ServerKeyDatabase getServerKeyDatabase(@NonNull File homeDir, @NonNull File sshDir)
ServerKeyDatabase
to verify server host keys. The
default implementation returns a ServerKeyDatabase
that
recognizes the two openssh standard files ~/.ssh/known_hosts
and
~/.ssh/known_hosts2
as well as any files configured via the
UserKnownHostsFile
option in the ssh config file.homeDir
- home directory to use for ~ replacementsshDir
- representing ~/.ssh/ServerKeyDatabase
@NonNull protected ServerKeyDatabase createServerKeyDatabase(@NonNull File homeDir, @NonNull File sshDir)
ServerKeyDatabase
to verify server host keys. The
default implementation returns a ServerKeyDatabase
that
recognizes the two openssh standard files ~/.ssh/known_hosts
and
~/.ssh/known_hosts2
as well as any files configured via the
UserKnownHostsFile
option in the ssh config file.homeDir
- home directory to use for ~ replacementsshDir
- representing ~/.ssh/ServerKeyDatabase
@NonNull protected List<Path> getDefaultKnownHostsFiles(@NonNull File sshDir)
UserKnownHostsFile
overrides this default.sshDir
- @NonNull protected Iterable<KeyPair> getDefaultKeys(@NonNull File sshDir)
default identity files
.
Subclasses may override and return an Iterable
of whatever keys
are appropriate. If the returned iterable lazily loads keys, it should be
an instance of
AbstractResourceKeyPairProvider
so that the session can later pass it
the password
provider
wrapped as a FilePasswordProvider
via
AbstractResourceKeyPairProvider#setPasswordFinder(FilePasswordProvider)
so that encrypted, password-protected keys can be loaded.
The default implementation uses exactly this mechanism; class
CachingKeyPairProvider
may serve as a model for a customized
lazy-loading Iterable
implementation
If the Iterable
returned has the keys already pre-loaded or
otherwise doesn't need to decrypt encrypted keys, it can be any
Iterable
, for instance a simple List
.
sshDir
- to look in for keysIterable
over the default keys@NonNull protected List<Path> getDefaultIdentities(@NonNull File sshDir)
SshConstants.DEFAULT_IDENTITIES
.sshDir
- the directory that represents ~/.ssh/protected final KeyCache getKeyCache()
KeyCache
to use to cache loaded keys.KeyCache
, or null
if none.@NonNull protected KeyPasswordProvider createKeyPasswordProvider(CredentialsProvider provider)
KeyPasswordProvider
for a new session.provider
- the CredentialsProvider
to delegate to for user
interactionsKeyPasswordProvider
protected String getDefaultPreferredAuthentications()
null
is returned the openssh default list will be in effect. If
the ssh config defines PreferredAuthentications
the value from
the ssh config takes precedence.null
if
noneCopyright © 2021 Eclipse JGit Project. All rights reserved.