# Buggy code
def create_certificate(self, csr, issuer_options):
    """
    Creates a CFSSL certificate.

    :param csr:
    :param issuer_options:
    :return:
    """
    current_app.logger.info(
        "Requesting a new cfssl certificate with csr: {0}".format(csr)
    )

    url = "{0}{1}".format(current_app.config.get("CFSSL_URL"), "/api/v1/cfssl/sign")

    data = {"certificate_request": csr}
    data = json.dumps(data)

    try:
        hex_key = current_app.config.get("CFSSL_KEY")
        key = bytes.fromhex(hex_key)
    except (ValueError, NameError):
        # unable to find CFSSL_KEY in config, continue using normal sign method
        pass
    else:
        data = data.encode()

        token = base64.b64encode(hmac.new(key, data, digestmod=hashlib.sha256).digest())
        data = base64.b64encode(data)

        data = json.dumps(
            {"token": token.decode("utf-8"), "request": data.decode("utf-8")}
        )

        url = "{0}{1}".format(
            current_app.config.get("CFSSL_URL"), "/api/v1/cfssl/authsign"
        )
    response = self.session.post(
        url, data=data.encode(encoding="utf_8", errors="strict")
    )
    if response.status_code > 399:
        metrics.send("cfssl_create_certificate_failure", "counter", 1)
        raise Exception("Error creating cert. Please check your CFSSL API server")
    response_json = json.loads(response.content.decode("utf_8"))
    cert = response_json["result"]["certificate"]
    parsed_cert = parse_certificate(cert)
    metrics.send("cfssl_create_certificate_success", "counter", 1)
    return (
        cert,
        current_app.config.get("CFSSL_INTERMEDIATE"),
        parsed_cert.serial_number,
    )


# Traceback (TypeError)
[2020-01-07 17:46:32,386] ERROR in service: Exception minting certificate
Traceback (most recent call last):
File "/www/lemur/lemur/certificates/service.py", line 273, in create
cert_body, private_key, cert_chain, external_id, csr = mint(**kwargs)
File "/www/lemur/lemur/certificates/service.py", line 223, in mint
cert_body, cert_chain, external_id = issuer.create_certificate(csr, kwargs)
File "/www/lemur/lemur/plugins/lemur_cfssl/plugin.py", line 58, in create_certificate
key = bytes.fromhex(hex_key)
TypeError: fromhex() argument must be str, not None
Exception minting certificate
Traceback (most recent call last):
File "/www/lemur/lemur/certificates/service.py", line 273, in create
cert_body, private_key, cert_chain, external_id, csr = mint(**kwargs)
File "/www/lemur/lemur/certificates/service.py", line 223, in mint
cert_body, cert_chain, external_id = issuer.create_certificate(csr, kwargs)
File "/www/lemur/lemur/plugins/lemur_cfssl/plugin.py", line 58, in create_certificate
key = bytes.fromhex(hex_key)
TypeError: fromhex() argument must be str, not None
Exception minting certificate
Traceback (most recent call last):
File "/www/lemur/lemur/certificates/service.py", line 273, in create
cert_body, private_key, cert_chain, external_id, csr = mint(**kwargs)
File "/www/lemur/lemur/certificates/service.py", line 223, in mint
cert_body, cert_chain, external_id = issuer.create_certificate(csr, kwargs)
File "/www/lemur/lemur/plugins/lemur_cfssl/plugin.py", line 58, in create_certificate
key = bytes.fromhex(hex_key)
TypeError: fromhex() argument must be str, not None
[2020-01-07 17:46:32,387] ERROR in schema: fromhex() argument must be str, not None
Traceback (most recent call last):
File "/www/lemur/lemur/common/schema.py", line 160, in decorated_function
resp = f(*args, **kwargs)
File "/www/lemur/lemur/certificates/views.py", line 482, in post
cert = service.create(**data)
File "/www/lemur/lemur/certificates/service.py", line 273, in create
cert_body, private_key, cert_chain, external_id, csr = mint(**kwargs)
File "/www/lemur/lemur/certificates/service.py", line 223, in mint
cert_body, cert_chain, external_id = issuer.create_certificate(csr, kwargs)
File "/www/lemur/lemur/plugins/lemur_cfssl/plugin.py", line 58, in create_certificate
key = bytes.fromhex(hex_key)
TypeError: fromhex() argument must be str, not None
fromhex() argument must be str, not None
Traceback (most recent call last):
File "/www/lemur/lemur/common/schema.py", line 160, in decorated_function
resp = f(*args, **kwargs)
File "/www/lemur/lemur/certificates/views.py", line 482, in post
cert = service.create(**data)
File "/www/lemur/lemur/certificates/service.py", line 273, in create
cert_body, private_key, cert_chain, external_id, csr = mint(**kwargs)
File "/www/lemur/lemur/certificates/service.py", line 223, in mint
cert_body, cert_chain, external_id = issuer.create_certificate(csr, kwargs)
File "/www/lemur/lemur/plugins/lemur_cfssl/plugin.py", line 58, in create_certificate
key = bytes.fromhex(hex_key)
TypeError: fromhex() argument must be str, not None
fromhex() argument must be str, not None
Traceback (most recent call last):
File "/www/lemur/lemur/common/schema.py", line 160, in decorated_function
resp = f(*args, **kwargs)
File "/www/lemur/lemur/certificates/views.py", line 482, in post
cert = service.create(**data)
File "/www/lemur/lemur/certificates/service.py", line 273, in create
cert_body, private_key, cert_chain, external_id, csr = mint(**kwargs)
File "/www/lemur/lemur/certificates/service.py", line 223, in mint
cert_body, cert_chain, external_id = issuer.create_certificate(csr, kwargs)
File "/www/lemur/lemur/plugins/lemur_cfssl/plugin.py", line 58, in create_certificate
key = bytes.fromhex(hex_key)
TypeError: fromhex() argument must be str, not None

# Fixed code
def create_certificate(self, csr, issuer_options):
    """
    Creates a CFSSL certificate.

    :param csr:
    :param issuer_options:
    :return:
    """
    current_app.logger.info(
        "Requesting a new cfssl certificate with csr: {0}".format(csr)
    )

    url = "{0}{1}".format(current_app.config.get("CFSSL_URL"), "/api/v1/cfssl/sign")

    data = {"certificate_request": csr}
    data = json.dumps(data)

    try:
        hex_key = current_app.config.get("CFSSL_KEY")
        key = bytes.fromhex(hex_key)
    except (ValueError, NameError, TypeError):
        # unable to find CFSSL_KEY in config, continue using normal sign method
        pass
    else:
        data = data.encode()

        token = base64.b64encode(hmac.new(key, data, digestmod=hashlib.sha256).digest())
        data = base64.b64encode(data)

        data = json.dumps(
            {"token": token.decode("utf-8"), "request": data.decode("utf-8")}
        )

        url = "{0}{1}".format(
            current_app.config.get("CFSSL_URL"), "/api/v1/cfssl/authsign"
        )
    response = self.session.post(
        url, data=data.encode(encoding="utf_8", errors="strict")
    )
    if response.status_code > 399:
        metrics.send("cfssl_create_certificate_failure", "counter", 1)
        raise Exception("Error creating cert. Please check your CFSSL API server")
    response_json = json.loads(response.content.decode("utf_8"))
    cert = response_json["result"]["certificate"]
    parsed_cert = parse_certificate(cert)
    metrics.send("cfssl_create_certificate_success", "counter", 1)
    return (
        cert,
        current_app.config.get("CFSSL_INTERMEDIATE"),
        parsed_cert.serial_number,
    )