From c06f35f6e30d65349d8211576e7b73aacf50409a Mon Sep 17 00:00:00 2001 From: Daniela Plascencia Date: Wed, 24 Jul 2024 20:43:03 +0200 Subject: [PATCH] chore: keep public-url config option for compatibility Since this is a breaking change, it is preferrable to keep some sort of compatibility that allow users with existing deployments to keep running Dex even after the upgrade. This configuration option can be removed after a deprecation notice is rolled out. --- config.yaml | 7 +++++++ src/charm.py | 13 ++++++++++++ tests/unit/test_charm.py | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/config.yaml b/config.yaml index f6ca5201..b46e5086 100644 --- a/config.yaml +++ b/config.yaml @@ -36,3 +36,10 @@ options: type: string default: '' description: Static password for logging in without an external auth service + public-url: + type: string + default: '' + description: | + DEPRECATED - Please leave empty or use issuer-url instead. This configuration option will be removed soon. + It has been preserved to avoid breaking compatibility with existing deployments. + Publicly-accessible endpoint for cluster diff --git a/src/charm.py b/src/charm.py index 5c843180..51394c09 100755 --- a/src/charm.py +++ b/src/charm.py @@ -104,6 +104,19 @@ def _issuer_url(self) -> str: """Return issuer-url value if config option exists; otherwise default Dex's endpoint.""" if issuer_url := self.model.config["issuer-url"]: return issuer_url + # TODO: remove this after the release of dex-auth 2.39 + # This should not be supported anymore, but has been kept for compatibility + if public_url := self.model.config["public-url"]: + self.logger.warning( + "DEPRECATED: The public-url config option is deprecated" + "Please leave empty or use issuer-url;" + "otherwise this may cause unexpected behaviour." + ) + # This will just cover cases like when the URL is my-dex.io:5557, + # not when it starts with a different protocol + if not public_url.startswith(("http://", "https://")): + public_url = f"http://{public_url}" + return f"{public_url}/dex" return ( f"http://{self.model.app.name}.{self._namespace}.svc:{self.model.config['port']}/dex" ) diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index 91e518cb..5d16ee08 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -159,6 +159,7 @@ def test_config_changed(update, harness): "connectors": "connector01", "static-username": "new-user", "static-password": "new-pass", + "public-url": "", } harness.update_config(config_updates) @@ -261,3 +262,46 @@ def test_grpc_relation_with_data_when_data_changes( # Change the port of the service and check the value changes assert provider_rel_data["issuer-url"] == harness.model.config["issuer-url"] + + +@patch("charm.KubernetesServicePatch", lambda *_, **__: None) +@pytest.mark.parametrize( + "issuer_url_config, default_value, expected_result", + ( + (None, True, None), + ("http://my-dex.io:5557/dex", False, "http://my-dex.io:5557/dex"), + ), +) +def test_issuer_url_property_with_issuer_url_config( + issuer_url_config, default_value, expected_result, harness +): + """Test the property returns as expected. + + + The first case assumes the issuer-url config option is not set, thus the default-value + of "http://dex-auth..svc:5556/dex" should be returned; the second case should return + the value set in the config option. + """ + harness.set_leader(True) + harness.update_config({"issuer-url": issuer_url_config}) + harness.begin() + + if default_value: + expected_result = f"http://{harness.model.app.name}.{harness.model.name}.svc:5556/dex" + assert harness.charm._issuer_url == expected_result + + +@patch("charm.KubernetesServicePatch", lambda *_, **__: None) +@pytest.mark.parametrize( + "public_url_config, expected_result", + ( + ("http://my-dex.io:5557", "http://my-dex.io:5557/dex"), + ("my-dex.io:5557", "http://my-dex.io:5557/dex"), + ), +) +def test_issuer_url_property_with_public_url_config(public_url_config, expected_result, harness): + """Test the property returns as expected.""" + harness.set_leader(True) + harness.update_config({"public-url": public_url_config}) + harness.begin() + assert harness.charm._issuer_url == expected_result