2017-10-10 08:19:14 -05:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
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-05-05 09:46:07 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
|
2021-02-23 15:10:55 -06:00
|
|
|
"github.com/grafana/grafana/pkg/registry"
|
2019-09-10 14:50:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2020-07-09 07:57:09 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2019-09-10 14:50:04 -05:00
|
|
|
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2017-10-10 08:19:14 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-03-08 00:02:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2019-09-10 14:50:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
2017-10-10 08:19:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-02-23 15:10:55 -06:00
|
|
|
registry.Register(®istry.Descriptor{
|
|
|
|
Name: "PostgresService",
|
|
|
|
InitPriority: registry.Low,
|
2021-03-08 00:02:49 -06:00
|
|
|
Instance: &PostgresService{},
|
2021-02-23 15:10:55 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
type PostgresService struct {
|
2021-02-23 15:10:55 -06:00
|
|
|
Cfg *setting.Cfg `inject:""`
|
|
|
|
logger log.Logger
|
|
|
|
tlsManager tlsSettingsProvider
|
2017-10-10 08:19:14 -05:00
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
func (s *PostgresService) Init() error {
|
2021-02-23 15:10:55 -06:00
|
|
|
s.logger = log.New("tsdb.postgres")
|
|
|
|
s.tlsManager = newTLSManager(s.logger, s.Cfg.DataPath)
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-09 05:48:29 -05:00
|
|
|
|
2021-04-22 20:03:11 -05:00
|
|
|
//nolint: staticcheck // plugins.DataPlugin deprecated
|
2021-03-08 00:02:49 -06:00
|
|
|
func (s *PostgresService) NewExecutor(datasource *models.DataSource) (plugins.DataPlugin, error) {
|
2021-02-23 15:10:55 -06:00
|
|
|
s.logger.Debug("Creating Postgres query endpoint")
|
|
|
|
|
|
|
|
cnnstr, err := s.generateConnectionString(datasource)
|
2020-04-09 05:48:29 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-10 08:19:14 -05:00
|
|
|
|
2021-02-23 15:10:55 -06:00
|
|
|
if s.Cfg.Env == setting.Dev {
|
|
|
|
s.logger.Debug("getEngine", "connection", cnnstr)
|
2019-03-15 05:53:30 -05:00
|
|
|
}
|
2017-10-10 08:19:14 -05:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
config := sqleng.DataPluginConfiguration{
|
2018-07-26 11:10:17 -05:00
|
|
|
DriverName: "postgres",
|
|
|
|
ConnectionString: cnnstr,
|
|
|
|
Datasource: datasource,
|
|
|
|
MetricColumnTypes: []string{"UNKNOWN", "TEXT", "VARCHAR", "CHAR"},
|
2017-10-10 08:19:14 -05:00
|
|
|
}
|
|
|
|
|
2019-09-24 13:50:49 -05:00
|
|
|
queryResultTransformer := postgresQueryResultTransformer{
|
2021-02-23 15:10:55 -06:00
|
|
|
log: s.logger,
|
2018-07-26 11:10:17 -05:00
|
|
|
}
|
|
|
|
|
2018-08-14 02:15:24 -05:00
|
|
|
timescaledb := datasource.JsonData.Get("timescaledb").MustBool(false)
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
plugin, err := sqleng.NewDataPlugin(config, &queryResultTransformer, newPostgresMacroEngine(timescaledb),
|
2021-02-23 15:10:55 -06:00
|
|
|
s.logger)
|
2020-07-09 07:57:09 -05:00
|
|
|
if err != nil {
|
2021-02-23 15:10:55 -06:00
|
|
|
s.logger.Error("Failed connecting to Postgres", "err", err)
|
2020-07-09 07:57:09 -05:00
|
|
|
return nil, err
|
2020-04-09 05:48:29 -05:00
|
|
|
}
|
2020-07-09 07:57:09 -05:00
|
|
|
|
2021-02-23 15:10:55 -06:00
|
|
|
s.logger.Debug("Successfully connected to Postgres")
|
2021-03-08 00:02:49 -06:00
|
|
|
return plugin, 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-03-08 00:02:49 -06:00
|
|
|
func (s *PostgresService) generateConnectionString(datasource *models.DataSource) (string, error) {
|
2020-07-09 07:57:09 -05:00
|
|
|
var host string
|
|
|
|
var port int
|
|
|
|
if strings.HasPrefix(datasource.Url, "/") {
|
|
|
|
host = datasource.Url
|
2021-02-23 15:10:55 -06:00
|
|
|
s.logger.Debug("Generating connection string with Unix socket specifier", "socket", host)
|
2020-07-09 07:57:09 -05:00
|
|
|
} else {
|
|
|
|
sp := strings.SplitN(datasource.Url, ":", 2)
|
|
|
|
host = sp[0]
|
|
|
|
if len(sp) > 1 {
|
|
|
|
var err error
|
|
|
|
port, err = strconv.Atoi(sp[1])
|
|
|
|
if err != nil {
|
|
|
|
return "", errutil.Wrapf(err, "invalid port in host specifier %q", sp[1])
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:10:55 -06:00
|
|
|
s.logger.Debug("Generating connection string with network host/port pair", "host", host, "port", port)
|
2020-07-09 07:57:09 -05:00
|
|
|
} else {
|
2021-02-23 15:10:55 -06:00
|
|
|
s.logger.Debug("Generating 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'",
|
|
|
|
escape(datasource.User), escape(datasource.DecryptedPassword()), escape(host), escape(datasource.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-02-23 15:10:55 -06:00
|
|
|
tlsSettings, err := s.tlsManager.getTLSSettings(datasource)
|
|
|
|
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 != "" {
|
|
|
|
s.logger.Debug("Setting server root certificate", "tlsRootCert", tlsSettings.RootCertFile)
|
|
|
|
connStr += fmt.Sprintf(" sslrootcert='%s'", escape(tlsSettings.RootCertFile))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach client certificate and key if both are provided
|
|
|
|
if tlsSettings.CertFile != "" && tlsSettings.CertKeyFile != "" {
|
|
|
|
s.logger.Debug("Setting TLS/SSL client auth", "tlsCert", tlsSettings.CertFile, "tlsKey", tlsSettings.CertKeyFile)
|
|
|
|
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-02-23 15:10:55 -06:00
|
|
|
s.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
|
|
|
}
|
|
|
|
|
2019-09-24 13:50:49 -05:00
|
|
|
type postgresQueryResultTransformer struct {
|
2018-07-26 11:10:17 -05:00
|
|
|
log log.Logger
|
2017-10-10 08:19:14 -05:00
|
|
|
}
|
|
|
|
|
2019-09-24 13:50:49 -05:00
|
|
|
func (t *postgresQueryResultTransformer) TransformQueryError(err error) error {
|
|
|
|
return err
|
|
|
|
}
|
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,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
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,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
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,
|
|
|
|
ReplaceFunc: func(in *string) (interface{}, error) {
|
|
|
|
if in == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseFloat(*in, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|