From b2eea99f468ec8be0f0580f8e0e07a47661b17ee Mon Sep 17 00:00:00 2001 From: Ivan Zubenko Date: Wed, 18 Sep 2024 20:34:50 +0200 Subject: [PATCH] make s3 credentials optional --- platform_monitoring/config.py | 4 ++-- platform_monitoring/config_factory.py | 4 ++-- tests/unit/test_config.py | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/platform_monitoring/config.py b/platform_monitoring/config.py index 42913503..d56de35d 100644 --- a/platform_monitoring/config.py +++ b/platform_monitoring/config.py @@ -37,9 +37,9 @@ class ElasticsearchConfig: @dataclass(frozen=True) class S3Config: region: str - access_key_id: str = field(repr=False) - secret_access_key: str = field(repr=False) job_logs_bucket_name: str + access_key_id: str | None = field(default=None, repr=False) + secret_access_key: str | None = field(default=None, repr=False) endpoint_url: URL | None = None diff --git a/platform_monitoring/config_factory.py b/platform_monitoring/config_factory.py index 9b2d7631..b5718d72 100644 --- a/platform_monitoring/config_factory.py +++ b/platform_monitoring/config_factory.py @@ -82,8 +82,8 @@ def _create_s3(self) -> S3Config | None: endpoint_url = self._environ.get("NP_MONITORING_S3_ENDPOINT_URL", "") return S3Config( region=self._environ.get("NP_MONITORING_S3_REGION", ""), - access_key_id=self._environ["NP_MONITORING_S3_ACCESS_KEY_ID"], - secret_access_key=self._environ["NP_MONITORING_S3_SECRET_ACCESS_KEY"], + access_key_id=self._environ.get("NP_MONITORING_S3_ACCESS_KEY_ID"), + secret_access_key=self._environ.get("NP_MONITORING_S3_SECRET_ACCESS_KEY"), endpoint_url=URL(endpoint_url) if endpoint_url else None, job_logs_bucket_name=self._environ["NP_MONITORING_S3_JOB_LOGS_BUCKET_NAME"], ) diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 1fcc77d6..3d12ad71 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -123,6 +123,25 @@ def test_create_with_kubernetes_labels(environ: dict[str, Any]) -> None: assert config.kube.node_pool_label == "node-pool" +def test_create_with_s3__default(environ: dict[str, Any]) -> None: + environ["NP_MONITORING_S3_REGION"] = "us-east-1" + environ["NP_MONITORING_S3_JOB_LOGS_BUCKET_NAME"] = "logs" + + config = EnvironConfigFactory(environ).create() + + assert config.s3 == S3Config( + region="us-east-1", + job_logs_bucket_name="logs", + ) + + environ["NP_MONITORING_S3_ENDPOINT_URL"] = "http://minio:9000" + + config = EnvironConfigFactory(environ).create() + + assert config.s3 + assert config.s3.endpoint_url == URL("http://minio:9000") + + def test_create_with_s3(environ: dict[str, Any]) -> None: environ["NP_MONITORING_S3_REGION"] = "us-east-1" environ["NP_MONITORING_S3_ACCESS_KEY_ID"] = "access_key"