Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Commit

Permalink
fixed JDBC URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlewis42 committed Sep 25, 2019
1 parent 50c51e2 commit d688df2
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/providers/builtin/cloudsql/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ func (b *CloudSQLBroker) BuildInstanceCredentials(ctx context.Context, bindRecor
uriFormat := ""
switch instanceRecord.ServiceId {
case MySqlServiceId:
uriFormat = `${str.queryEscape(UriPrefix)}mysql://${str.queryEscape(Username)}:${str.queryEscape(Password)}@${str.queryEscape(host)}/${str.queryEscape(database_name)}?ssl_mode=required`
uriFormat = `${UriPrefix}mysql://${str.queryEscape(Username)}:${str.queryEscape(Password)}@${str.queryEscape(host)}/${str.queryEscape(database_name)}?ssl_mode=required`
case PostgresServiceId:
uriFormat = `${str.queryEscape(UriPrefix)}postgres://${str.queryEscape(Username)}:${str.queryEscape(Password)}@${str.queryEscape(host)}/${str.queryEscape(database_name)}?sslmode=require&sslcert=${str.queryEscape(ClientCert)}&sslkey=${str.queryEscape(ClientKey)}&sslrootcert=${str.queryEscape(CaCert)}`
uriFormat = `${UriPrefix}postgres://${str.queryEscape(Username)}:${str.queryEscape(Password)}@${str.queryEscape(host)}/${str.queryEscape(database_name)}?sslmode=require&sslcert=${str.queryEscape(ClientCert)}&sslkey=${str.queryEscape(ClientKey)}&sslrootcert=${str.queryEscape(CaCert)}`
default:
return nil, errors.New("Unknown service")
}
Expand Down
135 changes: 135 additions & 0 deletions pkg/providers/builtin/cloudsql/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package cloudsql

import (
"context"
"encoding/json"
"reflect"
"strings"
"testing"

"github.com/GoogleCloudPlatform/gcp-service-broker/db_service/models"
"github.com/GoogleCloudPlatform/gcp-service-broker/pkg/broker"
"github.com/pivotal-cf/brokerapi"
"github.com/spf13/cast"
Expand Down Expand Up @@ -345,3 +348,135 @@ func TestPostgresCustomMachineTypes(t *testing.T) {
})
}
}

func TestBuildInstanceCredentials(t *testing.T) {

cases := map[string]struct {
serviceID string
bindDetails string
instanceDetails string

expectedCreds map[string]interface{}
}{
"no prefix mysql": {
serviceID: MySqlServiceId,
instanceDetails: `{
"database_name": "pcf-sb-2-1543346570614873901",
"host": "35.202.18.12"
}`,
bindDetails: `{
"Username": "sb15433468744175",
"Password": "pass=",
"UriPrefix": ""
}`,
expectedCreds: map[string]interface{}{
"Password": "pass=",
"UriPrefix": "",
"Username": "sb15433468744175",
"database_name": "pcf-sb-2-1543346570614873901",
"host": "35.202.18.12",
"uri": "mysql://sb15433468744175:pass%[email protected]/pcf-sb-2-1543346570614873901?ssl_mode=required",
},
},

"no prefix postgres": {
serviceID: PostgresServiceId,
instanceDetails: `{
"database_name": "pcf-sb-2-1543346570614873901",
"host": "35.202.18.12"
}`,
bindDetails: `{
"ClientCert": "@clientcert",
"ClientKey": "@clientkey",
"CaCert": "@cacert",
"Username": "sb15433468744175",
"Password": "pass=",
"UriPrefix": ""
}`,
expectedCreds: map[string]interface{}{
"ClientCert": "@clientcert",
"ClientKey": "@clientkey",
"CaCert": "@cacert",
"Password": "pass=",
"UriPrefix": "",
"Username": "sb15433468744175",
"database_name": "pcf-sb-2-1543346570614873901",
"host": "35.202.18.12",
"uri": "postgres://sb15433468744175:pass%[email protected]/pcf-sb-2-1543346570614873901?sslmode=require&sslcert=%40clientcert&sslkey=%40clientkey&sslrootcert=%40cacert",
},
},

"prefix mysql": {
serviceID: MySqlServiceId,
instanceDetails: `{
"database_name": "pcf-sb-2-1543346570614873901",
"host": "35.202.18.12"
}`,
bindDetails: `{
"Username": "sb15433468744175",
"Password": "pass=",
"UriPrefix": "jdbc:"
}`,
expectedCreds: map[string]interface{}{
"Password": "pass=",
"UriPrefix": "jdbc:",
"Username": "sb15433468744175",
"database_name": "pcf-sb-2-1543346570614873901",
"host": "35.202.18.12",
"uri": "jdbc:mysql://sb15433468744175:pass%[email protected]/pcf-sb-2-1543346570614873901?ssl_mode=required",
},
},

"prefix postgres": {
serviceID: PostgresServiceId,
instanceDetails: `{
"database_name": "pcf-sb-2-1543346570614873901",
"host": "35.202.18.12"
}`,
bindDetails: `{
"ClientCert": "@clientcert",
"ClientKey": "@clientkey",
"CaCert": "@cacert",
"Username": "sb15433468744175",
"Password": "pass=",
"UriPrefix": "jdbc:"
}`,
expectedCreds: map[string]interface{}{
"ClientCert": "@clientcert",
"ClientKey": "@clientkey",
"CaCert": "@cacert",
"Password": "pass=",
"UriPrefix": "jdbc:",
"Username": "sb15433468744175",
"database_name": "pcf-sb-2-1543346570614873901",
"host": "35.202.18.12",
"uri": "jdbc:postgres://sb15433468744175:pass%[email protected]/pcf-sb-2-1543346570614873901?sslmode=require&sslcert=%40clientcert&sslkey=%40clientkey&sslrootcert=%40cacert",
},
},
}

for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
broker := CloudSQLBroker{}

bindRecord := models.ServiceBindingCredentials{
OtherDetails: tc.bindDetails,
}

instanceRecord := models.ServiceInstanceDetails{
ServiceId: tc.serviceID,
OtherDetails: tc.instanceDetails,
}

binding, err := broker.BuildInstanceCredentials(context.Background(), bindRecord, instanceRecord)
if err != nil {
t.Error("expected no error, got:", err)
return
}

if !reflect.DeepEqual(binding.Credentials, tc.expectedCreds) {
t.Errorf("Expected credentials %#v, got %#v", tc.expectedCreds, binding.Credentials)
}
})
}
}

0 comments on commit d688df2

Please sign in to comment.