Skip to content

Commit

Permalink
Do not ignore error when Instance Metadata service doesn't exist (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
wlan0 authored Aug 4, 2022
1 parent a8bc847 commit fe4dc65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
5 changes: 4 additions & 1 deletion pkg/credentials/iam_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,10 @@ func getCredentials(client *http.Client, endpoint string) (ec2RoleCredRespBody,
}

// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html
token, _ := fetchIMDSToken(client, endpoint)
token, err := fetchIMDSToken(client, endpoint)
if err != nil {
return ec2RoleCredRespBody{}, err
}

// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html
u, err := getIAMRoleURL(endpoint)
Expand Down
34 changes: 10 additions & 24 deletions pkg/credentials/iam_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,8 @@ func initTestServerNoRoles() *httptest.Server {
return server
}

func initTestServer(expireOn string, failAssume bool) *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/latest/meta-data/iam/security-credentials/" {
fmt.Fprintln(w, "RoleName")
} else if r.URL.Path == "/latest/meta-data/iam/security-credentials/RoleName" {
if failAssume {
fmt.Fprint(w, credsFailRespTmpl)
} else {
fmt.Fprintf(w, credsRespTmpl, expireOn)
}
} else {
http.Error(w, "bad request", http.StatusBadRequest)
}
}))

return server
}

// Instance Metadata Service with V1 disabled.
func initIMDSv2Server(expireOn string) *httptest.Server {
func initIMDSv2Server(expireOn string, failAssume bool) *httptest.Server {
imdsToken := "IMDSTokenabc123=="
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path)
Expand All @@ -133,7 +115,11 @@ func initIMDSv2Server(expireOn string) *httptest.Server {
if r.URL.Path == "/latest/meta-data/iam/security-credentials/" {
fmt.Fprintln(w, "RoleName")
} else if r.URL.Path == "/latest/meta-data/iam/security-credentials/RoleName" {
fmt.Fprintf(w, credsRespTmpl, expireOn)
if failAssume {
fmt.Fprint(w, credsFailRespTmpl)
} else {
fmt.Fprintf(w, credsRespTmpl, expireOn)
}
} else {
http.Error(w, "bad request", http.StatusBadRequest)
}
Expand Down Expand Up @@ -203,7 +189,7 @@ func TestIAMNoRoles(t *testing.T) {
}

func TestIAM(t *testing.T) {
server := initTestServer("2014-12-16T01:51:37Z", false)
server := initIMDSv2Server("2014-12-16T01:51:37Z", false)
defer server.Close()

p := &IAM{
Expand Down Expand Up @@ -234,7 +220,7 @@ func TestIAM(t *testing.T) {
}

func TestIAMFailAssume(t *testing.T) {
server := initTestServer("2014-12-16T01:51:37Z", true)
server := initIMDSv2Server("2014-12-16T01:51:37Z", true)
defer server.Close()

p := &IAM{
Expand All @@ -252,7 +238,7 @@ func TestIAMFailAssume(t *testing.T) {
}

func TestIAMIsExpired(t *testing.T) {
server := initTestServer("2014-12-16T01:51:37Z", false)
server := initIMDSv2Server("2014-12-16T01:51:37Z", false)
defer server.Close()

p := &IAM{
Expand Down Expand Up @@ -429,7 +415,7 @@ func TestStsCn(t *testing.T) {
}

func TestIMDSv1Blocked(t *testing.T) {
server := initIMDSv2Server("2014-12-16T01:51:37Z")
server := initIMDSv2Server("2014-12-16T01:51:37Z", false)
p := &IAM{
Client: http.DefaultClient,
Endpoint: server.URL,
Expand Down

0 comments on commit fe4dc65

Please sign in to comment.