2017-10-10 08:19:14 -05:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
2021-09-07 02:35:37 -05:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-04-09 05:48:29 -05:00
|
|
|
"fmt"
|
2021-05-05 09:46:07 -05:00
|
|
|
"reflect"
|
2017-10-10 08:19:14 -05:00
|
|
|
"strconv"
|
2020-04-09 05:48:29 -05:00
|
|
|
"strings"
|
2017-10-10 08:19:14 -05:00
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
2023-07-18 16:23:02 -05:00
|
|
|
sdkproxy "github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
|
2021-05-05 09:46:07 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
|
2022-11-02 09:03:50 -05:00
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-09-10 14:50:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
2023-07-18 16:23:02 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/sqleng/proxyutil"
|
2017-10-10 08:19:14 -05:00
|
|
|
)
|
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
var logger = log.New("tsdb.postgres")
|
|
|
|
|
2022-01-20 11:16:22 -06:00
|
|
|
func ProvideService(cfg *setting.Cfg) *Service {
|
2021-09-07 02:35:37 -05:00
|
|
|
s := &Service{
|
2021-08-25 08:11:22 -05:00
|
|
|
tlsManager: newTLSManager(logger, cfg.DataPath),
|
|
|
|
}
|
2021-09-13 08:27:51 -05:00
|
|
|
s.im = datasource.NewInstanceManager(s.newInstanceSettings(cfg))
|
2022-01-20 11:16:22 -06:00
|
|
|
return s
|
2021-02-23 15:10:55 -06:00
|
|
|
}
|
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
type Service struct {
|
2021-02-23 15:10:55 -06:00
|
|
|
tlsManager tlsSettingsProvider
|
2021-09-07 02:35:37 -05:00
|
|
|
im instancemgmt.InstanceManager
|
2017-10-10 08:19:14 -05:00
|
|
|
}
|
|
|
|
|
2023-05-24 03:19:34 -05:00
|
|
|
func (s *Service) getDSInfo(ctx context.Context, pluginCtx backend.PluginContext) (*sqleng.DataSourceHandler, error) {
|
|
|
|
i, err := s.im.Get(ctx, pluginCtx)
|
2020-04-09 05:48:29 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-07 02:35:37 -05:00
|
|
|
instance := i.(*sqleng.DataSourceHandler)
|
|
|
|
return instance, nil
|
|
|
|
}
|
2017-10-10 08:19:14 -05:00
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
2023-05-24 03:19:34 -05:00
|
|
|
dsInfo, err := s.getDSInfo(ctx, req.PluginContext)
|
2021-09-07 02:35:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-03-15 05:53:30 -05:00
|
|
|
}
|
2021-09-07 02:35:37 -05:00
|
|
|
return dsInfo.QueryData(ctx, req)
|
|
|
|
}
|
2017-10-10 08:19:14 -05:00
|
|
|
|
2021-09-13 08:27:51 -05:00
|
|
|
func (s *Service) newInstanceSettings(cfg *setting.Cfg) datasource.InstanceFactoryFunc {
|
2023-09-21 04:33:31 -05:00
|
|
|
return func(_ context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
2021-09-07 02:35:37 -05:00
|
|
|
logger.Debug("Creating Postgres query endpoint")
|
|
|
|
jsonData := sqleng.JsonData{
|
2023-04-17 03:44:05 -05:00
|
|
|
MaxOpenConns: cfg.SqlDatasourceMaxOpenConnsDefault,
|
|
|
|
MaxIdleConns: cfg.SqlDatasourceMaxIdleConnsDefault,
|
|
|
|
ConnMaxLifetime: cfg.SqlDatasourceMaxConnLifetimeDefault,
|
2021-09-07 02:35:37 -05:00
|
|
|
Timescaledb: false,
|
|
|
|
ConfigurationMethod: "file-path",
|
2023-03-16 07:54:19 -05:00
|
|
|
SecureDSProxy: false,
|
2021-09-07 02:35:37 -05:00
|
|
|
}
|
2017-10-10 08:19:14 -05:00
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
err := json.Unmarshal(settings.JSONData, &jsonData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading settings: %w", err)
|
|
|
|
}
|
2022-12-13 02:56:52 -06:00
|
|
|
|
|
|
|
database := jsonData.Database
|
|
|
|
if database == "" {
|
|
|
|
database = settings.Database
|
|
|
|
}
|
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
dsInfo := sqleng.DataSourceInfo{
|
|
|
|
JsonData: jsonData,
|
|
|
|
URL: settings.URL,
|
|
|
|
User: settings.User,
|
2022-12-13 02:56:52 -06:00
|
|
|
Database: database,
|
2021-09-07 02:35:37 -05:00
|
|
|
ID: settings.ID,
|
|
|
|
Updated: settings.Updated,
|
|
|
|
UID: settings.UID,
|
|
|
|
DecryptedSecureJSONData: settings.DecryptedSecureJSONData,
|
|
|
|
}
|
2018-07-26 11:10:17 -05:00
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
cnnstr, err := s.generateConnectionString(dsInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-14 02:15:24 -05:00
|
|
|
|
2021-09-13 08:27:51 -05:00
|
|
|
if cfg.Env == setting.Dev {
|
2022-11-02 09:03:50 -05:00
|
|
|
logger.Debug("GetEngine", "connection", cnnstr)
|
2021-09-07 02:35:37 -05:00
|
|
|
}
|
|
|
|
|
2023-03-16 07:54:19 -05:00
|
|
|
driverName := "postgres"
|
|
|
|
// register a proxy driver if the secure socks proxy is enabled
|
2023-10-16 09:40:04 -05:00
|
|
|
proxyOpts := proxyutil.GetSQLProxyOptions(cfg.SecureSocksDSProxy, dsInfo)
|
|
|
|
if sdkproxy.New(proxyOpts).SecureSocksProxyEnabled() {
|
2023-07-18 16:23:02 -05:00
|
|
|
driverName, err = createPostgresProxyDriver(cnnstr, proxyOpts)
|
2023-03-16 07:54:19 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
config := sqleng.DataPluginConfiguration{
|
2023-03-16 07:54:19 -05:00
|
|
|
DriverName: driverName,
|
2021-09-07 02:35:37 -05:00
|
|
|
ConnectionString: cnnstr,
|
|
|
|
DSInfo: dsInfo,
|
|
|
|
MetricColumnTypes: []string{"UNKNOWN", "TEXT", "VARCHAR", "CHAR"},
|
2021-09-13 08:27:51 -05:00
|
|
|
RowLimit: cfg.DataProxyRowLimit,
|
2021-09-07 02:35:37 -05:00
|
|
|
}
|
|
|
|
|
2022-11-02 09:03:50 -05:00
|
|
|
queryResultTransformer := postgresQueryResultTransformer{}
|
2020-07-09 07:57:09 -05:00
|
|
|
|
2023-06-16 10:46:47 -05:00
|
|
|
handler, err := sqleng.NewQueryDataHandler(cfg, config, &queryResultTransformer, newPostgresMacroEngine(dsInfo.JsonData.Timescaledb),
|
2021-09-07 02:35:37 -05:00
|
|
|
logger)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed connecting to Postgres", "err", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("Successfully connected to Postgres")
|
|
|
|
return handler, nil
|
|
|
|
}
|
2017-10-10 08:19:14 -05:00
|
|
|
}
|
|
|
|
|
2020-07-09 07:57:09 -05:00
|
|
|
// escape single quotes and backslashes in Postgres connection string parameters.
|
|
|
|
func escape(input string) string {
|
2020-09-22 09:22:19 -05:00
|
|
|
return strings.ReplaceAll(strings.ReplaceAll(input, `\`, `\\`), "'", `\'`)
|
2020-07-09 07:57:09 -05:00
|
|
|
}
|
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
func (s *Service) generateConnectionString(dsInfo sqleng.DataSourceInfo) (string, error) {
|
2020-07-09 07:57:09 -05:00
|
|
|
var host string
|
|
|
|
var port int
|
2021-09-07 02:35:37 -05:00
|
|
|
if strings.HasPrefix(dsInfo.URL, "/") {
|
|
|
|
host = dsInfo.URL
|
|
|
|
logger.Debug("Generating connection string with Unix socket specifier", "socket", host)
|
2020-07-09 07:57:09 -05:00
|
|
|
} else {
|
2022-03-31 05:45:49 -05:00
|
|
|
index := strings.LastIndex(dsInfo.URL, ":")
|
|
|
|
v6Index := strings.Index(dsInfo.URL, "]")
|
2021-09-07 02:35:37 -05:00
|
|
|
sp := strings.SplitN(dsInfo.URL, ":", 2)
|
2020-07-09 07:57:09 -05:00
|
|
|
host = sp[0]
|
2022-03-31 05:45:49 -05:00
|
|
|
if v6Index == -1 {
|
|
|
|
if len(sp) > 1 {
|
|
|
|
var err error
|
|
|
|
port, err = strconv.Atoi(sp[1])
|
|
|
|
if err != nil {
|
2022-06-06 15:30:31 -05:00
|
|
|
return "", fmt.Errorf("invalid port in host specifier %q: %w", sp[1], err)
|
2022-03-31 05:45:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("Generating connection string with network host/port pair", "host", host, "port", port)
|
|
|
|
} else {
|
|
|
|
logger.Debug("Generating connection string with network host", "host", host)
|
2020-07-09 07:57:09 -05:00
|
|
|
}
|
|
|
|
} else {
|
2022-03-31 05:45:49 -05:00
|
|
|
if index == v6Index+1 {
|
2022-04-12 05:11:11 -05:00
|
|
|
host = dsInfo.URL[1 : index-1]
|
2022-03-31 05:45:49 -05:00
|
|
|
var err error
|
|
|
|
port, err = strconv.Atoi(dsInfo.URL[index+1:])
|
|
|
|
if err != nil {
|
2022-06-06 15:30:31 -05:00
|
|
|
return "", fmt.Errorf("invalid port in host specifier %q: %w", dsInfo.URL[index+1:], err)
|
2022-03-31 05:45:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("Generating ipv6 connection string with network host/port pair", "host", host, "port", port)
|
|
|
|
} else {
|
2022-04-12 05:11:11 -05:00
|
|
|
host = dsInfo.URL[1 : len(dsInfo.URL)-1]
|
2022-03-31 05:45:49 -05:00
|
|
|
logger.Debug("Generating ipv6 connection string with network host", "host", host)
|
|
|
|
}
|
2020-07-09 07:57:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:10:55 -06:00
|
|
|
connStr := fmt.Sprintf("user='%s' password='%s' host='%s' dbname='%s'",
|
2021-09-07 02:35:37 -05:00
|
|
|
escape(dsInfo.User), escape(dsInfo.DecryptedSecureJSONData["password"]), escape(host), escape(dsInfo.Database))
|
2020-07-09 07:57:09 -05:00
|
|
|
if port > 0 {
|
|
|
|
connStr += fmt.Sprintf(" port=%d", port)
|
|
|
|
}
|
2020-04-09 05:48:29 -05:00
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
tlsSettings, err := s.tlsManager.getTLSSettings(dsInfo)
|
2021-02-23 15:10:55 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-04-09 05:48:29 -05:00
|
|
|
|
2021-02-23 15:10:55 -06:00
|
|
|
connStr += fmt.Sprintf(" sslmode='%s'", escape(tlsSettings.Mode))
|
|
|
|
|
|
|
|
// Attach root certificate if provided
|
|
|
|
if tlsSettings.RootCertFile != "" {
|
2021-09-07 02:35:37 -05:00
|
|
|
logger.Debug("Setting server root certificate", "tlsRootCert", tlsSettings.RootCertFile)
|
2021-02-23 15:10:55 -06:00
|
|
|
connStr += fmt.Sprintf(" sslrootcert='%s'", escape(tlsSettings.RootCertFile))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach client certificate and key if both are provided
|
|
|
|
if tlsSettings.CertFile != "" && tlsSettings.CertKeyFile != "" {
|
2021-09-07 02:35:37 -05:00
|
|
|
logger.Debug("Setting TLS/SSL client auth", "tlsCert", tlsSettings.CertFile, "tlsKey", tlsSettings.CertKeyFile)
|
2021-02-23 15:10:55 -06:00
|
|
|
connStr += fmt.Sprintf(" sslcert='%s' sslkey='%s'", escape(tlsSettings.CertFile), escape(tlsSettings.CertKeyFile))
|
|
|
|
} else if tlsSettings.CertFile != "" || tlsSettings.CertKeyFile != "" {
|
|
|
|
return "", fmt.Errorf("TLS/SSL client certificate and key must both be specified")
|
2020-04-09 05:48:29 -05:00
|
|
|
}
|
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
logger.Debug("Generated Postgres connection string successfully")
|
2020-07-09 07:57:09 -05:00
|
|
|
return connStr, nil
|
2017-10-10 08:19:14 -05:00
|
|
|
}
|
|
|
|
|
2022-11-02 09:03:50 -05:00
|
|
|
type postgresQueryResultTransformer struct{}
|
2017-10-10 08:19:14 -05:00
|
|
|
|
2022-11-02 09:03:50 -05:00
|
|
|
func (t *postgresQueryResultTransformer) TransformQueryError(_ log.Logger, err error) error {
|
2019-09-24 13:50:49 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-05-05 09:46:07 -05:00
|
|
|
|
2022-12-19 10:17:52 -06:00
|
|
|
// CheckHealth pings the connected SQL database
|
|
|
|
func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
2023-05-24 03:19:34 -05:00
|
|
|
dsHandler, err := s.getDSInfo(ctx, req.PluginContext)
|
2022-12-19 10:17:52 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dsHandler.Ping()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Check health failed", "error", err)
|
|
|
|
return &backend.CheckHealthResult{Status: backend.HealthStatusError, Message: dsHandler.TransformQueryError(logger, err).Error()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &backend.CheckHealthResult{Status: backend.HealthStatusOk, Message: "Database Connection OK"}, nil
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:46:07 -05:00
|
|
|
func (t *postgresQueryResultTransformer) GetConverterList() []sqlutil.StringConverter {
|
|
|
|
return []sqlutil.StringConverter{
|
|
|
|
{
|
|
|
|
Name: "handle FLOAT4",
|
|
|
|
InputScanKind: reflect.Interface,
|
|
|
|
InputTypeName: "FLOAT4",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
2023-08-30 10:46:47 -05:00
|
|
|
ReplaceFunc: func(in *string) (any, error) {
|
2021-05-05 09:46:07 -05:00
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseFloat(*in, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle FLOAT8",
|
|
|
|
InputScanKind: reflect.Interface,
|
|
|
|
InputTypeName: "FLOAT8",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
2023-08-30 10:46:47 -05:00
|
|
|
ReplaceFunc: func(in *string) (any, error) {
|
2021-05-05 09:46:07 -05:00
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseFloat(*in, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle NUMERIC",
|
|
|
|
InputScanKind: reflect.Interface,
|
|
|
|
InputTypeName: "NUMERIC",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableFloat64,
|
2023-08-30 10:46:47 -05:00
|
|
|
ReplaceFunc: func(in *string) (any, error) {
|
2021-05-05 09:46:07 -05:00
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseFloat(*in, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-13 02:36:24 -05:00
|
|
|
{
|
|
|
|
Name: "handle INT2",
|
|
|
|
InputScanKind: reflect.Interface,
|
|
|
|
InputTypeName: "INT2",
|
|
|
|
ConversionFunc: func(in *string) (*string, error) { return in, nil },
|
|
|
|
Replacer: &sqlutil.StringFieldReplacer{
|
|
|
|
OutputFieldType: data.FieldTypeNullableInt16,
|
2023-08-30 10:46:47 -05:00
|
|
|
ReplaceFunc: func(in *string) (any, error) {
|
2021-07-13 02:36:24 -05:00
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
i64, err := strconv.ParseInt(*in, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
v := int16(i64)
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-05 09:46:07 -05:00
|
|
|
}
|
|
|
|
}
|