Package org.eclipse.jgit.junit.ssh
Class SshTestGitServer
- java.lang.Object
-
- org.eclipse.jgit.junit.ssh.SshTestGitServer
-
public class SshTestGitServer extends java.lang.ObjectA simple ssh/sftp git test server based on Apache MINA sshd.Supports only a single repository. Authenticates only the given test user against his given test public key. Supports fetch and push.
- Since:
- 5.2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classSshTestGitServer.EchoCommandSimple echo command that echoes back the command string.private static classSshTestGitServer.FakeUserAuthGSSprivate classSshTestGitServer.GitReceivePackCommandprivate classSshTestGitServer.GitUploadPackCommand
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringECHO_COMMANDSimple echo test command.private org.apache.sshd.common.util.threads.CloseableExecutorServiceexecutorServiceprotected java.util.List<java.security.KeyPair>hostKeysprotected Repositoryrepositoryprotected org.apache.sshd.server.SshServerserverprotected java.security.PublicKeytestKeyprotected java.lang.StringtestUser
-
Constructor Summary
Constructors Constructor Description SshTestGitServer(java.lang.String testUser, java.nio.file.Path testKey, Repository repository, byte[] hostKey)Creates a ssh git test server.SshTestGitServer(java.lang.String testUser, java.nio.file.Path testKey, Repository repository, java.security.KeyPair hostKey)Creates a ssh git test server.SshTestGitServer(java.lang.String testUser, java.security.PublicKey testKey, Repository repository, java.security.KeyPair hostKey)Creates a ssh git test server.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddHostKey(java.nio.file.Path key, boolean inFront)Adds an additional host key to the server.voidaddHostKey(java.security.KeyPair key, boolean inFront)Adds an additional host key to the server.protected voidconfigureAuthentication()Configures the authentication mechanisms of this test server.protected voidconfigureShell()Configures shell access for the test server.protected java.util.List<org.apache.sshd.server.subsystem.SubsystemFactory>configureSubsystems()Configures the test server's subsystems (sftp, scp).voidenableKeyboardInteractiveAuthentication()Enable keyboard-interactive authentication.voidenablePasswordAuthentication()Enable password authentication.private java.util.List<org.apache.sshd.server.auth.UserAuthFactory>getAuthFactories()org.apache.sshd.common.PropertyResolvergetPropertyResolver()Retrieves the server'sPropertyResolver, giving access to server properties.private static java.util.List<org.apache.sshd.common.NamedFactory<org.apache.sshd.common.signature.Signature>>getSignatureFactories()Apache MINA sshd 2.6.0 has removed DSA, DSA_CERT and RSA_CERT.private static java.security.KeyPairreadKeyPair(byte[] keyMaterial)private static java.security.PublicKeyreadPublicKey(java.nio.file.Path key)voidsetPreamble(java.lang.String... lines)Sets the lines the server sends before its server identification in the initial protocol version exchange.voidsetTestUserPublicKey(java.nio.file.Path key)Sets the test user's public key on the server.voidsetTestUserPublicKey(java.security.PublicKey key)Sets the test user's public key on the server.intstart()Starts the test server, listening on a random port.voidstop()Stops the test server.
-
-
-
Field Detail
-
ECHO_COMMAND
public static final java.lang.String ECHO_COMMAND
Simple echo test command. Replies with the command string as passed. If of the form "echo [int] anything", takes the integer value as a delay in seconds before replying, which may be useful to test various timeout-related things.- Since:
- 5.9
- See Also:
- Constant Field Values
-
testUser
@NonNull protected final java.lang.String testUser
-
repository
@NonNull protected final Repository repository
-
hostKeys
@NonNull protected final java.util.List<java.security.KeyPair> hostKeys
-
server
protected final org.apache.sshd.server.SshServer server
-
testKey
@NonNull protected java.security.PublicKey testKey
-
executorService
private final org.apache.sshd.common.util.threads.CloseableExecutorService executorService
-
-
Constructor Detail
-
SshTestGitServer
public SshTestGitServer(@NonNull java.lang.String testUser, @NonNull java.nio.file.Path testKey, @NonNull Repository repository, @NonNull byte[] hostKey) throws java.io.IOException, java.security.GeneralSecurityException
Creates a ssh git test server. It serves one single repository, and accepts public-key authentication for exactly one test user.- Parameters:
testUser- user name of the test usertestKey- public key file of the test userrepository- to servehostKey- the unencrypted private key to use as host key- Throws:
java.io.IOExceptionjava.security.GeneralSecurityException
-
SshTestGitServer
public SshTestGitServer(@NonNull java.lang.String testUser, @NonNull java.nio.file.Path testKey, @NonNull Repository repository, @NonNull java.security.KeyPair hostKey) throws java.io.IOException, java.security.GeneralSecurityException
Creates a ssh git test server. It serves one single repository, and accepts public-key authentication for exactly one test user.- Parameters:
testUser- user name of the test usertestKey- public key file of the test userrepository- to servehostKey- the unencrypted private key to use as host key- Throws:
java.io.IOExceptionjava.security.GeneralSecurityException- Since:
- 5.9
-
SshTestGitServer
public SshTestGitServer(@NonNull java.lang.String testUser, @NonNull java.security.PublicKey testKey, @NonNull Repository repository, @NonNull java.security.KeyPair hostKey)
Creates a ssh git test server. It serves one single repository, and accepts public-key authentication for exactly one test user.- Parameters:
testUser- user name of the test usertestKey- thePublicKeyof the test userrepository- to servehostKey- theKeyPairto use as host key- Since:
- 5.9
-
-
Method Detail
-
getSignatureFactories
private static java.util.List<org.apache.sshd.common.NamedFactory<org.apache.sshd.common.signature.Signature>> getSignatureFactories()
Apache MINA sshd 2.6.0 has removed DSA, DSA_CERT and RSA_CERT. We have to set it up explicitly to still allow users to connect with DSA keys.- Returns:
- a list of supported signature factories
-
readPublicKey
private static java.security.PublicKey readPublicKey(java.nio.file.Path key) throws java.io.IOException, java.security.GeneralSecurityException- Throws:
java.io.IOExceptionjava.security.GeneralSecurityException
-
readKeyPair
private static java.security.KeyPair readKeyPair(byte[] keyMaterial) throws java.io.IOException, java.security.GeneralSecurityException- Throws:
java.io.IOExceptionjava.security.GeneralSecurityException
-
getAuthFactories
private java.util.List<org.apache.sshd.server.auth.UserAuthFactory> getAuthFactories()
-
configureAuthentication
protected void configureAuthentication()
Configures the authentication mechanisms of this test server. Invoked from the constructor. The default sets up public key authentication for the test user, and a gssapi-with-mic authenticator that pretends to support this mechanism, but that then refuses to authenticate anyone.
-
configureSubsystems
@NonNull protected java.util.List<org.apache.sshd.server.subsystem.SubsystemFactory> configureSubsystems()
Configures the test server's subsystems (sftp, scp). Invoked from the constructor. The default provides a simple SFTP setup with the root directory as the given repository's .git directory's parent. (I.e., at the directory containing the .git directory.)- Returns:
- A possibly empty collection of subsystems.
-
configureShell
protected void configureShell()
Configures shell access for the test server. The default provides no shell at all.
-
addHostKey
public void addHostKey(@NonNull java.nio.file.Path key, boolean inFront) throws java.io.IOException, java.security.GeneralSecurityException
Adds an additional host key to the server.- Parameters:
key- path to the private key file; should not be encryptedinFront- whether to add the new key before other existing keys- Throws:
java.io.IOException- if the file denoted by thePathkeycannot be readjava.security.GeneralSecurityException- if the key contained in the file cannot be read
-
addHostKey
public void addHostKey(@NonNull java.security.KeyPair key, boolean inFront)
Adds an additional host key to the server.- Parameters:
key-KeyPairto addinFront- whether to add the new key before other existing keys- Since:
- 5.8
-
enablePasswordAuthentication
public void enablePasswordAuthentication()
Enable password authentication. The server will accept the test user's name, converted to all upper-case, as password.
-
enableKeyboardInteractiveAuthentication
public void enableKeyboardInteractiveAuthentication()
Enable keyboard-interactive authentication. The server will accept the test user's name, converted to all upper-case, as password.
-
getPropertyResolver
public org.apache.sshd.common.PropertyResolver getPropertyResolver()
Retrieves the server'sPropertyResolver, giving access to server properties.- Returns:
- the
PropertyResolver - Since:
- 5.9
-
start
public int start() throws java.io.IOExceptionStarts the test server, listening on a random port.- Returns:
- the port the server listens on; test clients should connect to that port
- Throws:
java.io.IOException
-
stop
public void stop() throws java.io.IOExceptionStops the test server.- Throws:
java.io.IOException
-
setTestUserPublicKey
public void setTestUserPublicKey(java.nio.file.Path key) throws java.io.IOException, java.security.GeneralSecurityExceptionSets the test user's public key on the server.- Parameters:
key- to set- Throws:
java.io.IOException- if the file cannot be readjava.security.GeneralSecurityException- if the public key cannot be extracted from the file
-
setTestUserPublicKey
public void setTestUserPublicKey(@NonNull java.security.PublicKey key)
Sets the test user's public key on the server.- Parameters:
key- to set- Since:
- 5.8
-
setPreamble
public void setPreamble(java.lang.String... lines)
Sets the lines the server sends before its server identification in the initial protocol version exchange.- Parameters:
lines- to send- Since:
- 5.5
-
-