From 6f90866f58807873816ae69f1e37d4a0fbcc8ddd Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 3 Nov 2022 20:09:52 -0500 Subject: [PATCH] Expose underlying pgconn GetSSLPassword support to pgx pgconn supports a GetSSLPassword function but the pgx connection functions did not expose a means of using it. See PR #1233 for more context. --- conn.go | 53 +++++++++++++++++++++++++++++++++--------------- pgconn/config.go | 2 +- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/conn.go b/conn.go index 4a68893a..cb5034bc 100644 --- a/conn.go +++ b/conn.go @@ -42,6 +42,11 @@ type ConnConfig struct { createdByParseConfig bool // Used to enforce created by ParseConfig rule. } +// ParseConfigOptions contains options that control how a config is built such as getsslpassword. +type ParseConfigOptions struct { + pgconn.ParseConfigOptions +} + // Copy returns a deep copy of the config that is safe to use and modify. // The only exception is the tls.Config: // according to the tls.Config docs it must not be modified after creation. @@ -110,6 +115,16 @@ func Connect(ctx context.Context, connString string) (*Conn, error) { return connect(ctx, connConfig) } +// ConnectWithOptions behaves exactly like Connect with the addition of options. At the present options is only used to +// provide a GetSSLPassword function. +func ConnectWithOptions(ctx context.Context, connString string, options ParseConfigOptions) (*Conn, error) { + connConfig, err := ParseConfigWithOptions(connString, options) + if err != nil { + return nil, err + } + return connect(ctx, connConfig) +} + // ConnectConfig establishes a connection with a PostgreSQL server with a configuration struct. // connConfig must have been created by ParseConfig. func ConnectConfig(ctx context.Context, connConfig *ConnConfig) (*Conn, error) { @@ -120,22 +135,10 @@ func ConnectConfig(ctx context.Context, connConfig *ConnConfig) (*Conn, error) { return connect(ctx, connConfig) } -// ParseConfig creates a ConnConfig from a connection string. ParseConfig handles all options that pgconn.ParseConfig -// does. In addition, it accepts the following options: -// -// default_query_exec_mode -// Possible values: "cache_statement", "cache_describe", "describe_exec", "exec", and "simple_protocol". See -// QueryExecMode constant documentation for the meaning of these values. Default: "cache_statement". -// -// statement_cache_capacity -// The maximum size of the statement cache used when executing a query with "cache_statement" query exec mode. -// Default: 512. -// -// description_cache_capacity -// The maximum size of the description cache used when executing a query with "cache_describe" query exec mode. -// Default: 512. -func ParseConfig(connString string) (*ConnConfig, error) { - config, err := pgconn.ParseConfig(connString) +// ParseConfigWithOptions behaves exactly as ParseConfig does with the addition of options. At the present options is +// only used to provide a GetSSLPassword function. +func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*ConnConfig, error) { + config, err := pgconn.ParseConfigWithOptions(connString, options.ParseConfigOptions) if err != nil { return nil, err } @@ -191,6 +194,24 @@ func ParseConfig(connString string) (*ConnConfig, error) { return connConfig, nil } +// ParseConfig creates a ConnConfig from a connection string. ParseConfig handles all options that pgconn.ParseConfig +// does. In addition, it accepts the following options: +// +// default_query_exec_mode +// Possible values: "cache_statement", "cache_describe", "describe_exec", "exec", and "simple_protocol". See +// QueryExecMode constant documentation for the meaning of these values. Default: "cache_statement". +// +// statement_cache_capacity +// The maximum size of the statement cache used when executing a query with "cache_statement" query exec mode. +// Default: 512. +// +// description_cache_capacity +// The maximum size of the description cache used when executing a query with "cache_describe" query exec mode. +// Default: 512. +func ParseConfig(connString string) (*ConnConfig, error) { + return ParseConfigWithOptions(connString, ParseConfigOptions{}) +} + // connect connects to a database. connect takes ownership of config. The caller must not use or access it again. func connect(ctx context.Context, config *ConnConfig) (c *Conn, err error) { if connectTracer, ok := config.Tracer.(ConnectTracer); ok { diff --git a/pgconn/config.go b/pgconn/config.go index cff4bed0..6282f41b 100644 --- a/pgconn/config.go +++ b/pgconn/config.go @@ -64,7 +64,7 @@ type Config struct { createdByParseConfig bool // Used to enforce created by ParseConfig rule. } -// ParseConfigOptions contains options that control how a config is built such as getsslpassword. +// ParseConfigOptions contains options that control how a config is built such as GetSSLPassword. type ParseConfigOptions struct { // GetSSLPassword gets the password to decrypt a SSL client certificate. This is analogous to the the libpq function // PQsetSSLKeyPassHook_OpenSSL.