1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
"""Base test class for DNS authenticators built on Lexicon."""
import contextlib
import sys
from types import ModuleType
from typing import Any
from typing import cast
from typing import Generator
from typing import List
from typing import Protocol
from typing import Tuple
from unittest import mock
from unittest.mock import MagicMock
import warnings
import josepy as jose
from requests import Response
from requests.exceptions import HTTPError
from requests.exceptions import RequestException
from certbot import errors
from certbot.achallenges import AnnotatedChallenge
from certbot.plugins import dns_test_common
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
from certbot.plugins.dns_common_lexicon import LexiconClient
from certbot.plugins.dns_test_common import _AuthenticatorCallableTestCase
from certbot.tests import util as test_util
DOMAIN = 'example.com'
KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem"))
DOMAIN_NOT_FOUND = Exception('No domain found')
GENERIC_ERROR = RequestException
LOGIN_ERROR = HTTPError('400 Client Error: ...', response=Response())
UNKNOWN_LOGIN_ERROR = HTTPError('500 Surprise! Error: ...', response=Response())
class _AuthenticatorCallableLexiconTestCase(_AuthenticatorCallableTestCase, Protocol):
"""
Protocol describing a TestCase suitable to test challenges against
a mocked LexiconClient instance.
"""
mock_client: MagicMock
achall: AnnotatedChallenge
class _LexiconAwareTestCase(Protocol):
"""
Protocol describing a TestCase suitable to test a real LexiconClient instance.
"""
client: LexiconClient
provider_mock: MagicMock
record_prefix: str
record_name: str
record_content: str
DOMAIN_NOT_FOUND: Exception
GENERIC_ERROR: Exception
LOGIN_ERROR: Exception
UNKNOWN_LOGIN_ERROR: Exception
def assertRaises(self, *unused_args: Any) -> None:
"""
See
https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises
"""
# These classes are intended to be subclassed/mixed in, so not all members are defined.
# pylint: disable=no-member
class BaseLexiconAuthenticatorTest(dns_test_common.BaseAuthenticatorTest): # pragma: no cover
@test_util.patch_display_util()
def test_perform(self: _AuthenticatorCallableLexiconTestCase,
unused_mock_get_utility: Any) -> None:
self.auth.perform([self.achall])
expected = [mock.call.add_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)]
self.assertEqual(expected, self.mock_client.mock_calls)
def test_cleanup(self: _AuthenticatorCallableLexiconTestCase) -> None:
self.auth._attempt_cleanup = True # _attempt_cleanup | pylint: disable=protected-access
self.auth.cleanup([self.achall])
expected = [mock.call.del_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)]
self.assertEqual(expected, self.mock_client.mock_calls)
class BaseLexiconClientTest: # pragma: no cover
DOMAIN_NOT_FOUND = DOMAIN_NOT_FOUND
GENERIC_ERROR = GENERIC_ERROR
LOGIN_ERROR = LOGIN_ERROR
UNKNOWN_LOGIN_ERROR = UNKNOWN_LOGIN_ERROR
record_prefix = "_acme-challenge"
record_name = record_prefix + "." + DOMAIN
record_content = "bar"
def test_add_txt_record(self: _LexiconAwareTestCase) -> None:
self.client.add_txt_record(DOMAIN, self.record_name, self.record_content)
self.provider_mock.create_record.assert_called_with(rtype='TXT',
name=self.record_name,
content=self.record_content)
def test_add_txt_record_try_twice_to_find_domain(self: _LexiconAwareTestCase) -> None:
self.provider_mock.authenticate.side_effect = [self.DOMAIN_NOT_FOUND, '']
self.client.add_txt_record(DOMAIN, self.record_name, self.record_content)
self.provider_mock.create_record.assert_called_with(rtype='TXT',
name=self.record_name,
content=self.record_content)
def test_add_txt_record_fail_to_find_domain(self: _LexiconAwareTestCase) -> None:
self.provider_mock.authenticate.side_effect = [self.DOMAIN_NOT_FOUND,
self.DOMAIN_NOT_FOUND,
self.DOMAIN_NOT_FOUND,]
self.assertRaises(errors.PluginError,
self.client.add_txt_record,
DOMAIN, self.record_name, self.record_content)
def test_add_txt_record_fail_to_authenticate(self: _LexiconAwareTestCase) -> None:
self.provider_mock.authenticate.side_effect = self.LOGIN_ERROR
self.assertRaises(errors.PluginError,
self.client.add_txt_record,
DOMAIN, self.record_name, self.record_content)
def test_add_txt_record_fail_to_authenticate_with_unknown_error(
self: _LexiconAwareTestCase) -> None:
self.provider_mock.authenticate.side_effect = self.UNKNOWN_LOGIN_ERROR
self.assertRaises(errors.PluginError,
self.client.add_txt_record,
DOMAIN, self.record_name, self.record_content)
def test_add_txt_record_error_finding_domain(self: _LexiconAwareTestCase) -> None:
self.provider_mock.authenticate.side_effect = self.GENERIC_ERROR
self.assertRaises(errors.PluginError,
self.client.add_txt_record,
DOMAIN, self.record_name, self.record_content)
def test_add_txt_record_error_adding_record(self: _LexiconAwareTestCase) -> None:
self.provider_mock.create_record.side_effect = self.GENERIC_ERROR
self.assertRaises(errors.PluginError,
self.client.add_txt_record,
DOMAIN, self.record_name, self.record_content)
def test_del_txt_record(self: _LexiconAwareTestCase) -> None:
self.client.del_txt_record(DOMAIN, self.record_name, self.record_content)
self.provider_mock.delete_record.assert_called_with(rtype='TXT',
name=self.record_name,
content=self.record_content)
def test_del_txt_record_fail_to_find_domain(self: _LexiconAwareTestCase) -> None:
self.provider_mock.authenticate.side_effect = [self.DOMAIN_NOT_FOUND,
self.DOMAIN_NOT_FOUND,
self.DOMAIN_NOT_FOUND, ]
self.client.del_txt_record(DOMAIN, self.record_name, self.record_content)
def test_del_txt_record_fail_to_authenticate(self: _LexiconAwareTestCase) -> None:
self.provider_mock.authenticate.side_effect = self.LOGIN_ERROR
self.client.del_txt_record(DOMAIN, self.record_name, self.record_content)
def test_del_txt_record_fail_to_authenticate_with_unknown_error(
self: _LexiconAwareTestCase) -> None:
self.provider_mock.authenticate.side_effect = self.UNKNOWN_LOGIN_ERROR
self.client.del_txt_record(DOMAIN, self.record_name, self.record_content)
def test_del_txt_record_error_finding_domain(self: _LexiconAwareTestCase) -> None:
self.provider_mock.authenticate.side_effect = self.GENERIC_ERROR
self.client.del_txt_record(DOMAIN, self.record_name, self.record_content)
def test_del_txt_record_error_deleting_record(self: _LexiconAwareTestCase) -> None:
self.provider_mock.delete_record.side_effect = self.GENERIC_ERROR
self.client.del_txt_record(DOMAIN, self.record_name, self.record_content)
class _BaseLexiconDNSAuthenticatorTestProto(_AuthenticatorCallableTestCase, Protocol):
"""Protocol for BaseLexiconDNSAuthenticatorTest instances"""
DOMAIN_NOT_FOUND: Exception
GENERIC_ERROR: Exception
LOGIN_ERROR: Exception
UNKNOWN_LOGIN_ERROR: Exception
achall: AnnotatedChallenge
class BaseLexiconDNSAuthenticatorTest(dns_test_common.BaseAuthenticatorTest):
DOMAIN_NOT_FOUND = DOMAIN_NOT_FOUND
GENERIC_ERROR = GENERIC_ERROR
LOGIN_ERROR = LOGIN_ERROR
UNKNOWN_LOGIN_ERROR = UNKNOWN_LOGIN_ERROR
def test_perform_succeed(self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with test_util.patch_display_util():
with _patch_lexicon_client() as (mock_client, mock_operations):
self.auth.perform([self.achall])
mock_client.assert_called()
config = mock_client.call_args[0][0]
self.assertEqual(DOMAIN, config.resolve('lexicon:domain'))
mock_operations.create_record.assert_called_with(
rtype='TXT', name=f'_acme-challenge.{DOMAIN}', content=mock.ANY)
def test_perform_with_one_domain_resolution_failure_succeed(
self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with test_util.patch_display_util():
with _patch_lexicon_client() as (mock_client, mock_operations):
mock_client.return_value.__enter__.side_effect = [
self.DOMAIN_NOT_FOUND, # First resolution domain attempt
mock_operations, # Second resolution domain attempt
mock_operations, # Create record operation
]
self.auth.perform([self.achall])
def test_perform_with_two_domain_resolution_failures_raise(
self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with test_util.patch_display_util():
with _patch_lexicon_client() as (mock_client, _):
mock_client.return_value.__enter__.side_effect = self.DOMAIN_NOT_FOUND
self.assertRaises(errors.PluginError,
self.auth.perform,
[self.achall])
def test_perform_with_domain_resolution_general_failure_raise(
self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with test_util.patch_display_util():
with _patch_lexicon_client() as (mock_client, _):
mock_client.return_value.__enter__.side_effect = self.GENERIC_ERROR
self.assertRaises(errors.PluginError,
self.auth.perform,
[self.achall])
def test_perform_with_auth_failure_raise(self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with test_util.patch_display_util():
with _patch_lexicon_client() as (mock_client, _):
mock_client.side_effect = self.LOGIN_ERROR
self.assertRaises(errors.PluginError,
self.auth.perform,
[self.achall])
def test_perform_with_unknown_auth_failure_raise(
self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with test_util.patch_display_util():
with _patch_lexicon_client() as (mock_client, _):
mock_client.side_effect = self.UNKNOWN_LOGIN_ERROR
self.assertRaises(errors.PluginError,
self.auth.perform,
[self.achall])
def test_perform_with_create_record_failure_raise(
self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with test_util.patch_display_util():
with _patch_lexicon_client() as (_, mock_operations):
mock_operations.create_record.side_effect = self.GENERIC_ERROR
self.assertRaises(errors.PluginError,
self.auth.perform,
[self.achall])
def test_cleanup_success(self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
self.auth._attempt_cleanup = True # _attempt_cleanup | pylint: disable=protected-access
with _patch_lexicon_client() as (mock_client, mock_operations):
self.auth.cleanup([self.achall])
mock_client.assert_called()
config = mock_client.call_args[0][0]
self.assertEqual(DOMAIN, config.resolve('lexicon:domain'))
mock_operations.delete_record.assert_called_with(
rtype='TXT', name=f'_acme-challenge.{DOMAIN}', content=mock.ANY)
def test_cleanup_with_auth_failure_ignore(self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with _patch_lexicon_client() as (mock_client, _):
mock_client.side_effect = self.LOGIN_ERROR
self.auth.cleanup([self.achall])
def test_cleanup_with_unknown_auth_failure_ignore(
self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with _patch_lexicon_client() as (mock_client, _):
mock_client.side_effect = self.LOGIN_ERROR
self.auth.cleanup([self.achall])
def test_cleanup_with_domain_resolution_failure_ignore(
self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with _patch_lexicon_client() as (mock_client, _):
mock_client.return_value.__enter__.side_effect = self.DOMAIN_NOT_FOUND
self.auth.cleanup([self.achall])
def test_cleanup_with_domain_resolution_general_failure_ignore(
self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with _patch_lexicon_client() as (mock_client, _):
mock_client.return_value.__enter__.side_effect = self.GENERIC_ERROR
self.auth.cleanup([self.achall])
def test_cleanup_with_delete_record_failure_ignore(
self: _BaseLexiconDNSAuthenticatorTestProto) -> None:
with _patch_lexicon_client() as (_, mock_operations):
mock_operations.create_record.side_effect = self.GENERIC_ERROR
self.auth.cleanup([self.achall])
@contextlib.contextmanager
def _patch_lexicon_client() -> Generator[Tuple[MagicMock, MagicMock], None, None]:
with mock.patch('certbot.plugins.dns_common_lexicon.Client') as mock_client:
mock_operations = MagicMock()
mock_client.return_value.__enter__.return_value = mock_operations
yield mock_client, mock_operations
# This class takes a similar approach to the cryptography project to deprecate attributes
# in public modules. See the _ModuleWithDeprecation class here:
# https://github.com/pyca/cryptography/blob/91105952739442a74582d3e62b3d2111365b0dc7/src/cryptography/utils.py#L129
class _DeprecationModule:
"""
Internal class delegating to a module, and displaying warnings when attributes
related to deprecated attributes in the current module.
"""
def __init__(self, module: ModuleType):
self.__dict__['_module'] = module
def __getattr__(self, attr: str) -> Any:
if attr in ('BaseLexiconAuthenticatorTest', 'BaseLexiconClientTest'):
warnings.warn(f'{attr} attribute in {__name__} module is deprecated '
'and will be removed soon.',
DeprecationWarning, stacklevel=2)
return getattr(self._module, attr)
def __setattr__(self, attr: str, value: Any) -> None: # pragma: no cover
setattr(self._module, attr, value)
def __delattr__(self, attr: str) -> Any: # pragma: no cover
delattr(self._module, attr)
def __dir__(self) -> List[str]: # pragma: no cover
return ['_module'] + dir(self._module)
# Patching ourselves to warn about deprecation and planned removal of some elements in the module.
sys.modules[__name__] = cast(ModuleType, _DeprecationModule(sys.modules[__name__]))
|