Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow proxy during SSH host key scan in FluxCD CLI #827

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ssh/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ go 1.22.0
require (
github.com/onsi/gomega v1.34.2
golang.org/x/crypto v0.27.0
golang.org/x/net v0.29.0
)

require (
github.com/google/go-cmp v0.6.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
27 changes: 23 additions & 4 deletions ssh/host_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ limitations under the License.
package ssh

import (
"context"
"encoding/base64"
"fmt"
"net"
"time"

"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
"golang.org/x/net/proxy"
)

// ScanHostKey collects the given host's preferred public key for the
Expand All @@ -45,16 +47,33 @@ func ScanHostKey(host string, timeout time.Duration, clientHostKeyAlgos []string
config.HostKeyAlgorithms = clientHostKeyAlgos
}

client, err := ssh.Dial("tcp", host, config)
if err == nil {
defer client.Close()
}
err := sshDial(host, config)

if len(col.knownKeys) > 0 {
return col.knownKeys, nil
}

return col.knownKeys, err
}

func sshDial(host string, config *ssh.ClientConfig) error {
ctx, cancel := context.WithTimeout(context.Background(), config.Timeout)
defer cancel()
// this reads the ALL_PROXY environment varaible
conn, err := proxy.Dial(ctx, "tcp", host)
if err != nil {
return err
}
c, chans, reqs, err := ssh.NewClientConn(conn, host, config)
if err != nil {
return err
}
client := ssh.NewClient(c, chans, reqs)
defer client.Close()

return nil
}

// HostKeyCollector offers a StoreKey method which provides an
// HostKeyCallBack to collect public keys from an SSH server.
type HostKeyCollector struct {
Expand Down
Loading