Skip to content

Commit

Permalink
Merge pull request #270 from DaspawnW/master
Browse files Browse the repository at this point in the history
Fix #241 by removing x-forwarded-for header from request to aws
  • Loading branch information
mwhittington21 authored Aug 31, 2020
2 parents 472b462 + 0af1fd4 commit ccdaac8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -42,6 +43,8 @@ const (
healthcheckInterval = 30 * time.Second
)

var tokenRouteRegexp = regexp.MustCompile("^/?[^/]+/api/token$")

// Keeps track of the names of registered handlers for metric value/label initialization
var registeredHandlerNames []string

Expand Down Expand Up @@ -358,6 +361,12 @@ func (s *Server) roleHandler(logger *log.Entry, w http.ResponseWriter, r *http.R
}

func (s *Server) reverseProxyHandler(logger *log.Entry, w http.ResponseWriter, r *http.Request) {
// Remove remoteaddr to prevent issues with new IMDSv2 to fail when x-forwarded-for header is present
// for more details please see: https://github.com/aws/aws-sdk-ruby/issues/2177 https://github.com/uswitch/kiam/issues/359
if r.Method == http.MethodPut && tokenRouteRegexp.MatchString(r.URL.Path) {
r.RemoteAddr = ""
}

proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: s.MetadataAddress})
proxy.ServeHTTP(w, r)
logger.WithField("metadata.url", s.MetadataAddress).Debug("Proxy ec2 metadata request")
Expand Down

0 comments on commit ccdaac8

Please sign in to comment.