2018-04-25 11:19:34 -07:00
|
|
|
import unittest
|
2020-04-09 18:17:05 -07:00
|
|
|
from unittest.mock import patch, Mock
|
2020-02-12 15:10:24 -08:00
|
|
|
|
2020-09-30 13:21:34 +02:00
|
|
|
import josepy as jose
|
2020-02-12 15:10:24 -08:00
|
|
|
from cryptography.x509 import DNSName
|
2020-06-11 14:15:40 -07:00
|
|
|
from lemur.plugins.lemur_acme import plugin
|
2020-09-30 13:21:34 +02:00
|
|
|
from lemur.common.utils import generate_private_key
|
2020-04-09 18:17:05 -07:00
|
|
|
from mock import MagicMock
|
2018-05-07 09:58:24 -07:00
|
|
|
|
2018-04-25 11:19:34 -07:00
|
|
|
|
2020-09-30 09:24:26 +02:00
|
|
|
class TestAcmeDns(unittest.TestCase):
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.dns_provider_service")
|
2018-08-13 14:22:59 -07:00
|
|
|
def setUp(self, mock_dns_provider_service):
|
2018-04-27 11:18:41 -07:00
|
|
|
self.ACMEIssuerPlugin = plugin.ACMEIssuerPlugin()
|
2020-09-30 11:56:12 +02:00
|
|
|
self.acme = plugin.AcmeDnsHandler()
|
2018-08-13 14:22:59 -07:00
|
|
|
mock_dns_provider = Mock()
|
|
|
|
mock_dns_provider.name = "cloudflare"
|
|
|
|
mock_dns_provider.credentials = "{}"
|
|
|
|
mock_dns_provider.provider_type = "cloudflare"
|
2019-05-16 07:57:02 -07:00
|
|
|
self.acme.dns_providers_for_domain = {
|
|
|
|
"www.test.com": [mock_dns_provider],
|
|
|
|
"test.fakedomain.net": [mock_dns_provider],
|
|
|
|
}
|
2018-04-27 11:18:41 -07:00
|
|
|
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.len", return_value=1)
|
2020-03-16 11:24:17 -07:00
|
|
|
def test_get_dns_challenges(self, mock_len):
|
2018-04-25 11:19:34 -07:00
|
|
|
assert mock_len
|
|
|
|
|
|
|
|
from acme import challenges
|
2019-05-16 07:57:02 -07:00
|
|
|
|
2020-03-16 11:24:17 -07:00
|
|
|
host = "example.com"
|
2018-04-25 11:19:34 -07:00
|
|
|
c = challenges.DNS01()
|
|
|
|
|
|
|
|
mock_authz = Mock()
|
|
|
|
mock_authz.body.resolved_combinations = []
|
|
|
|
mock_entry = Mock()
|
|
|
|
mock_entry.chall = c
|
|
|
|
mock_authz.body.resolved_combinations.append(mock_entry)
|
2020-03-16 11:24:17 -07:00
|
|
|
result = yield self.acme.get_dns_challenges(host, mock_authz)
|
2018-04-25 11:19:34 -07:00
|
|
|
self.assertEqual(result, mock_entry)
|
|
|
|
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("acme.client.Client")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.len", return_value=1)
|
2020-09-30 11:56:12 +02:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.get_dns_challenges")
|
2019-05-16 07:57:02 -07:00
|
|
|
def test_start_dns_challenge(
|
2020-04-09 18:17:05 -07:00
|
|
|
self, mock_get_dns_challenges, mock_len, mock_app, mock_acme
|
2019-05-16 07:57:02 -07:00
|
|
|
):
|
2018-04-25 11:19:34 -07:00
|
|
|
assert mock_len
|
2018-05-16 07:46:37 -07:00
|
|
|
mock_order = Mock()
|
2018-04-25 11:19:34 -07:00
|
|
|
mock_app.logger.debug = Mock()
|
|
|
|
mock_authz = Mock()
|
|
|
|
mock_authz.body.resolved_combinations = []
|
2018-05-08 11:04:25 -07:00
|
|
|
mock_entry = MagicMock()
|
2018-04-25 11:19:34 -07:00
|
|
|
from acme import challenges
|
2019-05-16 07:57:02 -07:00
|
|
|
|
2018-04-25 11:19:34 -07:00
|
|
|
c = challenges.DNS01()
|
2020-09-30 09:24:26 +02:00
|
|
|
mock_entry.chall = TestAcmeDns.test_complete_dns_challenge_fail
|
2018-04-25 11:19:34 -07:00
|
|
|
mock_authz.body.resolved_combinations.append(mock_entry)
|
|
|
|
mock_acme.request_domain_challenges = Mock(return_value=mock_authz)
|
|
|
|
mock_dns_provider = Mock()
|
|
|
|
mock_dns_provider.create_txt_record = Mock(return_value=1)
|
|
|
|
|
|
|
|
values = [mock_entry]
|
2020-03-16 11:24:17 -07:00
|
|
|
iterable = mock_get_dns_challenges.return_value
|
2018-04-25 11:19:34 -07:00
|
|
|
iterator = iter(values)
|
|
|
|
iterable.__iter__.return_value = iterator
|
2019-05-16 07:57:02 -07:00
|
|
|
result = self.acme.start_dns_challenge(
|
|
|
|
mock_acme, "accountid", "host", mock_dns_provider, mock_order, {}
|
|
|
|
)
|
2018-04-25 11:19:34 -07:00
|
|
|
self.assertEqual(type(result), plugin.AuthorizationRecord)
|
|
|
|
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("acme.client.Client")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
|
|
|
@patch("lemur.plugins.lemur_acme.cloudflare.wait_for_dns_change")
|
2020-02-12 15:10:24 -08:00
|
|
|
@patch("time.sleep")
|
2019-05-16 07:57:02 -07:00
|
|
|
def test_complete_dns_challenge_success(
|
2020-04-09 18:17:05 -07:00
|
|
|
self, mock_sleep, mock_wait_for_dns_change, mock_current_app, mock_acme
|
2019-05-16 07:57:02 -07:00
|
|
|
):
|
2018-04-25 11:19:34 -07:00
|
|
|
mock_dns_provider = Mock()
|
|
|
|
mock_dns_provider.wait_for_dns_change = Mock(return_value=True)
|
|
|
|
mock_authz = Mock()
|
2020-02-12 15:10:24 -08:00
|
|
|
mock_sleep.return_value = False
|
2018-04-25 11:19:34 -07:00
|
|
|
mock_authz.dns_challenge.response = Mock()
|
|
|
|
mock_authz.dns_challenge.response.simple_verify = Mock(return_value=True)
|
2018-05-16 07:46:37 -07:00
|
|
|
mock_authz.authz = []
|
2018-08-13 14:22:59 -07:00
|
|
|
mock_authz.host = "www.test.com"
|
2018-05-16 07:46:37 -07:00
|
|
|
mock_authz_record = Mock()
|
|
|
|
mock_authz_record.body.identifier.value = "test"
|
|
|
|
mock_authz.authz.append(mock_authz_record)
|
|
|
|
mock_authz.change_id = []
|
|
|
|
mock_authz.change_id.append("123")
|
|
|
|
mock_authz.dns_challenge = []
|
|
|
|
dns_challenge = Mock()
|
|
|
|
mock_authz.dns_challenge.append(dns_challenge)
|
2018-08-13 14:22:59 -07:00
|
|
|
self.acme.complete_dns_challenge(mock_acme, mock_authz)
|
2018-04-25 11:19:34 -07:00
|
|
|
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("acme.client.Client")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
|
|
|
@patch("lemur.plugins.lemur_acme.cloudflare.wait_for_dns_change")
|
|
|
|
def test_complete_dns_challenge_fail(
|
2020-04-09 18:17:05 -07:00
|
|
|
self, mock_wait_for_dns_change, mock_current_app, mock_acme
|
2019-05-16 07:57:02 -07:00
|
|
|
):
|
2018-04-25 11:19:34 -07:00
|
|
|
mock_dns_provider = Mock()
|
|
|
|
mock_dns_provider.wait_for_dns_change = Mock(return_value=True)
|
|
|
|
|
|
|
|
mock_authz = Mock()
|
|
|
|
mock_authz.dns_challenge.response = Mock()
|
|
|
|
mock_authz.dns_challenge.response.simple_verify = Mock(return_value=False)
|
2018-05-16 07:46:37 -07:00
|
|
|
mock_authz.authz = []
|
2018-08-13 14:22:59 -07:00
|
|
|
mock_authz.host = "www.test.com"
|
2018-05-16 07:46:37 -07:00
|
|
|
mock_authz_record = Mock()
|
|
|
|
mock_authz_record.body.identifier.value = "test"
|
|
|
|
mock_authz.authz.append(mock_authz_record)
|
|
|
|
mock_authz.change_id = []
|
|
|
|
mock_authz.change_id.append("123")
|
|
|
|
mock_authz.dns_challenge = []
|
|
|
|
dns_challenge = Mock()
|
|
|
|
mock_authz.dns_challenge.append(dns_challenge)
|
2018-04-25 11:19:34 -07:00
|
|
|
self.assertRaises(
|
2019-05-16 07:57:02 -07:00
|
|
|
ValueError, self.acme.complete_dns_challenge(mock_acme, mock_authz)
|
2018-04-25 11:19:34 -07:00
|
|
|
)
|
|
|
|
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("acme.client.Client")
|
|
|
|
@patch("OpenSSL.crypto", return_value="mock_cert")
|
|
|
|
@patch("josepy.util.ComparableX509")
|
2020-09-30 11:56:12 +02:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.get_dns_challenges")
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
|
|
|
def test_request_certificate(
|
2020-04-09 18:17:05 -07:00
|
|
|
self,
|
|
|
|
mock_current_app,
|
|
|
|
mock_get_dns_challenges,
|
|
|
|
mock_jose,
|
|
|
|
mock_crypto,
|
|
|
|
mock_acme,
|
2019-05-16 07:57:02 -07:00
|
|
|
):
|
2018-04-25 11:19:34 -07:00
|
|
|
mock_cert_response = Mock()
|
|
|
|
mock_cert_response.body = "123"
|
|
|
|
mock_cert_response_full = [mock_cert_response, True]
|
|
|
|
mock_acme.poll_and_request_issuance = Mock(return_value=mock_cert_response_full)
|
|
|
|
mock_authz = []
|
|
|
|
mock_authz_record = MagicMock()
|
|
|
|
mock_authz_record.authz = Mock()
|
|
|
|
mock_authz.append(mock_authz_record)
|
|
|
|
mock_acme.fetch_chain = Mock(return_value="mock_chain")
|
2019-05-16 07:57:02 -07:00
|
|
|
mock_crypto.dump_certificate = Mock(return_value=b"chain")
|
2018-05-16 07:46:37 -07:00
|
|
|
mock_order = Mock()
|
2020-07-15 17:04:49 -07:00
|
|
|
mock_current_app.config = {}
|
2018-08-13 14:22:59 -07:00
|
|
|
self.acme.request_certificate(mock_acme, [], mock_order)
|
2018-04-25 11:19:34 -07:00
|
|
|
|
|
|
|
def test_setup_acme_client_fail(self):
|
|
|
|
mock_authority = Mock()
|
|
|
|
mock_authority.options = []
|
|
|
|
with self.assertRaises(Exception):
|
2018-08-13 14:22:59 -07:00
|
|
|
self.acme.setup_acme_client(mock_authority)
|
2018-04-25 11:19:34 -07:00
|
|
|
|
2020-09-30 13:21:34 +02:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.jose.JWK.json_loads")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.BackwardsCompatibleClientV2")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
|
|
|
def test_setup_acme_client_success_load_account_from_authority(self, mock_current_app, mock_acme, mock_key_json_load):
|
|
|
|
mock_authority = Mock()
|
|
|
|
mock_authority.id = 2
|
|
|
|
mock_authority.options = '[{"name": "mock_name", "value": "mock_value"}, ' \
|
|
|
|
'{"name": "store_account", "value": true},' \
|
|
|
|
'{"name": "acme_private_key", "value": "{\\"n\\": \\"PwIOkViO\\", \\"kty\\": \\"RSA\\"}"}, ' \
|
|
|
|
'{"name": "acme_regr", "value": "{\\"body\\": {}, \\"uri\\": \\"http://test.com\\"}"}]'
|
|
|
|
mock_client = Mock()
|
|
|
|
mock_acme.return_value = mock_client
|
|
|
|
mock_current_app.config = {}
|
|
|
|
|
|
|
|
mock_key_json_load.return_value = jose.JWKRSA(key=generate_private_key("RSA2048"))
|
|
|
|
|
|
|
|
result_client, result_registration = self.acme.setup_acme_client(mock_authority)
|
|
|
|
|
|
|
|
mock_acme.new_account_and_tos.assert_not_called()
|
|
|
|
assert result_client
|
|
|
|
assert not result_registration
|
|
|
|
|
2020-09-30 12:59:52 +02:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.jose.JWKRSA.fields_to_partial_json")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.authorities_service")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.BackwardsCompatibleClientV2")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
|
|
|
def test_setup_acme_client_success_store_new_account(self, mock_current_app, mock_acme, mock_authorities_service,
|
|
|
|
mock_key_generation):
|
|
|
|
mock_authority = Mock()
|
|
|
|
mock_authority.id = 2
|
|
|
|
mock_authority.options = '[{"name": "mock_name", "value": "mock_value"}, ' \
|
|
|
|
'{"name": "store_account", "value": true}]'
|
|
|
|
mock_client = Mock()
|
|
|
|
mock_registration = Mock()
|
|
|
|
mock_registration.uri = "http://test.com"
|
|
|
|
mock_client.register = mock_registration
|
|
|
|
mock_client.agree_to_tos = Mock(return_value=True)
|
|
|
|
mock_client.new_account_and_tos.return_value = mock_registration
|
|
|
|
mock_acme.return_value = mock_client
|
|
|
|
mock_current_app.config = {}
|
|
|
|
|
|
|
|
mock_key_generation.return_value = {"n": "PwIOkViO"}
|
|
|
|
|
|
|
|
mock_authorities_service.update_options = Mock(return_value=True)
|
|
|
|
|
|
|
|
self.acme.setup_acme_client(mock_authority)
|
|
|
|
|
|
|
|
mock_authorities_service.update_options.assert_called_with(2, options='[{"name": "mock_name", "value": "mock_value"}, '
|
|
|
|
'{"name": "store_account", "value": true}, '
|
|
|
|
'{"name": "acme_private_key", "value": "{\\"n\\": \\"PwIOkViO\\", \\"kty\\": \\"RSA\\"}"}, '
|
|
|
|
'{"name": "acme_regr", "value": "{\\"body\\": {}, \\"uri\\": \\"http://test.com\\"}"}]')
|
|
|
|
|
2020-09-30 13:42:16 +02:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.authorities_service")
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.BackwardsCompatibleClientV2")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
2020-09-30 13:42:16 +02:00
|
|
|
def test_setup_acme_client_success(self, mock_current_app, mock_acme, mock_authorities_service):
|
2018-04-25 11:19:34 -07:00
|
|
|
mock_authority = Mock()
|
2020-09-23 17:12:49 +02:00
|
|
|
mock_authority.options = '[{"name": "mock_name", "value": "mock_value"}, ' \
|
2020-09-30 12:59:52 +02:00
|
|
|
'{"name": "store_account", "value": false}]'
|
2018-04-25 11:19:34 -07:00
|
|
|
mock_client = Mock()
|
|
|
|
mock_registration = Mock()
|
|
|
|
mock_registration.uri = "http://test.com"
|
|
|
|
mock_client.register = mock_registration
|
|
|
|
mock_client.agree_to_tos = Mock(return_value=True)
|
|
|
|
mock_acme.return_value = mock_client
|
2018-07-30 15:25:02 -07:00
|
|
|
mock_current_app.config = {}
|
2018-08-13 14:22:59 -07:00
|
|
|
result_client, result_registration = self.acme.setup_acme_client(mock_authority)
|
2020-09-30 13:42:16 +02:00
|
|
|
mock_authorities_service.update_options.assert_not_called()
|
2018-04-25 11:19:34 -07:00
|
|
|
assert result_client
|
|
|
|
assert result_registration
|
2018-04-27 11:18:41 -07:00
|
|
|
|
2020-04-09 18:17:05 -07:00
|
|
|
@patch('lemur.plugins.lemur_acme.plugin.current_app')
|
2018-04-27 11:18:41 -07:00
|
|
|
def test_get_domains_single(self, mock_current_app):
|
2019-05-16 07:57:02 -07:00
|
|
|
options = {"common_name": "test.netflix.net"}
|
2018-08-13 14:22:59 -07:00
|
|
|
result = self.acme.get_domains(options)
|
2018-04-27 11:18:41 -07:00
|
|
|
self.assertEqual(result, [options["common_name"]])
|
|
|
|
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
2018-04-27 11:18:41 -07:00
|
|
|
def test_get_domains_multiple(self, mock_current_app):
|
|
|
|
options = {
|
|
|
|
"common_name": "test.netflix.net",
|
|
|
|
"extensions": {
|
2020-02-12 15:10:24 -08:00
|
|
|
"sub_alt_names": {"names": [DNSName("test2.netflix.net"), DNSName("test3.netflix.net")]}
|
2019-05-16 07:57:02 -07:00
|
|
|
},
|
2018-04-27 11:18:41 -07:00
|
|
|
}
|
2018-08-13 14:22:59 -07:00
|
|
|
result = self.acme.get_domains(options)
|
2019-05-16 07:57:02 -07:00
|
|
|
self.assertEqual(
|
|
|
|
result, [options["common_name"], "test2.netflix.net", "test3.netflix.net"]
|
|
|
|
)
|
2018-04-27 11:18:41 -07:00
|
|
|
|
2020-02-12 15:10:24 -08:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
|
|
|
def test_get_domains_san(self, mock_current_app):
|
|
|
|
options = {
|
|
|
|
"common_name": "test.netflix.net",
|
|
|
|
"extensions": {
|
|
|
|
"sub_alt_names": {"names": [DNSName("test.netflix.net"), DNSName("test2.netflix.net")]}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
result = self.acme.get_domains(options)
|
|
|
|
self.assertEqual(
|
|
|
|
result, [options["common_name"], "test2.netflix.net"]
|
|
|
|
)
|
|
|
|
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch(
|
|
|
|
"lemur.plugins.lemur_acme.plugin.AcmeHandler.start_dns_challenge",
|
|
|
|
return_value="test",
|
|
|
|
)
|
2018-04-27 11:18:41 -07:00
|
|
|
def test_get_authorizations(self, mock_start_dns_challenge):
|
2018-05-16 07:46:37 -07:00
|
|
|
mock_order = Mock()
|
|
|
|
mock_order.body.identifiers = []
|
|
|
|
mock_domain = Mock()
|
|
|
|
mock_order.body.identifiers.append(mock_domain)
|
|
|
|
mock_order_info = Mock()
|
|
|
|
mock_order_info.account_number = 1
|
2018-05-16 08:00:33 -07:00
|
|
|
mock_order_info.domains = ["test.fakedomain.net"]
|
2019-05-16 07:57:02 -07:00
|
|
|
result = self.acme.get_authorizations(
|
|
|
|
"acme_client", mock_order, mock_order_info
|
|
|
|
)
|
2018-04-27 11:18:41 -07:00
|
|
|
self.assertEqual(result, ["test"])
|
|
|
|
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch(
|
|
|
|
"lemur.plugins.lemur_acme.plugin.AcmeHandler.complete_dns_challenge",
|
|
|
|
return_value="test",
|
|
|
|
)
|
2018-04-27 11:18:41 -07:00
|
|
|
def test_finalize_authorizations(self, mock_complete_dns_challenge):
|
|
|
|
mock_authz = []
|
|
|
|
mock_authz_record = MagicMock()
|
|
|
|
mock_authz_record.authz = Mock()
|
|
|
|
mock_authz_record.change_id = 1
|
|
|
|
mock_authz_record.dns_challenge.validation_domain_name = Mock()
|
|
|
|
mock_authz_record.dns_challenge.validation = Mock()
|
|
|
|
mock_authz.append(mock_authz_record)
|
|
|
|
mock_dns_provider = Mock()
|
|
|
|
mock_dns_provider.delete_txt_record = Mock()
|
|
|
|
|
|
|
|
mock_acme_client = Mock()
|
2018-08-13 14:22:59 -07:00
|
|
|
result = self.acme.finalize_authorizations(mock_acme_client, mock_authz)
|
2018-04-27 11:18:41 -07:00
|
|
|
self.assertEqual(result, mock_authz)
|
|
|
|
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
2018-04-27 11:18:41 -07:00
|
|
|
def test_create_authority(self, mock_current_app):
|
|
|
|
mock_current_app.config = Mock()
|
|
|
|
options = {
|
2019-05-16 07:57:02 -07:00
|
|
|
"plugin": {"plugin_options": [{"name": "certificate", "value": "123"}]}
|
2018-04-27 11:18:41 -07:00
|
|
|
}
|
|
|
|
acme_root, b, role = self.ACMEIssuerPlugin.create_authority(options)
|
|
|
|
self.assertEqual(acme_root, "123")
|
|
|
|
self.assertEqual(b, "")
|
2019-05-16 07:57:02 -07:00
|
|
|
self.assertEqual(role, [{"username": "", "password": "", "name": "acme"}])
|
|
|
|
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
|
|
|
@patch("lemur.plugins.lemur_acme.dyn.current_app")
|
|
|
|
@patch("lemur.plugins.lemur_acme.cloudflare.current_app")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.dns_provider_service")
|
|
|
|
def test_get_dns_provider(
|
2020-03-16 11:24:17 -07:00
|
|
|
self,
|
|
|
|
mock_dns_provider_service,
|
|
|
|
mock_current_app_cloudflare,
|
|
|
|
mock_current_app_dyn,
|
|
|
|
mock_current_app,
|
2019-05-16 07:57:02 -07:00
|
|
|
):
|
2018-05-08 11:03:17 -07:00
|
|
|
provider = plugin.ACMEIssuerPlugin()
|
|
|
|
route53 = provider.get_dns_provider("route53")
|
|
|
|
assert route53
|
|
|
|
cloudflare = provider.get_dns_provider("cloudflare")
|
|
|
|
assert cloudflare
|
|
|
|
dyn = provider.get_dns_provider("dyn")
|
|
|
|
assert dyn
|
|
|
|
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeHandler.setup_acme_client")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.dns_provider_service")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
2020-09-30 11:56:12 +02:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.get_authorizations")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.finalize_authorizations")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.request_certificate")
|
2019-05-16 07:57:02 -07:00
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.authorization_service")
|
|
|
|
def test_create_certificate(
|
2020-03-16 11:24:17 -07:00
|
|
|
self,
|
|
|
|
mock_authorization_service,
|
|
|
|
mock_request_certificate,
|
|
|
|
mock_finalize_authorizations,
|
|
|
|
mock_get_authorizations,
|
|
|
|
mock_current_app,
|
|
|
|
mock_dns_provider_service,
|
|
|
|
mock_acme,
|
2019-05-16 07:57:02 -07:00
|
|
|
):
|
2018-05-08 11:03:17 -07:00
|
|
|
provider = plugin.ACMEIssuerPlugin()
|
|
|
|
mock_authority = Mock()
|
|
|
|
|
|
|
|
mock_client = Mock()
|
|
|
|
mock_acme.return_value = (mock_client, "")
|
|
|
|
|
|
|
|
mock_dns_provider = Mock()
|
|
|
|
mock_dns_provider.credentials = '{"account_id": 1}'
|
|
|
|
mock_dns_provider.provider_type = "route53"
|
|
|
|
mock_dns_provider_service.get.return_value = mock_dns_provider
|
|
|
|
|
2018-05-10 12:57:54 -07:00
|
|
|
issuer_options = {
|
2019-05-16 07:57:02 -07:00
|
|
|
"authority": mock_authority,
|
|
|
|
"dns_provider": mock_dns_provider,
|
|
|
|
"common_name": "test.netflix.net",
|
2018-05-10 12:57:54 -07:00
|
|
|
}
|
2018-05-08 11:03:17 -07:00
|
|
|
csr = "123"
|
|
|
|
mock_request_certificate.return_value = ("pem_certificate", "chain")
|
|
|
|
result = provider.create_certificate(csr, issuer_options)
|
|
|
|
assert result
|
2020-09-30 11:56:12 +02:00
|
|
|
|
|
|
|
@patch(
|
|
|
|
"lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.start_dns_challenge",
|
|
|
|
return_value="test",
|
|
|
|
)
|
|
|
|
def test_get_authorizations(self, mock_start_dns_challenge):
|
|
|
|
mock_order = Mock()
|
|
|
|
mock_order.body.identifiers = []
|
|
|
|
mock_domain = Mock()
|
|
|
|
mock_order.body.identifiers.append(mock_domain)
|
|
|
|
mock_order_info = Mock()
|
|
|
|
mock_order_info.account_number = 1
|
|
|
|
mock_order_info.domains = ["test.fakedomain.net"]
|
|
|
|
result = self.acme.get_authorizations(
|
|
|
|
"acme_client", mock_order, mock_order_info
|
|
|
|
)
|
|
|
|
self.assertEqual(result, ["test"])
|
|
|
|
|
|
|
|
@patch(
|
|
|
|
"lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.complete_dns_challenge",
|
|
|
|
return_value="test",
|
|
|
|
)
|
|
|
|
def test_finalize_authorizations(self, mock_complete_dns_challenge):
|
|
|
|
mock_authz = []
|
|
|
|
mock_authz_record = MagicMock()
|
|
|
|
mock_authz_record.authz = Mock()
|
|
|
|
mock_authz_record.change_id = 1
|
|
|
|
mock_authz_record.dns_challenge.validation_domain_name = Mock()
|
|
|
|
mock_authz_record.dns_challenge.validation = Mock()
|
|
|
|
mock_authz.append(mock_authz_record)
|
|
|
|
mock_dns_provider = Mock()
|
|
|
|
mock_dns_provider.delete_txt_record = Mock()
|
|
|
|
|
|
|
|
mock_acme_client = Mock()
|
|
|
|
result = self.acme.finalize_authorizations(mock_acme_client, mock_authz)
|
|
|
|
self.assertEqual(result, mock_authz)
|
|
|
|
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeHandler.setup_acme_client")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.authorization_service")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.dns_provider_service")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.get_authorizations")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.finalize_authorizations")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.request_certificate")
|
|
|
|
def test_get_ordered_certificate(
|
|
|
|
self,
|
|
|
|
mock_request_certificate,
|
|
|
|
mock_finalize_authorizations,
|
|
|
|
mock_get_authorizations,
|
|
|
|
mock_dns_provider_service,
|
|
|
|
mock_authorization_service,
|
|
|
|
mock_current_app,
|
|
|
|
mock_acme,
|
|
|
|
):
|
|
|
|
mock_client = Mock()
|
|
|
|
mock_acme.return_value = (mock_client, "")
|
|
|
|
mock_request_certificate.return_value = ("pem_certificate", "chain")
|
|
|
|
|
|
|
|
mock_cert = Mock()
|
|
|
|
mock_cert.external_id = 1
|
|
|
|
|
|
|
|
provider = plugin.ACMEIssuerPlugin()
|
|
|
|
provider.get_dns_provider = Mock()
|
|
|
|
result = provider.get_ordered_certificate(mock_cert)
|
|
|
|
self.assertEqual(
|
|
|
|
result, {"body": "pem_certificate", "chain": "chain", "external_id": "1"}
|
|
|
|
)
|
|
|
|
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeHandler.setup_acme_client")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.current_app")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.authorization_service")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.dns_provider_service")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.get_authorizations")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.finalize_authorizations")
|
|
|
|
@patch("lemur.plugins.lemur_acme.plugin.AcmeDnsHandler.request_certificate")
|
|
|
|
def test_get_ordered_certificates(
|
|
|
|
self,
|
|
|
|
mock_request_certificate,
|
|
|
|
mock_finalize_authorizations,
|
|
|
|
mock_get_authorizations,
|
|
|
|
mock_dns_provider_service,
|
|
|
|
mock_authorization_service,
|
|
|
|
mock_current_app,
|
|
|
|
mock_acme,
|
|
|
|
):
|
|
|
|
mock_client = Mock()
|
|
|
|
mock_acme.return_value = (mock_client, "")
|
|
|
|
mock_request_certificate.return_value = ("pem_certificate", "chain")
|
|
|
|
|
|
|
|
mock_cert = Mock()
|
|
|
|
mock_cert.external_id = 1
|
|
|
|
|
|
|
|
mock_cert2 = Mock()
|
|
|
|
mock_cert2.external_id = 2
|
|
|
|
|
|
|
|
provider = plugin.ACMEIssuerPlugin()
|
|
|
|
provider.get_dns_provider = Mock()
|
|
|
|
result = provider.get_ordered_certificates([mock_cert, mock_cert2])
|
|
|
|
self.assertEqual(len(result), 2)
|
|
|
|
self.assertEqual(
|
|
|
|
result[0]["cert"],
|
|
|
|
{"body": "pem_certificate", "chain": "chain", "external_id": "1"},
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
result[1]["cert"],
|
|
|
|
{"body": "pem_certificate", "chain": "chain", "external_id": "2"},
|
|
|
|
)
|