1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/*
* Copyright (C) 2010, 2017 Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.junit.http;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jetty.security.AbstractLoginService;
import org.eclipse.jetty.security.Authenticator;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.security.RolePrincipal;
import org.eclipse.jetty.security.UserPrincipal;
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.security.Password;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jgit.transport.URIish;
/**
* Tiny web application server for unit testing.
* <p>
* Tests should start the server in their {@code setUp()} method and stop the
* server in their {@code tearDown()} method. Only while started the server's
* URL and/or port number can be obtained.
*/
public class AppServer {
/** Realm name for the secure access areas. */
public static final String realm = "Secure Area";
/** Username for secured access areas. */
public static final String username = "agitter";
/** Password for {@link #username} in secured access areas. */
public static final String password = "letmein";
/** SSL keystore password; must have at least 6 characters. */
private static final String keyPassword = "mykeys";
/** Role for authentication. */
private static final String authRole = "can-access";
static {
// Install a logger that throws warning messages.
//
final String prop = "org.eclipse.jetty.util.log.class";
System.setProperty(prop, RecordingLogger.class.getName());
}
private final Server server;
private final HttpConfiguration config;
private final ServerConnector connector;
private final HttpConfiguration secureConfig;
private final ServerConnector secureConnector;
private final ContextHandlerCollection contexts;
private final TestRequestLog log;
private List<File> filesToDelete = new ArrayList<>();
/**
* Constructor for <code>AppServer</code>.
*/
public AppServer() {
this(0, -1);
}
/**
* Constructor for <code>AppServer</code>.
*
* @param port
* the http port number; may be zero to allocate a port
* dynamically
* @since 4.2
*/
public AppServer(int port) {
this(port, -1);
}
/**
* Constructor for <code>AppServer</code>.
*
* @param port
* for http, may be zero to allocate a port dynamically
* @param sslPort
* for https,may be zero to allocate a port dynamically. If
* negative, the server will be set up without https support.
* @since 4.9
*/
public AppServer(int port, int sslPort) {
server = new Server();
config = new HttpConfiguration();
config.setSecureScheme("https");
config.setSecurePort(0);
config.setOutputBufferSize(32768);
connector = new ServerConnector(server,
new HttpConnectionFactory(config));
connector.setPort(port);
String ip;
String hostName;
try {
final InetAddress me = InetAddress.getByName("localhost");
ip = me.getHostAddress();
connector.setHost(ip);
hostName = InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
throw new RuntimeException("Cannot find localhost", e);
}
if (sslPort >= 0) {
SslContextFactory.Server sslContextFactory = createTestSslContextFactory(
hostName, ip);
secureConfig = new HttpConfiguration(config);
secureConfig.addCustomizer(new SecureRequestCustomizer());
HttpConnectionFactory http11 = new HttpConnectionFactory(
secureConfig);
SslConnectionFactory tls = new SslConnectionFactory(
sslContextFactory, http11.getProtocol());
secureConnector = new ServerConnector(server, tls, http11);
secureConnector.setPort(sslPort);
secureConnector.setHost(ip);
} else {
secureConfig = null;
secureConnector = null;
}
contexts = new ContextHandlerCollection();
log = new TestRequestLog();
log.setHandler(contexts);
if (secureConnector == null) {
server.setConnectors(new Connector[] { connector });
} else {
server.setConnectors(
new Connector[] { connector, secureConnector });
}
server.setHandler(log);
}
private SslContextFactory.Server createTestSslContextFactory(
String hostName, String ip) {
SslContextFactory.Server factory = new SslContextFactory.Server();
String dName = "CN=,OU=,O=,ST=,L=,C=";
try {
File tmpDir = Files.createTempDirectory("jks").toFile();
tmpDir.deleteOnExit();
makePrivate(tmpDir);
File keyStore = new File(tmpDir, "keystore.jks");
File keytool = new File(
new File(new File(System.getProperty("java.home")), "bin"),
"keytool");
Runtime.getRuntime().exec(
new String[] {
keytool.getAbsolutePath(), //
"-keystore", keyStore.getAbsolutePath(), //
"-storepass", keyPassword,
"-alias", hostName, //
"-ext",
String.format("san=IP:%s,DNS:%s", ip, hostName), //
"-genkeypair", //
"-keyalg", "RSA", //
"-keypass", keyPassword, //
"-dname", dName, //
"-validity", "2" //
}).waitFor();
keyStore.deleteOnExit();
makePrivate(keyStore);
filesToDelete.add(keyStore);
filesToDelete.add(tmpDir);
factory.setKeyStorePath(keyStore.getAbsolutePath());
factory.setKeyStorePassword(keyPassword);
factory.setKeyManagerPassword(keyPassword);
factory.setTrustStorePath(keyStore.getAbsolutePath());
factory.setTrustStorePassword(keyPassword);
} catch (InterruptedException | IOException e) {
throw new RuntimeException("Cannot create ssl key/certificate", e);
}
return factory;
}
private void makePrivate(File file) {
file.setReadable(false);
file.setWritable(false);
file.setExecutable(false);
file.setReadable(true, true);
file.setWritable(true, true);
if (file.isDirectory()) {
file.setExecutable(true, true);
}
}
/**
* Create a new servlet context within the server.
* <p>
* This method should be invoked before the server is started, once for each
* context the caller wants to register.
*
* @param path
* path of the context; use "/" for the root context if binding
* to the root is desired.
* @return the context to add servlets into.
*/
public ServletContextHandler addContext(String path) {
assertNotYetSetUp();
if ("".equals(path))
path = "/";
ServletContextHandler ctx = new ServletContextHandler();
ctx.setContextPath(path);
contexts.addHandler(ctx);
return ctx;
}
/**
* Configure basic authentication.
*
* @param ctx
* @param methods
* @return servlet context handler
*/
public ServletContextHandler authBasic(ServletContextHandler ctx,
String... methods) {
assertNotYetSetUp();
auth(ctx, new BasicAuthenticator(), methods);
return ctx;
}
static class TestMappedLoginService extends AbstractLoginService {
private RolePrincipal role;
protected final Map<String, UserPrincipal> users = new ConcurrentHashMap<>();
TestMappedLoginService(String role) {
this.role = new RolePrincipal(role);
}
@Override
protected void doStart() throws Exception {
UserPrincipal p = new UserPrincipal(username,
new Password(password));
users.put(username, p);
super.doStart();
}
@Override
protected UserPrincipal loadUserInfo(String user) {
return users.get(user);
}
@Override
protected List<RolePrincipal> loadRoleInfo(UserPrincipal user) {
if (users.get(user.getName()) == null) {
return null;
}
return Collections.singletonList(role);
}
}
private ConstraintMapping createConstraintMapping() {
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(new Constraint());
cm.getConstraint().setAuthenticate(true);
cm.getConstraint().setDataConstraint(Constraint.DC_NONE);
cm.getConstraint().setRoles(new String[] { authRole });
cm.setPathSpec("/*");
return cm;
}
private void auth(ServletContextHandler ctx, Authenticator authType,
String... methods) {
AbstractLoginService users = new TestMappedLoginService(authRole);
List<ConstraintMapping> mappings = new ArrayList<>();
if (methods == null || methods.length == 0) {
mappings.add(createConstraintMapping());
} else {
for (String method : methods) {
ConstraintMapping cm = createConstraintMapping();
cm.setMethod(method.toUpperCase(Locale.ROOT));
mappings.add(cm);
}
}
ConstraintSecurityHandler sec = new ConstraintSecurityHandler();
sec.setRealmName(realm);
sec.setAuthenticator(authType);
sec.setLoginService(users);
sec.setConstraintMappings(
mappings.toArray(new ConstraintMapping[0]));
sec.setHandler(ctx);
contexts.removeHandler(ctx);
contexts.addHandler(sec);
}
/**
* Start the server on a random local port.
*
* @throws Exception
* the server cannot be started, testing is not possible.
*/
public void setUp() throws Exception {
RecordingLogger.clear();
log.clear();
server.start();
config.setSecurePort(getSecurePort());
if (secureConfig != null) {
secureConfig.setSecurePort(getSecurePort());
}
}
/**
* Shutdown the server.
*
* @throws Exception
* the server refuses to halt, or wasn't running.
*/
public void tearDown() throws Exception {
RecordingLogger.clear();
log.clear();
server.stop();
for (File f : filesToDelete) {
f.delete();
}
filesToDelete.clear();
}
/**
* Get the URI to reference this server.
* <p>
* The returned URI includes the proper host name and port number, but does
* not contain a path.
*
* @return URI to reference this server's root context.
*/
public URI getURI() {
assertAlreadySetUp();
String host = connector.getHost();
if (host.contains(":") && !host.startsWith("["))
host = "[" + host + "]";
final String uri = "http://" + host + ":" + getPort();
try {
return new URI(uri);
} catch (URISyntaxException e) {
throw new RuntimeException("Unexpected URI error on " + uri, e);
}
}
/**
* Get port.
*
* @return the local port number the server is listening on.
*/
public int getPort() {
assertAlreadySetUp();
return connector.getLocalPort();
}
/**
* Get secure port.
*
* @return the HTTPS port or -1 if not configured.
*/
public int getSecurePort() {
assertAlreadySetUp();
return secureConnector != null ? secureConnector.getLocalPort() : -1;
}
/**
* Get requests.
*
* @return all requests since the server was started.
*/
public List<AccessEvent> getRequests() {
return new ArrayList<>(log.getEvents());
}
/**
* Get requests.
*
* @param base
* base URI used to access the server.
* @param path
* the path to locate requests for, relative to {@code base}.
* @return all requests which match the given path.
*/
public List<AccessEvent> getRequests(URIish base, String path) {
return getRequests(HttpTestCase.join(base, path));
}
/**
* Get requests.
*
* @param path
* the path to locate requests for.
* @return all requests which match the given path.
*/
public List<AccessEvent> getRequests(String path) {
ArrayList<AccessEvent> r = new ArrayList<>();
for (AccessEvent event : log.getEvents()) {
if (event.getPath().equals(path)) {
r.add(event);
}
}
return r;
}
private void assertNotYetSetUp() {
assertFalse("server is not running", server.isRunning());
}
private void assertAlreadySetUp() {
assertTrue("server is running", server.isRunning());
}
}
|