Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client SFTP Key file support and fix docs #236

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ ACHGateway:
Hostname: <host>
Username: <string>
[ Password: <secret> ]
[ ClientPrivateKey: <filename> ]
[ ClientPrivateKey: <string> ]
[ ClientPrivateKeyFile: <filename> ]
[ HostPublicKey: <filename> ]
[ DialTimeout: <duration> | default = 10s ]
[ MaxConnectionsPerFile: <number> | default = 1 ]
Expand Down
22 changes: 13 additions & 9 deletions internal/service/model_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ type SFTP struct {
Hostname string
Username string

Password string
ClientPrivateKey string
HostPublicKey string
Password string
ClientPrivateKey string
ClientPrivateKeyFile string
HostPublicKey string

DialTimeout time.Duration
MaxConnectionsPerFile int
Expand All @@ -154,9 +155,10 @@ func (cfg *SFTP) MarshalJSON() ([]byte, error) {
Hostname string
Username string

Password string
ClientPrivateKey string
HostPublicKey string
Password string
ClientPrivateKey string
ClientPrivateKeyFile string
HostPublicKey string

DialTimeout time.Duration
MaxConnectionsPerFile int
Expand All @@ -168,9 +170,10 @@ func (cfg *SFTP) MarshalJSON() ([]byte, error) {
Hostname: cfg.Hostname,
Username: cfg.Username,

Password: mask.Password(cfg.Password),
ClientPrivateKey: cfg.ClientPrivateKey,
HostPublicKey: cfg.HostPublicKey,
Password: mask.Password(cfg.Password),
ClientPrivateKey: cfg.ClientPrivateKey,
ClientPrivateKeyFile: cfg.ClientPrivateKeyFile,
HostPublicKey: cfg.HostPublicKey,

DialTimeout: cfg.DialTimeout,
MaxConnectionsPerFile: cfg.MaxConnectionsPerFile,
Expand Down Expand Up @@ -207,6 +210,7 @@ func (cfg *SFTP) String() string {
buf.WriteString(fmt.Sprintf("Username=%s, ", cfg.Username))
buf.WriteString(fmt.Sprintf("Password=%s, ", mask.Password(cfg.Password)))
buf.WriteString(fmt.Sprintf("ClientPrivateKey:%v, ", cfg.ClientPrivateKey != ""))
buf.WriteString(fmt.Sprintf("ClientPrivateKeyFile:%v, ", cfg.ClientPrivateKeyFile != ""))
buf.WriteString(fmt.Sprintf("HostPublicKey:%v}, ", cfg.HostPublicKey != ""))
return buf.String()
}
Expand Down
13 changes: 12 additions & 1 deletion internal/upload/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"

Expand All @@ -34,12 +35,22 @@ func newSFTPTransferAgent(logger log.Logger, cfg *service.UploadAgent) (*SFTPTra
return nil, fmt.Errorf("sftp: %s is not whitelisted: %v", cfg.SFTP.Hostname, err)
}

clientPrivateKey := cfg.SFTP.ClientPrivateKey

if clientPrivateKey == "" && cfg.SFTP.ClientPrivateKeyFile != "" {
key, err := os.ReadFile(cfg.SFTP.ClientPrivateKeyFile)
if err != nil {
return nil, fmt.Errorf("sftp: unable to read private key file %s: %v", cfg.SFTP.ClientPrivateKeyFile, err)
}
clientPrivateKey = string(key)
}

client, err := go_sftp.NewClient(logger, &go_sftp.ClientConfig{
Hostname: cfg.SFTP.Hostname,
Username: cfg.SFTP.Username,
Password: cfg.SFTP.Password,

ClientPrivateKey: cfg.SFTP.ClientPrivateKey,
ClientPrivateKey: clientPrivateKey,
HostPublicKey: cfg.SFTP.HostPublicKey,

Timeout: cfg.SFTP.DialTimeout,
Expand Down
Loading