/** * Checks if file is a directory * * @param sftpClient * @param filename * @return true, if filename is a directory */ public boolean isDirectory( SFTPv3Client sftpClient, String filename ) { try { return sftpClient.stat( filename ).isDirectory(); } catch ( Exception e ) { // Ignore FTP errors } return false; }
@VisibleForTesting static Connection createConnection( String serveur, int port ) { return new Connection( serveur, port ); } }
public void chmod(String path, int permissions) throws IOException { SFTPv3FileAttributes atts = new SFTPv3FileAttributes(); atts.permissions = permissions; setstat(path, atts); }
conn.setProxyData( new HTTPProxyData( proxyhost, proxyport, proxyusername, proxypassword ) ); } else { conn.setProxyData( new HTTPProxyData( proxyhost, proxyport ) ); conn.connect(); } else { conn.connect( null, 0, timeOut * 1000 ); conn.authenticateWithPublicKey( username, content, space.environmentSubstitute( passPhrase ) ); } else { isAuthenticated = conn.authenticateWithPassword( username, password ); conn.close();
Connection conn = new Connection( realservername, realserverport ); conn.setProxyData( new HTTPProxyData( realproxyhost, realproxyport ) ); .setProxyData( new HTTPProxyData( realproxyhost, realproxyport, realproxyusername, realproxypassword ) ); conn.connect( null, 0, timeout * 1000 ); conn.connect(); isAuthenticated = conn.authenticateWithPublicKey( realUsername, new File( realkeyFilename ), realkeyPass ); } else { isAuthenticated = conn.authenticateWithPassword( realUsername, realserverpassword ); sshclient = new SFTPv3Client( conn );
@SneakyThrows private Connection createSSHSession() { String password = UUID.randomUUID().toString(); container = new GenericContainer<>(TestcontainersConfiguration.getInstance().getSSHdImage()) .withExposedPorts(22) .withEnv("PASSWORD", password) .withCommand( "sh", "-c", // Disable ipv6 & Make it listen on all interfaces, not just localhost "echo \"root:$PASSWORD\" | chpasswd && /usr/sbin/sshd -D -o PermitRootLogin=yes -o AddressFamily=inet -o GatewayPorts=yes" ); container.start(); Connection connection = new Connection(container.getContainerIpAddress(), container.getMappedPort(22)); connection.setTCPNoDelay(true); connection.connect( (hostname, port, serverHostKeyAlgorithm, serverHostKey) -> true, (int) Duration.ofSeconds(30).toMillis(), (int) Duration.ofSeconds(30).toMillis() ); if (!connection.authenticateWithPassword("root", password)) { throw new IllegalStateException("Authentication failed."); } return connection; }
@Test( expected = KettleException.class ) public void testOpenConnection_2() throws Exception { when( connection.authenticateWithPassword( username, password ) ).thenReturn( false ); SSHData.OpenConnection( server, port, username, password, false, null, null, 0, null, null, 0, null, null ); verify( connection ).connect(); verify( connection ).authenticateWithPassword( username, password ); }
@Test public void testOpenConnectionProxy() throws Exception { when( connection.authenticateWithPassword( username, password ) ).thenReturn( true ); assertNotNull( SSHData.OpenConnection( server, port, username, password, false, null, null, 0, null, proxyHost, proxyPort, proxyUsername, proxyPassword ) ); verify( connection ).connect(); verify( connection ).authenticateWithPassword( username, password ); verify( connection ).setProxyData( any( HTTPProxyData.class ) ); }
session = data.conn.openSession(); if ( log.isDebug() ) { logDebug( BaseMessages.getString( PKG, "SSH.Log.SessionOpened" ) ); logDetailed( BaseMessages.getString( PKG, "SSH.Log.RunningCommand", data.commands ) ); session.execCommand( data.commands ); session.close(); if ( log.isDebug() ) { logDebug( BaseMessages.getString( PKG, "SSH.Log.SessionClosed" ) );
@Test public void testOpenConnectionUseKey_2() throws Exception { when( fileObject.exists() ).thenReturn( true ); when( fileObject.getContent() ).thenReturn( fileContent ); when( fileContent.getSize() ).thenReturn( 1000L ); when( fileContent.getInputStream() ).thenReturn( new ByteArrayInputStream( new byte[] { 1, 2, 3, 4, 5 } ) ); when( variableSpace.environmentSubstitute( passPhrase ) ).thenReturn( passPhrase ); when( connection.authenticateWithPublicKey( eq( username ), Matchers.<char[]>any(), eq( passPhrase ) ) ).thenReturn( true ); SSHData.OpenConnection( server, port, username, null, true, keyFilePath, passPhrase, 0, variableSpace, null, 0, null, null ); verify( connection ).connect(); verify( connection ).authenticateWithPublicKey( eq( username ), Matchers.<char[]>any(), eq( passPhrase ) ); }
private void readStd( Session session ) throws KettleException { InputStream isOut = null; InputStream isErr = null; try { isOut = session.getStdout(); isErr = session.getStderr(); setStdOut( readInputStream( isOut ) ); setStdErr( readInputStream( isErr ) ); } catch ( Exception e ) { throw new KettleException( e ); } finally { try { if ( isOut != null ) { isOut.close(); } if ( isErr != null ) { isErr.close(); } } catch ( Exception e ) { /* Ignore */ } } }
private void closeFTPConnections() { // Close FTP connection if necessary if ( ftpclient != null && ftpclient.connected() ) { try { ftpclient.quit(); ftpclient = null; } catch ( Exception e ) { // Ignore close errors } } // Close SecureFTP connection if necessary if ( sftpclient != null ) { try { sftpclient.disconnect(); sftpclient = null; } catch ( Exception e ) { // Ignore close errors } } // Close SSH connection if necessary if ( conn != null ) { try { conn.close(); conn = null; } catch ( Exception e ) { // Ignore close errors } } }
/** * Makes sure that the directory exists, by creating it if necessary. */ public void mkdirs(String path, int posixPermission) throws IOException { SFTPv3FileAttributes atts = _stat(path); if (atts!=null && atts.isDirectory()) return; int idx = path.lastIndexOf("/"); if (idx>0) mkdirs(path.substring(0,idx), posixPermission); try { mkdir(path, posixPermission); } catch (IOException e) { throw new IOException("Failed to mkdir "+path,e); } }
/** * Graceful {@link #stat(String)} that returns null if the path doesn't exist. */ public SFTPv3FileAttributes _stat(String path) throws IOException { try { return stat(path); } catch (SFTPException e) { int c = e.getServerErrorCode(); if (c== ErrorCodes.SSH_FX_NO_SUCH_FILE || c==ErrorCodes.SSH_FX_NO_SUCH_PATH) return null; else throw e; } }
@SneakyThrows public void exposeHostPort(int port) { if (exposedPorts.add(port)) { getSshConnection().requestRemotePortForwarding("", port, "localhost", port); } }
if ( conn == null ) { // Create a connection instance conn = new Connection( jobMeta.environmentSubstitute( wServerName.getText() ), Const.toInt( jobMeta .environmentSubstitute( wPort.getText() ), 22 ) ); conn.setProxyData( new HTTPProxyData( jobMeta.environmentSubstitute( wProxyHost.getText() ), Const.toInt( wProxyPort.getText(), 22 ), jobMeta.environmentSubstitute( wProxyUsername.getText() ), Utils.resolvePassword( jobMeta, wProxyPassword.getText() ) ) ); } else { conn.setProxyData( new HTTPProxyData( jobMeta.environmentSubstitute( wProxyHost.getText() ), Const .toInt( wProxyPort.getText(), 22 ) ) ); conn.connect(); conn.authenticateWithPublicKey( jobMeta.environmentSubstitute( wUserName.getText() ), new java.io.File( jobMeta .environmentSubstitute( wKeyFilename.getText() ) ), jobMeta } else { retval = conn.authenticateWithPassword( jobMeta.environmentSubstitute( wUserName.getText() ), Utils.resolvePassword( jobMeta, wPassword.getText() ) ); if ( conn != null ) { try { conn.close(); } catch ( Exception ignored ) {
@Test public void testOpenConnection_1() throws Exception { when( connection.authenticateWithPassword( username, password ) ).thenReturn( true ); assertNotNull( SSHData.OpenConnection( server, port, username, password, false, null, null, 0, null, null, 0, null, null ) ); verify( connection ).connect(); verify( connection ).authenticateWithPassword( username, password ); }
@Override public void dispose( StepMetaInterface smi, StepDataInterface sdi ) { meta = (SSHMeta) smi; data = (SSHData) sdi; if ( data.conn != null ) { data.conn.close(); if ( log.isDebug() ) { logDebug( BaseMessages.getString( PKG, "SSH.Log.ConnectionClosed" ) ); } } super.dispose( smi, sdi ); } }
/** * Checks if a directory exists * * @param sftpClient * @param directory * @return true, if directory exists */ public boolean sshDirectoryExists( SFTPv3Client sftpClient, String directory ) { try { SFTPv3FileAttributes attributes = sftpClient.stat( directory ); if ( attributes != null ) { return ( attributes.isDirectory() ); } else { return false; } } catch ( Exception e ) { return false; } }
@Test public void testOpenConnectionTimeOut() throws Exception { when( connection.authenticateWithPassword( username, password ) ).thenReturn( true ); assertNotNull( SSHData.OpenConnection( server, port, username, password, false, null, null, 100, null, null, proxyPort, proxyUsername, proxyPassword ) ); verify( connection ).connect( isNull( ServerHostKeyVerifier.class ), eq( 0 ), eq( 100 * 1000 ) ); }