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

fix: force use of TLSv1.3 when IAM auth enabled #108

Merged
merged 2 commits into from
May 26, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions google/cloud/sql/connector/instance_connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
super(ConnectionSSLContext, self).__init__(*args, **kwargs)


class TLSVersionError(Exception):
"""
Raised when the required TLS protocol version is not supported.
"""

def __init__(self, *args: Any) -> None:
super(TLSVersionError, self).__init__(self, *args)


class CloudSQLConnectionError(Exception):
"""
Raised when the provided connection string is not formatted
Expand Down Expand Up @@ -111,8 +120,16 @@ def __init__(
private_key: bytes,
server_ca_cert: str,
expiration: datetime.datetime,
enable_iam_auth: bool,
) -> None:
self.ip_addrs = ip_addrs

if enable_iam_auth and not ssl.HAS_TLSv1_3:
raise TLSVersionError(
"Your current version of OpenSSL does not support TLSv1.3, "
"which is required to use IAM Authentication."
)

self.context = ConnectionSSLContext()
self.expiration = expiration

Expand Down Expand Up @@ -293,18 +310,20 @@ async def _get_instance_data(self) -> InstanceMetadata:
expiration = datetime.datetime.strptime(
x509.get_notAfter().decode("ascii"), "%Y%m%d%H%M%SZ"
)
if self._credentials is not None:
token_expiration: datetime.datetime = self._credentials.expiry

if expiration > token_expiration:
expiration = token_expiration
if self._enable_iam_auth:
if self._credentials is not None:
token_expiration: datetime.datetime = self._credentials.expiry
if expiration > token_expiration:
expiration = token_expiration

return InstanceMetadata(
ephemeral_cert,
metadata["ip_addresses"],
priv_key,
metadata["server_ca_cert"],
expiration,
self._enable_iam_auth,
)

def _auth_init(self) -> None:
Expand Down