From cc081335f9f9c713ae2f26dee8c383313f6c7a74 Mon Sep 17 00:00:00 2001 From: David Rocamora Date: Tue, 18 Feb 2014 12:08:29 -0500 Subject: [PATCH] Test if on EC2 before trying IAM role auth --- common.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/common.go b/common.go index 2233210..7723478 100644 --- a/common.go +++ b/common.go @@ -11,6 +11,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "net" "net/http" "net/url" "os" @@ -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() } @@ -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/"