Skip to content

Commit

Permalink
fix: allow users to specify custom credentials (#57)
Browse files Browse the repository at this point in the history
The connection string did not extract and use any credentials that the user had set
in the connection string.
  • Loading branch information
olavloite committed Oct 18, 2021
1 parent 09f9a76 commit 1715929
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ const userAgent = "go-sql-spanner/0.1"
// 1. (Optional) Host: The host name and port number to connect to.
// 2. Database name: The database name to connect to in the format `projects/my-project/instances/my-instance/databases/my-database`
// 3. (Optional) Parameters: One or more parameters in the format `name=value`. Multiple entries are separated by `;`.
// The supported parameters are:
// - credentials: File name for the credentials to use. The connection will use the default credentials of the
// environment if no credentials file is specified in the connection string.
// - usePlainText: Boolean that indicates whether the connection should use plain text communication or not. Set this
// to true to connect to local mock servers that do not use SSL.
// - retryAbortsInternally: Boolean that indicates whether the connection should automatically retry aborted errors.
// The default is true.
// Example: `localhost:9010/projects/test-project/instances/test-instance/databases/test-database;usePlainText=true`
var dsnRegExp = regexp.MustCompile("((?P<HOSTGROUP>[\\w.-]+(?:\\.[\\w\\.-]+)*[\\w\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=.]+)/)?projects/(?P<PROJECTGROUP>(([a-z]|[-.:]|[0-9])+|(DEFAULT_PROJECT_ID)))(/instances/(?P<INSTANCEGROUP>([a-z]|[-]|[0-9])+)(/databases/(?P<DATABASEGROUP>([a-z]|[-]|[_]|[0-9])+))?)?(([\\?|;])(?P<PARAMSGROUP>.*))?")

Expand Down Expand Up @@ -150,6 +157,9 @@ func newConnector(d *Driver, dsn string) (*connector, error) {
if connectorConfig.host != "" {
opts = append(opts, option.WithEndpoint(connectorConfig.host))
}
if strval, ok := connectorConfig.params["credentials"]; ok {
opts = append(opts, option.WithCredentialsFile(strval))
}
if strval, ok := connectorConfig.params["useplaintext"]; ok {
if val, err := strconv.ParseBool(strval); err == nil && val {
opts = append(opts, option.WithGRPCDialOption(grpc.WithInsecure()), option.WithoutAuthentication())
Expand Down

0 comments on commit 1715929

Please sign in to comment.