forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazon_client.go
251 lines (231 loc) · 6.55 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
package obj
import (
"bytes"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"net/http"
"strings"
"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/cenkalti/backoff"
log "github.com/sirupsen/logrus"
)
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
}
func newAmazonClient(bucket string, cloudfrontDistribution string, id string, secret string, token string, region string) (*amazonClient, error) {
config := &aws.Config{
Region: aws.String(region),
}
if id != "" {
config.Credentials = credentials.NewStaticCredentials(id, secret, token)
}
session := session.New(config)
var signer *sign.URLSigner
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
}
signer = sign.NewURLSigner(cloudfrontKeyPairID, cloudfrontPrivateKey)
log.Infof("Using cloudfront security credentials - keypair ID (%v) - to sign cloudfront URLs", string(cloudfrontKeyPairID))
}
return &amazonClient{
bucket: bucket,
cloudfrontDistribution: cloudfrontDistribution,
cloudfrontURLSigner: signer,
s3: s3.New(session),
uploader: s3manager.NewUploader(session),
}, nil
}
func (c *amazonClient) Writer(name string) (io.WriteCloser, error) {
return newBackoffWriteCloser(c, newWriter(c, name)), nil
}
func (c *amazonClient) Walk(name string, fn func(name string) error) error {
var fnErr error
if err := c.s3.ListObjectsPages(
&s3.ListObjectsInput{
Bucket: aws.String(c.bucket),
Prefix: aws.String(name),
},
func(listObjectsOutput *s3.ListObjectsOutput, lastPage bool) bool {
for _, object := range listObjectsOutput.Contents {
if err := fn(*object.Key); err != nil {
fnErr = err
return false
}
}
return true
},
); err != nil {
return err
}
return fnErr
}
func (c *amazonClient) Reader(name string, offset uint64, size uint64) (io.ReadCloser, error) {
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 {
resp, connErr = http.DefaultClient.Do(req)
if connErr != nil && isNetRetryable(connErr) {
return connErr
}
return nil
}, backoff.NewExponentialBackOff(), func(err error, d time.Duration) {
log.Infof("Error connecting to (%v); retrying in %s: %#v", url, d, err)
})
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 {
getObjectOutput, err := c.s3.GetObject(&s3.GetObjectInput{
Bucket: aws.String(c.bucket),
Key: aws.String(name),
Range: aws.String(byteRange),
})
if err != nil {
return nil, err
}
reader = getObjectOutput.Body
}
return newBackoffReadCloser(c, reader), nil
}
func (c *amazonClient) Delete(name string) error {
_, err := c.s3.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(c.bucket),
Key: aws.String(name),
})
return err
}
func (c *amazonClient) Exists(name string) bool {
_, 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
}
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 {
errChan chan error
pipe *io.PipeWriter
}
func newWriter(client *amazonClient, name string) *amazonWriter {
reader, writer := io.Pipe()
w := &amazonWriter{
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"),
})
w.errChan <- err
}()
return w
}
func (w *amazonWriter) Write(p []byte) (int, error) {
return w.pipe.Write(p)
}
func (w *amazonWriter) Close() error {
if err := w.pipe.Close(); err != nil {
return err
}
return <-w.errChan
}