diff options
-rw-r--r-- | go/internal/config/config.go | 8 | ||||
-rw-r--r-- | go/internal/config/config_test.go | 8 | ||||
-rw-r--r-- | go/internal/gitlabnet/client.go | 2 | ||||
-rw-r--r-- | go/internal/gitlabnet/client_test.go | 7 |
4 files changed, 16 insertions, 9 deletions
diff --git a/go/internal/config/config.go b/go/internal/config/config.go index 53de6f2..36a25c4 100644 --- a/go/internal/config/config.go +++ b/go/internal/config/config.go @@ -36,6 +36,7 @@ type Config struct { Secret string `yaml:"secret"` HttpSettings HttpSettingsConfig `yaml:"http_settings"` HttpClient *HttpClient + IPAddr string } func New() (*Config, error) { @@ -52,7 +53,10 @@ func NewFromDir(dir string) (*Config, error) { } func newFromFile(filename string) (*Config, error) { - cfg := &Config{RootDir: path.Dir(filename)} + cfg := &Config{ + RootDir: path.Dir(filename), + IPAddr: getIPAddr(), + } configBytes, err := ioutil.ReadFile(filename) if err != nil { @@ -123,7 +127,7 @@ func parseSecret(cfg *Config) error { return nil } -func (c *Config) IpAddr() string { +func getIPAddr() string { address := os.Getenv("SSH_CONNECTION") if address != "" { return strings.Fields(address)[0] diff --git a/go/internal/config/config_test.go b/go/internal/config/config_test.go index e31ff70..1dfee28 100644 --- a/go/internal/config/config_test.go +++ b/go/internal/config/config_test.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "os" "path" "testing" @@ -110,3 +111,10 @@ func TestParseConfig(t *testing.T) { }) } } + +func TestGetIPAddr(t *testing.T) { + err := os.Setenv("SSH_CONNECTION", "127.0.0.1 0") + + require.Nil(t, err) + require.Equal(t, getIPAddr(), "127.0.0.1") +} diff --git a/go/internal/gitlabnet/client.go b/go/internal/gitlabnet/client.go index b668fbd..3aa30ad 100644 --- a/go/internal/gitlabnet/client.go +++ b/go/internal/gitlabnet/client.go @@ -109,7 +109,7 @@ func (c *GitlabClient) DoRequest(method, path string, data interface{}) (*http.R request.Header.Set(secretHeaderName, encodedSecret) request.Header.Add("Content-Type", "application/json") - request.Header.Add("X_FORWARDED_FOR", c.config.IpAddr()) + request.Header.Add("X_FORWARDED_FOR", c.config.IPAddr) request.Close = true diff --git a/go/internal/gitlabnet/client_test.go b/go/internal/gitlabnet/client_test.go index 07ac7b8..3f25d3c 100644 --- a/go/internal/gitlabnet/client_test.go +++ b/go/internal/gitlabnet/client_test.go @@ -6,7 +6,6 @@ import ( "fmt" "io/ioutil" "net/http" - "os" "path" "testing" @@ -108,7 +107,7 @@ func TestClients(t *testing.T) { tc.config.GitlabUrl = url tc.config.Secret = "sssh, it's a secret" - + tc.config.IPAddr = "127.0.0.1" client, err := GetClient(tc.config) require.NoError(t, err) @@ -229,9 +228,6 @@ func testAuthenticationHeader(t *testing.T, client *GitlabClient) { func testXForwardedForHeader(t *testing.T, client *GitlabClient) { t.Run("X-Forwarded-For for GET", func(t *testing.T) { - err := os.Setenv("SSH_CONNECTION", "127.0.0.1 0") - require.Nil(t, err) - response, err := client.Get("/with_ip") require.NoError(t, err) @@ -241,7 +237,6 @@ func testXForwardedForHeader(t *testing.T, client *GitlabClient) { t.Run("X-Forwarded-For for POST", func(t *testing.T) { data := map[string]string{"key": "value"} - os.Setenv("SSH_CONNECTION", "127.0.0.1 0") response, err := client.Post("/with_ip", data) require.NoError(t, err) |