Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow users to specify custom credentials #57

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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