@Test public void testCreateClientFailsIfPwdReturnsRelativePath() throws Exception { final String remoteRoot = "some/directory/in/my/home/dir"; hostConfig = createWithOverrideUsernameAndPassword(mockJSch); getHostConfig().setRemoteRootDir(remoteRoot); final BapSshCommonConfiguration commonConfiguration = new BapSshCommonConfiguration("Ignore me", null, null, false); getHostConfig().setCommonConfig(commonConfiguration); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockSession.setPassword(TEST_PASSPHRASE); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); mockSftp.connect(getHostConfig().getTimeout()); testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true); mockSftp.cd(getHostConfig().getRemoteRootDir()); expect(mockSftp.pwd()).andReturn("home/bap/" + remoteRoot); expect(mockSftp.isConnected()).andReturn(false); expect(mockSession.isConnected()).andReturn(false); assertCreateClientThrowsException("home/bap/" + remoteRoot); }
@Test public void testCreateClientWillUseKeyIfKeyAndKeyPathPresent() throws Exception { final String testKey = "MyVeryBigKey"; final BapSshCommonConfiguration defaultKeyInfo = new BapSshCommonConfiguration( TEST_PASSPHRASE, testKey, "/this/file/will/not/be/used", false); hostConfig = createWithDefaultKeyInfo(mockJSch, defaultKeyInfo); getHostConfig().setPassword("Ignore me"); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockJSch.addIdentity( isA(String.class), aryEq(BapSshUtil.toBytes(testKey)), (byte[]) isNull(), aryEq(BapSshUtil.toBytes(TEST_PASSPHRASE))); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); mockSftp.connect(getHostConfig().getTimeout()); testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true); mockSftp.cd(getHostConfig().getRemoteRootDir()); assertCreateClient(); }
@Test public void testCreateClientWithOverrideKeyPath() throws Exception { final String testKeyFilename = "myPrivateKey"; final RandomFile theKey = new RandomFile(jenkinsHome.getRoot(), testKeyFilename); hostConfig = createWithOverrideUsernameAndPassword(mockJSch, TEST_PASSPHRASE, testKeyFilename, ""); final BapSshCommonConfiguration commonConfiguration = new BapSshCommonConfiguration("Ignore me", null, null, false); getHostConfig().setCommonConfig(commonConfiguration); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockJSch.addIdentity( isA(String.class), aryEq(theKey.getContents()), (byte[]) isNull(), aryEq(BapSshUtil.toBytes(TEST_PASSPHRASE))); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); mockSftp.connect(getHostConfig().getTimeout()); testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true); mockSftp.cd(getHostConfig().getRemoteRootDir()); assertCreateClient(); }
private BapSshClient assertCreateClientWithDefaultKey(final boolean disableExec) throws Exception { final String testKey = "MyVeryBigKey"; final BapSshCommonConfiguration defaultKeyInfo = new BapSshCommonConfiguration(TEST_PASSPHRASE, testKey, null, disableExec); hostConfig = createWithDefaultKeyInfo(mockJSch, defaultKeyInfo); getHostConfig().setPassword("Ignore me"); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockJSch.addIdentity( isA(String.class), aryEq(BapSshUtil.toBytes(testKey)), (byte[]) isNull(), aryEq(BapSshUtil.toBytes(defaultKeyInfo.getPassphrase()))); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); mockSftp.connect(getHostConfig().getTimeout()); testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true); mockSftp.cd(getHostConfig().getRemoteRootDir()); return assertCreateClient(); }
@Override public ChannelSftp create() throws JSchException { checkConnected(); String channel = "sftp"; sftp = (ChannelSftp) sessionConnection.getSession().openChannel(channel); sftp.connect(); return sftp; }
public void login(String username, String password) throws JSchException { session = jSch.getSession(username, ip, port); session.setPassword(password); //// FIXME: 03/04/2016 Security session.setConfig("StrictHostKeyChecking", "no"); session.connect(); channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(); }
private void copyRecursively(String sourceFolder, String targetFolder) throws MachineException { // create target dir try { int execCode = execAndGetCode("mkdir -p " + targetFolder); if (execCode != 0) { throw new MachineException( format("Creation of folder %s failed. Exit code is %s", targetFolder, execCode)); } } catch (JSchException | IOException e) { throw new MachineException( format("Creation of folder %s failed. Error: %s", targetFolder, e.getLocalizedMessage())); } // not normalized paths don't work final String targetAbsolutePath = getAbsolutePath(targetFolder); // copy files ChannelSftp sftp = null; try { sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(connectionTimeout); final ChannelSftp finalSftp = sftp; Files.walkFileTree( Paths.get(sourceFolder), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { try { if (!attrs.isDirectory()) { copyFile( file.toString(), Paths.get(targetAbsolutePath, file.getFileName().toString()).toString(), finalSftp); } else { finalSftp.mkdir(file.normalize().toString()); } } catch (MachineException | SftpException e) { throw new IOException( format( "Sftp copying of file %s failed. Error: %s", file, e.getLocalizedMessage())); } return FileVisitResult.CONTINUE; } }); } catch (JSchException | IOException e) { throw new MachineException("Copying failed. Error: " + e.getLocalizedMessage()); } finally { if (sftp != null) { sftp.disconnect(); } } }
private void setLastModified(File localFile) throws JSchException { SftpATTRS fileAttributes = null; String remotePath = null; ChannelSftp channel = openSftpChannel(); channel.connect(); try { fileAttributes = channel.lstat(remoteDir(remoteFile) + localFile.getName()); } catch (SftpException e) { throw new JSchException("failed to stat remote file", e); } FileUtils.getFileUtils() .setFileLastModified(localFile, ((long) fileAttributes.getMTime()) * 1000); }
private ChannelSftp getSftp() { checkConnected(); logger.debug("%s@%s:%d: Opening sftp Channel.", username, host, port); ChannelSftp sftp = null; try { sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(); } catch (JSchException e) { throw new SshException( String.format("%s@%s:%d: Error connecting to sftp.", username, host, port), e); } return sftp; }
private void copyFile(String sourcePath, String targetPath) throws MachineException { ChannelSftp sftp = null; try { sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(connectionTimeout); String absoluteTargetPath = getAbsolutePath(targetPath); copyFile(sourcePath, absoluteTargetPath, sftp); } catch (JSchException e) { throw new MachineException("Sftp copying failed. Error: " + e.getLocalizedMessage()); } finally { if (sftp != null) { sftp.disconnect(); } } }
/** * Establish the connection to the server if not yet connected, and listen to ivy events for * closing connection when resolve is finished. Not meant to be used in multi threaded * environment. * * @return the ChannelSftp with which a connection is established * @throws IOException if any connection problem occurs */ private ChannelSftp getSftpChannel(String pathOrUri) throws IOException { Session session = getSession(pathOrUri); String host = session.getHost(); ChannelSftp channel = SshCache.getInstance().getChannelSftp(session); if (channel == null) { try { channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(); Message.verbose(":: SFTP :: connected to " + host + "!"); SshCache.getInstance().attachChannelSftp(session, channel); } catch (JSchException e) { IOException ex = new IOException(e.getMessage()); ex.initCause(e); throw ex; } } return channel; }
public static void main(String[] s) throws IOException, JSchException, SftpException { JSch jsch = new JSch(); Session session = null; session = jsch.getSession("username", "machineIp/hostname", 22); session.setPassword("userpass"); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelSftp channel = null; channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(); String destinationPath = "C:\\logs\\"; channel.cd("/home/username/"); Vector<ChannelSftp.LsEntry> list = channel.ls("*.txt"); for (ChannelSftp.LsEntry entry : list) { channel.get(entry.getFilename(), destinationPath + entry.getFilename()); } channel.disconnect(); session.disconnect(); }
@Test public void failToConnectSftpChanel() throws Exception { hostConfig = createWithOverrideUsernameAndPassword(mockJSch); getHostConfig().setCommonConfig(new BapSshCommonConfiguration("", "", "", false)); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockSession.setPassword(TEST_PASSPHRASE); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); final JSchException exception = new JSchException("meh"); mockSftp.connect(getHostConfig().getTimeout()); expectLastCall().andThrow(exception); expectDisconnect(); assertCreateClientThrowsException(exception); }
@Test public void testSendFileWithSftpException() throws Exception { mockJSch = createMock(JSch.class); mockSession = createMock(Session.class); mockChannelSftp = createMock(ChannelSftp.class); expect(mockJSch.getSession(USERNAME, HOSTNAME, PORT)).andReturn(mockSession); mockSession.setPassword(PASSWORD); mockSession.setConfig(STRICTHOSTKEYCHECKING, STRICTHOSTKEYCHECKING_VALUE); mockSession.connect(); expect(mockSession.openChannel("sftp")).andReturn(mockChannelSftp); mockChannelSftp.connect(); mockChannelSftp.put(isA(InputStream.class), eq(FTPDESTINATIONFILENAME)); expectLastCall().andThrow(new SftpException(1, "Test")); replay(mockJSch, mockSession, mockChannelSftp); sftpClientImpl.setJsch(mockJSch); assertEquals(false, sftpClientImpl.sendFile(FILE_CONTENT)); assertEquals("Error in SftpClient\n", logFactoryMock.getError(true)); }
/** * Executes a the given command inside a short-lived channel in the session. * * <p>Performance could be likely improved by reusing a single channel, though the gains would be * minimal compared to sharing the Session. * * @param session Session in which to create the channel * @return All standard output from the command * @throws JSchException * @throws SftpException * @throws IOException */ private String doFileTransfer(Session session, String src, String dst, SampleResult res) throws JSchException, SftpException, IOException { StringBuilder sb = new StringBuilder(); ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); res.sampleStart(); channel.connect(); if (SFTP_COMMAND_GET.equals(action)) { if (!printFile) { channel.get(src, dst); } else { BufferedReader br = new BufferedReader(new InputStreamReader(channel.get(src))); for (String line = br.readLine(); line != null; line = br.readLine()) { sb.append(line); sb.append("\n"); } } } else if (SFTP_COMMAND_PUT.equals(action)) { channel.put(src, dst); } else if (SFTP_COMMAND_LS.equals(action)) { List<ChannelSftp.LsEntry> ls = channel.ls(src); for (ChannelSftp.LsEntry line : ls) { sb.append(line.getLongname()); sb.append("\n"); } } else if (SFTP_COMMAND_RM.equals(action)) { channel.rm(src); } else if (SFTP_COMMAND_RMDIR.equals(action)) { channel.rmdir(src); } else if (SFTP_COMMAND_RENAME.equals(action)) { channel.rename(src, dst); } res.sampleEnd(); channel.disconnect(); return sb.toString(); }
@Test public void testCreateClientWithDefaultPassword() throws Exception { final BapSshCommonConfiguration defaultKeyInfo = new BapSshCommonConfiguration(TEST_PASSPHRASE, null, null, false); hostConfig = createWithDefaultKeyInfo(mockJSch, defaultKeyInfo); getHostConfig().setPassword("Ignore me"); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockSession.setPassword(defaultKeyInfo.getPassphrase()); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); mockSftp.connect(getHostConfig().getTimeout()); testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true); mockSftp.cd(getHostConfig().getRemoteRootDir()); assertCreateClient(); }
private BapSshClient assertCreateWithDefaultInfo(final String responseFromPwd) throws JSchException, SftpException { final BapSshCommonConfiguration commonConfiguration = new BapSshCommonConfiguration("Ignore me", null, null, false); getHostConfig().setCommonConfig(commonConfiguration); expect( mockJSch.getSession( getHostConfig().getUsername(), getHostConfig().getHostname(), getHostConfig().getPort())) .andReturn(mockSession); mockSession.setPassword(getHostConfig().getPassword()); mockSession.setConfig((Properties) anyObject()); mockSession.connect(getHostConfig().getTimeout()); expect(mockSession.openChannel("sftp")).andReturn(mockSftp); mockSftp.connect(getHostConfig().getTimeout()); testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true); mockSftp.cd(getHostConfig().getRemoteRootDir()); if (responseFromPwd != null) expect(mockSftp.pwd()).andReturn(responseFromPwd); return assertCreateClient(); }
@Override public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException { if (!(dir instanceof SFTPPath)) { throw new IllegalArgumentException(dir.toString()); } SFTPHost sftpHost = (SFTPHost) dir.getFileSystem(); String username = sftpHost.getUserName(); String host = sftpHost.getHost(); int port = sftpHost.getPort(); Session session; try { session = jsch.getSession(username, host, port); UserInfo userinfo = new SFTPUserInfo(sftpHost.getPassword()); session.setUserInfo(userinfo); session.connect(); ChannelSftp sftp = (ChannelSftp) session.openChannel(SFTP); sftp.connect(); SFTPPath sftpPath = (SFTPPath) dir; String dirString = sftpPath.getPathString(); try { sftp.mkdir(dirString); } catch (SftpException e) { throw new IOException(dirString, e); } sftp.quit(); session.disconnect(); // throw new UnsupportedOperationException(); } catch (JSchException e) { throw new FileSystemException(e.getMessage()); } }
@Before public void setup() throws JSchException, SftpException { mockJSch = createMock(JSch.class); mockSession = createMock(Session.class); mockChannelSftp = createMock(ChannelSftp.class); expect(mockJSch.getSession(USERNAME, HOSTNAME, PORT)).andReturn(mockSession); mockSession.setPassword(PASSWORD); mockSession.setConfig(STRICTHOSTKEYCHECKING, STRICTHOSTKEYCHECKING_VALUE); mockSession.connect(); expect(mockSession.openChannel("sftp")).andReturn(mockChannelSftp); mockChannelSftp.connect(); mockChannelSftp.put(isA(InputStream.class), eq(FTPDESTINATIONFILENAME)); mockChannelSftp.disconnect(); mockSession.disconnect(); replay(mockJSch, mockSession, mockChannelSftp); sftpClientImpl.setJsch(mockJSch); sftpClientImpl.setFtpDestinationFileName(FTPDESTINATIONFILENAME); sftpClientImpl.setHostname(HOSTNAME); sftpClientImpl.setPassword(PASSWORD); sftpClientImpl.setUsername(USERNAME); sftpClientImpl.setPort(PORT); logFactoryMock.getError(true); }
public void start() throws Exception { if (!channel.isConnected()) { channel.connect(); } }
public boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException { if (isConnected()) { // already connected return true; } boolean connected = false; int attempt = 0; while (!connected) { try { if (LOG.isTraceEnabled() && attempt > 0) { LOG.trace( "Reconnect attempt #{} connecting to + {}", attempt, configuration.remoteServerInformation()); } if (channel == null || !channel.isConnected()) { if (session == null || !session.isConnected()) { LOG.trace("Session isn't connected, trying to recreate and connect."); session = createSession(configuration); if (endpoint.getConfiguration().getConnectTimeout() > 0) { LOG.trace( "Connecting use connectTimeout: " + endpoint.getConfiguration().getConnectTimeout() + " ..."); session.connect(endpoint.getConfiguration().getConnectTimeout()); } else { LOG.trace("Connecting ..."); session.connect(); } } LOG.trace("Channel isn't connected, trying to recreate and connect."); channel = (ChannelSftp) session.openChannel("sftp"); if (endpoint.getConfiguration().getConnectTimeout() > 0) { LOG.trace( "Connecting use connectTimeout: " + endpoint.getConfiguration().getConnectTimeout() + " ..."); channel.connect(endpoint.getConfiguration().getConnectTimeout()); } else { LOG.trace("Connecting ..."); channel.connect(); } LOG.info("Connected to " + configuration.remoteServerInformation()); } // yes we could connect connected = true; } catch (Exception e) { // check if we are interrupted so we can break out if (Thread.currentThread().isInterrupted()) { throw new GenericFileOperationFailedException( "Interrupted during connecting", new InterruptedException("Interrupted during connecting")); } GenericFileOperationFailedException failed = new GenericFileOperationFailedException( "Cannot connect to " + configuration.remoteServerInformation(), e); LOG.trace("Cannot connect due: {}", failed.getMessage()); attempt++; if (attempt > endpoint.getMaximumReconnectAttempts()) { throw failed; } if (endpoint.getReconnectDelay() > 0) { try { Thread.sleep(endpoint.getReconnectDelay()); } catch (InterruptedException ie) { // we could potentially also be interrupted during sleep Thread.currentThread().interrupt(); throw new GenericFileOperationFailedException("Interrupted during sleeping", ie); } } } } return true; }
@BeforeMethod(groups = "spring") public void initSftp() throws JSchException { sftpChannel = (ChannelSftp) session.openChannel("sftp"); sftpChannel.connect(); }