From a41d4afc40d69988cf38fa8a9477e5b8a6de39ff Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 21 Jan 2024 09:27:49 +0800 Subject: [PATCH] chore(protocol): improve IPv6 address. (#268) * docs: improve documentation and configuration handling - Clarify valid values for the IP protocol in usage messages for both main application and proxy settings Signed-off-by: Bo-Yi Wu * test: improve IPv6 command execution tests - Add a new test function `TestCommandWithIPv6` to check command execution with an IPv6 address - Initialize test variables and expected output for the IPv6 command test - Set up a `Plugin` struct with IPv6 host, user, port, key path, script, and command timeout for testing - Verify that `plugin.Exec()` returns `nil` (no error) in the IPv6 test - Assert that the output of the command execution matches the expected output in the IPv6 test Signed-off-by: Bo-Yi Wu * test: enhance test suite and CI robustness - Add support for IPv6 protocol in `TestCommandWithIPv6` test case in `plugin_test.go` Signed-off-by: Bo-Yi Wu * update Signed-off-by: Bo-Yi Wu * update Signed-off-by: Bo-Yi Wu * update Signed-off-by: Bo-Yi Wu * update Signed-off-by: Bo-Yi Wu * update2 Signed-off-by: Bo-Yi Wu * update3 Signed-off-by: Bo-Yi Wu * update4 Signed-off-by: Bo-Yi Wu * update5 Signed-off-by: Bo-Yi Wu * update5 Signed-off-by: Bo-Yi Wu * update5 Signed-off-by: Bo-Yi Wu --------- Signed-off-by: Bo-Yi Wu --- Makefile | 2 ++ main.go | 4 ++-- plugin.go | 4 +++- plugin_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index d8878db..03cffb9 100644 --- a/Makefile +++ b/Makefile @@ -117,6 +117,8 @@ ssh-server: rm -rf /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_dsa_key sed -i 's/^#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config sed -i 's/AllowTcpForwarding no/AllowTcpForwarding yes/g' /etc/ssh/sshd_config + sed -i 's/^#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/g' /etc/ssh/sshd_config + sed -i 's/^#ListenAddress ::/ListenAddress ::/g' /etc/ssh/sshd_config ./tests/entrypoint.sh /usr/sbin/sshd -D & coverage: diff --git a/main.go b/main.go index 9d7d82f..af28cc7 100644 --- a/main.go +++ b/main.go @@ -53,7 +53,7 @@ func main() { }, &cli.StringFlag{ Name: "protocol", - Usage: "The IP protocol to use. Default to tcp (both IPv4 and IPv6).", + Usage: "The IP protocol to use. Valid values are \"tcp\". \"tcp4\" or \"tcp6\". Default to tcp.", EnvVars: []string{"PLUGIN_PROTOCOL", "SSH_PROTOCOL", "INPUT_PROTOCOL"}, Value: "tcp", }, @@ -149,7 +149,7 @@ func main() { }, &cli.StringFlag{ Name: "proxy.protocol", - Usage: "The IP protocol to use for the proxy. Default to tcp (both IPv4 and IPv6).", + Usage: "The IP protocol to use for the proxy. Valid values are \"tcp\". \"tcp4\" or \"tcp6\". Default to tcp.", EnvVars: []string{"PLUGIN_PROTOCOL", "SSH_PROTOCOL", "INPUT_PROTOCOL"}, Value: "tcp", }, diff --git a/plugin.go b/plugin.go index 56d5b76..d99639d 100644 --- a/plugin.go +++ b/plugin.go @@ -61,7 +61,9 @@ func escapeArg(arg string) string { func (p Plugin) hostPort(host string) (string, string) { hosts := strings.Split(host, ":") port := strconv.Itoa(p.Config.Port) - if len(hosts) > 1 { + if len(hosts) > 1 && + (p.Config.Protocol == easyssh.PROTOCOL_TCP || + p.Config.Protocol == easyssh.PROTOCOL_TCP4) { host = hosts[0] port = hosts[1] } diff --git a/plugin_test.go b/plugin_test.go index e5c7f91..251b425 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -797,7 +797,8 @@ func TestPlugin_hostPort(t *testing.T) { name: "different port", fields: fields{ Config: Config{ - Port: 22, + Port: 22, + Protocol: easyssh.PROTOCOL_TCP4, }, }, args: args{ @@ -806,6 +807,20 @@ func TestPlugin_hostPort(t *testing.T) { wantHost: "localhost", wantPort: "443", }, + { + name: "ipv6", + fields: fields{ + Config: Config{ + Port: 22, + Protocol: easyssh.PROTOCOL_TCP6, + }, + }, + args: args{ + h: "::1", + }, + wantHost: "::1", + wantPort: "22", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -949,3 +964,34 @@ func TestSudoCommand(t *testing.T) { assert.Nil(t, plugin.Exec()) assert.Equal(t, unindent(expected), unindent(buffer.String())) } + +// TODO: TestCommandWithIPv6 is not working on github actions. +// func TestCommandWithIPv6(t *testing.T) { +// var ( +// buffer bytes.Buffer +// expected = ` +// ======CMD====== +// whoami +// ======END====== +// out: drone-scp +// ` +// ) + +// plugin := Plugin{ +// Config: Config{ +// Host: []string{"::1"}, +// Username: "drone-scp", +// Port: 22, +// KeyPath: "./tests/.ssh/id_rsa", +// Script: []string{ +// "whoami", +// }, +// Protocol: easyssh.PROTOCOL_TCP6, +// CommandTimeout: 10 * time.Second, +// }, +// Writer: &buffer, +// } + +// assert.Nil(t, plugin.Exec()) +// assert.Equal(t, unindent(expected), unindent(buffer.String())) +// }