-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathclient.go
596 lines (520 loc) · 14.4 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Copyright 2022 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package go_sftp
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/go-kit/kit/metrics/prometheus"
"github.com/moov-io/base/log"
"github.com/pkg/sftp"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"golang.org/x/crypto/ssh"
)
var (
sftpAgentUp = prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Name: "sftp_agent_up",
Help: "Status of SFTP agent connection",
}, []string{"hostname"})
sftpConnectionRetries = prometheus.NewCounterFrom(stdprometheus.CounterOpts{
Name: "sftp_connection_retries",
Help: "Counter of SFTP connection retry attempts",
}, []string{"hostname"})
)
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
}
type client struct {
logger log.Logger
cfg ClientConfig
mu sync.Mutex // protects all read/write methods
conn *ssh.Client
client *sftp.Client
}
func NewClient(logger log.Logger, cfg *ClientConfig) (Client, error) {
if cfg == nil {
return nil, errors.New("nil SFTP config")
}
cc := &client{cfg: *cfg, logger: logger}
conn, err := cc.connection()
cc.record(err) // track up metric for remote server
err = cc.clearConnectionOnError(err)
// Print an initial startup message
if conn != nil && logger != nil {
wd, wdErr := conn.Getwd()
if wdErr != nil {
err = cc.clearConnectionOnError(wdErr)
}
if wd != "" {
logger.Logf("starting SFTP client in %s", wd)
}
}
return cc, err
}
// connection returns an sftp.Client which is connected to the remote server.
// This function will attempt to establish a new connection if none exists already.
//
// connection must be called within a mutex lock.
func (c *client) connection() (*sftp.Client, error) {
if c == nil {
return nil, errors.New("nil client / config")
}
if c.client != nil {
// Verify the connection works and if not drop through and reconnect
if _, err := c.client.Getwd(); err == nil {
return c.client, nil
} else {
// Our connection is having issues, so retry connecting
c.client.Close()
}
}
conn, stdin, stdout, err := sftpConnect(c.logger, c.cfg)
if err != nil {
return nil, fmt.Errorf("sftp: %w", err)
}
c.conn = conn
// Setup our SFTP client
var opts = []sftp.ClientOption{
sftp.MaxConcurrentRequestsPerFile(c.cfg.MaxConnections),
}
if c.cfg.PacketSize > 0 {
opts = append(opts, sftp.MaxPacket(c.cfg.PacketSize))
}
// client, err := sftp.NewClient(conn, opts...)
client, err := sftp.NewClientPipe(stdout, stdin, opts...)
if err != nil {
go conn.Close()
return nil, fmt.Errorf("sftp: sftp connect: %w", err)
}
c.client = client
return c.client, nil
}
// clearConnectionOnError accepts any error from a call involving the SSH/SFTP connection.
// If an error is encountered that causes either connection (SSH or SFTP) to be
// lost it will tear down the connections. The next invocation of c.connection()
// will re-establish new connections.
//
// When the error is captured by clearConnectionOnError the client will attempt to reconnect
// and that new connection error will be returned.
func (c *client) clearConnectionOnError(err error) error {
if err == nil {
return nil
}
// Possible errors from github.com/pkg/sftp/request-errors.go
switch {
case errors.Is(err, sftp.ErrSSHFxEOF),
errors.Is(err, sftp.ErrSSHFxFailure),
errors.Is(err, sftp.ErrSSHFxBadMessage),
errors.Is(err, sftp.ErrSSHFxNoConnection),
errors.Is(err, sftp.ErrSSHFxConnectionLost):
// Teardown the existing connections
if c.conn != nil {
c.conn.Close()
c.conn = nil
}
if c.client != nil {
c.client.Close()
c.client = nil
}
// Reconnect if needed and replace the initial error
_, err = c.connection()
}
return err
}
var (
hostKeyCallbackOnce sync.Once
hostKeyCallback = func(logger log.Logger) {
msg := "sftp: WARNING!!! Insecure default of skipping SFTP host key validation. Please set HostPublicKey(s)"
if logger != nil {
logger.Warn().Log(msg)
}
}
)
func sftpConnect(logger log.Logger, cfg ClientConfig) (*ssh.Client, io.WriteCloser, io.Reader, error) {
conf := &ssh.ClientConfig{
User: cfg.Username,
Timeout: cfg.Timeout,
}
conf.SetDefaults()
if hostKeys := cfg.HostKeys(); len(hostKeys) > 0 {
callback, err := NewMultiKeyCallback(hostKeys)
if err != nil {
return nil, nil, nil, err
}
conf.HostKeyCallback = callback
} else {
hostKeyCallbackOnce.Do(func() {
hostKeyCallback(logger)
})
//nolint:gosec
conf.HostKeyCallback = ssh.InsecureIgnoreHostKey() // insecure default
}
// Setup various Authentication methods
if cfg.Password != "" {
conf.Auth = append(conf.Auth, ssh.Password(cfg.Password))
}
if cfg.ClientPrivateKey != "" {
signer, err := readSigner(cfg.ClientPrivateKey, cfg.ClientPrivateKeyPassword)
if err != nil {
return nil, nil, nil, fmt.Errorf("sftpConnect: failed to read client private key: %w", err)
}
conf.Auth = append(conf.Auth, ssh.PublicKeys(signer))
}
// Connect to the remote server
var client *ssh.Client
var err error
for i := 0; i < 3; i++ {
if client == nil {
if i > 0 {
sftpConnectionRetries.With("hostname", cfg.Hostname).Add(1)
}
client, err = ssh.Dial("tcp", cfg.Hostname, conf) // retry connection
time.Sleep(250 * time.Millisecond)
}
}
if client == nil && err != nil {
return nil, nil, nil, fmt.Errorf("sftpConnect: %w", err)
}
session, err := client.NewSession()
if err != nil {
go client.Close()
return nil, nil, nil, err
}
if err = session.RequestSubsystem("sftp"); err != nil {
go client.Close()
return nil, nil, nil, err
}
pw, err := session.StdinPipe()
if err != nil {
go client.Close()
return nil, nil, nil, err
}
pr, err := session.StdoutPipe()
if err != nil {
go client.Close()
return nil, nil, nil, err
}
return client, pw, pr, nil
}
func readSigner(raw, passphrase string) (ssh.Signer, error) {
decoded, err := base64.StdEncoding.DecodeString(raw)
if len(decoded) > 0 && err == nil {
return readPrivateKey(decoded, passphrase)
}
return readPrivateKey([]byte(raw), passphrase)
}
func readPrivateKey(data []byte, passphrase string) (ssh.Signer, error) {
if passphrase != "" {
return ssh.ParsePrivateKeyWithPassphrase(data, []byte(passphrase))
}
return ssh.ParsePrivateKey(data)
}
func (c *client) Ping() error {
if c == nil {
return errors.New("nil SFTPTransferAgent")
}
c.mu.Lock()
defer c.mu.Unlock()
conn, err := c.connection()
c.record(err)
err = c.clearConnectionOnError(err)
if err != nil {
return err
}
_, err = conn.ReadDir(".")
c.record(err)
err = c.clearConnectionOnError(err)
if err != nil {
return fmt.Errorf("sftp: ping %w", err)
}
return nil
}
func (c *client) record(err error) {
if c == nil {
return
}
if err != nil {
sftpAgentUp.With("hostname", c.cfg.Hostname).Set(0)
} else {
sftpAgentUp.With("hostname", c.cfg.Hostname).Set(1)
}
}
func (c *client) Close() error {
if c == nil {
return nil
}
if c.client != nil {
c.client.Close()
}
if c.conn != nil {
c.conn.Close()
}
return nil
}
func (c *client) Delete(path string) error {
c.mu.Lock()
defer c.mu.Unlock()
conn, err := c.connection()
err = c.clearConnectionOnError(err)
if err != nil {
return err
}
info, err := conn.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return nil // file doesn't exist
}
// The error is something else related to STAT so return that
err = c.clearConnectionOnError(err)
return fmt.Errorf("sftp: delete stat: %w", err)
}
if info != nil {
err := conn.Remove(path)
err = c.clearConnectionOnError(err)
if err != nil {
return fmt.Errorf("sftp: delete: %w", err)
}
}
return nil // not found
}
// UploadFile creates a file containing the provided contents at the specified path
//
// The File's contents will always be closed
func (c *client) UploadFile(path string, contents io.ReadCloser) error {
defer contents.Close()
c.mu.Lock()
defer c.mu.Unlock()
conn, err := c.connection()
err = c.clearConnectionOnError(err)
if err != nil {
return err
}
// Create the directory if it doesn't exist
if !c.cfg.SkipDirectoryCreation {
dir, _ := filepath.Split(path)
info, err := conn.Stat(dir)
err = c.clearConnectionOnError(err)
if info == nil || err != nil {
if os.IsNotExist(err) || strings.Contains(err.Error(), "file does not exist") {
err := conn.MkdirAll(dir)
err = c.clearConnectionOnError(err)
if err != nil {
return fmt.Errorf("sftp: problem creating %s as parent dir: %w", dir, err)
}
} else {
return fmt.Errorf("problem checking if %s exists: %w", dir, err)
}
}
}
// Some servers don't allow you to open a file for reading and writing at the same time.
// For these we follow the pkg/sftp docs to open files for writing (not reading).
fd, err := conn.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
err = c.clearConnectionOnError(err)
if err != nil {
return fmt.Errorf("sftp: problem creating remote file %s: %w", path, err)
}
n, err := io.Copy(fd, contents)
if err != nil {
return fmt.Errorf("sftp: problem copying (n=%d) %s: %w", n, path, err)
}
err = fd.Sync()
err = c.clearConnectionOnError(err)
if err != nil {
// Skip sync if the remote server doesn't support it
if !strings.Contains(err.Error(), "SSH_FX_OP_UNSUPPORTED") {
return fmt.Errorf("sftp: problem with sync on %s: %v", path, err)
}
}
if !c.cfg.SkipChmodAfterUpload {
err := fd.Chmod(0600)
err = c.clearConnectionOnError(err)
if err != nil {
return fmt.Errorf("sftp: problem chmod %s: %w", path, err)
}
}
err = fd.Close()
err = c.clearConnectionOnError(err)
if err != nil {
return fmt.Errorf("sftp: closing %s after writing failed: %w", path, err)
}
return nil
}
// ListFiles will return the paths of files within dir. Paths are returned as locations from dir,
// so if dir is an absolute path the returned paths will be.
//
// Paths are matched in case-insensitive comparisons, but results are returned exactly as they
// appear on the server.
func (c *client) ListFiles(dir string) ([]string, error) {
pattern := filepath.Clean(strings.TrimPrefix(dir, string(os.PathSeparator)))
conn, err := c.connection()
if err = c.clearConnectionOnError(err); err != nil {
return nil, err
}
wd := "."
switch {
case dir == "/":
pattern = "*"
case pattern == ".":
if dir == "" {
pattern = "*"
} else {
pattern = filepath.Join(dir, "*")
}
case pattern != "":
pattern = "[/?]" + pattern + "/*"
wd, err = conn.Getwd()
if err = c.clearConnectionOnError(err); err != nil {
return nil, err
}
}
var filenames []string
err = c.Walk(wd, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
// Check if the server's path matches what we're searching for in a case-insensitive comparison.
matches, err := filepath.Match(strings.ToLower(pattern), strings.ToLower(path))
if matches && err == nil {
// Return the path with exactly the case on the server.
trimmedPattern := strings.TrimPrefix(strings.TrimSuffix(pattern, "*"), "[/?]")
idx := strings.Index(strings.ToLower(path), strings.ToLower(trimmedPattern))
if idx > -1 {
path = path[idx:]
if strings.HasPrefix(dir, "/") && !strings.HasPrefix(path, "/") {
path = "/" + path
}
filenames = append(filenames, path)
} else {
// Fallback to Go logic of presenting the path
filenames = append(filenames, filepath.Join(dir, filepath.Base(path)))
}
}
return err
})
if err != nil {
return nil, fmt.Errorf("listing %s failed: %w", dir, err)
}
return filenames, nil
}
// Reader will open the file at path and provide a reader to access its contents.
// Callers need to close the returned Contents
//
// Callers should be aware that network errors while reading can occur since contents
// are streamed from the SFTP server.
func (c *client) Reader(path string) (*File, error) {
c.mu.Lock()
defer c.mu.Unlock()
conn, err := c.connection()
err = c.clearConnectionOnError(err)
if err != nil {
return nil, err
}
fd, err := conn.Open(path)
err = c.clearConnectionOnError(err)
if err != nil {
return nil, fmt.Errorf("sftp: open %s: %w", path, err)
}
var fileinfo fs.FileInfo
modTime := time.Now().In(time.UTC)
if stat, _ := fd.Stat(); stat != nil {
fileinfo = stat
modTime = stat.ModTime()
}
return &File{
Filename: fd.Name(),
Contents: fd,
ModTime: modTime,
fileinfo: fileinfo,
}, nil
}
// Open will return the contents at path and consume the entire file contents.
// WARNING: This method can use a lot of memory by consuming the entire file into memory.
func (c *client) Open(path string) (*File, error) {
r, err := c.Reader(path)
if err != nil {
return nil, err
}
// read the entire remote file
var buf bytes.Buffer
if n, err := io.Copy(&buf, r.Contents); err != nil {
r.Close()
if err != nil && !strings.Contains(err.Error(), sftp.ErrInternalInconsistency.Error()) {
return nil, fmt.Errorf("sftp: read (n=%d) %s: %w", n, r.Filename, err)
}
return nil, fmt.Errorf("sftp: read (n=%d) on %s: %w", n, r.Filename, err)
} else {
r.Close()
}
return &File{
Filename: r.Filename,
Contents: io.NopCloser(&buf),
ModTime: r.ModTime,
}, nil
}
// Walk will traverse dir and call fs.WalkDirFunc on each entry.
//
// Follow the docs for fs.WalkDirFunc for details on traversal. Walk accepts fs.SkipDir to not process directories.
func (c *client) Walk(dir string, fn fs.WalkDirFunc) error {
c.mu.Lock()
defer c.mu.Unlock()
conn, err := c.connection()
if err = c.clearConnectionOnError(err); err != nil {
return err
}
w := conn.Walk(dir)
if w == nil {
return errors.New("nil *fs.Walker")
}
var skippedDirs []string
// Pass the callback to each file found
for w.Step() {
if err := w.Err(); err != nil {
return err
}
var skip bool
for _, sd := range skippedDirs {
matched := strings.HasPrefix(w.Path(), sd)
if matched {
skip = true
break
}
}
if skip {
w.SkipDir()
continue
}
info := w.Stat()
if info == nil {
continue
}
err := fn(w.Path(), fs.FileInfoToDirEntry(info), w.Err())
if err != nil {
if err == fs.SkipDir {
skippedDirs = append(skippedDirs, filepath.Join(dir, ""))
w.SkipDir()
} else {
return err
}
}
}
return w.Err()
}