Commit 8071ea11 authored by Małgorzata Ksionek's avatar Małgorzata Ksionek

Add cr remarks

parent 216bfafb
......@@ -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]
......
......@@ -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")
}
......@@ -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
......
......@@ -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)
......
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