forked from livepeer/go-livepeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.go
267 lines (239 loc) · 7.12 KB
/
s3.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
package drivers
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"mime/multipart"
"net/http"
"path"
"strings"
"time"
"github.com/golang/glog"
"github.com/livepeer/go-livepeer/common"
"github.com/livepeer/go-livepeer/net"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// S3_POLICY_EXPIRE_IN_HOURS how long access rights given to other node will be valid
const S3_POLICY_EXPIRE_IN_HOURS = 24
/* S3OS S# backed object storage driver. For own storage access key and access key secret
should be specified. To give to other nodes access to own S3 storage so called 'POST' policy
is created. This policy is valid for S3_POLICY_EXPIRE_IN_HOURS hours.
*/
type s3OS struct {
host string
region string
bucket string
awsAccessKeyID string
awsSecretAccessKey string
s3svc *s3.S3
}
type s3Session struct {
host string
key string
policy string
signature string
credential string
xAmzDate string
storageType net.OSInfo_StorageType
fields map[string]string
}
// S3BUCKET s3 bucket owned by this node
var S3BUCKET string
func s3Host(bucket string) string {
return fmt.Sprintf("https://%s.s3.amazonaws.com", bucket)
}
// IsOwnStorageS3 returns true if uri points to S3 bucket owned by this node
func IsOwnStorageS3(uri string) bool {
return strings.HasPrefix(uri, s3Host(S3BUCKET))
}
func newS3Session(info *net.S3OSInfo) OSSession {
sess := &s3Session{
host: info.Host,
key: info.Key,
policy: info.Policy,
signature: info.Signature,
xAmzDate: info.XAmzDate,
credential: info.Credential,
storageType: net.OSInfo_S3,
}
sess.fields = s3GetFields(sess)
return sess
}
func NewS3Driver(region, bucket, accessKey, accessKeySecret string) OSDriver {
os := &s3OS{
host: s3Host(bucket),
region: region,
bucket: bucket,
awsAccessKeyID: accessKey,
awsSecretAccessKey: accessKeySecret,
}
if os.awsAccessKeyID != "" {
creds := credentials.NewStaticCredentials(os.awsAccessKeyID, os.awsSecretAccessKey, "")
cfg := aws.NewConfig().WithRegion(os.region).WithCredentials(creds)
os.s3svc = s3.New(session.New(), cfg)
}
return os
}
func (os *s3OS) NewSession(path string) OSSession {
policy, signature, credential, xAmzDate := createPolicy(os.awsAccessKeyID,
os.bucket, os.region, os.awsSecretAccessKey, path)
sess := &s3Session{
host: s3Host(os.bucket),
key: path,
policy: policy,
signature: signature,
credential: credential,
xAmzDate: xAmzDate,
storageType: net.OSInfo_S3,
}
sess.fields = s3GetFields(sess)
return sess
}
func s3GetFields(sess *s3Session) map[string]string {
return map[string]string{
"x-amz-algorithm": "AWS4-HMAC-SHA256",
"x-amz-credential": sess.credential,
"x-amz-date": sess.xAmzDate,
"x-amz-signature": sess.signature,
}
}
func (os *s3Session) IsExternal() bool {
return true
}
func (os *s3Session) EndSession() {
}
func (os *s3Session) SaveData(name string, data []byte) (string, error) {
// tentativeUrl just used for logging
tentativeURL := path.Join(os.host, os.key, name)
glog.V(common.VERBOSE).Infof("Saving to S3 %s", tentativeURL)
path, err := os.postData(name, data)
if err != nil {
// handle error
glog.Errorf("Save S3 error: %v", err)
return "", err
}
url := os.getAbsURL(path)
glog.V(common.VERBOSE).Infof("Saved to S3 %s", tentativeURL)
return url, err
}
func (os *s3Session) getAbsURL(path string) string {
return os.host + "/" + path
}
func (os *s3Session) GetInfo() *net.OSInfo {
oi := &net.OSInfo{
S3Info: &net.S3OSInfo{
Host: os.host,
Key: os.key,
Policy: os.policy,
Signature: os.signature,
Credential: os.credential,
XAmzDate: os.xAmzDate,
},
StorageType: os.storageType,
}
return oi
}
// if s3 storage is not our own, we are saving data into it using POST request
func (os *s3Session) postData(fileName string, buffer []byte) (string, error) {
fileBytes := bytes.NewReader(buffer)
fileType := http.DetectContentType(buffer)
path, fileName := path.Split(path.Join(os.key, fileName))
fields := map[string]string{
"acl": "public-read",
"Content-Type": fileType,
"key": path + "${filename}",
"policy": os.policy,
}
for k, v := range os.fields {
fields[k] = v
}
req, err := newfileUploadRequest(os.host, fields, fileBytes, fileName)
if err != nil {
glog.Error(err)
return "", err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
glog.Error(err)
return "", err
}
body := &bytes.Buffer{}
sz, err := body.ReadFrom(resp.Body)
if err != nil {
glog.Error(err)
return "", err
}
resp.Body.Close()
if sz > 0 {
// usually there's an error at this point, so log
glog.Error("Got response from from S3: ", body)
return "", fmt.Errorf(body.String()) // sorta bad
}
return path + fileName, err
}
func makeHmac(key []byte, data []byte) []byte {
hash := hmac.New(sha256.New, key)
hash.Write(data)
return hash.Sum(nil)
}
func signString(stringToSign, sregion, amzDate, secret string) string {
date := makeHmac([]byte("AWS4"+secret), []byte(amzDate))
region := makeHmac(date, []byte(sregion))
service := makeHmac(region, []byte("s3"))
credentials := makeHmac(service, []byte("aws4_request"))
signature := makeHmac(credentials, []byte(stringToSign))
sSignature := hex.EncodeToString(signature)
return sSignature
}
// createPolicy returns policy, signature, xAmzCredentail and xAmzDate
func createPolicy(key, bucket, region, secret, path string) (string, string, string, string) {
const timeFormat = "2006-01-02T15:04:05.999Z"
const shortTimeFormat = "20060102"
expireAt := time.Now().Add(S3_POLICY_EXPIRE_IN_HOURS * time.Hour)
expireFmt := expireAt.UTC().Format(timeFormat)
xAmzDate := time.Now().UTC().Format(shortTimeFormat)
xAmzCredential := fmt.Sprintf("%s/%s/%s/s3/aws4_request", key, xAmzDate, region)
src := fmt.Sprintf(`{ "expiration": "%s",
"conditions": [
{"bucket": "%s"},
{"acl": "public-read"},
["starts-with", "$Content-Type", ""],
["starts-with", "$key", "%s"],
{"x-amz-algorithm": "AWS4-HMAC-SHA256"},
{"x-amz-credential": "%s"},
{"x-amz-date": "%sT000000Z" }
]
}`, expireFmt, bucket, path, xAmzCredential, xAmzDate)
policy := base64.StdEncoding.EncodeToString([]byte(src))
return policy, signString(policy, region, xAmzDate, secret), xAmzCredential, xAmzDate + "T000000Z"
}
func newfileUploadRequest(uri string, params map[string]string, fData io.Reader, fileName string) (*http.Request, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for key, val := range params {
err := writer.WriteField(key, val)
if err != nil {
glog.Error(err)
}
}
part, err := writer.CreateFormFile("file", fileName)
if err != nil {
return nil, err
}
_, err = io.Copy(part, fData)
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}