Skip to content

Commit 974fe4e

Browse files
committed
Merge pull request #11 from sakama/support-proxy-settings
Support proxy settings
2 parents a08249c + 9164067 commit 974fe4e

File tree

6 files changed

+302
-12
lines changed

6 files changed

+302
-12
lines changed

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Stores files on a SFTP Server
1414
## Configuration
1515

1616
- **host**: (string, required)
17-
- **port**: (string, default: `22`)
17+
- **port**: (int, default: `22`)
1818
- **user**: (string, required)
1919
- **password**: (string, default: `null`)
2020
- **secret_key_file**: (string, default: `null`) [see below](#secret-keyfile-configuration)
@@ -25,6 +25,19 @@ Stores files on a SFTP Server
2525
- **file_ext**: Extension of output files (string, required)
2626
- **sequence_format**: Format for sequence part of output files (string, default: `".%03d.%02d"`)
2727

28+
### Proxy configuration
29+
30+
- **proxy**:
31+
- **type**: (string(http | socks | stream), required, default: `null`)
32+
- **http**: use HTTP Proxy
33+
- **socks**: use SOCKS Proxy
34+
- **stream**: Connects to the SFTP server through a remote host reached by SSH
35+
- **host**: (string, required)
36+
- **port**: (int, default: `22`)
37+
- **user**: (string, optional)
38+
- **password**: (string, optional, default: `null`)
39+
- **command**: (string, optional)
40+
2841
## Example
2942

3043
```yaml
@@ -42,10 +55,31 @@ out:
4255
sequence_format: ".%01d%01d"
4356
```
4457
58+
With proxy
59+
```yaml
60+
out:
61+
type: sftp
62+
host: 127.0.0.1
63+
port: 22
64+
user: embulk
65+
secret_key_file: /Users/embulk/.ssh/id_rsa
66+
secret_key_passphrase: secret_pass
67+
user_directory_is_root: false
68+
timeout: 600
69+
path_prefix: /data/sftp
70+
proxy:
71+
type: http
72+
host: proxy_host
73+
port: 8080
74+
user: proxy_user
75+
password: proxy_secret_pass
76+
command:
77+
```
78+
4579
### Secret Keyfile configuration
4680
4781
Please set path of secret_key_file as follows.
48-
```
82+
```yaml
4983
out:
5084
type: sftp
5185
...
@@ -54,7 +88,7 @@ out:
5488
```
5589

5690
You can also embed contents of secret_key_file at config.yml.
57-
```
91+
```yaml
5892
out:
5993
type: sftp
6094
...

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ dependencies {
2929
testCompile "org.embulk:embulk-core:0.8.6:tests"
3030
testCompile "org.embulk:embulk-standards:0.8.6"
3131
testCompile "org.apache.sshd:apache-sshd:1.1.0+"
32+
testCompile "org.littleshoot:littleproxy:1.1.0-beta1"
33+
testCompile "io.netty:netty-all:4.0.34.Final"
3234
}
3335

3436
jacocoTestReport {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.embulk.output.sftp;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
import com.google.common.base.Optional;
6+
import org.apache.commons.vfs2.FileSystemOptions;
7+
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
8+
import org.embulk.config.Config;
9+
import org.embulk.config.ConfigDefault;
10+
import org.embulk.config.ConfigException;
11+
import org.embulk.config.Task;
12+
13+
import java.util.Locale;
14+
15+
interface ProxyTask
16+
extends Task
17+
{
18+
@Config("type")
19+
public ProxyType getType();
20+
21+
@Config("host")
22+
public Optional<String> getHost();
23+
24+
@Config("user")
25+
@ConfigDefault("null")
26+
public Optional<String> getUser();
27+
28+
@Config("password")
29+
@ConfigDefault("null")
30+
public Optional<String> getPassword();
31+
32+
@Config("port")
33+
@ConfigDefault("22")
34+
public int getPort();
35+
36+
@Config("command")
37+
@ConfigDefault("null")
38+
public Optional<String> getCommand();
39+
40+
public enum ProxyType
41+
{
42+
HTTP,
43+
SOCKS,
44+
STREAM;
45+
46+
@JsonValue
47+
@Override
48+
public String toString()
49+
{
50+
return name().toLowerCase(Locale.ENGLISH);
51+
}
52+
53+
@JsonCreator
54+
public static ProxyType fromString(String value)
55+
{
56+
switch (value) {
57+
case "http":
58+
return HTTP;
59+
case "socks":
60+
return SOCKS;
61+
case "stream":
62+
return STREAM;
63+
default:
64+
throw new ConfigException(String.format("Unknown proxy type '%s'. Supported proxy types are http, socks, stream", value));
65+
}
66+
}
67+
68+
public static SftpFileSystemConfigBuilder setProxyType(SftpFileSystemConfigBuilder builder, FileSystemOptions fsOptions, ProxyTask.ProxyType type)
69+
{
70+
SftpFileSystemConfigBuilder.ProxyType setType = null;
71+
switch (type) {
72+
case HTTP:
73+
setType = SftpFileSystemConfigBuilder.PROXY_HTTP;
74+
break;
75+
case SOCKS:
76+
setType = SftpFileSystemConfigBuilder.PROXY_SOCKS5;
77+
break;
78+
case STREAM:
79+
setType = SftpFileSystemConfigBuilder.PROXY_STREAM;
80+
}
81+
builder.setProxyType(fsOptions, setType);
82+
return builder;
83+
}
84+
}
85+
}

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,41 @@ private FileSystemOptions initializeFsOptions(PluginTask task)
7979
FileSystemOptions fsOptions = new FileSystemOptions();
8080

8181
try {
82-
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fsOptions, task.getUserDirIsRoot());
83-
SftpFileSystemConfigBuilder.getInstance().setTimeout(fsOptions, task.getSftpConnectionTimeout());
84-
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fsOptions, "no");
82+
SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
83+
builder.setUserDirIsRoot(fsOptions, task.getUserDirIsRoot());
84+
builder.setTimeout(fsOptions, task.getSftpConnectionTimeout());
85+
builder.setStrictHostKeyChecking(fsOptions, "no");
8586
if (task.getSecretKeyFilePath().isPresent()) {
8687
IdentityInfo identityInfo = new IdentityInfo(
8788
new File((task.getSecretKeyFilePath().transform(localFileToPathString()).get())),
8889
task.getSecretKeyPassphrase().getBytes()
8990
);
90-
SftpFileSystemConfigBuilder.getInstance().setIdentityInfo(fsOptions, identityInfo);
91+
builder.setIdentityInfo(fsOptions, identityInfo);
9192
logger.info("set identity: {}", task.getSecretKeyFilePath().get());
9293
}
94+
95+
if (task.getProxy().isPresent()) {
96+
ProxyTask proxy = task.getProxy().get();
97+
98+
ProxyTask.ProxyType.setProxyType(builder, fsOptions, proxy.getType());
99+
100+
if (proxy.getHost().isPresent()) {
101+
builder.setProxyHost(fsOptions, proxy.getHost().get());
102+
builder.setProxyPort(fsOptions, proxy.getPort());
103+
}
104+
105+
if (proxy.getUser().isPresent()) {
106+
builder.setProxyUser(fsOptions, proxy.getUser().get());
107+
}
108+
109+
if (proxy.getPassword().isPresent()) {
110+
builder.setProxyPassword(fsOptions, proxy.getPassword().get());
111+
}
112+
113+
if (proxy.getCommand().isPresent()) {
114+
builder.setProxyCommand(fsOptions, proxy.getCommand().get());
115+
}
116+
}
93117
}
94118
catch (FileSystemException e) {
95119
logger.error(e.getMessage());

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public interface PluginTask
6868
@Config("sequence_format")
6969
@ConfigDefault("\"%03d.%02d.\"")
7070
public String getSequenceFormat();
71+
72+
@Config("proxy")
73+
@ConfigDefault("null")
74+
public Optional<ProxyTask> getProxy();
7175
}
7276

7377
@Override

0 commit comments

Comments
 (0)