Search code examples
javaapache-camelsftppgp

Unable to use apache-camel for sftp file transfer


Currently I am unable to grab -> archive -> decrypt a file from an SFTP server. I have tested the logic using local directories but with no success using SFTP.

The connection appears to be established to the server as neglecting to pass the private key will result in a connection exception. When the key is being passed no exception is given from the route itself but no files are copied. What would be a potential solution or next steps to help in troubleshooting this issue?

I am using the absolute directory's in which the files would be stored from the sftp location.

CamelContext camelContext = new DefaultCamelContext();
camelContext.getRegistry().bind("SFTPPrivateKey",Byte[].class,privateKey.getBytes());
String sftpInput = buildURISFTP(input,inputOptions,connectionConfig);
String sfpOutput = buildURISFTP(output,outputOptions,connectionConfig);
String sfpArchive = buildURISFTP(archive,archiveOptions,connectionConfig);

camelContext.addRoutes(new RouteBuilder() {
    public void configure() throws Exception { 
        PGPDataFormat pgpDataFormat = new PGPDataFormat();
        pgpDataFormat.setKeyFileName(pPgpSecretKey);
        pgpDataFormat.setKeyUserid(pgpUserId);
        pgpDataFormat.setPassword(pgpPassword);
        pgpDataFormat.setArmored(true);

        from(sftpInput)
                .to(sfpArchive);
                //tested decryption local with file to file routing
                .unmarshal(pgpDataFormat)
                .to(sfpOutput);
    }
});
camelContext.start();
Thread.sleep(timeout);
camelContext.stop();
public String buildURISFTP(String directory, String options, ConnectionConfig connectionConfig){
    StringBuilder uri = new StringBuilder();
    uri.append("sftp://");
    uri.append(connectionConfig.getSftpHost());
    uri.append(":");
    uri.append(connectionConfig.getSftpPort());
    uri.append(directory);
    uri.append("?username=");
    uri.append(connectionConfig.getSftpUser());
    if(!StringUtils.isEmpty(connectionConfig.getSftpPassword())){
        uri.append("&password=");
        uri.append(connectionConfig.getSftpPassword());
    }
    uri.append("&privateKey=#SFTPPrivateKey");
    if(!StringUtils.isEmpty(options)){
        uri.append(options);
    }
    return uri.toString();
}

Solution

  • Issue was due to lack of knowledge around FTP component https://camel.apache.org/components/3.18.x/ftp-component.html

    Where it is specified that absolute paths are not supported, unfortunately I did not read this page and only referenced the SFTP component page where it is not specified. https://camel.apache.org/components/3.18.x/sftp-component.html

    Issue was resolved by backtracking directories with /../../ before giving the absolute path.