forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftp.go
372 lines (327 loc) · 10.2 KB
/
sftp.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
package input
import (
"context"
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/dafanshu/benthos/v3/internal/codec"
"github.com/dafanshu/benthos/v3/internal/docs"
sftpSetup "github.com/dafanshu/benthos/v3/internal/impl/sftp"
"github.com/dafanshu/benthos/v3/internal/interop"
"github.com/dafanshu/benthos/v3/lib/input/reader"
"github.com/dafanshu/benthos/v3/lib/log"
"github.com/dafanshu/benthos/v3/lib/message"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/types"
"github.com/pkg/sftp"
)
func init() {
watcherDocs := docs.FieldSpecs{
docs.FieldCommon(
"enabled",
"Whether file watching is enabled.",
),
docs.FieldCommon(
"minimum_age",
"The minimum period of time since a file was last updated before attempting to consume it. Increasing this period decreases the likelihood that a file will be consumed whilst it is still being written to.",
"10s", "1m", "10m",
),
docs.FieldCommon(
"poll_interval",
"The interval between each attempt to scan the target paths for new files.",
"100ms", "1s",
),
docs.FieldCommon(
"cache",
"A [cache resource](/docs/components/caches/about) for storing the paths of files already consumed.",
),
}
Constructors[TypeSFTP] = TypeSpec{
constructor: fromSimpleConstructor(func(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (Type, error) {
r, err := newSFTPReader(conf.SFTP, mgr, log, stats)
if err != nil {
return nil, err
}
return NewAsyncReader(
TypeSFTP,
true,
reader.NewAsyncPreserver(r),
log, stats,
)
}),
Status: docs.StatusExperimental,
Version: "3.39.0",
Summary: `Consumes files from a server over SFTP.`,
Description: `
## Metadata
This input adds the following metadata fields to each message:
` + "```" + `
- sftp_path
` + "```" + `
You can access these metadata fields using [function interpolation](/docs/configuration/interpolation#metadata).`,
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon(
"address",
"The address of the server to connect to that has the target files.",
),
docs.FieldCommon(
"credentials",
"The credentials to use to log into the server.",
).WithChildren(sftpSetup.CredentialsDocs()...),
docs.FieldString(
"paths",
"A list of paths to consume sequentially. Glob patterns are supported.",
).Array(),
codec.ReaderDocs,
docs.FieldAdvanced("delete_on_finish", "Whether to delete files from the server once they are processed."),
docs.FieldAdvanced("max_buffer", "The largest token size expected when consuming delimited files."),
docs.FieldCommon(
"watcher",
"An experimental mode whereby the input will periodically scan the target paths for new files and consume them, when all files are consumed the input will continue polling for new files.",
).WithChildren(watcherDocs...).AtVersion("3.42.0"),
},
Categories: []Category{
CategoryNetwork,
},
}
}
//------------------------------------------------------------------------------
type watcherConfig struct {
Enabled bool `json:"enabled" yaml:"enabled"`
MinimumAge string `json:"minimum_age" yaml:"minimum_age"`
PollInterval string `json:"poll_interval" yaml:"poll_interval"`
Cache string `json:"cache" yaml:"cache"`
}
// SFTPConfig contains configuration fields for the SFTP input type.
type SFTPConfig struct {
Address string `json:"address" yaml:"address"`
Credentials sftpSetup.Credentials `json:"credentials" yaml:"credentials"`
Paths []string `json:"paths" yaml:"paths"`
Codec string `json:"codec" yaml:"codec"`
DeleteOnFinish bool `json:"delete_on_finish" yaml:"delete_on_finish"`
MaxBuffer int `json:"max_buffer" yaml:"max_buffer"`
Watcher watcherConfig `json:"watcher" yaml:"watcher"`
}
// NewSFTPConfig creates a new SFTPConfig with default values.
func NewSFTPConfig() SFTPConfig {
return SFTPConfig{
Address: "",
Credentials: sftpSetup.Credentials{},
Paths: []string{},
Codec: "all-bytes",
DeleteOnFinish: false,
MaxBuffer: 1000000,
Watcher: watcherConfig{
Enabled: false,
MinimumAge: "1s",
PollInterval: "1s",
Cache: "",
},
}
}
//------------------------------------------------------------------------------
type sftpReader struct {
conf SFTPConfig
log log.Modular
stats metrics.Type
mgr types.Manager
client *sftp.Client
paths []string
scannerCtor codec.ReaderConstructor
scannerMut sync.Mutex
scanner codec.Reader
currentPath string
watcherPollInterval time.Duration
watcherMinAge time.Duration
}
func newSFTPReader(conf SFTPConfig, mgr types.Manager, log log.Modular, stats metrics.Type) (*sftpReader, error) {
codecConf := codec.NewReaderConfig()
codecConf.MaxScanTokenSize = conf.MaxBuffer
ctor, err := codec.GetReader(conf.Codec, codecConf)
if err != nil {
return nil, err
}
var watcherPollInterval, watcherMinAge time.Duration
if conf.Watcher.Enabled {
if watcherPollInterval, err = time.ParseDuration(conf.Watcher.PollInterval); err != nil {
return nil, fmt.Errorf("failed to parse watcher poll interval: %w", err)
}
if watcherMinAge, err = time.ParseDuration(conf.Watcher.MinimumAge); err != nil {
return nil, fmt.Errorf("failed to parse watcher minimum age: %w", err)
}
if conf.Watcher.Cache == "" {
return nil, errors.New("a cache must be specified when watcher mode is enabled")
}
if err := interop.ProbeCache(context.Background(), mgr, conf.Watcher.Cache); err != nil {
return nil, err
}
}
s := &sftpReader{
conf: conf,
log: log,
stats: stats,
mgr: mgr,
scannerCtor: ctor,
watcherPollInterval: watcherPollInterval,
watcherMinAge: watcherMinAge,
}
return s, err
}
// ConnectWithContext attempts to establish a connection to the target SFTP server.
func (s *sftpReader) ConnectWithContext(ctx context.Context) error {
var err error
s.scannerMut.Lock()
defer s.scannerMut.Unlock()
if s.scanner != nil {
return nil
}
if s.client == nil {
if s.client, err = s.conf.Credentials.GetClient(s.conf.Address); err != nil {
return err
}
s.log.Debugln("Finding more paths")
s.paths, err = s.getFilePaths()
if err != nil {
return err
}
}
if len(s.paths) == 0 {
if !s.conf.Watcher.Enabled {
s.client.Close()
s.client = nil
s.log.Debugln("Paths exhausted, closing input")
return types.ErrTypeClosed
}
select {
case <-time.After(s.watcherPollInterval):
case <-ctx.Done():
return ctx.Err()
}
s.paths, err = s.getFilePaths()
return err
}
nextPath := s.paths[0]
file, err := s.client.Open(nextPath)
if err != nil {
return err
}
if s.scanner, err = s.scannerCtor(nextPath, file, func(ctx context.Context, err error) error {
if err == nil && s.conf.DeleteOnFinish {
return s.client.Remove(nextPath)
}
return nil
}); err != nil {
file.Close()
return err
}
s.currentPath = nextPath
s.paths = s.paths[1:]
s.log.Infof("Consuming from file '%v'\n", nextPath)
return err
}
// ReadWithContext attempts to read a new message from the target file(s) on the server.
func (s *sftpReader) ReadWithContext(ctx context.Context) (types.Message, reader.AsyncAckFn, error) {
s.scannerMut.Lock()
defer s.scannerMut.Unlock()
if s.scanner == nil || s.client == nil {
return nil, nil, types.ErrNotConnected
}
parts, codecAckFn, err := s.scanner.Next(ctx)
if err != nil {
if errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded) {
err = types.ErrTimeout
}
if err != types.ErrTimeout {
if s.conf.Watcher.Enabled {
var setErr error
if cerr := interop.AccessCache(ctx, s.mgr, s.conf.Watcher.Cache, func(cache types.Cache) {
setErr = cache.Set(s.currentPath, []byte("@"))
}); cerr != nil {
return nil, nil, fmt.Errorf("failed to get the cache for sftp watcher mode: %v", cerr)
}
if setErr != nil {
return nil, nil, fmt.Errorf("failed to update path in cache %s: %v", s.currentPath, err)
}
}
s.scanner.Close(ctx)
s.scanner = nil
}
if errors.Is(err, io.EOF) {
err = types.ErrTimeout
}
return nil, nil, err
}
for _, part := range parts {
part.Metadata().Set("sftp_path", s.currentPath)
}
msg := message.New(nil)
msg.Append(parts...)
return msg, func(ctx context.Context, res types.Response) error {
return codecAckFn(ctx, res.Error())
}, nil
}
// CloseAsync begins cleaning up resources used by this reader asynchronously.
func (s *sftpReader) CloseAsync() {
go func() {
s.scannerMut.Lock()
if s.scanner != nil {
s.scanner.Close(context.Background())
s.scanner = nil
s.paths = nil
}
if s.client != nil {
s.client.Close()
s.client = nil
}
s.scannerMut.Unlock()
}()
}
// WaitForClose will block until either the reader is closed or a specified
// timeout occurs.
func (s *sftpReader) WaitForClose(timeout time.Duration) error {
return nil
}
func (s *sftpReader) getFilePaths() ([]string, error) {
var filepaths []string
if !s.conf.Watcher.Enabled {
for _, p := range s.conf.Paths {
paths, err := s.client.Glob(p)
if err != nil {
s.log.Warnf("Failed to scan files from path %v: %v\n", p, err)
continue
}
filepaths = append(filepaths, paths...)
}
return filepaths, nil
}
if cerr := interop.AccessCache(context.Background(), s.mgr, s.conf.Watcher.Cache, func(cache types.Cache) {
for _, p := range s.conf.Paths {
paths, err := s.client.Glob(p)
if err != nil {
s.log.Warnf("Failed to scan files from path %v: %v\n", p, err)
continue
}
for _, path := range paths {
info, err := s.client.Stat(path)
if err != nil {
s.log.Warnf("Failed to stat path %v: %v\n", path, err)
continue
}
if time.Since(info.ModTime()) < s.watcherMinAge {
continue
}
if _, err := cache.Get(path); err != nil {
filepaths = append(filepaths, path)
} else if err = cache.Set(path, []byte("@")); err != nil { // Reset the TTL for the path
s.log.Warnf("Failed to set key in cache for path %v: %v\n", path, err)
}
}
}
}); cerr != nil {
return nil, fmt.Errorf("error getting cache in getFilePaths: %v", cerr)
}
return filepaths, nil
}