Skip to content

Commit d0fe81e

Browse files
committed
use proxy.Dial instead of net.Dial for ScanHostKey
ssh.Dial uses net.DialTimeout under the hood and there is no possibility to use a proxy when running command like `flux create source git` so we use almost all internal implementation of ssh.Dial except net.DialTimeout is replaced with proxy.Dial like it is done in go-git
1 parent aa0e9ce commit d0fe81e

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

ssh/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ go 1.22.0
55
require (
66
github.com/onsi/gomega v1.34.2
77
golang.org/x/crypto v0.27.0
8+
golang.org/x/net v0.29.0
89
)
910

1011
require (
1112
github.com/google/go-cmp v0.6.0 // indirect
12-
golang.org/x/net v0.29.0 // indirect
1313
golang.org/x/sys v0.25.0 // indirect
1414
golang.org/x/text v0.18.0 // indirect
1515
gopkg.in/yaml.v3 v3.0.1 // indirect

ssh/host_key.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ limitations under the License.
1717
package ssh
1818

1919
import (
20+
"context"
2021
"encoding/base64"
2122
"fmt"
2223
"net"
2324
"time"
2425

2526
"golang.org/x/crypto/ssh"
2627
"golang.org/x/crypto/ssh/knownhosts"
28+
"golang.org/x/net/proxy"
2729
)
2830

2931
// ScanHostKey collects the given host's preferred public key for the
@@ -45,10 +47,20 @@ func ScanHostKey(host string, timeout time.Duration, clientHostKeyAlgos []string
4547
config.HostKeyAlgorithms = clientHostKeyAlgos
4648
}
4749

48-
client, err := ssh.Dial("tcp", host, config)
49-
if err == nil {
50-
defer client.Close()
50+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
51+
defer cancel()
52+
// support for ALL_PROXY ENV varaible
53+
conn, err := proxy.Dial(ctx, "tcp", host)
54+
if err != nil {
55+
return nil, err
5156
}
57+
c, chans, reqs, err := ssh.NewClientConn(conn, host, config)
58+
if err != nil {
59+
return nil, err
60+
}
61+
client := ssh.NewClient(c, chans, reqs)
62+
defer client.Close()
63+
5264
if len(col.knownKeys) > 0 {
5365
return col.knownKeys, nil
5466
}

0 commit comments

Comments
 (0)