1 | |
package org.apache.maven.wagon.providers.ssh; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
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 | |
|
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 | |
|
66 | |
|
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 | |
|
77 | |
|
78 | 0 | this.keyAuthz = keyAuthz; |
79 | |
|
80 | 0 | publickeyAuthenticator = new TestPublickeyAuthenticator( this.keyAuthz ); |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
|
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 | |
|
100 | |
|
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 | |
|
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 | |
|
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 | |
} |