-
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
7 changed files
with
195 additions
and
18 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
79 changes: 79 additions & 0 deletions
79
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,79 @@ | ||
/* | ||
* 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 implements ConnectionInfoCache { | ||
private final ConnectionConfig config; | ||
private final CloudSqlInstanceName instanceName; | ||
|
||
private final LazyRefreshStrategy refreshStrategy; | ||
|
||
/** | ||
* 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 LazyRefreshConnectionInfoCache( | ||
ConnectionConfig config, | ||
ConnectionInfoRepository connectionInfoRepository, | ||
CredentialFactory tokenSourceFactory, | ||
KeyPair keyPair) { | ||
this.config = config; | ||
this.instanceName = new CloudSqlInstanceName(config.getCloudSqlInstance()); | ||
|
||
AccessTokenSupplier accessTokenSupplier = | ||
DefaultAccessTokenSupplier.newInstance(config.getAuthType(), tokenSourceFactory); | ||
CloudSqlInstanceName instanceName = new CloudSqlInstanceName(config.getCloudSqlInstance()); | ||
|
||
this.refreshStrategy = | ||
new LazyRefreshStrategy( | ||
config.getCloudSqlInstance(), | ||
() -> | ||
connectionInfoRepository.getConnectionInfoSync( | ||
instanceName, accessTokenSupplier, config.getAuthType(), keyPair)); | ||
} | ||
|
||
@Override | ||
public ConnectionMetadata getConnectionMetadata(long timeoutMs) { | ||
return refreshStrategy.getConnectionInfo(timeoutMs).toConnectionMetadata(config, instanceName); | ||
} | ||
|
||
@Override | ||
public void forceRefresh() { | ||
refreshStrategy.forceRefresh(); | ||
} | ||
|
||
@Override | ||
public void refreshIfExpired() { | ||
refreshStrategy.refreshIfExpired(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
refreshStrategy.close(); | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
/* | ||
* 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 = | ||
new LazyRefreshConnectionInfoCache( | ||
new ConnectionConfig.Builder().withCloudSqlInstance("project:region:instance").build(), | ||
instanceDataSupplier, | ||
stubCredentialFactory, | ||
kp); | ||
|
||
ConnectionMetadata gotMetadata = connectionInfoCache.getConnectionMetadata(300); | ||
ConnectionMetadata gotMetadata2 = 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()); | ||
assertThat(gotMetadata.getKeyManagerFactory()) | ||
.isSameInstanceAs(gotMetadata2.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
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