-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawsrequest.go
623 lines (547 loc) · 20.6 KB
/
awsrequest.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* Copyright (c) 2014, fromkeith
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* * Neither the name of the fromkeith nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package awsgo
import (
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/pmylund/sortutil"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"syscall"
"time"
)
const (
RequestSigningType_AWS4 = 1
RequestSigningType_REST = 2
RequestSigningType_AWS2 = 3
RequestSigningType_AWS3 = 4
)
var (
defaultRequestClient *http.Client = http.DefaultClient
firstRequestCreate sync.Mutex
)
type RequestError struct {
BaseError error
Location string
Action string
}
func (r RequestError) Error() string {
return fmt.Sprintf("%s: %s: %v", r.Location, r.Action, r.BaseError)
}
// Base of a request. Used across all requests.
type RequestBuilder struct {
// The Host we are hitting
Host AwsHost `json:"-"`
// The Credentials to use
Key Credentials `json:"-"`
// Any custom headers
Headers map[string]string `json:"-"`
// The method we are using GET, PUT, POST, ...
RequestMethod string `json:"-"`
// The uri we are hitting.
CanonicalUri string `json:"-"`
// The http client to use. A default one will be used if not specified
HttpClient *http.Client `json:"-"`
}
// Implemented by each AWS request
// Provides some standard steps to doing a request, and handling its response.
type RequestBuilderInterface interface {
// verify the request before we send it
VerifyInput() error
// Get the underlying RequestBuilder in the struct
GetRequestBuilder() *RequestBuilder
// Unmarshal the response
DeMarshalResponse(response []byte, headers map[string]string, statusCode int) (interface{})
}
type AwsRequest struct {
Host AwsHost
Date time.Time
Headers map[string]string
Payload string
PayloadReader io.ReadCloser
Key Credentials
RequestMethod string
CanonicalUri string
RequestSigningType int
HttpClient *http.Client
// generated
signature string
scope string
payloadHash []byte
}
func (r *RequestBuilder) GetRequestBuilder() *RequestBuilder {
return r
}
// verify the RequestBuilder base has what is required.
func verifyInput(r *RequestBuilder) (error) {
if len(r.Host.Domain) == 0 {
r.Host.Domain = "amazonaws.com"
}
// if we haven't set the keys, try getting the default ones...
if r.Key.AccessKeyId == "" {
var err error
r.Key, err = GetSecurityKeys()
if err != nil {
return err
}
}
if len(r.Key.AccessKeyId) == 0 {
return Verification_Error_AccessKeyEmpty
}
if len(r.Key.SecretAccessKey) == 0 {
return Verification_Error_SecretAccessKeyEmpty
}
return nil
}
// Start creating the request.
// Take the base information, and the data we are going to transport.
func createAwsRequest(rb *RequestBuilder, marsh interface{}) (request AwsRequest) {
request.Host = rb.Host
request.Key = rb.Key
request.Headers = rb.Headers
request.RequestMethod = rb.RequestMethod
request.CanonicalUri = rb.CanonicalUri
request.HttpClient = rb.HttpClient
request.Date = time.Now()
if r, ok := marsh.(io.ReadCloser); ok {
request.PayloadReader = r
} else if marsh != nil {
pay, _ := json.Marshal(marsh)
request.Payload = string(pay)
if request.Headers == nil {
request.Headers = make(map[string]string)
}
if _, ok := request.Headers["Content-Type"]; !ok {
request.Headers["Content-Type"] = "application/x-amz-json-1.0"
}
request.Headers["Content-Length"] = fmt.Sprintf("%d", len(request.Payload))
}
return
}
// Given the RequestBuilderInterface this will verify the underlying request
// and then create a new AwsRequest instance.
// returned Request is only valid if error is not nill
func NewAwsRequest(rb RequestBuilderInterface, marsh interface{}) (request AwsRequest, verifyError error) {
verifyError = rb.VerifyInput()
if verifyError != nil {
return
}
verifyError = verifyInput(rb.GetRequestBuilder())
if verifyError != nil {
return
}
request = createAwsRequest(rb.GetRequestBuilder(), marsh)
return
}
// Perform the actual request, calling the demarshall on the RequestBuilderInterface
// returns the result of the Demarshall, or other errors.
func (request AwsRequest) DoAndDemarshall(rb RequestBuilderInterface) (interface{}, error) {
responseIo, responseHeaders, statusCode, err := request.Do()
if err != nil {
return nil, err
}
defer responseIo.Close()
var responseContent []byte
buf := bytes.Buffer{}
if _, err = io.Copy(&buf, responseIo); err != nil {
// i think i'm seeing some connection reset by peer errors here, but i'm not 100% sure
// best i could find was the suggestion that the body was closed by the server before
// we could read it, and that this might happen on bad request.
if statusCode >= 200 && statusCode < 300 {
return nil, RequestError{
BaseError: err,
Location: "Aws.Request.DoAndDemarshall",
Action: "io.Copy",
}
}
}
responseContent = []byte(buf.String())
//fmt.Println("responseContent")
val := rb.DeMarshalResponse(responseContent, responseHeaders, statusCode)
if t, ok := val.(error); ok {
return nil, t
}
return val, nil
}
// Performs the actual request
// Returns:
// io.ReaderCloser - the unclosed response of the request
// map[string]string - the headers in the response
// int - that status code the response
// error - any errors that occured
func (req AwsRequest) Do() (io.ReadCloser, map[string]string, int, error) {
// add the required headers
req.Headers["Host"] = strings.ToLower(req.Host.ToString())
req.Headers["user-agent"] = "go-aws-client-0.1"
if _, ok := req.Headers["x-amz-date"]; !ok {
req.Headers["x-amz-date"] = IsoDate(req.Date)
}
if req.Key.token != "" {
req.Headers["x-amz-security-token"] = req.Key.token
}
if err := signRequest(&req); err != nil {
return nil, nil, 0, err
}
// create headers for the actual request
reqHeaders := http.Header{}
for k, v := range req.Headers {
reqHeaders.Add(k, v)
}
url_, err := getUrl(req)
if err != nil {
return nil, nil, 0, err
}
//fmt.Println("Request url: ", url_.String())
// the base request
hreq := http.Request {
URL: url_,
Method: req.RequestMethod,
ProtoMajor: 1,
ProtoMinor: 1,
Close: true, // until this is fixed (https://code.google.com/p/go/issues/detail?id=4677) we want to Close.
Header: reqHeaders,
}
httpClient := req.HttpClient
if httpClient == nil {
httpClient = defaultRequestClient
}
if val, ok := req.Headers["Content-Length"]; ok {
hreq.ContentLength, _ = strconv.ParseInt(val, 10, 64)
}
// we can only retry if we can reset Body..
canRetry := true
if req.PayloadReader != nil {
// can't retry here, as we can't reset req.PayloadReader
hreq.Body = req.PayloadReader
canRetry = false
}
var resp *http.Response
for try := 0; ; try ++ {
// for payloads we know are static, we can reset them for each try.
if req.Payload != "" {
hreq.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(req.Payload)))
}
resp, err = httpClient.Do(&hreq)
if err != nil {
if try > 5 {
return nil, nil, 0, RequestError{
BaseError: err,
Location: "Aws.Request.Do (Exceeded tries)",
Action: "httpClient.Do",
}
}
// if a temporary error, and we can retry, then do.
if netErr, ok := err.(net.Error); ok && canRetry {
if netErr.Temporary() {
time.Sleep(time.Duration(100 * try * try) * time.Millisecond)
continue
}
}
// try mimic the fix in https://code.google.com/p/go/issues/detail?id=6163
if sysError, ok := err.(syscall.Errno); ok && canRetry {
if sysError == syscall.ECONNRESET || sysError == syscall.ECONNABORTED {
time.Sleep(time.Duration(100 * try * try) * time.Millisecond)
continue
}
}
return nil, nil, 0, RequestError{
BaseError: err,
Location: "Aws.Request.Do (Non net, non temp)",
Action: "httpClient.Do",
}
}
if try > 5 || !canRetry {
break
}
// retry on 500s, if possible
if resp.StatusCode == 500 {
resp.Body.Close()
time.Sleep(time.Duration(100 * try * try) * time.Millisecond)
continue
}
break
}
responseHeaders := make(map[string]string)
for k, v := range resp.Header {
responseHeaders[strings.ToLower(k)] = strings.Join(v, ";")
}
return resp.Body, responseHeaders, resp.StatusCode, nil
}
func SetDefaultHttpClient(client *http.Client) {
defaultRequestClient = client
}
// helper function to create a http client that accepts certain certs
func CreateCertApprovedClient(certsToAdd []*x509.Certificate) (*http.Client) {
// add in any custom certs they want us to use
var rootCA *x509.CertPool
if len(certsToAdd) > 0 {
rootCA = x509.NewCertPool()
for i := range certsToAdd {
rootCA.AddCert(certsToAdd[i])
}
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs : rootCA,
},
DisableKeepAlives: true,
}
requestClient := &http.Client{Transport: tr}
return requestClient
}
func getUrl(req AwsRequest) (*url.URL, error) {
// create the url string. Not all aws request have regions.
var urlStr string
if req.Host.Override != "" {
urlStr = fmt.Sprintf("https://%s%s", req.Host.Override, req.CanonicalUri)
} else if req.Host.Region == "" {
urlStr = fmt.Sprintf("https://%s.%s%s", req.Host.Service, req.Host.Domain, req.CanonicalUri)
} else {
urlStr = fmt.Sprintf("https://%s.%s.%s%s", req.Host.Service, req.Host.Region, req.Host.Domain, req.CanonicalUri)
}
return url.Parse(urlStr)
}
func signRequest(req *AwsRequest) error {
if req.RequestSigningType == RequestSigningType_AWS4 {
req.createSignature()
} else if req.RequestSigningType == RequestSigningType_REST {
req.createRestSignature()
} else if req.RequestSigningType == RequestSigningType_AWS2 {
req.createV2Signature()
} else if req.RequestSigningType == RequestSigningType_AWS3 {
req.createSignatureAws3()
} else {
return errors.New("Invalid request signing type")
}
return nil
}
// http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
func (req * AwsRequest) createRestSignature() {
payloadHash := ""
md5Hasher := md5.New()
if req.Payload != "" {
md5Hasher.Write([]byte(req.Payload)) // TODO: check return code?
payloadHash = string(md5Hasher.Sum(nil))
}
canonicalHeaders, _ := req.createCanonicalHeaders("x-amz-", false)
canonicalResource := fmt.Sprintf("%s", req.CanonicalUri)
stringToSign := fmt.Sprintf("%s\n%s\n%s\n%s\n%s%s",
req.RequestMethod, payloadHash, req.Headers["Content-Type"], "" /*old school date*/,
canonicalHeaders, canonicalResource)
hmacHasher := hmac.New(createHMacHasher1, []byte(req.Key.SecretAccessKey))
hmacHasher.Write([]byte(stringToSign))
signature := base64.StdEncoding.EncodeToString(hmacHasher.Sum(nil))
authorization := fmt.Sprintf("AWS %s:%s", req.Key.AccessKeyId, signature)
req.Headers["Authorization"] = authorization
}
func (req * AwsRequest) createCanonicalHeaders(prefixReq string, includeHost bool) (canonicalHeaders string, signedHeaders string) {
mapKeys := make([]string, len(req.Headers))
i := 0
for k := range req.Headers {
mapKeys[i] = k
i++
}
sortutil.CiAsc(mapKeys)
canonicalHeaders = ""
signedHeaders = ""
for i := range mapKeys {
if prefixReq != "" {
if !strings.HasPrefix(strings.ToLower(mapKeys[i]), prefixReq) {
if !includeHost || strings.ToLower(mapKeys[i]) != "host" {
continue
}
}
}
if len(signedHeaders) > 0 {
signedHeaders = fmt.Sprintf("%s;%s", signedHeaders, strings.ToLower(mapKeys[i]))
canonicalHeaders = fmt.Sprintf("%s%s:%s\n", canonicalHeaders, strings.ToLower(mapKeys[i]), req.Headers[mapKeys[i]])
} else {
signedHeaders = fmt.Sprintf("%s", strings.ToLower(mapKeys[i]))
canonicalHeaders = fmt.Sprintf("%s:%s\n", strings.ToLower(mapKeys[i]), req.Headers[mapKeys[i]])
}
}
return
}
//http://docs.aws.amazon.com/general/latest/gr/signature-version-2.html
func (req * AwsRequest) createV2Signature() {
canonicalQueryString := ""
if !strings.Contains(req.CanonicalUri, "?") {
req.CanonicalUri = req.CanonicalUri + "?"
} else {
req.CanonicalUri = req.CanonicalUri + "&"
}
now := time.Now()
req.CanonicalUri = fmt.Sprintf("%sAWSAccessKeyId=%s&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=%s",
req.CanonicalUri,
Escape(req.Key.AccessKeyId),
Escape(now.Format(time.RFC3339)),
)
if req.Key.token != "" {
req.CanonicalUri = fmt.Sprintf("%s&SecurityToken=%s",
req.CanonicalUri,
Escape(req.Key.token),
)
}
urlSplit := strings.Split(req.CanonicalUri, "?")
if len(urlSplit) != 2 {
panic(fmt.Sprintf("CanonicalUri does not have 2 parts. Got: %s", req.CanonicalUri))
}
fixedUrl := urlSplit[0]
sp := strings.Split(urlSplit[1], "&")
sortutil.Asc(sp)
for i := range sp {
if len(canonicalQueryString) > 0 {
unEscaped := strings.Replace(sp[i], "+", "%20", -1)
canonicalQueryString = fmt.Sprintf("%s&%s", canonicalQueryString, unEscaped)
} else {
canonicalQueryString = sp[i]
}
}
toSign := fmt.Sprintf("%s\n%s\n%s\n%s",
req.RequestMethod,
req.Host.ToString(),
fixedUrl,
canonicalQueryString)
hmacHasher := hmac.New(createHMacHasher256, []byte(req.Key.SecretAccessKey))
hmacHasher.Write([]byte(toSign))
sig := hmacHasher.Sum(nil)
req.CanonicalUri = fmt.Sprintf("%s&Signature=%s",
req.CanonicalUri,
Escape(base64.StdEncoding.EncodeToString(sig)),
)
}
// http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
func (req * AwsRequest) createSignature() {
hasher := sha256.New()
hasher.Write([]byte(req.Payload)) // TODO: check return code?
req.payloadHash = hasher.Sum(nil)
// Stupid Amazon. Why have the service name and the url not match???
fixedService := req.Host.Service
if req.Host.Service == "email" {
fixedService = "ses"
}
req.scope = fmt.Sprintf("%s/%s/%s/aws4_request", simpleDate(req.Date), req.Host.Region, fixedService)
canonicalQueryString := ""
fixedUrl := req.CanonicalUri
if strings.Contains(req.CanonicalUri, "?") {
urlSplit := strings.Split(req.CanonicalUri, "?")
fixedUrl = urlSplit[0]
sp := strings.Split(urlSplit[1], "&")
sortutil.CiAsc(sp)
for i := range sp {
if len(canonicalQueryString) > 0 {
unEscaped := strings.Replace(sp[i], "+", "%20", -1)
canonicalQueryString = fmt.Sprintf("%s&%s", canonicalQueryString, unEscaped)
} else {
canonicalQueryString = sp[i]
}
}
}
req.Headers["x-amz-content-sha256"] = fmt.Sprintf("%x", req.payloadHash)
canonicalHeaders, signedHeaders := req.createCanonicalHeaders("", false)
canonicalReq := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%x",
req.RequestMethod, fixedUrl, canonicalQueryString, canonicalHeaders, signedHeaders, req.payloadHash)
hasher.Reset()
hasher.Write([]byte(canonicalReq)) // TODO: check return code?
hashedCanonicalReq := hasher.Sum(nil)
stringToSign := fmt.Sprintf("AWS4-HMAC-SHA256\n%s\n%s\n%x",
IsoDate(req.Date), req.scope, hashedCanonicalReq)
//fmt.Println("Canon", canonicalReq)
//fmt.Println("String To Sign:", stringToSign)
hasher.Reset()
hmacHasher := hmac.New(createHMacHasher256, []byte(fmt.Sprintf("AWS4%s", req.Key.SecretAccessKey)))
hmacHasher.Write([]byte(simpleDate(req.Date)))
hmacDate := hmacHasher.Sum(nil)
hmacHasher = hmac.New(createHMacHasher256, hmacDate)
hmacHasher.Write([]byte(req.Host.Region))
hmacRegion := hmacHasher.Sum(nil)
hmacHasher = hmac.New(createHMacHasher256, hmacRegion)
hmacHasher.Write([]byte(fixedService))
hmacService := hmacHasher.Sum(nil)
hmacHasher = hmac.New(createHMacHasher256, hmacService)
hmacHasher.Write([]byte("aws4_request"))
signingKey := hmacHasher.Sum(nil)
hmacHasher = hmac.New(createHMacHasher256, signingKey)
hmacHasher.Write([]byte(stringToSign))
req.signature = fmt.Sprintf("%x", hmacHasher.Sum(nil))
req.Headers["Authorization"] =
fmt.Sprintf("AWS4-HMAC-SHA256 Credential=%s/%s/%s/%s/aws4_request, SignedHeaders=%s, Signature=%s",
req.Key.AccessKeyId, simpleDate(req.Date), req.Host.Region, fixedService, signedHeaders, req.signature)
}
// http://docs.aws.amazon.com/amazonswf/latest/developerguide/HMACAuth-swf.html
func (req * AwsRequest) createSignatureAws3() {
req.Headers["x-amz-date"] = req.Date.Format(time.RFC1123)
canonicalHeaders, signedHeaders := req.createCanonicalHeaders("x-amz-", true)
canonicalQueryString := ""
fixedUrl := req.CanonicalUri
if strings.Contains(req.CanonicalUri, "?") {
urlSplit := strings.Split(req.CanonicalUri, "?")
fixedUrl = urlSplit[0]
sp := strings.Split(urlSplit[1], "&")
sortutil.CiAsc(sp)
for i := range sp {
if len(canonicalQueryString) > 0 {
unEscaped := strings.Replace(sp[i], "+", "%20", -1)
canonicalQueryString = fmt.Sprintf("%s&%s", canonicalQueryString, unEscaped)
} else {
canonicalQueryString = sp[i]
}
}
}
stringToSign := fmt.Sprintf("%s\n%s\n%s\n%s\n%s",
req.RequestMethod,
fixedUrl,
canonicalQueryString,
canonicalHeaders,
req.Payload,
)
hasher := sha256.New()
hasher.Write([]byte(stringToSign))
sumToSign := hasher.Sum(nil)
hmacHasher := hmac.New(sha256.New, []byte(req.Key.SecretAccessKey))
hmacHasher.Write(sumToSign)
signature := base64.StdEncoding.EncodeToString(hmacHasher.Sum(nil))
authorization := fmt.Sprintf("AWS3 AWSAccessKeyId=%s,Algorithm=HmacSHA256,SignedHeaders=%s,Signature=%s", req.Key.AccessKeyId, signedHeaders, signature)
req.Headers["X-Amzn-Authorization"] = authorization
}