/
s3_storage.go
398 lines (319 loc) · 10 KB
/
s3_storage.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
// Package s3 implements Storage based on an S3 bucket.
package s3
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/clock"
"github.com/kopia/kopia/internal/gather"
"github.com/kopia/kopia/internal/iocopy"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/blob/retrying"
)
const (
s3storageType = "s3"
latestVersionID = ""
)
type s3Storage struct {
Options
cli *minio.Client
storageConfig *StorageConfig
}
func (s *s3Storage) GetCapacity(ctx context.Context) (blob.Capacity, error) {
return blob.Capacity{}, blob.ErrNotAVolume
}
func (s *s3Storage) GetBlob(ctx context.Context, b blob.ID, offset, length int64, output blob.OutputBuffer) error {
return s.getBlobWithVersion(ctx, b, latestVersionID, offset, length, output)
}
// getBlobWithVersion returns full or partial contents of a blob with given ID and version.
func (s *s3Storage) getBlobWithVersion(ctx context.Context, b blob.ID, version string, offset, length int64, output blob.OutputBuffer) error {
output.Reset()
attempt := func() error {
opt := minio.GetObjectOptions{VersionID: version}
if length > 0 {
if err := opt.SetRange(offset, offset+length-1); err != nil {
return errors.Wrap(blob.ErrInvalidRange, "unable to set range")
}
}
if length == 0 {
// zero-length ranges require special handling, set non-zero range and
// we won't be trying to read the response anyway.
if err := opt.SetRange(0, 1); err != nil {
return errors.Wrap(blob.ErrInvalidRange, "unable to set range")
}
}
o, err := s.cli.GetObject(ctx, s.BucketName, s.getObjectNameString(b), opt)
if err != nil {
return errors.Wrap(err, "GetObject")
}
defer o.Close() //nolint:errcheck
if length == 0 {
return nil
}
//nolint:wrapcheck
return iocopy.JustCopy(output, o)
}
if err := attempt(); err != nil {
return translateError(err)
}
//nolint:wrapcheck
return blob.EnsureLengthExactly(output.Length(), length)
}
func isInvalidCredentials(err error) bool {
return err != nil && strings.Contains(err.Error(), blob.InvalidCredentialsErrStr)
}
func translateError(err error) error {
var me minio.ErrorResponse
if isInvalidCredentials(err) {
return blob.ErrInvalidCredentials
}
if errors.As(err, &me) {
switch me.StatusCode {
case http.StatusOK:
return nil
case http.StatusNotFound:
return blob.ErrBlobNotFound
case http.StatusRequestedRangeNotSatisfiable:
return blob.ErrInvalidRange
}
}
return err
}
func (s *s3Storage) GetMetadata(ctx context.Context, b blob.ID) (blob.Metadata, error) {
vm, err := s.getVersionMetadata(ctx, b, "")
return vm.Metadata, err
}
func (s *s3Storage) getVersionMetadata(ctx context.Context, b blob.ID, version string) (versionMetadata, error) {
opts := minio.GetObjectOptions{
VersionID: version,
}
oi, err := s.cli.StatObject(ctx, s.BucketName, s.getObjectNameString(b), opts)
if err != nil {
return versionMetadata{}, errors.Wrap(translateError(err), "StatObject")
}
return infoToVersionMetadata(s.Prefix, &oi), nil
}
func (s *s3Storage) PutBlob(ctx context.Context, b blob.ID, data blob.Bytes, opts blob.PutOptions) error {
switch {
case opts.DoNotRecreate:
return errors.Wrap(blob.ErrUnsupportedPutBlobOption, "do-not-recreate")
case !opts.SetModTime.IsZero():
return blob.ErrSetTimeUnsupported
}
_, err := s.putBlob(ctx, b, data, opts)
if opts.GetModTime != nil {
bm, err2 := s.GetMetadata(ctx, b)
if err2 != nil {
return err2
}
*opts.GetModTime = bm.Timestamp
}
return err
}
func (s *s3Storage) putBlob(ctx context.Context, b blob.ID, data blob.Bytes, opts blob.PutOptions) (versionMetadata, error) {
var (
storageClass = s.storageConfig.getStorageClassForBlobID(b)
retentionMode minio.RetentionMode
retainUntilDate time.Time
)
if opts.RetentionPeriod != 0 {
retentionMode = minio.RetentionMode(opts.RetentionMode)
if !retentionMode.IsValid() {
return versionMetadata{}, errors.Errorf("invalid retention mode: %q", opts.RetentionMode)
}
retainUntilDate = clock.Now().Add(opts.RetentionPeriod).UTC()
}
uploadInfo, err := s.cli.PutObject(ctx, s.BucketName, s.getObjectNameString(b), data.Reader(), int64(data.Length()), minio.PutObjectOptions{
ContentType: "application/x-kopia",
// Kopia already splits snapshot contents into small blobs to improve
// upload throughput. There is no need for further splitting
// through multipart uploads.
DisableMultipart: true,
// The Content-MD5 header is required for any request to upload an object
// with a retention period configured using Amazon S3 Object Lock.
// Unconditionally computing the content MD5, potentially incurring
// a slightly higher CPU overhead.
SendContentMd5: true,
StorageClass: storageClass,
RetainUntilDate: retainUntilDate,
Mode: retentionMode,
})
if isInvalidCredentials(err) {
return versionMetadata{}, blob.ErrInvalidCredentials
}
var er minio.ErrorResponse
if errors.As(err, &er) && er.Code == "InvalidRequest" && strings.Contains(strings.ToLower(er.Message), "content-md5") {
return versionMetadata{}, err //nolint:wrapcheck
}
if errors.Is(err, io.EOF) && uploadInfo.Size == 0 {
// special case empty stream
_, err = s.cli.PutObject(ctx, s.BucketName, s.getObjectNameString(b), bytes.NewBuffer(nil), 0, minio.PutObjectOptions{
ContentType: "application/x-kopia",
StorageClass: storageClass,
RetainUntilDate: retainUntilDate,
Mode: retentionMode,
})
}
if err != nil {
return versionMetadata{}, err //nolint:wrapcheck
}
return versionMetadata{
Metadata: blob.Metadata{
BlobID: b,
Length: uploadInfo.Size,
Timestamp: uploadInfo.LastModified,
},
Version: uploadInfo.VersionID,
}, nil
}
func (s *s3Storage) DeleteBlob(ctx context.Context, b blob.ID) error {
err := translateError(s.cli.RemoveObject(ctx, s.BucketName, s.getObjectNameString(b), minio.RemoveObjectOptions{}))
if errors.Is(err, blob.ErrBlobNotFound) {
return nil
}
return err
}
func (s *s3Storage) getObjectNameString(b blob.ID) string {
return s.Prefix + string(b)
}
func (s *s3Storage) ListBlobs(ctx context.Context, prefix blob.ID, callback func(blob.Metadata) error) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
oi := s.cli.ListObjects(ctx, s.BucketName, minio.ListObjectsOptions{
Prefix: s.getObjectNameString(prefix),
})
for o := range oi {
if err := o.Err; err != nil {
if isInvalidCredentials(err) {
return blob.ErrInvalidCredentials
}
return err
}
bm := blob.Metadata{
BlobID: blob.ID(o.Key[len(s.Prefix):]),
Length: o.Size,
Timestamp: o.LastModified,
}
if bm.BlobID == ConfigName {
continue
}
if err := callback(bm); err != nil {
return err
}
}
return nil
}
func (s *s3Storage) ConnectionInfo() blob.ConnectionInfo {
return blob.ConnectionInfo{
Type: s3storageType,
Config: &s.Options,
}
}
func (s *s3Storage) Close(ctx context.Context) error {
return nil
}
func (s *s3Storage) String() string {
return fmt.Sprintf("s3://%v/%v", s.BucketName, s.Prefix)
}
func (s *s3Storage) DisplayName() string {
return fmt.Sprintf("S3: %v %v", s.Endpoint, s.BucketName)
}
func (s *s3Storage) FlushCaches(ctx context.Context) error {
return nil
}
func getCustomTransport(insecureSkipVerify bool) (transport *http.Transport) {
//nolint:gosec
customTransport := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify}}
return customTransport
}
// New creates new S3-backed storage with specified options:
//
// - the 'BucketName' field is required and all other parameters are optional.
func New(ctx context.Context, opt *Options) (blob.Storage, error) {
st, err := newStorage(ctx, opt)
if err != nil {
return nil, err
}
s, err := maybePointInTimeStore(ctx, st, opt.PointInTime)
if err != nil {
return nil, err
}
return retrying.NewWrapper(s), nil
}
func newStorage(ctx context.Context, opt *Options) (*s3Storage, error) {
creds := credentials.NewChainCredentials(
[]credentials.Provider{
&credentials.Static{
Value: credentials.Value{
AccessKeyID: opt.AccessKeyID,
SecretAccessKey: opt.SecretAccessKey,
SessionToken: opt.SessionToken,
SignerType: credentials.SignatureV4,
},
},
&credentials.EnvAWS{},
&credentials.IAM{
Client: &http.Client{
Transport: http.DefaultTransport,
},
},
},
)
return newStorageWithCredentials(ctx, creds, opt)
}
func newStorageWithCredentials(ctx context.Context, creds *credentials.Credentials, opt *Options) (*s3Storage, error) {
if opt.BucketName == "" {
return nil, errors.New("bucket name must be specified")
}
minioOpts := &minio.Options{
Creds: creds,
Secure: !opt.DoNotUseTLS,
Region: opt.Region,
}
if opt.DoNotVerifyTLS {
minioOpts.Transport = getCustomTransport(true)
}
cli, err := minio.New(opt.Endpoint, minioOpts)
if err != nil {
return nil, errors.Wrap(err, "unable to create client")
}
ok, err := cli.BucketExists(ctx, opt.BucketName)
if err != nil {
return nil, errors.Wrapf(err, "unable to determine if bucket %q exists", opt.BucketName)
}
if !ok {
return nil, errors.Errorf("bucket %q does not exist", opt.BucketName)
}
s := s3Storage{
Options: *opt,
cli: cli,
storageConfig: &StorageConfig{},
}
var scOutput gather.WriteBuffer
if getBlobErr := s.GetBlob(ctx, ConfigName, 0, -1, &scOutput); getBlobErr == nil {
if scErr := s.storageConfig.Load(scOutput.Bytes().Reader()); scErr != nil {
return nil, errors.Wrapf(scErr, "error parsing storage config for bucket %q", opt.BucketName)
}
} else if !errors.Is(getBlobErr, blob.ErrBlobNotFound) {
return nil, errors.Wrapf(getBlobErr, "error retrieving storage config from bucket %q", opt.BucketName)
}
return &s, nil
}
func init() {
blob.AddSupportedStorage(
s3storageType,
func() interface{} {
return &Options{}
},
func(ctx context.Context, o interface{}, isCreate bool) (blob.Storage, error) {
return New(ctx, o.(*Options)) //nolint:forcetypeassert
})
}