/
blobstore.go
348 lines (303 loc) · 9.46 KB
/
blobstore.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
package blobstore
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"cloud.google.com/go/storage"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/buildbuddy-io/buildbuddy/server/config"
"github.com/buildbuddy-io/buildbuddy/server/interfaces"
"github.com/buildbuddy-io/buildbuddy/server/util/disk"
"google.golang.org/api/option"
)
// Returns whatever blobstore is specified in the config.
func GetConfiguredBlobstore(c *config.Configurator) (interfaces.Blobstore, error) {
if c.GetStorageDiskRootDir() != "" {
return NewDiskBlobStore(c.GetStorageDiskRootDir()), nil
}
if gcsConfig := c.GetStorageGCSConfig(); gcsConfig != nil && gcsConfig.Bucket != "" {
opts := make([]option.ClientOption, 0)
if gcsConfig.CredentialsFile != "" {
opts = append(opts, option.WithCredentialsFile(gcsConfig.CredentialsFile))
}
return NewGCSBlobStore(gcsConfig.Bucket, gcsConfig.ProjectID, opts...)
}
if awsConfig := c.GetStorageAWSS3Config(); awsConfig != nil && awsConfig.Bucket != "" {
return NewAwsS3BlobStore(awsConfig)
}
return nil, fmt.Errorf("No storage backend configured -- please specify at least one in the config")
}
// A Disk-based blob storage implementation that reads and writes blobs to/from
// files.
type DiskBlobStore struct {
rootDir string
}
func NewDiskBlobStore(rootDir string) *DiskBlobStore {
return &DiskBlobStore{
rootDir: rootDir,
}
}
func decompress(in []byte, err error) ([]byte, error) {
if err != nil {
return in, err
}
var buf bytes.Buffer
// Write instead of using NewBuffer because if this is not a gzip file
// we want to return "in" directly later, and NewBuffer would take
// ownership of it.
if _, err := buf.Write(in); err != nil {
return nil, err
}
zr, err := gzip.NewReader(&buf)
if err == gzip.ErrHeader {
// Compatibility hack: if we got a header error it means this
// is probably an uncompressed record written before we were
// compressing. Just read it as-is.
return in, nil
}
if err != nil {
log.Printf("zr err: %s", err)
return nil, err
}
rsp, err := ioutil.ReadAll(zr)
if err != nil {
log.Printf("readall err: %s", err)
return nil, err
}
if err := zr.Close(); err != nil {
log.Printf("close err: %s", err)
return nil, err
}
return rsp, nil
}
func compress(in []byte) ([]byte, error) {
var buf bytes.Buffer
zr := gzip.NewWriter(&buf)
if _, err := zr.Write(in); err != nil {
return nil, err
}
if err := zr.Close(); err != nil {
return nil, err
}
return ioutil.ReadAll(&buf)
}
func (d *DiskBlobStore) blobPath(blobName string) (string, error) {
// Probably could be more careful here but we are generating these ourselves
// for now.
if strings.Contains(blobName, "..") {
return "", fmt.Errorf("blobName (%s) must not contain ../", blobName)
}
return filepath.Join(d.rootDir, blobName), nil
}
func (d *DiskBlobStore) WriteBlob(ctx context.Context, blobName string, data []byte) (int, error) {
fullPath, err := d.blobPath(blobName)
if err != nil {
return 0, err
}
compressedData, err := compress(data)
if err != nil {
return 0, err
}
return disk.WriteFile(ctx, fullPath, compressedData)
}
func (d *DiskBlobStore) ReadBlob(ctx context.Context, blobName string) ([]byte, error) {
fullPath, err := d.blobPath(blobName)
if err != nil {
return nil, err
}
return decompress(disk.ReadFile(ctx, fullPath))
}
func (d *DiskBlobStore) DeleteBlob(ctx context.Context, blobName string) error {
fullPath, err := d.blobPath(blobName)
if err != nil {
return err
}
return disk.DeleteFile(ctx, fullPath)
}
func (d *DiskBlobStore) BlobExists(ctx context.Context, blobName string) (bool, error) {
fullPath, err := d.blobPath(blobName)
if err != nil {
return false, err
}
return disk.FileExists(ctx, fullPath)
}
// GCSBlobStore implements the blobstore API on top of the google cloud storage API.
type GCSBlobStore struct {
gcsClient *storage.Client
bucketHandle *storage.BucketHandle
projectID string
}
func NewGCSBlobStore(bucketName, projectID string, opts ...option.ClientOption) (*GCSBlobStore, error) {
ctx := context.Background()
gcsClient, err := storage.NewClient(ctx, opts...)
if err != nil {
return nil, err
}
g := &GCSBlobStore{
gcsClient: gcsClient,
projectID: projectID,
}
err = g.createBucketIfNotExists(ctx, bucketName)
if err != nil {
return nil, err
}
return g, nil
}
func (g *GCSBlobStore) createBucketIfNotExists(ctx context.Context, bucketName string) error {
if _, err := g.gcsClient.Bucket(bucketName).Attrs(ctx); err != nil {
log.Printf("Creating storage bucket: %s", bucketName)
g.bucketHandle = g.gcsClient.Bucket(bucketName)
return g.bucketHandle.Create(ctx, g.projectID, nil)
}
g.bucketHandle = g.gcsClient.Bucket(bucketName)
return nil
}
func (g *GCSBlobStore) ReadBlob(ctx context.Context, blobName string) ([]byte, error) {
reader, err := g.bucketHandle.Object(blobName).NewReader(ctx)
if err != nil {
if err == storage.ErrObjectNotExist {
return nil, os.ErrNotExist
}
return nil, err
}
return decompress(ioutil.ReadAll(reader))
}
func (g *GCSBlobStore) WriteBlob(ctx context.Context, blobName string, data []byte) (int, error) {
writer := g.bucketHandle.Object(blobName).NewWriter(ctx)
defer writer.Close()
compressedData, err := compress(data)
if err != nil {
return 0, err
}
return writer.Write(compressedData)
}
func (g *GCSBlobStore) DeleteBlob(ctx context.Context, blobName string) error {
return g.bucketHandle.Object(blobName).Delete(ctx)
}
func (g *GCSBlobStore) BlobExists(ctx context.Context, blobName string) (bool, error) {
_, err := g.bucketHandle.Object(blobName).Attrs(ctx)
if err == storage.ErrObjectNotExist {
return false, nil
} else if err == nil {
return true, nil
} else {
return false, err
}
}
// AWS stuff
type AwsS3BlobStore struct {
s3 *s3.S3
bucket *string
downloader *s3manager.Downloader
uploader *s3manager.Uploader
}
func NewAwsS3BlobStore(awsConfig *config.AwsS3Config) (*AwsS3BlobStore, error) {
ctx := context.Background()
var creds *credentials.Credentials
// See https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials
if awsConfig.CredentialsProfile != "" {
creds = credentials.NewSharedCredentials("", awsConfig.CredentialsProfile)
}
sess, err := session.NewSession(&aws.Config{
Region: aws.String(awsConfig.Region),
Credentials: creds,
})
if err != nil {
return nil, err
}
// Create S3 service client
svc := s3.New(sess)
awsBlobStore := &AwsS3BlobStore{
s3: svc,
bucket: aws.String(awsConfig.Bucket),
downloader: s3manager.NewDownloader(sess),
uploader: s3manager.NewUploader(sess),
}
// S3 access points can't modify or delete buckets
// https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/doc_source/access-points.md
const s3AccessPointPrefix = "arn:aws:s3"
if strings.HasPrefix(awsConfig.Bucket, s3AccessPointPrefix) {
log.Printf("Encountered an S3 access point %s...not creating bucket", awsConfig.Bucket)
} else {
if err := awsBlobStore.createBucketIfNotExists(ctx, awsConfig.Bucket); err != nil {
return nil, err
}
}
return awsBlobStore, nil
}
func (a *AwsS3BlobStore) createBucketIfNotExists(ctx context.Context, bucketName string) error {
// HeadBucket call will return 404 or 403
if _, err := a.s3.HeadBucketWithContext(ctx, &s3.HeadBucketInput{Bucket: aws.String(bucketName)}); err != nil {
aerr := err.(awserr.Error)
// AWS returns codes as strings
// https://github.com/aws/aws-sdk-go/blob/master/service/s3/s3manager/bucket_region_test.go#L70
if aerr.Code() != "NotFound" {
return err
}
log.Printf("Creating storage bucket: %s", bucketName)
if _, err := a.s3.CreateBucketWithContext(ctx, &s3.CreateBucketInput{Bucket: aws.String(bucketName)}); err != nil {
return err
}
return a.s3.WaitUntilBucketExistsWithContext(ctx, &s3.HeadBucketInput{
Bucket: aws.String(bucketName),
})
}
return nil
}
func (a *AwsS3BlobStore) ReadBlob(ctx context.Context, blobName string) ([]byte, error) {
buff := &aws.WriteAtBuffer{}
_, err := a.downloader.DownloadWithContext(ctx, buff, &s3.GetObjectInput{
Bucket: a.bucket,
Key: aws.String(blobName),
})
return decompress(buff.Bytes(), err)
}
func (a *AwsS3BlobStore) WriteBlob(ctx context.Context, blobName string, data []byte) (int, error) {
compressedData, err := compress(data)
if err != nil {
return 0, err
}
uploadParams := &s3manager.UploadInput{
Bucket: a.bucket,
Key: aws.String(blobName),
Body: bytes.NewReader(compressedData),
}
if _, err := a.uploader.UploadWithContext(ctx, uploadParams); err != nil {
return -1, err
}
return len(compressedData), nil
}
func (a *AwsS3BlobStore) DeleteBlob(ctx context.Context, blobName string) error {
deleteParams := &s3.DeleteObjectInput{
Bucket: a.bucket,
Key: aws.String(blobName),
}
if _, err := a.s3.DeleteObjectWithContext(ctx, deleteParams); err != nil {
return err
}
return a.s3.WaitUntilObjectNotExistsWithContext(ctx, &s3.HeadObjectInput{
Bucket: a.bucket,
Key: aws.String(blobName),
})
}
func (a *AwsS3BlobStore) BlobExists(ctx context.Context, blobName string) (bool, error) {
params := &s3.HeadObjectInput{
Bucket: a.bucket,
Key: aws.String(blobName),
}
if _, err := a.s3.HeadObjectWithContext(ctx, params); err != nil {
return false, err
}
return true, nil
}