Skip to content

Commit

Permalink
Test if on EC2 before trying IAM role auth
Browse files Browse the repository at this point in the history
  • Loading branch information
drocamor committed Feb 18, 2014
1 parent a0ebb4b commit cc08133
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -52,7 +53,7 @@ func checkKeys() {
}
}
// if accesskey and the secretkey are blank, get the key from the role
if Keys.AccessKeyID == "" {
if Keys.AccessKeyID == "" && onEC2() {

Keys = getIAMRoleCredentials()
}
Expand All @@ -63,6 +64,18 @@ func checkKeys() {
}
}

func onEC2() bool {

c, err := net.DialTimeout("tcp", "169.254.169.254:80", time.Second)

if err != nil {
return false
} else {
c.Close()
return true
}
}

func getIAMRoleList() []string {
// Get a list of the roles that are available to this instance
url := "http://169.254.169.254/latest/meta-data/iam/security-credentials/"
Expand Down

0 comments on commit cc08133

Please sign in to comment.