From ac1bd72f1c151edcdf2197d97c75e6764bf94317 Mon Sep 17 00:00:00 2001 From: Olly Levett Date: Thu, 9 May 2019 11:10:58 +0100 Subject: [PATCH] Add jdbcUrl to postgres binding (#698) * Add jdbcUrl to postgres binding * query escape password in connection strings * run through gofmt --- docs/modules/postgresql.md | 6 ++++-- pkg/services/postgresql/common_bind.go | 21 ++++++++++++++++++++- pkg/services/postgresql/common_types.go | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/modules/postgresql.md b/docs/modules/postgresql.md index 400b37e5a..d16d118b1 100644 --- a/docs/modules/postgresql.md +++ b/docs/modules/postgresql.md @@ -175,8 +175,9 @@ Binding returns the following connection details and credentials: | `database` | `string` | The name of the database. | | `username` | `string` | The name of the database user (in the form username@host). | | `password` | `string` | The password for the database user. | -| `sslRequired` | `boolean` | Flag indicating if SSL is required to connect the MySQL DBMS. | +| `sslRequired` | `boolean` | Flag indicating if SSL is required to connect the PostgreSQL DBMS. | | `uri` | `string` | A URI string containing all necessary connection information. | +| `jdbcUrl` | `string` | A fully formed JDBC url. | | `tags` | `string[]` | A list of tags consumers can use to identify the credential. | ##### Unbind @@ -465,8 +466,9 @@ Binding returns the following connection details and credentials: | `database` | `string` | The name of the database. | | `username` | `string` | The name of the database user (in the form username@host). | | `password` | `string` | The password for the database user. | -| `sslRequired` | `boolean` | Flag indicating if SSL is required to connect the MySQL DBMS. | +| `sslRequired` | `boolean` | Flag indicating if SSL is required to connect the PostgreSQL DBMS. | | `uri` | `string` | A URI string containing all necessary connection information. | +| `jdbcUrl` | `string` | A fully formed JDBC url. | | `tags` | `string[]` | A list of tags consumers can use to identify the credential. | ##### Unbind diff --git a/pkg/services/postgresql/common_bind.go b/pkg/services/postgresql/common_bind.go index 938ccbc01..53cf4001b 100644 --- a/pkg/services/postgresql/common_bind.go +++ b/pkg/services/postgresql/common_bind.go @@ -105,11 +105,29 @@ func createCredential( connectionString := fmt.Sprintf( connectionTemplate, url.QueryEscape(username), - string(bindDetails.Password), + url.QueryEscape(string(bindDetails.Password)), fqdn, port, databaseName, ) + + var jdbcTemplate string + if sslRequired { + jdbcTemplate = + "jdbc:postgresql://%s:%d/%s?user=%s&password=%s&sslmode=require" + } else { + jdbcTemplate = "jdbc:postgresql://%s:%d/%s?user=%s&password=%s" + } + + jdbc := fmt.Sprintf( + jdbcTemplate, + fqdn, + port, + databaseName, + url.QueryEscape(username), + url.QueryEscape(string(bindDetails.Password)), + ) + return credentials{ Host: fqdn, Port: port, @@ -118,6 +136,7 @@ func createCredential( Password: string(bindDetails.Password), SSLRequired: sslRequired, URI: connectionString, + JDBC: jdbc, Tags: []string{"postgresql"}, } } diff --git a/pkg/services/postgresql/common_types.go b/pkg/services/postgresql/common_types.go index eb9dd9536..5eddec81c 100644 --- a/pkg/services/postgresql/common_types.go +++ b/pkg/services/postgresql/common_types.go @@ -14,6 +14,7 @@ type credentials struct { Username string `json:"username"` Password string `json:"password"` URI string `json:"uri"` + JDBC string `json:"jdbcUrl"` SSLRequired bool `json:"sslRequired"` Tags []string `json:"tags"` }