Merge branch 'master' into rewrite-java-keystore-use-pyjks

This commit is contained in:
Curtis 2019-04-17 10:43:44 -07:00 committed by GitHub
commit 8177e12f3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 16 deletions

View File

@ -112,10 +112,20 @@ class CertificateInputSchema(CertificateCreationSchema):
if data.get('replacements'): if data.get('replacements'):
data['replaces'] = data['replacements'] # TODO remove when field is deprecated data['replaces'] = data['replacements'] # TODO remove when field is deprecated
if data.get('csr'): if data.get('csr'):
dns_names = cert_utils.get_dns_names_from_csr(data['csr']) csr_sans = cert_utils.get_sans_from_csr(data['csr'])
if not data['extensions']['subAltNames']['names']: if not data.get('extensions'):
data['extensions'] = {
'subAltNames': {
'names': []
}
}
elif not data['extensions'].get('subAltNames'):
data['extensions']['subAltNames'] = {
'names': []
}
elif not data['extensions']['subAltNames'].get('names'):
data['extensions']['subAltNames']['names'] = [] data['extensions']['subAltNames']['names'] = []
data['extensions']['subAltNames']['names'] += dns_names data['extensions']['subAltNames']['names'] += csr_sans
return missing.convert_validity_years(data) return missing.convert_validity_years(data)

View File

@ -14,14 +14,14 @@ from cryptography.hazmat.backends import default_backend
from marshmallow.exceptions import ValidationError from marshmallow.exceptions import ValidationError
def get_dns_names_from_csr(data): def get_sans_from_csr(data):
""" """
Fetches DNSNames from CSR. Fetches SubjectAlternativeNames from CSR.
Potentially extendable to any kind of SubjectAlternativeName Works with any kind of SubjectAlternativeName
:param data: PEM-encoded string with CSR :param data: PEM-encoded string with CSR
:return: :return: List of LemurAPI-compatible subAltNames
""" """
dns_names = [] sub_alt_names = []
try: try:
request = x509.load_pem_x509_csr(data.encode('utf-8'), default_backend()) request = x509.load_pem_x509_csr(data.encode('utf-8'), default_backend())
except Exception: except Exception:
@ -29,14 +29,12 @@ def get_dns_names_from_csr(data):
try: try:
alt_names = request.extensions.get_extension_for_class(x509.SubjectAlternativeName) alt_names = request.extensions.get_extension_for_class(x509.SubjectAlternativeName)
for alt_name in alt_names.value:
for name in alt_names.value.get_values_for_type(x509.DNSName): sub_alt_names.append({
dns_name = { 'nameType': type(alt_name).__name__,
'nameType': 'DNSName', 'value': alt_name.value
'value': name })
}
dns_names.append(dns_name)
except x509.ExtensionNotFound: except x509.ExtensionNotFound:
pass pass
return dns_names return sub_alt_names

View File

@ -284,6 +284,31 @@ def test_certificate_input_with_extensions(client, authority):
assert not errors assert not errors
def test_certificate_input_schema_parse_csr(authority):
from lemur.certificates.schemas import CertificateInputSchema
test_san_dns = 'foobar.com'
extensions = {'sub_alt_names': {'names': x509.SubjectAlternativeName([x509.DNSName(test_san_dns)])}}
csr, private_key = create_csr(owner='joe@example.com', common_name='ACommonName', organization='test',
organizational_unit='Meters', country='NL', state='Noord-Holland', location='Amsterdam',
key_type='RSA2048', extensions=extensions)
input_data = {
'commonName': 'test.example.com',
'owner': 'jim@example.com',
'authority': {'id': authority.id},
'description': 'testtestest',
'csr': csr,
'dnsProvider': None,
}
data, errors = CertificateInputSchema().load(input_data)
for san in data['extensions']['sub_alt_names']['names']:
assert san.value == test_san_dns
assert not errors
def test_certificate_out_of_range_date(client, authority): def test_certificate_out_of_range_date(client, authority):
from lemur.certificates.schemas import CertificateInputSchema from lemur.certificates.schemas import CertificateInputSchema
input_data = { input_data = {