Skip to content

Commit

Permalink
Use application/json as content type when returning credentials
Browse files Browse the repository at this point in the history
The credentials are returned as a JSON object so update the content type
to match instead of text/plain.
  • Loading branch information
dleen committed Mar 16, 2022
1 parent 4ee3630 commit 5efe1c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pkg/server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

// InstanceMetadataMiddleware is a convenience wrapper that chains TokenMiddleware, BrowserFilterMiddleware, and AWSHeaderMiddleware
func InstanceMetadataMiddleware(next http.HandlerFunc) http.HandlerFunc {
return TokenMiddleware(TaskMetadataMiddleware(next))
return BrowserFilterMiddleware(AWSHeaderMiddleware(TokenMiddleware(next)))
}

// TaskMetadataMiddleware is a convenience wrapper that chains BrowserFilterMiddleware and AWSHeaderMiddleware
Expand All @@ -48,6 +48,8 @@ func TokenMiddleware(next http.HandlerFunc) http.HandlerFunc {
var remainingTtl int
var ok bool

w.Header().Set("Content-Type", "application/json")

token := r.Header.Get("x-aws-ec2-metadata-token")
if token != "" {
if ok, remainingTtl = session.CheckToken(token); !ok {
Expand Down
24 changes: 21 additions & 3 deletions pkg/server/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestAWSHeaderMiddleware(t *testing.T) {
w.WriteHeader(http.StatusOK)
})
t.Logf("test case: %s", description)
bfmHandler := InstanceMetadataMiddleware(nextHandler)
bfmHandler := AWSHeaderMiddleware(nextHandler)
req := httptest.NewRequest("GET", "http://localhost", nil)
rec := httptest.NewRecorder()
bfmHandler.ServeHTTP(rec, req)
Expand Down Expand Up @@ -167,9 +167,27 @@ func TestCredentialServiceMiddleware(t *testing.T) {
if server := rec.Header().Get("Server"); server != "EC2ws" {
t.Errorf("%s failed: got Server header %s, expected %s", tc.Description, server, "EC2ws")
}
if contentType := rec.Header().Get("Content-Type"); contentType != "text/plain" {
t.Errorf("%s failed: got Content-Type header %s, expected %s", tc.Description, contentType, "text/plain")
if contentType := rec.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("%s failed: got Content-Type header %s, expected %s", tc.Description, contentType, "application/json")
}
}
}
}

func TestTokenMiddleware(t *testing.T) {
description := "aws token middleware"
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
t.Logf("test case: %s", description)
bfmHandler := TokenMiddleware(nextHandler)
req := httptest.NewRequest("GET", "http://localhost", nil)
rec := httptest.NewRecorder()
bfmHandler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("%s failed: got status %d, expected %d", description, rec.Code, http.StatusOK)
}
if contentType := rec.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("%s failed: got Content-Type header %s, expected %s", description, contentType, "application/json")
}
}

0 comments on commit 5efe1c4

Please sign in to comment.