From e074a14ee9f932b88494b89ecbb3fc59c9b508cf Mon Sep 17 00:00:00 2001 From: Curtis Castrapel Date: Wed, 28 Nov 2018 14:27:03 -0800 Subject: [PATCH] unit test --- lemur/certificates/models.py | 1 - lemur/certificates/service.py | 3 ++- lemur/common/celery.py | 4 ++-- lemur/manage.py | 2 +- lemur/plugins/lemur_acme/plugin.py | 13 ++++++++++++- lemur/tests/conf.py | 5 ++--- lemur/tests/test_pending_certificates.py | 6 +++--- 7 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lemur/certificates/models.py b/lemur/certificates/models.py index 97794c38..e2ac2cba 100644 --- a/lemur/certificates/models.py +++ b/lemur/certificates/models.py @@ -138,7 +138,6 @@ class Certificate(db.Model): logs = relationship('Log', backref='certificate') endpoints = relationship('Endpoint', backref='certificate') rotation_policy = relationship("RotationPolicy") - sensitive_fields = ('private_key',) def __init__(self, **kwargs): diff --git a/lemur/certificates/service.py b/lemur/certificates/service.py index 3a99a5f9..d965192e 100644 --- a/lemur/certificates/service.py +++ b/lemur/certificates/service.py @@ -281,7 +281,8 @@ def create(**kwargs): # "attribute refresh operation cannot proceed" pending_cert = database.session_query(PendingCertificate).get(cert.id) from lemur.common.celery import fetch_acme_cert - fetch_acme_cert.delay(pending_cert.id) + if not current_app.config.get("ACME_DISABLE_AUTORESOLVE", False): + fetch_acme_cert.delay(pending_cert.id) return cert diff --git a/lemur/common/celery.py b/lemur/common/celery.py index 82977051..b7f65886 100644 --- a/lemur/common/celery.py +++ b/lemur/common/celery.py @@ -25,8 +25,8 @@ flask_app = create_app() def make_celery(app): - celery = Celery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'], - broker=app.config['CELERY_BROKER_URL']) + celery = Celery(app.import_name, backend=app.config.get('CELERY_RESULT_BACKEND'), + broker=app.config.get('CELERY_BROKER_URL')) celery.conf.update(app.config) TaskBase = celery.Task diff --git a/lemur/manage.py b/lemur/manage.py index 6b1e1013..b972e8a5 100755 --- a/lemur/manage.py +++ b/lemur/manage.py @@ -47,7 +47,7 @@ from lemur.logs.models import Log # noqa from lemur.endpoints.models import Endpoint # noqa from lemur.policies.models import RotationPolicy # noqa from lemur.pending_certificates.models import PendingCertificate # noqa - +from lemur.dns_providers.models import DnsProvider # noqa manager = Manager(create_app) manager.add_option('-c', '--config', dest='config') diff --git a/lemur/plugins/lemur_acme/plugin.py b/lemur/plugins/lemur_acme/plugin.py index 62e647c4..53d11935 100644 --- a/lemur/plugins/lemur_acme/plugin.py +++ b/lemur/plugins/lemur_acme/plugin.py @@ -333,9 +333,12 @@ class ACMEIssuerPlugin(IssuerPlugin): def __init__(self, *args, **kwargs): super(ACMEIssuerPlugin, self).__init__(*args, **kwargs) - self.acme = AcmeHandler() + self.acme = None def get_dns_provider(self, type): + if not self.acme: + self.acme = AcmeHandler() + provider_types = { 'cloudflare': cloudflare, 'dyn': dyn, @@ -347,12 +350,16 @@ class ACMEIssuerPlugin(IssuerPlugin): return provider def get_all_zones(self, dns_provider): + if not self.acme: + self.acme = AcmeHandler() dns_provider_options = json.loads(dns_provider.credentials) account_number = dns_provider_options.get("account_id") dns_provider_plugin = self.get_dns_provider(dns_provider.provider_type) return dns_provider_plugin.get_zones(account_number=account_number) def get_ordered_certificate(self, pending_cert): + if not self.acme: + self.acme = AcmeHandler() acme_client, registration = self.acme.setup_acme_client(pending_cert.authority) order_info = authorization_service.get(pending_cert.external_id) if pending_cert.dns_provider_id: @@ -388,6 +395,8 @@ class ACMEIssuerPlugin(IssuerPlugin): return cert def get_ordered_certificates(self, pending_certs): + if not self.acme: + self.acme = AcmeHandler() pending = [] certs = [] for pending_cert in pending_certs: @@ -470,6 +479,8 @@ class ACMEIssuerPlugin(IssuerPlugin): :param issuer_options: :return: :raise Exception: """ + if not self.acme: + self.acme = AcmeHandler() authority = issuer_options.get('authority') create_immediately = issuer_options.get('create_immediately', False) acme_client, registration = self.acme.setup_acme_client(authority) diff --git a/lemur/tests/conf.py b/lemur/tests/conf.py index c2b5d83d..bbe155cd 100644 --- a/lemur/tests/conf.py +++ b/lemur/tests/conf.py @@ -1,7 +1,7 @@ - # This is just Python which means you can inherit and tweak settings import os + _basedir = os.path.abspath(os.path.dirname(__file__)) THREADS_PER_PAGE = 8 @@ -78,14 +78,12 @@ DIGICERT_API_KEY = 'api-key' DIGICERT_ORG_ID = 111111 DIGICERT_ROOT = "ROOT" - VERISIGN_URL = 'http://example.com' VERISIGN_PEM_PATH = '~/' VERISIGN_FIRST_NAME = 'Jim' VERISIGN_LAST_NAME = 'Bob' VERSIGN_EMAIL = 'jim@example.com' - ACME_AWS_ACCOUNT_NUMBER = '11111111111' ACME_PRIVATE_KEY = ''' @@ -180,6 +178,7 @@ ACME_URL = 'https://acme-v01.api.letsencrypt.org' ACME_EMAIL = 'jim@example.com' ACME_TEL = '4088675309' ACME_DIRECTORY_URL = 'https://acme-v01.api.letsencrypt.org' +ACME_DISABLE_AUTORESOLVE = True LDAP_AUTH = True LDAP_BIND_URI = 'ldap://localhost' diff --git a/lemur/tests/test_pending_certificates.py b/lemur/tests/test_pending_certificates.py index 567159e1..7accf7d9 100644 --- a/lemur/tests/test_pending_certificates.py +++ b/lemur/tests/test_pending_certificates.py @@ -2,11 +2,10 @@ import json import pytest +from lemur.pending_certificates.views import * # noqa from .vectors import CSR_STR, INTERMEDIATE_CERT_STR, VALID_ADMIN_API_TOKEN, VALID_ADMIN_HEADER_TOKEN, \ VALID_USER_HEADER_TOKEN, WILDCARD_CERT_STR -from lemur.pending_certificates.views import * # noqa - def test_increment_attempt(pending_certificate): from lemur.pending_certificates.service import increment_attempt @@ -17,7 +16,8 @@ def test_increment_attempt(pending_certificate): def test_create_pending_certificate(async_issuer_plugin, async_authority, user): from lemur.certificates.service import create - pending_cert = create(authority=async_authority, csr=CSR_STR, owner='joe@example.com', creator=user['user'], common_name='ACommonName') + pending_cert = create(authority=async_authority, csr=CSR_STR, owner='joe@example.com', creator=user['user'], + common_name='ACommonName') assert pending_cert.external_id == '12345'