protected static void connect(ShellTool tool) { if (tool instanceof SshTool) ((SshTool)tool).connect(); }
@Override @Test(enabled=false, groups = {"Integration"}) public void testConnectWithInvalidUserThrowsException() throws Exception { final SshTool localtool = newTool(ImmutableMap.of("user", "wronguser", "host", "localhost", "privateKeyFile", "~/.ssh/id_rsa")); tools.add(localtool); try { localtool.connect(); int result = localtool.execScript(ImmutableMap.<String,Object>of(), ImmutableList.of("date")); fail("exitCode="+result+", but expected exception"); } catch (SshException e) { if (!e.toString().contains("failed to connect")) throw e; } }
@Test(groups = {"Integration"}) public void testSshKeyWithNoKeyDefaultsToIdrsa() throws Exception { final SshTool localtool = newTool(ImmutableMap.<String,Object>builder() .put(SshTool.PROP_HOST.getName(), "localhost") .build()); tools.add(localtool); localtool.connect(); assertEquals(localtool.execScript(MutableMap.<String,Object>of(), ImmutableList.of("date")), 0); }
@Test(groups = {"Integration"}) public void testFlags() throws Exception { final SshTool localtool = newTool(ImmutableMap.of("sshFlags", "-vvv -tt", "host", "localhost")); tools.add(localtool); try { localtool.connect(); Map<String,Object> props = new LinkedHashMap<String, Object>(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); props.put("out", out); props.put("err", err); int exitcode = localtool.execScript(props, Arrays.asList("echo hello err > /dev/stderr"), null); Assert.assertEquals(0, exitcode, "exitCode="+exitcode+", but expected 0"); log.debug("OUT from ssh -vvv command is: "+out); log.debug("ERR from ssh -vvv command is: "+err); assertFalse(err.toString().contains("hello err"), "hello found where it shouldn't have been, in stderr (should have been tty merged to stdout): "+err); assertTrue(out.toString().contains("hello err"), "no hello in stdout: "+err); // look for word 'ssh' to confirm we got verbose output assertTrue(err.toString().toLowerCase().contains("ssh"), "no mention of ssh in stderr: "+err); } catch (SshException e) { if (!e.toString().contains("failed to connect")) throw e; } }
private int execScript(List<String> cmds, boolean captureOutandErr) { ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); MutableMap<String,?> flags = (captureOutandErr) ? MutableMap.of("out", out, "err", err) : MutableMap.<String,Object>of(); tool = newSshTool(MutableMap.of("host", "localhost")); tool.connect(); int result = tool.execScript(flags, cmds); tool.disconnect(); int outlen = out.toByteArray().length; int errlen = out.toByteArray().length; if (LOG.isTraceEnabled()) LOG.trace("Executed: result={}; stdout={}; stderr={}", new Object[] {result, outlen, errlen}); return result; } }
@Test(groups = {"Integration"}) public void testSshKeyWithPassphrase() throws Exception { final SshTool localtool = newTool(ImmutableMap.<String,Object>builder() .put(SshTool.PROP_HOST.getName(), "localhost") .put(SshTool.PROP_PRIVATE_KEY_FILE.getName(), SSH_KEY_WITH_PASSPHRASE) .put(SshTool.PROP_PRIVATE_KEY_PASSPHRASE.getName(), SSH_PASSPHRASE) .build()); localtool.connect(); assertEquals(tool.execScript(MutableMap.<String,Object>of(), ImmutableList.of("date")), 0); // Also needs the negative test to prove that we're really using an ssh-key with a passphrase try { final SshTool localtool2 = newTool(ImmutableMap.<String,Object>builder() .put(SshTool.PROP_HOST.getName(), "localhost") .put(SshTool.PROP_PRIVATE_KEY_FILE.getName(), SSH_KEY_WITH_PASSPHRASE) .build()); localtool2.connect(); fail(); } catch (Exception e) { SshException se = Exceptions.getFirstThrowableOfType(e, SshException.class); if (se == null) throw e; } }
@Override public void run() { tool = newSshTool(MutableMap.of("host", "localhost")); tool.connect(); tool.disconnect(); } };
try { ssh.connect(); } finally { Tasks.setBlockingDetails(null); } previouslyConnected = true; return ssh;
@Test(groups = {"Integration"}) public void testSshKeyWithPrivateKeyData() throws Exception { final SshTool localtool = newTool(ImmutableMap.<String,Object>builder() .put(SshTool.PROP_HOST.getName(), "localhost") .put(SshTool.PROP_PRIVATE_KEY_DATA.getName(), new String(Files.toByteArray(new File(Os.tidyPath(SSH_DEFAULT_KEYFILE))), StandardCharsets.UTF_8)) .build()); localtool.connect(); assertEquals(localtool.execScript(MutableMap.<String,Object>of(), ImmutableList.of("date")), 0); // Also needs the negative test to prove that we're really using an ssh-key with a passphrase try { final SshTool localtool2 = newTool(ImmutableMap.<String,Object>builder() .put(SshTool.PROP_HOST.getName(), "localhost") .put(SshTool.PROP_PRIVATE_KEY_DATA.getName(), "invalid data") .build()); localtool2.connect(); localtool2.execScript(MutableMap.<String,Object>of(), ImmutableList.of("date")); // Notice that executing a command may succeed for SshCliToolIntegrationTest.testSshKeyWithPrivateKeyData if you already have valid keys loaded in the ssh-agent fail(); } catch (Exception e) { SshException se = Exceptions.getFirstThrowableOfType(e, SshException.class); if (se == null) throw e; } }