-
Notifications
You must be signed in to change notification settings - Fork 137
feat: ability to update credentials on long running client #4371
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
Open
ldetmer
wants to merge
53
commits into
main
Choose a base branch
from
mutable-credentials
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+385
−1
Open
Changes from 9 commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
3556d72
feat: ability to update credentials on long running client
ldetmer 02a00ac
chore: generate libraries at Mon Mar 2 15:00:10 UTC 2026
cloud-java-bot bd89a33
Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/co…
ldetmer e871f0f
fixed comp issue
ldetmer b802f0a
suggestions from gemini review + lint fixes
ldetmer 40abd6d
chore: generate libraries at Mon Mar 2 15:20:24 UTC 2026
cloud-java-bot e5654af
test IT test
ldetmer 28bb07a
remove commented out code
ldetmer 1a302e8
chore: generate libraries at Mon Mar 2 17:08:19 UTC 2026
cloud-java-bot 74995fe
added missing override methods
ldetmer 61468f0
attempt to fix IT tests
ldetmer fc8c682
chore: generate libraries at Tue Mar 3 21:06:51 UTC 2026
cloud-java-bot 2cf5b89
try to use default key file
ldetmer 1751af2
chore: generate libraries at Tue Mar 3 21:25:30 UTC 2026
cloud-java-bot 22a9ba7
try to use hardcoded service account file
ldetmer 80b67bf
chore: generate libraries at Tue Mar 3 22:13:15 UTC 2026
cloud-java-bot b0c1514
change to use resource as stream
ldetmer e920709
Merge branch 'main' into mutable-credentials
ldetmer 910eb82
chore: generate libraries at Wed Mar 4 17:58:06 UTC 2026
cloud-java-bot 816dcfc
change to use correct project Id
ldetmer d3dab34
change to use new api for test
ldetmer 9739af2
chore: generate libraries at Wed Mar 4 18:27:30 UTC 2026
cloud-java-bot 8c690d2
fix instance name
ldetmer 2d70ec5
add invalid test key for IT tests
ldetmer 917cb2d
change test key to be invalid
ldetmer fa7ea61
chore: generate libraries at Wed Mar 4 19:01:08 UTC 2026
cloud-java-bot 1d41b10
working IT test
ldetmer 0378706
need to check error message on kokoro as its different then local
ldetmer 9c4b12b
need to check error message on kokoro as its different then local
ldetmer 89d4789
provide default scopes constructor
ldetmer bb56c65
chore: generate libraries at Thu Mar 5 15:41:33 UTC 2026
cloud-java-bot 52b761b
testing default credential accesss
ldetmer 06103fd
chore: generate libraries at Thu Mar 5 15:59:43 UTC 2026
cloud-java-bot d5fb2f8
testing default credential accesss
ldetmer e5a4c93
chore: generate libraries at Thu Mar 5 16:16:58 UTC 2026
cloud-java-bot 3e71bef
try to disable the nocredentials process
ldetmer bbfb3e7
chore: generate libraries at Thu Mar 5 18:31:08 UTC 2026
cloud-java-bot 2778372
hopefully fixed IT tests
ldetmer 4c5ec92
chore: generate libraries at Thu Mar 5 19:17:19 UTC 2026
cloud-java-bot eb0164e
cleaned up tasks + added sample code
ldetmer bf52fa2
chore: generate libraries at Thu Mar 5 20:28:36 UTC 2026
cloud-java-bot a50eee8
bump sample dependency so code samples compile
ldetmer 8d640ef
chore: generate libraries at Thu Mar 5 21:10:30 UTC 2026
cloud-java-bot 38ddfb4
bump sample dependency so code samples compile
ldetmer 4ed2f0b
chore: generate libraries at Thu Mar 5 21:49:47 UTC 2026
cloud-java-bot 09c1e63
updates from PR review
ldetmer 8805dd7
chore: generate libraries at Fri Mar 6 14:38:15 UTC 2026
cloud-java-bot 90c6b7b
moved IT test to correct package
ldetmer ef64649
chore: generate libraries at Fri Mar 6 14:42:23 UTC 2026
cloud-java-bot 4119282
remove spanner version in samples
ldetmer 325db5d
chore: generate libraries at Fri Mar 6 15:13:14 UTC 2026
cloud-java-bot 960e41e
remove MutableCredentials to separate PR
ldetmer de735ad
chore: generate libraries at Fri Mar 6 15:23:50 UTC 2026
cloud-java-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...e-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/MutableCredentials.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.google.cloud.spanner.connection; | ||
|
|
||
| import com.google.auth.Credentials; | ||
| import com.google.auth.oauth2.ServiceAccountCredentials; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A mutable {@link Credentials} implementation that delegates authentication behavior to a scoped | ||
| * {@link ServiceAccountCredentials} instance. | ||
| * | ||
| * <p>This class is intended for scenarios where an application needs to replace the underlying | ||
| * service account credentials for a long running Spanner Client. | ||
| * | ||
| * <p>All operations inherited from {@link Credentials} are forwarded to the current delegate, | ||
| * including request metadata retrieval and token refresh. Calling {@link | ||
| * #updateCredentials(ServiceAccountCredentials)} replaces the delegate with a newly scoped | ||
| * credentials instance created from the same scopes that were provided when this object was | ||
| * constructed. | ||
| */ | ||
| public class MutableCredentials extends Credentials { | ||
| private volatile ServiceAccountCredentials delegate; | ||
| private final List<String> scopes; | ||
|
|
||
| public MutableCredentials(ServiceAccountCredentials credentials, List<String> scopes) { | ||
| this.scopes = new java.util.ArrayList<>(scopes); | ||
ldetmer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| delegate = (ServiceAccountCredentials) credentials.createScoped(this.scopes); | ||
| } | ||
|
|
||
| /** | ||
| * Replaces the current delegate with a newly scoped credentials instance. | ||
| * | ||
| * <p>The provided {@link ServiceAccountCredentials} is scoped using the same scopes that were | ||
| * supplied when this {@link MutableCredentials} instance was created. | ||
| * | ||
| * @param credentials the new base service account credentials to scope and use for client | ||
| * authorization. | ||
| */ | ||
| public void updateCredentials(ServiceAccountCredentials credentials) { | ||
| delegate = (ServiceAccountCredentials) credentials.createScoped(scopes); | ||
| } | ||
|
|
||
| @Override | ||
| public String getAuthenticationType() { | ||
| return delegate.getAuthenticationType(); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException { | ||
| return delegate.getRequestMetadata(uri); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasRequestMetadata() { | ||
| return delegate.hasRequestMetadata(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasRequestMetadataOnly() { | ||
| return delegate.hasRequestMetadataOnly(); | ||
| } | ||
|
|
||
| @Override | ||
| public void refresh() throws IOException { | ||
| delegate.refresh(); | ||
| } | ||
| } | ||
108 changes: 108 additions & 0 deletions
108
...oud-spanner/src/test/java/com/google/cloud/spanner/connection/MutableCredentialsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.spanner.connection; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertSame; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.google.auth.oauth2.ServiceAccountCredentials; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class MutableCredentialsTest { | ||
| ServiceAccountCredentials initialCredentials = mock(ServiceAccountCredentials.class); | ||
| ServiceAccountCredentials initialScopedCredentials = mock(ServiceAccountCredentials.class); | ||
| ServiceAccountCredentials updatedCredentials = mock(ServiceAccountCredentials.class); | ||
| ServiceAccountCredentials updatedScopedCredentials = mock(ServiceAccountCredentials.class); | ||
| List<String> scopes = Arrays.asList("scope-a", "scope-b"); | ||
| Map<String, List<String>> initialMetadata = | ||
| Collections.singletonMap("Authorization", Collections.singletonList("v1")); | ||
| Map<String, List<String>> updatedMetadata = | ||
| Collections.singletonMap("Authorization", Collections.singletonList("v2")); | ||
| String initialAuthType = "auth-1"; | ||
| String updatedAuthType = "auth-2"; | ||
|
|
||
| @Test | ||
| public void testCreateMutableCredentials() throws IOException { | ||
| setupInitialCredentials(); | ||
|
|
||
| MutableCredentials credentials = new MutableCredentials(initialCredentials, scopes); | ||
|
|
||
| assertEquals(initialAuthType, credentials.getAuthenticationType()); | ||
| assertTrue(credentials.hasRequestMetadata()); | ||
| assertTrue(credentials.hasRequestMetadataOnly()); | ||
| assertEquals( | ||
| initialMetadata, | ||
| credentials.getRequestMetadata(URI.create("https://spanner.googleapis.com"))); | ||
|
|
||
| credentials.refresh(); | ||
|
|
||
| verify(initialScopedCredentials, times(1)).refresh(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUpdateMutableCredentials() throws IOException { | ||
| setupInitialCredentials(); | ||
| setupUpdatedCredentials(); | ||
|
|
||
| MutableCredentials credentials = new MutableCredentials(initialCredentials, scopes); | ||
|
|
||
| assertEquals(initialAuthType, credentials.getAuthenticationType()); | ||
|
|
||
| credentials.updateCredentials(updatedCredentials); | ||
|
|
||
| assertEquals(updatedAuthType, credentials.getAuthenticationType()); | ||
| assertFalse(credentials.hasRequestMetadata()); | ||
| assertFalse(credentials.hasRequestMetadataOnly()); | ||
| assertSame(updatedMetadata, credentials.getRequestMetadata(URI.create("https://example.com"))); | ||
|
|
||
| credentials.refresh(); | ||
|
|
||
| verify(updatedScopedCredentials, times(1)).refresh(); | ||
| } | ||
|
|
||
| private void setupInitialCredentials() throws IOException { | ||
| when(initialCredentials.createScoped(scopes)).thenReturn(initialScopedCredentials); | ||
| when(initialScopedCredentials.getAuthenticationType()).thenReturn(initialAuthType); | ||
| when(initialScopedCredentials.getRequestMetadata(any(URI.class))).thenReturn(initialMetadata); | ||
| when(initialScopedCredentials.hasRequestMetadata()).thenReturn(true); | ||
| when(initialScopedCredentials.hasRequestMetadataOnly()).thenReturn(true); | ||
| } | ||
|
|
||
| private void setupUpdatedCredentials() throws IOException { | ||
| when(updatedCredentials.createScoped(scopes)).thenReturn(updatedScopedCredentials); | ||
| when(updatedScopedCredentials.getAuthenticationType()).thenReturn(updatedAuthType); | ||
| when(updatedScopedCredentials.getRequestMetadata(any(URI.class))).thenReturn(updatedMetadata); | ||
| when(updatedScopedCredentials.hasRequestMetadata()).thenReturn(false); | ||
| when(updatedScopedCredentials.hasRequestMetadataOnly()).thenReturn(false); | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
...panner/src/test/java/com/google/cloud/spanner/connection/it/ITMutableCredentialsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.spanner.connection.it; | ||
|
|
||
| import static org.junit.Assert.*; | ||
ldetmer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import static org.junit.Assume.assumeTrue; | ||
|
|
||
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import com.google.auth.oauth2.ServiceAccountCredentials; | ||
| import com.google.cloud.spanner.*; | ||
| import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
| import com.google.cloud.spanner.connection.ITAbstractSpannerTest; | ||
| import com.google.cloud.spanner.connection.MutableCredentials; | ||
| import com.google.spanner.admin.database.v1.Database; | ||
| import com.google.spanner.admin.database.v1.DatabaseName; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.junit.Test; | ||
| import org.junit.experimental.categories.Category; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @Category(SerialIntegrationTest.class) | ||
| @RunWith(JUnit4.class) | ||
| public class ITMutableCredentialsTest extends ITAbstractSpannerTest { | ||
| private static final String INVALID_KEY_FILE = | ||
| ITMutableCredentialsTest.class.getResource("test-key.json").getPath(); | ||
|
|
||
| @Test | ||
| public void testMutableCredentialsUpdateAuthorizationForRunningClient() throws IOException { | ||
|
|
||
| GoogleCredentials credentialsFromFile; | ||
| try (InputStream stream = | ||
| Files.newInputStream(Paths.get(GceTestEnvConfig.GCE_CREDENTIALS_FILE))) { | ||
| credentialsFromFile = GoogleCredentials.fromStream(stream); | ||
| } | ||
| assumeTrue( | ||
| "This test requires service account credentials", | ||
| credentialsFromFile instanceof ServiceAccountCredentials); | ||
|
|
||
| ServiceAccountCredentials validCredentials = (ServiceAccountCredentials) credentialsFromFile; | ||
| ServiceAccountCredentials invalidCredentials; | ||
| try (InputStream stream = Files.newInputStream(Paths.get(INVALID_KEY_FILE))) { | ||
| invalidCredentials = ServiceAccountCredentials.fromStream(stream); | ||
| } | ||
|
|
||
| List<String> scopes = new ArrayList<>(getTestEnv().getTestHelper().getOptions().getScopes()); | ||
| MutableCredentials mutableCredentials = new MutableCredentials(validCredentials, scopes); | ||
|
|
||
| SpannerOptions options = SpannerOptions.newBuilder().setCredentials(mutableCredentials).build(); | ||
|
|
||
| try (Spanner spanner = options.getService(); | ||
| DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { | ||
| String dbName = | ||
| DatabaseName.of( | ||
| GceTestEnvConfig.GCE_PROJECT_ID, | ||
| getTestEnv().getTestHelper().getInstanceId().getInstance(), | ||
| "TEST") | ||
| .toString(); | ||
| Database database = databaseAdminClient.getDatabase(dbName); | ||
| assertNotNull(database); | ||
| try { | ||
| mutableCredentials.updateCredentials(invalidCredentials); | ||
| databaseAdminClient.getDatabase(dbName); | ||
| fail("Expected UNAUTHENTICATED after switching to invalid credentials"); | ||
| } catch (SpannerException e) { | ||
| assertEquals(ErrorCode.UNAUTHENTICATED, e.getErrorCode()); | ||
| } | ||
| } finally { | ||
| closeSpanner(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.