From 5d1a37fab8d3133bfa137f660b1c390bf6a647d8 Mon Sep 17 00:00:00 2001 From: LiZhenCheng9527 Date: Wed, 22 Nov 2023 19:23:52 +0800 Subject: [PATCH] Replaced go-scp package Signed-off-by: LiZhenCheng9527 --- go.mod | 1 - go.sum | 4 ---- pkg/plugin/join/kubeedge/kubeedge.go | 25 +++++++++++++++++-------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index d754559b8..25eba2ea4 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,6 @@ require ( github.com/agiledragon/gomonkey/v2 v2.10.1 github.com/aws/aws-sdk-go v1.44.191 github.com/awslabs/goformation/v4 v4.19.5 - github.com/bramvdbogaerde/go-scp v1.2.0 github.com/cert-manager/cert-manager v1.10.0 github.com/coreos/go-semver v0.3.0 github.com/fluxcd/helm-controller/api v0.32.2 diff --git a/go.sum b/go.sum index 5fb3d792e..0b45e175b 100644 --- a/go.sum +++ b/go.sum @@ -303,8 +303,6 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dR github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI= github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= -github.com/bramvdbogaerde/go-scp v1.2.0 h1:mNF1lCXQ6jQcxCBBuc2g/CQwVy/4QONaoD5Aqg9r+Zg= -github.com/bramvdbogaerde/go-scp v1.2.0/go.mod h1:s4ZldBoRAOgUg8IrRP2Urmq5qqd2yPXQTPshACY8vQ0= github.com/brancz/gojsontoyaml v0.0.0-20190425155809-e8bd32d46b3d/go.mod h1:IyUJYN1gvWjtLF5ZuygmxbnsAyP3aJS6cHzIuZY50B0= github.com/briankassouf/jose v0.9.2-0.20180619214549-d2569464773f/go.mod h1:HQhVmdUf7dBNwIIdBTivnCDxcf6IZY3/zrb+uKSJz6Y= github.com/bshuster-repo/logrus-logstash-hook v0.4.1/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk= @@ -2218,7 +2216,6 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= -golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -2519,7 +2516,6 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/pkg/plugin/join/kubeedge/kubeedge.go b/pkg/plugin/join/kubeedge/kubeedge.go index 94abe8723..c5e3067b1 100644 --- a/pkg/plugin/join/kubeedge/kubeedge.go +++ b/pkg/plugin/join/kubeedge/kubeedge.go @@ -19,12 +19,13 @@ package kubeedge import ( "context" "fmt" + "io" "os" "path/filepath" "runtime" "strings" - "github.com/bramvdbogaerde/go-scp" + "github.com/pkg/sftp" "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" corev1 "k8s.io/api/core/v1" @@ -189,12 +190,9 @@ func (p *JoinPlugin) scpKeadm(sshClient *ssh.Client) error { return nil } - client, err := scp.NewClientBySSH(sshClient) + client, err := sftp.NewClient(sshClient) if err != nil { - return fmt.Errorf("creating new SSH session from existing connection fail, %w", err) - } - if err := client.Connect(); err != nil { - return fmt.Errorf("establish a connection to the remote server fail, %w", err) + return fmt.Errorf("creating new SFTP session from existing connection failed: %w", err) } defer client.Close() @@ -204,9 +202,20 @@ func (p *JoinPlugin) scpKeadm(sshClient *ssh.Client) error { } defer f.Close() - if err := client.CopyFromFile(context.Background(), *f, destPath, "0755"); err != nil { - return fmt.Errorf("scp file fail, %w", err) + dstFile, err := client.Create(destPath) + if err != nil { + return fmt.Errorf("failed to create remote file: %w", err) + } + defer dstFile.Close() + + if _, err := io.Copy(dstFile, f); err != nil { + return fmt.Errorf("failed to copy file to remote server: %w", err) } + + if err := dstFile.Chmod(0755); err != nil { + return fmt.Errorf("failed to change file permissions on remote server: %w", err) + } + return nil }