From 773ff030b78cc1b0a57fcc1de8e3324f63b9dc59 Mon Sep 17 00:00:00 2001 From: Tom Wheeler Date: Mon, 10 Feb 2025 17:36:02 -0600 Subject: [PATCH 1/3] added class to configure a client based on environment variables --- client_provider.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 client_provider.py diff --git a/client_provider.py b/client_provider.py new file mode 100644 index 0000000..0741579 --- /dev/null +++ b/client_provider.py @@ -0,0 +1,36 @@ +import os +from temporalio.client import Client, TLSConfig + +async def get_temporal_client() -> Client: + cert_path = os.getenv("TEMPORAL_TLS_CERT") + key_path = os.getenv("TEMPORAL_TLS_KEY") + api_key = os.getenv("TEMPORAL_API_KEY") + + # Check for mTLS authentication + if cert_path and key_path: + with open(cert_path, "rb") as f: + client_cert = f.read() + with open(key_path, "rb") as f: + client_key = f.read() + + return await Client.connect( + os.getenv("TEMPORAL_ADDRESS"), + namespace=os.getenv("TEMPORAL_NAMESPACE"), + tls=TLSConfig( + client_cert=client_cert, + client_private_key=client_key, + ), + ) + elif api_key: + return await Client.connect( + os.getenv("TEMPORAL_ADDRESS"), + namespace=os.getenv("TEMPORAL_NAMESPACE"), + api_key=api_key, + tls=True, + ) + + else: + return await Client.connect( + os.getenv("TEMPORAL_ADDRESS", "localhost:7233"), + namespace=os.getenv("TEMPORAL_NAMESPACE", "default"), + ) From 2d20d3b982ed55de43d4e23270a7c1945b368560 Mon Sep 17 00:00:00 2001 From: Tom Wheeler Date: Mon, 10 Feb 2025 17:36:30 -0600 Subject: [PATCH 2/3] updated to use the environment-based client configuration --- run_worker.py | 4 +++- run_workflow.py | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/run_worker.py b/run_worker.py index 0aa9f8f..c7ea51b 100644 --- a/run_worker.py +++ b/run_worker.py @@ -4,13 +4,15 @@ from temporalio.client import Client from temporalio.worker import Worker +from client_provider import get_temporal_client from activities import BankingActivities from shared import MONEY_TRANSFER_TASK_QUEUE_NAME from workflows import MoneyTransfer async def main() -> None: - client: Client = await Client.connect("localhost:7233", namespace="default") + #client: Client = await Client.connect("localhost:7233", namespace="default") + client = await get_temporal_client() # Run the worker activities = BankingActivities() worker: Worker = Worker( diff --git a/run_workflow.py b/run_workflow.py index 1e53a3c..4363c77 100644 --- a/run_workflow.py +++ b/run_workflow.py @@ -4,13 +4,13 @@ from temporalio.client import Client, WorkflowFailureError +from client_provider import get_temporal_client from shared import MONEY_TRANSFER_TASK_QUEUE_NAME, PaymentDetails from workflows import MoneyTransfer async def main() -> None: - # Create client connected to server at the given address - client: Client = await Client.connect("localhost:7233") + client = await get_temporal_client() data: PaymentDetails = PaymentDetails( source_account="85-150", From f8ddd23a301baa6bf012f044d4521e47280d77f0 Mon Sep 17 00:00:00 2001 From: Tom Wheeler Date: Fri, 21 Mar 2025 17:59:55 -0500 Subject: [PATCH 3/3] removed commented-out line of code --- run_worker.py | 1 - 1 file changed, 1 deletion(-) diff --git a/run_worker.py b/run_worker.py index c7ea51b..607997d 100644 --- a/run_worker.py +++ b/run_worker.py @@ -11,7 +11,6 @@ async def main() -> None: - #client: Client = await Client.connect("localhost:7233", namespace="default") client = await get_temporal_client() # Run the worker activities = BankingActivities()