Skip to content
moov-ioPublic

About

A Go client for performing common SFTP operations

Topics

Resources

Contributing

Security policy

Stars

58 stars

Watchers

5 watching

Forks

Latest commit

 

History

666 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Moov Banner Logo

Community · Blog

GoDoc Build Status Go Report Card Apache 2 License Slack Channel Twitter

moov-io/go-sftp

Moov's mission is to give developers an easy way to create and integrate bank processing into their own software products. Our open source projects are each focused on solving a single responsibility in financial services and designed around performance, scalability, and ease of use.

moov-io/go-sftp provides a simple SFTP client for uploading, listing, and opening files on an SFTP server.

This project requires Go 1.26 or higher. The current go.mod also sets toolchain go1.27.4.

The package name is go_sftp, so import it with an alias:

import (
    "io"
    "strings"

    "github.com/moov-io/base/log"
    gosftp "github.com/moov-io/go-sftp"
)

func main() {
    client, err := gosftp.NewClient(log.NewDefaultLogger(), &gosftp.ClientConfig{
        Hostname:       "sftp.example.com:22",
        Username:       "alice",
        Password:       "correct-horse",
        MaxConnections: 1,
        HostPublicKeys: []string{"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleHostKey"},
    })
    if err != nil {
        // client may still be non-nil; later calls will retry the connection
        panic(err)
    }
    defer client.Close()

    // Relative named dirs such as "outbox" only list correctly when Getwd is "/".
    files, err := client.ListFiles("outbox")
    if err != nil {
        panic(err)
    }

    for _, path := range files {
        file, err := client.Reader(path)
        if err != nil {
            panic(err)
        }
        // ... read file.Contents, then file.Close()
        file.Close()
    }

    err = client.UploadFile("inbox/ppd.ach", io.NopCloser(strings.NewReader("file contents")))
    if err != nil {
        panic(err)
    }

    err = client.Delete("outbox/processed.ach")
    if err != nil {
        panic(err)
    }
}

A nil logger is allowed. MaxConnections must be >= 1. Leaving it at zero (or a negative value) makes NewClient return a non-nil Client and an error from pkg/sftp after SSH connects: sftp: sftp connect: n must be greater or equal to 1. The caller's ClientConfig is not mutated.

type Client interface {
	Ping() error
	Close() error

	Open(path string) (*File, error)
	Reader(path string) (*File, error)

	Delete(path string) error
	UploadFile(path string, contents io.ReadCloser) error

	ListFiles(dir string) ([]string, error)
	Walk(dir string, fn fs.WalkDirFunc) error
}

ListFiles returns the files in dir (not subdirectories; use Walk for a recursive listing). Matching is case-insensitive, and results keep the server's spelling. On a case-sensitive server that has sibling directories differing only by case (for example Inbound and inbound), a listing of either directory can include files from all of them.

Named relative directories such as "outbox" are resolved by walking from Getwd with a glob of the form [/?]<dir>/*. That only lists correctly when Getwd is / (typical of chroot servers). On a nested home directory the same call returns an empty list with no error. Absolute directories work only when they are under Getwd; an absolute path outside Getwd returns an empty list and no error. ListFiles("/") lists the working directory (not the real filesystem root) and prefixes results with /. Symlinked directories are not followed.

ListFiles walks the entire tree under Getwd, so cost grows with the whole tree, and one unreadable directory anywhere under Getwd makes every ListFiles call fail (for example listing ... failed: permission denied).

Delete returns nil if the file does not exist.

The library also includes a mock client implementation which uses a local filesystem temporary directory for testing. NewMockClient requires a *testing.T and is intended for tests. The mock differs from the real client in a few ways: Open/Reader set Filename to the base name of the path; Delete of a missing file returns an error; ListFiles includes directory entries, returns paths joined with dir, and creates dir with MkdirAll (errors ignored).

Security / host keys

If HostPublicKey and HostPublicKeys are both empty, the client skips host-key verification (ssh.InsecureIgnoreHostKey). A warning is logged when the logger is non-nil. For any server that moves ACH or other financial files, pin the host key.

HostPublicKeys accepts one key per slice element, in either of these formats:

  • an authorized_keys line, which is what ssh-keyscan prints
  • a base64-encoded key
ssh-keyscan -t ed25519,rsa sftp.example.com
cfg := &gosftp.ClientConfig{
    Hostname:       "sftp.example.com:22",
    Username:       "alice",
    Password:       "correct-horse",
    MaxConnections: 1,
    HostPublicKeys: []string{
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleHostKey",
    },
}

The deprecated HostPublicKey field is still honored and merged into HostPublicKeys.

Project status

moov-io/go-sftp is actively used in production environments. Please star the project if you are interested in its progress. Please let us know if you encounter any bugs/unclear documentation or have feature suggestions by opening up an issue or pull request. Thanks!

Getting help

channel info
Twitter @moov You can follow Moov.io's Twitter feed to get updates on our project(s). You can also tweet us questions or just share blogs or stories.
GitHub Issue If you are able to reproduce a problem please open a GitHub Issue under the specific project that caused the error.
moov-io slack Join our slack channel (#infra) to have an interactive discussion about the development of the project.

Supported and tested platforms

  • 64-bit Linux (Ubuntu, Debian), macOS, and Windows

Contributing

Yes please! Please review our Contributing guide and Code of Conduct to get started!

See Golang's install instructions for help setting up Go. You can download the source code and we offer tagged and released versions as well. We highly recommend you use a tagged release for production.

Releasing

To make a release of go-sftp simply open a pull request with CHANGELOG.md updated with the next version number and details. You'll also need to push the tag (i.e. git push origin v1.0.0) to origin in order for CI to make the release.

Testing

Tests start in-process SSH+SFTP servers, so go test ./... works on Linux, macOS, and Windows without Docker. One server uses the host filesystem (case-insensitive on Windows); another is in-memory and always case-sensitive.

go test -short ./... skips tests that talk to the fixture server and to OpenSSH on 127.0.0.1:2222.

make setup starts atmoz/sftp via Docker Compose. When that container is listening on 127.0.0.1:2222, the suite also runs against OpenSSH. Linux CI starts it; Windows CI relies on the in-process servers.

make setup    # starts atmoz/sftp via docker compose
go test ./...
make teardown

make test runs go test with coverage. make check downloads the Moov lint script and needs network access.

License

Apache License 2.0 - See LICENSE for details.

About

A Go client for performing common SFTP operations

Topics

Resources

Contributing

Security policy

Stars

58 stars

Watchers

5 watching

Forks

Releases

Used by

Contributors

Languages