You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OpenSshConfigTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright (C) 2008, 2017 Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.junit.Assert.assertArrayEquals;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertNotNull;
  49. import static org.junit.Assert.assertNotSame;
  50. import static org.junit.Assert.assertNull;
  51. import static org.junit.Assert.assertTrue;
  52. import java.io.File;
  53. import java.io.FileOutputStream;
  54. import java.io.IOException;
  55. import java.io.OutputStreamWriter;
  56. import java.time.Instant;
  57. import java.util.concurrent.TimeUnit;
  58. import org.eclipse.jgit.junit.RepositoryTestCase;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.transport.OpenSshConfig.Host;
  61. import org.eclipse.jgit.util.FS;
  62. import org.eclipse.jgit.util.FileUtils;
  63. import org.eclipse.jgit.util.SystemReader;
  64. import org.junit.Before;
  65. import org.junit.Test;
  66. import com.jcraft.jsch.ConfigRepository;
  67. import com.jcraft.jsch.ConfigRepository.Config;
  68. public class OpenSshConfigTest extends RepositoryTestCase {
  69. private File home;
  70. private File configFile;
  71. private OpenSshConfig osc;
  72. @Override
  73. @Before
  74. public void setUp() throws Exception {
  75. super.setUp();
  76. home = new File(trash, "home");
  77. FileUtils.mkdir(home);
  78. configFile = new File(new File(home, ".ssh"), Constants.CONFIG);
  79. FileUtils.mkdir(configFile.getParentFile());
  80. mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "jex_junit");
  81. osc = new OpenSshConfig(home, configFile);
  82. }
  83. private void config(String data) throws IOException {
  84. FS fs = FS.DETECTED;
  85. long resolution = FS.getFileStoreAttributes(configFile.toPath())
  86. .getFsTimestampResolution().toNanos();
  87. Instant lastMtime = fs.lastModifiedInstant(configFile);
  88. do {
  89. try (final OutputStreamWriter fw = new OutputStreamWriter(
  90. new FileOutputStream(configFile), UTF_8)) {
  91. fw.write(data);
  92. TimeUnit.NANOSECONDS.sleep(resolution);
  93. } catch (InterruptedException e) {
  94. Thread.interrupted();
  95. }
  96. } while (lastMtime.equals(fs.lastModifiedInstant(configFile)));
  97. }
  98. @Test
  99. public void testNoConfig() {
  100. final Host h = osc.lookup("repo.or.cz");
  101. assertNotNull(h);
  102. assertEquals("repo.or.cz", h.getHostName());
  103. assertEquals("jex_junit", h.getUser());
  104. assertEquals(22, h.getPort());
  105. assertEquals(1, h.getConnectionAttempts());
  106. assertNull(h.getIdentityFile());
  107. }
  108. @Test
  109. public void testSeparatorParsing() throws Exception {
  110. config("Host\tfirst\n" +
  111. "\tHostName\tfirst.tld\n" +
  112. "\n" +
  113. "Host second\n" +
  114. " HostName\tsecond.tld\n" +
  115. "Host=third\n" +
  116. "HostName=third.tld\n\n\n" +
  117. "\t Host = fourth\n\n\n" +
  118. " \t HostName\t=fourth.tld\n" +
  119. "Host\t = last\n" +
  120. "HostName \t last.tld");
  121. assertNotNull(osc.lookup("first"));
  122. assertEquals("first.tld", osc.lookup("first").getHostName());
  123. assertNotNull(osc.lookup("second"));
  124. assertEquals("second.tld", osc.lookup("second").getHostName());
  125. assertNotNull(osc.lookup("third"));
  126. assertEquals("third.tld", osc.lookup("third").getHostName());
  127. assertNotNull(osc.lookup("fourth"));
  128. assertEquals("fourth.tld", osc.lookup("fourth").getHostName());
  129. assertNotNull(osc.lookup("last"));
  130. assertEquals("last.tld", osc.lookup("last").getHostName());
  131. }
  132. @Test
  133. public void testQuoteParsing() throws Exception {
  134. config("Host \"good\"\n" +
  135. " HostName=\"good.tld\"\n" +
  136. " Port=\"6007\"\n" +
  137. " User=\"gooduser\"\n" +
  138. "Host multiple unquoted and \"quoted\" \"hosts\"\n" +
  139. " Port=\"2222\"\n" +
  140. "Host \"spaced\"\n" +
  141. "# Bad host name, but testing preservation of spaces\n" +
  142. " HostName=\" spaced\ttld \"\n" +
  143. "# Misbalanced quotes\n" +
  144. "Host \"bad\"\n" +
  145. "# OpenSSH doesn't allow this but ...\n" +
  146. " HostName=bad.tld\"\n");
  147. assertEquals("good.tld", osc.lookup("good").getHostName());
  148. assertEquals("gooduser", osc.lookup("good").getUser());
  149. assertEquals(6007, osc.lookup("good").getPort());
  150. assertEquals(2222, osc.lookup("multiple").getPort());
  151. assertEquals(2222, osc.lookup("quoted").getPort());
  152. assertEquals(2222, osc.lookup("and").getPort());
  153. assertEquals(2222, osc.lookup("unquoted").getPort());
  154. assertEquals(2222, osc.lookup("hosts").getPort());
  155. assertEquals(" spaced\ttld ", osc.lookup("spaced").getHostName());
  156. assertEquals("bad.tld\"", osc.lookup("bad").getHostName());
  157. }
  158. @Test
  159. public void testCaseInsensitiveKeyLookup() throws Exception {
  160. config("Host orcz\n" + "Port 29418\n"
  161. + "\tHostName repo.or.cz\nStrictHostKeyChecking yes\n");
  162. final Host h = osc.lookup("orcz");
  163. Config c = h.getConfig();
  164. String exactCase = c.getValue("StrictHostKeyChecking");
  165. assertEquals("yes", exactCase);
  166. assertEquals(exactCase, c.getValue("stricthostkeychecking"));
  167. assertEquals(exactCase, c.getValue("STRICTHOSTKEYCHECKING"));
  168. assertEquals(exactCase, c.getValue("sTrIcThostKEYcheckING"));
  169. assertNull(c.getValue("sTrIcThostKEYcheckIN"));
  170. }
  171. @Test
  172. public void testAlias_DoesNotMatch() throws Exception {
  173. config("Host orcz\n" + "Port 29418\n" + "\tHostName repo.or.cz\n");
  174. final Host h = osc.lookup("repo.or.cz");
  175. assertNotNull(h);
  176. assertEquals("repo.or.cz", h.getHostName());
  177. assertEquals("jex_junit", h.getUser());
  178. assertEquals(22, h.getPort());
  179. assertNull(h.getIdentityFile());
  180. final Host h2 = osc.lookup("orcz");
  181. assertEquals("repo.or.cz", h.getHostName());
  182. assertEquals("jex_junit", h.getUser());
  183. assertEquals(29418, h2.getPort());
  184. assertNull(h.getIdentityFile());
  185. }
  186. @Test
  187. public void testAlias_OptionsSet() throws Exception {
  188. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\tPort 2222\n"
  189. + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
  190. + "\tForwardX11 no\n");
  191. final Host h = osc.lookup("orcz");
  192. assertNotNull(h);
  193. assertEquals("repo.or.cz", h.getHostName());
  194. assertEquals("jex", h.getUser());
  195. assertEquals(2222, h.getPort());
  196. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  197. }
  198. @Test
  199. public void testAlias_OptionsKeywordCaseInsensitive() throws Exception {
  200. config("hOsT orcz\n" + "\thOsTnAmE repo.or.cz\n" + "\tPORT 2222\n"
  201. + "\tuser jex\n" + "\tidentityfile .ssh/id_jex\n"
  202. + "\tForwardX11 no\n");
  203. final Host h = osc.lookup("orcz");
  204. assertNotNull(h);
  205. assertEquals("repo.or.cz", h.getHostName());
  206. assertEquals("jex", h.getUser());
  207. assertEquals(2222, h.getPort());
  208. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  209. }
  210. @Test
  211. public void testAlias_OptionsInherit() throws Exception {
  212. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  213. + "\tHostName not.a.host.example.com\n" + "\tPort 2222\n"
  214. + "\tUser jex\n" + "\tIdentityFile .ssh/id_jex\n"
  215. + "\tForwardX11 no\n");
  216. final Host h = osc.lookup("orcz");
  217. assertNotNull(h);
  218. assertEquals("repo.or.cz", h.getHostName());
  219. assertEquals("jex", h.getUser());
  220. assertEquals(2222, h.getPort());
  221. assertEquals(new File(home, ".ssh/id_jex"), h.getIdentityFile());
  222. }
  223. @Test
  224. public void testAlias_PreferredAuthenticationsDefault() throws Exception {
  225. final Host h = osc.lookup("orcz");
  226. assertNotNull(h);
  227. assertNull(h.getPreferredAuthentications());
  228. }
  229. @Test
  230. public void testAlias_PreferredAuthentications() throws Exception {
  231. config("Host orcz\n" + "\tPreferredAuthentications publickey\n");
  232. final Host h = osc.lookup("orcz");
  233. assertNotNull(h);
  234. assertEquals("publickey", h.getPreferredAuthentications());
  235. }
  236. @Test
  237. public void testAlias_InheritPreferredAuthentications() throws Exception {
  238. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  239. + "\tPreferredAuthentications publickey, hostbased\n");
  240. final Host h = osc.lookup("orcz");
  241. assertNotNull(h);
  242. assertEquals("publickey,hostbased", h.getPreferredAuthentications());
  243. }
  244. @Test
  245. public void testAlias_BatchModeDefault() throws Exception {
  246. final Host h = osc.lookup("orcz");
  247. assertNotNull(h);
  248. assertFalse(h.isBatchMode());
  249. }
  250. @Test
  251. public void testAlias_BatchModeYes() throws Exception {
  252. config("Host orcz\n" + "\tBatchMode yes\n");
  253. final Host h = osc.lookup("orcz");
  254. assertNotNull(h);
  255. assertTrue(h.isBatchMode());
  256. }
  257. @Test
  258. public void testAlias_InheritBatchMode() throws Exception {
  259. config("Host orcz\n" + "\tHostName repo.or.cz\n" + "\n" + "Host *\n"
  260. + "\tBatchMode yes\n");
  261. final Host h = osc.lookup("orcz");
  262. assertNotNull(h);
  263. assertTrue(h.isBatchMode());
  264. }
  265. @Test
  266. public void testAlias_ConnectionAttemptsDefault() throws Exception {
  267. final Host h = osc.lookup("orcz");
  268. assertNotNull(h);
  269. assertEquals(1, h.getConnectionAttempts());
  270. }
  271. @Test
  272. public void testAlias_ConnectionAttempts() throws Exception {
  273. config("Host orcz\n" + "\tConnectionAttempts 5\n");
  274. final Host h = osc.lookup("orcz");
  275. assertNotNull(h);
  276. assertEquals(5, h.getConnectionAttempts());
  277. }
  278. @Test
  279. public void testAlias_invalidConnectionAttempts() throws Exception {
  280. config("Host orcz\n" + "\tConnectionAttempts -1\n");
  281. final Host h = osc.lookup("orcz");
  282. assertNotNull(h);
  283. assertEquals(1, h.getConnectionAttempts());
  284. }
  285. @Test
  286. public void testAlias_badConnectionAttempts() throws Exception {
  287. config("Host orcz\n" + "\tConnectionAttempts xxx\n");
  288. final Host h = osc.lookup("orcz");
  289. assertNotNull(h);
  290. assertEquals(1, h.getConnectionAttempts());
  291. }
  292. @Test
  293. public void testDefaultBlock() throws Exception {
  294. config("ConnectionAttempts 5\n\nHost orcz\nConnectionAttempts 3\n");
  295. final Host h = osc.lookup("orcz");
  296. assertNotNull(h);
  297. assertEquals(5, h.getConnectionAttempts());
  298. }
  299. @Test
  300. public void testHostCaseInsensitive() throws Exception {
  301. config("hOsT orcz\nConnectionAttempts 3\n");
  302. final Host h = osc.lookup("orcz");
  303. assertNotNull(h);
  304. assertEquals(3, h.getConnectionAttempts());
  305. }
  306. @Test
  307. public void testListValueSingle() throws Exception {
  308. config("Host orcz\nUserKnownHostsFile /foo/bar\n");
  309. final ConfigRepository.Config c = osc.getConfig("orcz");
  310. assertNotNull(c);
  311. assertEquals("/foo/bar", c.getValue("UserKnownHostsFile"));
  312. }
  313. @Test
  314. public void testListValueMultiple() throws Exception {
  315. // Tilde expansion occurs within the parser
  316. config("Host orcz\nUserKnownHostsFile \"~/foo/ba z\" /foo/bar \n");
  317. final ConfigRepository.Config c = osc.getConfig("orcz");
  318. assertNotNull(c);
  319. assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
  320. "/foo/bar" },
  321. c.getValues("UserKnownHostsFile"));
  322. }
  323. @Test
  324. public void testRepeatedLookupsWithModification() throws Exception {
  325. config("Host orcz\n" + "\tConnectionAttempts -1\n");
  326. final Host h1 = osc.lookup("orcz");
  327. assertNotNull(h1);
  328. assertEquals(1, h1.getConnectionAttempts());
  329. config("Host orcz\n" + "\tConnectionAttempts 5\n");
  330. final Host h2 = osc.lookup("orcz");
  331. assertNotNull(h2);
  332. assertNotSame(h1, h2);
  333. assertEquals(5, h2.getConnectionAttempts());
  334. assertEquals(1, h1.getConnectionAttempts());
  335. assertNotSame(h1.getConfig(), h2.getConfig());
  336. }
  337. @Test
  338. public void testIdentityFile() throws Exception {
  339. config("Host orcz\nIdentityFile \"~/foo/ba z\"\nIdentityFile /foo/bar");
  340. final Host h = osc.lookup("orcz");
  341. assertNotNull(h);
  342. File f = h.getIdentityFile();
  343. assertNotNull(f);
  344. // Host does tilde replacement
  345. assertEquals(new File(home, "foo/ba z"), f);
  346. final ConfigRepository.Config c = h.getConfig();
  347. // Config does tilde replacement, too
  348. assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
  349. "/foo/bar" },
  350. c.getValues("IdentityFile"));
  351. }
  352. @Test
  353. public void testMultiIdentityFile() throws Exception {
  354. config("IdentityFile \"~/foo/ba z\"\nHost orcz\nIdentityFile /foo/bar\nHOST *\nIdentityFile /foo/baz");
  355. final Host h = osc.lookup("orcz");
  356. assertNotNull(h);
  357. File f = h.getIdentityFile();
  358. assertNotNull(f);
  359. // Host does tilde replacement
  360. assertEquals(new File(home, "foo/ba z"), f);
  361. final ConfigRepository.Config c = h.getConfig();
  362. // Config does tilde replacement, too
  363. assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
  364. "/foo/bar", "/foo/baz" },
  365. c.getValues("IdentityFile"));
  366. }
  367. @Test
  368. public void testNegatedPattern() throws Exception {
  369. config("Host repo.or.cz\nIdentityFile ~/foo/bar\nHOST !*.or.cz\nIdentityFile /foo/baz");
  370. final Host h = osc.lookup("repo.or.cz");
  371. assertNotNull(h);
  372. assertEquals(new File(home, "foo/bar"), h.getIdentityFile());
  373. assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath() },
  374. h.getConfig().getValues("IdentityFile"));
  375. }
  376. @Test
  377. public void testPattern() throws Exception {
  378. config("Host repo.or.cz\nIdentityFile ~/foo/bar\nHOST *.or.cz\nIdentityFile /foo/baz");
  379. final Host h = osc.lookup("repo.or.cz");
  380. assertNotNull(h);
  381. assertEquals(new File(home, "foo/bar"), h.getIdentityFile());
  382. assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath(),
  383. "/foo/baz" },
  384. h.getConfig().getValues("IdentityFile"));
  385. }
  386. @Test
  387. public void testMultiHost() throws Exception {
  388. config("Host orcz *.or.cz\nIdentityFile ~/foo/bar\nHOST *.or.cz\nIdentityFile /foo/baz");
  389. final Host h1 = osc.lookup("repo.or.cz");
  390. assertNotNull(h1);
  391. assertEquals(new File(home, "foo/bar"), h1.getIdentityFile());
  392. assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath(),
  393. "/foo/baz" },
  394. h1.getConfig().getValues("IdentityFile"));
  395. final Host h2 = osc.lookup("orcz");
  396. assertNotNull(h2);
  397. assertEquals(new File(home, "foo/bar"), h2.getIdentityFile());
  398. assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath() },
  399. h2.getConfig().getValues("IdentityFile"));
  400. }
  401. @Test
  402. public void testEqualsSign() throws Exception {
  403. config("Host=orcz\n\tConnectionAttempts = 5\n\tUser=\t foobar\t\n");
  404. final Host h = osc.lookup("orcz");
  405. assertNotNull(h);
  406. assertEquals(5, h.getConnectionAttempts());
  407. assertEquals("foobar", h.getUser());
  408. }
  409. @Test
  410. public void testMissingArgument() throws Exception {
  411. config("Host=orcz\n\tSendEnv\nIdentityFile\t\nForwardX11\n\tUser=\t foobar\t\n");
  412. final Host h = osc.lookup("orcz");
  413. assertNotNull(h);
  414. assertEquals("foobar", h.getUser());
  415. assertArrayEquals(new String[0], h.getConfig().getValues("SendEnv"));
  416. assertNull(h.getIdentityFile());
  417. assertNull(h.getConfig().getValue("ForwardX11"));
  418. }
  419. @Test
  420. public void testHomeDirUserReplacement() throws Exception {
  421. config("Host=orcz\n\tIdentityFile %d/.ssh/%u_id_dsa");
  422. final Host h = osc.lookup("orcz");
  423. assertNotNull(h);
  424. assertEquals(new File(new File(home, ".ssh"), "jex_junit_id_dsa"),
  425. h.getIdentityFile());
  426. }
  427. @Test
  428. public void testHostnameReplacement() throws Exception {
  429. config("Host=orcz\nHost *.*\n\tHostname %h\nHost *\n\tHostname %h.example.org");
  430. final Host h = osc.lookup("orcz");
  431. assertNotNull(h);
  432. assertEquals("orcz.example.org", h.getHostName());
  433. }
  434. @Test
  435. public void testRemoteUserReplacement() throws Exception {
  436. config("Host=orcz\n\tUser foo\n" + "Host *.*\n\tHostname %h\n"
  437. + "Host *\n\tHostname %h.ex%%20ample.org\n\tIdentityFile ~/.ssh/%h_%r_id_dsa");
  438. final Host h = osc.lookup("orcz");
  439. assertNotNull(h);
  440. assertEquals(
  441. new File(new File(home, ".ssh"),
  442. "orcz.ex%20ample.org_foo_id_dsa"),
  443. h.getIdentityFile());
  444. }
  445. @Test
  446. public void testLocalhostFQDNReplacement() throws Exception {
  447. String localhost = SystemReader.getInstance().getHostname();
  448. config("Host=orcz\n\tIdentityFile ~/.ssh/%l_id_dsa");
  449. final Host h = osc.lookup("orcz");
  450. assertNotNull(h);
  451. assertEquals(
  452. new File(new File(home, ".ssh"), localhost + "_id_dsa"),
  453. h.getIdentityFile());
  454. }
  455. }