Commit 9304b1d6 authored by Małgorzata Ksionek's avatar Małgorzata Ksionek

Add cr remarks

parent 8071ea11
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
) )
...@@ -36,7 +35,6 @@ type Config struct { ...@@ -36,7 +35,6 @@ type Config struct {
Secret string `yaml:"secret"` Secret string `yaml:"secret"`
HttpSettings HttpSettingsConfig `yaml:"http_settings"` HttpSettings HttpSettingsConfig `yaml:"http_settings"`
HttpClient *HttpClient HttpClient *HttpClient
IPAddr string
} }
func New() (*Config, error) { func New() (*Config, error) {
...@@ -55,7 +53,6 @@ func NewFromDir(dir string) (*Config, error) { ...@@ -55,7 +53,6 @@ func NewFromDir(dir string) (*Config, error) {
func newFromFile(filename string) (*Config, error) { func newFromFile(filename string) (*Config, error) {
cfg := &Config{ cfg := &Config{
RootDir: path.Dir(filename), RootDir: path.Dir(filename),
IPAddr: getIPAddr(),
} }
configBytes, err := ioutil.ReadFile(filename) configBytes, err := ioutil.ReadFile(filename)
...@@ -126,11 +123,3 @@ func parseSecret(cfg *Config) error { ...@@ -126,11 +123,3 @@ func parseSecret(cfg *Config) error {
return nil return nil
} }
func getIPAddr() string {
address := os.Getenv("SSH_CONNECTION")
if address != "" {
return strings.Fields(address)[0]
}
return address
}
...@@ -2,7 +2,6 @@ package config ...@@ -2,7 +2,6 @@ package config
import ( import (
"fmt" "fmt"
"os"
"path" "path"
"testing" "testing"
...@@ -111,10 +110,3 @@ func TestParseConfig(t *testing.T) { ...@@ -111,10 +110,3 @@ 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")
}
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"strings" "strings"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/config" "gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/sshenv"
) )
const ( const (
...@@ -109,7 +110,10 @@ func (c *GitlabClient) DoRequest(method, path string, data interface{}) (*http.R ...@@ -109,7 +110,10 @@ func (c *GitlabClient) DoRequest(method, path string, data interface{}) (*http.R
request.Header.Set(secretHeaderName, encodedSecret) request.Header.Set(secretHeaderName, encodedSecret)
request.Header.Add("Content-Type", "application/json") request.Header.Add("Content-Type", "application/json")
request.Header.Add("X_FORWARDED_FOR", c.config.IPAddr) ipAddr := sshenv.LocalAddr()
if ipAddr != "" {
request.Header.Add("X-Forwarded-For", ipAddr)
}
request.Close = true request.Close = true
......
...@@ -53,7 +53,7 @@ func TestClients(t *testing.T) { ...@@ -53,7 +53,7 @@ func TestClients(t *testing.T) {
{ {
Path: "/api/v4/internal/with_ip", Path: "/api/v4/internal/with_ip",
Handler: func(w http.ResponseWriter, r *http.Request) { Handler: func(w http.ResponseWriter, r *http.Request) {
header := r.Header.Get("X_FORWARDED_FOR") header := r.Header.Get("X-Forwarded-For")
require.Equal(t, "127.0.0.1", header) require.Equal(t, "127.0.0.1", header)
}, },
}, },
...@@ -107,7 +107,6 @@ func TestClients(t *testing.T) { ...@@ -107,7 +107,6 @@ func TestClients(t *testing.T) {
tc.config.GitlabUrl = url tc.config.GitlabUrl = url
tc.config.Secret = "sssh, it's a secret" tc.config.Secret = "sssh, it's a secret"
tc.config.IPAddr = "127.0.0.1"
client, err := GetClient(tc.config) client, err := GetClient(tc.config)
require.NoError(t, err) require.NoError(t, err)
...@@ -228,6 +227,10 @@ func testAuthenticationHeader(t *testing.T, client *GitlabClient) { ...@@ -228,6 +227,10 @@ func testAuthenticationHeader(t *testing.T, client *GitlabClient) {
func testXForwardedForHeader(t *testing.T, client *GitlabClient) { func testXForwardedForHeader(t *testing.T, client *GitlabClient) {
t.Run("X-Forwarded-For for GET", func(t *testing.T) { t.Run("X-Forwarded-For for GET", func(t *testing.T) {
cleanup, err := testhelper.Setenv("SSH_CONNECTION", "127.0.0.1 0")
require.NoError(t, err)
defer cleanup()
response, err := client.Get("/with_ip") response, err := client.Get("/with_ip")
require.NoError(t, err) require.NoError(t, err)
...@@ -237,6 +240,10 @@ func testXForwardedForHeader(t *testing.T, client *GitlabClient) { ...@@ -237,6 +240,10 @@ func testXForwardedForHeader(t *testing.T, client *GitlabClient) {
t.Run("X-Forwarded-For for POST", func(t *testing.T) { t.Run("X-Forwarded-For for POST", func(t *testing.T) {
data := map[string]string{"key": "value"} data := map[string]string{"key": "value"}
cleanup, err := testhelper.Setenv("SSH_CONNECTION", "127.0.0.1 0")
require.NoError(t, err)
defer cleanup()
response, err := client.Post("/with_ip", data) response, err := client.Post("/with_ip", data)
require.NoError(t, err) require.NoError(t, err)
......
package sshenv
import (
"os"
"strings"
)
func LocalAddr() string {
address := os.Getenv("SSH_CONNECTION")
if address != "" {
return strings.Fields(address)[0]
}
return address
}
package sshenv
import (
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/testhelper"
)
func TestLocalAddr(t *testing.T) {
cleanup, err := testhelper.Setenv("SSH_CONNECTION", "127.0.0.1 0")
require.NoError(t, err)
defer cleanup()
require.Nil(t, err)
require.Equal(t, LocalAddr(), "127.0.0.1")
}
...@@ -85,3 +85,9 @@ func getTestDataDir() (string, error) { ...@@ -85,3 +85,9 @@ func getTestDataDir() (string, error) {
return path.Join(path.Dir(currentFile), "testdata"), nil return path.Join(path.Dir(currentFile), "testdata"), nil
} }
func Setenv(key, value string) (func(), error) {
oldValue := os.Getenv(key)
err := os.Setenv(key, value)
return func() { os.Setenv(key, oldValue) }, err
}
...@@ -5,7 +5,7 @@ include GoBuild ...@@ -5,7 +5,7 @@ include GoBuild
def main def main
ensure_build_dir_exists ensure_build_dir_exists
run!(GO_ENV, %w[go test ./...], chdir: GO_DIR) run!(GO_ENV, %w[go test -v ./...], chdir: GO_DIR)
puts 'OK' puts 'OK'
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment