-
Notifications
You must be signed in to change notification settings - Fork 5
/
bucket.go
226 lines (197 loc) · 5.33 KB
/
bucket.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
package filelist
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"path/filepath"
"strings"
"github.com/moov-io/ach-web-viewer/pkg/service"
"github.com/moov-io/ach-web-viewer/pkg/yyyymmdd"
"github.com/moov-io/cryptfs"
"cloud.google.com/go/storage"
"gocloud.dev/blob"
_ "gocloud.dev/blob/gcsblob"
"golang.org/x/sync/errgroup"
)
type bucketLister struct {
sourceID string
buck *blob.Bucket
paths []string
cryptors []*cryptfs.FS
}
func newBucketLister(sourceID string, cfg service.Source) (*bucketLister, error) {
if cfg.Bucket == nil {
return nil, errors.New("missing BucketConfig")
}
buck, err := blob.OpenBucket(context.Background(), cfg.Bucket.URL)
if err != nil {
return nil, err
}
ls := &bucketLister{
sourceID: sourceID,
buck: buck,
paths: cfg.Bucket.Paths,
}
if cfg.Encryption != nil && cfg.Encryption.GPG != nil {
conf := cfg.Encryption.GPG
// Decrypt with either config
if conf.KeyFile != "" {
cc, err := cryptfs.FromCryptor(cryptfs.NewGPGDecryptorFile(conf.KeyFile, []byte(conf.KeyPassword)))
if err != nil {
return nil, fmt.Errorf("problem reading %s: %v", conf.KeyFile, err)
}
if cc != nil {
ls.cryptors = append(ls.cryptors, cc)
}
}
// Read from .Files and add those
for i := range conf.Files {
cc, err := cryptfs.FromCryptor(cryptfs.NewGPGDecryptorFile(conf.Files[i].KeyFile, []byte(conf.Files[i].KeyPassword)))
if err != nil {
return nil, fmt.Errorf("problem reading %s: %v", conf.Files[i].KeyFile, err)
}
if cc != nil {
ls.cryptors = append(ls.cryptors, cc)
}
}
}
return ls, nil
}
func (ls *bucketLister) SourceID() string {
return ls.sourceID
}
func (ls *bucketLister) GetFiles(opts ListOpts) (Files, error) {
out := Files{
SourceID: ls.sourceID,
SourceType: "Bucket",
}
for i := range ls.paths {
files, err := ls.listFiles(opts, ls.paths[i])
if err != nil {
return out, fmt.Errorf("error reading %s bucket path: %v", ls.paths[i], err)
}
out.Files = append(out.Files, files...)
}
return out, nil
}
func (ls *bucketLister) GetFile(path string) (*File, error) {
rdr, err := ls.buck.NewReader(context.Background(), path, nil)
if err != nil {
return nil, err
}
bs, err := ls.maybeDecrypt(rdr)
if err != nil {
rdr.Close()
return nil, err
}
rdr.Close()
_, name := filepath.Split(path)
file, err := readFile(bytes.NewReader(bs))
return &File{
Name: name,
StoragePath: path,
Contents: file,
CreatedAt: rdr.ModTime(),
Size: int64(len(bs)),
}, err
}
func (ls *bucketLister) maybeDecrypt(r io.Reader) ([]byte, error) {
initial, err := io.ReadAll(r)
if err != nil {
return nil, err
}
for i := range ls.cryptors {
bs, err := ls.cryptors[i].Reveal(initial)
if len(bs) > 0 && err == nil {
return bs, err
}
}
return initial, err
}
func (ls *bucketLister) listFiles(opts ListOpts, pathPrefix string) ([]File, error) {
// Different underlying storage engines will let us scan/glob parts of the bucket differently.
var gcsBucket *storage.Client
if ls.buck.As(&gcsBucket) {
return ls.listFilesFromGCSBucket(opts, pathPrefix)
}
return ls.listFilesFromCDKBucket(opts, pathPrefix)
}
func (ls *bucketLister) listFilesFromGCSBucket(opts ListOpts, pathPrefix string) ([]File, error) {
var g errgroup.Group
datePrefixes := yyyymmdd.Prefixes(opts.StartDate, opts.EndDate)
discoveredFiles := make(chan []File, len(datePrefixes))
for _, datePrefix := range datePrefixes {
g.Go(func() error {
beforeList := func(as func(interface{}) bool) error {
var q *storage.Query
if as(&q) {
// If pathPrefix contains a "/" then it's formatted as "folder/hostname"
folder, hostname := pathPrefix, "*"
idx := strings.Index(pathPrefix, "/")
if idx > 0 {
folder, hostname = pathPrefix[:idx], pathPrefix[idx:]
hostname = strings.ReplaceAll(hostname, "/", "")
}
// achgateway stores files under two path schemes:
//
// /odfi /sftp.bank.com:22 /Returned /2024-04-05
// /outbound /sftp.bank.com:22 /2022-10-17
//
// Glob for the optional "Returned" directory
q.MatchGlob = fmt.Sprintf("%s/%s/**/%s*/**", folder, hostname, datePrefix)
}
return nil
}
listOptions := &blob.ListOptions{
Prefix: pathPrefix, // + "/",
BeforeList: beforeList,
}
files, err := ls.listFilesFromCursor(opts, ls.buck.List(listOptions))
if len(files) > 0 {
discoveredFiles <- files
}
return err
})
}
err := g.Wait()
if err != nil {
return nil, err
}
close(discoveredFiles)
var out []File
for files := range discoveredFiles {
out = append(out, files...)
}
return out, nil
}
func (ls *bucketLister) listFilesFromCDKBucket(opts ListOpts, pathPrefix string) ([]File, error) {
return ls.listFilesFromCursor(opts, ls.buck.List(&blob.ListOptions{
Delimiter: "/",
Prefix: pathPrefix,
}))
}
func (ls *bucketLister) listFilesFromCursor(opts ListOpts, cur *blob.ListIterator) ([]File, error) {
var out []File
for {
obj, err := cur.Next(context.Background())
if err != nil {
if err == io.EOF {
break // finished with cursor
}
return nil, err
}
// Skip this file if it's outside of our query params
if !opts.Inside(obj.ModTime) {
continue
}
dir, name := filepath.Split(obj.Key)
out = append(out, File{
Name: name,
StoragePath: dir,
CreatedAt: obj.ModTime,
})
}
return out, nil
}