Skip to content

Commit 5415654

Browse files
committed
allow empty or invalid env credentials
On environments with no or invalid application credentials, the JDBC driver would throw an IllegalArgumentException when a connection was being created without an explicit credentials URL included in the connection string.
1 parent 1f1c514 commit 5415654

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

google-cloud-clients/google-cloud-contrib/google-cloud-spanner-jdbc/src/main/java/com/google/cloud/spanner/jdbc/ConnectionOptions.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,23 @@ private ConnectionOptions(Builder builder) {
370370
+ matcher.group(Builder.HOST_GROUP);
371371
this.instanceId = matcher.group(Builder.INSTANCE_GROUP);
372372
this.databaseName = matcher.group(Builder.DATABASE_GROUP);
373-
this.credentials =
374-
builder.credentials == null
375-
? getCredentialsService().createCredentials(this.credentialsUrl)
376-
: builder.credentials;
373+
Credentials credentials = null;
374+
if (builder.credentials == null) {
375+
try {
376+
credentials = getCredentialsService().createCredentials(this.credentialsUrl);
377+
} catch (Exception e) {
378+
// Ignore the error if the user did not set a credentials URL.
379+
// The error is then caused by an invalid environment configuration
380+
// and should not be thrown here.
381+
if (this.credentialsUrl != null) {
382+
throw e;
383+
}
384+
credentials = null;
385+
}
386+
} else {
387+
credentials = builder.credentials;
388+
}
389+
this.credentials = credentials;
377390
String numChannelsValue = parseNumChannels(builder.uri);
378391
if (numChannelsValue != null) {
379392
try {

google-cloud-clients/google-cloud-contrib/google-cloud-spanner-jdbc/src/main/java/com/google/cloud/spanner/jdbc/SpannerPool.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,10 @@ Spanner createSpanner(SpannerPoolKey key) {
277277
builder
278278
.setClientLibToken(MoreObjects.firstNonNull(key.userAgent, JdbcDriver.getClientLibToken()))
279279
.setHost(key.host)
280-
.setProjectId(key.projectId)
281-
.setCredentials(key.credentials);
280+
.setProjectId(key.projectId);
281+
if (key.credentials != null) {
282+
builder.setCredentials(key.credentials);
283+
}
282284
if (key.numChannels != null) {
283285
builder.setNumChannels(key.numChannels);
284286
}

0 commit comments

Comments
 (0)