Skip to content

Commit 9906c68

Browse files
authored
Merge pull request #236 from kevinbarbour/feat/clientKeyFile
Add client SFTP Key file support and fix docs
2 parents 5bd8e2c + 55c1efd commit 9906c68

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

docs/config.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ ACHGateway:
244244
Hostname: <host>
245245
Username: <string>
246246
[ Password: <secret> ]
247-
[ ClientPrivateKey: <filename> ]
247+
[ ClientPrivateKey: <string> ]
248+
[ ClientPrivateKeyFile: <filename> ]
248249
[ HostPublicKey: <filename> ]
249250
[ DialTimeout: <duration> | default = 10s ]
250251
[ MaxConnectionsPerFile: <number> | default = 1 ]

internal/service/model_upload.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ type SFTP struct {
136136
Hostname string
137137
Username string
138138

139-
Password string
140-
ClientPrivateKey string
141-
HostPublicKey string
139+
Password string
140+
ClientPrivateKey string
141+
ClientPrivateKeyFile string
142+
HostPublicKey string
142143

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

157-
Password string
158-
ClientPrivateKey string
159-
HostPublicKey string
158+
Password string
159+
ClientPrivateKey string
160+
ClientPrivateKeyFile string
161+
HostPublicKey string
160162

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

171-
Password: mask.Password(cfg.Password),
172-
ClientPrivateKey: cfg.ClientPrivateKey,
173-
HostPublicKey: cfg.HostPublicKey,
173+
Password: mask.Password(cfg.Password),
174+
ClientPrivateKey: cfg.ClientPrivateKey,
175+
ClientPrivateKeyFile: cfg.ClientPrivateKeyFile,
176+
HostPublicKey: cfg.HostPublicKey,
174177

175178
DialTimeout: cfg.DialTimeout,
176179
MaxConnectionsPerFile: cfg.MaxConnectionsPerFile,
@@ -207,6 +210,7 @@ func (cfg *SFTP) String() string {
207210
buf.WriteString(fmt.Sprintf("Username=%s, ", cfg.Username))
208211
buf.WriteString(fmt.Sprintf("Password=%s, ", mask.Password(cfg.Password)))
209212
buf.WriteString(fmt.Sprintf("ClientPrivateKey:%v, ", cfg.ClientPrivateKey != ""))
213+
buf.WriteString(fmt.Sprintf("ClientPrivateKeyFile:%v, ", cfg.ClientPrivateKeyFile != ""))
210214
buf.WriteString(fmt.Sprintf("HostPublicKey:%v}, ", cfg.HostPublicKey != ""))
211215
return buf.String()
212216
}

internal/upload/sftp.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11+
"os"
1112
"path/filepath"
1213
"strings"
1314

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

38+
clientPrivateKey := cfg.SFTP.ClientPrivateKey
39+
40+
if clientPrivateKey == "" && cfg.SFTP.ClientPrivateKeyFile != "" {
41+
key, err := os.ReadFile(cfg.SFTP.ClientPrivateKeyFile)
42+
if err != nil {
43+
return nil, fmt.Errorf("sftp: unable to read private key file %s: %v", cfg.SFTP.ClientPrivateKeyFile, err)
44+
}
45+
clientPrivateKey = string(key)
46+
}
47+
3748
client, err := go_sftp.NewClient(logger, &go_sftp.ClientConfig{
3849
Hostname: cfg.SFTP.Hostname,
3950
Username: cfg.SFTP.Username,
4051
Password: cfg.SFTP.Password,
4152

42-
ClientPrivateKey: cfg.SFTP.ClientPrivateKey,
53+
ClientPrivateKey: clientPrivateKey,
4354
HostPublicKey: cfg.SFTP.HostPublicKey,
4455

4556
Timeout: cfg.SFTP.DialTimeout,

0 commit comments

Comments
 (0)