forked from pachyderm/pachyderm
/
amazon_client.go
462 lines (422 loc) · 13.4 KB
/
amazon_client.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package obj
import (
"bytes"
"context"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"net/http"
"path"
"strings"
"sync"
"time"
"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/cloudfront/sign"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/storagegateway"
"github.com/pachyderm/pachyderm/src/client/pkg/tracing"
"github.com/pachyderm/pachyderm/src/server/pkg/backoff"
log "github.com/sirupsen/logrus"
vault "github.com/hashicorp/vault/api"
)
const oneDayInSeconds = 60 * 60 * 24
const twoDaysInSeconds = 60 * 60 * 48
const maxRetries = 10
var (
// By default, objects uploaded to a bucket are only accessible to the
// uploader, and not the owner of the bucket. We want to ensure that
// the owner of the bucket can access the buckets as well.
uploadACL = "bucket-owner-full-control"
)
type amazonClient struct {
bucket string
cloudfrontDistribution string
cloudfrontURLSigner *sign.URLSigner
s3 *s3.S3
uploader *s3manager.Uploader
// When true, keys are stored in reversed order; e.g. the key "ABCD" is
// rewritten to "DCBA". This is because, as of 12/2018, if a lot of assets
// have the same prefix, the same S3 servers will be hit. This comes into
// conflict with Pachyderm, which prefixes the keys of blocks with where
// the blocks came from, causing a lot of blocks with the same prefix to
// be written around the same time, and overloading S3. Reversing the
// order of keys gives an easy way to spread out S3 assets and
// substantially speed up block writing.
reversed bool
}
type vaultCredentialsProvider struct {
vaultClient *vault.Client // client used to retrieve S3 creds from vault
vaultRole string // get vault creds from: /aws/creds/<vaultRole>
// ID, duration, and last renew time of the vault lease that governs the most
// recent AWS secret issued to this pachd instance (and a mutex protecting
// them)
leaseMu sync.Mutex
leaseID string
leaseLastRenew time.Time
leaseDuration time.Duration
}
// updateLease extracts the duration of the lease governing 'secret' (an AWS
// secret). IIUC, because the AWS backend issues dynamic secrets, there is no
// tokens associated with them, and vaultSecret.TokenTTL can be ignored
func (v *vaultCredentialsProvider) updateLease(secret *vault.Secret) {
v.leaseMu.Lock()
defer v.leaseMu.Unlock()
v.leaseID = secret.LeaseID
v.leaseLastRenew = time.Now()
v.leaseDuration = time.Duration(secret.LeaseDuration) * time.Second
}
func (v *vaultCredentialsProvider) getLeaseDuration() time.Duration {
v.leaseMu.Lock()
defer v.leaseMu.Unlock()
return v.leaseDuration
}
// Retrieve returns nil if it successfully retrieved the value. Error is
// returned if the value were not obtainable, or empty.
func (v *vaultCredentialsProvider) Retrieve() (credentials.Value, error) {
var emptyCreds, result credentials.Value // result
// retrieve AWS creds from vault
vaultSecret, err := v.vaultClient.Logical().Read(path.Join("aws", "creds", v.vaultRole))
if err != nil {
return emptyCreds, fmt.Errorf("could not retrieve creds from vault: %v", err)
}
accessKeyIface, accessKeyOk := vaultSecret.Data["access_key"]
awsSecretIface, awsSecretOk := vaultSecret.Data["secret_key"]
if !accessKeyOk || !awsSecretOk {
return emptyCreds, fmt.Errorf("aws creds not present in vault response")
}
// Convert access key & secret in response to strings
result.AccessKeyID, accessKeyOk = accessKeyIface.(string)
result.SecretAccessKey, awsSecretOk = awsSecretIface.(string)
if !accessKeyOk || !awsSecretOk {
return emptyCreds, fmt.Errorf("aws creds in vault response were not both strings (%T and %T)", accessKeyIface, awsSecretIface)
}
// update the lease values in 'v', and spawn a goroutine to renew the lease
v.updateLease(vaultSecret)
go func() {
for {
// renew at half the lease duration or one day, whichever is greater
// (lease must expire eventually)
renewInterval := v.getLeaseDuration()
if renewInterval.Seconds() < oneDayInSeconds {
renewInterval = oneDayInSeconds * time.Second
}
// Wait until 'renewInterval' has elapsed, then renew the lease
time.Sleep(renewInterval)
backoff.RetryNotify(func() error {
// every two days, renew the lease for this node's AWS credentials
vaultSecret, err := v.vaultClient.Sys().Renew(v.leaseID, twoDaysInSeconds)
if err != nil {
return err
}
v.updateLease(vaultSecret)
return nil
}, backoff.NewExponentialBackOff(), func(err error, _ time.Duration) error {
log.Errorf("could not renew vault lease: %v", err)
return nil
})
}
}()
// Per https://www.vaultproject.io/docs/secrets/aws/index.html#usage, wait
// until token is usable
time.Sleep(10 * time.Second)
return result, nil
}
// IsExpired returns if the credentials are no longer valid, and need to be
// retrieved.
func (v *vaultCredentialsProvider) IsExpired() bool {
v.leaseMu.Lock()
defer v.leaseMu.Unlock()
return time.Now().After(v.leaseLastRenew.Add(v.leaseDuration))
}
// AmazonCreds are options that are applicable specifically to Pachd's
// credentials in an AWS deployment
type AmazonCreds struct {
// Direct credentials. Only applicable if Pachyderm is given its own permanent
// AWS credentials
ID string // Access Key ID
Secret string // Secret Access Key
Token string // Access token (if using temporary security credentials
// Vault options (if getting AWS credentials from Vault)
VaultAddress string // normally addresses come from env, but don't have vault service name
VaultRole string
VaultToken string
}
func newAmazonClient(region, bucket string, creds *AmazonCreds, cloudfrontDistribution string, endpoint string, reversed ...bool) (*amazonClient, error) {
// set up aws config, including credentials (if neither creds.ID nor
// creds.VaultAddress are set, then this will use the EC2 metadata service
awsConfig := &aws.Config{
Region: aws.String(region),
MaxRetries: aws.Int(maxRetries),
}
if creds.ID != "" {
awsConfig.Credentials = credentials.NewStaticCredentials(creds.ID, creds.Secret, creds.Token)
} else if creds.VaultAddress != "" {
vaultClient, err := vault.NewClient(&vault.Config{
Address: creds.VaultAddress,
})
if err != nil {
return nil, fmt.Errorf("error creating vault client: %v", err)
}
vaultClient.SetToken(creds.VaultToken)
awsConfig.Credentials = credentials.NewCredentials(&vaultCredentialsProvider{
vaultClient: vaultClient,
vaultRole: creds.VaultRole,
})
}
// Set custom endpoint for a custom deployment.
if endpoint != "" {
awsConfig.Endpoint = aws.String(endpoint)
awsConfig.S3ForcePathStyle = aws.Bool(true)
}
// Create new session using awsConfig
session := session.New(awsConfig)
var r bool
if len(reversed) > 0 {
r = reversed[0]
} else {
r = true
}
awsClient := &amazonClient{
bucket: bucket,
s3: s3.New(session),
uploader: s3manager.NewUploader(session),
reversed: r,
}
// Set awsClient.cloudfrontURLSigner and cloudfrontDistribution (if Pachd is
// using cloudfront)
awsClient.cloudfrontDistribution = strings.TrimSpace(cloudfrontDistribution)
if cloudfrontDistribution != "" {
rawCloudfrontPrivateKey, err := readSecretFile("/cloudfrontPrivateKey")
if err != nil {
return nil, err
}
cloudfrontKeyPairID, err := readSecretFile("/cloudfrontKeyPairId")
if err != nil {
return nil, err
}
block, _ := pem.Decode(bytes.TrimSpace([]byte(rawCloudfrontPrivateKey)))
if block == nil || block.Type != "RSA PRIVATE KEY" {
return nil, fmt.Errorf("block undefined or wrong type: type is (%v) should be (RSA PRIVATE KEY)", block.Type)
}
cloudfrontPrivateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
awsClient.cloudfrontURLSigner = sign.NewURLSigner(cloudfrontKeyPairID, cloudfrontPrivateKey)
log.Infof("Using cloudfront security credentials - keypair ID (%v) - to sign cloudfront URLs", string(cloudfrontKeyPairID))
}
return awsClient, nil
}
func (c *amazonClient) Writer(ctx context.Context, name string) (io.WriteCloser, error) {
if c.reversed {
name = reverse(name)
}
return newBackoffWriteCloser(ctx, c, newWriter(ctx, c, name)), nil
}
func (c *amazonClient) Walk(_ context.Context, name string, fn func(name string) error) error {
var fnErr error
var prefix *string
if c.reversed {
prefix = nil
} else {
prefix = &name
}
if err := c.s3.ListObjectsPages(
&s3.ListObjectsInput{
Bucket: aws.String(c.bucket),
Prefix: prefix,
},
func(listObjectsOutput *s3.ListObjectsOutput, lastPage bool) bool {
for _, object := range listObjectsOutput.Contents {
key := *object.Key
if c.reversed {
key = reverse(key)
}
if strings.HasPrefix(key, name) {
if err := fn(key); err != nil {
fnErr = err
return false
}
}
}
return true
},
); err != nil {
return err
}
return fnErr
}
func (c *amazonClient) Reader(ctx context.Context, name string, offset uint64, size uint64) (io.ReadCloser, error) {
if c.reversed {
name = reverse(name)
}
byteRange := byteRange(offset, size)
if byteRange != "" {
byteRange = fmt.Sprintf("bytes=%s", byteRange)
}
var reader io.ReadCloser
if c.cloudfrontDistribution != "" {
var resp *http.Response
var connErr error
url := fmt.Sprintf("http://%v.cloudfront.net/%v", c.cloudfrontDistribution, name)
if c.cloudfrontURLSigner != nil {
signedURL, err := c.cloudfrontURLSigner.Sign(url, time.Now().Add(1*time.Hour))
if err != nil {
return nil, err
}
url = strings.TrimSpace(signedURL)
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Range", byteRange)
backoff.RetryNotify(func() error {
span, _ := tracing.AddSpanToAnyExisting(ctx, "aws/cloudfront.Get")
defer tracing.FinishAnySpan(span)
resp, connErr = http.DefaultClient.Do(req)
if connErr != nil && isNetRetryable(connErr) {
return connErr
}
return nil
}, backoff.NewExponentialBackOff(), func(err error, d time.Duration) error {
log.Infof("Error connecting to (%v); retrying in %s: %#v", url, d, err)
return nil
})
if connErr != nil {
return nil, connErr
}
if resp.StatusCode >= 300 {
// Cloudfront returns 200s, and 206s as success codes
return nil, fmt.Errorf("cloudfront returned HTTP error code %v for url %v", resp.Status, url)
}
reader = resp.Body
} else {
objIn := &s3.GetObjectInput{
Bucket: aws.String(c.bucket),
Key: aws.String(name),
}
if byteRange != "" {
objIn.Range = aws.String(byteRange)
}
getObjectOutput, err := c.s3.GetObject(objIn)
if err != nil {
return nil, err
}
reader = getObjectOutput.Body
}
return newBackoffReadCloser(ctx, c, reader), nil
}
func (c *amazonClient) Delete(_ context.Context, name string) error {
if c.reversed {
name = reverse(name)
}
_, err := c.s3.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(c.bucket),
Key: aws.String(name),
})
return err
}
func (c *amazonClient) Exists(_ context.Context, name string) bool {
if c.reversed {
name = reverse(name)
}
_, err := c.s3.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(c.bucket),
Key: aws.String(name),
})
return err == nil
}
func (c *amazonClient) IsRetryable(err error) (retVal bool) {
if strings.Contains(err.Error(), "unexpected EOF") {
return true
}
if strings.Contains(err.Error(), "SlowDown:") {
return true
}
awsErr, ok := err.(awserr.Error)
if !ok {
return false
}
for _, c := range []string{
storagegateway.ErrorCodeServiceUnavailable,
storagegateway.ErrorCodeInternalError,
storagegateway.ErrorCodeGatewayInternalError,
} {
if c == awsErr.Code() {
return true
}
}
return false
}
func (c *amazonClient) IsIgnorable(err error) bool {
return false
}
func (c *amazonClient) IsNotExist(err error) bool {
if c.cloudfrontDistribution != "" {
// cloudfront returns forbidden error for nonexisting data
if strings.Contains(err.Error(), "error code 403") {
return true
}
}
awsErr, ok := err.(awserr.Error)
if !ok {
return false
}
if awsErr.Code() == "NoSuchKey" {
return true
}
return false
}
type amazonWriter struct {
ctx context.Context
errChan chan error
pipe *io.PipeWriter
}
func newWriter(ctx context.Context, client *amazonClient, name string) *amazonWriter {
reader, writer := io.Pipe()
w := &amazonWriter{
ctx: ctx,
errChan: make(chan error),
pipe: writer,
}
go func() {
_, err := client.uploader.Upload(&s3manager.UploadInput{
ACL: &uploadACL,
Body: reader,
Bucket: aws.String(client.bucket),
Key: aws.String(name),
ContentEncoding: aws.String("application/octet-stream"),
})
if err != nil {
w.pipe.CloseWithError(err)
}
w.errChan <- err
}()
return w
}
func (w *amazonWriter) Write(p []byte) (int, error) {
span, _ := tracing.AddSpanToAnyExisting(w.ctx, "amazonWriter.Write")
defer tracing.FinishAnySpan(span)
return w.pipe.Write(p)
}
func (w *amazonWriter) Close() error {
span, _ := tracing.AddSpanToAnyExisting(w.ctx, "amazonWriter.Close")
defer tracing.FinishAnySpan(span)
if err := w.pipe.Close(); err != nil {
return err
}
return <-w.errChan
}
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}