Skip to content

Commit

Permalink
updating test and gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
drocamor committed Feb 18, 2014
1 parent 6ae075f commit 8d0cb12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
25 changes: 14 additions & 11 deletions awsauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
. "github.com/smartystreets/goconvey/convey"
"net/http"
"net/url"
//"os"
"strings"
"testing"
)
Expand All @@ -15,7 +14,7 @@ func TestIntegration(t *testing.T) {
req := newRequest("GET", "https://iam.amazonaws.com/?Version=2010-05-08&Action=ListRoles", url.Values{})
resp := sign4AndDo(req)

if !envCredentialsSet() {
if !credentialsSet() {
SkipSo(resp.StatusCode, ShouldEqual, http.StatusOK)
} else {
So(resp.StatusCode, ShouldEqual, http.StatusOK)
Expand All @@ -26,7 +25,7 @@ func TestIntegration(t *testing.T) {
req, _ := http.NewRequest("GET", "https://s3.amazonaws.com", nil)
resp := signS3AndDo(req)

if !envCredentialsSet() {
if !credentialsSet() {
SkipSo(resp.StatusCode, ShouldEqual, http.StatusOK)
} else {
So(resp.StatusCode, ShouldEqual, http.StatusOK)
Expand All @@ -39,7 +38,7 @@ func TestIntegration(t *testing.T) {
})
resp := sign2AndDo(req)

if !envCredentialsSet() {
if !credentialsSet() {
SkipSo(resp.StatusCode, ShouldEqual, http.StatusOK)
} else {
So(resp.StatusCode, ShouldEqual, http.StatusOK)
Expand All @@ -52,7 +51,7 @@ func TestIntegration(t *testing.T) {
})
resp := sign4AndDo(req)

if !envCredentialsSet() {
if !credentialsSet() {
SkipSo(resp.StatusCode, ShouldEqual, http.StatusOK)
} else {
So(resp.StatusCode, ShouldEqual, http.StatusOK)
Expand All @@ -63,7 +62,7 @@ func TestIntegration(t *testing.T) {
req := newRequest("GET", "https://email.us-east-1.amazonaws.com/?Action=GetSendStatistics", url.Values{})
resp := sign3AndDo(req)

if !envCredentialsSet() {
if !credentialsSet() {
SkipSo(resp.StatusCode, ShouldEqual, http.StatusOK)
} else {
So(resp.StatusCode, ShouldEqual, http.StatusOK)
Expand All @@ -74,7 +73,7 @@ func TestIntegration(t *testing.T) {
req := newRequest("GET", "https://route53.amazonaws.com/2013-04-01/hostedzone?maxitems=1", url.Values{})
resp := sign3AndDo(req)

if !envCredentialsSet() {
if !credentialsSet() {
SkipSo(resp.StatusCode, ShouldEqual, http.StatusOK)
} else {
So(resp.StatusCode, ShouldEqual, http.StatusOK)
Expand All @@ -85,7 +84,7 @@ func TestIntegration(t *testing.T) {
req := newRequest("GET", "https://sdb.amazonaws.com/?Action=ListDomains&Version=2009-04-15", url.Values{})
resp := sign2AndDo(req)

if !envCredentialsSet() {
if !credentialsSet() {
SkipSo(resp.StatusCode, ShouldEqual, http.StatusOK)
} else {
So(resp.StatusCode, ShouldEqual, http.StatusOK)
Expand Down Expand Up @@ -137,9 +136,13 @@ func TestSign(t *testing.T) {
})
}

func envCredentialsSet() bool {
// return os.Getenv(envAccessKeyID) != "" && os.Getenv(envSecretAccessKey) != ""
return true
func credentialsSet() bool {
checkKeys()
if Keys.AccessKeyID == "" {
return false
} else {
return true
}
}

func newRequest(method string, url string, v url.Values) *http.Request {
Expand Down
18 changes: 7 additions & 11 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
"encoding/json"
)

func serviceAndRegion(host string) (string, string) {
Expand Down Expand Up @@ -48,19 +48,18 @@ func checkKeys() {
os.Getenv(envAccessKeyID),
os.Getenv(envSecretAccessKey),
os.Getenv(envSecurityToken),

}
}
// if accesskey and the secretkey are blank, get the key from the role
if Keys.AccessKeyID == "" {

Keys = getIAMRoleCredentials()
}
// if the expiration is set and it's less than 5 minutes in the future, get a new key
}

func getIAMRoleCredentials() *Credentials {

// Hack city!!

// Get a list of the roles that are available to this instance
Expand All @@ -72,8 +71,6 @@ func getIAMRoleCredentials() *Credentials {
buf.ReadFrom(resp.Body)
role := buf.String()



// append the url to get the url to the role
var buffer bytes.Buffer
buffer.WriteString(url)
Expand All @@ -83,18 +80,17 @@ func getIAMRoleCredentials() *Credentials {
// Get the role

rolereq, _ := http.NewRequest("GET", roleurl, nil)



roleresp, _ := client.Do(rolereq)
rolebuf := new(bytes.Buffer)
rolebuf.ReadFrom(roleresp.Body)

creds := Credentials{}

_ = json.Unmarshal(rolebuf.Bytes(), &creds)

return &creds

}

func augmentRequestQuery(req *http.Request, values url.Values) *http.Request {
Expand Down

0 comments on commit 8d0cb12

Please sign in to comment.