Gracefully handle importing certificates with missing data (#674)

* fixing index out of range issue

* catching exceptions is common values aren't set

* fixing lint errors

* fixing unrelated lint/import error
This commit is contained in:
Nevins 2017-01-24 16:48:53 -05:00 committed by kevgliss
parent b1723b4985
commit 162d5ccb62
2 changed files with 37 additions and 18 deletions

View File

@ -7,6 +7,7 @@
"""
import requests
import subprocess
from requests.exceptions import ConnectionError
from cryptography import x509
from cryptography.hazmat.backends import default_backend

View File

@ -53,9 +53,12 @@ def common_name(cert):
:param cert:
:return: Common name or None
"""
return cert.subject.get_attributes_for_oid(
x509.OID_COMMON_NAME
)[0].value.strip()
try:
return cert.subject.get_attributes_for_oid(
x509.OID_COMMON_NAME
)[0].value.strip()
except Exception as e:
current_app.logger.error("Unable to get common name! {0}".format(e))
def organization(cert):
@ -64,9 +67,12 @@ def organization(cert):
:param cert:
:return:
"""
return cert.subject.get_attributes_for_oid(
x509.OID_ORGANIZATION_NAME
)[0].value.strip()
try:
return cert.subject.get_attributes_for_oid(
x509.OID_ORGANIZATION_NAME
)[0].value.strip()
except Exception as e:
current_app.logger.error("Unable to get organization! {0}".format(e))
def organizational_unit(cert):
@ -75,9 +81,12 @@ def organizational_unit(cert):
:param cert:
:return:
"""
return cert.subject.get_attributes_for_oid(
x509.OID_ORGANIZATIONAL_UNIT_NAME
)[0].value.strip()
try:
return cert.subject.get_attributes_for_oid(
x509.OID_ORGANIZATIONAL_UNIT_NAME
)[0].value.strip()
except Exception as e:
current_app.logger.error("Unable to get organizational unit! {0}".format(e))
def country(cert):
@ -86,9 +95,12 @@ def country(cert):
:param cert:
:return:
"""
return cert.subject.get_attributes_for_oid(
x509.OID_COUNTRY_NAME
)[0].value.strip()
try:
return cert.subject.get_attributes_for_oid(
x509.OID_COUNTRY_NAME
)[0].value.strip()
except Exception as e:
current_app.logger.error("Unable to get country! {0}".format(e))
def state(cert):
@ -97,9 +109,12 @@ def state(cert):
:param cert:
:return:
"""
return cert.subject.get_attributes_for_oid(
x509.OID_STATE_OR_PROVINCE_NAME
)[0].value.strip()
try:
return cert.subject.get_attributes_for_oid(
x509.OID_STATE_OR_PROVINCE_NAME
)[0].value.strip()
except Exception as e:
current_app.logger.error("Unable to get state! {0}".format(e))
def location(cert):
@ -108,9 +123,12 @@ def location(cert):
:param cert:
:return:
"""
return cert.subject.get_attributes_for_oid(
x509.OID_LOCALITY_NAME
)[0].value.strip()
try:
return cert.subject.get_attributes_for_oid(
x509.OID_LOCALITY_NAME
)[0].value.strip()
except Exception as e:
current_app.logger.error("Unable to get location! {0}".format(e))
def domains(cert):