-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add an SFTP service definition - Implement an SFTP upload service method using native Go SSH and SFTP packages - Add SFTP configuration with some default values - Generate a service mock with mockgen - Add tests against a test SFTP server
- Loading branch information
Showing
21 changed files
with
656 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package sftp | ||
|
||
import "path/filepath" | ||
|
||
type Config struct { | ||
Host string | ||
Port string | ||
|
||
KnownHostsFile string | ||
PrivateKey PrivateKey | ||
} | ||
|
||
type PrivateKey struct { | ||
Path string | ||
Passphrase string | ||
} | ||
|
||
// SetDefaults sets default values for some configs. | ||
func (c *Config) SetDefaults() { | ||
if c.Host == "" { | ||
c.Host = "localhost" | ||
} | ||
|
||
if c.Port == "" { | ||
c.Port = "22" | ||
} | ||
|
||
if c.KnownHostsFile == "" { | ||
c.KnownHostsFile = filepath.Join("$HOME", ".ssh", "known_hosts") | ||
} | ||
|
||
if c.PrivateKey.Path == "" { | ||
c.PrivateKey.Path = filepath.Join("$HOME", ".ssh", "id_rsa") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package sftp | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/pkg/sftp" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
// GoClient implements the SFTP service using native Go SSH and SFTP packages. | ||
type GoClient struct { | ||
cfg Config | ||
|
||
ssh *ssh.Client | ||
sftp *sftp.Client | ||
} | ||
|
||
var _ Service = (*GoClient)(nil) | ||
|
||
// NewGoClient returns a new GoSFTP client with the given configuration. | ||
func NewGoClient(cfg Config) *GoClient { | ||
cfg.SetDefaults() | ||
|
||
return &GoClient{cfg: cfg} | ||
} | ||
|
||
// Upload writes the data from src to the remote file at dest and returns the | ||
// number of bytes written. A new SFTP connection is opened before writing, and | ||
// closed when the upload is complete. | ||
func (c *GoClient) Upload(src io.Reader, dest string) (int64, error) { | ||
if err := c.dial(); err != nil { | ||
return 0, err | ||
} | ||
defer c.close() | ||
|
||
// Note: Some SFTP servers don't support O_RDWR mode. | ||
w, err := c.sftp.OpenFile(dest, (os.O_WRONLY | os.O_CREATE | os.O_TRUNC)) | ||
if err != nil { | ||
return 0, fmt.Errorf("SFTP: couldn't create remote file %q: %w", dest, err) | ||
} | ||
|
||
bytes, err := io.Copy(w, src) | ||
if err != nil { | ||
return 0, fmt.Errorf("SFTP: failed to write to %q: %w", dest, err) | ||
} | ||
|
||
return bytes, nil | ||
} | ||
|
||
// Dial connects to an SSH host then creates an SFTP client on the connection. | ||
// When the clients are no longer needed, close() must be called to prevent | ||
// leaks. | ||
func (c *GoClient) dial() error { | ||
sshc, err := SSHConnect(c.cfg) | ||
if err != nil { | ||
return fmt.Errorf("SSH: %w", err) | ||
} | ||
c.ssh = sshc | ||
|
||
sftpc, err := sftp.NewClient(sshc) | ||
if err != nil { | ||
return fmt.Errorf("Unable to start SFTP subsystem: %w", err) | ||
} | ||
c.sftp = sftpc | ||
|
||
return nil | ||
} | ||
|
||
// Close closes the SFTP client first, then the SSH client. | ||
func (c *GoClient) close() error { | ||
var errs error | ||
|
||
if err := c.sftp.Close(); err != nil { | ||
errs = errors.Join(err, errs) | ||
} | ||
if err := c.ssh.Close(); err != nil { | ||
errs = errors.Join(err, errs) | ||
} | ||
|
||
return errs | ||
} |
Oops, something went wrong.