Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1f5fef3cd9e12d3d330f6074721d7daf27c1b95 (plain) (blame)
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
 * Copyright (C) 2018, 2020 Thomas Wolf <thomas.wolf@paranor.ch> 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.transport.sshd;

import static org.apache.sshd.core.CoreModuleProperties.MAX_CONCURRENT_SESSIONS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.sshd.client.config.hosts.KnownHostEntry;
import org.apache.sshd.client.config.hosts.KnownHostHashValue;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.config.keys.AuthorizedKeyEntry;
import org.apache.sshd.common.config.keys.KeyUtils;
import org.apache.sshd.common.config.keys.PublicKeyEntry;
import org.apache.sshd.common.config.keys.PublicKeyEntryResolver;
import org.apache.sshd.common.kex.BuiltinDHFactories;
import org.apache.sshd.common.kex.DHFactory;
import org.apache.sshd.common.kex.KeyExchangeFactory;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.util.net.SshdSocketAddress;
import org.apache.sshd.server.ServerAuthenticationManager;
import org.apache.sshd.server.ServerBuilder;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.forward.StaticDecisionForwardingFilter;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.junit.ssh.SshTestBase;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.transport.RemoteSession;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.util.FS;
import org.junit.Test;
import org.junit.experimental.theories.Theories;
import org.junit.runner.RunWith;

@RunWith(Theories.class)
public class ApacheSshTest extends SshTestBase {

	@Override
	protected SshSessionFactory createSessionFactory() {
		SshdSessionFactory result = new SshdSessionFactory(new JGitKeyCache(),
				null);
		// The home directory is mocked at this point!
		result.setHomeDirectory(FS.DETECTED.userHome());
		result.setSshDirectory(sshDir);
		return result;
	}

	@Override
	protected void installConfig(String... config) {
		File configFile = new File(sshDir, Constants.CONFIG);
		if (config != null) {
			try {
				Files.write(configFile.toPath(), Arrays.asList(config));
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}
	}

	@Test
	public void testEd25519HostKey() throws Exception {
		// Using ed25519 user identities is tested in the super class in
		// testSshKeys().
		File newHostKey = new File(getTemporaryDirectory(), "newhostkey");
		copyTestResource("id_ed25519", newHostKey);
		server.addHostKey(newHostKey.toPath(), true);
		File newHostKeyPub = new File(getTemporaryDirectory(),
				"newhostkey.pub");
		copyTestResource("id_ed25519.pub", newHostKeyPub);
		createKnownHostsFile(knownHosts, "localhost", testPort, newHostKeyPub);
		cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
				"Host git", //
				"HostName localhost", //
				"Port " + testPort, //
				"User " + TEST_USER, //
				"IdentityFile " + privateKey1.getAbsolutePath());
	}

	@Test
	public void testHashedKnownHosts() throws Exception {
		assertTrue("Failed to delete known_hosts", knownHosts.delete());
		// The provider will answer "yes" to all questions, so we should be able
		// to connect and end up with a new known_hosts file with the host key.
		TestCredentialsProvider provider = new TestCredentialsProvider();
		cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
				"HashKnownHosts yes", //
				"Host localhost", //
				"HostName localhost", //
				"Port " + testPort, //
				"User " + TEST_USER, //
				"IdentityFile " + privateKey1.getAbsolutePath());
		List<LogEntry> messages = provider.getLog();
		assertFalse("Expected user interaction", messages.isEmpty());
		assertEquals(
				"Expected to be asked about the key, and the file creation", 2,
				messages.size());
		assertTrue("~/.ssh/known_hosts should exist now", knownHosts.exists());
		// Let's clone again without provider. If it works, the server host key
		// was written correctly.
		File clonedAgain = new File(getTemporaryDirectory(), "cloned2");
		cloneWith("ssh://localhost/doesntmatter", clonedAgain, null, //
				"Host localhost", //
				"HostName localhost", //
				"Port " + testPort, //
				"User " + TEST_USER, //
				"IdentityFile " + privateKey1.getAbsolutePath());
		// Check that the first line contains neither "localhost" nor
		// "127.0.0.1", but does contain the expected hash.
		List<String> lines = Files.readAllLines(knownHosts.toPath()).stream()
				.filter(s -> s != null && s.length() >= 1 && s.charAt(0) != '#'
						&& !s.trim().isEmpty())
				.collect(Collectors.toList());
		assertEquals("Unexpected number of known_hosts lines", 1, lines.size());
		String line = lines.get(0);
		assertFalse("Found host in line", line.contains("localhost"));
		assertFalse("Found IP in line", line.contains("127.0.0.1"));
		assertTrue("Hash not found", line.contains("|"));
		KnownHostEntry entry = KnownHostEntry.parseKnownHostEntry(line);
		assertTrue("Hash doesn't match localhost",
				entry.isHostMatch("localhost", testPort)
						|| entry.isHostMatch("127.0.0.1", testPort));
	}

	@Test
	public void testPreamble() throws Exception {
		// Test that the client can deal with strange lines being sent before
		// the server identification string.
		StringBuilder b = new StringBuilder();
		for (int i = 0; i < 257; i++) {
			b.append('a');
		}
		server.setPreamble("A line with a \000 NUL",
				"A long line: " + b.toString());
		cloneWith(
				"ssh://" + TEST_USER + "@localhost:" + testPort
						+ "/doesntmatter",
				defaultCloneDir, null,
				"IdentityFile " + privateKey1.getAbsolutePath());
	}

	@Test
	public void testLongPreamble() throws Exception {
		// Test that the client can deal with a long (about 60k) preamble.
		StringBuilder b = new StringBuilder();
		for (int i = 0; i < 1024; i++) {
			b.append('a');
		}
		String line = b.toString();
		String[] lines = new String[60];
		for (int i = 0; i < lines.length; i++) {
			lines[i] = line;
		}
		server.setPreamble(lines);
		cloneWith(
				"ssh://" + TEST_USER + "@localhost:" + testPort
						+ "/doesntmatter",
				defaultCloneDir, null,
				"IdentityFile " + privateKey1.getAbsolutePath());
	}

	@Test
	public void testHugePreamble() throws Exception {
		// Test that the connection fails when the preamble is longer than 64k.
		StringBuilder b = new StringBuilder();
		for (int i = 0; i < 1024; i++) {
			b.append('a');
		}
		String line = b.toString();
		String[] lines = new String[70];
		for (int i = 0; i < lines.length; i++) {
			lines[i] = line;
		}
		server.setPreamble(lines);
		TransportException e = assertThrows(TransportException.class,
				() -> cloneWith(
						"ssh://" + TEST_USER + "@localhost:" + testPort
								+ "/doesntmatter",
						defaultCloneDir, null,
						"IdentityFile " + privateKey1.getAbsolutePath()));
		// The assertions test that we don't run into bug 565394 / SSHD-1050
		assertFalse(e.getMessage().contains("timeout"));
		assertTrue(e.getMessage().contains("65536")
				|| e.getMessage().contains("closed"));
	}

	/**
	 * Test for SSHD-1028. If the server doesn't close sessions, the second
	 * fetch will fail. Occurs on sshd 2.5.[01].
	 *
	 * @throws Exception
	 *             on errors
	 * @see <a href=
	 *      "https://issues.apache.org/jira/projects/SSHD/issues/SSHD-1028">SSHD-1028</a>
	 */
	@Test
	public void testCloneAndFetchWithSessionLimit() throws Exception {
		MAX_CONCURRENT_SESSIONS
				.set(server.getPropertyResolver(), Integer.valueOf(2));
		File localClone = cloneWith("ssh://localhost/doesntmatter",
				defaultCloneDir, null, //
				"Host localhost", //
				"HostName localhost", //
				"Port " + testPort, //
				"User " + TEST_USER, //
				"IdentityFile " + privateKey1.getAbsolutePath());
		// Fetch a couple of times
		try (Git git = Git.open(localClone)) {
			git.fetch().call();
			git.fetch().call();
		}
	}

	/**
	 * Creates a simple SSH server without git setup.
	 *
	 * @param user
	 *            to accept
	 * @param userKey
	 *            public key of that user at this server
	 * @return the {@link SshServer}, not yet started
	 * @throws Exception
	 */
	private SshServer createServer(String user, File userKey) throws Exception {
		SshServer srv = SshServer.setUpDefaultServer();
		// Give the server its own host key
		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
		generator.initialize(2048);
		KeyPair proxyHostKey = generator.generateKeyPair();
		srv.setKeyPairProvider(
				session -> Collections.singletonList(proxyHostKey));
		// Allow (only) publickey authentication
		srv.setUserAuthFactories(Collections.singletonList(
				ServerAuthenticationManager.DEFAULT_USER_AUTH_PUBLIC_KEY_FACTORY));
		// Install the user's public key
		PublicKey userProxyKey = AuthorizedKeyEntry
				.readAuthorizedKeys(userKey.toPath()).get(0)
				.resolvePublicKey(null, PublicKeyEntryResolver.IGNORING);
		srv.setPublickeyAuthenticator(
				(userName, publicKey, session) -> user.equals(userName)
						&& KeyUtils.compareKeys(userProxyKey, publicKey));
		return srv;
	}

	/**
	 * Writes the server's host key to our knownhosts file.
	 *
	 * @param srv to register
	 * @throws Exception
	 */
	private void registerServer(SshServer srv) throws Exception {
		// Add the proxy's host key to knownhosts
		try (BufferedWriter writer = Files.newBufferedWriter(
				knownHosts.toPath(), StandardCharsets.US_ASCII,
				StandardOpenOption.WRITE, StandardOpenOption.APPEND)) {
			writer.append('\n');
			KnownHostHashValue.appendHostPattern(writer, "localhost",
					srv.getPort());
			writer.append(',');
			KnownHostHashValue.appendHostPattern(writer, "127.0.0.1",
					srv.getPort());
			writer.append(' ');
			PublicKeyEntry.appendPublicKeyEntry(writer,
					srv.getKeyPairProvider().loadKeys(null).iterator().next().getPublic());
			writer.append('\n');
		}
	}

	/**
	 * Creates a simple proxy server. Accepts only publickey authentication from
	 * the given user with the given key, allows all forwardings. Adds the
	 * proxy's host key to {@link #knownHosts}.
	 *
	 * @param user
	 *            to accept
	 * @param userKey
	 *            public key of that user at this server
	 * @param report
	 *            single-element array to report back the forwarded address.
	 * @return the started server
	 * @throws Exception
	 */
	private SshServer createProxy(String user, File userKey,
			SshdSocketAddress[] report) throws Exception {
		SshServer proxy = createServer(user, userKey);
		// Allow forwarding
		proxy.setForwardingFilter(new StaticDecisionForwardingFilter(true) {

			@Override
			protected boolean checkAcceptance(String request, Session session,
					SshdSocketAddress target) {
				report[0] = target;
				return super.checkAcceptance(request, session, target);
			}
		});
		proxy.start();
		registerServer(proxy);
		return proxy;
	}

	@Test
	public void testJumpHost() throws Exception {
		SshdSocketAddress[] forwarded = { null };
		try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
				forwarded)) {
			try {
				// Now try to clone via the proxy
				cloneWith("ssh://server/doesntmatter", defaultCloneDir, null, //
						"Host server", //
						"HostName localhost", //
						"Port " + testPort, //
						"User " + TEST_USER, //
						"IdentityFile " + privateKey1.getAbsolutePath(), //
						"ProxyJump " + TEST_USER + "X@proxy:" + proxy.getPort(), //
						"", //
						"Host proxy", //
						"Hostname localhost", //
						"IdentityFile " + privateKey2.getAbsolutePath());
				assertNotNull(forwarded[0]);
				assertEquals(testPort, forwarded[0].getPort());
			} finally {
				proxy.stop();
			}
		}
	}

	@Test
	public void testJumpHostWrongKeyAtProxy() throws Exception {
		// Test that we find the proxy server's URI in the exception message
		SshdSocketAddress[] forwarded = { null };
		try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
				forwarded)) {
			try {
				// Now try to clone via the proxy
				TransportException e = assertThrows(TransportException.class,
						() -> cloneWith("ssh://server/doesntmatter",
								defaultCloneDir, null, //
								"Host server", //
								"HostName localhost", //
								"Port " + testPort, //
								"User " + TEST_USER, //
								"IdentityFile " + privateKey1.getAbsolutePath(),
								"ProxyJump " + TEST_USER + "X@proxy:"
										+ proxy.getPort(), //
								"", //
								"Host proxy", //
								"Hostname localhost", //
								"IdentityFile "
										+ privateKey1.getAbsolutePath()));
				String message = e.getMessage();
				assertTrue(message.contains("localhost:" + proxy.getPort()));
				assertTrue(message.contains("proxy:" + proxy.getPort()));
			} finally {
				proxy.stop();
			}
		}
	}

	@Test
	public void testJumpHostWrongKeyAtServer() throws Exception {
		// Test that we find the target server's URI in the exception message
		SshdSocketAddress[] forwarded = { null };
		try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
				forwarded)) {
			try {
				// Now try to clone via the proxy
				TransportException e = assertThrows(TransportException.class,
						() -> cloneWith("ssh://server/doesntmatter",
								defaultCloneDir, null, //
								"Host server", //
								"HostName localhost", //
								"Port " + testPort, //
								"User " + TEST_USER, //
								"IdentityFile " + privateKey2.getAbsolutePath(),
								"ProxyJump " + TEST_USER + "X@proxy:"
										+ proxy.getPort(), //
								"", //
								"Host proxy", //
								"Hostname localhost", //
								"IdentityFile "
										+ privateKey2.getAbsolutePath()));
				String message = e.getMessage();
				assertTrue(message.contains("localhost:" + testPort));
				assertTrue(message.contains("ssh://server"));
			} finally {
				proxy.stop();
			}
		}
	}

	@Test
	public void testJumpHostNonSsh() throws Exception {
		SshdSocketAddress[] forwarded = { null };
		try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
				forwarded)) {
			try {
				TransportException e = assertThrows(TransportException.class,
						() -> cloneWith("ssh://server/doesntmatter",
								defaultCloneDir, null, //
								"Host server", //
								"HostName localhost", //
								"Port " + testPort, //
								"User " + TEST_USER, //
								"IdentityFile " + privateKey1.getAbsolutePath(), //
								"ProxyJump http://" + TEST_USER + "X@proxy:"
										+ proxy.getPort(), //
								"", //
								"Host proxy", //
								"Hostname localhost", //
								"IdentityFile "
										+ privateKey2.getAbsolutePath()));
				// Find the expected message
				Throwable t = e;
				while (t != null) {
					if (t instanceof URISyntaxException) {
						break;
					}
					t = t.getCause();
				}
				assertNotNull(t);
				assertTrue(t.getMessage().contains("Non-ssh"));
			} finally {
				proxy.stop();
			}
		}
	}

	@Test
	public void testJumpHostWithPath() throws Exception {
		SshdSocketAddress[] forwarded = { null };
		try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
				forwarded)) {
			try {
				TransportException e = assertThrows(TransportException.class,
						() -> cloneWith("ssh://server/doesntmatter",
								defaultCloneDir, null, //
								"Host server", //
								"HostName localhost", //
								"Port " + testPort, //
								"User " + TEST_USER, //
								"IdentityFile " + privateKey1.getAbsolutePath(), //
								"ProxyJump ssh://" + TEST_USER + "X@proxy:"
										+ proxy.getPort() + "/wrongPath", //
								"", //
								"Host proxy", //
								"Hostname localhost", //
								"IdentityFile "
										+ privateKey2.getAbsolutePath()));
				// Find the expected message
				Throwable t = e;
				while (t != null) {
					if (t instanceof URISyntaxException) {
						break;
					}
					t = t.getCause();
				}
				assertNotNull(t);
				assertTrue(t.getMessage().contains("wrongPath"));
			} finally {
				proxy.stop();
			}
		}
	}

	@Test
	public void testJumpHostWithPathShort() throws Exception {
		SshdSocketAddress[] forwarded = { null };
		try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
				forwarded)) {
			try {
				TransportException e = assertThrows(TransportException.class,
						() -> cloneWith("ssh://server/doesntmatter",
								defaultCloneDir, null, //
								"Host server", //
								"HostName localhost", //
								"Port " + testPort, //
								"User " + TEST_USER, //
								"IdentityFile " + privateKey1.getAbsolutePath(), //
								"ProxyJump " + TEST_USER + "X@proxy:wrongPath", //
								"", //
								"Host proxy", //
								"Hostname localhost", //
								"Port " + proxy.getPort(), //
								"IdentityFile "
										+ privateKey2.getAbsolutePath()));
				// Find the expected message
				Throwable t = e;
				while (t != null) {
					if (t instanceof URISyntaxException) {
						break;
					}
					t = t.getCause();
				}
				assertNotNull(t);
				assertTrue(t.getMessage().contains("wrongPath"));
			} finally {
				proxy.stop();
			}
		}
	}

	@Test
	public void testJumpHostChain() throws Exception {
		SshdSocketAddress[] forwarded1 = { null };
		SshdSocketAddress[] forwarded2 = { null };
		try (SshServer proxy1 = createProxy(TEST_USER + 'X', publicKey2,
				forwarded1);
				SshServer proxy2 = createProxy("foo", publicKey1, forwarded2)) {
			try {
				// Clone proxy1 -> proxy2 -> server
				cloneWith("ssh://server/doesntmatter", defaultCloneDir, null, //
						"Host server", //
						"HostName localhost", //
						"Port " + testPort, //
						"User " + TEST_USER, //
						"IdentityFile " + privateKey1.getAbsolutePath(), //
						"ProxyJump proxy2," + TEST_USER + "X@proxy:"
								+ proxy1.getPort(), //
						"", //
						"Host proxy", //
						"Hostname localhost", //
						"IdentityFile " + privateKey2.getAbsolutePath(), //
						"", //
						"Host proxy2", //
						"Hostname localhost", //
						"User foo", //
						"Port " + proxy2.getPort(), //
						"IdentityFile " + privateKey1.getAbsolutePath());
				assertNotNull(forwarded1[0]);
				assertEquals(proxy2.getPort(), forwarded1[0].getPort());
				assertNotNull(forwarded2[0]);
				assertEquals(testPort, forwarded2[0].getPort());
			} finally {
				proxy1.stop();
				proxy2.stop();
			}
		}
	}

	@Test
	public void testJumpHostCascade() throws Exception {
		SshdSocketAddress[] forwarded1 = { null };
		SshdSocketAddress[] forwarded2 = { null };
		try (SshServer proxy1 = createProxy(TEST_USER + 'X', publicKey2,
				forwarded1);
				SshServer proxy2 = createProxy("foo", publicKey1, forwarded2)) {
			try {
				// Clone proxy2 -> proxy1 -> server
				cloneWith("ssh://server/doesntmatter", defaultCloneDir, null, //
						"Host server", //
						"HostName localhost", //
						"Port " + testPort, //
						"User " + TEST_USER, //
						"IdentityFile " + privateKey1.getAbsolutePath(), //
						"ProxyJump " + TEST_USER + "X@proxy", //
						"", //
						"Host proxy", //
						"Hostname localhost", //
						"Port " + proxy1.getPort(), //
						"ProxyJump ssh://proxy2:" + proxy2.getPort(), //
						"IdentityFile " + privateKey2.getAbsolutePath(), //
						"", //
						"Host proxy2", //
						"Hostname localhost", //
						"User foo", //
						"IdentityFile " + privateKey1.getAbsolutePath());
				assertNotNull(forwarded1[0]);
				assertEquals(testPort, forwarded1[0].getPort());
				assertNotNull(forwarded2[0]);
				assertEquals(proxy1.getPort(), forwarded2[0].getPort());
			} finally {
				proxy1.stop();
				proxy2.stop();
			}
		}
	}

	@Test
	public void testJumpHostRecursion() throws Exception {
		SshdSocketAddress[] forwarded1 = { null };
		SshdSocketAddress[] forwarded2 = { null };
		try (SshServer proxy1 = createProxy(TEST_USER + 'X', publicKey2,
				forwarded1);
				SshServer proxy2 = createProxy("foo", publicKey1, forwarded2)) {
			try {
				TransportException e = assertThrows(TransportException.class,
						() -> cloneWith(
						"ssh://server/doesntmatter", defaultCloneDir, null, //
						"Host server", //
						"HostName localhost", //
						"Port " + testPort, //
						"User " + TEST_USER, //
						"IdentityFile " + privateKey1.getAbsolutePath(), //
						"ProxyJump " + TEST_USER + "X@proxy", //
						"", //
						"Host proxy", //
						"Hostname localhost", //
						"Port " + proxy1.getPort(), //
						"ProxyJump ssh://proxy2:" + proxy2.getPort(), //
						"IdentityFile " + privateKey2.getAbsolutePath(), //
						"", //
						"Host proxy2", //
						"Hostname localhost", //
						"User foo", //
						"ProxyJump " + TEST_USER + "X@proxy", //
						"IdentityFile " + privateKey1.getAbsolutePath()));
				assertTrue(e.getMessage().contains("proxy"));
			} finally {
				proxy1.stop();
				proxy2.stop();
			}
		}
	}

	/**
	 * Tests that one can log in to an old server that doesn't handle
	 * rsa-sha2-512 if one puts ssh-rsa first in the client's list of public key
	 * signature algorithms.
	 *
	 * @see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=572056">bug
	 *      572056</a>
	 * @throws Exception
	 *             on failure
	 */
	@Test
	public void testConnectAuthSshRsaPubkeyAcceptedAlgorithms()
			throws Exception {
		try (SshServer oldServer = createServer(TEST_USER, publicKey1)) {
			oldServer.setSignatureFactoriesNames("ssh-rsa");
			oldServer.start();
			registerServer(oldServer);
			installConfig("Host server", //
					"HostName localhost", //
					"Port " + oldServer.getPort(), //
					"User " + TEST_USER, //
					"IdentityFile " + privateKey1.getAbsolutePath(), //
					"PubkeyAcceptedAlgorithms ^ssh-rsa");
			RemoteSession session = getSessionFactory().getSession(
					new URIish("ssh://server/doesntmatter"), null, FS.DETECTED,
					10000);
			assertNotNull(session);
			session.disconnect();
		}
	}

	/**
	 * Tests that one can log in to an old server that knows only the ssh-rsa
	 * signature algorithm. The client has by default the list of signature
	 * algorithms for RSA as "rsa-sha2-512,rsa-sha2-256,ssh-rsa". It should try
	 * all three with the single key configured, and finally succeed.
	 * <p>
	 * The re-ordering mechanism (see
	 * {@link #testConnectAuthSshRsaPubkeyAcceptedAlgorithms()}) is still
	 * important; servers may impose a penalty (back-off delay) for subsequent
	 * attempts with signature algorithms unknown to the server. So a user
	 * connecting to such a server and noticing delays may still want to put
	 * ssh-rsa first in the list for that host.
	 * </p>
	 *
	 * @see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=572056">bug
	 *      572056</a>
	 * @throws Exception
	 *             on failure
	 */
	@Test
	public void testConnectAuthSshRsa() throws Exception {
		try (SshServer oldServer = createServer(TEST_USER, publicKey1)) {
			oldServer.setSignatureFactoriesNames("ssh-rsa");
			oldServer.start();
			registerServer(oldServer);
			installConfig("Host server", //
					"HostName localhost", //
					"Port " + oldServer.getPort(), //
					"User " + TEST_USER, //
					"IdentityFile " + privateKey1.getAbsolutePath());
			RemoteSession session = getSessionFactory().getSession(
					new URIish("ssh://server/doesntmatter"), null, FS.DETECTED,
					10000);
			assertNotNull(session);
			session.disconnect();
		}
	}

	/**
	 * Tests that one can log in at an even poorer server that also only has the
	 * SHA1 KEX methods available. Apparently this is the case for at least some
	 * Microsoft TFS instances. The user has to enable the poor KEX methods in
	 * the ssh config explicitly; we don't enable them by default.
	 *
	 * @throws Exception
	 *             on failure
	 */
	@Test
	public void testConnectOnlyRsaSha1() throws Exception {
		try (SshServer oldServer = createServer(TEST_USER, publicKey1)) {
			oldServer.setSignatureFactoriesNames("ssh-rsa");
			List<DHFactory> sha1Factories = BuiltinDHFactories
					.parseDHFactoriesList(
							"diffie-hellman-group1-sha1,diffie-hellman-group14-sha1")
					.getParsedFactories();
			assertEquals(2, sha1Factories.size());
			List<KeyExchangeFactory> kexFactories = NamedFactory
					.setUpTransformedFactories(true, sha1Factories,
							ServerBuilder.DH2KEX);
			oldServer.setKeyExchangeFactories(kexFactories);
			oldServer.start();
			registerServer(oldServer);
			installConfig("Host server", //
					"HostName localhost", //
					"Port " + oldServer.getPort(), //
					"User " + TEST_USER, //
					"IdentityFile " + privateKey1.getAbsolutePath(), //
					"KexAlgorithms +diffie-hellman-group1-sha1");
			RemoteSession session = getSessionFactory().getSession(
					new URIish("ssh://server/doesntmatter"), null, FS.DETECTED,
					10000);
			assertNotNull(session);
			session.disconnect();
		}
	}
}

Back to the top