From 8a2131b3f0bc0d6b3921ed9828b090472cd908a2 Mon Sep 17 00:00:00 2001 From: Kumbirai Tanekha Date: Wed, 29 Dec 2021 22:45:05 +0000 Subject: [PATCH] pg connector pass client options pg connector sends all client options in startup message. options specified in secretless config take precendence over those sent by the client --- internal/plugin/connectors/tcp/pg/backend.go | 10 +++++++++- internal/plugin/connectors/tcp/pg/connector.go | 1 + internal/plugin/connectors/tcp/pg/startup.go | 5 +++++ test/connector/tcp/pg/test.sql | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/internal/plugin/connectors/tcp/pg/backend.go b/internal/plugin/connectors/tcp/pg/backend.go index 88f8dea8c..943c69c11 100644 --- a/internal/plugin/connectors/tcp/pg/backend.go +++ b/internal/plugin/connectors/tcp/pg/backend.go @@ -87,11 +87,19 @@ func (s *SingleUseConnector) ConnectToBackend() error { return err } + options := map[string]string{} + for k, v := range s.options { + options[k] = v + } + for k, v := range s.connectionDetails.Options { + options[k] = v + } + startupMessage := protocol.CreateStartupMessage( protocol.ProtocolVersion, s.connectionDetails.Username, s.databaseName, - s.connectionDetails.Options, + options, ) s.backendConn.Write(startupMessage) diff --git a/internal/plugin/connectors/tcp/pg/connector.go b/internal/plugin/connectors/tcp/pg/connector.go index f7d65fef5..c5c6d0bbd 100644 --- a/internal/plugin/connectors/tcp/pg/connector.go +++ b/internal/plugin/connectors/tcp/pg/connector.go @@ -23,6 +23,7 @@ type SingleUseConnector struct { connectionDetails *ConnectionDetails // databaseName is specified by the client application databaseName string + options map[string]string } func (s *SingleUseConnector) abort(err error) { diff --git a/internal/plugin/connectors/tcp/pg/startup.go b/internal/plugin/connectors/tcp/pg/startup.go index da0d17764..70491203a 100644 --- a/internal/plugin/connectors/tcp/pg/startup.go +++ b/internal/plugin/connectors/tcp/pg/startup.go @@ -40,6 +40,11 @@ func (s *SingleUseConnector) Startup() error { return fmt.Errorf("no 'database' found in connect options") } + delete(options, "database") + delete(options, "user") + + s.options = options + return nil } diff --git a/test/connector/tcp/pg/test.sql b/test/connector/tcp/pg/test.sql index bfd599f6f..34de30e3e 100644 --- a/test/connector/tcp/pg/test.sql +++ b/test/connector/tcp/pg/test.sql @@ -3,8 +3,23 @@ CREATE USER test PASSWORD 'test'; CREATE SCHEMA test; CREATE TABLE test.test ( id INTEGER PRIMARY KEY ); +CREATE TABLE test.encodings ( encoding TEXT, value TEXT ); + INSERT INTO test.test VALUES ( generate_series(0, 99999) ); +INSERT INTO test.encodings VALUES ('latin1', 'tést'); --- tést in latin1 GRANT ALL ON SCHEMA test TO test; GRANT ALL ON test.test TO test; +GRANT ALL ON test.encodings TO test; + +-- Run the following command : +-- +-- docker-compose exec -e PGCLIENTENCODING=latin1 test psql -h secretless-dev -p 3318 -d postgres -c "select value from test.encodings where encoding='latin1'" +-- +-- It should yield: +-- +-- value +-- ------- +-- tést +-- (1 row)