postgres: socks proxy: use plugin-sdk (#82376)

This commit is contained in:
Gábor Farkas
2024-02-14 13:05:31 +01:00
committed by GitHub
parent 7c44dd713a
commit 4c221966e4
5 changed files with 32 additions and 40 deletions

View File

@ -3,40 +3,37 @@ package postgres
import (
"database/sql"
"fmt"
"net"
"testing"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/sqleng"
"github.com/grafana/grafana/pkg/tsdb/sqleng/proxyutil"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
"golang.org/x/net/proxy"
)
type testDialer struct {
}
func (d *testDialer) Dial(network, addr string) (c net.Conn, err error) {
return nil, fmt.Errorf("test-dialer is not functional")
}
var _ proxy.Dialer = (&testDialer{})
func TestPostgresProxyDriver(t *testing.T) {
settings := proxyutil.SetupTestSecureSocksProxySettings(t)
proxySettings := setting.SecureSocksDSProxySettings{
Enabled: true,
ClientCert: settings.ClientCert,
ClientKey: settings.ClientKey,
RootCA: settings.RootCA,
ProxyAddress: settings.ProxyAddress,
ServerName: settings.ServerName,
}
opts := proxyutil.GetSQLProxyOptions(proxySettings, sqleng.DataSourceInfo{UID: "1", JsonData: sqleng.JsonData{SecureDSProxy: true}}, "pg", "postgres")
dbURL := "localhost:5432"
cnnstr := fmt.Sprintf("postgres://auser:password@%s/db?sslmode=disable", dbURL)
t.Run("Connector should use dialer context that routes through the socks proxy to db", func(t *testing.T) {
connector, err := pq.NewConnector(cnnstr)
require.NoError(t, err)
dialer, err := newPostgresProxyDialer(opts)
require.NoError(t, err)
dialer := newPostgresProxyDialer(&testDialer{})
connector.Dialer(dialer)
db := sql.OpenDB(connector)
err = db.Ping()
require.Contains(t, err.Error(), fmt.Sprintf("socks connect %s %s->%s", "tcp", settings.ProxyAddress, dbURL))
require.Contains(t, err.Error(), "test-dialer is not functional")
})
}