Skip to content
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

refactor: move SSL/TLS context creation to ConnectionInfo #1079

Merged
merged 6 commits into from
May 22, 2024

Conversation

jackwotherspoon
Copy link
Collaborator

@jackwotherspoon jackwotherspoon commented May 9, 2024

This refactor moves all SSL/TLS configuration into its own method create_ssl_context() on the ConnectionInfo class.

This way ConnectionInfo() prepares all the info required to connect and then create_ssl_context will use the info to establish the SSL/TLS connection when called from Connector.connect at the time of connection.

This refactor also improves the testing infra to be more in line with AlloyDB Python Connector.

Based on Go: GoogleCloudPlatform/cloud-sql-go-connector#761

@jackwotherspoon jackwotherspoon self-assigned this May 9, 2024
@jackwotherspoon jackwotherspoon requested a review from a team as a code owner May 9, 2024 13:36
Comment on lines +305 to +309
# check that refresh is valid
if not await _is_valid(refresh_task):
raise RefreshNotValidError(
f"['{self._instance_connection_string}']: Invalid refresh operation. Certficate appears to be expired."
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines -88 to -117
class BadRefresh(Exception):
pass


class MockMetadata(ConnectionInfo):
"""Mock class for ConnectionInfo"""

def __init__(
self, expiration: datetime.datetime, ip_addrs: Dict = {"PRIMARY": "0.0.0.0"}
) -> None:
self.expiration = expiration
self.ip_addrs = ip_addrs


async def instance_metadata_success(*args: Any, **kwargs: Any) -> MockMetadata:
return MockMetadata(
datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=10)
)


async def instance_metadata_expired(*args: Any, **kwargs: Any) -> MockMetadata:
return MockMetadata(
datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(minutes=10)
)


async def instance_metadata_error(*args: Any, **kwargs: Any) -> None:
raise BadRefresh("something went wrong...")


Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this legacy mock was hiding a bad test, removed it and now our tests properly rely on our internals.

return await connector(ip_address, instance_data.context, **kwargs)
return await connector(
ip_address,
instance_data.create_ssl_context(enable_iam_auth),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess the downside of this is now we are creating an SSL/TLS context per connection vs in the past where we were creating it once per ConnectionInfo class...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a significant cost associated with creating an SSL context per connection? What would a Python developer expect?

return await connector(ip_address, instance_data.context, **kwargs)
return await connector(
ip_address,
instance_data.create_ssl_context(enable_iam_auth),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a significant cost associated with creating an SSL context per connection? What would a Python developer expect?

@@ -79,33 +81,31 @@ def _from_str(cls, ip_type_str: str) -> IPTypes:
return cls(ip_type_str.upper())


@dataclass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

google/cloud/sql/connector/instance.py Show resolved Hide resolved
Copy link
Member

@enocom enocom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to test that the ConnectionInfo caches its ssl context?

This could be a simple verification of object identity being the same from one call to the next.

google/cloud/sql/connector/instance.py Show resolved Hide resolved
@jackwotherspoon jackwotherspoon merged commit 043c611 into main May 22, 2024
15 checks passed
@jackwotherspoon jackwotherspoon deleted the move-tls-config branch May 22, 2024 20:48
@jackwotherspoon
Copy link
Collaborator Author

Related to #805

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants