Skip to content

Commit 0eb0092

Browse files
authored
Merge pull request #48 from embulk/add-validation-for-host
Add validation for "host" and "proxy.host"
2 parents 4434999 + 9b3382a commit 0eb0092

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/main/java/org/embulk/output/sftp/SftpFileOutputPlugin.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public ConfigDiff transaction(ConfigSource config, int taskCount,
8181
FileOutputPlugin.Control control)
8282
{
8383
PluginTask task = config.loadConfig(PluginTask.class);
84+
SftpUtils sftpUtils = null;
85+
try {
86+
sftpUtils = new SftpUtils(task);
87+
sftpUtils.validateHost(task);
88+
}
89+
finally {
90+
if (sftpUtils != null) {
91+
sftpUtils.close();
92+
}
93+
}
8494

8595
// retryable (idempotent) output:
8696
// return resume(task.dump(), taskCount, control);

src/main/java/org/embulk/output/sftp/SftpUtils.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class SftpUtils
3535
private final DefaultFileSystemManager manager;
3636
private final FileSystemOptions fsOptions;
3737
private final String userInfo;
38+
private final String user;
3839
private final String host;
3940
private final int port;
4041
private final int maxConnectionRetry;
@@ -127,6 +128,7 @@ private FileSystemOptions initializeFsOptions(PluginTask task)
127128
{
128129
this.manager = initializeStandardFileSystemManager();
129130
this.userInfo = initializeUserInfo(task);
131+
this.user = task.getUser();
130132
this.fsOptions = initializeFsOptions(task);
131133
this.host = task.getHost();
132134
this.port = task.getPort();
@@ -182,6 +184,9 @@ public Void call() throws IOException
182184
@Override
183185
public boolean isRetryableException(Exception exception)
184186
{
187+
if (exception instanceof ConfigException) {
188+
return false;
189+
}
185190
return true;
186191
}
187192

@@ -264,14 +269,28 @@ public void onGiveup(Exception firstException, Exception lastException) throws R
264269
}
265270
}
266271

272+
public void validateHost(PluginTask task)
273+
{
274+
if (task.getHost().contains("%s")) {
275+
throw new ConfigException("'host' can't contain spaces");
276+
}
277+
getSftpFileUri("/");
278+
279+
if (task.getProxy().isPresent() && task.getProxy().get().getHost().isPresent()) {
280+
if (task.getProxy().get().getHost().get().contains("%s")) {
281+
throw new ConfigException("'proxy.host' can't contains spaces");
282+
}
283+
}
284+
}
285+
267286
private URI getSftpFileUri(String remoteFilePath)
268287
{
269288
try {
270289
return new URI("sftp", userInfo, host, port, remoteFilePath, null, null);
271290
}
272291
catch (URISyntaxException e) {
273-
logger.error(e.getMessage());
274-
throw new ConfigException(e);
292+
String message = String.format("URISyntaxException was thrown: Illegal character in sftp://%s:******@%s:%s%s", user, host, port, remoteFilePath);
293+
throw new ConfigException(message);
275294
}
276295
}
277296

src/test/java/org/embulk/output/sftp/TestSftpFileOutputPlugin.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,37 @@ private void assertRecordsInFile(String filePath)
247247
}
248248
}
249249

250+
@Test(expected = ConfigException.class)
251+
public void testInvalidHost()
252+
{
253+
// setting embulk config
254+
final String pathPrefix = "/test/testUserPassword";
255+
String configYaml = "" +
256+
"type: sftp\n" +
257+
"host: " + HOST + "\n" +
258+
"user: " + USERNAME + "\n" +
259+
"path_prefix: " + pathPrefix + "\n" +
260+
"file_ext: txt\n" +
261+
"formatter:\n" +
262+
" type: csv\n" +
263+
" newline: CRLF\n" +
264+
" newline_in_field: LF\n" +
265+
" header_line: true\n" +
266+
" charset: UTF-8\n" +
267+
" quote_policy: NONE\n" +
268+
" quote: \"\\\"\"\n" +
269+
" escape: \"\\\\\"\n" +
270+
" null_string: \"\"\n" +
271+
" default_timezone: 'UTC'";
272+
273+
ConfigSource config = getConfigFromYaml(configYaml);
274+
config.set("host", HOST + " ");
275+
PluginTask task = config.loadConfig(PluginTask.class);
276+
277+
SftpUtils utils = new SftpUtils(task);
278+
utils.validateHost(task);
279+
}
280+
250281
@Test
251282
public void testConfigValuesIncludingDefault()
252283
{

0 commit comments

Comments
 (0)