# Buggy code
def mint(issuer_options):
    """
    Minting is slightly different for each authority.
    Support for multiple authorities is handled by individual plugins.

    :param issuer_options:
    """
    authority = issuer_options["authority"]

    issuer = plugins.get(authority.plugin_name)

    # allow the CSR to be specified by the user
    if not issuer_options.get("csr"):
        csr, private_key = create_csr(issuer_options)
    else:
        csr = issuer_options.get("csr")
        private_key = None

    issuer_options["creator"] = g.user.email
    cert_body, cert_chain = issuer.create_certificate(csr, issuer_options)

    cert = Certificate(cert_body, private_key, cert_chain)

    cert.user = g.user
    cert.authority = authority
    database.update(cert)
    return (
        cert,
        private_key,
        cert_chain,
    )


# Traceback (KeyError)
2016-02-19 21:19:04,405 ERROR: 45 [in /apps/lemur/lemur/common/utils.py:60]
Traceback (most recent call last):
File "/apps/lemur/lemur/common/utils.py", line 46, in wrapper
resp = f(*args, **kwargs)
File "/apps/lemur/lemur/certificates/views.py", line 387, in post
return service.create(**args)
File "/apps/lemur/lemur/certificates/service.py", line 247, in create
cert, private_key, cert_chain = mint(kwargs)
File "/apps/lemur/lemur/certificates/service.py", line 151, in mint
cert_body, cert_chain = issuer.create_certificate(csr, issuer_options)
File "/apps/lemur/lemur/plugins/lemur_verisign/plugin.py", line 156, in create_certificate
response = self.session.post(url, data=data)
File "/apps/python/local/lib/python2.7/site-packages/requests/sessions.py", line 511, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/apps/python/local/lib/python2.7/site-packages/requests/sessions.py", line 454, in request
prep = self.prepare_request(req)
File "/apps/python/local/lib/python2.7/site-packages/requests/sessions.py", line 388, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/apps/python/local/lib/python2.7/site-packages/requests/models.py", line 296, in prepare
self.prepare_body(data, files, json)
File "/apps/python/local/lib/python2.7/site-packages/requests/models.py", line 450, in prepare_body
body = self._encode_params(data)
File "/apps/python/local/lib/python2.7/site-packages/requests/models.py", line 97, in _encode_params
return urlencode(result, doseq=True)
File "/usr/lib/python2.7/urllib.py", line 1338, in urlencode
v = quote_plus(v)
File "/usr/lib/python2.7/urllib.py", line 1293, in quote_plus
s = quote(s, safe + ' ')
File "/usr/lib/python2.7/urllib.py", line 1288, in quote
return ''.join(map(quoter, s))
KeyError: 45

# Fixed code
def mint(issuer_options):
    """
    Minting is slightly different for each authority.
    Support for multiple authorities is handled by individual plugins.

    :param issuer_options:
    """
    authority = issuer_options["authority"]

    issuer = plugins.get(authority.plugin_name)

    # allow the CSR to be specified by the user
    if not issuer_options.get("csr"):
        csr, private_key = create_csr(issuer_options)
    else:
        csr = str(issuer_options.get("csr"))
        private_key = None

    issuer_options["creator"] = g.user.email
    cert_body, cert_chain = issuer.create_certificate(csr, issuer_options)

    cert = Certificate(cert_body, private_key, cert_chain)

    cert.user = g.user
    cert.authority = authority
    database.update(cert)
    return (
        cert,
        private_key,
        cert_chain,
    )