diff --git a/README.md b/README.md index 2adfb7f..fb8105b 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ certificates. The subsequent section delineates the pertinent arguments and thei | `--dns-stackit-project-id` | '8a4c68b1-586a-4534-aa0c-9f8c12334a76' | Sets the STACKIT project id if the service account authentication is used. (Recommended)| | `--dns-stackit-service-account` | ./service-account.pem | Denotes the directory path to the STACKIT service account file. (Recommended) | | `--dns-stackit-credentials` | ./credentials.ini | Denotes the directory path to the credentials file for STACKIT DNS. This document must encapsulate the dns_stackit_auth_token and dns_stackit_project_id variables. | +| `--dns-stackit-base-url` | https://dns.api.stackit.cloud | Sets the STACKIT DNS API base URL. (Default: https://dns.api.stackit.cloud) | +| `--dns-stackit-token-url` | https://service-account.api.stackit.cloud/token | Sets the STACKIT service account token endpoint URL when using service account authentication. (Default: https://service-account.api.stackit.cloud/token) | | `--dns-stackit-propagation-seconds` | 900 | Configures the delay prior to initiating the DNS record query. A 900-second interval (equivalent to 15 minutes) is recommended. (Default: 900) | Either the --dns-stackit-credentials flag or the --dns-stackit-service-account and --dns-stackit-project-id flags are mandatory. diff --git a/certbot_dns_stackit/stackit.py b/certbot_dns_stackit/stackit.py index c9eedd6..b4d8bf2 100644 --- a/certbot_dns_stackit/stackit.py +++ b/certbot_dns_stackit/stackit.py @@ -275,6 +275,14 @@ def add_parser_arguments(cls, add: Callable, **kwargs): add("service-account", help="Service account file path") add("credentials", help="STACKIT credentials INI file.") add("project-id", help="STACKIT project ID") + add( + "base-url", + help="STACKIT DNS API base URL (default: https://dns.api.stackit.cloud)", + ) + add( + "token-url", + help="STACKIT service account token URL (default: https://service-account.api.stackit.cloud/token)", + ) def _setup_credentials(self): """Set up and configure the STACKIT credentials based on provided input.""" @@ -319,8 +327,11 @@ def _get_stackit_client(self) -> _StackitClient: :return: A StackitClient object. """ base_url = "https://dns.api.stackit.cloud" - if self.credentials and self.credentials.conf("base_url") is not None: - base_url = self.credentials.conf("base_url") + custom_base_url = self.conf("base-url") + if isinstance(custom_base_url, str) and custom_base_url: + base_url = custom_base_url.rstrip("/") + elif self.credentials and self.credentials.conf("base_url") is not None: + base_url = self.credentials.conf("base_url").rstrip("/") if self.service_account is not None: access_token = self._generate_jwt_token(self.conf("service_account")) @@ -376,13 +387,18 @@ def _request_access_token(self, jwt_token: str) -> str: :param jwt_token: The JWT token used to request the access token. :return: An access token if the request is successful, None otherwise. """ + token_url = "https://service-account.api.stackit.cloud/token" + custom_token_url = self.conf("token-url") + if isinstance(custom_token_url, str) and custom_token_url: + token_url = custom_token_url + data = { "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer", "assertion": jwt_token, } try: response = requests.post( - "https://service-account.api.stackit.cloud/token", + token_url, data=data, headers={"Content-Type": "application/x-www-form-urlencoded"}, ) diff --git a/certbot_dns_stackit/test_stackit.py b/certbot_dns_stackit/test_stackit.py index c4ba535..3e24039 100644 --- a/certbot_dns_stackit/test_stackit.py +++ b/certbot_dns_stackit/test_stackit.py @@ -215,7 +215,7 @@ def test_add_txt_record_with_rrset_with_validation(self): class TestAuthenticator(unittest.TestCase): def setUp(self): mock_config = Mock() - mock_name = Mock() + mock_name = "dns-stackit" self.authenticator = Authenticator(mock_config, mock_name) @patch.object(Authenticator, "conf") @@ -351,6 +351,62 @@ def test_request_access_token_success(self, mock_post): ) self.assertEqual(result, "mocked_access_token") + @patch.object(Authenticator, "conf") + @patch("requests.post") + def test_request_access_token_custom_token_url(self, mock_post, mock_conf): + mock_conf.return_value = "https://custom.token.url/token" + mock_response = mock_post.return_value + mock_response.raise_for_status = lambda: None + mock_response.json.return_value = {"access_token": "mocked_access_token"} + + result = self.authenticator._request_access_token("jwt_token_example") + + mock_post.assert_called_once_with( + "https://custom.token.url/token", + data={ + "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer", + "assertion": "jwt_token_example", + }, + headers={"Content-Type": "application/x-www-form-urlencoded"}, + ) + self.assertEqual(result, "mocked_access_token") + + @patch.object(Authenticator, "_generate_jwt_token", return_value="sa_token") + @patch.object(Authenticator, "conf") + def test_get_stackit_client_service_account_custom_base_url( + self, mock_conf, mock_gen_jwt + ): + self.authenticator.service_account = "path/to/sa.json" + conf_values = { + "base-url": "https://custom.dns.api.stackit.cloud/", + "service_account": "path/to/sa.json", + "project-id": "proj-123", + } + mock_conf.side_effect = lambda key: conf_values.get(key) + + client = self.authenticator._get_stackit_client() + + self.assertEqual(client.base_url, "https://custom.dns.api.stackit.cloud") + self.assertEqual(client.project_id, "proj-123") + self.assertEqual(client.auth_token, "sa_token") + + @patch.object(Authenticator, "conf", return_value=None) + def test_get_stackit_client_credentials_base_url(self, mock_conf): + mock_creds = Mock() + creds_values = { + "base_url": "https://ini.dns.api.stackit.cloud/", + "auth_token": "ini_token", + "project_id": "proj-456", + } + mock_creds.conf.side_effect = lambda key: creds_values.get(key) + self.authenticator.credentials = mock_creds + + client = self.authenticator._get_stackit_client() + + self.assertEqual(client.base_url, "https://ini.dns.api.stackit.cloud") + self.assertEqual(client.project_id, "proj-456") + self.assertEqual(client.auth_token, "ini_token") + @patch("requests.post") def test_request_access_token_failure_raises_http_error(self, mock_post): mock_response = Response()