Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-shell
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-shell
Commits
7f0f6e76
Commit
7f0f6e76
authored
Sep 17, 2019
by
Małgorzata Ksionek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ip address to headers
parent
659ad7f7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
0 deletions
+42
-0
go/internal/gitlabnet/client.go
go/internal/gitlabnet/client.go
+12
-0
go/internal/gitlabnet/client_test.go
go/internal/gitlabnet/client_test.go
+30
-0
No files found.
go/internal/gitlabnet/client.go
View file @
7f0f6e76
...
...
@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"
"gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
...
...
@@ -109,6 +110,9 @@ func (c *GitlabClient) DoRequest(method, path string, data interface{}) (*http.R
request
.
Header
.
Set
(
secretHeaderName
,
encodedSecret
)
request
.
Header
.
Add
(
"Content-Type"
,
"application/json"
)
ip
:=
ipAddr
()
request
.
Header
.
Add
(
"X_FORWARDED_FOR"
,
ip
)
request
.
Close
=
true
response
,
err
:=
c
.
httpClient
.
Do
(
request
)
...
...
@@ -123,6 +127,14 @@ func (c *GitlabClient) DoRequest(method, path string, data interface{}) (*http.R
return
response
,
nil
}
func
ipAddr
()
string
{
address
:=
os
.
Getenv
(
"SSH_CONNECTION"
)
if
address
!=
""
{
return
strings
.
Fields
(
address
)[
0
]
}
return
address
}
func
ParseJSON
(
hr
*
http
.
Response
,
response
interface
{})
error
{
if
err
:=
json
.
NewDecoder
(
hr
.
Body
)
.
Decode
(
response
);
err
!=
nil
{
return
ParsingError
...
...
go/internal/gitlabnet/client_test.go
View file @
7f0f6e76
...
...
@@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"testing"
...
...
@@ -50,6 +51,13 @@ func TestClients(t *testing.T) {
fmt
.
Fprint
(
w
,
r
.
Header
.
Get
(
secretHeaderName
))
},
},
{
Path
:
"/api/v4/internal/with_ip"
,
Handler
:
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
header
:=
r
.
Header
.
Get
(
"X_FORWARDED_FOR"
)
require
.
Equal
(
t
,
header
,
"127.0.0.1"
)
},
},
{
Path
:
"/api/v4/internal/error"
,
Handler
:
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
@@ -110,6 +118,7 @@ func TestClients(t *testing.T) {
testMissing
(
t
,
client
)
testErrorMessage
(
t
,
client
)
testAuthenticationHeader
(
t
,
client
)
testXForwardedForHeader
(
t
,
client
)
})
}
}
...
...
@@ -217,3 +226,24 @@ func testAuthenticationHeader(t *testing.T, client *GitlabClient) {
assert
.
Equal
(
t
,
"sssh, it's a secret"
,
string
(
header
))
})
}
func
testXForwardedForHeader
(
t
*
testing
.
T
,
client
*
GitlabClient
)
{
t
.
Run
(
"X-Forwarded-For for GET"
,
func
(
t
*
testing
.
T
)
{
os
.
Setenv
(
"SSH_CONNECTION"
,
"127.0.0.1 0"
)
response
,
err
:=
client
.
Get
(
"/with_ip"
)
defer
response
.
Body
.
Close
()
require
.
NoError
(
t
,
err
)
require
.
NotNil
(
t
,
response
)
})
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
)
defer
response
.
Body
.
Close
()
require
.
NoError
(
t
,
err
)
require
.
NotNil
(
t
,
response
)
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment