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
|
package awsauth
import (
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping long-running integration test.")
}
Convey("Given real credentials from environment variables", t, func() {
Convey("A request (with out-of-order query string) with to IAM should succeed (assuming Administrator Access policy)", func() {
request := newRequest("GET", "https://iam.amazonaws.com/?Version=2010-05-08&Action=ListRoles", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign4AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to S3 should succeed", func() {
request, _ := http.NewRequest("GET", "https://s3.amazonaws.com", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign4AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to EC2 should succeed", func() {
request := newRequest("GET", "https://ec2.amazonaws.com/?Version=2013-10-15&Action=DescribeInstances", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign2AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to SQS should succeed", func() {
request := newRequest("POST", "https://sqs.us-west-2.amazonaws.com", url.Values{
"Action": []string{"ListQueues"},
})
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign4AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to SES should succeed", func() {
request := newRequest("GET", "https://email.us-east-1.amazonaws.com/?Action=GetSendStatistics", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign3AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to Route 53 should succeed", func() {
request := newRequest("GET", "https://route53.amazonaws.com/2013-04-01/hostedzone?maxitems=1", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign3AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to SimpleDB should succeed", func() {
request := newRequest("GET", "https://sdb.amazonaws.com/?Action=ListDomains&Version=2009-04-15", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign2AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("If S3Resource env variable is set", func() {
s3res := os.Getenv("S3Resource")
Convey("A URL-signed request to that S3 resource should succeed", func() {
request, _ := http.NewRequest("GET", s3res, nil)
if !credentialsSet() || s3res == "" {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := signS3UrlAndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
})
})
}
func TestSign(t *testing.T) {
Convey("Requests to services using Version 2 should be signed accordingly", t, func() {
reqs := []*http.Request{
newRequest("GET", "https://ec2.amazonaws.com", url.Values{}),
newRequest("GET", "https://elasticache.amazonaws.com/", url.Values{}),
}
for _, request := range reqs {
signedReq := Sign(request)
So(signedReq.URL.Query().Get("SignatureVersion"), ShouldEqual, "2")
}
})
Convey("Requests to services using Version 3 should be signed accordingly", t, func() {
reqs := []*http.Request{
newRequest("GET", "https://route53.amazonaws.com", url.Values{}),
newRequest("GET", "https://email.us-east-1.amazonaws.com/", url.Values{}),
}
for _, request := range reqs {
signedReq := Sign(request)
So(signedReq.Header.Get("X-Amzn-Authorization"), ShouldNotBeBlank)
}
})
Convey("Requests to services using Version 4 should be signed accordingly", t, func() {
reqs := []*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 reqs {
signedReq := Sign(request)
So(signedReq.Header.Get("Authorization"), ShouldContainSubstring, ", Signature=")
}
})
var keys Credentials
keys = newKeys()
Convey("Requests to services using existing credentials Version 2 should be signed accordingly", t, func() {
reqs := []*http.Request{
newRequest("GET", "https://ec2.amazonaws.com", url.Values{}),
newRequest("GET", "https://elasticache.amazonaws.com/", url.Values{}),
}
for _, request := range reqs {
signedReq := Sign(request, keys)
So(signedReq.URL.Query().Get("SignatureVersion"), ShouldEqual, "2")
}
})
Convey("Requests to services using existing credentials Version 3 should be signed accordingly", t, func() {
reqs := []*http.Request{
newRequest("GET", "https://route53.amazonaws.com", url.Values{}),
newRequest("GET", "https://email.us-east-1.amazonaws.com/", url.Values{}),
}
for _, request := range reqs {
signedReq := Sign(request, keys)
So(signedReq.Header.Get("X-Amzn-Authorization"), ShouldNotBeBlank)
}
})
Convey("Requests to services using existing credentials Version 4 should be signed accordingly", t, func() {
reqs := []*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 reqs {
signedReq := Sign(request, keys)
So(signedReq.Header.Get("Authorization"), ShouldContainSubstring, ", Signature=")
}
})
}
func TestExpiration(t *testing.T) {
var credentials = &Credentials{}
Convey("Credentials without an expiration can't expire", t, func() {
So(credentials.expired(), ShouldBeFalse)
})
Convey("Credentials that expire in 5 minutes aren't expired", t, func() {
credentials.Expiration = time.Now().Add(5 * time.Minute)
So(credentials.expired(), ShouldBeFalse)
})
Convey("Credentials that expire in 1 minute are expired", t, func() {
credentials.Expiration = time.Now().Add(1 * time.Minute)
So(credentials.expired(), ShouldBeTrue)
})
Convey("Credentials that expired 2 hours ago are expired", t, func() {
credentials.Expiration = time.Now().Add(-2 * time.Hour)
So(credentials.expired(), ShouldBeTrue)
})
}
func credentialsSet() bool {
var keys Credentials
keys = newKeys()
if keys.AccessKeyID == "" {
return false
} else {
return true
}
}
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{}
|