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 db4a3558 authored by Nick Thomas's avatar Nick Thomas
Browse files

gitlab-sshd: Support the PROXY protocol

parent dddd5c2e
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -43,4 +43,4 @@ check:
bin/check
clean:
rm -f bin/check bin/gitlab-shell bin/gitlab-shell-authorized-keys-check bin/gitlab-shell-authorized-principals-check
rm -f bin/check bin/gitlab-shell bin/gitlab-shell-authorized-keys-check bin/gitlab-shell-authorized-principals-check bin/gitlab-sshd
Loading
Loading
@@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
Loading
Loading
@@ -18,6 +19,7 @@ import (
"testing"
"github.com/mikesmitty/edkey"
"github.com/pires/go-proxyproto"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ssh"
)
Loading
Loading
@@ -72,6 +74,7 @@ secret: "0123456789abcdef"
gitlab_url: "` + gitlabUrl + `"
sshd:
listen: "127.0.0.1:0"
proxy_protocol: true
web_listen: ""
host_key_files:
- "` + hostKeyPath + `"`)
Loading
Loading
@@ -89,13 +92,37 @@ func buildClient(t *testing.T, addr string, hostKey ed25519.PublicKey) *ssh.Clie
clientSigner, err := ssh.NewSignerFromKey(clientPrivKey)
require.NoError(t, err)
client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
// Use the proxy protocol to spoof our client address
target, err := net.ResolveTCPAddr("tcp", addr)
require.NoError(t, err)
conn, err := net.DialTCP("tcp", nil, target)
require.NoError(t, err)
t.Cleanup(func() { conn.Close() })
// Create a proxyprotocol header or use HeaderProxyFromAddrs() if you
// have two conn's
header := &proxyproto.Header{
Version: 2,
Command: proxyproto.PROXY,
TransportProtocol: proxyproto.TCPv4,
SourceAddr: &net.TCPAddr{
IP: net.ParseIP("10.1.1.1"),
Port: 1000,
},
DestinationAddr: target,
}
// After the connection was created write the proxy headers first
_, err = header.WriteTo(conn)
require.NoError(t, err)
sshConn, chans, reqs, err := ssh.NewClientConn(conn, addr, &ssh.ClientConfig{
User: "git",
Auth: []ssh.AuthMethod{ssh.PublicKeys(clientSigner)},
HostKeyCallback: ssh.FixedHostKey(pubKey),
})
require.NoError(t, err)
client := ssh.NewClient(sshConn, chans, reqs)
t.Cleanup(func() { client.Close() })
return client
Loading
Loading
Loading
Loading
@@ -66,6 +66,9 @@ audit_usernames: false
sshd:
# Address which the SSH server listens on. Defaults to [::]:22.
listen: "[::]:22"
# Set to true if gitlab-sshd is being fronted by a load balancer that implements
# the PROXY protocol.
proxy_protocol: false
# 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
@@ -6,6 +6,7 @@ require (
github.com/mattn/go-shellwords v1.0.11
github.com/mikesmitty/edkey v0.0.0-20170222072505-3356ea4e686a
github.com/otiai10/copy v1.4.2
github.com/pires/go-proxyproto v0.5.0
github.com/prometheus/client_golang v1.9.0
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.6.1
Loading
Loading
Loading
Loading
@@ -19,6 +19,7 @@ const (
type ServerConfig struct {
Listen string `yaml:"listen,omitempty"`
ProxyProtocol bool `yaml:"proxy_protocol,omitempty"`
WebListen string `yaml:"web_listen,omitempty"`
ConcurrentSessionsLimit int64 `yaml:"concurrent_sessions_limit,omitempty"`
HostKeyFiles []string `yaml:"host_key_files,omitempty"`
Loading
Loading
Loading
Loading
@@ -10,17 +10,20 @@ import (
"strconv"
"time"
log "github.com/sirupsen/logrus"
"github.com/pires/go-proxyproto"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"golang.org/x/sync/semaphore"
"gitlab.com/gitlab-org/gitlab-shell/internal/command"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/authorizedkeys"
"gitlab.com/gitlab-org/gitlab-shell/internal/sshenv"
"golang.org/x/crypto/ssh"
"golang.org/x/sync/semaphore"
)
const (
Loading
Loading
@@ -73,6 +76,12 @@ func Run(cfg *config.Config) error {
if err != nil {
return fmt.Errorf("failed to listen for connection: %w", err)
}
if cfg.Server.ProxyProtocol {
sshListener = &proxyproto.Listener{Listener: sshListener}
log.Info("Proxy protocol is enabled")
}
defer sshListener.Close()
log.Infof("Listening on %v", sshListener.Addr().String())
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