diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2020-01-28 17:48:10 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2020-01-28 17:48:10 +0300 |
commit | 5624b616b1aff4fd027607e37ca989817459126c (patch) | |
tree | f10e0ca3358868c957a910f1746ac157f0422712 | |
parent | 0509d6b4aa276d0a61592f5ad9d464f13c8e458f (diff) |
Add X-Forwarded headers to serverless proxy
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | internal/serving/serverless/director.go | 5 | ||||
-rw-r--r-- | internal/serving/serverless/serverless_test.go | 3 |
4 files changed, 10 insertions, 1 deletions
@@ -20,6 +20,7 @@ require ( github.com/rs/cors v1.7.0 github.com/sirupsen/logrus v1.4.2 github.com/stretchr/testify v1.4.0 + github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad gitlab.com/gitlab-org/labkit v0.0.0-20190902063225-3253d7975ca7 gitlab.com/lupine/go-mimedb v0.0.0-20180307000149-e8af1d659877 @@ -127,6 +127,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= +github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc= +github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g= github.com/uber/jaeger-client-go v2.15.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= github.com/uber/jaeger-lib v1.5.0/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U= diff --git a/internal/serving/serverless/director.go b/internal/serving/serverless/director.go index 83d792c3..0d046af9 100644 --- a/internal/serving/serverless/director.go +++ b/internal/serving/serverless/director.go @@ -3,6 +3,8 @@ package serverless import ( "net/http" "strings" + + "github.com/tomasen/realip" ) // NewDirectorFunc returns a director function capable of configuring a proxy @@ -13,6 +15,7 @@ func NewDirectorFunc(cluster Cluster) func(*http.Request) { request.URL.Host = strings.Join([]string{cluster.Address, cluster.Port}, ":") request.URL.Scheme = "https" request.Header.Set("User-Agent", "GitLab Pages Daemon") - request.Header.Set("X-Forwarded-For", "123") // TODO + request.Header.Set("X-Forwarded-For", realip.FromRequest(request)) + request.Header.Set("X-Forwarded-Proto", "https") } } diff --git a/internal/serving/serverless/serverless_test.go b/internal/serving/serverless/serverless_test.go index 169f3de5..fc604608 100644 --- a/internal/serving/serverless/serverless_test.go +++ b/internal/serving/serverless/serverless_test.go @@ -20,6 +20,8 @@ func TestServeFileHTTP(t *testing.T) { cluster := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "GitLab Pages Daemon", r.Header.Get("User-Agent")) + assert.Equal(t, "https", r.Header.Get("X-Forwarded-Proto")) + assert.Contains(t, r.Header.Get("X-Forwarded-For"), "127.0.0.123") })) cluster.TLS = &tls.Config{ @@ -43,6 +45,7 @@ func TestServeFileHTTP(t *testing.T) { t.Run("when proxying simple request to a cluster", func(t *testing.T) { writer := httptest.NewRecorder() request := httptest.NewRequest("GET", "http://example.gitlab.com", nil) + request.Header.Set("X-Real-IP", "127.0.0.123") handler := serving.Handler{Writer: writer, Request: request} assert.True(t, serverless.ServeFileHTTP(handler)) |