-
Notifications
You must be signed in to change notification settings - Fork 8
chore: support static connection info #406
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cd6df85
feat: support static connection info
rhatgadkar-goog 7d8e3f3
Add support for static.json with no PSC config
rhatgadkar-goog a8e5780
Remove static_conn_info from connect() and fix lint issues
rhatgadkar-goog a59cd2f
Change Tuple to tuple
rhatgadkar-goog 7f0e89c
Change List to list
rhatgadkar-goog 61660c3
Fix lint errors using black
rhatgadkar-goog aac315c
Address lint issues with imports
rhatgadkar-goog 5abf212
Fix lint errors for imports
rhatgadkar-goog 6b3290c
Fix lint issues related to ordering of imports
rhatgadkar-goog 30505c2
Remove Generator from conftest.py
rhatgadkar-goog 1ff96c7
fix lint errors
rhatgadkar-goog 5d9c473
Address PR comments
rhatgadkar-goog 171e6e1
Add unit tests for StaticConnectionInfoCache
rhatgadkar-goog 0d75bd9
Address PR comments
rhatgadkar-goog 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
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
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
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
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,102 @@ | ||
| # Copyright 2025 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. | ||
|
|
||
| from datetime import datetime | ||
| from datetime import timedelta | ||
| from datetime import timezone | ||
| import io | ||
| import json | ||
|
|
||
| from cryptography.hazmat.primitives import serialization | ||
|
|
||
| from google.cloud.alloydb.connector.connection_info import ConnectionInfo | ||
|
|
||
|
|
||
| class StaticConnectionInfoCache: | ||
| """ | ||
| StaticConnectionInfoCache creates a connection info cache that will always | ||
| return a pre-defined connection info. This is a *dev-only* option and | ||
| should not be used in production as it will result in failed connections | ||
| after the client certificate expires. It is also subject to breaking changes | ||
| in the format. NOTE: The static connection info is not refreshed by the | ||
| connector. The JSON format supports multiple instances, regardless of | ||
| cluster. | ||
|
|
||
| This static connection info should hold JSON with the following format: | ||
| { | ||
| "publicKey": "<PEM Encoded public RSA key>", | ||
| "privateKey": "<PEM Encoded private RSA key>", | ||
| "projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>": { | ||
| "ipAddress": "<PSA-based private IP address>", | ||
| "publicIpAddress": "<public IP address>", | ||
| "pscInstanceConfig": { | ||
| "pscDnsName": "<PSC DNS name>" | ||
| }, | ||
| "pemCertificateChain": [ | ||
| "<client cert>", "<intermediate cert>", "<CA cert>" | ||
| ], | ||
| "caCert": "<CA cert>" | ||
| } | ||
| } | ||
| """ | ||
|
|
||
| def __init__(self, instance_uri: str, static_conn_info: io.TextIOBase) -> None: | ||
| """ | ||
| Initializes a StaticConnectionInfoCache instance. | ||
|
|
||
| Args: | ||
| instance_uri (str): The AlloyDB instance's connection URI. | ||
| static_conn_info (io.TextIOBase): The static connection info JSON. | ||
| """ | ||
| static_info = json.load(static_conn_info) | ||
| ca_cert = static_info[instance_uri]["caCert"] | ||
| cert_chain = static_info[instance_uri]["pemCertificateChain"] | ||
| dns = "" | ||
| if static_info[instance_uri]["pscInstanceConfig"]: | ||
| dns = static_info[instance_uri]["pscInstanceConfig"]["pscDnsName"].rstrip( | ||
| "." | ||
| ) | ||
| ip_addrs = { | ||
| "PRIVATE": static_info[instance_uri]["ipAddress"], | ||
| "PUBLIC": static_info[instance_uri]["publicIpAddress"], | ||
| "PSC": dns, | ||
| } | ||
| expiration = datetime.now(timezone.utc) + timedelta(hours=1) | ||
| priv_key = static_info["privateKey"] | ||
| priv_key_bytes = serialization.load_pem_private_key( | ||
| priv_key.encode("UTF-8"), password=None | ||
| ) | ||
| self._info = ConnectionInfo( | ||
| cert_chain, ca_cert, priv_key_bytes, ip_addrs, expiration | ||
| ) | ||
|
|
||
| async def force_refresh(self) -> None: | ||
| """ | ||
| This is a no-op as the cache holds only static connection information | ||
| and does no refresh. | ||
| """ | ||
| pass | ||
|
|
||
| async def connect_info(self) -> ConnectionInfo: | ||
| """ | ||
| Retrieves ConnectionInfo instance for establishing a secure | ||
| connection to the AlloyDB instance. | ||
| """ | ||
| return self._info | ||
|
|
||
| async def close(self) -> None: | ||
| """ | ||
| This is a no-op. | ||
| """ | ||
| pass |
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,23 @@ | ||
| # Copyright 2025 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. | ||
|
|
||
| import typing | ||
|
|
||
| from google.cloud.alloydb.connector.instance import RefreshAheadCache | ||
| from google.cloud.alloydb.connector.lazy import LazyRefreshCache | ||
| from google.cloud.alloydb.connector.static import StaticConnectionInfoCache | ||
|
|
||
| CacheTypes = typing.Union[ | ||
| RefreshAheadCache, LazyRefreshCache, StaticConnectionInfoCache | ||
| ] |
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
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 |
|---|---|---|
|
|
@@ -16,7 +16,6 @@ | |
| import socket | ||
| import ssl | ||
| from threading import Thread | ||
| from typing import Generator | ||
|
|
||
| from aiofiles.tempfile import TemporaryDirectory | ||
| from mocks import FakeAlloyDBClient | ||
|
|
@@ -27,6 +26,8 @@ | |
|
|
||
| from google.cloud.alloydb.connector.utils import _write_to_file | ||
|
|
||
| DELAY = 1.0 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def credentials() -> FakeCredentials: | ||
|
|
@@ -66,16 +67,16 @@ async def start_proxy_server(instance: FakeInstance) -> None: | |
| # listen for incoming connections | ||
| sock.listen(5) | ||
|
|
||
| while True: | ||
| with context.wrap_socket(sock, server_side=True) as ssock: | ||
| with context.wrap_socket(sock, server_side=True) as ssock: | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is needed to run multiple tests that use the same |
||
| while True: | ||
| conn, _ = ssock.accept() | ||
| metadata_exchange(conn) | ||
| conn.sendall(instance.name.encode("utf-8")) | ||
| conn.close() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def proxy_server(fake_instance: FakeInstance) -> Generator: | ||
| def proxy_server(fake_instance: FakeInstance) -> None: | ||
| """Run local proxy server capable of performing metadata exchange""" | ||
| thread = Thread( | ||
| target=asyncio.run, | ||
|
|
@@ -87,5 +88,4 @@ def proxy_server(fake_instance: FakeInstance) -> Generator: | |
| daemon=True, | ||
| ) | ||
| thread.start() | ||
| yield thread | ||
| thread.join() | ||
| thread.join(DELAY) # add a delay to allow the proxy server to start | ||
rhatgadkar-goog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Oops, something went wrong.
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.