-
Notifications
You must be signed in to change notification settings - Fork 14
/
client.go
143 lines (130 loc) · 4.4 KB
/
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
package aws_signing_client
import (
"bytes"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
type AWSSigner interface {
Sign(r *http.Request, body io.ReadSeeker, service, region string, signTime time.Time) (http.Header, error)
}
type (
// Signer implements the http.RoundTripper interface and houses an optional RoundTripper that will be called between
// signing and response.
Signer struct {
transport http.RoundTripper
v4 AWSSigner
service string
region string
logger *log.Logger
}
// MissingSignerError is an implementation of the error interface that indicates that no AWS v4.Signer was
// provided in order to create a client.
MissingSignerError struct{}
// MissingServiceError is an implementation of the error interface that indicates that no AWS service was
// provided in order to create a client.
MissingServiceError struct{}
// MissingRegionError is an implementation of the error interface that indicates that no AWS region was
// provided in order to create a client.
MissingRegionError struct{}
)
var signer *Signer
// New obtains an HTTP client with a RoundTripper that signs AWS requests for the provided service. An
// existing client can be specified for the `client` value, or--if nil--a new HTTP client will be created.
func New(v4s AWSSigner, client *http.Client, service string, region string) (*http.Client, error) {
c := client
switch {
case v4s == nil:
return nil, MissingSignerError{}
case service == "":
return nil, MissingServiceError{}
case region == "":
return nil, MissingRegionError{}
case c == nil:
c = &http.Client{}
}
s := &Signer{
transport: c.Transport,
v4: v4s,
service: service,
region: region,
logger: log.New(ioutil.Discard, "", 0),
}
if s.transport == nil {
s.transport = http.DefaultTransport
}
signer = s
c.Transport = s
return c, nil
}
// SetDebugLog sets a logger for use in debugging requests and responses.
func SetDebugLog(l *log.Logger) {
signer.logger = l
}
// RoundTrip implements the http.RoundTripper interface and is used to wrap HTTP requests in order to sign them for AWS
// API calls. The scheme for all requests will be changed to HTTPS.
func (s *Signer) RoundTrip(req *http.Request) (*http.Response, error) {
if h, ok := req.Header["Authorization"]; ok && len(h) > 0 && strings.HasPrefix(h[0], "AWS4") {
s.logger.Println("Received request to sign that is already signed. Skipping.")
return s.transport.RoundTrip(req)
}
s.logger.Printf("Receiving request for signing: %+v", req)
req.URL.Scheme = "https"
if strings.Contains(req.URL.RawPath, "%2C") {
s.logger.Printf("Escaping path for URL path '%s'", req.URL.RawPath)
req.URL.RawPath = escapePath(req.URL.RawPath, false)
}
t := time.Now()
req.Header.Set("Date", t.Format(time.RFC3339))
s.logger.Printf("Final request to be signed: %+v", req)
var err error
switch req.Body {
case nil:
s.logger.Println("Signing request with no body...")
_, err = s.v4.Sign(req, nil, s.service, s.region, t)
default:
d, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
req.Body = ioutil.NopCloser(bytes.NewReader(d))
s.logger.Println("Signing request with body...")
_, err = s.v4.Sign(req, bytes.NewReader(d), s.service, s.region, t)
}
if err != nil {
s.logger.Printf("Error while attempting to sign request: '%s'", err)
return nil, err
}
s.logger.Printf("Signing succesful. Set header to: '%+v'", req.Header)
s.logger.Printf("Sending signed request to RoundTripper: %+v", req)
resp, err := s.transport.RoundTrip(req)
if err != nil {
s.logger.Printf("Error from RoundTripper.\n\n\tResponse: %+v\n\n\tError: '%s'", resp, err)
return resp, err
}
respBody := "<nil>"
if resp.Body != nil {
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
respBody = buf.String()
resp.Body = ioutil.NopCloser(bytes.NewReader(buf.Bytes()))
}
s.logger.Printf("Successful response from RoundTripper: %+v\n\nBody: '%s'\n", resp, respBody)
return resp, nil
}
// Error implements the error interface.
func (err MissingSignerError) Error() string {
return "No signer was provided. Cannot create client."
}
// Error implements the error interface.
func (err MissingServiceError) Error() string {
return "No AWS service abbreviation was provided. Cannot create client."
}
// Error implements the error interface.
func (err MissingRegionError) Error() string {
return "No AWS region was provided. Cannot create client."
}