- java.lang.Object
-
- org.eclipse.jgit.transport.SshSessionFactory
-
- org.eclipse.jgit.transport.sshd.SshdSessionFactory
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class SshdSessionFactory extends SshSessionFactory implements Closeable
ASshSessionFactorythat uses Apache MINA sshd. Classes from Apache MINA sshd are kept private to avoid API evolution problems when Apache MINA sshd interfaces change.- Since:
- 5.2
-
-
Constructor Summary
Constructors Constructor Description SshdSessionFactory()Creates a newSshdSessionFactorywithout key cache and aDefaultProxyDataFactory.SshdSessionFactory(KeyCache keyCache, ProxyDataFactory proxies)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()protected KeyPasswordProvidercreateKeyPasswordProvider(CredentialsProvider provider)Creates aKeyPasswordProviderfor a new session.protected ServerKeyDatabasecreateServerKeyDatabase(File homeDir, File sshDir)Creates aServerKeyDatabaseto verify server host keys.protected SshConfigStorecreateSshConfigStore(File homeDir, File configFile, String localUserName)Obtains aSshConfigStore, ornullif not SSH config is to be used.protected ConnectorFactorygetConnectorFactory()Gets aConnectorFactory.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 StringgetDefaultPreferredAuthentications()Gets the list of default preferred authentication mechanisms.FilegetHomeDirectory()Retrieves the global user home directoryprotected KeyCachegetKeyCache()Obtains theKeyCacheto use to cache loaded keys.protected ServerKeyDatabasegetServerKeyDatabase(File homeDir, File sshDir)Obtains aServerKeyDatabaseto verify server host keys.SshdSessiongetSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms)protected FilegetSshConfig(File sshDir)Determines the ssh config file.FilegetSshDirectory()Retrieves the global .ssh directoryStringgetType()voidsetHomeDirectory(File homeDir)Set a global directory to use as the user's home directoryvoidsetSshDirectory(File sshDir)Set a global directory to use as the .ssh directory-
Methods inherited from class org.eclipse.jgit.transport.SshSessionFactory
getInstance, getLocalUserName, releaseSession, setInstance
-
-
-
-
Constructor Detail
-
SshdSessionFactory
public SshdSessionFactory()
Creates a newSshdSessionFactorywithout key cache and aDefaultProxyDataFactory.
-
SshdSessionFactory
public SshdSessionFactory(KeyCache keyCache, ProxyDataFactory proxies)
Creates a newSshdSessionFactoryusing the givenKeyCacheandProxyDataFactory. ThekeyCacheis used for all sessions created through this session factory; cached keys are destroyed when the session factory isclosed.Caching ssh keys in memory for an extended period of time is generally considered bad practice, but there may be circumstances where using a
KeyCacheis 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 aKeyPasswordProviderthat 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.By default, the factory uses the
ServiceLoaderto find aConnectorFactoryfor creating aConnectorto connect to a running SSH agent. If it finds one, the SSH agent is used in publickey authentication. If there is none, no SSH agent will ever be contacted. Note that one can defineIdentitiesOnly yesfor a host entry in the~/.ssh/configfile to bypass the SSH agent in any case.- Parameters:
keyCache-KeyCacheto use for caching ssh keys, ornullto not use a key cacheproxies-ProxyDataFactoryto use, ornullto not use a proxy database (in which case connections through proxies will not be possible)
-
-
Method Detail
-
getType
public String getType()
- Specified by:
getTypein classSshSessionFactory
-
getSession
public SshdSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms) throws TransportException
- Specified by:
getSessionin classSshSessionFactory- Throws:
TransportException
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
setHomeDirectory
public void setHomeDirectory(@NonNull File homeDir)
Set a global directory to use as the user's home directory- Parameters:
homeDir- to use
-
getHomeDirectory
public File getHomeDirectory()
Retrieves the global user home directory- Returns:
- the directory, or
nullif not set
-
setSshDirectory
public void setSshDirectory(@NonNull File sshDir)
Set a global directory to use as the .ssh directory- Parameters:
sshDir- to use
-
getSshDirectory
public File getSshDirectory()
Retrieves the global .ssh directory- Returns:
- the directory, or
nullif not set
-
getSshConfig
protected File getSshConfig(@NonNull File sshDir)
Determines the ssh config file. The default implementation returns ~/.ssh/config. If the file does not exist and is created later it will be picked up. To not use a config file at all, returnnull.- Parameters:
sshDir- representing ~/.ssh/- Returns:
- the file (need not exist), or
nullif no config file shall be used - Since:
- 5.5
-
createSshConfigStore
protected SshConfigStore createSshConfigStore(@NonNull File homeDir, File configFile, String localUserName)
Obtains aSshConfigStore, ornullif not SSH config is to be used. The default implementation returnsnullifconfigFile == nulland otherwise an OpenSSH-compatible store reading host entries from the given file.- Parameters:
homeDir- may be used for ~-replacements by the returned config storeconfigFile- to use, ornullif nonelocalUserName- user name of the current user on the local OS- Returns:
- A
SshConfigStore, ornullif none is to be used - Since:
- 5.8
-
getServerKeyDatabase
@NonNull protected ServerKeyDatabase getServerKeyDatabase(@NonNull File homeDir, @NonNull File sshDir)
Obtains aServerKeyDatabaseto verify server host keys. The default implementation returns aServerKeyDatabasethat recognizes the two openssh standard files~/.ssh/known_hostsand~/.ssh/known_hosts2as well as any files configured via theUserKnownHostsFileoption in the ssh config file.- Parameters:
homeDir- home directory to use for ~ replacementsshDir- representing ~/.ssh/- Returns:
- the
ServerKeyDatabase - Since:
- 5.5
-
createServerKeyDatabase
@NonNull protected ServerKeyDatabase createServerKeyDatabase(@NonNull File homeDir, @NonNull File sshDir)
Creates aServerKeyDatabaseto verify server host keys. The default implementation returns aServerKeyDatabasethat recognizes the two openssh standard files~/.ssh/known_hostsand~/.ssh/known_hosts2as well as any files configured via theUserKnownHostsFileoption in the ssh config file.- Parameters:
homeDir- home directory to use for ~ replacementsshDir- representing ~/.ssh/- Returns:
- the
ServerKeyDatabase - Since:
- 5.8
-
getConnectorFactory
protected ConnectorFactory getConnectorFactory()
Gets aConnectorFactory. If this returnsnull, SSH agents are not supported.The default implementation uses
ConnectorFactory.getDefault()- Returns:
- the factory, or
nullif no SSH agent support is desired - Since:
- 6.0
-
getDefaultKnownHostsFiles
@NonNull protected List<Path> getDefaultKnownHostsFiles(@NonNull File sshDir)
Gets the list of default user known hosts files. The default returns ~/.ssh/known_hosts and ~/.ssh/known_hosts2. The ssh configUserKnownHostsFileoverrides this default.- Parameters:
sshDir-- Returns:
- the possibly empty list of default known host file paths.
-
getDefaultKeys
@NonNull protected Iterable<KeyPair> getDefaultKeys(@NonNull File sshDir)
Determines the default keys. The default implementation will lazy load thedefault identity files.Subclasses may override and return an
Iterableof whatever keys are appropriate. If the returned iterable lazily loads keys, it should be an instance ofAbstractResourceKeyPairProviderso that the session can later pass it thepassword providerwrapped as aFilePasswordProviderviaAbstractResourceKeyPairProvider#setPasswordFinder(FilePasswordProvider)so that encrypted, password-protected keys can be loaded.The default implementation uses exactly this mechanism; class
CachingKeyPairProvidermay serve as a model for a customized lazy-loadingIterableimplementationIf the
Iterablereturned has the keys already pre-loaded or otherwise doesn't need to decrypt encrypted keys, it can be anyIterable, for instance a simpleList.- Parameters:
sshDir- to look in for keys- Returns:
- an
Iterableover the default keys - Since:
- 5.3
-
getDefaultIdentities
@NonNull protected List<Path> getDefaultIdentities(@NonNull File sshDir)
Gets a list of default identities, i.e., private key files that shall always be tried for public key authentication. Typically those are ~/.ssh/id_dsa, ~/.ssh/id_rsa, and so on. The default implementation returns the files defined inSshConstants.DEFAULT_IDENTITIES.- Parameters:
sshDir- the directory that represents ~/.ssh/- Returns:
- a possibly empty list of paths containing default identities (private keys)
-
getKeyCache
protected final KeyCache getKeyCache()
Obtains theKeyCacheto use to cache loaded keys.- Returns:
- the
KeyCache, ornullif none.
-
createKeyPasswordProvider
@NonNull protected KeyPasswordProvider createKeyPasswordProvider(CredentialsProvider provider)
Creates aKeyPasswordProviderfor a new session.- Parameters:
provider- theCredentialsProviderto delegate to for user interactions- Returns:
- a new
KeyPasswordProvider
-
getDefaultPreferredAuthentications
protected String getDefaultPreferredAuthentications()
Gets the list of default preferred authentication mechanisms. Ifnullis returned the openssh default list will be in effect. If the ssh config definesPreferredAuthenticationsthe value from the ssh config takes precedence.- Returns:
- a comma-separated list of mechanism names, or
nullif none
-
-