Class GitSshServer


  • public class GitSshServer
    extends java.lang.Object
    Local Git SSH server for testing purposes. It acts on top of an existing repository root directory. It uses Apache MINA SSHD for the SSH server implementation.

    The server generates a key pair during initialization and accepts connections using the private key which can be extracted via writePrivateKeyAsPkcs8(Path, String). Alternatively one may use a custom key pair and add the public key using addPublicKey(PublicKey).

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.List<java.security.PublicKey> acceptedPublicKeys  
      protected java.security.KeyPair keyPair  
      protected org.apache.sshd.server.SshServer sshServer  
    • Constructor Summary

      Constructors 
      Constructor Description
      GitSshServer()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addPublicKey​(java.security.PublicKey publicKey)  
      int getPort()  
      void start​(java.nio.file.Path repositoryRoot)  
      void stop()  
      void writePrivateKeyAsPkcs8​(java.nio.file.Path file, java.lang.String passphrase)
      Writes a private key which is accepted by this server to the specified file in PKCS8 format.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • sshServer

        protected final org.apache.sshd.server.SshServer sshServer
      • keyPair

        protected final java.security.KeyPair keyPair
      • acceptedPublicKeys

        protected final java.util.List<java.security.PublicKey> acceptedPublicKeys
    • Constructor Detail

      • GitSshServer

        public GitSshServer()
                     throws java.security.GeneralSecurityException
        Throws:
        java.security.GeneralSecurityException
    • Method Detail

      • writePrivateKeyAsPkcs8

        public void writePrivateKeyAsPkcs8​(java.nio.file.Path file,
                                           java.lang.String passphrase)
                                    throws java.io.IOException,
                                           java.security.GeneralSecurityException
        Writes a private key which is accepted by this server to the specified file in PKCS8 format. If a passphrase is provided, the key will be encrypted using OpenSSH's format. If no passphrase is provided, the key will be written as an unencrypted PKCS8 private key. For the same server instance the private key is always the same, so it can be reused.
        Parameters:
        file - the file to write the private key to
        passphrase - the passphrase for encryption, or null for unencrypted
        Throws:
        java.security.GeneralSecurityException - if a security error occurs
        java.io.IOException - if an I/O error occurs
      • addPublicKey

        public void addPublicKey​(java.security.PublicKey publicKey)
      • getPort

        public int getPort()
      • start

        public void start​(java.nio.file.Path repositoryRoot)
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • stop

        public void stop()
                  throws java.io.IOException
        Throws:
        java.io.IOException