Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not ignore error when Instance Metadata service doesn't exist #1682

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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