-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add lazy refresh strategy to the connector. Fixes #992.
The lazy refresh strategy only refreshes credentials and certificate information when the application attempts to establish a new database connection. On Cloud Run and other serverless runtimes, this is more reliable than the default background refresh strategy. Fixes #992.
- Loading branch information
Showing
5 changed files
with
167 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
61 changes: 61 additions & 0 deletions
61
core/src/main/java/com/google/cloud/sql/core/LazyRefreshConnectionInfoCache.java
This file contains 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,61 @@ | ||
/* | ||
* Copyright 2024 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.sql.core; | ||
|
||
import com.google.cloud.sql.CredentialFactory; | ||
import java.security.KeyPair; | ||
|
||
/** | ||
* Implements the lazy refresh cache strategy, which loads the new certificate as needed during a | ||
* request for a new connection. | ||
*/ | ||
class LazyRefreshConnectionInfoCache extends BaseConnectionInfoCache { | ||
|
||
/** | ||
* Initializes a new Cloud SQL instance based on the given connection name using the lazy refresh | ||
* strategy. | ||
* | ||
* @param config instance connection name in the format "PROJECT_ID:REGION_ID:INSTANCE_ID" | ||
* @param connectionInfoRepository Service class for interacting with the Cloud SQL Admin API | ||
* @param keyPair public/private key pair used to authenticate connections | ||
*/ | ||
public static LazyRefreshConnectionInfoCache newInstance( | ||
ConnectionConfig config, | ||
ConnectionInfoRepository connectionInfoRepository, | ||
CredentialFactory tokenSourceFactory, | ||
KeyPair keyPair) { | ||
|
||
AccessTokenSupplier accessTokenSupplier = | ||
BaseConnectionInfoCache.newAccessTokenSupplier(config, tokenSourceFactory); | ||
CloudSqlInstanceName instanceName = new CloudSqlInstanceName(config.getCloudSqlInstance()); | ||
|
||
LazyRefreshStrategy strategy = | ||
new LazyRefreshStrategy( | ||
config.getCloudSqlInstance(), | ||
() -> | ||
connectionInfoRepository.getConnectionInfoSync( | ||
instanceName, accessTokenSupplier, config.getAuthType(), keyPair)); | ||
return new LazyRefreshConnectionInfoCache(config, accessTokenSupplier, strategy); | ||
} | ||
|
||
public LazyRefreshConnectionInfoCache( | ||
ConnectionConfig config, | ||
AccessTokenSupplier accessTokenSupplier, | ||
RefreshStrategy refreshStrategy) { | ||
super(config, accessTokenSupplier, refreshStrategy); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
core/src/test/java/com/google/cloud/sql/core/LazyRefreshConnectionInfoCacheTest.java
This file contains 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,60 @@ | ||
/* | ||
* Copyright 2024 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.sql.core; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import java.security.KeyPair; | ||
import java.util.concurrent.ExecutionException; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class LazyRefreshConnectionInfoCacheTest { | ||
private ListenableFuture<KeyPair> keyPairFuture; | ||
private final StubCredentialFactory stubCredentialFactory = | ||
new StubCredentialFactory("my-token", System.currentTimeMillis() + 3600L); | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
MockAdminApi mockAdminApi = new MockAdminApi(); | ||
this.keyPairFuture = Futures.immediateFuture(mockAdminApi.getClientKeyPair()); | ||
} | ||
|
||
@Test | ||
public void testCloudSqlInstanceDataLazyStrategyRetrievedSuccessfully() | ||
throws ExecutionException, InterruptedException { | ||
KeyPair kp = keyPairFuture.get(); | ||
TestDataSupplier instanceDataSupplier = new TestDataSupplier(false); | ||
// initialize connectionInfoCache after mocks are set up | ||
LazyRefreshConnectionInfoCache connectionInfoCache = | ||
LazyRefreshConnectionInfoCache.newInstance( | ||
new ConnectionConfig.Builder().withCloudSqlInstance("project:region:instance").build(), | ||
instanceDataSupplier, | ||
stubCredentialFactory, | ||
kp); | ||
|
||
ConnectionMetadata gotMetadata = connectionInfoCache.getConnectionMetadata(300); | ||
|
||
// Assert that the underlying ConnectionInfo was retrieved exactly once. | ||
assertThat(instanceDataSupplier.counter.get()).isEqualTo(1); | ||
|
||
// Assert that the ConnectionInfo fields are added to ConnectionMetadata | ||
assertThat(gotMetadata.getKeyManagerFactory()) | ||
.isSameInstanceAs(instanceDataSupplier.response.getSslData().getKeyManagerFactory()); | ||
} | ||
} |
This file contains 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
This file contains 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