/
datastore_aws.go
403 lines (357 loc) · 10.3 KB
/
datastore_aws.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
// Copyright(c) 2017-2018 Zededa, Inc.
// All rights reserved.
package zedUpload
import (
"fmt"
"net"
"net/url"
"os"
"strings"
"time"
"github.com/lf-edge/eve/libs/nettrace"
zedAWS "github.com/lf-edge/eve/libs/zedUpload/awsutil"
"github.com/lf-edge/eve/libs/zedUpload/types"
)
// Action : execute selected action targeting AWS datastore.
func (ep *AwsTransportMethod) Action(req *DronaRequest) error {
var err error
var size int
var list []string
var contentLength int64
var remoteFileMD5 string
switch req.operation {
case SyncOpUpload:
err, size = ep.processS3Upload(req)
case SyncOpDownload:
err, size = ep.processS3Download(req)
case SyncOpDelete:
err = ep.processS3Delete(req)
case SyncOpList:
list, err, size = ep.processS3List(req)
req.imgList = list
case SyncOpGetObjectMetaData:
contentLength, remoteFileMD5, err = ep.processS3ObjectMetaData(req)
req.contentLength = contentLength
req.remoteFileMD5 = remoteFileMD5
case SysOpPutPart:
etagID, uploadID, err := ep.processMultipartUpload(req)
if err == nil {
req.UploadID = uploadID
req.EtagID = etagID
}
case SysOpCompleteParts:
err = ep.completeMultipartUpload(req)
case SyncOpGetURI:
sasURL, err := ep.generateSignedURL(req)
if err == nil {
req.SasURI = sasURL
}
case SysOpDownloadByChunks:
err = ep.processS3DownloadByChunks(req)
default:
err = fmt.Errorf("Unknown AWS S3 datastore operation")
}
req.asize = int64(size)
if err != nil {
req.status = fmt.Sprintf("%v", err)
}
return err
}
func (ep *AwsTransportMethod) Open() error {
return nil
}
func (ep *AwsTransportMethod) Close() error {
return ep.hClientWrap.close()
}
// WithSrcIP : use the specific IP as source address for this connection.
func (ep *AwsTransportMethod) WithSrcIP(localAddr net.IP) error {
return ep.hClientWrap.withSrcIP(localAddr)
}
// WithProxy : connect via the provided proxy URL.
func (ep *AwsTransportMethod) WithProxy(proxy *url.URL) error {
return ep.hClientWrap.withProxy(proxy)
}
// WithTrustedCerts : run requests with these certificates added as trusted.
func (ep *AwsTransportMethod) WithTrustedCerts(certs [][]byte) error {
return ep.hClientWrap.withTrustedCerts(certs)
}
// WithBindIntf : bind to specific interface for this connection
func (ep *AwsTransportMethod) WithBindIntf(intf string) error {
return ep.hClientWrap.withBindIntf(intf)
}
// WithLogging enables or disables logging.
func (ep *AwsTransportMethod) WithLogging(onoff bool) error {
return nil
}
// WithNetTracing enables network tracing.
func (ep *AwsTransportMethod) WithNetTracing(opts ...nettrace.TraceOpt) error {
return ep.hClientWrap.withNetTracing(opts...)
}
// GetNetTrace returns collected network trace and packet captures.
func (ep *AwsTransportMethod) GetNetTrace(description string) (
nettrace.AnyNetTrace, []nettrace.PacketCapture, error) {
return ep.hClientWrap.getNetTrace(description)
}
// File upload to AWS S3 Datastore
func (ep *AwsTransportMethod) processS3Upload(req *DronaRequest) (error, int) {
fInfo, err := os.Stat(req.objloc)
if err != nil {
return err, 0
}
prgChan := make(types.StatsNotifChan)
defer close(prgChan)
if req.ackback {
go statsUpdater(req, ep.ctx, prgChan)
}
hClient, err := ep.hClientWrap.unwrap()
if err != nil {
return err, 0
}
// FiXME: strings.TrimSuffix needs to go away once final soultion is done.
// upload, always the compression file.
sc := zedAWS.NewAwsCtx(ep.token, ep.apiKey, ep.region, hClient)
if sc == nil {
return fmt.Errorf("unable to create S3 context"), 0
}
if req.cancelContext != nil {
sc = sc.WithContext(req.cancelContext)
}
if req.logger != nil {
sc = sc.WithLogger(req.logger)
}
location, err := sc.UploadFile(req.objloc, ep.bucket, req.name, false, prgChan)
if len(location) > 0 {
req.objloc = location
}
return err, int(fInfo.Size())
}
// File download from AWS S3 Datastore
func (ep *AwsTransportMethod) processS3Download(req *DronaRequest) (error, int) {
var csize int
pwd := strings.TrimSuffix(ep.apiKey, "\n")
prgChan := make(types.StatsNotifChan)
defer close(prgChan)
hClient, err := ep.hClientWrap.unwrap()
if err != nil {
return err, 0
}
if req.ackback {
s := zedAWS.NewAwsCtx(ep.token, pwd, ep.region, hClient)
if req.cancelContext != nil {
s = s.WithContext(req.cancelContext)
}
if req.logger != nil {
s = s.WithLogger(req.logger)
}
if s != nil {
err, length := s.GetObjectSize(ep.bucket, req.name)
if err == nil {
ep.ctx.postSize(req, length, 0)
}
}
go statsUpdater(req, ep.ctx, prgChan)
}
sc := zedAWS.NewAwsCtx(ep.token, pwd, ep.region, hClient)
if sc == nil {
return fmt.Errorf("unable to create S3 context"), 0
}
if req.cancelContext != nil {
sc = sc.WithContext(req.cancelContext)
}
if req.logger != nil {
sc = sc.WithLogger(req.logger)
}
doneParts, err := sc.DownloadFile(req.objloc, ep.bucket, req.name, req.sizelimit, req.doneParts, prgChan)
req.doneParts = doneParts
if err != nil {
return err, 0
}
// check for download complete
st, err := os.Stat(req.objloc)
if err != nil {
return err, 0
}
csize = int(st.Size())
return err, csize
}
func (ep *AwsTransportMethod) processS3DownloadByChunks(req *DronaRequest) error {
hClient, err := ep.hClientWrap.unwrap()
if err != nil {
return err
}
pwd := strings.TrimSuffix(ep.apiKey, "\n")
sc := zedAWS.NewAwsCtx(ep.token, pwd, ep.region, hClient)
if sc == nil {
return fmt.Errorf("unable to create S3 context")
}
if req.cancelContext != nil {
sc = sc.WithContext(req.cancelContext)
}
if req.logger != nil {
sc = sc.WithLogger(req.logger)
}
readCloser, size, err := sc.DownloadFileByChunks(req.objloc, ep.bucket, req.name)
if err != nil {
return err
}
req.chunkInfoChan = make(chan ChunkData, 1)
chunkChan := make(chan ChunkData)
go func(chunkChan chan ChunkData) {
for chunkData := range chunkChan {
ep.ctx.postChunk(req, chunkData)
}
}(chunkChan)
return processChunkByChunk(readCloser, size, chunkChan)
}
// File delete from AWS S3 Datastore
func (ep *AwsTransportMethod) processS3Delete(req *DronaRequest) error {
hClient, err := ep.hClientWrap.unwrap()
if err != nil {
return err
}
s3ctx := zedAWS.NewAwsCtx(ep.token, ep.apiKey, ep.region, hClient)
if s3ctx != nil {
if req.cancelContext != nil {
s3ctx = s3ctx.WithContext(req.cancelContext)
}
if req.logger != nil {
s3ctx = s3ctx.WithLogger(req.logger)
}
err = s3ctx.DeleteObject(ep.bucket, req.name)
} else {
return fmt.Errorf("no s3 context")
}
if err != nil {
return err
}
//log.Printf("Successfully deleted file from %s, bucket:%s, %s", ep.token, ep.bucket, req.name)
return err
}
// File list from AWS S3 Datastore
func (ep *AwsTransportMethod) processS3List(req *DronaRequest) ([]string, error, int) {
var csize int
var s []string
pwd := strings.TrimSuffix(ep.apiKey, "\n")
prgChan := make(types.StatsNotifChan)
defer close(prgChan)
if req.ackback {
go statsUpdater(req, ep.ctx, prgChan)
}
hClient, err := ep.hClientWrap.unwrap()
if err != nil {
return s, err, 0
}
sc := zedAWS.NewAwsCtx(ep.token, pwd, ep.region, hClient)
if sc == nil {
return s, fmt.Errorf("unable to create S3 context"), 0
}
if req.cancelContext != nil {
sc = sc.WithContext(req.cancelContext)
}
if req.logger != nil {
sc = sc.WithLogger(req.logger)
}
list, err := sc.ListImages(ep.bucket, prgChan)
if err != nil {
return s, err, 0
}
//log.Printf("S3 Image List: %v", list)
//log.Printf("Successfully listed s3 images at %v", ep.bucket)
return list, err, csize
}
// Verify Uploaded Object Size and MD5 sum
func (ep *AwsTransportMethod) processS3ObjectMetaData(req *DronaRequest) (int64, string, error) {
hClient, err := ep.hClientWrap.unwrap()
if err != nil {
return 0, "", err
}
pwd := strings.TrimSuffix(ep.apiKey, "\n")
sc := zedAWS.NewAwsCtx(ep.token, pwd, ep.region, hClient)
if sc == nil {
return 0, "", fmt.Errorf("unable to create S3 context")
}
if req.cancelContext != nil {
sc = sc.WithContext(req.cancelContext)
}
if req.logger != nil {
sc = sc.WithLogger(req.logger)
}
size, remoteFileMD5, err := sc.GetObjectMetaData(ep.bucket, req.name)
if len(remoteFileMD5) > 0 && remoteFileMD5[0] == '"' {
remoteFileMD5 = remoteFileMD5[1:]
}
if len(remoteFileMD5) > 0 && remoteFileMD5[len(remoteFileMD5)-1] == '"' {
remoteFileMD5 = remoteFileMD5[:len(remoteFileMD5)-1]
}
return size, remoteFileMD5, err
}
func (ep *AwsTransportMethod) NewRequest(opType SyncOpType, objname, objloc string, sizelimit int64, ackback bool, reply chan *DronaRequest) *DronaRequest {
dR := &DronaRequest{}
dR.syncEp = ep
dR.operation = opType
dR.name = objname
dR.ackback = ackback
// FIXME:...we need this later
dR.localName = objname
dR.objloc = objloc
// limit for this download
dR.sizelimit = sizelimit
dR.result = reply
return dR
}
func (ep *AwsTransportMethod) getContext() *DronaCtx {
return ep.ctx
}
func (ep *AwsTransportMethod) processMultipartUpload(req *DronaRequest) (string, string, error) {
hClient, err := ep.hClientWrap.unwrap()
if err != nil {
return "", "", err
}
s3ctx := zedAWS.NewAwsCtx(ep.token, ep.apiKey, ep.region, hClient)
if req.cancelContext != nil {
s3ctx = s3ctx.WithContext(req.cancelContext)
}
if req.logger != nil {
s3ctx = s3ctx.WithLogger(req.logger)
}
return s3ctx.UploadPart(ep.bucket, req.localName, req.Adata, req.PartID, req.UploadID)
}
func (ep *AwsTransportMethod) completeMultipartUpload(req *DronaRequest) error {
hClient, err := ep.hClientWrap.unwrap()
if err != nil {
return err
}
s3ctx := zedAWS.NewAwsCtx(ep.token, ep.apiKey, ep.region, hClient)
if req.cancelContext != nil {
s3ctx = s3ctx.WithContext(req.cancelContext)
}
if req.logger != nil {
s3ctx = s3ctx.WithLogger(req.logger)
}
return s3ctx.CompleteUploadedParts(ep.bucket, req.localName, req.UploadID, req.Blocks)
}
func (ep *AwsTransportMethod) generateSignedURL(req *DronaRequest) (string, error) {
hClient, err := ep.hClientWrap.unwrap()
if err != nil {
return "", err
}
s3ctx := zedAWS.NewAwsCtx(ep.token, ep.apiKey, ep.region, hClient)
if req.cancelContext != nil {
s3ctx = s3ctx.WithContext(req.cancelContext)
}
if req.logger != nil {
s3ctx = s3ctx.WithLogger(req.logger)
}
return s3ctx.GetSignedURL(ep.bucket, req.localName, req.Duration)
}
type AwsTransportMethod struct {
transport SyncTransportType
region string
bucket string
//Auth
token string
apiKey string
failPostTime time.Time
ctx *DronaCtx
hClientWrap *httpClientWrapper
}