Search in sources :

Example 1 with FTPSClient

use of org.apache.commons.net.ftp.FTPSClient in project camel by apache.

the class FtpsEndpoint method createRemoteFileOperations.

@Override
public RemoteFileOperations<FTPFile> createRemoteFileOperations() throws Exception {
    // configure ftp client
    FTPSClient client = getFtpsClient();
    if (client == null) {
        // must use a new client if not explicit configured to use a custom client
        client = (FTPSClient) createFtpClient();
    }
    // use configured buffer size which is larger and therefore faster (as the default is no buffer)
    if (getConfiguration().getReceiveBufferSize() > 0) {
        client.setBufferSize(getConfiguration().getReceiveBufferSize());
    }
    // set any endpoint configured timeouts
    if (getConfiguration().getConnectTimeout() > -1) {
        client.setConnectTimeout(getConfiguration().getConnectTimeout());
    }
    if (getConfiguration().getSoTimeout() > -1) {
        soTimeout = getConfiguration().getSoTimeout();
    }
    dataTimeout = getConfiguration().getTimeout();
    if (ftpClientParameters != null) {
        Map<String, Object> localParameters = new HashMap<String, Object>(ftpClientParameters);
        // setting soTimeout has to be done later on FTPClient (after it has connected)
        Object timeout = localParameters.remove("soTimeout");
        if (timeout != null) {
            soTimeout = getCamelContext().getTypeConverter().convertTo(int.class, timeout);
        }
        // and we want to keep data timeout so we can log it later
        timeout = localParameters.remove("dataTimeout");
        if (timeout != null) {
            dataTimeout = getCamelContext().getTypeConverter().convertTo(int.class, dataTimeout);
        }
        setProperties(client, localParameters);
    }
    if (ftpClientConfigParameters != null) {
        // client config is optional so create a new one if we have parameter for it
        if (ftpClientConfig == null) {
            ftpClientConfig = new FTPClientConfig();
        }
        Map<String, Object> localConfigParameters = new HashMap<String, Object>(ftpClientConfigParameters);
        setProperties(ftpClientConfig, localConfigParameters);
    }
    if (dataTimeout > 0) {
        client.setDataTimeout(dataTimeout);
    }
    if (log.isDebugEnabled()) {
        log.debug("Created FTPClient [connectTimeout: {}, soTimeout: {}, dataTimeout: {}, bufferSize: {}" + ", receiveDataSocketBufferSize: {}, sendDataSocketBufferSize: {}]: {}", new Object[] { client.getConnectTimeout(), getSoTimeout(), dataTimeout, client.getBufferSize(), client.getReceiveDataSocketBufferSize(), client.getSendDataSocketBufferSize(), client });
    }
    FtpsOperations operations = new FtpsOperations(client, getFtpClientConfig());
    operations.setEndpoint(this);
    return operations;
}
Also used : HashMap(java.util.HashMap) FTPClientConfig(org.apache.commons.net.ftp.FTPClientConfig) FTPSClient(org.apache.commons.net.ftp.FTPSClient)

Example 2 with FTPSClient

use of org.apache.commons.net.ftp.FTPSClient in project ddf by codice.

the class TestFtp method testFtpsPutDotFile.

/**
     * Test the ftp command sequence of uploading a dot-file (i.e. .foo) and then renaming that
     * file to the final filename (i.e. test.txt). Confirm that the catalog contains only one
     * object and the title of that object is "test.txt".
     */
@Test
public void testFtpsPutDotFile() throws Exception {
    setClientAuthConfiguration(NEED);
    FTPSClient client = createSecureClient(true);
    String dotFilename = ".foo";
    String finalFilename = "test.txt";
    ftpPut(client, SAMPLE_DATA, dotFilename);
    boolean ret = client.rename(dotFilename, finalFilename);
    assertTrue(ret);
    verifyIngest(1, finalFilename);
}
Also used : FTPSClient(org.apache.commons.net.ftp.FTPSClient) AbstractIntegrationTest(org.codice.ddf.itests.common.AbstractIntegrationTest) Test(org.junit.Test)

Example 3 with FTPSClient

use of org.apache.commons.net.ftp.FTPSClient in project camel by apache.

the class FtpsEndpoint method createFtpClient.

/**
     * Create the FTPS client.
     */
protected FTPClient createFtpClient() throws Exception {
    FTPSClient client;
    if (sslContextParameters != null) {
        SSLContext context = sslContextParameters.createSSLContext(getCamelContext());
        client = new FTPSClient(getFtpsConfiguration().isImplicit(), context);
        // The FTPSClient tries to manage the following SSLSocket related configuration options
        // on its own based on internal configuration options.  FTPSClient does not lend itself
        // to subclassing for the purpose of overriding this behavior (private methods, fields, etc.).
        // As such, we create a socket (preconfigured by SSLContextParameters) from the context
        // we gave to FTPSClient and then setup FTPSClient to reuse the already configured configuration
        // from the socket for all future sockets it creates.  Not sexy and a little brittle, but it works.
        SSLSocket socket = (SSLSocket) context.getSocketFactory().createSocket();
        client.setEnabledCipherSuites(socket.getEnabledCipherSuites());
        client.setEnabledProtocols(socket.getEnabledProtocols());
        client.setNeedClientAuth(socket.getNeedClientAuth());
        client.setWantClientAuth(socket.getWantClientAuth());
        client.setEnabledSessionCreation(socket.getEnableSessionCreation());
    } else {
        client = new FTPSClient(getFtpsConfiguration().getSecurityProtocol(), getFtpsConfiguration().isImplicit());
        if (ftpClientKeyStoreParameters != null) {
            String type = (ftpClientKeyStoreParameters.containsKey("type")) ? (String) ftpClientKeyStoreParameters.get("type") : KeyStore.getDefaultType();
            String file = (String) ftpClientKeyStoreParameters.get("file");
            String password = (String) ftpClientKeyStoreParameters.get("password");
            String algorithm = (ftpClientKeyStoreParameters.containsKey("algorithm")) ? (String) ftpClientKeyStoreParameters.get("algorithm") : KeyManagerFactory.getDefaultAlgorithm();
            String keyPassword = (String) ftpClientKeyStoreParameters.get("keyPassword");
            KeyStore keyStore = KeyStore.getInstance(type);
            FileInputStream keyStoreFileInputStream = new FileInputStream(new File(file));
            try {
                keyStore.load(keyStoreFileInputStream, password.toCharArray());
            } finally {
                IOHelper.close(keyStoreFileInputStream, "keyStore", log);
            }
            KeyManagerFactory keyMgrFactory = KeyManagerFactory.getInstance(algorithm);
            keyMgrFactory.init(keyStore, keyPassword.toCharArray());
            client.setNeedClientAuth(true);
            client.setKeyManager(keyMgrFactory.getKeyManagers()[0]);
        }
        if (ftpClientTrustStoreParameters != null) {
            String type = (ftpClientTrustStoreParameters.containsKey("type")) ? (String) ftpClientTrustStoreParameters.get("type") : KeyStore.getDefaultType();
            String file = (String) ftpClientTrustStoreParameters.get("file");
            String password = (String) ftpClientTrustStoreParameters.get("password");
            String algorithm = (ftpClientTrustStoreParameters.containsKey("algorithm")) ? (String) ftpClientTrustStoreParameters.get("algorithm") : TrustManagerFactory.getDefaultAlgorithm();
            KeyStore trustStore = KeyStore.getInstance(type);
            FileInputStream trustStoreFileInputStream = new FileInputStream(new File(file));
            try {
                trustStore.load(trustStoreFileInputStream, password.toCharArray());
            } finally {
                IOHelper.close(trustStoreFileInputStream, "trustStore", log);
            }
            TrustManagerFactory trustMgrFactory = TrustManagerFactory.getInstance(algorithm);
            trustMgrFactory.init(trustStore);
            client.setTrustManager(trustMgrFactory.getTrustManagers()[0]);
        }
    }
    return client;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) FTPSClient(org.apache.commons.net.ftp.FTPSClient) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 4 with FTPSClient

use of org.apache.commons.net.ftp.FTPSClient in project camel by apache.

the class FileToFtpsWithFtpClientConfigRefTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry jndi = super.createRegistry();
    jndi.bind("ftpsClient", new FTPSClient("SSL"));
    jndi.bind("ftpsClientIn", new FTPSClient("SSL"));
    return jndi;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) FTPSClient(org.apache.commons.net.ftp.FTPSClient)

Example 5 with FTPSClient

use of org.apache.commons.net.ftp.FTPSClient in project ddf by codice.

the class TestFtp method setClientAuthConfiguration.

/**
     * Sets the clientAuth configuration in catalog-ftp feature.
     * <p/>
     * An FTPS client without a certificate is used to indicate when the clientAuth configuration
     * has taken effect at the FTP server level.
     * -    When the clientAuth is set to "want", this FTPS client without a certificate should be
     * able to connect to the FTP server and successfully complete the SSL handshake.
     * -    When the clientAuth is set to "need", this FTPS client without a certificate should be
     * able to connect to the FTP server but fail to complete the SSL handshake.
     * <p/>
     * SocketException and FTPConnectionClosedException are thrown when the client cannot connect to
     * the server or the connection was closed unexpectedly. These exceptions are thrown when the
     * server is being updated. SSLException is thrown only after a client has successfully
     * connected and when the SSL handshake between the client and server fails.
     *
     * @throws Exception
     */
private void setClientAuthConfiguration(String clientAuth) throws Exception {
    Configuration config = getAdminConfig().getConfiguration("ddf.catalog.ftp.FtpServerStarter");
    config.setBundleLocation("mvn:ddf.catalog/ftp/" + System.getProperty("ddf.version"));
    Dictionary properties = new Hashtable<>();
    properties.put(CLIENT_AUTH, clientAuth);
    config.update(properties);
    //wait until the clientAuth configuration has taken effect at the FTP server level
    switch(clientAuth) {
        case WANT:
            expect("SSL handshake to succeed with FTPS client without certificate because clientAuth = \"want\"").within(SET_CLIENT_AUTH_TIMEOUT_SEC, TimeUnit.SECONDS).until(() -> {
                FTPSClient client = null;
                try {
                    client = createSecureClient(false);
                    disconnectClient(client);
                    return true;
                } catch (SSLException e) {
                    //SSL handshake failed
                    return false;
                } catch (SocketException | FTPConnectionClosedException e) {
                    //connection failed
                    return false;
                }
            });
            break;
        case NEED:
            expect("SSL handshake to fail with FTPS client without certificate because clientAuth = \"need\"").within(SET_CLIENT_AUTH_TIMEOUT_SEC, TimeUnit.SECONDS).until(() -> {
                FTPSClient client = null;
                try {
                    client = createSecureClient(false);
                    disconnectClient(client);
                    return false;
                } catch (SSLException e) {
                    //SSL handshake failed
                    return true;
                } catch (SocketException | FTPConnectionClosedException e) {
                    //connection failed
                    return false;
                }
            });
    }
}
Also used : Dictionary(java.util.Dictionary) SocketException(java.net.SocketException) Configuration(org.osgi.service.cm.Configuration) FTPConnectionClosedException(org.apache.commons.net.ftp.FTPConnectionClosedException) Hashtable(java.util.Hashtable) FTPSClient(org.apache.commons.net.ftp.FTPSClient) SSLException(javax.net.ssl.SSLException)

Aggregations

FTPSClient (org.apache.commons.net.ftp.FTPSClient)7 File (java.io.File)2 SocketException (java.net.SocketException)2 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)2 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 KeyStore (java.security.KeyStore)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 KeyManager (javax.net.ssl.KeyManager)1 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)1 SSLContext (javax.net.ssl.SSLContext)1 SSLException (javax.net.ssl.SSLException)1 SSLSocket (javax.net.ssl.SSLSocket)1 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)1 JndiRegistry (org.apache.camel.impl.JndiRegistry)1 FTPClientConfig (org.apache.commons.net.ftp.FTPClientConfig)1 FTPConnectionClosedException (org.apache.commons.net.ftp.FTPConnectionClosedException)1 FTPFile (org.apache.commons.net.ftp.FTPFile)1