diff --git a/resource_sample_py/pyproject.toml b/resource_sample_py/pyproject.toml index c8fc4f4..ee6c5ee 100644 --- a/resource_sample_py/pyproject.toml +++ b/resource_sample_py/pyproject.toml @@ -14,7 +14,7 @@ packages = [ python = "^3.8" protobuf = "^3.2.0" PyYAML = "^6.0" -temporal-lib-py = "^1.3.1" +temporal-lib-py = "^1.4.3" python-json-logger = "^2.0.4" urllib3 = "^1.26.16" diff --git a/src/charm.py b/src/charm.py index 9533710..32ca7ec 100755 --- a/src/charm.py +++ b/src/charm.py @@ -278,9 +278,22 @@ def _update(self, event): # noqa: C901 context.update({key: value}) context.update( - {convert_env_var(key): value for key, value in self.config.items() if key not in ["environment"]} + { + convert_env_var(key, prefix="TWC_"): value + for key, value in self.config.items() + if key not in ["environment"] + } ) - context.update({"TWC_PROMETHEUS_PORT": PROMETHEUS_PORT}) + + context.update( + { + convert_env_var(key, prefix="TEMPORAL_"): value + for key, value in self.config.items() + if key not in ["environment"] + } + ) + + context.update({"TWC_PROMETHEUS_PORT": PROMETHEUS_PORT, "TEMPORAL_PROMETHEUS_PORT": PROMETHEUS_PORT}) pebble_layer = { "summary": "temporal worker layer", diff --git a/src/environment_processors.py b/src/environment_processors.py index 63ae18d..c222208 100644 --- a/src/environment_processors.py +++ b/src/environment_processors.py @@ -113,6 +113,10 @@ def process_vault_variables(charm, parsed_environment_data): raise ValueError(f"Unable to read vault secret `{from_key}` at path `{path}`: {e}") from e charm_env.update({key_name: secret}) + for key in charm_env: + if key.startswith("TEMPORAL_") or key.startswith("TWC_"): + raise ValueError("Environment variables cannot use reserved prefix 'TEMPORAL_' or 'TWC_'") + return charm_env diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index 94ea176..bb6db98 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -6,6 +6,7 @@ import asyncio import logging +import os import time from datetime import timedelta from pathlib import Path @@ -133,19 +134,26 @@ def unseal_vault(client, endpoint: str, root_token: str, unseal_key: str): client.sys.submit_unseal_key(unseal_key) -async def run_sample_workflow(ops_test: OpsTest, workflow_type=None): +async def run_sample_workflow(ops_test: OpsTest, workflow_type=None, use_env_variables=False): """Connect to a client and runs a basic Temporal workflow. Args: ops_test: PyTest object. workflow_type: set to "vault" to test Vault workflow. + use_env_variables: whether to initialize Temporal client using environment variables or declared parameters. """ url = await get_application_url(ops_test, application=APP_NAME_SERVER, port=7233) logger.info("running workflow on app address: %s", url) - client = await Client.connect( - Options(host=url, queue=BASE_WORKER_CONFIG["queue"], namespace=BASE_WORKER_CONFIG["namespace"]) - ) + if use_env_variables: + os.environ["TEMPORAL_HOST"] = url + os.environ["TEMPORAL_NAMESPACE"] = BASE_WORKER_CONFIG["namespace"] + os.environ["TEMPORAL_QUEUE"] = BASE_WORKER_CONFIG["queue"] + client = await Client.connect(Options()) + else: + client = await Client.connect( + Options(host=url, queue=BASE_WORKER_CONFIG["queue"], namespace=BASE_WORKER_CONFIG["namespace"]) + ) workflow_name = "GreetingWorkflow" if workflow_type == "vault": diff --git a/tests/integration/test_charm.py b/tests/integration/test_charm.py index ace750b..4e385c0 100644 --- a/tests/integration/test_charm.py +++ b/tests/integration/test_charm.py @@ -21,4 +21,5 @@ class TestDeployment: async def test_basic_client(self, ops_test: OpsTest): """Connects a client and runs a basic Temporal workflow.""" - await run_sample_workflow(ops_test) + await run_sample_workflow(ops_test, use_env_variables=False) + await run_sample_workflow(ops_test, use_env_variables=True) diff --git a/tests/unit/literals.py b/tests/unit/literals.py index d5ecd55..e351d8c 100644 --- a/tests/unit/literals.py +++ b/tests/unit/literals.py @@ -43,6 +43,34 @@ } WANT_ENV = { + "TEMPORAL_AUTH_PROVIDER": "candid", + "TEMPORAL_CANDID_PRIVATE_KEY": "test-private-key", + "TEMPORAL_CANDID_PUBLIC_KEY": "test-public-key", + "TEMPORAL_CANDID_URL": "test-url", + "TEMPORAL_CANDID_USERNAME": "test-username", + "TEMPORAL_ENCRYPTION_KEY": "", + "TEMPORAL_HOST": "test-host", + "TEMPORAL_LOG_LEVEL": "debug", + "TEMPORAL_NAMESPACE": "test-namespace", + "TEMPORAL_PROMETHEUS_PORT": 9000, + "TEMPORAL_OIDC_AUTH_CERT_URL": "", + "TEMPORAL_OIDC_AUTH_TYPE": "", + "TEMPORAL_OIDC_AUTH_URI": "", + "TEMPORAL_OIDC_CLIENT_CERT_URL": "", + "TEMPORAL_OIDC_CLIENT_EMAIL": "", + "TEMPORAL_OIDC_CLIENT_ID": "", + "TEMPORAL_OIDC_PRIVATE_KEY": "", + "TEMPORAL_OIDC_PRIVATE_KEY_ID": "", + "TEMPORAL_OIDC_PROJECT_ID": "", + "TEMPORAL_OIDC_TOKEN_URI": "", + "TEMPORAL_QUEUE": "test-queue", + "TEMPORAL_SENTRY_DSN": "", + "TEMPORAL_SENTRY_ENVIRONMENT": "", + "TEMPORAL_SENTRY_RELEASE": "", + "TEMPORAL_SENTRY_SAMPLE_RATE": 1.0, + "TEMPORAL_SENTRY_REDACT_PARAMS": False, + "TEMPORAL_TLS_ROOT_CAS": "", + # The below are kept for backwards-compatibility "TWC_AUTH_PROVIDER": "candid", "TWC_CANDID_PRIVATE_KEY": "test-private-key", "TWC_CANDID_PUBLIC_KEY": "test-public-key", diff --git a/tox.ini b/tox.ini index 3acca1b..842c609 100644 --- a/tox.ini +++ b/tox.ini @@ -62,7 +62,7 @@ commands = isort --check-only --diff {[vars]all_path} black --check --diff {[vars]all_path} mypy {[vars]all_path} --ignore-missing-imports --install-types --non-interactive --explicit-package-bases - pylint {[vars]all_path} --disable=E0401,W1203,W0613,W0718,R0903,W1514,C0301,C0103,C0104,R0914,R0915,R0801,W0212,W0719,W0511,R0913,R0912,R0902 + pylint {[vars]all_path} --disable=E0401,W1203,W0613,W0718,R0903,W1514,C0301,C0103,C0104,R0914,R0915,R0801,W0212,W0719,W0511,R0913,R0912,R0902,R0917 [testenv:unit] description = Run unit tests @@ -102,7 +102,7 @@ deps = juju==3.5.2.0 pytest==7.1.3 pytest-operator==0.31.1 - temporal-lib-py==1.3.1 + temporal-lib-py==1.4.3 pytest-asyncio==0.21 poetry==1.8.3 -r{toxinidir}/requirements.txt @@ -118,7 +118,7 @@ deps = juju==3.5.2.0 pytest==7.1.3 pytest-operator==0.31.1 - temporal-lib-py==1.3.1 + temporal-lib-py==1.4.3 pytest-asyncio==0.21 poetry==1.8.3 hvac==2.3.0