Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit d701eaf

Browse files
Only read TLS CA once (#281)
## What is the goal of this PR? We modify the cluster server client so that it only reads the TLS root CA once (if provided), and reuses the SSL credentials when prompted for a new channel. ## What are the changes implemented in this PR? Drive-by: don't crash trying to access auth token before the stub is created. Closes #280. Closes #273.
1 parent 773fcbd commit d701eaf

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

typedb/connection/cluster/server_client.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class _ClusterServerClient(_TypeDBClientImpl):
3333
def __init__(self, address: str, credential: TypeDBCredential, parallelisation: int = 2):
3434
super(_ClusterServerClient, self).__init__(address, parallelisation)
3535
self._credential = credential
36+
if self._credential.tls_root_ca_path() is not None:
37+
with open(self._credential.tls_root_ca_path(), 'rb') as root_ca:
38+
self._channel_credentials = grpc.ssl_channel_credentials(root_ca.read())
39+
else:
40+
self._channel_credentials = grpc.ssl_channel_credentials()
3641
self._channel, self._stub = self.new_channel_and_stub()
3742
self._databases = _TypeDBDatabaseManagerImpl(self.stub())
3843

@@ -50,14 +55,12 @@ def new_channel_and_stub(self) -> (grpc.Channel, _ClusterServerStub):
5055
return channel, _ClusterServerStub(channel, self._credential)
5156

5257
def _new_channel(self) -> grpc.Channel:
53-
if self._credential.tls_root_ca_path() is not None:
54-
with open(self._credential.tls_root_ca_path(), 'rb') as root_ca:
55-
channel_credentials = grpc.ssl_channel_credentials(root_ca.read())
56-
else:
57-
channel_credentials = grpc.ssl_channel_credentials()
5858
combined_credentials = grpc.composite_channel_credentials(
59-
channel_credentials,
60-
grpc.metadata_call_credentials(_CredentialAuth(credential=self._credential, token_fn=lambda: self._stub.token()))
59+
self._channel_credentials,
60+
grpc.metadata_call_credentials(_CredentialAuth(
61+
credential=self._credential,
62+
token_fn=lambda: None if self._stub is None else self._stub.token()
63+
))
6164
)
6265
return grpc.secure_channel(self._address, combined_credentials)
6366

0 commit comments

Comments
 (0)