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