Set DatasourceName and DatasourceType in proxy.Options (#80923)

* Set DatasourceName and DatasourceType in proxy.Options

* upgrade github.com/grafana/grafana-plugin-sdk-go to v0.206.0 and fix merge conflicts
This commit is contained in:
Bruno 2024-02-01 11:09:58 -03:00 committed by GitHub
parent d7ded807a2
commit 2332bfb007
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 18 additions and 16 deletions

2
go.mod
View File

@ -63,7 +63,7 @@ require (
github.com/grafana/cuetsy v0.1.11 // @grafana/grafana-as-code
github.com/grafana/grafana-aws-sdk v0.19.1 // @grafana/aws-datasources
github.com/grafana/grafana-azure-sdk-go v1.11.0 // @grafana/partner-datasources
github.com/grafana/grafana-plugin-sdk-go v0.204.0 // @grafana/plugins-platform-backend
github.com/grafana/grafana-plugin-sdk-go v0.206.0 // @grafana/plugins-platform-backend
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // @grafana/backend-platform
github.com/hashicorp/go-hclog v1.6.2 // @grafana/plugins-platform-backend
github.com/hashicorp/go-plugin v1.6.0 // @grafana/plugins-platform-backend

4
go.sum
View File

@ -1832,8 +1832,8 @@ github.com/grafana/grafana-openapi-client-go v0.0.0-20231213163343-bd475d63fb79
github.com/grafana/grafana-openapi-client-go v0.0.0-20231213163343-bd475d63fb79/go.mod h1:wc6Hbh3K2TgCUSfBC/BOzabItujtHMESZeFk5ZhdxhQ=
github.com/grafana/grafana-plugin-sdk-go v0.94.0/go.mod h1:3VXz4nCv6wH5SfgB3mlW39s+c+LetqSCjFj7xxPC5+M=
github.com/grafana/grafana-plugin-sdk-go v0.114.0/go.mod h1:D7x3ah+1d4phNXpbnOaxa/osSaZlwh9/ZUnGGzegRbk=
github.com/grafana/grafana-plugin-sdk-go v0.204.0 h1:4+VtZEqmNwfN9OvDersv7rN861tKTvJdaZKCwfuIAmc=
github.com/grafana/grafana-plugin-sdk-go v0.204.0/go.mod h1:MetVbF3bGwmUvxulWsl0SskJFUq3+ZGnyjDH0dQ4sSg=
github.com/grafana/grafana-plugin-sdk-go v0.206.0 h1:DQK2M07/q0t7wqcJBSw6O/FQJZC1tm24uzGfHbA71+A=
github.com/grafana/grafana-plugin-sdk-go v0.206.0/go.mod h1:MetVbF3bGwmUvxulWsl0SskJFUq3+ZGnyjDH0dQ4sSg=
github.com/grafana/kindsys v0.0.0-20230508162304-452481b63482 h1:1YNoeIhii4UIIQpCPU+EXidnqf449d0C3ZntAEt4KSo=
github.com/grafana/kindsys v0.0.0-20230508162304-452481b63482/go.mod h1:GNcfpy5+SY6RVbNGQW264gC0r336Dm+0zgQ5vt6+M8Y=
github.com/grafana/prometheus-alertmanager v0.25.1-0.20231027171310-70c52bf65758 h1:ATUhvJSJwzdzhnmzUI92fxVFqyqmcnzJ47wtHTK3LW4=

View File

@ -57,7 +57,7 @@ func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest)
return dsInfo.QueryData(ctx, req)
}
func newPostgres(cfg *setting.Cfg, dsInfo sqleng.DataSourceInfo, cnnstr string, logger log.Logger) (*sql.DB, *sqleng.DataSourceHandler, error) {
func newPostgres(cfg *setting.Cfg, dsInfo sqleng.DataSourceInfo, cnnstr string, logger log.Logger, settings backend.DataSourceInstanceSettings) (*sql.DB, *sqleng.DataSourceHandler, error) {
connector, err := pq.NewConnector(cnnstr)
if err != nil {
logger.Error("postgres connector creation failed", "error", err)
@ -65,7 +65,7 @@ func newPostgres(cfg *setting.Cfg, dsInfo sqleng.DataSourceInfo, cnnstr string,
}
// use the proxy-dialer if the secure socks proxy is enabled
proxyOpts := proxyutil.GetSQLProxyOptions(cfg.SecureSocksDSProxy, dsInfo)
proxyOpts := proxyutil.GetSQLProxyOptions(cfg.SecureSocksDSProxy, dsInfo, settings.Name, settings.Type)
if sdkproxy.New(proxyOpts).SecureSocksProxyEnabled() {
dialer, err := newPostgresProxyDialer(proxyOpts)
if err != nil {
@ -140,7 +140,7 @@ func (s *Service) newInstanceSettings(cfg *setting.Cfg) datasource.InstanceFacto
return nil, err
}
_, handler, err := newPostgres(cfg, dsInfo, cnnstr, logger)
_, handler, err := newPostgres(cfg, dsInfo, cnnstr, logger, settings)
if err != nil {
logger.Error("Failed connecting to Postgres", "err", err)

View File

@ -164,7 +164,7 @@ func TestIntegrationPostgresSnapshots(t *testing.T) {
cnnstr := getCnnStr()
db, handler, err := newPostgres(cfg, dsInfo, cnnstr, logger)
db, handler, err := newPostgres(cfg, dsInfo, cnnstr, logger, backend.DataSourceInstanceSettings{})
t.Cleanup((func() {
_, err := db.Exec("DROP TABLE tbl")

View File

@ -212,7 +212,7 @@ func TestIntegrationPostgres(t *testing.T) {
cnnstr := postgresTestDBConnString()
db, exe, err := newPostgres(cfg, dsInfo, cnnstr, logger)
db, exe, err := newPostgres(cfg, dsInfo, cnnstr, logger, backend.DataSourceInstanceSettings{})
require.NoError(t, err)
@ -1268,7 +1268,7 @@ func TestIntegrationPostgres(t *testing.T) {
dsInfo := sqleng.DataSourceInfo{}
conf := setting.NewCfg()
conf.DataProxyRowLimit = 1
_, handler, err := newPostgres(conf, dsInfo, cnnstr, logger)
_, handler, err := newPostgres(conf, dsInfo, cnnstr, logger, backend.DataSourceInstanceSettings{})
require.NoError(t, err)

View File

@ -22,7 +22,7 @@ func TestPostgresProxyDriver(t *testing.T) {
ProxyAddress: settings.ProxyAddress,
ServerName: settings.ServerName,
}
opts := proxyutil.GetSQLProxyOptions(proxySettings, sqleng.DataSourceInfo{UID: "1", JsonData: sqleng.JsonData{SecureDSProxy: true}})
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)

View File

@ -114,7 +114,7 @@ func newInstanceSettings(cfg *setting.Cfg, logger log.Logger) datasource.Instanc
}
// register a new proxy driver if the secure socks proxy is enabled
proxyOpts := proxyutil.GetSQLProxyOptions(cfg.SecureSocksDSProxy, dsInfo)
proxyOpts := proxyutil.GetSQLProxyOptions(cfg.SecureSocksDSProxy, dsInfo, settings.Name, settings.Type)
if sdkproxy.New(proxyOpts).SecureSocksProxyEnabled() {
URL, err := ParseURL(dsInfo.URL, logger)
if err != nil {

View File

@ -22,7 +22,7 @@ func TestMSSQLProxyDriver(t *testing.T) {
ProxyAddress: settings.ProxyAddress,
ServerName: settings.ServerName,
}
opts := proxyutil.GetSQLProxyOptions(proxySettings, sqleng.DataSourceInfo{UID: "1", JsonData: sqleng.JsonData{SecureDSProxy: true}})
opts := proxyutil.GetSQLProxyOptions(proxySettings, sqleng.DataSourceInfo{UID: "1", JsonData: sqleng.JsonData{SecureDSProxy: true}}, "mssql", "mssql")
cnnstr := "server=127.0.0.1;port=1433;user id=sa;password=yourStrong(!)Password;database=db"
driverName, err := createMSSQLProxyDriver(cnnstr, "127.0.0.1", opts)
require.NoError(t, err)

View File

@ -90,7 +90,7 @@ func newInstanceSettings(cfg *setting.Cfg, logger log.Logger) datasource.Instanc
}
// register the secure socks proxy dialer context, if enabled
proxyOpts := proxyutil.GetSQLProxyOptions(cfg.SecureSocksDSProxy, dsInfo)
proxyOpts := proxyutil.GetSQLProxyOptions(cfg.SecureSocksDSProxy, dsInfo, settings.Name, settings.Type)
if sdkproxy.New(proxyOpts).SecureSocksProxyEnabled() {
// UID is only unique per org, the only way to ensure uniqueness is to do it by connection information
uniqueIdentifier := dsInfo.User + dsInfo.DecryptedSecureJSONData["password"] + dsInfo.URL + dsInfo.Database

View File

@ -23,7 +23,7 @@ func TestMySQLProxyDialer(t *testing.T) {
ServerName: settings.ServerName,
}
protocol := "tcp"
opts := proxyutil.GetSQLProxyOptions(proxySettings, sqleng.DataSourceInfo{UID: "1", JsonData: sqleng.JsonData{SecureDSProxy: true}})
opts := proxyutil.GetSQLProxyOptions(proxySettings, sqleng.DataSourceInfo{UID: "1", JsonData: sqleng.JsonData{SecureDSProxy: true}}, "mysql", "mysql")
dbURL := "localhost:5432"
network, err := registerProxyDialerContext(protocol, dbURL, opts)
require.NoError(t, err)

View File

@ -6,9 +6,11 @@ import (
"github.com/grafana/grafana/pkg/tsdb/sqleng"
)
func GetSQLProxyOptions(cfg setting.SecureSocksDSProxySettings, dsInfo sqleng.DataSourceInfo) *sdkproxy.Options {
func GetSQLProxyOptions(cfg setting.SecureSocksDSProxySettings, dsInfo sqleng.DataSourceInfo, datasource, datasourceType string) *sdkproxy.Options {
opts := &sdkproxy.Options{
Enabled: dsInfo.JsonData.SecureDSProxy && cfg.Enabled,
Enabled: dsInfo.JsonData.SecureDSProxy && cfg.Enabled,
DatasourceName: datasource,
DatasourceType: datasourceType,
Auth: &sdkproxy.AuthOptions{
Username: dsInfo.UID,
},