From e7b276d6a5951ed7d174b5df24897b17665baed5 Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Tue, 28 Feb 2023 17:11:17 -0800 Subject: [PATCH] reuse VerifyHostKeyFromKnownHosts (#143) --- libplugin/util.go | 21 +++++++++++++++++++++ plugin/internal/workingdir/workingdir.go | 17 +++-------------- plugin/kubernetes/kubernetes.go | 19 +------------------ plugin/yaml/yaml.go | 19 +------------------ 4 files changed, 26 insertions(+), 50 deletions(-) diff --git a/libplugin/util.go b/libplugin/util.go index 1662e54f..03f0797e 100644 --- a/libplugin/util.go +++ b/libplugin/util.go @@ -7,6 +7,8 @@ import ( "strconv" "github.com/sirupsen/logrus" + "golang.org/x/crypto/ssh" + "golang.org/x/crypto/ssh/knownhosts" ) func AuthMethodTypeToName(a AuthMethod) string { @@ -132,3 +134,22 @@ func CreateNextPluginAuth(meta map[string]string) *Upstream_NextPlugin { }, } } + +func VerifyHostKeyFromKnownHosts(knownhostsData io.Reader, hostname, netaddr string, key []byte) error { + hostKeyCallback, err := knownhosts.NewFromReader(knownhostsData) + if err != nil { + return err + } + + pub, err := ssh.ParsePublicKey(key) + if err != nil { + return err + } + + addr, err := net.ResolveTCPAddr("tcp", netaddr) + if err != nil { + return err + } + + return hostKeyCallback(hostname, addr, pub) +} diff --git a/plugin/internal/workingdir/workingdir.go b/plugin/internal/workingdir/workingdir.go index 54c39656..f17f619d 100644 --- a/plugin/internal/workingdir/workingdir.go +++ b/plugin/internal/workingdir/workingdir.go @@ -4,7 +4,6 @@ import ( "bufio" "bytes" "fmt" - "net" "os" "path" "regexp" @@ -12,7 +11,6 @@ import ( "github.com/tg123/sshpiper/libplugin" "golang.org/x/crypto/ssh" - "golang.org/x/crypto/ssh/knownhosts" log "github.com/sirupsen/logrus" ) @@ -96,22 +94,13 @@ func (w *Workingdir) VerifyHostKey(hostname, netaddr string, key []byte) error { return nil } - hostKeyCallback, err := knownhosts.New(w.fullpath(userKnownHosts)) + f, err := os.Open(w.fullpath(userKnownHosts)) if err != nil { return err } + defer f.Close() - pub, err := ssh.ParsePublicKey(key) - if err != nil { - return err - } - - addr, err := net.ResolveTCPAddr("tcp", netaddr) - if err != nil { - return err - } - - return hostKeyCallback(hostname, addr, pub) + return libplugin.VerifyHostKeyFromKnownHosts(f, hostname, netaddr, key) } func (w *Workingdir) checkPerm(file string) error { diff --git a/plugin/kubernetes/kubernetes.go b/plugin/kubernetes/kubernetes.go index 89d2646c..3b25f89e 100644 --- a/plugin/kubernetes/kubernetes.go +++ b/plugin/kubernetes/kubernetes.go @@ -5,7 +5,6 @@ import ( "context" "encoding/base64" "fmt" - "net" "regexp" "time" @@ -15,7 +14,6 @@ import ( sshpiper "github.com/tg123/sshpiper/plugin/kubernetes/generated/clientset/versioned" piperlister "github.com/tg123/sshpiper/plugin/kubernetes/generated/listers/sshpiper/v1beta1" "golang.org/x/crypto/ssh" - "golang.org/x/crypto/ssh/knownhosts" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/labels" @@ -125,22 +123,7 @@ func (p *plugin) verifyHostKey(conn libplugin.ConnMetadata, hostname, netaddr st return err } - hostKeyCallback, err := knownhosts.NewFromReader(bytes.NewBuffer(data)) - if err != nil { - return err - } - - pub, err := ssh.ParsePublicKey(key) - if err != nil { - return err - } - - addr, err := net.ResolveTCPAddr("tcp", netaddr) - if err != nil { - return err - } - - return hostKeyCallback(hostname, addr, pub) + return libplugin.VerifyHostKeyFromKnownHosts(bytes.NewBuffer(data), hostname, netaddr, key) } func (p *plugin) createUpstream(conn libplugin.ConnMetadata, pipe *piperv1beta1.Pipe, originPassword string) (*libplugin.Upstream, error) { diff --git a/plugin/yaml/yaml.go b/plugin/yaml/yaml.go index 67fe3936..83b093a8 100644 --- a/plugin/yaml/yaml.go +++ b/plugin/yaml/yaml.go @@ -6,7 +6,6 @@ import ( "bytes" "encoding/base64" "fmt" - "net" "os" "path/filepath" "regexp" @@ -15,7 +14,6 @@ import ( "github.com/patrickmn/go-cache" "github.com/tg123/sshpiper/libplugin" "golang.org/x/crypto/ssh" - "golang.org/x/crypto/ssh/knownhosts" "gopkg.in/yaml.v3" ) @@ -173,22 +171,7 @@ func (p *plugin) verifyHostKey(conn libplugin.ConnMetadata, hostname, netaddr st return err } - hostKeyCallback, err := knownhosts.NewFromReader(bytes.NewBuffer(data)) - if err != nil { - return err - } - - pub, err := ssh.ParsePublicKey(key) - if err != nil { - return err - } - - addr, err := net.ResolveTCPAddr("tcp", netaddr) - if err != nil { - return err - } - - return hostKeyCallback(hostname, addr, pub) + return libplugin.VerifyHostKeyFromKnownHosts(bytes.NewBuffer(data), hostname, netaddr, key) } func (p *plugin) createUpstream(conn libplugin.ConnMetadata, to pipeConfigTo, originPassword string) (*libplugin.Upstream, error) {