forked from sha1sum/aws_signing_client
/
client.go
162 lines (140 loc) · 4.82 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package aws_signing_client
import (
"bytes"
"context"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/aws/aws-sdk-go/private/protocol/rest"
)
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 *v4.Signer
service string
region string
logger ContextLogger
}
// ContextLogger is used for context-enabled logging.
ContextLogger interface {
Printf(ctx context.Context, format string, v ...interface{})
}
// DefaultLogger implements ContextLogger interface using log.Logger. This effectively means the context
// is ignored.
DefaultLogger struct {
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{}
)
// DefaultLogger.Printf() ignores the specified context.
func (dl *DefaultLogger) Printf(ctx context.Context, format string, v ...interface{}) {
dl.logger.Printf(format, v...)
}
// 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 *v4.Signer, client *http.Client, service string, region string, cl ContextLogger) (*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.DefaultClient
}
if cl == nil {
cl = &DefaultLogger{
logger: log.New(ioutil.Discard, "", 0),
}
}
s := &Signer{
transport: c.Transport,
v4: v4s,
service: service,
region: region,
logger: cl,
}
if s.transport == nil {
s.transport = http.DefaultTransport
}
c.Transport = s
return c, nil
}
// 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) {
ctx := req.Context()
if h, ok := req.Header["Authorization"]; ok && len(h) > 0 && strings.HasPrefix(h[0], "AWS4") {
s.logger.Printf(ctx, "Received request to sign that is already signed. Skipping.")
return s.transport.RoundTrip(req)
}
req.URL.Scheme = "https"
if strings.Contains(req.URL.RawPath, "%2C") {
s.logger.Printf(ctx, "Escaping path for URL path '%s'", req.URL.RawPath)
req.URL.RawPath = rest.EscapePath(req.URL.RawPath, false)
}
t := time.Now()
req.Header.Set("Date", t.Format(time.RFC3339))
s.logger.Printf(ctx, "Request to be signed: %+v", req)
var latency int64
var err error
switch req.Body {
case nil:
s.logger.Printf(ctx, "Signing request with no body...")
start := time.Now()
_, err = s.v4.Sign(req, nil, s.service, s.region, t)
latency = int64(time.Now().Sub(start)/time.Millisecond)
default:
d, err := ioutil.ReadAll(req.Body)
if err != nil {
s.logger.Printf(ctx, "Error while attempting to read request body: '%s'", err)
return nil, err
}
req.Body = ioutil.NopCloser(bytes.NewReader(d))
s.logger.Printf(ctx, "Signing request with body...")
start := time.Now()
_, err = s.v4.Sign(req, bytes.NewReader(d), s.service, s.region, t)
latency = int64(time.Now().Sub(start)/time.Millisecond)
}
if err != nil {
s.logger.Printf(ctx, "Error while attempting to sign request: '%s'", err)
return nil, err
}
s.logger.Printf(ctx, "Signing succesful. Latency: %d ms", latency)
start := time.Now()
resp, err := s.transport.RoundTrip(req)
latency = int64(time.Now().Sub(start)/time.Millisecond)
if err != nil {
s.logger.Printf(ctx, "Error from RoundTripper. Latency: %d ms, Error: %s", latency, err)
return resp, err
}
s.logger.Printf(ctx, "Successful response from RoundTripper. Latency: %d ms", latency)
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."
}