forked from bmizerany/aws4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.go
216 lines (190 loc) · 4.71 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
206
207
208
209
210
211
212
213
214
215
216
// Package aws4 signs HTTP requests as prescribed in
// http://docs.amazonwebservices.com/general/latest/gr/signature-version-4.html
package aws4
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"sort"
"strings"
"time"
)
const iSO8601BasicFormat = "20060102T150405Z"
const iSO8601BasicFormatShort = "20060102"
var lf = []byte{'\n'}
// Keys holds a set of Amazon Security Credentials.
type Keys struct {
AccessKey string
SecretKey string
}
func (k *Keys) 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_request"))
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(keys *Keys, 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]
sv.Sign(keys, r)
return nil
}
// Sign signs an HTTP request with the given AWS keys for use on service s.
func (s *Service) Sign(keys *Keys, 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 := keys.sign(s, t)
h := hmac.New(sha256.New, k)
s.writeStringToSign(h, t, r)
auth := bytes.NewBufferString("AWS4-HMAC-SHA256 ")
auth.Write([]byte("Credential=" + keys.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) {
var b []byte
// If the payload is empty, use the empty string as the input to the SHA256 function
// http://docs.amazonwebservices.com/general/latest/gr/sigv4-create-canonical-request.html
if r.Body == nil {
b = []byte("")
} else {
var err error
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) {
path := r.URL.RequestURI()
if r.URL.RawQuery != "" {
path = path[:len(path)-len(r.URL.RawQuery)-1]
}
slash := strings.HasSuffix(path, "/")
path = filepath.Clean(path)
if path != "/" && slash {
path += "/"
}
w.Write([]byte(path))
}
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 t.Format(iSO8601BasicFormatShort) + "/" + s.Region + "/" + s.Name + "/aws4_request"
}
func ghmac(key, data []byte) []byte {
h := hmac.New(sha256.New, key)
h.Write(data)
return h.Sum(nil)
}