Skip to content

Commit

Permalink
Merge pull request #40 from data-integrations/PLUGIN-1525-fix-colon-i…
Browse files Browse the repository at this point in the history
…n-password

PLUGIN-1525 fix colon in password for sftp
  • Loading branch information
albertshau authored Mar 1, 2023
2 parents c053c48 + 278e53b commit 6a3afe8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,19 @@ String getPathFromConfig() {
}

/**
* This method extracts the password from url
* This method extracts the password from url.
* This does some manual URI parsing and will not work properly if the username contains a colon,
* and will not work properly in all sorts of edge cases with malformed URIs.
* However, the underlying Hadoop FTP library also does not work properly on usernames with colon (see PLUGIN-1525).
*/
public String extractPasswordFromUrl() {
if (!path.startsWith("ftp://") && !path.startsWith("sftp://")) {
throw new IllegalArgumentException("Invalid path. Please ensure that it starts with ftp:// or sftp://");
}
int getLastIndexOfAtSign = path.lastIndexOf("@");
String authentication = path.substring(0, getLastIndexOfAtSign);
return authentication.substring(authentication.lastIndexOf(":") + 1);
int usernameEndIndex = authentication.indexOf(':', authentication.indexOf(':') + 1);
return authentication.substring(usernameEndIndex + 1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public class FTPBatchSourceTest {

private static final String USER = "user";
private static final String PASSWORD_WITH_SPECIAL_CHARACTERS = "wex^Yz@123#456!";
private static final String PASSWORD_WITH_SPECIAL_CHARACTERS = "we:/_x^Yz @123#456!";
private static final String PASSWORD_WITHOUT_SPECIAL_CHARACTERS = "wexYz123456";
private static final String HOST = "localhost";
private static final int FTP_DEFAULT_PORT = 21;
Expand Down Expand Up @@ -82,8 +82,7 @@ public static void ftpTeardown() {

@Test
public void testSchemaDetection() {
String path = String.format("ftp://user2:%s@%s:%d/tmp/file1.txt",
PASSWORD_WITH_SPECIAL_CHARACTERS, HOST, ftpServerPort);
String path = String.format("ftp://user:password@%s:%d/tmp/file1.txt", HOST, ftpServerPort);
FTPBatchSource.FTPBatchSourceConfig config = new FTPBatchSource.FTPBatchSourceConfig(path, "csv", false);
FTPBatchSource ftpBatchSource = new FTPBatchSource(config);
Map<String, Object> plugins = new HashMap<>();
Expand Down

0 comments on commit 6a3afe8

Please sign in to comment.