As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Commit 230e9e96 authored by Ash McKenzie's avatar Ash McKenzie
Browse files

Merge branch 'proxy_ip_allowed' into 'main'

Restrict IP access for PROXY protocol

Closes #577

See merge request https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/693



Merged-by: default avatarAsh McKenzie <amckenzie@gitlab.com>
Approved-by: default avatarAlejandro Rodríguez <alejandro@gitlab.com>
Co-authored-by: default avatarJames Fargher <jfargher@gitlab.com>
parents 83032619 344ada05
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -477,6 +477,7 @@ func TestGitUploadArchiveSuccess(t *testing.T) {
require.NoError(t, err)
_, err = fmt.Fprintln(stdin, "0012argument HEAD\n0000")
require.NoError(t, err)
line, err := reader.ReadString('\n')
require.Equal(t, "0008ACK\n", line)
Loading
Loading
@@ -489,5 +490,6 @@ func TestGitUploadArchiveSuccess(t *testing.T) {
output, err := io.ReadAll(stdout)
require.NoError(t, err)
t.Logf("output: %q", output)
require.Equal(t, []byte("0000"), output[len(output)-4:])
}
Loading
Loading
@@ -72,6 +72,10 @@ sshd:
# Proxy protocol policy ("use", "require", "reject", "ignore"), "use" is the default value
# Values: https://github.com/pires/go-proxyproto/blob/195fedcfbfc1be163f3a0d507fac1709e9d81fed/policy.go#L20
proxy_policy: "use"
# Proxy allowed IP addresses. Takes precedent over proxy_policy. Disabled by default.
# proxy_allowed:
# - "192.168.0.1"
# - "192.168.1.0/24"
# Address which the server listens on HTTP for monitoring/health checks. Defaults to localhost:9122.
web_listen: "localhost:9122"
# Maximum number of concurrent sessions allowed on a single SSH connection. Defaults to 10.
Loading
Loading
Loading
Loading
@@ -27,6 +27,7 @@ type ServerConfig struct {
Listen string `yaml:"listen,omitempty"`
ProxyProtocol bool `yaml:"proxy_protocol,omitempty"`
ProxyPolicy string `yaml:"proxy_policy,omitempty"`
ProxyAllowed []string `yaml:"proxy_allowed,omitempty"`
WebListen string `yaml:"web_listen,omitempty"`
ConcurrentSessionsLimit int64 `yaml:"concurrent_sessions_limit,omitempty"`
ClientAliveInterval YamlDuration `yaml:"client_alive_interval,omitempty"`
Loading
Loading
Loading
Loading
@@ -9,7 +9,7 @@ import (
"sync"
"time"
"github.com/pires/go-proxyproto"
proxyproto "github.com/pires/go-proxyproto"
"golang.org/x/crypto/ssh"
"gitlab.com/gitlab-org/gitlab-shell/v14/client"
Loading
Loading
@@ -95,9 +95,14 @@ func (s *Server) listen(ctx context.Context) error {
}
if s.Config.Server.ProxyProtocol {
policy, err := s.proxyPolicy()
if err != nil {
return fmt.Errorf("invalid policy configuration: %w", err)
}
sshListener = &proxyproto.Listener{
Listener: sshListener,
Policy: s.requirePolicy,
Policy: policy,
ReadHeaderTimeout: time.Duration(s.Config.Server.ProxyHeaderTimeout),
}
Loading
Loading
@@ -200,17 +205,27 @@ func (s *Server) handleConn(ctx context.Context, nconn net.Conn) {
})
}
func (s *Server) requirePolicy(_ net.Addr) (proxyproto.Policy, error) {
func (s *Server) proxyPolicy() (proxyproto.PolicyFunc, error) {
if len(s.Config.Server.ProxyAllowed) > 0 {
return proxyproto.StrictWhiteListPolicy(s.Config.Server.ProxyAllowed)
}
// Set the Policy value based on config
// Values are taken from https://github.com/pires/go-proxyproto/blob/195fedcfbfc1be163f3a0d507fac1709e9d81fed/policy.go#L20
switch strings.ToLower(s.Config.Server.ProxyPolicy) {
case "require":
return proxyproto.REQUIRE, nil
return staticProxyPolicy(proxyproto.REQUIRE), nil
case "ignore":
return proxyproto.IGNORE, nil
return staticProxyPolicy(proxyproto.IGNORE), nil
case "reject":
return proxyproto.REJECT, nil
return staticProxyPolicy(proxyproto.REJECT), nil
default:
return proxyproto.USE, nil
return staticProxyPolicy(proxyproto.USE), nil
}
}
func staticProxyPolicy(policy proxyproto.Policy) proxyproto.PolicyFunc {
return func(_ net.Addr) (proxyproto.Policy, error) {
return policy, nil
}
}
Loading
Loading
@@ -50,7 +50,7 @@ func TestListenAndServe(t *testing.T) {
verifyStatus(t, s, StatusClosed)
}
func TestListenAndServeRejectsPlainConnectionsWhenProxyProtocolEnabled(t *testing.T) {
func TestListenAndServe_proxyProtocolEnabled(t *testing.T) {
target, err := net.ResolveTCPAddr("tcp", serverUrl)
require.NoError(t, err)
Loading
Loading
@@ -70,10 +70,11 @@ func TestListenAndServeRejectsPlainConnectionsWhenProxyProtocolEnabled(t *testin
}()
testCases := []struct {
desc string
proxyPolicy string
header *proxyproto.Header
isRejected bool
desc string
proxyPolicy string
proxyAllowed []string
header *proxyproto.Header
isRejected bool
}{
{
desc: "USE (default) without a header",
Loading
Loading
@@ -123,11 +124,65 @@ func TestListenAndServeRejectsPlainConnectionsWhenProxyProtocolEnabled(t *testin
header: header,
isRejected: false,
},
{
desc: "Allow-listed IP with a header",
proxyAllowed: []string{"127.0.0.1"},
header: header,
isRejected: false,
},
{
desc: "Allow-listed IP without a header",
proxyAllowed: []string{"127.0.0.1"},
header: nil,
isRejected: false,
},
{
desc: "Allow-listed range with a header",
proxyAllowed: []string{"127.0.0.0/24"},
header: header,
isRejected: false,
},
{
desc: "Allow-listed range without a header",
proxyAllowed: []string{"127.0.0.0/24"},
header: nil,
isRejected: false,
},
{
desc: "Not allow-listed IP with a header",
proxyAllowed: []string{"192.168.1.1"},
header: header,
isRejected: true,
},
{
desc: "Not allow-listed IP without a header",
proxyAllowed: []string{"192.168.1.1"},
header: nil,
isRejected: false,
},
{
desc: "Not allow-listed range with a header",
proxyAllowed: []string{"192.168.1.0/24"},
header: header,
isRejected: true,
},
{
desc: "Not allow-listed range without a header",
proxyAllowed: []string{"192.168.1.0/24"},
header: nil,
isRejected: false,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
setupServerWithConfig(t, &config.Config{Server: config.ServerConfig{ProxyProtocol: true, ProxyPolicy: tc.proxyPolicy}})
setupServerWithConfig(t, &config.Config{
Server: config.ServerConfig{
ProxyProtocol: true,
ProxyPolicy: tc.proxyPolicy,
ProxyAllowed: tc.proxyAllowed,
},
})
conn, err := net.DialTCP("tcp", nil, target)
require.NoError(t, err)
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment