Coverage Report - org.apache.maven.wagon.providers.ssh.SshServerEmbedded
 
Classes in this File Line Coverage Branch Coverage Complexity
SshServerEmbedded
0 %
0/39
N/A
1
SshServerEmbedded$1
0 %
0/2
N/A
1
SshServerEmbedded$2
0 %
0/2
N/A
1
SshServerEmbedded$3
0 %
0/2
N/A
1
SshServerEmbedded$3$1
0 %
0/9
N/A
1
SshServerEmbedded$TestSshFile
0 %
0/2
N/A
1
 
 1  
 package org.apache.maven.wagon.providers.ssh;
 2  
 /*
 3  
  * Licensed to the Apache Software Foundation (ASF) under one
 4  
  * or more contributor license agreements.  See the NOTICE file
 5  
  * distributed with this work for additional information
 6  
  * regarding copyright ownership.  The ASF licenses this file
 7  
  * to you under the Apache License, Version 2.0 (the
 8  
  * "License"); you may not use this file except in compliance
 9  
  * with the License.  You may obtain a copy of the License at
 10  
  *
 11  
  *   http://www.apache.org/licenses/LICENSE-2.0
 12  
  *
 13  
  * Unless required by applicable law or agreed to in writing,
 14  
  * software distributed under the License is distributed on an
 15  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 16  
  * KIND, either express or implied.  See the License for the
 17  
  * specific language governing permissions and limitations
 18  
  * under the License.
 19  
  */
 20  
 
 21  
 import org.apache.mina.core.session.IoSession;
 22  
 import org.apache.sshd.SshServer;
 23  
 import org.apache.sshd.common.Session;
 24  
 import org.apache.sshd.common.session.AbstractSession;
 25  
 import org.apache.sshd.server.Command;
 26  
 import org.apache.sshd.server.CommandFactory;
 27  
 import org.apache.sshd.server.FileSystemFactory;
 28  
 import org.apache.sshd.server.FileSystemView;
 29  
 import org.apache.sshd.server.SshFile;
 30  
 import org.apache.sshd.server.auth.UserAuthPassword;
 31  
 import org.apache.sshd.server.auth.UserAuthPublicKey;
 32  
 import org.apache.sshd.server.filesystem.NativeSshFile;
 33  
 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
 34  
 import org.apache.sshd.server.session.SessionFactory;
 35  
 import org.apache.sshd.server.shell.ProcessShellFactory;
 36  
 import org.codehaus.plexus.util.FileUtils;
 37  
 
 38  
 import java.io.File;
 39  
 import java.io.IOException;
 40  
 import java.util.ArrayList;
 41  
 import java.util.Arrays;
 42  
 import java.util.List;
 43  
 
 44  
 /**
 45  
  * @author Olivier Lamy
 46  
  */
 47  
 public class SshServerEmbedded
 48  
 {
 49  
     private String wagonProtocol;
 50  
 
 51  
     private int port;
 52  
 
 53  
     private SshServer sshd;
 54  
 
 55  0
     private List<String> sshKeysResources = new ArrayList<String>();
 56  
 
 57  
     public TestPublickeyAuthenticator publickeyAuthenticator;
 58  
 
 59  0
     public TestPasswordAuthenticator passwordAuthenticator = new TestPasswordAuthenticator();
 60  
 
 61  
     private boolean keyAuthz;
 62  
 
 63  
 
 64  
     /**
 65  
      * @param wagonProtocol    scp scpexe
 66  
      * @param sshKeysResources paths in the classlaoder with ssh keys
 67  
      */
 68  
     public SshServerEmbedded( String wagonProtocol, List<String> sshKeysResources, boolean keyAuthz )
 69  0
     {
 70  0
         this.wagonProtocol = wagonProtocol;
 71  
 
 72  0
         this.sshKeysResources = sshKeysResources;
 73  
 
 74  0
         this.sshd = SshServer.setUpDefaultServer();
 75  
 
 76  
         //this.sshd.setKeyExchangeFactories(  );
 77  
 
 78  0
         this.keyAuthz = keyAuthz;
 79  
 
 80  0
         publickeyAuthenticator = new TestPublickeyAuthenticator( this.keyAuthz );
 81  0
     }
 82  
 
 83  
     /**
 84  
      * @return random port used
 85  
      */
 86  
     public int start()
 87  
         throws IOException
 88  
     {
 89  0
         sshd.setPort( 0 );
 90  
 
 91  0
         sshd.setUserAuthFactories( Arrays.asList( new UserAuthPublicKey.Factory(), new UserAuthPassword.Factory() ) );
 92  
 
 93  0
         sshd.setPublickeyAuthenticator( this.publickeyAuthenticator );
 94  
 
 95  0
         sshd.setPasswordAuthenticator( this.passwordAuthenticator );
 96  
 
 97  0
         sshd.setUserAuthFactories( Arrays.asList( new UserAuthPublicKey.Factory(), new UserAuthPassword.Factory() ) );
 98  
 
 99  
         //ResourceKeyPairProvider resourceKeyPairProvider =
 100  
         //    new ResourceKeyPairProvider( sshKeysResources.toArray( new String[sshKeysResources.size()] ) );
 101  
 
 102  0
         File path = new File( "target/keys" );
 103  0
         path.mkdirs();
 104  0
         path = new File( path, "simple.key" );
 105  0
         path.delete();
 106  
 
 107  0
         PEMGeneratorHostKeyProvider provider = new PEMGeneratorHostKeyProvider();
 108  0
         provider.setAlgorithm( "RSA" );
 109  0
         provider.setKeySize( 1024 );
 110  0
         provider.setPath( path.getPath() );
 111  
 
 112  0
         sshd.setKeyPairProvider( provider );
 113  0
         SessionFactory sessionFactory = new SessionFactory()
 114  0
         {
 115  
             @Override
 116  
             protected AbstractSession doCreateSession( IoSession ioSession )
 117  
                 throws Exception
 118  
             {
 119  0
                 return super.doCreateSession( ioSession );
 120  
             }
 121  
         };
 122  0
         sshd.setSessionFactory( sessionFactory );
 123  
 
 124  
         //sshd.setFileSystemFactory(  );
 125  
 
 126  0
         final ProcessShellFactory processShellFactory =
 127  
             new ProcessShellFactory( new String[]{ "/bin/sh", "-i", "-l" } );
 128  0
         sshd.setShellFactory( processShellFactory );
 129  
 
 130  0
         CommandFactory delegateCommandFactory = new CommandFactory()
 131  0
         {
 132  
             public Command createCommand( String command )
 133  
             {
 134  0
                 return new ShellCommand( command );
 135  
             }
 136  
         };
 137  
 
 138  0
         ScpCommandFactory commandFactory = new ScpCommandFactory( delegateCommandFactory );
 139  0
         sshd.setCommandFactory( commandFactory );
 140  
 
 141  0
         FileSystemFactory fileSystemFactory = new FileSystemFactory()
 142  0
         {
 143  
             public FileSystemView createFileSystemView( Session session )
 144  
                 throws IOException
 145  
             {
 146  0
                 return new FileSystemView()
 147  0
                 {
 148  
                     public SshFile getFile( String file )
 149  
                     {
 150  0
                         file = file.replace( "\\", "" );
 151  0
                         file = file.replace( "\"", "" );
 152  0
                         File f = new File( FileUtils.normalize( file ) );
 153  
 
 154  0
                         return new SshServerEmbedded.TestSshFile( f.getAbsolutePath(), f,
 155  
                                                                   System.getProperty( "user.name" ) );
 156  
                     }
 157  
 
 158  
                     public SshFile getFile( SshFile baseDir, String file )
 159  
                     {
 160  0
                         file = file.replace( "\\", "" );
 161  0
                         file = file.replace( "\"", "" );
 162  0
                         File f = new File( FileUtils.normalize( file ) );
 163  0
                         return new SshServerEmbedded.TestSshFile( f.getAbsolutePath(), f,
 164  
                                                                   System.getProperty( "user.name" ) );
 165  
                     }
 166  
                 };
 167  
             }
 168  
         };
 169  0
         sshd.setNioWorkers( 0 );
 170  
         //sshd.setScheduledExecutorService(  );
 171  0
         sshd.setFileSystemFactory( fileSystemFactory );
 172  0
         sshd.start();
 173  0
         this.port = sshd.getPort();
 174  0
         return this.port;
 175  
     }
 176  
 
 177  
 
 178  
     public void stop()
 179  
         throws InterruptedException
 180  
     {
 181  0
         sshd.stop( Boolean.getBoolean( "sshd.stopImmediatly" ) );
 182  0
     }
 183  
 
 184  
     public int getPort()
 185  
     {
 186  0
         return port;
 187  
     }
 188  
 
 189  
     public static class TestSshFile
 190  
         extends NativeSshFile
 191  
     {
 192  
         public TestSshFile( String fileName, File file, String userName )
 193  
         {
 194  
 
 195  0
             super( FileUtils.normalize( fileName ), file, userName );
 196  0
         }
 197  
     }
 198  
 
 199  
 }