forked from sendgridlabs/go-kinesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.go
205 lines (179 loc) · 4.37 KB
/
sign.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
package kinesis
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"sort"
"strings"
"time"
)
const (
iSO8601BasicFormat = "20060102T150405Z"
iSO8601BasicFormatShort = "20060102"
AWS4_URL = "aws4_request"
)
var lf = []byte{'\n'}
// Sign API request by
// http://docs.amazonwebservices.com/general/latest/gr/signature-version-4.html
func (k *Auth) sign(s *Service, t time.Time) []byte {
h := ghmac([]byte("AWS4"+k.SecretKey), []byte(t.Format(iSO8601BasicFormatShort)))
h = ghmac(h, []byte(s.Region))
h = ghmac(h, []byte(s.Name))
h = ghmac(h, []byte(AWS4_URL))
return h
}
// Service represents an AWS-compatible service.
type Service struct {
// Name is the name of the service being used (i.e. iam, etc)
Name string
// Region is the region you want to communicate with the service through. (i.e. us-east-1)
Region string
}
// Sign signs a request with a Service derived from r.Host
func Sign(authKeys *Auth, r *http.Request) error {
parts := strings.Split(r.Host, ".")
if len(parts) < 4 {
return fmt.Errorf("Invalid AWS Endpoint: %s", r.Host)
}
sv := new(Service)
sv.Name = parts[0]
sv.Region = parts[1]
return sv.Sign(authKeys, r)
}
// Sign signs an HTTP request with the given AWS keys for use on service s.
func (s *Service) Sign(authKeys *Auth, r *http.Request) error {
date := r.Header.Get("Date")
t := time.Now().UTC()
if date != "" {
var err error
t, err = time.Parse(http.TimeFormat, date)
if err != nil {
return err
}
}
r.Header.Set("Date", t.Format(iSO8601BasicFormat))
k := authKeys.sign(s, t)
h := hmac.New(sha256.New, k)
s.writeStringToSign(h, t, r)
auth := bytes.NewBufferString("AWS4-HMAC-SHA256 ")
auth.Write([]byte("Credential=" + authKeys.AccessKey + "/" + s.creds(t)))
auth.Write([]byte{',', ' '})
auth.Write([]byte("SignedHeaders="))
s.writeHeaderList(auth, r)
auth.Write([]byte{',', ' '})
auth.Write([]byte("Signature=" + fmt.Sprintf("%x", h.Sum(nil))))
r.Header.Set("Authorization", auth.String())
return nil
}
func (s *Service) writeQuery(w io.Writer, r *http.Request) {
var a []string
for k, vs := range r.URL.Query() {
k = url.QueryEscape(k)
for _, v := range vs {
if v == "" {
a = append(a, k)
} else {
v = url.QueryEscape(v)
a = append(a, k+"="+v)
}
}
}
sort.Strings(a)
for i, s := range a {
if i > 0 {
w.Write([]byte{'&'})
}
w.Write([]byte(s))
}
}
func (s *Service) writeHeader(w io.Writer, r *http.Request) {
i, a := 0, make([]string, len(r.Header))
for k, v := range r.Header {
sort.Strings(v)
a[i] = strings.ToLower(k) + ":" + strings.Join(v, ",")
i++
}
sort.Strings(a)
for i, s := range a {
if i > 0 {
w.Write(lf)
}
io.WriteString(w, s)
}
}
func (s *Service) writeHeaderList(w io.Writer, r *http.Request) {
i, a := 0, make([]string, len(r.Header))
for k, _ := range r.Header {
a[i] = strings.ToLower(k)
i++
}
sort.Strings(a)
for i, s := range a {
if i > 0 {
w.Write([]byte{';'})
}
w.Write([]byte(s))
}
}
func (s *Service) writeBody(w io.Writer, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(b))
h := sha256.New()
h.Write(b)
fmt.Fprintf(w, "%x", h.Sum(nil))
}
func (s *Service) writeURI(w io.Writer, r *http.Request) {
ruri := r.URL.RequestURI()
if r.URL.RawQuery != "" {
ruri = ruri[:len(ruri)-len(r.URL.RawQuery)-1]
}
slash := strings.HasSuffix(ruri, "/")
ruri = path.Clean(ruri)
if ruri != "/" && slash {
ruri += "/"
}
w.Write([]byte(ruri))
}
func (s *Service) writeRequest(w io.Writer, r *http.Request) {
r.Header.Set("host", r.Host)
w.Write([]byte(r.Method))
w.Write(lf)
s.writeURI(w, r)
w.Write(lf)
s.writeQuery(w, r)
w.Write(lf)
s.writeHeader(w, r)
w.Write(lf)
w.Write(lf)
s.writeHeaderList(w, r)
w.Write(lf)
s.writeBody(w, r)
}
func (s *Service) writeStringToSign(w io.Writer, t time.Time, r *http.Request) {
w.Write([]byte("AWS4-HMAC-SHA256"))
w.Write(lf)
w.Write([]byte(t.Format(iSO8601BasicFormat)))
w.Write(lf)
w.Write([]byte(s.creds(t)))
w.Write(lf)
h := sha256.New()
s.writeRequest(h, r)
fmt.Fprintf(w, "%x", h.Sum(nil))
}
func (s *Service) creds(t time.Time) string {
return fmt.Sprintf("%s/%s/%s/%s", t.Format(iSO8601BasicFormatShort), s.Region, s.Name, AWS4_URL)
}
func ghmac(key, data []byte) []byte {
h := hmac.New(sha256.New, key)
h.Write(data)
return h.Sum(nil)
}