From 444f0b61759d96c9dbe84fe10215f4a54e5595ec Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 1 Jul 2024 00:57:18 +0000 Subject: [PATCH] Bump github.com/bramvdbogaerde/go-scp from 1.2.1 to 1.4.0 Bumps [github.com/bramvdbogaerde/go-scp](https://github.com/bramvdbogaerde/go-scp) from 1.2.1 to 1.4.0. - [Release notes](https://github.com/bramvdbogaerde/go-scp/releases) - [Commits](https://github.com/bramvdbogaerde/go-scp/compare/v1.2.1...v1.4.0) --- updated-dependencies: - dependency-name: github.com/bramvdbogaerde/go-scp dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 6 +- .../bramvdbogaerde/go-scp/README.md | 2 +- .../bramvdbogaerde/go-scp/client.go | 98 +++++++++++++------ .../bramvdbogaerde/go-scp/configurer.go | 9 +- .../github.com/bramvdbogaerde/go-scp/scp.go | 12 +-- vendor/modules.txt | 4 +- 7 files changed, 83 insertions(+), 50 deletions(-) diff --git a/go.mod b/go.mod index 5771ed2ecb7..0b986db115f 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.22 toolchain go1.22.4 require ( - github.com/bramvdbogaerde/go-scp v1.2.1 + github.com/bramvdbogaerde/go-scp v1.4.0 github.com/canonical/go-efilib v0.9.5 github.com/cavaliergopher/grab/v3 v3.0.1 github.com/cenkalti/backoff/v4 v4.2.1 diff --git a/go.sum b/go.sum index f0a4d9cc384..af3cd53e69d 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= -github.com/bramvdbogaerde/go-scp v1.2.1 h1:BKTqrqXiQYovrDlfuVFaEGz0r4Ou6EED8L7jCXw6Buw= -github.com/bramvdbogaerde/go-scp v1.2.1/go.mod h1:s4ZldBoRAOgUg8IrRP2Urmq5qqd2yPXQTPshACY8vQ0= +github.com/bramvdbogaerde/go-scp v1.4.0 h1:jKMwpwCbcX1KyvDbm/PDJuXcMuNVlLGi0Q0reuzjyKY= +github.com/bramvdbogaerde/go-scp v1.4.0/go.mod h1:on2aH5AxaFb2G0N5Vsdy6B0Ml7k9HuHSwfo1y0QzAbQ= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/canonical/go-efilib v0.9.5 h1:zRpWG4z61GiYsEmFYvXYuj+8xV2eJ200YY5Ht9EjrRU= github.com/canonical/go-efilib v0.9.5/go.mod h1:tHjv3Mni7hEpNSUNd1KJEV/AZJsFSH6LX/EQ0I75AZE= @@ -447,7 +447,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200420201142-3c4aac89819a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= @@ -519,7 +518,6 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/vendor/github.com/bramvdbogaerde/go-scp/README.md b/vendor/github.com/bramvdbogaerde/go-scp/README.md index 041988da87b..e37a76c188a 100644 --- a/vendor/github.com/bramvdbogaerde/go-scp/README.md +++ b/vendor/github.com/bramvdbogaerde/go-scp/README.md @@ -46,7 +46,7 @@ func main() { // Close the file after it has been copied defer f.Close() - // Finaly, copy the file over + // Finally, copy the file over // Usage: CopyFromFile(context, file, remotePath, permission) // the context can be adjusted to provide time-outs or inherit from other contexts if this is embedded in a larger application. diff --git a/vendor/github.com/bramvdbogaerde/go-scp/client.go b/vendor/github.com/bramvdbogaerde/go-scp/client.go index a12dddc0476..ee7d6f808a1 100644 --- a/vendor/github.com/bramvdbogaerde/go-scp/client.go +++ b/vendor/github.com/bramvdbogaerde/go-scp/client.go @@ -1,4 +1,4 @@ -/* Copyright (c) 2021 Bram Vandenbogaerde And Contributors +/* Copyright (c) 2024 Bram Vandenbogaerde And Contributors * You may use, distribute or modify this code under the * terms of the Mozilla Public License 2.0, which is distributed * along with the source code. @@ -21,6 +21,27 @@ import ( "golang.org/x/crypto/ssh" ) +// Callback for freeing managed resources +type ICloseHandler interface { + Close() +} + +// Close handler equivalent to a no-op. Used by default +// when no resources have to be cleaned. +type EmptyHandler struct{} + +func (EmptyHandler) Close() {} + +// Close handler to close an SSH client +type CloseSSHCLient struct { + // Reference to the used SSH client + sshClient *ssh.Client +} + +func (scp CloseSSHCLient) Close() { + scp.sshClient.Close() +} + type PassThru func(r io.Reader, total int64) io.Reader type Client struct { @@ -30,11 +51,8 @@ type Client struct { // ClientConfig the client config to use. ClientConfig *ssh.ClientConfig - // Session stores the SSH session while the connection is running. - Session *ssh.Session - - // Conn stores the SSH connection itself in order to close it after transfer. - Conn ssh.Conn + // Keep the ssh client around for generating new sessions + sshClient *ssh.Client // Timeout the maximal amount of time to wait for a file transfer to complete. // Deprecated: use context.Context for each function instead. @@ -42,27 +60,30 @@ type Client struct { // RemoteBinary the absolute path to the remote SCP binary. RemoteBinary string + + // Handler called when calling `Close` to clean up any remaining + // resources managed by `Client`. + closeHandler ICloseHandler } // Connect connects to the remote SSH server, returns error if it couldn't establish a session to the SSH server. func (a *Client) Connect() error { - if a.Session != nil { - return nil - } - client, err := ssh.Dial("tcp", a.Host, a.ClientConfig) if err != nil { return err } - a.Conn = client.Conn - a.Session, err = client.NewSession() - if err != nil { - return err - } + a.sshClient = client + a.closeHandler = CloseSSHCLient{sshClient: client} return nil } +// Returns the underlying SSH client, this should be used carefully as +// it will be closed by `client.Close`. +func (a *Client) SSHClient() *ssh.Client { + return a.sshClient +} + // CopyFromFile copies the contents of an os.File to a remote location, it will get the length of the file by looking it up from the filesystem. func (a *Client) CopyFromFile(ctx context.Context, file os.File, remotePath string, permissions string) error { return a.CopyFromFilePassThru(ctx, file, remotePath, permissions, nil) @@ -139,11 +160,17 @@ func (a *Client) Copy(ctx context.Context, r io.Reader, remotePath string, permi // CopyPassThru copies the contents of an io.Reader to a remote location. // Access copied bytes by providing a PassThru reader factory func (a *Client) CopyPassThru(ctx context.Context, r io.Reader, remotePath string, permissions string, size int64, passThru PassThru) error { - stdout, err := a.Session.StdoutPipe() + session, err := a.sshClient.NewSession() + if err != nil { + return fmt.Errorf("Error creating ssh session in copy to remote: %v", err) + } + defer session.Close() + + stdout, err := session.StdoutPipe() if err != nil { return err } - w, err := a.Session.StdinPipe() + w, err := session.StdinPipe() if err != nil { return err } @@ -155,11 +182,19 @@ func (a *Client) CopyPassThru(ctx context.Context, r io.Reader, remotePath strin filename := path.Base(remotePath) + // Start the command first and get confirmation that it has been started + // before sending anything through the pipes. + err = session.Start(fmt.Sprintf("%s -qt %q", a.RemoteBinary, remotePath)) + if err != nil { + return err + } + wg := sync.WaitGroup{} wg.Add(2) errCh := make(chan error, 2) + // SCP protocol and file sending go func() { defer wg.Done() defer w.Close() @@ -193,31 +228,37 @@ func (a *Client) CopyPassThru(ctx context.Context, r io.Reader, remotePath strin } }() + // Wait for the process to exit go func() { defer wg.Done() - err := a.Session.Run(fmt.Sprintf("%s -qt %q", a.RemoteBinary, remotePath)) + err := session.Wait() if err != nil { errCh <- err return } }() + // If there is a timeout, stop the transfer if it has been exceeded if a.Timeout > 0 { var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, a.Timeout) defer cancel() } + // Wait for one of the conditions (error/timeout/completion) to occur if err := wait(&wg, ctx); err != nil { return err } close(errCh) + + // Collect any errors from the error channel for err := range errCh { if err != nil { return err } } + return nil } @@ -232,6 +273,12 @@ func (a *Client) CopyFromRemote(ctx context.Context, file *os.File, remotePath s // to keep track of progress and how many bytes that were download from the remote. // `passThru` can be set to nil to disable this behaviour. func (a *Client) CopyFromRemotePassThru(ctx context.Context, w io.Writer, remotePath string, passThru PassThru) error { + session, err := a.sshClient.NewSession() + if err != nil { + return fmt.Errorf("Error creating ssh session in copy from remote: %v", err) + } + defer session.Close() + wg := sync.WaitGroup{} errCh := make(chan error, 4) @@ -247,20 +294,20 @@ func (a *Client) CopyFromRemotePassThru(ctx context.Context, w io.Writer, remote }() - r, err := a.Session.StdoutPipe() + r, err := session.StdoutPipe() if err != nil { errCh <- err return } - in, err := a.Session.StdinPipe() + in, err := session.StdinPipe() if err != nil { errCh <- err return } defer in.Close() - err = a.Session.Start(fmt.Sprintf("%s -f %q", a.RemoteBinary, remotePath)) + err = session.Start(fmt.Sprintf("%s -f %q", a.RemoteBinary, remotePath)) if err != nil { errCh <- err return @@ -310,7 +357,7 @@ func (a *Client) CopyFromRemotePassThru(ctx context.Context, w io.Writer, remote return } - err = a.Session.Wait() + err = session.Wait() if err != nil { errCh <- err return @@ -332,10 +379,5 @@ func (a *Client) CopyFromRemotePassThru(ctx context.Context, w io.Writer, remote } func (a *Client) Close() { - if a.Session != nil { - a.Session.Close() - } - if a.Conn != nil { - a.Conn.Close() - } + a.closeHandler.Close() } diff --git a/vendor/github.com/bramvdbogaerde/go-scp/configurer.go b/vendor/github.com/bramvdbogaerde/go-scp/configurer.go index faaf192f568..3f1c16e41b8 100644 --- a/vendor/github.com/bramvdbogaerde/go-scp/configurer.go +++ b/vendor/github.com/bramvdbogaerde/go-scp/configurer.go @@ -20,6 +20,7 @@ type ClientConfigurer struct { session *ssh.Session timeout time.Duration remoteBinary string + sshClient *ssh.Client } // NewConfigurer creates a new client configurer. @@ -64,9 +65,8 @@ func (c *ClientConfigurer) ClientConfig(config *ssh.ClientConfig) *ClientConfigu return c } -// Session alters the ssh.Session. -func (c *ClientConfigurer) Session(session *ssh.Session) *ClientConfigurer { - c.session = session +func (c *ClientConfigurer) SSHClient(sshClient *ssh.Client) *ClientConfigurer { + c.sshClient = sshClient return c } @@ -77,6 +77,7 @@ func (c *ClientConfigurer) Create() Client { ClientConfig: c.clientConfig, Timeout: c.timeout, RemoteBinary: c.remoteBinary, - Session: c.session, + sshClient: c.sshClient, + closeHandler: EmptyHandler{}, } } diff --git a/vendor/github.com/bramvdbogaerde/go-scp/scp.go b/vendor/github.com/bramvdbogaerde/go-scp/scp.go index 3e9073ee56c..837ccaa8f1f 100644 --- a/vendor/github.com/bramvdbogaerde/go-scp/scp.go +++ b/vendor/github.com/bramvdbogaerde/go-scp/scp.go @@ -27,19 +27,11 @@ func NewClientWithTimeout(host string, config *ssh.ClientConfig, timeout time.Du // NewClientBySSH returns a new scp.Client using an already existing established SSH connection. func NewClientBySSH(ssh *ssh.Client) (Client, error) { - session, err := ssh.NewSession() - if err != nil { - return Client{}, err - } - return NewConfigurer("", nil).Session(session).Create(), nil + return NewConfigurer("", nil).SSHClient(ssh).Create(), nil } // NewClientBySSHWithTimeout same as NewClientWithTimeout but uses an existing SSH client. // Deprecated: provide meaningful context to each "Copy*" function instead. func NewClientBySSHWithTimeout(ssh *ssh.Client, timeout time.Duration) (Client, error) { - session, err := ssh.NewSession() - if err != nil { - return Client{}, err - } - return NewConfigurer("", nil).Session(session).Timeout(timeout).Create(), nil + return NewConfigurer("", nil).SSHClient(ssh).Timeout(timeout).Create(), nil } diff --git a/vendor/modules.txt b/vendor/modules.txt index 1b1282fec72..d04837ae4e8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -58,8 +58,8 @@ github.com/ProtonMail/go-crypto/openpgp/s2k # github.com/StackExchange/wmi v1.2.1 ## explicit; go 1.13 github.com/StackExchange/wmi -# github.com/bramvdbogaerde/go-scp v1.2.1 -## explicit; go 1.13 +# github.com/bramvdbogaerde/go-scp v1.4.0 +## explicit; go 1.21 github.com/bramvdbogaerde/go-scp # github.com/canonical/go-efilib v0.9.5 ## explicit; go 1.18