Skip to content

Commit

Permalink
pg connector pass client options
Browse files Browse the repository at this point in the history
pg connector sends all client options in startup message. options specified in secretless config take precendence over those sent by the client
  • Loading branch information
doodlesbykumbi authored Dec 29, 2021
1 parent a5591d8 commit 8a2131b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/plugin/connectors/tcp/pg/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/plugin/connectors/tcp/pg/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions internal/plugin/connectors/tcp/pg/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
15 changes: 15 additions & 0 deletions test/connector/tcp/pg/test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 8a2131b

Please sign in to comment.