View Javadoc

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      private List<String> sshKeysResources = new ArrayList<String>();
56  
57      public TestPublickeyAuthenticator publickeyAuthenticator;
58  
59      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      {
70          this.wagonProtocol = wagonProtocol;
71  
72          this.sshKeysResources = sshKeysResources;
73  
74          this.sshd = SshServer.setUpDefaultServer();
75  
76          //this.sshd.setKeyExchangeFactories(  );
77  
78          this.keyAuthz = keyAuthz;
79  
80          publickeyAuthenticator = new TestPublickeyAuthenticator( this.keyAuthz );
81      }
82  
83      /**
84       * @return random port used
85       */
86      public int start()
87          throws IOException
88      {
89          sshd.setPort( 0 );
90  
91          sshd.setUserAuthFactories( Arrays.asList( new UserAuthPublicKey.Factory(), new UserAuthPassword.Factory() ) );
92  
93          sshd.setPublickeyAuthenticator( this.publickeyAuthenticator );
94  
95          sshd.setPasswordAuthenticator( this.passwordAuthenticator );
96  
97          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         File path = new File( "target/keys" );
103         path.mkdirs();
104         path = new File( path, "simple.key" );
105         path.delete();
106 
107         PEMGeneratorHostKeyProvider provider = new PEMGeneratorHostKeyProvider();
108         provider.setAlgorithm( "RSA" );
109         provider.setKeySize( 1024 );
110         provider.setPath( path.getPath() );
111 
112         sshd.setKeyPairProvider( provider );
113         SessionFactory sessionFactory = new SessionFactory()
114         {
115             @Override
116             protected AbstractSession doCreateSession( IoSession ioSession )
117                 throws Exception
118             {
119                 return super.doCreateSession( ioSession );
120             }
121         };
122         sshd.setSessionFactory( sessionFactory );
123 
124         //sshd.setFileSystemFactory(  );
125 
126         final ProcessShellFactory processShellFactory =
127             new ProcessShellFactory( new String[]{ "/bin/sh", "-i", "-l" } );
128         sshd.setShellFactory( processShellFactory );
129 
130         CommandFactory delegateCommandFactory = new CommandFactory()
131         {
132             public Command createCommand( String command )
133             {
134                 return new ShellCommand( command );
135             }
136         };
137 
138         ScpCommandFactory commandFactory = new ScpCommandFactory( delegateCommandFactory );
139         sshd.setCommandFactory( commandFactory );
140 
141         FileSystemFactory fileSystemFactory = new FileSystemFactory()
142         {
143             public FileSystemView createFileSystemView( Session session )
144                 throws IOException
145             {
146                 return new FileSystemView()
147                 {
148                     public SshFile getFile( String file )
149                     {
150                         file = file.replace( "\\", "" );
151                         file = file.replace( "\"", "" );
152                         File f = new File( FileUtils.normalize( file ) );
153 
154                         return new SshServerEmbedded.TestSshFile( f.getAbsolutePath(), f,
155                                                                   System.getProperty( "user.name" ) );
156                     }
157 
158                     public SshFile getFile( SshFile baseDir, String file )
159                     {
160                         file = file.replace( "\\", "" );
161                         file = file.replace( "\"", "" );
162                         File f = new File( FileUtils.normalize( file ) );
163                         return new SshServerEmbedded.TestSshFile( f.getAbsolutePath(), f,
164                                                                   System.getProperty( "user.name" ) );
165                     }
166                 };
167             }
168         };
169         sshd.setNioWorkers( 0 );
170         //sshd.setScheduledExecutorService(  );
171         sshd.setFileSystemFactory( fileSystemFactory );
172         sshd.start();
173         this.port = sshd.getPort();
174         return this.port;
175     }
176 
177 
178     public void stop()
179         throws InterruptedException
180     {
181         sshd.stop( Boolean.getBoolean( "sshd.stopImmediatly" ) );
182     }
183 
184     public int getPort()
185     {
186         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             super( FileUtils.normalize( fileName ), file, userName );
196         }
197     }
198 
199 }