forked from smarty-archives/go-aws-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsauth_test.go
215 lines (184 loc) · 6.37 KB
/
awsauth_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
package awsauth
import (
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"testing"
"time"
"github.com/smartystreets/assertions"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestIntegrationFixture(t *testing.T) {
if !credentialsSet() {
t.Skip("Required credentials absent from environment.")
}
gunit.RunSequential(new(IntegrationFixture), t)
}
type IntegrationFixture struct {
*gunit.Fixture
}
func (this *IntegrationFixture) assertOK(response *http.Response) {
if !this.So(response.StatusCode, should.Equal, http.StatusOK) {
message, _ := ioutil.ReadAll(response.Body)
this.Error(string(message))
}
}
func (this *IntegrationFixture) LongTestSign4_IAM_OutOfOrderQueryString() {
request := newRequest("GET", "https://iam.amazonaws.com/?Version=2010-05-08&Action=ListRoles", nil)
response := sign4AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSign4_S3() {
request, _ := http.NewRequest("GET", "https://s3.amazonaws.com", nil)
response := sign4AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSign2_EC2() {
request := newRequest("GET", "https://ec2.amazonaws.com/?Version=2013-10-15&Action=DescribeInstances", nil)
response := sign2AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSign4_SQS() {
request := newRequest("POST", "https://sqs.us-west-2.amazonaws.com", url.Values{"Action": []string{"ListQueues"}})
response := sign4AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSign3_SES() {
request := newRequest("GET", "https://email.us-east-1.amazonaws.com/?Action=GetSendStatistics", nil)
response := sign3AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSign3_Route53() {
request := newRequest("GET", "https://route53.amazonaws.com/2013-04-01/hostedzone?maxitems=1", nil)
response := sign3AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSign2_SimpleDB() {
request := newRequest("GET", "https://sdb.amazonaws.com/?Action=ListDomains&Version=2009-04-15", nil)
response := sign2AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSignS3Url() {
s3res := os.Getenv("S3Resource")
if s3res == "" {
return
}
request, _ := http.NewRequest("GET", s3res, nil)
response := signS3UrlAndDo(request)
this.assertOK(response)
}
func TestSign_Version2(t *testing.T) {
requests := []*http.Request{
newRequest("GET", "https://ec2.amazonaws.com", url.Values{}),
newRequest("GET", "https://elasticache.amazonaws.com/", url.Values{}),
}
for _, request := range requests {
signed := Sign(request)
assertions.New(t).So(signed.URL.Query().Get("SignatureVersion"), should.Equal, "2")
}
}
func TestSign_Version3(t *testing.T) {
requests := []*http.Request{
newRequest("GET", "https://route53.amazonaws.com", url.Values{}),
newRequest("GET", "https://email.us-east-1.amazonaws.com/", url.Values{}),
}
for _, request := range requests {
signed := Sign(request)
assertions.New(t).So(signed.Header.Get("X-Amzn-Authorization"), should.NotBeBlank)
}
}
func TestSign_Version4(t *testing.T) {
requests := []*http.Request{
newRequest("POST", "https://sqs.amazonaws.com/", url.Values{}),
newRequest("GET", "https://iam.amazonaws.com", url.Values{}),
newRequest("GET", "https://s3.amazonaws.com", url.Values{}),
}
for _, request := range requests {
signed := Sign(request)
assertions.New(t).So(signed.Header.Get("Authorization"), should.ContainSubstring, ", Signature=")
}
}
func TestSign_ExistingCredentials_Version2(t *testing.T) {
requests := []*http.Request{
newRequest("GET", "https://ec2.amazonaws.com", url.Values{}),
newRequest("GET", "https://elasticache.amazonaws.com/", url.Values{}),
}
for _, request := range requests {
signed := Sign(request, newKeys())
assertions.New(t).So(signed.URL.Query().Get("SignatureVersion"), should.Equal, "2")
}
}
func TestSign_ExistingCredentials_Version3(t *testing.T) {
requests := []*http.Request{
newRequest("GET", "https://route53.amazonaws.com", url.Values{}),
newRequest("GET", "https://email.us-east-1.amazonaws.com/", url.Values{}),
}
for _, request := range requests {
signed := Sign(request, newKeys())
assertions.New(t).So(signed.Header.Get("X-Amzn-Authorization"), should.NotBeBlank)
}
}
func TestSign_ExistingCredentials_Version4(t *testing.T) {
requests := []*http.Request{
newRequest("POST", "https://sqs.amazonaws.com/", url.Values{}),
newRequest("GET", "https://iam.amazonaws.com", url.Values{}),
newRequest("GET", "https://s3.amazonaws.com", url.Values{}),
}
for _, request := range requests {
signed := Sign(request, newKeys())
assertions.New(t).So(signed.Header.Get("Authorization"), should.ContainSubstring, ", Signature=")
}
}
func TestExpiration(t *testing.T) {
assert := assertions.New(t)
var credentials = &Credentials{}
// Credentials without an expiration can't expire
assert.So(credentials.expired(), should.BeFalse)
// Credentials that expire in 5 minutes aren't expired
credentials.Expiration = time.Now().Add(5 * time.Minute)
assert.So(credentials.expired(), should.BeFalse)
// Credentials that expire in 1 minute are expired
credentials.Expiration = time.Now().Add(1 * time.Minute)
assert.So(credentials.expired(), should.BeTrue)
// Credentials that expired 2 hours ago are expired
credentials.Expiration = time.Now().Add(-2 * time.Hour)
assert.So(credentials.expired(), should.BeTrue)
}
func credentialsSet() bool {
var keys Credentials
keys = newKeys()
return keys.AccessKeyID != ""
}
func newRequest(method string, url string, v url.Values) *http.Request {
request, _ := http.NewRequest(method, url, strings.NewReader(v.Encode()))
return request
}
func sign2AndDo(request *http.Request) *http.Response {
Sign2(request)
response, _ := client.Do(request)
return response
}
func sign3AndDo(request *http.Request) *http.Response {
Sign3(request)
response, _ := client.Do(request)
return response
}
func sign4AndDo(request *http.Request) *http.Response {
Sign4(request)
response, _ := client.Do(request)
return response
}
func signS3AndDo(request *http.Request) *http.Response {
SignS3(request)
response, _ := client.Do(request)
return response
}
func signS3UrlAndDo(request *http.Request) *http.Response {
SignS3Url(request, time.Now().AddDate(0, 0, 1))
response, _ := client.Do(request)
return response
}
var client = &http.Client{}