Skip to content

Commit 905bb4c

Browse files
authored
chore: allows to specify IP protocol (#261)
* Allows to specify IP protocol, including IPv4 only * Add tests forcing IPv4 or IPv6
1 parent 066b72a commit 905bb4c

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

DOCS.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,23 @@ Example configuration for passphrase which protecting a private key:
178178
- echo "you can't see the steps."
179179
```
180180

181+
Example configuration for forcing protocol to IPv4 only:
182+
183+
```diff
184+
- name: ssh commands
185+
image: ghcr.io/appleboy/drone-ssh
186+
settings:
187+
host: foo.com
188+
username: root
189+
password: 1234
190+
port: 22
191+
+ protocol: tcp4
192+
script:
193+
- echo hello
194+
- echo world
195+
```
196+
197+
181198
## Secret Reference
182199

183200
| Key | Description |
@@ -197,6 +214,7 @@ Example configuration for passphrase which protecting a private key:
197214
|-----|-------------|
198215
| `host` | target hostname or IP |
199216
| `port` | ssh port of target host |
217+
| `protocol` | IP protocol to use: either tcp, tcp4 or tcp6 |
200218
| `username` | account for target host user |
201219
| `password` | password for target host user |
202220
| `key` | plain text of user private key |
@@ -208,6 +226,7 @@ Example configuration for passphrase which protecting a private key:
208226
| `command_timeout` | Command timeout is the maximum amount of time for the execute commands, default is 10 minutes. |
209227
| `proxy_host` | proxy hostname or IP |
210228
| `proxy_port` | ssh port of proxy host |
229+
| `proxy_protocol` | IP protocol to use for the proxy: either tcp, tcp4 or tcp6 |
211230
| `proxy_username` | account for proxy host user |
212231
| `proxy_password` | password for proxy host user |
213232
| `proxy_key` | plain text of proxy private key |

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/appleboy/drone-ssh
33
go 1.18
44

55
require (
6-
github.com/appleboy/easyssh-proxy v1.3.10
6+
github.com/appleboy/easyssh-proxy v1.3.11-0.20230609114136-a50c12e64039
77
github.com/joho/godotenv v1.5.1
88
github.com/stretchr/testify v1.8.4
99
github.com/urfave/cli/v2 v2.25.7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/ScaleFT/sshkeys v1.2.0 h1:5BRp6rTVIhJzXT3VcUQrKgXR8zWA3sOsNeuyW15WUA8=
22
github.com/ScaleFT/sshkeys v1.2.0/go.mod h1:gxOHeajFfvGQh/fxlC8oOKBe23xnnJTif00IFFbiT+o=
3-
github.com/appleboy/easyssh-proxy v1.3.10 h1:iriF68tlrYoxgWhS7t7Wyr0FA+hJlOem5tMfm+RDlx4=
4-
github.com/appleboy/easyssh-proxy v1.3.10/go.mod h1:T81pu/Cxx/zf/7YXhFCFiucBa4xeQ81ci5b0PFnMRJc=
3+
github.com/appleboy/easyssh-proxy v1.3.11-0.20230609114136-a50c12e64039 h1:u1o1Ft7PltG4+iOAmEubuyXx5fiDHWIvP+JiCClL+ak=
4+
github.com/appleboy/easyssh-proxy v1.3.11-0.20230609114136-a50c12e64039/go.mod h1:6nn1KPv9GP9BrK8tgl+d/w68jX4rpLeooO3FAcC/lds=
55
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
66
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
77
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ func main() {
5151
EnvVars: []string{"PLUGIN_PORT", "SSH_PORT", "INPUT_PORT"},
5252
Value: 22,
5353
},
54+
&cli.StringFlag{
55+
Name: "protocol",
56+
Usage: "The IP protocol to use. Default to tcp (both IPv4 and IPv6).",
57+
EnvVars: []string{"PLUGIN_PROTOCOL", "SSH_PROTOCOL", "INPUT_PROTOCOL"},
58+
Value: "tcp",
59+
},
5460
&cli.StringFlag{
5561
Name: "username",
5662
Aliases: []string{"user", "u"},
@@ -141,6 +147,12 @@ func main() {
141147
EnvVars: []string{"PLUGIN_PROXY_PORT", "PROXY_SSH_PORT", "INPUT_PROXY_PORT"},
142148
Value: "22",
143149
},
150+
&cli.StringFlag{
151+
Name: "proxy.protocol",
152+
Usage: "The IP protocol to use for the proxy. Default to tcp (both IPv4 and IPv6).",
153+
EnvVars: []string{"PLUGIN_PROTOCOL", "SSH_PROTOCOL", "INPUT_PROTOCOL"},
154+
Value: "tcp",
155+
},
144156
&cli.StringFlag{
145157
Name: "proxy.username",
146158
Usage: "connect as user of proxy",
@@ -259,6 +271,7 @@ func run(c *cli.Context) error {
259271
Fingerprint: c.String("fingerprint"),
260272
Host: c.StringSlice("host"),
261273
Port: c.Int("port"),
274+
Protocol: easyssh.Protocol(c.String("protocol")),
262275
Timeout: c.Duration("timeout"),
263276
CommandTimeout: c.Duration("command.timeout"),
264277
Script: scripts,
@@ -278,6 +291,7 @@ func run(c *cli.Context) error {
278291
Fingerprint: c.String("proxy.fingerprint"),
279292
Server: c.String("proxy.host"),
280293
Port: c.String("proxy.port"),
294+
Protocol: easyssh.Protocol(c.String("proxy.protocol")),
281295
Timeout: c.Duration("proxy.timeout"),
282296
Ciphers: c.StringSlice("proxy.ciphers"),
283297
UseInsecureCipher: c.Bool("proxy.useInsecureCipher"),

plugin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type (
3030
Password string
3131
Host []string
3232
Port int
33+
Protocol easyssh.Protocol
3334
Fingerprint string
3435
Timeout time.Duration
3536
CommandTimeout time.Duration
@@ -75,6 +76,7 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
7576
User: p.Config.Username,
7677
Password: p.Config.Password,
7778
Port: port,
79+
Protocol: p.Config.Protocol,
7880
Key: p.Config.Key,
7981
KeyPath: p.Config.KeyPath,
8082
Passphrase: p.Config.Passphrase,
@@ -87,6 +89,7 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
8789
User: p.Config.Proxy.User,
8890
Password: p.Config.Proxy.Password,
8991
Port: p.Config.Proxy.Port,
92+
Protocol: p.Config.Proxy.Protocol,
9093
Key: p.Config.Proxy.Key,
9194
KeyPath: p.Config.Proxy.KeyPath,
9295
Passphrase: p.Config.Proxy.Passphrase,

plugin_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,40 @@ func TestSSHScriptFromKeyFile(t *testing.T) {
113113
assert.Nil(t, err)
114114
}
115115

116+
func TestSSHIPv4Only(t *testing.T) {
117+
plugin := Plugin{
118+
Config: Config{
119+
Host: []string{"localhost", "127.0.0.1"},
120+
Username: "drone-scp",
121+
Port: 22,
122+
Protocol: easyssh.PROTOCOL_TCP4,
123+
KeyPath: "./tests/.ssh/id_rsa",
124+
Script: []string{"whoami", "ls -al"},
125+
CommandTimeout: 60 * time.Second,
126+
},
127+
}
128+
129+
err := plugin.Exec()
130+
assert.Nil(t, err)
131+
}
132+
133+
func TestSSHIPv6OnlyError(t *testing.T) {
134+
plugin := Plugin{
135+
Config: Config{
136+
Host: []string{"127.0.0.1"},
137+
Username: "drone-scp",
138+
Port: 22,
139+
Protocol: easyssh.PROTOCOL_TCP6,
140+
KeyPath: "./tests/.ssh/id_rsa",
141+
Script: []string{"whoami", "ls -al"},
142+
CommandTimeout: 60 * time.Second,
143+
},
144+
}
145+
146+
err := plugin.Exec()
147+
assert.NotNil(t, err)
148+
}
149+
116150
func TestStreamFromSSHCommand(t *testing.T) {
117151
plugin := Plugin{
118152
Config: Config{

0 commit comments

Comments
 (0)