Skip to content

Commit

Permalink
Merge pull request #258 from avi-08/topic/avi-08/scp-using-ipv6
Browse files Browse the repository at this point in the history
Add IPv6 support for scp
  • Loading branch information
franknstyle authored Oct 18, 2023
2 parents 26b8e3b + ec8fadb commit 1af06cf
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
k8s.io/apimachinery v0.27.3
k8s.io/cli-runtime v0.27.3
k8s.io/client-go v0.27.3
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
)

require (
Expand Down Expand Up @@ -55,7 +56,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw=
k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5FJ2kxm1WrQFanWchyKuqGg=
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f/go.mod h1:byini6yhqGC14c3ebc/QwanvYwhuMWF6yz2F8uwW8eg=
k8s.io/utils v0.0.0-20230209194617-a36077c30491 h1:r0BAOLElQnnFhE/ApUsg3iHdVYYPBjNSSOMowRZxxsY=
k8s.io/utils v0.0.0-20230209194617-a36077c30491/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
13 changes: 11 additions & 2 deletions ssh/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
)

var (
support *testcrashd.TestSupport
testSSHArgs SSHArgs
support *testcrashd.TestSupport
testSSHArgs SSHArgs
testSSHArgsIPv6 SSHArgs
)

func TestMain(m *testing.M) {
Expand All @@ -36,6 +37,14 @@ func TestMain(m *testing.M) {
MaxRetries: support.MaxConnectionRetries(),
}

testSSHArgsIPv6 = SSHArgs{
User: support.CurrentUsername(),
PrivateKeyPath: support.PrivateKeyPath(),
Host: "::1",
Port: support.PortValue(),
MaxRetries: support.MaxConnectionRetries(),
}

testResult := m.Run()

logrus.Debug("Shutting down test...")
Expand Down
7 changes: 7 additions & 0 deletions ssh/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/vladimirvivien/gexe"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/utils/net"
)

// CopyFrom copies one or more files using SCP from remote host
Expand Down Expand Up @@ -167,9 +168,15 @@ func makeSCPCmdStr(progName string, args SSHArgs) (string, error) {
}

func getCopyFromSourceTarget(args SSHArgs, sourcePath, targetPath string) string {
if net.IsIPv6String(args.Host) {
return fmt.Sprintf("%s@[%s]:%s %s", args.User, args.Host, sourcePath, targetPath)
}
return fmt.Sprintf("%s@%s:%s %s", args.User, args.Host, sourcePath, targetPath)
}

func getCopyToSourceTarget(args SSHArgs, sourcePath, targetPath string) string {
if net.IsIPv6String(args.Host) {
return fmt.Sprintf("%s %s@[%s]:%s", sourcePath, args.User, args.Host, targetPath)
}
return fmt.Sprintf("%s %s@%s:%s", sourcePath, args.User, args.Host, targetPath)
}
48 changes: 48 additions & 0 deletions ssh/scp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ func TestCopyFrom(t *testing.T) {
srcFile: "bar/",
fileContent: "FooBar",
},
{
name: "copy single file from IPv6 host",
sshArgs: testSSHArgsIPv6,
remoteFiles: map[string]string{"foo.txt": "FooBar"},
srcFile: "foo.txt",
fileContent: "FooBar",
},
{
name: "copy single file in dir from IPv6 host",
sshArgs: testSSHArgsIPv6,
remoteFiles: map[string]string{"foo/bar.txt": "FooBar"},
srcFile: "foo/bar.txt",
fileContent: "FooBar",
},
{
name: "copy dir from IPv6 host",
sshArgs: testSSHArgsIPv6,
remoteFiles: map[string]string{"bar/foo.csv": "FooBar", "bar/bar.txt": "BarBar"},
srcFile: "bar/",
fileContent: "FooBar",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -111,6 +132,27 @@ func TestCopyTo(t *testing.T) {
file: "local-bar/",
fileContent: "FooBar",
},
{
name: "copy single file to remote IPv6 host",
sshArgs: testSSHArgsIPv6,
localFiles: map[string]string{"local-foo.txt": "FooBar"},
file: "local-foo.txt",
fileContent: "FooBar",
},
{
name: "copy single file in dir to remote IPv6 host",
sshArgs: testSSHArgsIPv6,
localFiles: map[string]string{"local-foo/local-bar.txt": "FooBar"},
file: "local-foo/local-bar.txt",
fileContent: "FooBar",
},
{
name: "copy dir entire dir to remote IPv6 host",
sshArgs: testSSHArgsIPv6,
localFiles: map[string]string{"local-bar/local-foo.csv": "FooBar", "local-bar/local-bar.txt": "BarBar"},
file: "local-bar/",
fileContent: "FooBar",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -175,6 +217,12 @@ func TestMakeSCPCmdStr(t *testing.T) {
args: SSHArgs{User: "sshuser"},
shouldFail: true,
},
{
name: "IPv6 host",
args: SSHArgs{User: "sshuser", Host: "b::1"},
source: "/tmp/any",
cmdStr: "scp -rpq -o StrictHostKeyChecking=no -P 22",
},
}

for _, test := range tests {
Expand Down
26 changes: 26 additions & 0 deletions ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func TestRunRead(t *testing.T) {
cmd: "echo 'Hello World!'",
result: "Hello World!",
},
{
name: "simple cmd on IPv6 host",
args: testSSHArgsIPv6,
cmd: "echo 'Hello World!'",
result: "Hello World!",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -102,6 +108,26 @@ func TestSSHRunMakeCmdStr(t *testing.T) {
args: SSHArgs{User: "sshuser"},
shouldFail: true,
},
{
name: "user and IPv6 host",
args: SSHArgs{User: "sshuser", Host: "b::1"},
cmdStr: "ssh -q -o StrictHostKeyChecking=no -p 22 sshuser@b::1",
},
{
name: "user IPv6 host and pkpath",
args: SSHArgs{User: "sshuser", Host: "b::1", PrivateKeyPath: "/pk/path"},
cmdStr: "ssh -q -o StrictHostKeyChecking=no -i /pk/path -p 22 sshuser@b::1",
},
{
name: "user IPv6 host pkpath and proxy",
args: SSHArgs{User: "sshuser", Host: "b::1", PrivateKeyPath: "/pk/path", ProxyJump: &ProxyJumpArgs{User: "juser", Host: "jhost"}},
cmdStr: "ssh -q -o StrictHostKeyChecking=no -i /pk/path -p 22 sshuser@b::1 -o \"ProxyCommand ssh -o StrictHostKeyChecking=no -W %h:%p -i /pk/path juser@jhost\"",
},
{
name: "user IPv6 host and IPv6 proxy",
args: SSHArgs{User: "sshuser", Host: "b::1", ProxyJump: &ProxyJumpArgs{User: "juser", Host: "::a"}},
cmdStr: "ssh -q -o StrictHostKeyChecking=no -p 22 sshuser@b::1 -o \"ProxyCommand ssh -o StrictHostKeyChecking=no -W %h:%p juser@::a\"",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 1af06cf

Please sign in to comment.