-
Notifications
You must be signed in to change notification settings - Fork 34
/
sign_test.go
313 lines (301 loc) · 8.99 KB
/
sign_test.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
package s3
import (
"bytes"
"net/http"
"testing"
)
var exKeys = Keys{
AccessKey: "AKIAIOSFODNN7EXAMPLE",
SecretKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
}
// Temporary Credentials
var tokenExKeys = Keys{
AccessKey: "AKIAIOSFODNN7EXAMPLE",
SecretKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
SecurityToken: "dummy",
}
var StorageIOService = &Service{
Domain: "storage.io",
Bucket: IdentityBucket,
}
var signTest = []struct {
service *Service
method string
url string
more http.Header
expBuf string
expSig string
}{
{
DefaultService,
"GET",
"http://johnsmith.s3.amazonaws.com/photos/puppy.jpg",
http.Header{
"Date": {"Tue, 27 Mar 2007 19:36:42 +0000"},
},
"GET\n\n\nTue, 27 Mar 2007 19:36:42 +0000\n/johnsmith/photos/puppy.jpg",
"AWS AKIAIOSFODNN7EXAMPLE:bWq2s1WEIj+Ydj0vQ697zp+IXMU=",
},
{
DefaultService,
"PUT",
"http://johnsmith.s3.amazonaws.com/photos/puppy.jpg",
http.Header{
"Content-Type": {"image/jpeg"},
"Content-Length": {"94328"},
"Date": {"Tue, 27 Mar 2007 21:15:45 +0000"},
},
"PUT\n\nimage/jpeg\nTue, 27 Mar 2007 21:15:45 +0000\n/johnsmith/photos/puppy.jpg",
"AWS AKIAIOSFODNN7EXAMPLE:MyyxeRY7whkBe+bq8fHCL/2kKUg=",
},
{
DefaultService,
"GET",
"http://johnsmith.s3.amazonaws.com/?prefix=photos&max-keys=50&marker=puppy",
http.Header{
"User-Agent": {"Mozilla/5.0"},
"Date": {"Tue, 27 Mar 2007 19:42:41 +0000"},
},
"GET\n\n\nTue, 27 Mar 2007 19:42:41 +0000\n/johnsmith/",
"AWS AKIAIOSFODNN7EXAMPLE:htDYFYduRNen8P9ZfE/s9SuKy0U=",
},
{
DefaultService,
"GET",
"http://johnsmith.s3.amazonaws.com/?acl",
http.Header{
"Date": {"Tue, 27 Mar 2007 19:44:46 +0000"},
},
"GET\n\n\nTue, 27 Mar 2007 19:44:46 +0000\n/johnsmith/?acl",
"AWS AKIAIOSFODNN7EXAMPLE:c2WLPFtWHVgbEmeEG93a4cG37dM=",
},
{
DefaultService,
"DELETE",
"http://s3.amazonaws.com/johnsmith/photos/puppy.jpg",
http.Header{
"User-Agent": {"dotnet"},
"Date": {"Tue, 27 Mar 2007 21:20:27 +0000"},
"x-amz-date": {"Tue, 27 Mar 2007 21:20:26 +0000"},
},
"DELETE\n\n\n\nx-amz-date:Tue, 27 Mar 2007 21:20:26 +0000\n/johnsmith/photos/puppy.jpg",
// The expected signature in Amazon's documentation,
// "AWS AKIAIOSFODNN7EXAMPLE:9b2sXq0KfxsxHtdZkzx/9Ngqyh8=",
// appears to be incorrect.
"AWS AKIAIOSFODNN7EXAMPLE:R4dJ53KECjStyBO5iTBJZ4XVOaI=",
},
{
DefaultService,
"PUT",
"http://static.johnsmith.net:8080/db-backup.dat.gz",
http.Header{
"User-Agent": {"curl/7.15.5"},
"Date": {"Tue, 27 Mar 2007 21:06:08 +0000"},
"x-amz-acl": {"public-read"},
"content-type": {"application/x-download"},
"Content-MD5": {"4gJE4saaMU4BqNR0kLY+lw=="},
"X-Amz-Meta-ReviewedBy": {"[email protected]", "[email protected]"},
"X-Amz-Meta-FileChecksum": {"0x02661779"},
"X-Amz-Meta-ChecksumAlgorithm": {"crc32"},
"Content-Disposition": {"attachment; filename=database.dat"},
"Content-Encoding": {"gzip"},
"Content-Length": {"5913339"},
},
"PUT\n4gJE4saaMU4BqNR0kLY+lw==\napplication/x-download\nTue, 27 Mar 2007 21:06:08 +0000\nx-amz-acl:public-read\nx-amz-meta-checksumalgorithm:crc32\nx-amz-meta-filechecksum:0x02661779\nx-amz-meta-reviewedby:[email protected],[email protected]\n/static.johnsmith.net/db-backup.dat.gz",
"AWS AKIAIOSFODNN7EXAMPLE:ilyl83RwaSoYIEdixDQcA4OnAnc=",
},
{
DefaultService,
"GET",
"http://s3.amazonaws.com/",
http.Header{
"Date": {"Wed, 28 Mar 2007 01:29:59 +0000"},
},
"GET\n\n\nWed, 28 Mar 2007 01:29:59 +0000\n/",
"AWS AKIAIOSFODNN7EXAMPLE:qGdzdERIC03wnaRNKh6OqZehG9s=",
},
{
DefaultService,
"GET",
// I've altered this example from the one documented by Amazon
// since package http never produces lower-case %-encodings.
"http://s3.amazonaws.com/dictionary/fran%C3%A7ais/pr%C3%A9f%C3%A8re",
http.Header{
"Date": {"Wed, 28 Mar 2007 01:49:49 +0000"},
},
"GET\n\n\nWed, 28 Mar 2007 01:49:49 +0000\n/dictionary/fran%C3%A7ais/pr%C3%A9f%C3%A8re",
"AWS AKIAIOSFODNN7EXAMPLE:81VEw/Bc3GDt/k65Xrrk3AdfI4c=",
},
{
DefaultService,
"POST",
// ?delete is required in CanonicalizedResource for:
// http://docs.amazonwebservices.com/AmazonS3/latest/API/multiobjectdeleteapi.html
"http://bucketname.S3.amazonaws.com/?delete",
http.Header{
"x-amz-date": {"Wed, 30 Nov 2011 03:39:05 GMT"},
"Content-MD5": {"p5/WA/oEr30qrEEl21PAqw=="},
"Content-Length": {"125"},
},
"POST\np5/WA/oEr30qrEEl21PAqw==\n\n\nx-amz-date:Wed, 30 Nov 2011 03:39:05 GMT\n/bucketname/?delete",
// Doesn't match the example in the Amazon docs
"AWS AKIAIOSFODNN7EXAMPLE:DXGmXMY+1QnRGC7vicUqu1gTmK4=",
},
{
DefaultService,
"POST",
// object?restore is required in CanonicalizedResource for:
// http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOSTrestore.html
"http://bucketname.S3.amazonaws.com/photo1.jpg?restore",
http.Header{
"x-amz-date": {"Mon, 22 Oct 2012 01:49:52 GMT"},
"Content-MD5": {"p5/WA/oEr30qrEEl21PAqw=="},
"Content-Length": {"125"},
},
"POST\np5/WA/oEr30qrEEl21PAqw==\n\n\nx-amz-date:Mon, 22 Oct 2012 01:49:52 GMT\n/bucketname/photo1.jpg?restore",
"AWS AKIAIOSFODNN7EXAMPLE:Yuldf0uSoOutFCGMkKYQzxVYeDk=",
},
{
StorageIOService,
"GET",
"http://bucket.storage.io/ubuntu-12.04.2-server-amd64.iso",
http.Header{
"Date": {"Tue, 27 Mar 2007 19:36:42 +0000"},
},
"GET\n\n\nTue, 27 Mar 2007 19:36:42 +0000\n/bucket/ubuntu-12.04.2-server-amd64.iso",
"AWS AKIAIOSFODNN7EXAMPLE:eLAv1CJmnBwV4DXj2z508eunQQs=",
},
{
StorageIOService,
"GET",
"http://storage.io/bucket/ubuntu-12.04.2-server-amd64.iso",
http.Header{
"Date": {"Tue, 27 Mar 2007 19:36:42 +0000"},
},
"GET\n\n\nTue, 27 Mar 2007 19:36:42 +0000\n/bucket/ubuntu-12.04.2-server-amd64.iso",
"AWS AKIAIOSFODNN7EXAMPLE:eLAv1CJmnBwV4DXj2z508eunQQs=",
},
{
DefaultService,
"PUT",
"http://static.johnsmith.net:8080/db-backup.dat.gz",
http.Header{
"Date": {"Tue, 27 Mar 2007 21:06:08 +0000"},
"X-Amz-Copy-Source": {"other_object"},
"X-Amz-Copy-Source-Range": {"bytes=0-960463"},
},
"PUT\n\n\nTue, 27 Mar 2007 21:06:08 +0000\nx-amz-copy-source:other_object\nx-amz-copy-source-range:bytes=0-960463\n/static.johnsmith.net/db-backup.dat.gz",
"AWS AKIAIOSFODNN7EXAMPLE:rH0p5yotiFsFs8BnYl9cS/MBXvk=",
},
}
func TestSign(t *testing.T) {
for _, ts := range signTest {
r, err := http.NewRequest(ts.method, ts.url, nil)
if err != nil {
panic(err)
}
for k, vs := range ts.more {
for _, v := range vs {
r.Header.Add(k, v)
}
}
var buf bytes.Buffer
ts.service.writeSigData(&buf, r)
if buf.String() != ts.expBuf {
t.Errorf("in %s:", r.Method)
t.Logf("url %s", r.URL.String())
t.Logf("exp %q", ts.expBuf)
t.Logf("got %q", buf.String())
}
ts.service.Sign(r, exKeys)
if got := r.Header.Get("Authorization"); got != ts.expSig {
t.Errorf("in %s:", r.Method)
t.Logf("url %s", r.URL.String())
t.Logf("exp %q", ts.expSig)
t.Logf("got %q", got)
}
// Reset Auth header and test signing with temporary credentials
r.Header.Del("Authorization")
ts.service.Sign(r, tokenExKeys)
if got := r.Header.Get("X-Amz-Security-Token"); got != tokenExKeys.SecurityToken {
t.Errorf("in %s:", r.Method)
t.Logf("url %s", r.URL.String())
t.Logf("exp %q", tokenExKeys.SecurityToken)
t.Logf("got %q", got)
}
}
}
var bucketTest = []struct {
url string
svc *Service
w string
}{
{
"http://johnsmith.s3.amazonaws.com/photos/puppy.jpg",
&Service{Domain: "amazonaws.com"},
"/johnsmith",
},
{
"http://johnsmith.s3-ap-northeast-1.amazonaws.com/photos/puppy.jpg",
&Service{Domain: "amazonaws.com"},
"/johnsmith",
},
{
"http://johnsmith.s3.amazonaws.com/?prefix=photos&max-keys=50&marker=puppy",
&Service{Domain: "amazonaws.com"},
"/johnsmith",
},
{
"http://johnsmith.s3.amazonaws.com/?acl",
&Service{Domain: "amazonaws.com"},
"/johnsmith",
},
{
"http://s3.amazonaws.com/johnsmith/photos/puppy.jpg",
&Service{Domain: "amazonaws.com"},
"",
},
{
"http://static.johnsmith.net:8080/db-backup.dat.gz",
&Service{Domain: "amazonaws.com"},
"/static.johnsmith.net",
},
{
"http://s3.amazonaws.com/",
&Service{Domain: "amazonaws.com"},
"",
},
{
"http://s3.amazonaws.com/dictionary/fran%C3%A7ais/pr%C3%A9f%C3%A8re",
&Service{Domain: "amazonaws.com"},
"",
},
{
"http://bucketname.s3.amazonaws.com/?delete",
&Service{Domain: "amazonaws.com"},
"/bucketname",
},
{
"http://johnsmith.storage.io/photos/puppy.jpg",
&Service{Domain: "storage.io", Bucket: IdentityBucket},
"/johnsmith",
},
{
"http://static.johnsmith.net:8080/db-backup.dat.gz",
&Service{Domain: "storage.io", Bucket: IdentityBucket},
"/static.johnsmith.net",
},
}
func TestVhostBucket(t *testing.T) {
for i, ts := range bucketTest {
r, err := http.NewRequest("GET", ts.url, nil)
if err != nil {
panic(err)
}
var g bytes.Buffer
ts.svc.writeVhostBucket(&g, r.Host)
if g.String() != ts.w {
t.Errorf("test %d: want %q, got %q", i, ts.w, g.String())
}
}
}