mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 07:21:50 +08:00
Revert "Postgres: Switch the datasource plugin from lib/pq to pgx" (#83760)
Revert "Postgres: Switch the datasource plugin from lib/pq to pgx (#81353)" This reverts commit 8c18d06386c87f2786119ea9a6334e35e4181cbe.
This commit is contained in:
@ -14,16 +14,19 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb/grafana-postgresql-datasource/tls"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func TestGenerateConnectionConfig(t *testing.T) {
|
||||
rootCertBytes, err := tls.CreateRandomRootCertBytes()
|
||||
require.NoError(t, err)
|
||||
// Test generateConnectionString.
|
||||
func TestIntegrationGenerateConnectionString(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
cfg := setting.NewCfg()
|
||||
cfg.DataPath = t.TempDir()
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
@ -31,15 +34,10 @@ func TestGenerateConnectionConfig(t *testing.T) {
|
||||
user string
|
||||
password string
|
||||
database string
|
||||
tlsMode string
|
||||
tlsRootCert []byte
|
||||
tlsSettings tlsSettings
|
||||
expConnStr string
|
||||
expErr string
|
||||
expHost string
|
||||
expPort uint16
|
||||
expUser string
|
||||
expPassword string
|
||||
expDatabase string
|
||||
expTLS bool
|
||||
uid string
|
||||
}{
|
||||
{
|
||||
desc: "Unix socket host",
|
||||
@ -47,11 +45,8 @@ func TestGenerateConnectionConfig(t *testing.T) {
|
||||
user: "user",
|
||||
password: "password",
|
||||
database: "database",
|
||||
tlsMode: "disable",
|
||||
expUser: "user",
|
||||
expPassword: "password",
|
||||
expHost: "/var/run/postgresql",
|
||||
expDatabase: "database",
|
||||
tlsSettings: tlsSettings{Mode: "verify-full"},
|
||||
expConnStr: "user='user' password='password' host='/var/run/postgresql' dbname='database' sslmode='verify-full'",
|
||||
},
|
||||
{
|
||||
desc: "TCP host",
|
||||
@ -59,12 +54,8 @@ func TestGenerateConnectionConfig(t *testing.T) {
|
||||
user: "user",
|
||||
password: "password",
|
||||
database: "database",
|
||||
tlsMode: "disable",
|
||||
expUser: "user",
|
||||
expPassword: "password",
|
||||
expHost: "host",
|
||||
expPort: 5432,
|
||||
expDatabase: "database",
|
||||
tlsSettings: tlsSettings{Mode: "verify-full"},
|
||||
expConnStr: "user='user' password='password' host='host' dbname='database' sslmode='verify-full'",
|
||||
},
|
||||
{
|
||||
desc: "TCP/port host",
|
||||
@ -72,12 +63,8 @@ func TestGenerateConnectionConfig(t *testing.T) {
|
||||
user: "user",
|
||||
password: "password",
|
||||
database: "database",
|
||||
tlsMode: "disable",
|
||||
expUser: "user",
|
||||
expPassword: "password",
|
||||
expHost: "host",
|
||||
expPort: 1234,
|
||||
expDatabase: "database",
|
||||
tlsSettings: tlsSettings{Mode: "verify-full"},
|
||||
expConnStr: "user='user' password='password' host='host' dbname='database' port=1234 sslmode='verify-full'",
|
||||
},
|
||||
{
|
||||
desc: "Ipv6 host",
|
||||
@ -85,11 +72,8 @@ func TestGenerateConnectionConfig(t *testing.T) {
|
||||
user: "user",
|
||||
password: "password",
|
||||
database: "database",
|
||||
tlsMode: "disable",
|
||||
expUser: "user",
|
||||
expPassword: "password",
|
||||
expHost: "::1",
|
||||
expDatabase: "database",
|
||||
tlsSettings: tlsSettings{Mode: "verify-full"},
|
||||
expConnStr: "user='user' password='password' host='::1' dbname='database' sslmode='verify-full'",
|
||||
},
|
||||
{
|
||||
desc: "Ipv6/port host",
|
||||
@ -97,20 +81,16 @@ func TestGenerateConnectionConfig(t *testing.T) {
|
||||
user: "user",
|
||||
password: "password",
|
||||
database: "database",
|
||||
tlsMode: "disable",
|
||||
expUser: "user",
|
||||
expPassword: "password",
|
||||
expHost: "::1",
|
||||
expPort: 1234,
|
||||
expDatabase: "database",
|
||||
tlsSettings: tlsSettings{Mode: "verify-full"},
|
||||
expConnStr: "user='user' password='password' host='::1' dbname='database' port=1234 sslmode='verify-full'",
|
||||
},
|
||||
{
|
||||
desc: "Invalid port",
|
||||
host: "host:invalid",
|
||||
user: "user",
|
||||
database: "database",
|
||||
tlsMode: "disable",
|
||||
expErr: "invalid port in host specifier",
|
||||
desc: "Invalid port",
|
||||
host: "host:invalid",
|
||||
user: "user",
|
||||
database: "database",
|
||||
tlsSettings: tlsSettings{},
|
||||
expErr: "invalid port in host specifier",
|
||||
},
|
||||
{
|
||||
desc: "Password with single quote and backslash",
|
||||
@ -118,11 +98,8 @@ func TestGenerateConnectionConfig(t *testing.T) {
|
||||
user: "user",
|
||||
password: `p'\assword`,
|
||||
database: "database",
|
||||
tlsMode: "disable",
|
||||
expUser: "user",
|
||||
expPassword: `p'\assword`,
|
||||
expHost: "host",
|
||||
expDatabase: "database",
|
||||
tlsSettings: tlsSettings{Mode: "verify-full"},
|
||||
expConnStr: `user='user' password='p\'\\assword' host='host' dbname='database' sslmode='verify-full'`,
|
||||
},
|
||||
{
|
||||
desc: "User/DB with single quote and backslash",
|
||||
@ -130,11 +107,8 @@ func TestGenerateConnectionConfig(t *testing.T) {
|
||||
user: `u'\ser`,
|
||||
password: `password`,
|
||||
database: `d'\atabase`,
|
||||
tlsMode: "disable",
|
||||
expUser: `u'\ser`,
|
||||
expPassword: "password",
|
||||
expDatabase: `d'\atabase`,
|
||||
expHost: "host",
|
||||
tlsSettings: tlsSettings{Mode: "verify-full"},
|
||||
expConnStr: `user='u\'\\ser' password='password' host='host' dbname='d\'\\atabase' sslmode='verify-full'`,
|
||||
},
|
||||
{
|
||||
desc: "Custom TLS mode disabled",
|
||||
@ -142,55 +116,45 @@ func TestGenerateConnectionConfig(t *testing.T) {
|
||||
user: "user",
|
||||
password: "password",
|
||||
database: "database",
|
||||
tlsMode: "disable",
|
||||
expUser: "user",
|
||||
expPassword: "password",
|
||||
expHost: "host",
|
||||
expDatabase: "database",
|
||||
tlsSettings: tlsSettings{Mode: "disable"},
|
||||
expConnStr: "user='user' password='password' host='host' dbname='database' sslmode='disable'",
|
||||
},
|
||||
{
|
||||
desc: "Custom TLS mode verify-full with certificate files",
|
||||
host: "host",
|
||||
user: "user",
|
||||
password: "password",
|
||||
database: "database",
|
||||
tlsMode: "verify-full",
|
||||
tlsRootCert: rootCertBytes,
|
||||
expUser: "user",
|
||||
expPassword: "password",
|
||||
expDatabase: "database",
|
||||
expHost: "host",
|
||||
expTLS: true,
|
||||
desc: "Custom TLS mode verify-full with certificate files",
|
||||
host: "host",
|
||||
user: "user",
|
||||
password: "password",
|
||||
database: "database",
|
||||
tlsSettings: tlsSettings{
|
||||
Mode: "verify-full",
|
||||
RootCertFile: "i/am/coding/ca.crt",
|
||||
CertFile: "i/am/coding/client.crt",
|
||||
CertKeyFile: "i/am/coding/client.key",
|
||||
},
|
||||
expConnStr: "user='user' password='password' host='host' dbname='database' sslmode='verify-full' " +
|
||||
"sslrootcert='i/am/coding/ca.crt' sslcert='i/am/coding/client.crt' sslkey='i/am/coding/client.key'",
|
||||
},
|
||||
}
|
||||
for _, tt := range testCases {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
ds := sqleng.DataSourceInfo{
|
||||
URL: tt.host,
|
||||
User: tt.user,
|
||||
DecryptedSecureJSONData: map[string]string{
|
||||
"password": tt.password,
|
||||
"tlsCACert": string(tt.tlsRootCert),
|
||||
},
|
||||
Database: tt.database,
|
||||
JsonData: sqleng.JsonData{
|
||||
Mode: tt.tlsMode,
|
||||
ConfigurationMethod: "file-content",
|
||||
},
|
||||
svc := Service{
|
||||
tlsManager: &tlsTestManager{settings: tt.tlsSettings},
|
||||
logger: backend.NewLoggerWith("logger", "tsdb.postgres"),
|
||||
}
|
||||
|
||||
c, err := generateConnectionConfig(ds)
|
||||
ds := sqleng.DataSourceInfo{
|
||||
URL: tt.host,
|
||||
User: tt.user,
|
||||
DecryptedSecureJSONData: map[string]string{"password": tt.password},
|
||||
Database: tt.database,
|
||||
UID: tt.uid,
|
||||
}
|
||||
|
||||
connStr, err := svc.generateConnectionString(ds)
|
||||
|
||||
if tt.expErr == "" {
|
||||
require.NoError(t, err, tt.desc)
|
||||
assert.Equal(t, tt.expHost, c.Host)
|
||||
if tt.expPort != 0 {
|
||||
assert.Equal(t, tt.expPort, c.Port)
|
||||
}
|
||||
assert.Equal(t, tt.expUser, c.User)
|
||||
assert.Equal(t, tt.expDatabase, c.Database)
|
||||
assert.Equal(t, tt.expPassword, c.Password)
|
||||
require.Equal(t, tt.expTLS, c.TLSConfig != nil)
|
||||
assert.Equal(t, tt.expConnStr, connStr)
|
||||
} else {
|
||||
require.Error(t, err, tt.desc)
|
||||
assert.True(t, strings.HasPrefix(err.Error(), tt.expErr),
|
||||
@ -242,10 +206,9 @@ func TestIntegrationPostgres(t *testing.T) {
|
||||
|
||||
logger := backend.NewLoggerWith("logger", "postgres.test")
|
||||
|
||||
cnn, err := postgresTestDBConn()
|
||||
require.NoError(t, err)
|
||||
cnnstr := postgresTestDBConnString()
|
||||
|
||||
db, exe, err := newPostgres(context.Background(), "error", 10000, dsInfo, cnn, logger, backend.DataSourceInstanceSettings{})
|
||||
db, exe, err := newPostgres(context.Background(), "error", 10000, dsInfo, cnnstr, logger, backend.DataSourceInstanceSettings{})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
@ -1299,7 +1262,7 @@ func TestIntegrationPostgres(t *testing.T) {
|
||||
|
||||
t.Run("When row limit set to 1", func(t *testing.T) {
|
||||
dsInfo := sqleng.DataSourceInfo{}
|
||||
_, handler, err := newPostgres(context.Background(), "error", 1, dsInfo, cnn, logger, backend.DataSourceInstanceSettings{})
|
||||
_, handler, err := newPostgres(context.Background(), "error", 1, dsInfo, cnnstr, logger, backend.DataSourceInstanceSettings{})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
@ -1414,6 +1377,14 @@ func genTimeRangeByInterval(from time.Time, duration time.Duration, interval tim
|
||||
return timeRange
|
||||
}
|
||||
|
||||
type tlsTestManager struct {
|
||||
settings tlsSettings
|
||||
}
|
||||
|
||||
func (m *tlsTestManager) getTLSSettings(dsInfo sqleng.DataSourceInfo) (tlsSettings, error) {
|
||||
return m.settings, nil
|
||||
}
|
||||
|
||||
func isTestDbPostgres() bool {
|
||||
if db, present := os.LookupEnv("GRAFANA_TEST_DB"); present {
|
||||
return db == "postgres"
|
||||
@ -1422,7 +1393,7 @@ func isTestDbPostgres() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func postgresTestDBConn() (*pgx.ConnConfig, error) {
|
||||
func postgresTestDBConnString() string {
|
||||
host := os.Getenv("POSTGRES_HOST")
|
||||
if host == "" {
|
||||
host = "localhost"
|
||||
@ -1431,8 +1402,6 @@ func postgresTestDBConn() (*pgx.ConnConfig, error) {
|
||||
if port == "" {
|
||||
port = "5432"
|
||||
}
|
||||
connStr := fmt.Sprintf("user=grafanatest password=grafanatest host=%s port=%s dbname=grafanadstest sslmode=disable",
|
||||
return fmt.Sprintf("user=grafanatest password=grafanatest host=%s port=%s dbname=grafanadstest sslmode=disable",
|
||||
host, port)
|
||||
|
||||
return pgx.ParseConfig(connStr)
|
||||
}
|
||||
|
Reference in New Issue
Block a user